Search code examples
javaandroiddrmwidevine

How To Integrate Widevine DRM In My Android App To Secure Videos


I'm building an android app where people can host videos and I want people to be able to stream and download DRM protected videos on their android devices.

I've done a lot of research on widevine and I understand that its google's main choice for DRM.

But I have found very little guide on how to integrate the library in an android app and how to go about getting the license and keys for each of the videos.

I've set up other parts of the app, but I'm honestly at loss on where to start integrating widevine. I've checked how to play the videos using exoplayer , but my problem is encrypting the videos and allowing people download the encrypted form with DRM.

Can anyone help?


Solution

  • Widevine will already be deployed on the Android device (in nearly all cases) so you just need to use it in your app.

    Assuming you are playing back DRM protected videos you will likely be using ExoPlayer.

    ExoPlayer provides documentation on DRM including Widevine integration:

    Possibly even more useful is the ExoPlayer demo player, which includes examples of Widevine protected content playback with full source code. The player looks like:

    enter image description here

    The source code is available to use or modify on the ExoPlayer GitHub at Demos/Main: https://github.com/google/ExoPlayer/tree/release-v2/demos/main

    If you look in media.exolist.json, which is used for config, you'll see some config related to the Widevine protected content - e.g.:

     "name": "Widevine DASH (MP4, H264)",
        "samples": [
          {
            "name": "HD (cenc)",
            "uri": "https://storage.googleapis.com/wvmedia/cenc/h264/tears/tears.mpd",
            "drm_scheme": "widevine",
            "drm_license_uri": "https://proxy.uat.widevine.com/proxy?video_id=2015_tears&provider=widevine_test"
          },
    

    You can see how the DRM is set up in the PlayerActivity in the demo for each MediaItem (https://github.com/google/ExoPlayer/blob/release-v2/demos/main/src/main/java/com/google/android/exoplayer2/demo/PlayerActivity.java):

    MediaItem.DrmConfiguration drmConfiguration = mediaItem.localConfiguration.drmConfiguration;
          if (drmConfiguration != null) {
            if (Util.SDK_INT < 18) {
              showToast(R.string.error_drm_unsupported_before_api_18);
              finish();
              return Collections.emptyList();
            } else if (!FrameworkMediaDrm.isCryptoSchemeSupported(drmConfiguration.scheme)) {
              showToast(R.string.error_drm_unsupported_scheme);
              finish();
              return Collections.emptyList();
            }
          }
    

    A quick way to test your content outside of your own app is to add it to the list in the media.exolist.json, or modify an existing entry with your video manifest and license server URL, and then compile and run the demo again.

    On the server side, to encrypt and stream your video, there are several open source packagers available which you can use to produce HLS or DASH video streams, and which will support the major DRM's, Widevine, PlayReady and FairPlay.

    For your case, Shaka Packager, also provided by Google, include instructions for including Widevine protection in your streamed video - see here: