[BackgroundSync] Clean up some tests
[chromium-blink-merge.git] / media / base / android / media_drm_bridge.h
blob30842b977315ed085e9608b2483c53cb881d50da
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 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
114 // otherwise.
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,
127 jobject j_media_drm,
128 jint j_promise_id,
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,
135 jobject j_media_drm,
136 jint j_promise_id,
137 jstring j_error_message);
139 // Session event callbacks.
140 // Note: Session expiration update is not supported by MediaDrm.
142 void OnSessionMessage(JNIEnv* env,
143 jobject j_media_drm,
144 jbyteArray j_session_id,
145 jbyteArray j_message,
146 jstring j_legacy_destination_url);
147 void OnSessionClosed(JNIEnv* env,
148 jobject j_media_drm,
149 jbyteArray j_session_id);
151 // Note: Key ID is not available in MediaDrm, so only a generic |j_key_status|
152 // and |has_additional_usable_key| are returned.
153 void OnSessionKeysChange(JNIEnv* env,
154 jobject j_media_drm,
155 jbyteArray j_session_id,
156 bool has_additional_usable_key,
157 jint j_key_status);
159 // Called by the CDM when an error occurred in session |j_session_id|
160 // unrelated to one of the MediaKeys calls that accept a |promise|.
161 // Note:
162 // - This method is only for supporting prefixed EME API.
163 // - This method will be ignored by unprefixed EME. All errors reported
164 // in this method should probably also be reported by one of other methods.
165 void OnLegacySessionError(JNIEnv* env,
166 jobject j_media_drm,
167 jbyteArray j_session_id,
168 jstring j_error_message);
170 // Reset the device credentials.
171 void ResetDeviceCredentials(const ResetCredentialsCB& callback);
173 // Called by the java object when credential reset is completed.
174 void OnResetDeviceCredentialsCompleted(JNIEnv* env, jobject, bool success);
176 // Helper function to determine whether a protected surface is needed for the
177 // video playback.
178 bool IsProtectedSurfaceRequired();
180 private:
181 MediaDrmBridge(const std::vector<uint8>& scheme_uuid,
182 const SessionMessageCB& session_message_cb,
183 const SessionClosedCB& session_closed_cb,
184 const LegacySessionErrorCB& legacy_session_error_cb,
185 const SessionKeysChangeCB& session_keys_change_cb);
187 static bool IsSecureDecoderRequired(SecurityLevel security_level);
189 // Get the security level of the media.
190 SecurityLevel GetSecurityLevel();
192 // UUID of the key system.
193 std::vector<uint8> scheme_uuid_;
195 // Java MediaDrm instance.
196 base::android::ScopedJavaGlobalRef<jobject> j_media_drm_;
198 // Callbacks for firing session events.
199 SessionMessageCB session_message_cb_;
200 SessionClosedCB session_closed_cb_;
201 LegacySessionErrorCB legacy_session_error_cb_;
202 SessionKeysChangeCB session_keys_change_cb_;
204 base::Closure media_crypto_ready_cb_;
206 ResetCredentialsCB reset_credentials_cb_;
208 PlayerTrackerImpl player_tracker_;
210 CdmPromiseAdapter cdm_promise_adapter_;
212 DISALLOW_COPY_AND_ASSIGN(MediaDrmBridge);
215 } // namespace media
217 #endif // MEDIA_BASE_ANDROID_MEDIA_DRM_BRIDGE_H_