Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / media / base / android / media_drm_bridge.h
blobb767aa64a117f8941bbc281bb085dca069057852
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_
8 #include <jni.h>
9 #include <string>
10 #include <vector>
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"
19 #include "url/gurl.h"
21 class GURL;
23 namespace media {
25 class MediaPlayerManager;
27 // This class provides DRM services for android EME implementation.
28 class MEDIA_EXPORT MediaDrmBridge : public BrowserCdm {
29 public:
30 // TODO(ddorwin): These are specific to Widevine. http://crbug.com/459400
31 enum SecurityLevel {
32 SECURITY_LEVEL_NONE = 0,
33 SECURITY_LEVEL_1 = 1,
34 SECURITY_LEVEL_3 = 3,
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
62 // pointer otherwise.
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.
80 // Note:
81 // - If this function is not called, the default security level of the device
82 // will be used.
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 uint8* certificate_data,
90 int certificate_data_length,
91 scoped_ptr<media::SimpleCdmPromise> promise) override;
92 void CreateSessionAndGenerateRequest(
93 SessionType session_type,
94 media::EmeInitDataType init_data_type,
95 const uint8* init_data,
96 int init_data_length,
97 scoped_ptr<media::NewSessionCdmPromise> promise) override;
98 void LoadSession(SessionType session_type,
99 const std::string& session_id,
100 scoped_ptr<media::NewSessionCdmPromise> promise) override;
101 void UpdateSession(const std::string& session_id,
102 const uint8* response,
103 int response_length,
104 scoped_ptr<media::SimpleCdmPromise> promise) override;
105 void CloseSession(const std::string& session_id,
106 scoped_ptr<media::SimpleCdmPromise> promise) override;
107 void RemoveSession(const std::string& session_id,
108 scoped_ptr<media::SimpleCdmPromise> promise) override;
109 CdmContext* GetCdmContext() override;
111 // PlayerTracker (via BrowserCdm) implementation.
112 int RegisterPlayer(const base::Closure& new_key_cb,
113 const base::Closure& cdm_unset_cb) override;
114 void UnregisterPlayer(int registration_id) override;
116 // Returns a MediaCrypto object if it's already created. Returns a null object
117 // otherwise.
118 base::android::ScopedJavaLocalRef<jobject> GetMediaCrypto();
120 // Sets callback which will be called when MediaCrypto is ready.
121 // If |closure| is null, previously set callback will be cleared.
122 void SetMediaCryptoReadyCB(const base::Closure& closure);
124 // Called after a MediaCrypto object is created.
125 void OnMediaCryptoReady(JNIEnv* env, jobject j_media_drm);
127 // Callbacks to resolve the promise for |promise_id|.
128 void OnPromiseResolved(JNIEnv* env, jobject j_media_drm, jint j_promise_id);
129 void OnPromiseResolvedWithSession(JNIEnv* env,
130 jobject j_media_drm,
131 jint j_promise_id,
132 jbyteArray j_session_id);
134 // Callback to reject the promise for |promise_id| with |error_message|.
135 // Note: No |system_error| is available from MediaDrm.
136 // TODO(xhwang): Implement Exception code.
137 void OnPromiseRejected(JNIEnv* env,
138 jobject j_media_drm,
139 jint j_promise_id,
140 jstring j_error_message);
142 // Session event callbacks.
143 // Note: Session expiration update is not supported by MediaDrm.
145 void OnSessionMessage(JNIEnv* env,
146 jobject j_media_drm,
147 jbyteArray j_session_id,
148 jbyteArray j_message,
149 jstring j_legacy_destination_url);
150 void OnSessionClosed(JNIEnv* env,
151 jobject j_media_drm,
152 jbyteArray j_session_id);
154 // Note: Key ID is not available in MediaDrm, so only a generic |j_key_status|
155 // and |has_additional_usable_key| are returned.
156 void OnSessionKeysChange(JNIEnv* env,
157 jobject j_media_drm,
158 jbyteArray j_session_id,
159 bool has_additional_usable_key,
160 jint j_key_status);
162 // Called by the CDM when an error occurred in session |j_session_id|
163 // unrelated to one of the MediaKeys calls that accept a |promise|.
164 // Note:
165 // - This method is only for supporting prefixed EME API.
166 // - This method will be ignored by unprefixed EME. All errors reported
167 // in this method should probably also be reported by one of other methods.
168 void OnLegacySessionError(JNIEnv* env,
169 jobject j_media_drm,
170 jbyteArray j_session_id,
171 jstring j_error_message);
173 // Reset the device credentials.
174 void ResetDeviceCredentials(const ResetCredentialsCB& callback);
176 // Called by the java object when credential reset is completed.
177 void OnResetDeviceCredentialsCompleted(JNIEnv* env, jobject, bool success);
179 // Helper function to determine whether a protected surface is needed for the
180 // video playback.
181 bool IsProtectedSurfaceRequired();
183 private:
184 MediaDrmBridge(const std::vector<uint8>& scheme_uuid,
185 const SessionMessageCB& session_message_cb,
186 const SessionClosedCB& session_closed_cb,
187 const LegacySessionErrorCB& legacy_session_error_cb,
188 const SessionKeysChangeCB& session_keys_change_cb);
190 static bool IsSecureDecoderRequired(SecurityLevel security_level);
192 // Get the security level of the media.
193 SecurityLevel GetSecurityLevel();
195 // UUID of the key system.
196 std::vector<uint8> scheme_uuid_;
198 // Java MediaDrm instance.
199 base::android::ScopedJavaGlobalRef<jobject> j_media_drm_;
201 // Callbacks for firing session events.
202 SessionMessageCB session_message_cb_;
203 SessionClosedCB session_closed_cb_;
204 LegacySessionErrorCB legacy_session_error_cb_;
205 SessionKeysChangeCB session_keys_change_cb_;
207 base::Closure media_crypto_ready_cb_;
209 ResetCredentialsCB reset_credentials_cb_;
211 PlayerTrackerImpl player_tracker_;
213 CdmPromiseAdapter cdm_promise_adapter_;
215 DISALLOW_COPY_AND_ASSIGN(MediaDrmBridge);
218 } // namespace media
220 #endif // MEDIA_BASE_ANDROID_MEDIA_DRM_BRIDGE_H_