| 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" | 15 #include "base/memory/weak_ptr.h" |
| 16 #include "media/base/browser_cdm.h" | |
| 17 #include "media/base/cdm_promise_adapter.h" | 16 #include "media/base/cdm_promise_adapter.h" |
| 18 #include "media/base/media_export.h" | 17 #include "media/base/media_export.h" |
| 18 #include "media/base/media_keys.h" |
| 19 #include "media/base/player_tracker.h" |
| 19 #include "media/cdm/player_tracker_impl.h" | 20 #include "media/cdm/player_tracker_impl.h" |
| 20 #include "url/gurl.h" | 21 #include "url/gurl.h" |
| 21 | 22 |
| 22 class GURL; | 23 class GURL; |
| 23 | 24 |
| 24 namespace media { | 25 namespace media { |
| 25 | 26 |
| 26 class MediaDrmBridge; | 27 // Implements a CDM using Android MediaDrm API. |
| 28 // |
| 29 // Thread Safety: |
| 30 // |
| 31 // This class lives on the thread where it is created. All methods must be |
| 32 // called on the |task_runner_| except for the PlayerTracker methods and |
| 33 // SetMediaCryptoReadyCB(), which can be called on any thread. |
| 27 | 34 |
| 28 using ScopedMediaDrmBridgePtr = scoped_ptr<MediaDrmBridge, BrowserCdmDeleter>; | 35 class MEDIA_EXPORT MediaDrmBridge : public MediaKeys, public PlayerTracker { |
| 29 | |
| 30 // This class provides DRM services for android EME implementation. | |
| 31 class MEDIA_EXPORT MediaDrmBridge : public BrowserCdm { | |
| 32 public: | 36 public: |
| 33 // TODO(ddorwin): These are specific to Widevine. http://crbug.com/459400 | 37 // TODO(ddorwin): These are specific to Widevine. http://crbug.com/459400 |
| 34 enum SecurityLevel { | 38 enum SecurityLevel { |
| 35 SECURITY_LEVEL_NONE = 0, | 39 SECURITY_LEVEL_NONE = 0, |
| 36 SECURITY_LEVEL_1 = 1, | 40 SECURITY_LEVEL_1 = 1, |
| 37 SECURITY_LEVEL_3 = 3, | 41 SECURITY_LEVEL_3 = 3, |
| 38 }; | 42 }; |
| 39 | 43 |
| 40 using JavaObjectPtr = scoped_ptr<base::android::ScopedJavaGlobalRef<jobject>>; | 44 using JavaObjectPtr = scoped_ptr<base::android::ScopedJavaGlobalRef<jobject>>; |
| 41 | 45 |
| 42 using ResetCredentialsCB = base::Callback<void(bool)>; | 46 using ResetCredentialsCB = base::Callback<void(bool)>; |
| 43 | 47 |
| 44 // Notification called when MediaCrypto object is ready. | 48 // Notification called when MediaCrypto object is ready. |
| 45 // Parameters: | 49 // Parameters: |
| 46 // |media_crypto| - global reference to MediaCrypto object | 50 // |media_crypto| - global reference to MediaCrypto object |
| 47 // |needs_protected_surface| - true if protected surface is required. | 51 // |needs_protected_surface| - true if protected surface is required. |
| 48 using MediaCryptoReadyCB = base::Callback<void(JavaObjectPtr media_crypto, | 52 using MediaCryptoReadyCB = base::Callback<void(JavaObjectPtr media_crypto, |
| 49 bool needs_protected_surface)>; | 53 bool needs_protected_surface)>; |
| 50 | 54 |
| 51 ~MediaDrmBridge() override; | |
| 52 | |
| 53 void DeleteOnCorrectThread() override; | |
| 54 | |
| 55 // Checks whether MediaDRM is available. | 55 // Checks whether MediaDRM is available. |
| 56 // All other static methods check IsAvailable() internally. There's no need | 56 // All other static methods check IsAvailable() internally. There's no need |
| 57 // to check IsAvailable() explicitly before calling them. | 57 // to check IsAvailable() explicitly before calling them. |
| 58 static bool IsAvailable(); | 58 static bool IsAvailable(); |
| 59 | 59 |
| 60 static bool RegisterMediaDrmBridge(JNIEnv* env); | 60 static bool RegisterMediaDrmBridge(JNIEnv* env); |
| 61 | 61 |
| 62 // Checks whether |key_system| is supported. | 62 // Checks whether |key_system| is supported. |
| 63 static bool IsKeySystemSupported(const std::string& key_system); | 63 static bool IsKeySystemSupported(const std::string& key_system); |
| 64 | 64 |
| 65 // Checks whether |key_system| is supported with |container_mime_type|. | 65 // Checks whether |key_system| is supported with |container_mime_type|. |
| 66 // |container_mime_type| must not be empty. | 66 // |container_mime_type| must not be empty. |
| 67 static bool IsKeySystemSupportedWithType( | 67 static bool IsKeySystemSupportedWithType( |
| 68 const std::string& key_system, | 68 const std::string& key_system, |
| 69 const std::string& container_mime_type); | 69 const std::string& container_mime_type); |
| 70 | 70 |
| 71 // Returns the list of the platform-supported key system names that | 71 // Returns the list of the platform-supported key system names that |
| 72 // are not handled by Chrome explicitly. | 72 // are not handled by Chrome explicitly. |
| 73 static std::vector<std::string> GetPlatformKeySystemNames(); | 73 static std::vector<std::string> GetPlatformKeySystemNames(); |
| 74 | 74 |
| 75 // Returns a MediaDrmBridge instance if |key_system| is supported, or a NULL | 75 // Returns a MediaDrmBridge instance if |key_system| is supported, or a NULL |
| 76 // pointer otherwise. | 76 // pointer otherwise. |
| 77 // TODO(xhwang): Is it okay not to update session expiration info? | 77 // TODO(xhwang): Is it okay not to update session expiration info? |
| 78 static ScopedMediaDrmBridgePtr Create( | 78 static scoped_refptr<MediaDrmBridge> Create( |
| 79 const std::string& key_system, | 79 const std::string& key_system, |
| 80 const SessionMessageCB& session_message_cb, | 80 const SessionMessageCB& session_message_cb, |
| 81 const SessionClosedCB& session_closed_cb, | 81 const SessionClosedCB& session_closed_cb, |
| 82 const LegacySessionErrorCB& legacy_session_error_cb, | 82 const LegacySessionErrorCB& legacy_session_error_cb, |
| 83 const SessionKeysChangeCB& session_keys_change_cb, | 83 const SessionKeysChangeCB& session_keys_change_cb, |
| 84 const SessionExpirationUpdateCB& session_expiration_update_cb); | 84 const SessionExpirationUpdateCB& session_expiration_update_cb); |
| 85 | 85 |
| 86 // Returns a MediaDrmBridge instance if |key_system| is supported, or a NULL | 86 // Returns a MediaDrmBridge instance if |key_system| is supported, or a NULL |
| 87 // otherwise. No session callbacks are provided. This is used when we need to | 87 // otherwise. No session callbacks are provided. This is used when we need to |
| 88 // use MediaDrmBridge without creating any sessions. | 88 // use MediaDrmBridge without creating any sessions. |
| 89 static ScopedMediaDrmBridgePtr CreateWithoutSessionSupport( | 89 static scoped_refptr<MediaDrmBridge> CreateWithoutSessionSupport( |
| 90 const std::string& key_system); | 90 const std::string& key_system); |
| 91 | 91 |
| 92 // Returns a WeakPtr to be used on the |task_runner_|. | 92 // MediaKeys implementation. |
| 93 base::WeakPtr<MediaDrmBridge> WeakPtr(); | |
| 94 | |
| 95 // MediaKeys (via BrowserCdm) implementation. | |
| 96 void SetServerCertificate( | 93 void SetServerCertificate( |
| 97 const std::vector<uint8_t>& certificate, | 94 const std::vector<uint8_t>& certificate, |
| 98 scoped_ptr<media::SimpleCdmPromise> promise) override; | 95 scoped_ptr<media::SimpleCdmPromise> promise) override; |
| 99 void CreateSessionAndGenerateRequest( | 96 void CreateSessionAndGenerateRequest( |
| 100 SessionType session_type, | 97 SessionType session_type, |
| 101 media::EmeInitDataType init_data_type, | 98 media::EmeInitDataType init_data_type, |
| 102 const std::vector<uint8_t>& init_data, | 99 const std::vector<uint8_t>& init_data, |
| 103 scoped_ptr<media::NewSessionCdmPromise> promise) override; | 100 scoped_ptr<media::NewSessionCdmPromise> promise) override; |
| 104 void LoadSession(SessionType session_type, | 101 void LoadSession(SessionType session_type, |
| 105 const std::string& session_id, | 102 const std::string& session_id, |
| 106 scoped_ptr<media::NewSessionCdmPromise> promise) override; | 103 scoped_ptr<media::NewSessionCdmPromise> promise) override; |
| 107 void UpdateSession(const std::string& session_id, | 104 void UpdateSession(const std::string& session_id, |
| 108 const std::vector<uint8_t>& response, | 105 const std::vector<uint8_t>& response, |
| 109 scoped_ptr<media::SimpleCdmPromise> promise) override; | 106 scoped_ptr<media::SimpleCdmPromise> promise) override; |
| 110 void CloseSession(const std::string& session_id, | 107 void CloseSession(const std::string& session_id, |
| 111 scoped_ptr<media::SimpleCdmPromise> promise) override; | 108 scoped_ptr<media::SimpleCdmPromise> promise) override; |
| 112 void RemoveSession(const std::string& session_id, | 109 void RemoveSession(const std::string& session_id, |
| 113 scoped_ptr<media::SimpleCdmPromise> promise) override; | 110 scoped_ptr<media::SimpleCdmPromise> promise) override; |
| 114 CdmContext* GetCdmContext() override; | 111 CdmContext* GetCdmContext() override; |
| 112 void DeleteOnCorrectThread() const override; |
| 115 | 113 |
| 116 // PlayerTracker (via BrowserCdm) implementation. | 114 // PlayerTracker implementation. Can be called on any thread. |
| 115 // The registered callbacks will be fired on |task_runner_|. The caller |
| 116 // should make sure that the callbacks are posted to the correct thread. |
| 117 // |
| 118 // Note: RegisterPlayer() should be called before SetMediaCryptoReadyCB() to |
| 119 // avoid missing any new key notifications. |
| 117 int RegisterPlayer(const base::Closure& new_key_cb, | 120 int RegisterPlayer(const base::Closure& new_key_cb, |
| 118 const base::Closure& cdm_unset_cb) override; | 121 const base::Closure& cdm_unset_cb) override; |
| 119 void UnregisterPlayer(int registration_id) override; | 122 void UnregisterPlayer(int registration_id) override; |
| 120 | 123 |
| 121 // Returns true if |security_level| is successfully set, or false otherwise. | 124 // Returns true if |security_level| is successfully set, or false otherwise. |
| 122 // Call this function right after Create() and before any other calls. | 125 // Call this function right after Create() and before any other calls. |
| 123 // Note: | 126 // Note: |
| 124 // - If this function is not called, the default security level of the device | 127 // - If this function is not called, the default security level of the device |
| 125 // will be used. | 128 // will be used. |
| 126 // - It's recommended to call this function only once on a MediaDrmBridge | 129 // - It's recommended to call this function only once on a MediaDrmBridge |
| (...skipping 10 matching lines...) Expand all Loading... |
| 137 // Helper functions to resolve promises. | 140 // Helper functions to resolve promises. |
| 138 void ResolvePromise(uint32_t promise_id); | 141 void ResolvePromise(uint32_t promise_id); |
| 139 void ResolvePromiseWithSession(uint32_t promise_id, | 142 void ResolvePromiseWithSession(uint32_t promise_id, |
| 140 const std::string& session_id); | 143 const std::string& session_id); |
| 141 void RejectPromise(uint32_t promise_id, const std::string& error_message); | 144 void RejectPromise(uint32_t promise_id, const std::string& error_message); |
| 142 | 145 |
| 143 // Returns a MediaCrypto object if it's already created. Returns a null object | 146 // Returns a MediaCrypto object if it's already created. Returns a null object |
| 144 // otherwise. | 147 // otherwise. |
| 145 base::android::ScopedJavaLocalRef<jobject> GetMediaCrypto(); | 148 base::android::ScopedJavaLocalRef<jobject> GetMediaCrypto(); |
| 146 | 149 |
| 147 // Sets callback which will be called when MediaCrypto is ready. If | 150 // Registers a callback which will be called when MediaCrypto is ready. |
| 148 // |media_crypto_ready_cb| is null, previously set callback will be cleared. | 151 // Can be called on any thread. Only one callback should be registered. |
| 152 // The registered callbacks will be fired on |task_runner_|. The caller |
| 153 // should make sure that the callbacks are posted to the correct thread. |
| 154 // TODO(xhwang): Move this up to be close to RegisterPlayer(). |
| 149 void SetMediaCryptoReadyCB(const MediaCryptoReadyCB& media_crypto_ready_cb); | 155 void SetMediaCryptoReadyCB(const MediaCryptoReadyCB& media_crypto_ready_cb); |
| 150 | 156 |
| 151 // All the OnXxx functions below are called from Java. The implementation must | 157 // All the OnXxx functions below are called from Java. The implementation must |
| 152 // only do minimal work and then post tasks to avoid reentrancy issues. | 158 // only do minimal work and then post tasks to avoid reentrancy issues. |
| 153 | 159 |
| 154 // Called by Java after a MediaCrypto object is created. | 160 // Called by Java after a MediaCrypto object is created. |
| 155 void OnMediaCryptoReady(JNIEnv* env, jobject j_media_drm); | 161 void OnMediaCryptoReady(JNIEnv* env, jobject j_media_drm); |
| 156 | 162 |
| 157 // Callbacks to resolve the promise for |promise_id|. | 163 // Callbacks to resolve the promise for |promise_id|. |
| 158 void OnPromiseResolved(JNIEnv* env, jobject j_media_drm, jint j_promise_id); | 164 void OnPromiseResolved(JNIEnv* env, jobject j_media_drm, jint j_promise_id); |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 205 // in this method should probably also be reported by one of other methods. | 211 // in this method should probably also be reported by one of other methods. |
| 206 void OnLegacySessionError(JNIEnv* env, | 212 void OnLegacySessionError(JNIEnv* env, |
| 207 jobject j_media_drm, | 213 jobject j_media_drm, |
| 208 jbyteArray j_session_id, | 214 jbyteArray j_session_id, |
| 209 jstring j_error_message); | 215 jstring j_error_message); |
| 210 | 216 |
| 211 // Called by the java object when credential reset is completed. | 217 // Called by the java object when credential reset is completed. |
| 212 void OnResetDeviceCredentialsCompleted(JNIEnv* env, jobject, bool success); | 218 void OnResetDeviceCredentialsCompleted(JNIEnv* env, jobject, bool success); |
| 213 | 219 |
| 214 private: | 220 private: |
| 221 // For DeleteSoon() in DeleteOnCorrectThread(). |
| 222 friend class base::DeleteHelper<MediaDrmBridge>; |
| 223 |
| 215 MediaDrmBridge(const std::vector<uint8>& scheme_uuid, | 224 MediaDrmBridge(const std::vector<uint8>& scheme_uuid, |
| 216 const SessionMessageCB& session_message_cb, | 225 const SessionMessageCB& session_message_cb, |
| 217 const SessionClosedCB& session_closed_cb, | 226 const SessionClosedCB& session_closed_cb, |
| 218 const LegacySessionErrorCB& legacy_session_error_cb, | 227 const LegacySessionErrorCB& legacy_session_error_cb, |
| 219 const SessionKeysChangeCB& session_keys_change_cb, | 228 const SessionKeysChangeCB& session_keys_change_cb, |
| 220 const SessionExpirationUpdateCB& session_expiration_update_cb); | 229 const SessionExpirationUpdateCB& session_expiration_update_cb); |
| 221 | 230 |
| 231 ~MediaDrmBridge() override; |
| 232 |
| 222 static bool IsSecureDecoderRequired(SecurityLevel security_level); | 233 static bool IsSecureDecoderRequired(SecurityLevel security_level); |
| 223 | 234 |
| 224 // Get the security level of the media. | 235 // Get the security level of the media. |
| 225 SecurityLevel GetSecurityLevel(); | 236 SecurityLevel GetSecurityLevel(); |
| 226 | 237 |
| 227 // A helper method that calls a |player_tracker_| method on correct thread. | |
| 228 void NotifyNewKeyOnCorrectThread(); | |
| 229 | |
| 230 // A helper method that calculates the |media_crypto_ready_cb_| arguments and | 238 // A helper method that calculates the |media_crypto_ready_cb_| arguments and |
| 231 // run this callback. | 239 // run this callback. |
| 232 void NotifyMediaCryptoReady(const MediaCryptoReadyCB& cb); | 240 void NotifyMediaCryptoReady(const MediaCryptoReadyCB& cb); |
| 233 | 241 |
| 234 // UUID of the key system. | 242 // UUID of the key system. |
| 235 std::vector<uint8> scheme_uuid_; | 243 std::vector<uint8> scheme_uuid_; |
| 236 | 244 |
| 237 // Java MediaDrm instance. | 245 // Java MediaDrm instance. |
| 238 base::android::ScopedJavaGlobalRef<jobject> j_media_drm_; | 246 base::android::ScopedJavaGlobalRef<jobject> j_media_drm_; |
| 239 | 247 |
| 240 // Callbacks for firing session events. | 248 // Callbacks for firing session events. |
| 241 SessionMessageCB session_message_cb_; | 249 SessionMessageCB session_message_cb_; |
| 242 SessionClosedCB session_closed_cb_; | 250 SessionClosedCB session_closed_cb_; |
| 243 LegacySessionErrorCB legacy_session_error_cb_; | 251 LegacySessionErrorCB legacy_session_error_cb_; |
| 244 SessionKeysChangeCB session_keys_change_cb_; | 252 SessionKeysChangeCB session_keys_change_cb_; |
| 245 SessionExpirationUpdateCB session_expiration_update_cb_; | 253 SessionExpirationUpdateCB session_expiration_update_cb_; |
| 246 | 254 |
| 247 MediaCryptoReadyCB media_crypto_ready_cb_; | 255 MediaCryptoReadyCB media_crypto_ready_cb_; |
| 248 | 256 |
| 249 ResetCredentialsCB reset_credentials_cb_; | 257 ResetCredentialsCB reset_credentials_cb_; |
| 250 | 258 |
| 251 // The |player_tracker_| must be accessed by one thread only. It is accessed | |
| 252 // by the Media thread when |use_media_thread_| is true. | |
| 253 PlayerTrackerImpl player_tracker_; | 259 PlayerTrackerImpl player_tracker_; |
| 254 | 260 |
| 261 // TODO(xhwang): Host a CdmPromiseAdapter directly. No need to use scoped_ptr. |
| 255 scoped_ptr<CdmPromiseAdapter> cdm_promise_adapter_; | 262 scoped_ptr<CdmPromiseAdapter> cdm_promise_adapter_; |
| 256 | 263 |
| 257 // Default task runner. | 264 // Default task runner. |
| 258 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | 265 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
| 259 | 266 |
| 260 // This flag is set when we use media thread for certain callbacks. | |
| 261 const bool use_media_thread_; | |
| 262 | |
| 263 // NOTE: Weak pointers must be invalidated before all other member variables. | 267 // NOTE: Weak pointers must be invalidated before all other member variables. |
| 264 | |
| 265 // WeakPtrFactory to generate weak pointers to be used on the media thread. | |
| 266 base::WeakPtrFactory<MediaDrmBridge> media_weak_factory_; | |
| 267 | |
| 268 // Default WeakPtrFactory. | |
| 269 base::WeakPtrFactory<MediaDrmBridge> weak_factory_; | 268 base::WeakPtrFactory<MediaDrmBridge> weak_factory_; |
| 270 | 269 |
| 271 DISALLOW_COPY_AND_ASSIGN(MediaDrmBridge); | 270 DISALLOW_COPY_AND_ASSIGN(MediaDrmBridge); |
| 272 }; | 271 }; |
| 273 | 272 |
| 274 } // namespace media | 273 } // namespace media |
| 275 | 274 |
| 276 #endif // MEDIA_BASE_ANDROID_MEDIA_DRM_BRIDGE_H_ | 275 #endif // MEDIA_BASE_ANDROID_MEDIA_DRM_BRIDGE_H_ |
| OLD | NEW |