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.
5 #ifndef MEDIA_BASE_ANDROID_MEDIA_DRM_BRIDGE_H_
6 #define MEDIA_BASE_ANDROID_MEDIA_DRM_BRIDGE_H_
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/cdm_promise_adapter.h"
17 #include "media/base/media_export.h"
18 #include "media/cdm/player_tracker_impl.h"
25 class MediaPlayerManager
;
27 // This class provides DRM services for android EME implementation.
28 class MEDIA_EXPORT MediaDrmBridge
: public BrowserCdm
{
30 // TODO(ddorwin): These are specific to Widevine. http://crbug.com/459400
32 SECURITY_LEVEL_NONE
= 0,
37 typedef base::Callback
<void(bool)> ResetCredentialsCB
;
39 ~MediaDrmBridge() override
;
41 // Checks whether MediaDRM is available.
42 // All other static methods check IsAvailable() internally. There's no need
43 // to check IsAvailable() explicitly before calling them.
44 static bool IsAvailable();
46 // Checks whether |key_system| is supported.
47 static bool IsKeySystemSupported(const std::string
& key_system
);
49 // Returns the list of the platform-supported key system names that
50 // are not handled by Chrome explicitly.
51 static std::vector
<std::string
> GetPlatformKeySystemNames();
53 // Checks whether |key_system| is supported with |container_mime_type|.
54 // |container_mime_type| must not be empty.
55 static bool IsKeySystemSupportedWithType(
56 const std::string
& key_system
,
57 const std::string
& container_mime_type
);
59 static bool RegisterMediaDrmBridge(JNIEnv
* env
);
61 // Returns a MediaDrmBridge instance if |key_system| is supported, or a NULL
63 // TODO(xhwang): Is it okay not to update session expiration info?
64 static scoped_ptr
<MediaDrmBridge
> Create(
65 const std::string
& key_system
,
66 const SessionMessageCB
& session_message_cb
,
67 const SessionClosedCB
& session_closed_cb
,
68 const LegacySessionErrorCB
& legacy_session_error_cb
,
69 const SessionKeysChangeCB
& session_keys_change_cb
,
70 const SessionExpirationUpdateCB
& session_expiration_update_cb
);
72 // 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
74 // use MediaDrmBridge without creating any sessions.
75 static scoped_ptr
<MediaDrmBridge
> CreateWithoutSessionSupport(
76 const std::string
& key_system
);
78 // Returns true if |security_level| is successfully set, or false otherwise.
79 // Call this function right after Create() and before any other calls.
81 // - If this function is not called, the default security level of the device
83 // - It's recommended to call this function only once on a MediaDrmBridge
84 // object. Calling this function multiples times may cause errors.
85 bool SetSecurityLevel(SecurityLevel security_level
);
87 // MediaKeys (via BrowserCdm) implementation.
88 void SetServerCertificate(
89 const std::vector
<uint8_t>& certificate
,
90 scoped_ptr
<media::SimpleCdmPromise
> promise
) override
;
91 void CreateSessionAndGenerateRequest(
92 SessionType session_type
,
93 media::EmeInitDataType init_data_type
,
94 const std::vector
<uint8_t>& init_data
,
95 scoped_ptr
<media::NewSessionCdmPromise
> promise
) override
;
96 void LoadSession(SessionType session_type
,
97 const std::string
& session_id
,
98 scoped_ptr
<media::NewSessionCdmPromise
> promise
) override
;
99 void UpdateSession(const std::string
& session_id
,
100 const std::vector
<uint8_t>& response
,
101 scoped_ptr
<media::SimpleCdmPromise
> promise
) override
;
102 void CloseSession(const std::string
& session_id
,
103 scoped_ptr
<media::SimpleCdmPromise
> promise
) override
;
104 void RemoveSession(const std::string
& session_id
,
105 scoped_ptr
<media::SimpleCdmPromise
> promise
) override
;
106 CdmContext
* GetCdmContext() override
;
108 // PlayerTracker (via BrowserCdm) implementation.
109 int RegisterPlayer(const base::Closure
& new_key_cb
,
110 const base::Closure
& cdm_unset_cb
) override
;
111 void UnregisterPlayer(int registration_id
) override
;
113 // Returns a MediaCrypto object if it's already created. Returns a null object
115 base::android::ScopedJavaLocalRef
<jobject
> GetMediaCrypto();
117 // Sets callback which will be called when MediaCrypto is ready.
118 // If |closure| is null, previously set callback will be cleared.
119 void SetMediaCryptoReadyCB(const base::Closure
& closure
);
121 // Called after a MediaCrypto object is created.
122 void OnMediaCryptoReady(JNIEnv
* env
, jobject j_media_drm
);
124 // Callbacks to resolve the promise for |promise_id|.
125 void OnPromiseResolved(JNIEnv
* env
, jobject j_media_drm
, jint j_promise_id
);
126 void OnPromiseResolvedWithSession(JNIEnv
* env
,
129 jbyteArray j_session_id
);
131 // Callback to reject the promise for |promise_id| with |error_message|.
132 // Note: No |system_error| is available from MediaDrm.
133 // TODO(xhwang): Implement Exception code.
134 void OnPromiseRejected(JNIEnv
* env
,
137 jstring j_error_message
);
139 // Session event callbacks.
140 // Note: Session expiration update is not supported by MediaDrm.
142 // TODO(xhwang): Remove |j_legacy_destination_url| when prefixed EME support
144 void OnSessionMessage(JNIEnv
* env
,
146 jbyteArray j_session_id
,
148 jbyteArray j_message
,
149 jstring j_legacy_destination_url
);
150 void OnSessionClosed(JNIEnv
* env
,
152 jbyteArray j_session_id
);
154 void OnSessionKeysChange(JNIEnv
* env
,
156 jbyteArray j_session_id
,
157 jobjectArray j_keys_info
,
158 bool has_additional_usable_key
);
160 // |expiry_time_ms| is the new expiration time for the keys in the session.
161 // The time is in milliseconds, relative to the Unix epoch. A time of 0
162 // indicates that the keys never expire.
163 void OnSessionExpirationUpdate(JNIEnv
* env
,
165 jbyteArray j_session_id
,
166 jlong expiry_time_ms
);
168 // Called by the CDM when an error occurred in session |j_session_id|
169 // unrelated to one of the MediaKeys calls that accept a |promise|.
171 // - This method is only for supporting prefixed EME API.
172 // - This method will be ignored by unprefixed EME. All errors reported
173 // in this method should probably also be reported by one of other methods.
174 void OnLegacySessionError(JNIEnv
* env
,
176 jbyteArray j_session_id
,
177 jstring j_error_message
);
179 // Reset the device credentials.
180 void ResetDeviceCredentials(const ResetCredentialsCB
& callback
);
182 // Called by the java object when credential reset is completed.
183 void OnResetDeviceCredentialsCompleted(JNIEnv
* env
, jobject
, bool success
);
185 // Helper function to determine whether a protected surface is needed for the
187 bool IsProtectedSurfaceRequired();
190 MediaDrmBridge(const std::vector
<uint8
>& scheme_uuid
,
191 const SessionMessageCB
& session_message_cb
,
192 const SessionClosedCB
& session_closed_cb
,
193 const LegacySessionErrorCB
& legacy_session_error_cb
,
194 const SessionKeysChangeCB
& session_keys_change_cb
,
195 const SessionExpirationUpdateCB
& session_expiration_update_cb
);
197 static bool IsSecureDecoderRequired(SecurityLevel security_level
);
199 // Get the security level of the media.
200 SecurityLevel
GetSecurityLevel();
202 // UUID of the key system.
203 std::vector
<uint8
> scheme_uuid_
;
205 // Java MediaDrm instance.
206 base::android::ScopedJavaGlobalRef
<jobject
> j_media_drm_
;
208 // Callbacks for firing session events.
209 SessionMessageCB session_message_cb_
;
210 SessionClosedCB session_closed_cb_
;
211 LegacySessionErrorCB legacy_session_error_cb_
;
212 SessionKeysChangeCB session_keys_change_cb_
;
213 SessionExpirationUpdateCB session_expiration_update_cb_
;
215 base::Closure media_crypto_ready_cb_
;
217 ResetCredentialsCB reset_credentials_cb_
;
219 PlayerTrackerImpl player_tracker_
;
221 CdmPromiseAdapter cdm_promise_adapter_
;
223 DISALLOW_COPY_AND_ASSIGN(MediaDrmBridge
);
228 #endif // MEDIA_BASE_ANDROID_MEDIA_DRM_BRIDGE_H_