| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef MEDIA_BASE_ANDROID_MEDIA_DRM_BRIDGE_H_ | 5 #ifndef MEDIA_BASE_ANDROID_MEDIA_DRM_BRIDGE_H_ |
| 6 #define MEDIA_BASE_ANDROID_MEDIA_DRM_BRIDGE_H_ | 6 #define MEDIA_BASE_ANDROID_MEDIA_DRM_BRIDGE_H_ |
| 7 | 7 |
| 8 #include <jni.h> | 8 #include <jni.h> |
| 9 #include <string> | 9 #include <string> |
| 10 #include <vector> | 10 #include <vector> |
| 11 | 11 |
| 12 #include "base/android/scoped_java_ref.h" | 12 #include "base/android/scoped_java_ref.h" |
| 13 #include "base/callback.h" | 13 #include "base/callback.h" |
| 14 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
| 15 #include "base/memory/weak_ptr.h" |
| 16 #include "base/synchronization/lock.h" |
| 15 #include "media/base/browser_cdm.h" | 17 #include "media/base/browser_cdm.h" |
| 16 #include "media/base/cdm_promise_adapter.h" | 18 #include "media/base/cdm_promise_adapter.h" |
| 17 #include "media/base/media_export.h" | 19 #include "media/base/media_export.h" |
| 18 #include "media/cdm/player_tracker_impl.h" | 20 #include "media/cdm/player_tracker_impl.h" |
| 19 #include "url/gurl.h" | 21 #include "url/gurl.h" |
| 20 | 22 |
| 21 class GURL; | 23 class GURL; |
| 22 | 24 |
| 23 namespace media { | 25 namespace media { |
| 24 | 26 |
| 25 class MediaPlayerManager; | 27 class MediaPlayerManager; |
| 26 | 28 |
| 27 // This class provides DRM services for android EME implementation. | 29 // This class provides DRM services for android EME implementation. |
| 28 class MEDIA_EXPORT MediaDrmBridge : public BrowserCdm { | 30 class MEDIA_EXPORT MediaDrmBridge : public BrowserCdm { |
| 29 public: | 31 public: |
| 30 // TODO(ddorwin): These are specific to Widevine. http://crbug.com/459400 | 32 // TODO(ddorwin): These are specific to Widevine. http://crbug.com/459400 |
| 31 enum SecurityLevel { | 33 enum SecurityLevel { |
| 32 SECURITY_LEVEL_NONE = 0, | 34 SECURITY_LEVEL_NONE = 0, |
| 33 SECURITY_LEVEL_1 = 1, | 35 SECURITY_LEVEL_1 = 1, |
| 34 SECURITY_LEVEL_3 = 3, | 36 SECURITY_LEVEL_3 = 3, |
| 35 }; | 37 }; |
| 36 | 38 |
| 39 typedef scoped_ptr<base::android::ScopedJavaGlobalRef<jobject>> JavaObjectPtr; |
| 40 |
| 37 typedef base::Callback<void(bool)> ResetCredentialsCB; | 41 typedef base::Callback<void(bool)> ResetCredentialsCB; |
| 38 | 42 |
| 43 // Notification called when MediaCrypto object is ready. |
| 44 // Parameters: |
| 45 // |media_crypto| - global reference to MediaCrypto object |
| 46 // |needs_protected_surface| - true if protected surface is required. |
| 47 typedef base::Callback<void(JavaObjectPtr media_crypto, |
| 48 bool needs_protected_surface)> MediaCryptoReadyCB; |
| 49 |
| 39 ~MediaDrmBridge() override; | 50 ~MediaDrmBridge() override; |
| 40 | 51 |
| 52 void DeleteOnCorrectThread() override; |
| 53 |
| 41 // Checks whether MediaDRM is available. | 54 // Checks whether MediaDRM is available. |
| 42 // All other static methods check IsAvailable() internally. There's no need | 55 // All other static methods check IsAvailable() internally. There's no need |
| 43 // to check IsAvailable() explicitly before calling them. | 56 // to check IsAvailable() explicitly before calling them. |
| 44 static bool IsAvailable(); | 57 static bool IsAvailable(); |
| 45 | 58 |
| 46 // Checks whether |key_system| is supported. | 59 // Checks whether |key_system| is supported. |
| 47 static bool IsKeySystemSupported(const std::string& key_system); | 60 static bool IsKeySystemSupported(const std::string& key_system); |
| 48 | 61 |
| 49 // Returns the list of the platform-supported key system names that | 62 // Returns the list of the platform-supported key system names that |
| 50 // are not handled by Chrome explicitly. | 63 // are not handled by Chrome explicitly. |
| 51 static std::vector<std::string> GetPlatformKeySystemNames(); | 64 static std::vector<std::string> GetPlatformKeySystemNames(); |
| 52 | 65 |
| 53 // Checks whether |key_system| is supported with |container_mime_type|. | 66 // Checks whether |key_system| is supported with |container_mime_type|. |
| 54 // |container_mime_type| must not be empty. | 67 // |container_mime_type| must not be empty. |
| 55 static bool IsKeySystemSupportedWithType( | 68 static bool IsKeySystemSupportedWithType( |
| 56 const std::string& key_system, | 69 const std::string& key_system, |
| 57 const std::string& container_mime_type); | 70 const std::string& container_mime_type); |
| 58 | 71 |
| 59 static bool RegisterMediaDrmBridge(JNIEnv* env); | 72 static bool RegisterMediaDrmBridge(JNIEnv* env); |
| 60 | 73 |
| 61 // Returns a MediaDrmBridge instance if |key_system| is supported, or a NULL | 74 // Returns a MediaDrmBridge instance if |key_system| is supported, or a NULL |
| 62 // pointer otherwise. | 75 // pointer otherwise. |
| 63 // TODO(xhwang): Is it okay not to update session expiration info? | 76 // TODO(xhwang): Is it okay not to update session expiration info? |
| 64 static scoped_ptr<MediaDrmBridge> Create( | 77 static scoped_ptr<MediaDrmBridge, BrowserCdmDeleter> Create( |
| 65 const std::string& key_system, | 78 const std::string& key_system, |
| 66 const SessionMessageCB& session_message_cb, | 79 const SessionMessageCB& session_message_cb, |
| 67 const SessionClosedCB& session_closed_cb, | 80 const SessionClosedCB& session_closed_cb, |
| 68 const LegacySessionErrorCB& legacy_session_error_cb, | 81 const LegacySessionErrorCB& legacy_session_error_cb, |
| 69 const SessionKeysChangeCB& session_keys_change_cb, | 82 const SessionKeysChangeCB& session_keys_change_cb, |
| 70 const SessionExpirationUpdateCB& session_expiration_update_cb); | 83 const SessionExpirationUpdateCB& session_expiration_update_cb); |
| 71 | 84 |
| 72 // Returns a MediaDrmBridge instance if |key_system| is supported, or a NULL | 85 // Returns a MediaDrmBridge instance if |key_system| is supported, or a NULL |
| 73 // otherwise. No session callbacks are provided. This is used when we need to | 86 // otherwise. No session callbacks are provided. This is used when we need to |
| 74 // use MediaDrmBridge without creating any sessions. | 87 // use MediaDrmBridge without creating any sessions. |
| 75 static scoped_ptr<MediaDrmBridge> CreateWithoutSessionSupport( | 88 static scoped_ptr<MediaDrmBridge, BrowserCdmDeleter> |
| 76 const std::string& key_system); | 89 CreateWithoutSessionSupport(const std::string& key_system); |
| 77 | 90 |
| 78 // Returns true if |security_level| is successfully set, or false otherwise. | 91 // Returns true if |security_level| is successfully set, or false otherwise. |
| 79 // Call this function right after Create() and before any other calls. | 92 // Call this function right after Create() and before any other calls. |
| 80 // Note: | 93 // Note: |
| 81 // - If this function is not called, the default security level of the device | 94 // - If this function is not called, the default security level of the device |
| 82 // will be used. | 95 // will be used. |
| 83 // - It's recommended to call this function only once on a MediaDrmBridge | 96 // - It's recommended to call this function only once on a MediaDrmBridge |
| 84 // object. Calling this function multiples times may cause errors. | 97 // object. Calling this function multiples times may cause errors. |
| 85 bool SetSecurityLevel(SecurityLevel security_level); | 98 bool SetSecurityLevel(SecurityLevel security_level); |
| 86 | 99 |
| (...skipping 22 matching lines...) Expand all Loading... |
| 109 int RegisterPlayer(const base::Closure& new_key_cb, | 122 int RegisterPlayer(const base::Closure& new_key_cb, |
| 110 const base::Closure& cdm_unset_cb) override; | 123 const base::Closure& cdm_unset_cb) override; |
| 111 void UnregisterPlayer(int registration_id) override; | 124 void UnregisterPlayer(int registration_id) override; |
| 112 | 125 |
| 113 // Returns a MediaCrypto object if it's already created. Returns a null object | 126 // Returns a MediaCrypto object if it's already created. Returns a null object |
| 114 // otherwise. | 127 // otherwise. |
| 115 base::android::ScopedJavaLocalRef<jobject> GetMediaCrypto(); | 128 base::android::ScopedJavaLocalRef<jobject> GetMediaCrypto(); |
| 116 | 129 |
| 117 // Sets callback which will be called when MediaCrypto is ready. | 130 // Sets callback which will be called when MediaCrypto is ready. |
| 118 // If |closure| is null, previously set callback will be cleared. | 131 // If |closure| is null, previously set callback will be cleared. |
| 119 void SetMediaCryptoReadyCB(const base::Closure& closure); | 132 void SetMediaCryptoReadyCB(const MediaCryptoReadyCB& closure); |
| 120 | 133 |
| 121 // Called after a MediaCrypto object is created. | 134 // Called by Java after a MediaCrypto object is created. |
| 122 void OnMediaCryptoReady(JNIEnv* env, jobject j_media_drm); | 135 void OnMediaCryptoReady(JNIEnv* env, jobject j_media_drm); |
| 123 | 136 |
| 124 // Callbacks to resolve the promise for |promise_id|. | 137 // Callbacks to resolve the promise for |promise_id|. |
| 125 void OnPromiseResolved(JNIEnv* env, jobject j_media_drm, jint j_promise_id); | 138 void OnPromiseResolved(JNIEnv* env, jobject j_media_drm, jint j_promise_id); |
| 126 void OnPromiseResolvedWithSession(JNIEnv* env, | 139 void OnPromiseResolvedWithSession(JNIEnv* env, |
| 127 jobject j_media_drm, | 140 jobject j_media_drm, |
| 128 jint j_promise_id, | 141 jint j_promise_id, |
| 129 jbyteArray j_session_id); | 142 jbyteArray j_session_id); |
| 130 | 143 |
| 131 // Callback to reject the promise for |promise_id| with |error_message|. | 144 // Callback to reject the promise for |promise_id| with |error_message|. |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 179 // Reset the device credentials. | 192 // Reset the device credentials. |
| 180 void ResetDeviceCredentials(const ResetCredentialsCB& callback); | 193 void ResetDeviceCredentials(const ResetCredentialsCB& callback); |
| 181 | 194 |
| 182 // Called by the java object when credential reset is completed. | 195 // Called by the java object when credential reset is completed. |
| 183 void OnResetDeviceCredentialsCompleted(JNIEnv* env, jobject, bool success); | 196 void OnResetDeviceCredentialsCompleted(JNIEnv* env, jobject, bool success); |
| 184 | 197 |
| 185 // Helper function to determine whether a protected surface is needed for the | 198 // Helper function to determine whether a protected surface is needed for the |
| 186 // video playback. | 199 // video playback. |
| 187 bool IsProtectedSurfaceRequired(); | 200 bool IsProtectedSurfaceRequired(); |
| 188 | 201 |
| 202 // We use this pointer when we post SetMediaCryptoReadyCB onto UI thread. |
| 203 base::WeakPtr<MediaDrmBridge> WeakPtrForUIThread(); |
| 204 |
| 189 private: | 205 private: |
| 190 MediaDrmBridge(const std::vector<uint8>& scheme_uuid, | 206 MediaDrmBridge(const std::vector<uint8>& scheme_uuid, |
| 191 const SessionMessageCB& session_message_cb, | 207 const SessionMessageCB& session_message_cb, |
| 192 const SessionClosedCB& session_closed_cb, | 208 const SessionClosedCB& session_closed_cb, |
| 193 const LegacySessionErrorCB& legacy_session_error_cb, | 209 const LegacySessionErrorCB& legacy_session_error_cb, |
| 194 const SessionKeysChangeCB& session_keys_change_cb, | 210 const SessionKeysChangeCB& session_keys_change_cb, |
| 195 const SessionExpirationUpdateCB& session_expiration_update_cb); | 211 const SessionExpirationUpdateCB& session_expiration_update_cb); |
| 196 | 212 |
| 197 static bool IsSecureDecoderRequired(SecurityLevel security_level); | 213 static bool IsSecureDecoderRequired(SecurityLevel security_level); |
| 198 | 214 |
| 199 // Get the security level of the media. | 215 // Get the security level of the media. |
| 200 SecurityLevel GetSecurityLevel(); | 216 SecurityLevel GetSecurityLevel(); |
| 201 | 217 |
| 218 // A helper method used to relay |player_tracker_| access to the Media thread. |
| 219 void InternalKeysAdded(bool has_additional_usable_key); |
| 220 |
| 221 // A helper method that posts or runs |media_crypto_ready_cb_|. |
| 222 void PostMediaCryptoReadyOnCorrectThread(const MediaCryptoReadyCB& cb, |
| 223 bool always_post_on_ui_thread); |
| 224 |
| 202 // UUID of the key system. | 225 // UUID of the key system. |
| 203 std::vector<uint8> scheme_uuid_; | 226 std::vector<uint8> scheme_uuid_; |
| 204 | 227 |
| 205 // Java MediaDrm instance. | 228 // Java MediaDrm instance. |
| 206 base::android::ScopedJavaGlobalRef<jobject> j_media_drm_; | 229 base::android::ScopedJavaGlobalRef<jobject> j_media_drm_; |
| 207 | 230 |
| 208 // Callbacks for firing session events. | 231 // Callbacks for firing session events. |
| 209 SessionMessageCB session_message_cb_; | 232 SessionMessageCB session_message_cb_; |
| 210 SessionClosedCB session_closed_cb_; | 233 SessionClosedCB session_closed_cb_; |
| 211 LegacySessionErrorCB legacy_session_error_cb_; | 234 LegacySessionErrorCB legacy_session_error_cb_; |
| 212 SessionKeysChangeCB session_keys_change_cb_; | 235 SessionKeysChangeCB session_keys_change_cb_; |
| 213 SessionExpirationUpdateCB session_expiration_update_cb_; | 236 SessionExpirationUpdateCB session_expiration_update_cb_; |
| 214 | 237 |
| 215 base::Closure media_crypto_ready_cb_; | 238 MediaCryptoReadyCB media_crypto_ready_cb_; |
| 216 | 239 |
| 217 ResetCredentialsCB reset_credentials_cb_; | 240 ResetCredentialsCB reset_credentials_cb_; |
| 218 | 241 |
| 219 PlayerTrackerImpl player_tracker_; | 242 PlayerTrackerImpl player_tracker_; |
| 220 | 243 |
| 221 CdmPromiseAdapter cdm_promise_adapter_; | 244 CdmPromiseAdapter cdm_promise_adapter_; |
| 222 | 245 |
| 246 // Callback for posting Java notifications to Media thread. |
| 247 typedef base::Callback<void(bool)> KeysAddedCallback; |
| 248 KeysAddedCallback internal_keys_added_cb_; |
| 249 |
| 250 // Object for posting tasks on UI thread. |
| 251 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner_; |
| 252 |
| 253 // This flag is set when we use media thread for certain callbacks. |
| 254 bool use_media_thread_; |
| 255 |
| 256 // NOTE: Weak pointers must be invalidated before all other member variables. |
| 257 base::WeakPtrFactory<MediaDrmBridge> media_weak_factory_; |
| 258 base::WeakPtrFactory<MediaDrmBridge> ui_weak_factory_; |
| 259 |
| 223 DISALLOW_COPY_AND_ASSIGN(MediaDrmBridge); | 260 DISALLOW_COPY_AND_ASSIGN(MediaDrmBridge); |
| 224 }; | 261 }; |
| 225 | 262 |
| 226 } // namespace media | 263 } // namespace media |
| 227 | 264 |
| 228 #endif // MEDIA_BASE_ANDROID_MEDIA_DRM_BRIDGE_H_ | 265 #endif // MEDIA_BASE_ANDROID_MEDIA_DRM_BRIDGE_H_ |
| OLD | NEW |