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
{
31 SECURITY_LEVEL_NONE
= 0,
36 typedef base::Callback
<void(bool)> ResetCredentialsCB
;
38 ~MediaDrmBridge() override
;
40 // Checks whether MediaDRM is available.
41 // All other static methods check IsAvailable() internally. There's no need
42 // to check IsAvailable() explicitly before calling them.
43 static bool IsAvailable();
45 static bool IsSecurityLevelSupported(const std::string
& key_system
,
46 SecurityLevel security_level
);
48 // Checks whether |key_system| is supported.
49 static bool IsKeySystemSupported(const std::string
& key_system
);
51 // Returns the list of the platform-supported key system names that
52 // are not handled by Chrome explicitly.
53 static std::vector
<std::string
> GetPlatformKeySystemNames();
55 // Checks whether |key_system| is supported with |container_mime_type|.
56 // |container_mime_type| must not be empty.
57 static bool IsKeySystemSupportedWithType(
58 const std::string
& key_system
,
59 const std::string
& container_mime_type
);
61 static bool IsSecureDecoderRequired(SecurityLevel security_level
);
63 static bool RegisterMediaDrmBridge(JNIEnv
* env
);
65 // Returns a MediaDrmBridge instance if |key_system| is supported, or a NULL
67 // TODO(xhwang): Is it okay not to update session expiration info?
68 static scoped_ptr
<MediaDrmBridge
> Create(
69 const std::string
& key_system
,
70 const SessionMessageCB
& session_message_cb
,
71 const SessionClosedCB
& session_closed_cb
,
72 const SessionErrorCB
& session_error_cb
,
73 const SessionKeysChangeCB
& session_keys_change_cb
,
74 const SessionExpirationUpdateCB
& session_expiration_update_cb
);
76 // Returns a MediaDrmBridge instance if |key_system| is supported, or a NULL
77 // otherwise. No session callbacks are provided. This is used when we need to
78 // use MediaDrmBridge without creating any sessions.
79 static scoped_ptr
<MediaDrmBridge
> CreateWithoutSessionSupport(
80 const std::string
& key_system
);
82 // Returns true if |security_level| is successfully set, or false otherwise.
83 // Call this function right after Create() and before any other calls.
85 // - If this function is not called, the default security level of the device
87 // - It's recommended to call this function only once on a MediaDrmBridge
88 // object. Calling this function multiples times may cause errors.
89 bool SetSecurityLevel(SecurityLevel security_level
);
91 // MediaKeys (via BrowserCdm) implementation.
92 void SetServerCertificate(
93 const uint8
* certificate_data
,
94 int certificate_data_length
,
95 scoped_ptr
<media::SimpleCdmPromise
> promise
) override
;
96 void CreateSessionAndGenerateRequest(
97 SessionType session_type
,
98 const std::string
& init_data_type
,
99 const uint8
* init_data
,
100 int init_data_length
,
101 scoped_ptr
<media::NewSessionCdmPromise
> promise
) override
;
102 void LoadSession(SessionType session_type
,
103 const std::string
& session_id
,
104 scoped_ptr
<media::NewSessionCdmPromise
> promise
) override
;
105 void UpdateSession(const std::string
& session_id
,
106 const uint8
* response
,
108 scoped_ptr
<media::SimpleCdmPromise
> promise
) override
;
109 void CloseSession(const std::string
& session_id
,
110 scoped_ptr
<media::SimpleCdmPromise
> promise
) override
;
111 void RemoveSession(const std::string
& session_id
,
112 scoped_ptr
<media::SimpleCdmPromise
> promise
) override
;
113 CdmContext
* GetCdmContext() override
;
115 // PlayerTracker (via BrowserCdm) implementation.
116 int RegisterPlayer(const base::Closure
& new_key_cb
,
117 const base::Closure
& cdm_unset_cb
) override
;
118 void UnregisterPlayer(int registration_id
) override
;
120 // Returns a MediaCrypto object if it's already created. Returns a null object
122 base::android::ScopedJavaLocalRef
<jobject
> GetMediaCrypto();
124 // Sets callback which will be called when MediaCrypto is ready.
125 // If |closure| is null, previously set callback will be cleared.
126 void SetMediaCryptoReadyCB(const base::Closure
& closure
);
128 // Called after a MediaCrypto object is created.
129 void OnMediaCryptoReady(JNIEnv
* env
, jobject j_media_drm
);
131 // Callbacks to resolve the promise for |promise_id|.
132 void OnPromiseResolved(JNIEnv
* env
, jobject j_media_drm
, jint j_promise_id
);
133 void OnPromiseResolvedWithSession(JNIEnv
* env
,
136 jbyteArray j_session_id
);
138 // Callback to reject the promise for |promise_id| with |error_message|.
139 // Note: No |system_error| is available from MediaDrm.
140 // TODO(xhwang): Implement Exception code.
141 void OnPromiseRejected(JNIEnv
* env
,
144 jstring j_error_message
);
146 // Session event callbacks.
147 // Note: Session expiration update is not supported by MediaDrm.
149 void OnSessionMessage(JNIEnv
* env
,
151 jbyteArray j_session_id
,
152 jbyteArray j_message
,
153 jstring j_legacy_destination_url
);
154 void OnSessionClosed(JNIEnv
* env
,
156 jbyteArray j_session_id
);
158 // Note: Key ID is not available in MediaDrm, so only a generic |j_key_status|
159 // and |has_additional_usable_key| are returned.
160 void OnSessionKeysChange(JNIEnv
* env
,
162 jbyteArray j_session_id
,
163 bool has_additional_usable_key
,
166 // Called by the CDM when an error occurred in session |j_session_id|
167 // unrelated to one of the MediaKeys calls that accept a |promise|.
169 // - This method is only for supporting prefixed EME API.
170 // - This method will be ignored by unprefixed EME. All errors reported
171 // in this method should probably also be reported by one of other methods.
172 void OnLegacySessionError(JNIEnv
* env
,
174 jbyteArray j_session_id
,
175 jstring j_error_message
);
177 // Reset the device credentials.
178 void ResetDeviceCredentials(const ResetCredentialsCB
& callback
);
180 // Called by the java object when credential reset is completed.
181 void OnResetDeviceCredentialsCompleted(JNIEnv
* env
, jobject
, bool success
);
183 // Helper function to determine whether a protected surface is needed for the
185 bool IsProtectedSurfaceRequired();
188 MediaDrmBridge(const std::vector
<uint8
>& scheme_uuid
,
189 const SessionMessageCB
& session_message_cb
,
190 const SessionClosedCB
& session_closed_cb
,
191 const SessionErrorCB
& session_error_cb
,
192 const SessionKeysChangeCB
& session_keys_change_cb
);
194 // Get the security level of the media.
195 SecurityLevel
GetSecurityLevel();
197 // UUID of the key system.
198 std::vector
<uint8
> scheme_uuid_
;
200 // Java MediaDrm instance.
201 base::android::ScopedJavaGlobalRef
<jobject
> j_media_drm_
;
203 // Callbacks for firing session events.
204 SessionMessageCB session_message_cb_
;
205 SessionClosedCB session_closed_cb_
;
206 SessionErrorCB session_error_cb_
;
207 SessionKeysChangeCB session_keys_change_cb_
;
209 base::Closure media_crypto_ready_cb_
;
211 ResetCredentialsCB reset_credentials_cb_
;
213 PlayerTrackerImpl player_tracker_
;
215 CdmPromiseAdapter cdm_promise_adapter_
;
217 DISALLOW_COPY_AND_ASSIGN(MediaDrmBridge
);
222 #endif // MEDIA_BASE_ANDROID_MEDIA_DRM_BRIDGE_H_