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; |
| 42 typedef base::Callback<void(JavaObjectPtr)> MediaCryptoReadyCB; |
38 | 43 |
39 ~MediaDrmBridge() override; | 44 ~MediaDrmBridge() override; |
40 | 45 |
| 46 void DeleteOnCorrectThread() override; |
| 47 |
41 // Checks whether MediaDRM is available. | 48 // Checks whether MediaDRM is available. |
42 // All other static methods check IsAvailable() internally. There's no need | 49 // All other static methods check IsAvailable() internally. There's no need |
43 // to check IsAvailable() explicitly before calling them. | 50 // to check IsAvailable() explicitly before calling them. |
44 static bool IsAvailable(); | 51 static bool IsAvailable(); |
45 | 52 |
46 // Checks whether |key_system| is supported. | 53 // Checks whether |key_system| is supported. |
47 static bool IsKeySystemSupported(const std::string& key_system); | 54 static bool IsKeySystemSupported(const std::string& key_system); |
48 | 55 |
49 // Returns the list of the platform-supported key system names that | 56 // Returns the list of the platform-supported key system names that |
50 // are not handled by Chrome explicitly. | 57 // are not handled by Chrome explicitly. |
51 static std::vector<std::string> GetPlatformKeySystemNames(); | 58 static std::vector<std::string> GetPlatformKeySystemNames(); |
52 | 59 |
53 // Checks whether |key_system| is supported with |container_mime_type|. | 60 // Checks whether |key_system| is supported with |container_mime_type|. |
54 // |container_mime_type| must not be empty. | 61 // |container_mime_type| must not be empty. |
55 static bool IsKeySystemSupportedWithType( | 62 static bool IsKeySystemSupportedWithType( |
56 const std::string& key_system, | 63 const std::string& key_system, |
57 const std::string& container_mime_type); | 64 const std::string& container_mime_type); |
58 | 65 |
59 static bool RegisterMediaDrmBridge(JNIEnv* env); | 66 static bool RegisterMediaDrmBridge(JNIEnv* env); |
60 | 67 |
61 // Returns a MediaDrmBridge instance if |key_system| is supported, or a NULL | 68 // Returns a MediaDrmBridge instance if |key_system| is supported, or a NULL |
62 // pointer otherwise. | 69 // pointer otherwise. |
63 // TODO(xhwang): Is it okay not to update session expiration info? | 70 // TODO(xhwang): Is it okay not to update session expiration info? |
64 static scoped_ptr<MediaDrmBridge> Create( | 71 static scoped_ptr<MediaDrmBridge, BrowserCdmDeleter> Create( |
65 const std::string& key_system, | 72 const std::string& key_system, |
66 const SessionMessageCB& session_message_cb, | 73 const SessionMessageCB& session_message_cb, |
67 const SessionClosedCB& session_closed_cb, | 74 const SessionClosedCB& session_closed_cb, |
68 const LegacySessionErrorCB& legacy_session_error_cb, | 75 const LegacySessionErrorCB& legacy_session_error_cb, |
69 const SessionKeysChangeCB& session_keys_change_cb, | 76 const SessionKeysChangeCB& session_keys_change_cb, |
70 const SessionExpirationUpdateCB& session_expiration_update_cb); | 77 const SessionExpirationUpdateCB& session_expiration_update_cb); |
71 | 78 |
72 // Returns a MediaDrmBridge instance if |key_system| is supported, or a NULL | 79 // 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 | 80 // otherwise. No session callbacks are provided. This is used when we need to |
74 // use MediaDrmBridge without creating any sessions. | 81 // use MediaDrmBridge without creating any sessions. |
75 static scoped_ptr<MediaDrmBridge> CreateWithoutSessionSupport( | 82 static scoped_ptr<MediaDrmBridge, BrowserCdmDeleter> |
76 const std::string& key_system); | 83 CreateWithoutSessionSupport(const std::string& key_system); |
77 | 84 |
78 // Returns true if |security_level| is successfully set, or false otherwise. | 85 // Returns true if |security_level| is successfully set, or false otherwise. |
79 // Call this function right after Create() and before any other calls. | 86 // Call this function right after Create() and before any other calls. |
80 // Note: | 87 // Note: |
81 // - If this function is not called, the default security level of the device | 88 // - If this function is not called, the default security level of the device |
82 // will be used. | 89 // will be used. |
83 // - It's recommended to call this function only once on a MediaDrmBridge | 90 // - It's recommended to call this function only once on a MediaDrmBridge |
84 // object. Calling this function multiples times may cause errors. | 91 // object. Calling this function multiples times may cause errors. |
85 bool SetSecurityLevel(SecurityLevel security_level); | 92 bool SetSecurityLevel(SecurityLevel security_level); |
86 | 93 |
(...skipping 22 matching lines...) Expand all Loading... |
109 int RegisterPlayer(const base::Closure& new_key_cb, | 116 int RegisterPlayer(const base::Closure& new_key_cb, |
110 const base::Closure& cdm_unset_cb) override; | 117 const base::Closure& cdm_unset_cb) override; |
111 void UnregisterPlayer(int registration_id) override; | 118 void UnregisterPlayer(int registration_id) override; |
112 | 119 |
113 // Returns a MediaCrypto object if it's already created. Returns a null object | 120 // Returns a MediaCrypto object if it's already created. Returns a null object |
114 // otherwise. | 121 // otherwise. |
115 base::android::ScopedJavaLocalRef<jobject> GetMediaCrypto(); | 122 base::android::ScopedJavaLocalRef<jobject> GetMediaCrypto(); |
116 | 123 |
117 // Sets callback which will be called when MediaCrypto is ready. | 124 // Sets callback which will be called when MediaCrypto is ready. |
118 // If |closure| is null, previously set callback will be cleared. | 125 // If |closure| is null, previously set callback will be cleared. |
119 void SetMediaCryptoReadyCB(const base::Closure& closure); | 126 void SetMediaCryptoReadyCB(const MediaCryptoReadyCB& closure); |
120 | 127 |
121 // Called after a MediaCrypto object is created. | 128 // Called by Java after a MediaCrypto object is created. |
122 void OnMediaCryptoReady(JNIEnv* env, jobject j_media_drm); | 129 void OnMediaCryptoReady(JNIEnv* env, jobject j_media_drm); |
123 | 130 |
124 // Callbacks to resolve the promise for |promise_id|. | 131 // Callbacks to resolve the promise for |promise_id|. |
125 void OnPromiseResolved(JNIEnv* env, jobject j_media_drm, jint j_promise_id); | 132 void OnPromiseResolved(JNIEnv* env, jobject j_media_drm, jint j_promise_id); |
126 void OnPromiseResolvedWithSession(JNIEnv* env, | 133 void OnPromiseResolvedWithSession(JNIEnv* env, |
127 jobject j_media_drm, | 134 jobject j_media_drm, |
128 jint j_promise_id, | 135 jint j_promise_id, |
129 jbyteArray j_session_id); | 136 jbyteArray j_session_id); |
130 | 137 |
131 // Callback to reject the promise for |promise_id| with |error_message|. | 138 // Callback to reject the promise for |promise_id| with |error_message|. |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
192 const SessionClosedCB& session_closed_cb, | 199 const SessionClosedCB& session_closed_cb, |
193 const LegacySessionErrorCB& legacy_session_error_cb, | 200 const LegacySessionErrorCB& legacy_session_error_cb, |
194 const SessionKeysChangeCB& session_keys_change_cb, | 201 const SessionKeysChangeCB& session_keys_change_cb, |
195 const SessionExpirationUpdateCB& session_expiration_update_cb); | 202 const SessionExpirationUpdateCB& session_expiration_update_cb); |
196 | 203 |
197 static bool IsSecureDecoderRequired(SecurityLevel security_level); | 204 static bool IsSecureDecoderRequired(SecurityLevel security_level); |
198 | 205 |
199 // Get the security level of the media. | 206 // Get the security level of the media. |
200 SecurityLevel GetSecurityLevel(); | 207 SecurityLevel GetSecurityLevel(); |
201 | 208 |
| 209 // A helper method used to relay |player_tracker_| access to the Media thread. |
| 210 void InternalKeysAdded(bool has_additional_usable_key); |
| 211 |
| 212 // A helper method that posts or runs |media_crypto_ready_cb_|. |
| 213 void CheckMediaCryptoAndInvokeCB(); |
| 214 |
202 // UUID of the key system. | 215 // UUID of the key system. |
203 std::vector<uint8> scheme_uuid_; | 216 std::vector<uint8> scheme_uuid_; |
204 | 217 |
205 // Java MediaDrm instance. | 218 // Java MediaDrm instance. |
206 base::android::ScopedJavaGlobalRef<jobject> j_media_drm_; | 219 base::android::ScopedJavaGlobalRef<jobject> j_media_drm_; |
207 | 220 |
208 // Callbacks for firing session events. | 221 // Callbacks for firing session events. |
209 SessionMessageCB session_message_cb_; | 222 SessionMessageCB session_message_cb_; |
210 SessionClosedCB session_closed_cb_; | 223 SessionClosedCB session_closed_cb_; |
211 LegacySessionErrorCB legacy_session_error_cb_; | 224 LegacySessionErrorCB legacy_session_error_cb_; |
212 SessionKeysChangeCB session_keys_change_cb_; | 225 SessionKeysChangeCB session_keys_change_cb_; |
213 SessionExpirationUpdateCB session_expiration_update_cb_; | 226 SessionExpirationUpdateCB session_expiration_update_cb_; |
214 | 227 |
215 base::Closure media_crypto_ready_cb_; | 228 MediaCryptoReadyCB media_crypto_ready_cb_; |
| 229 |
| 230 // Protects |media_crypto_ready_cb_|. |
| 231 base::Lock media_crypto_ready_lock_; |
216 | 232 |
217 ResetCredentialsCB reset_credentials_cb_; | 233 ResetCredentialsCB reset_credentials_cb_; |
218 | 234 |
219 PlayerTrackerImpl player_tracker_; | 235 PlayerTrackerImpl player_tracker_; |
220 | 236 |
221 CdmPromiseAdapter cdm_promise_adapter_; | 237 CdmPromiseAdapter cdm_promise_adapter_; |
222 | 238 |
| 239 // Callback for posting Java notifications to Media thread. |
| 240 typedef base::Callback<void(bool)> KeysAddedCallback; |
| 241 KeysAddedCallback internal_keys_added_cb_; |
| 242 |
| 243 // Object for posting tasks on UI thread. |
| 244 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner_; |
| 245 |
| 246 // This flag is set when we use media thread for certain callbacks. |
| 247 bool use_media_thread_; |
| 248 |
| 249 // NOTE: Weak pointers must be invalidated before all other member variables. |
| 250 base::WeakPtrFactory<MediaDrmBridge> weak_factory_; |
| 251 |
223 DISALLOW_COPY_AND_ASSIGN(MediaDrmBridge); | 252 DISALLOW_COPY_AND_ASSIGN(MediaDrmBridge); |
224 }; | 253 }; |
225 | 254 |
226 } // namespace media | 255 } // namespace media |
227 | 256 |
228 #endif // MEDIA_BASE_ANDROID_MEDIA_DRM_BRIDGE_H_ | 257 #endif // MEDIA_BASE_ANDROID_MEDIA_DRM_BRIDGE_H_ |
OLD | NEW |