Junburg
Junburg

Reputation: 360

Using ExoPlayer Input stream with own encryption logic

I am now trying to play an encrypted video(mp4) complete with my own logic. It takes too much time to play back the decoded file because it is too large to create and play. So, what I have found is how to play it while decrypting it with InputStream using ExoPlayer. But it's too difficult at my level to apply it. When I was worried for two days, I had a night, but I still do not see any results. So I ask for help here.

What I am looking for is a reference that can be helpful. I must accept and decode the buffer size (4096). I do not know where to write this code.

And the flow to complete the function I think is as follows. 1. Complete the ExoPlayer UI. 2. Encrypt the downloaded file using my encryption logic. (buffer size is 4096) 3. InputStream receives the file, decodes it at the same time, and plays it. (streaming)

I will do it somehow until 1 and 2, but 3 is very difficult for me. Do you have any specific code and explanation? If you know anyone, please give me a favor. Thank you.

 try {
        ios = new FileInputStream(params[0]);
        fos = context.openFileOutput(params[1] + ".mp4", MODE_PRIVATE);

        ScatteringByteChannel sbc = ios.getChannel();
        GatheringByteChannel gbc = fos.getChannel();

        File file = new File(params[0]);
        fileLength = file.length();

        startTime = System.currentTimeMillis();
        int read = 0;
        readb = 0;
        ByteBuffer bb = ByteBuffer.allocate(4096);
        while ((read = sbc.read(bb)) != -1) {
            bb.flip();
            gbc.write(ByteBuffer.wrap(enDecryptVideo.combineByteArray(bb.array())));
            bb.clear();
            readb += read;
            if (readb % (4096  * 1024 * 3) == 0){
                publishProgress(((int) ( readb * 100 / fileLength)));
            } else if (readb == fileLength) {
                publishProgress(101);
            }
        }



        ios.close();
        fos.close();
    } catch (Exception e) {
        e.getMessage();
    } finally {
        Log.d(TAG, "doInBackground: " + (System.currentTimeMillis() - startTime));
    }

This is my code when I use File play. The above code is the code I used when I made a decoded file and played it. Now I have to play back at the same time as decoding. It does not create a file. I am very eager. Because I have been working for a month since I started work, but I have received something that does not fit my level. But I really want to hit this target... Teach me please.

Upvotes: 1

Views: 525

Answers (1)

Mick
Mick

Reputation: 25491

You can actually leverage the platforms inbuilt encryption functionality for streamed video, either using a commercial DRM or using a 'clear key' encryption.

If these meet your needs it should much easier to work with as you won't have to implement the encryption and decryption yourself.

This answer provides an example for creating both an HLS / AES stream and a DASH clearkey stream:

This does not provide the same security as DRM, as the keys themselves are not encrypted, but it may be sufficient for your needs.

These streams can then be played with the standard iOS, Android or HTML5 players.

Upvotes: 1

Related Questions