media_drm_bridge.h revision 68043e1e95eeb07d5cae7aca370b26518b0867d6
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  // 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  static void set_can_use_media_drm(bool can_use_media_drm) {
106    can_use_media_drm_ = can_use_media_drm;
107  }
108
109 private:
110  static bool IsSecureDecoderRequired(SecurityLevel security_level);
111
112  static bool can_use_media_drm_;
113
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);
119
120  // Get the security level of the media.
121  SecurityLevel GetSecurityLevel();
122
123  // ID of the MediaKeys object.
124  int media_keys_id_;
125
126  // UUID of the key system.
127  std::vector<uint8> scheme_uuid_;
128
129  // media stream's frame URL.
130  const GURL frame_url_;
131
132  // Java MediaDrm instance.
133  base::android::ScopedJavaGlobalRef<jobject> j_media_drm_;
134
135  // Non-owned pointer.
136  MediaPlayerManager* manager_;
137
138  base::Closure media_crypto_ready_cb_;
139
140  ResetCredentialsCB reset_credentials_cb_;
141
142  DISALLOW_COPY_AND_ASSIGN(MediaDrmBridge);
143};
144
145}  // namespace media
146
147#endif  // MEDIA_BASE_ANDROID_MEDIA_DRM_BRIDGE_H_
148