media_drm_bridge.h revision e5d81f57cb97b3b6b7fccc9c5610d21eb81db09d
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
19class GURL;
20
21namespace media {
22
23class MediaPlayerManager;
24
25// This class provides DRM services for android EME implementation.
26// TODO(qinmin): implement all the functions in this class.
27class 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  // Checks whether MediaDRM is available.
40  // All other static methods check IsAvailable() internally. There's no need
41  // to check IsAvailable() explicitly before calling them.
42  static bool IsAvailable();
43
44  static bool IsSecurityLevelSupported(const std::string& key_system,
45                                       SecurityLevel security_level);
46
47  // TODO(xhwang): The |container_mime_type| is not the same as contentType in
48  // the EME spec. Revisit this once the spec issue with initData type is
49  // resolved.
50  static bool IsKeySystemSupportedWithType(
51      const std::string& key_system,
52      const std::string& container_mime_type);
53
54  static bool IsSecureDecoderRequired(SecurityLevel security_level);
55
56  static bool RegisterMediaDrmBridge(JNIEnv* env);
57
58  // Returns a MediaDrmBridge instance if |key_system| is supported, or a NULL
59  // pointer otherwise.
60  static scoped_ptr<MediaDrmBridge> Create(int cdm_id,
61                                           const std::string& key_system,
62                                           const GURL& security_origin,
63                                           MediaPlayerManager* manager);
64
65  // Returns true if |security_level| is successfully set, or false otherwise.
66  // Call this function right after Create() and before any other calls.
67  // Note:
68  // - If this function is not called, the default security level of the device
69  //   will be used.
70  // - It's recommended to call this function only once on a MediaDrmBridge
71  //   object. Calling this function multiples times may cause errors.
72  bool SetSecurityLevel(SecurityLevel security_level);
73
74  // MediaKeys implementations.
75  virtual bool CreateSession(uint32 session_id,
76                             const std::string& content_type,
77                             const uint8* init_data,
78                             int init_data_length) OVERRIDE;
79  virtual void LoadSession(uint32 session_id,
80                           const std::string& web_session_id) OVERRIDE;
81  virtual void UpdateSession(uint32 session_id,
82                             const uint8* response,
83                             int response_length) OVERRIDE;
84  virtual void ReleaseSession(uint32 session_id) OVERRIDE;
85
86  // Returns a MediaCrypto object if it's already created. Returns a null object
87  // otherwise.
88  base::android::ScopedJavaLocalRef<jobject> GetMediaCrypto();
89
90  // Sets callback which will be called when MediaCrypto is ready.
91  // If |closure| is null, previously set callback will be cleared.
92  void SetMediaCryptoReadyCB(const base::Closure& closure);
93
94  // Called after a MediaCrypto object is created.
95  void OnMediaCryptoReady(JNIEnv* env, jobject j_media_drm);
96
97  // Callbacks for firing session events.
98  void OnSessionCreated(JNIEnv* env,
99                        jobject j_media_drm,
100                        jint j_session_id,
101                        jstring j_web_session_id);
102  void OnSessionMessage(JNIEnv* env,
103                        jobject j_media_drm,
104                        jint j_session_id,
105                        jbyteArray j_message,
106                        jstring j_destination_url);
107  void OnSessionReady(JNIEnv* env, jobject j_media_drm, jint j_session_id);
108  void OnSessionClosed(JNIEnv* env, jobject j_media_drm, jint j_session_id);
109  void OnSessionError(JNIEnv* env, jobject j_media_drm, jint j_session_id);
110
111  // Reset the device credentials.
112  void ResetDeviceCredentials(const ResetCredentialsCB& callback);
113
114  // Called by the java object when credential reset is completed.
115  void OnResetDeviceCredentialsCompleted(JNIEnv* env, jobject, bool success);
116
117  // Helper function to determine whether a protected surface is needed for the
118  // video playback.
119  bool IsProtectedSurfaceRequired();
120
121  int cdm_id() const { return cdm_id_; }
122
123  const GURL& security_origin() const { return security_origin_; }
124
125 private:
126  MediaDrmBridge(int cdm_id,
127                 const std::vector<uint8>& scheme_uuid,
128                 const GURL& security_origin,
129                 MediaPlayerManager* manager);
130
131  // Get the security level of the media.
132  SecurityLevel GetSecurityLevel();
133
134  // ID of the CDM object.
135  int cdm_id_;
136
137  // UUID of the key system.
138  std::vector<uint8> scheme_uuid_;
139
140  // media stream's security origin.
141  const GURL security_origin_;
142
143  // Java MediaDrm instance.
144  base::android::ScopedJavaGlobalRef<jobject> j_media_drm_;
145
146  // Non-owned pointer.
147  MediaPlayerManager* manager_;
148
149  base::Closure media_crypto_ready_cb_;
150
151  ResetCredentialsCB reset_credentials_cb_;
152
153  DISALLOW_COPY_AND_ASSIGN(MediaDrmBridge);
154};
155
156}  // namespace media
157
158#endif  // MEDIA_BASE_ANDROID_MEDIA_DRM_BRIDGE_H_
159