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_; }
106 static bool IsSecureDecoderRequired(SecurityLevel security_level);
108 MediaDrmBridge(int media_keys_id,
109 const std::vector<uint8>& scheme_uuid,
110 const GURL& frame_url,
111 const std::string& security_level,
112 MediaPlayerManager* manager);
114 // Get the security level of the media.
115 SecurityLevel GetSecurityLevel();
117 // ID of the MediaKeys object.
120 // UUID of the key system.
121 std::vector<uint8> scheme_uuid_;
123 // media stream's frame URL.
124 const GURL frame_url_;
126 // Java MediaDrm instance.
127 base::android::ScopedJavaGlobalRef<jobject> j_media_drm_;
129 // Non-owned pointer.
130 MediaPlayerManager* manager_;
132 base::Closure media_crypto_ready_cb_;
134 ResetCredentialsCB reset_credentials_cb_;
136 DISALLOW_COPY_AND_ASSIGN(MediaDrmBridge);
141 #endif // MEDIA_BASE_ANDROID_MEDIA_DRM_BRIDGE_H_