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