- add sources.
[platform/framework/web/crosswalk.git] / src / media / base / android / media_drm_bridge.h
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 <string>
10 #include <vector>
11
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"
17 #include "url/gurl.h"
18
19 class GURL;
20
21 namespace media {
22
23 class MediaPlayerManager;
24
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 {
28  public:
29   enum SecurityLevel {
30     SECURITY_LEVEL_NONE = 0,
31     SECURITY_LEVEL_1 = 1,
32     SECURITY_LEVEL_3 = 3,
33   };
34
35   typedef base::Callback<void(bool)> ResetCredentialsCB;
36
37   virtual ~MediaDrmBridge();
38
39   // Returns a MediaDrmBridge instance if |scheme_uuid| is supported, or a NULL
40   // pointer otherwise.
41   static scoped_ptr<MediaDrmBridge> Create(
42       int media_keys_id,
43       const std::vector<uint8>& scheme_uuid,
44       const GURL& frame_url,
45       const std::string& security_level,
46       MediaPlayerManager* manager);
47
48   // Checks whether MediaDRM is available.
49   static bool IsAvailable();
50
51   static bool IsSecurityLevelSupported(const std::vector<uint8>& scheme_uuid,
52                                        const std::string& security_level);
53
54   static bool IsCryptoSchemeSupported(const std::vector<uint8>& scheme_uuid,
55                                       const std::string& container_mime_type);
56
57   static bool IsSecureDecoderRequired(const std::string& security_level_str);
58
59   static bool RegisterMediaDrmBridge(JNIEnv* env);
60
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;
69
70   // Returns a MediaCrypto object if it's already created. Returns a null object
71   // otherwise.
72   base::android::ScopedJavaLocalRef<jobject> GetMediaCrypto();
73
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);
77
78   // Called after a MediaCrypto object is created.
79   void OnMediaCryptoReady(JNIEnv* env, jobject);
80
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);
84
85   // Called when key is added.
86   void OnKeyAdded(JNIEnv* env, jobject, jstring j_session_id);
87
88   // Called when error happens.
89   void OnKeyError(JNIEnv* env, jobject, jstring j_session_id);
90
91   // Reset the device credentials.
92   void ResetDeviceCredentials(const ResetCredentialsCB& callback);
93
94   // Called by the java object when credential reset is completed.
95   void OnResetDeviceCredentialsCompleted(JNIEnv* env, jobject, bool success);
96
97   // Helper function to determine whether a protected surface is needed for the
98   // video playback.
99   bool IsProtectedSurfaceRequired();
100
101   int media_keys_id() const { return media_keys_id_; }
102
103   GURL frame_url() const { return frame_url_; }
104
105  private:
106   static bool IsSecureDecoderRequired(SecurityLevel security_level);
107
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);
113
114   // Get the security level of the media.
115   SecurityLevel GetSecurityLevel();
116
117   // ID of the MediaKeys object.
118   int media_keys_id_;
119
120   // UUID of the key system.
121   std::vector<uint8> scheme_uuid_;
122
123   // media stream's frame URL.
124   const GURL frame_url_;
125
126   // Java MediaDrm instance.
127   base::android::ScopedJavaGlobalRef<jobject> j_media_drm_;
128
129   // Non-owned pointer.
130   MediaPlayerManager* manager_;
131
132   base::Closure media_crypto_ready_cb_;
133
134   ResetCredentialsCB reset_credentials_cb_;
135
136   DISALLOW_COPY_AND_ASSIGN(MediaDrmBridge);
137 };
138
139 }  // namespace media
140
141 #endif  // MEDIA_BASE_ANDROID_MEDIA_DRM_BRIDGE_H_