• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #ifndef MEDIA_BASE_ANDROID_MEDIA_DRM_BRIDGE_H_
6 #define MEDIA_BASE_ANDROID_MEDIA_DRM_BRIDGE_H_
7 
8 #include <jni.h>
9 #include <map>
10 #include <queue>
11 #include <string>
12 #include <vector>
13 
14 #include "base/android/scoped_java_ref.h"
15 #include "base/callback.h"
16 #include "base/memory/scoped_ptr.h"
17 #include "media/base/media_export.h"
18 #include "media/base/media_keys.h"
19 #include "url/gurl.h"
20 
21 class GURL;
22 
23 namespace media {
24 
25 class MediaPlayerManager;
26 
27 // This class provides DRM services for android EME implementation.
28 // TODO(qinmin): implement all the functions in this class.
29 class MEDIA_EXPORT MediaDrmBridge : public MediaKeys {
30  public:
31   enum SecurityLevel {
32     SECURITY_LEVEL_NONE = 0,
33     SECURITY_LEVEL_1 = 1,
34     SECURITY_LEVEL_3 = 3,
35   };
36 
37   typedef base::Callback<void(bool)> ResetCredentialsCB;
38 
39   virtual ~MediaDrmBridge();
40 
41   // Returns a MediaDrmBridge instance if |scheme_uuid| is supported, or a NULL
42   // pointer otherwise.
43   static scoped_ptr<MediaDrmBridge> Create(
44       int media_keys_id,
45       const std::vector<uint8>& scheme_uuid,
46       const GURL& frame_url,
47       const std::string& security_level,
48       MediaPlayerManager* manager);
49 
50   // Checks whether MediaDRM is available.
51   static bool IsAvailable();
52 
53   static bool IsSecurityLevelSupported(const std::vector<uint8>& scheme_uuid,
54                                        const std::string& security_level);
55 
56   static bool IsCryptoSchemeSupported(const std::vector<uint8>& scheme_uuid,
57                                       const std::string& container_mime_type);
58 
59   static bool IsSecureDecoderRequired(const std::string& security_level_str);
60 
61   static bool RegisterMediaDrmBridge(JNIEnv* env);
62 
63   // MediaKeys implementations.
64   virtual bool CreateSession(uint32 session_id,
65                              const std::string& type,
66                              const uint8* init_data,
67                              int init_data_length) OVERRIDE;
68   virtual void UpdateSession(uint32 session_id,
69                              const uint8* response,
70                              int response_length) OVERRIDE;
71   virtual void ReleaseSession(uint32 session_id) OVERRIDE;
72 
73   // Returns a MediaCrypto object if it's already created. Returns a null object
74   // otherwise.
75   base::android::ScopedJavaLocalRef<jobject> GetMediaCrypto();
76 
77   // Sets callback which will be called when MediaCrypto is ready.
78   // If |closure| is null, previously set callback will be cleared.
79   void SetMediaCryptoReadyCB(const base::Closure& closure);
80 
81   // Called after a MediaCrypto object is created.
82   void OnMediaCryptoReady(JNIEnv* env, jobject j_media_drm);
83 
84   // Callbacks for firing session events.
85   void OnSessionCreated(JNIEnv* env,
86                         jobject j_media_drm,
87                         jint j_session_id,
88                         jstring j_web_session_id);
89   void OnSessionMessage(JNIEnv* env,
90                         jobject j_media_drm,
91                         jint j_session_id,
92                         jbyteArray j_message,
93                         jstring j_destination_url);
94   void OnSessionReady(JNIEnv* env, jobject j_media_drm, jint j_session_id);
95   void OnSessionClosed(JNIEnv* env, jobject j_media_drm, jint j_session_id);
96   void OnSessionError(JNIEnv* env, jobject j_media_drm, jint j_session_id);
97 
98   // Reset the device credentials.
99   void ResetDeviceCredentials(const ResetCredentialsCB& callback);
100 
101   // Called by the java object when credential reset is completed.
102   void OnResetDeviceCredentialsCompleted(JNIEnv* env, jobject, bool success);
103 
104   // Helper function to determine whether a protected surface is needed for the
105   // video playback.
106   bool IsProtectedSurfaceRequired();
107 
media_keys_id()108   int media_keys_id() const { return media_keys_id_; }
109 
frame_url()110   GURL frame_url() const { return frame_url_; }
111 
112  private:
113   static bool IsSecureDecoderRequired(SecurityLevel security_level);
114 
115   MediaDrmBridge(int media_keys_id,
116                  const std::vector<uint8>& scheme_uuid,
117                  const GURL& frame_url,
118                  const std::string& security_level,
119                  MediaPlayerManager* manager);
120 
121   // Get the security level of the media.
122   SecurityLevel GetSecurityLevel();
123 
124   // ID of the MediaKeys object.
125   int media_keys_id_;
126 
127   // UUID of the key system.
128   std::vector<uint8> scheme_uuid_;
129 
130   // media stream's frame URL.
131   const GURL frame_url_;
132 
133   // Java MediaDrm instance.
134   base::android::ScopedJavaGlobalRef<jobject> j_media_drm_;
135 
136   // Non-owned pointer.
137   MediaPlayerManager* manager_;
138 
139   base::Closure media_crypto_ready_cb_;
140 
141   ResetCredentialsCB reset_credentials_cb_;
142 
143   DISALLOW_COPY_AND_ASSIGN(MediaDrmBridge);
144 };
145 
146 }  // namespace media
147 
148 #endif  // MEDIA_BASE_ANDROID_MEDIA_DRM_BRIDGE_H_
149