Home | History | Annotate | Download | only in android
      1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #ifndef MEDIA_BASE_ANDROID_MEDIA_DRM_BRIDGE_H_
      6 #define MEDIA_BASE_ANDROID_MEDIA_DRM_BRIDGE_H_
      7 
      8 #include <jni.h>
      9 #include <string>
     10 #include <vector>
     11 
     12 #include "base/android/scoped_java_ref.h"
     13 #include "base/callback.h"
     14 #include "base/memory/scoped_ptr.h"
     15 #include "media/base/browser_cdm.h"
     16 #include "media/base/media_export.h"
     17 #include "media/cdm/player_tracker_impl.h"
     18 #include "url/gurl.h"
     19 
     20 class GURL;
     21 
     22 namespace media {
     23 
     24 class MediaPlayerManager;
     25 
     26 // This class provides DRM services for android EME implementation.
     27 // TODO(qinmin): implement all the functions in this class.
     28 class MEDIA_EXPORT MediaDrmBridge : public BrowserCdm {
     29  public:
     30   enum SecurityLevel {
     31     SECURITY_LEVEL_NONE = 0,
     32     SECURITY_LEVEL_1 = 1,
     33     SECURITY_LEVEL_3 = 3,
     34   };
     35 
     36   typedef base::Callback<void(bool)> ResetCredentialsCB;
     37 
     38   virtual ~MediaDrmBridge();
     39 
     40   // Checks whether MediaDRM is available.
     41   // All other static methods check IsAvailable() internally. There's no need
     42   // to check IsAvailable() explicitly before calling them.
     43   static bool IsAvailable();
     44 
     45   static bool IsSecurityLevelSupported(const std::string& key_system,
     46                                        SecurityLevel security_level);
     47 
     48   // Checks whether |key_system| is supported.
     49   static bool IsKeySystemSupported(const std::string& key_system);
     50 
     51   // Returns the list of the platform-supported key system names that
     52   // are not handled by Chrome explicitly.
     53   static std::vector<std::string> GetPlatformKeySystemNames();
     54 
     55   // Checks whether |key_system| is supported with |container_mime_type|.
     56   // |container_mime_type| must not be empty.
     57   static bool IsKeySystemSupportedWithType(
     58       const std::string& key_system,
     59       const std::string& container_mime_type);
     60 
     61   static bool IsSecureDecoderRequired(SecurityLevel security_level);
     62 
     63   static bool RegisterMediaDrmBridge(JNIEnv* env);
     64 
     65   // Returns a MediaDrmBridge instance if |key_system| is supported, or a NULL
     66   // pointer otherwise.
     67   static scoped_ptr<MediaDrmBridge> Create(
     68       const std::string& key_system,
     69       const SessionCreatedCB& session_created_cb,
     70       const SessionMessageCB& session_message_cb,
     71       const SessionReadyCB& session_ready_cb,
     72       const SessionClosedCB& session_closed_cb,
     73       const SessionErrorCB& session_error_cb);
     74 
     75   // Returns a MediaDrmBridge instance if |key_system| is supported, or a NULL
     76   // otherwise. No session callbacks are provided. This is used when we need to
     77   // use MediaDrmBridge without creating any sessions.
     78   static scoped_ptr<MediaDrmBridge> CreateSessionless(
     79       const std::string& key_system);
     80 
     81   // Returns true if |security_level| is successfully set, or false otherwise.
     82   // Call this function right after Create() and before any other calls.
     83   // Note:
     84   // - If this function is not called, the default security level of the device
     85   //   will be used.
     86   // - It's recommended to call this function only once on a MediaDrmBridge
     87   //   object. Calling this function multiples times may cause errors.
     88   bool SetSecurityLevel(SecurityLevel security_level);
     89 
     90   // BrowserCdm implementations.
     91   virtual bool CreateSession(uint32 session_id,
     92                              const std::string& content_type,
     93                              const uint8* init_data,
     94                              int init_data_length) OVERRIDE;
     95   virtual void LoadSession(uint32 session_id,
     96                            const std::string& web_session_id) OVERRIDE;
     97   virtual void UpdateSession(uint32 session_id,
     98                              const uint8* response,
     99                              int response_length) OVERRIDE;
    100   virtual void ReleaseSession(uint32 session_id) OVERRIDE;
    101   virtual int RegisterPlayer(const base::Closure& new_key_cb,
    102                              const base::Closure& cdm_unset_cb) OVERRIDE;
    103   virtual void UnregisterPlayer(int registration_id) OVERRIDE;
    104 
    105   // Returns a MediaCrypto object if it's already created. Returns a null object
    106   // otherwise.
    107   base::android::ScopedJavaLocalRef<jobject> GetMediaCrypto();
    108 
    109   // Sets callback which will be called when MediaCrypto is ready.
    110   // If |closure| is null, previously set callback will be cleared.
    111   void SetMediaCryptoReadyCB(const base::Closure& closure);
    112 
    113   // Called after a MediaCrypto object is created.
    114   void OnMediaCryptoReady(JNIEnv* env, jobject j_media_drm);
    115 
    116   // Callbacks for firing session events.
    117   void OnSessionCreated(JNIEnv* env,
    118                         jobject j_media_drm,
    119                         jint j_session_id,
    120                         jstring j_web_session_id);
    121   void OnSessionMessage(JNIEnv* env,
    122                         jobject j_media_drm,
    123                         jint j_session_id,
    124                         jbyteArray j_message,
    125                         jstring j_destination_url);
    126   void OnSessionReady(JNIEnv* env, jobject j_media_drm, jint j_session_id);
    127   void OnSessionClosed(JNIEnv* env, jobject j_media_drm, jint j_session_id);
    128   void OnSessionError(JNIEnv* env, jobject j_media_drm, jint j_session_id);
    129 
    130   // Reset the device credentials.
    131   void ResetDeviceCredentials(const ResetCredentialsCB& callback);
    132 
    133   // Called by the java object when credential reset is completed.
    134   void OnResetDeviceCredentialsCompleted(JNIEnv* env, jobject, bool success);
    135 
    136   // Helper function to determine whether a protected surface is needed for the
    137   // video playback.
    138   bool IsProtectedSurfaceRequired();
    139 
    140  private:
    141   MediaDrmBridge(const std::vector<uint8>& scheme_uuid,
    142                  const SessionCreatedCB& session_created_cb,
    143                  const SessionMessageCB& session_message_cb,
    144                  const SessionReadyCB& session_ready_cb,
    145                  const SessionClosedCB& session_closed_cb,
    146                  const SessionErrorCB& session_error_cb);
    147 
    148   // Get the security level of the media.
    149   SecurityLevel GetSecurityLevel();
    150 
    151   // UUID of the key system.
    152   std::vector<uint8> scheme_uuid_;
    153 
    154   // Java MediaDrm instance.
    155   base::android::ScopedJavaGlobalRef<jobject> j_media_drm_;
    156 
    157   // Callbacks for firing session events.
    158   SessionCreatedCB session_created_cb_;
    159   SessionMessageCB session_message_cb_;
    160   SessionReadyCB session_ready_cb_;
    161   SessionClosedCB session_closed_cb_;
    162   SessionErrorCB session_error_cb_;
    163 
    164   base::Closure media_crypto_ready_cb_;
    165 
    166   ResetCredentialsCB reset_credentials_cb_;
    167 
    168   PlayerTrackerImpl player_tracker_;
    169 
    170   DISALLOW_COPY_AND_ASSIGN(MediaDrmBridge);
    171 };
    172 
    173 }  // namespace media
    174 
    175 #endif  // MEDIA_BASE_ANDROID_MEDIA_DRM_BRIDGE_H_
    176