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/media_export.h"
16 #include "media/base/media_keys.h"
23 class MediaPlayerManager
;
25 // This class provides DRM services for android EME implementation.
26 // TODO(qinmin): implement all the functions in this class.
27 class MEDIA_EXPORT MediaDrmBridge
: public MediaKeys
{
30 SECURITY_LEVEL_NONE
= 0,
35 typedef base::Callback
<void(bool)> ResetCredentialsCB
;
37 virtual ~MediaDrmBridge();
39 // Returns a MediaDrmBridge instance if |scheme_uuid| is supported, or a NULL
41 static scoped_ptr
<MediaDrmBridge
> Create(
43 const std::vector
<uint8
>& scheme_uuid
,
44 const GURL
& frame_url
,
45 const std::string
& security_level
,
46 MediaPlayerManager
* manager
);
48 // Checks whether MediaDRM is available.
49 static bool IsAvailable();
51 static bool IsSecurityLevelSupported(const std::vector
<uint8
>& scheme_uuid
,
52 const std::string
& security_level
);
54 static bool IsCryptoSchemeSupported(const std::vector
<uint8
>& scheme_uuid
,
55 const std::string
& container_mime_type
);
57 static bool IsSecureDecoderRequired(const std::string
& security_level_str
);
59 static bool RegisterMediaDrmBridge(JNIEnv
* env
);
61 // MediaKeys implementations.
62 virtual bool GenerateKeyRequest(const std::string
& type
,
63 const uint8
* init_data
,
64 int init_data_length
) OVERRIDE
;
65 virtual void AddKey(const uint8
* key
, int key_length
,
66 const uint8
* init_data
, int init_data_length
,
67 const std::string
& session_id
) OVERRIDE
;
68 virtual void CancelKeyRequest(const std::string
& session_id
) OVERRIDE
;
70 // Returns a MediaCrypto object if it's already created. Returns a null object
72 base::android::ScopedJavaLocalRef
<jobject
> GetMediaCrypto();
74 // Sets callback which will be called when MediaCrypto is ready.
75 // If |closure| is null, previously set callback will be cleared.
76 void SetMediaCryptoReadyCB(const base::Closure
& closure
);
78 // Called after a MediaCrypto object is created.
79 void OnMediaCryptoReady(JNIEnv
* env
, jobject
);
81 // Called after we got the response for GenerateKeyRequest().
82 void OnKeyMessage(JNIEnv
* env
, jobject
, jstring j_session_id
,
83 jbyteArray message
, jstring destination_url
);
85 // Called when key is added.
86 void OnKeyAdded(JNIEnv
* env
, jobject
, jstring j_session_id
);
88 // Called when error happens.
89 void OnKeyError(JNIEnv
* env
, jobject
, jstring j_session_id
);
91 // Reset the device credentials.
92 void ResetDeviceCredentials(const ResetCredentialsCB
& callback
);
94 // Called by the java object when credential reset is completed.
95 void OnResetDeviceCredentialsCompleted(JNIEnv
* env
, jobject
, bool success
);
97 // Helper function to determine whether a protected surface is needed for the
99 bool IsProtectedSurfaceRequired();
101 int media_keys_id() const { return media_keys_id_
; }
103 GURL
frame_url() const { return frame_url_
; }
105 static void set_can_use_media_drm(bool can_use_media_drm
) {
106 can_use_media_drm_
= can_use_media_drm
;
110 static bool IsSecureDecoderRequired(SecurityLevel security_level
);
112 static bool can_use_media_drm_
;
114 MediaDrmBridge(int media_keys_id
,
115 const std::vector
<uint8
>& scheme_uuid
,
116 const GURL
& frame_url
,
117 const std::string
& security_level
,
118 MediaPlayerManager
* manager
);
120 // Get the security level of the media.
121 SecurityLevel
GetSecurityLevel();
123 // ID of the MediaKeys object.
126 // UUID of the key system.
127 std::vector
<uint8
> scheme_uuid_
;
129 // media stream's frame URL.
130 const GURL frame_url_
;
132 // Java MediaDrm instance.
133 base::android::ScopedJavaGlobalRef
<jobject
> j_media_drm_
;
135 // Non-owned pointer.
136 MediaPlayerManager
* manager_
;
138 base::Closure media_crypto_ready_cb_
;
140 ResetCredentialsCB reset_credentials_cb_
;
142 DISALLOW_COPY_AND_ASSIGN(MediaDrmBridge
);
147 #endif // MEDIA_BASE_ANDROID_MEDIA_DRM_BRIDGE_H_