17d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)// Copyright (c) 2013 The Chromium Authors. All rights reserved.
27d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
37d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)// found in the LICENSE file.
47d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
57d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#ifndef MEDIA_BASE_ANDROID_MEDIA_DRM_BRIDGE_H_
67d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#define MEDIA_BASE_ANDROID_MEDIA_DRM_BRIDGE_H_
77d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
87d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include <jni.h>
97d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include <string>
107d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include <vector>
117d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
127d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "base/android/scoped_java_ref.h"
13424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)#include "base/callback.h"
1458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#include "base/memory/scoped_ptr.h"
1546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#include "media/base/browser_cdm.h"
167d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "media/base/media_export.h"
1746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#include "media/cdm/player_tracker_impl.h"
1868043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)#include "url/gurl.h"
1968043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)
2068043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)class GURL;
217d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
227d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)namespace media {
237d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
24eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochclass MediaPlayerManager;
25eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
267d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)// This class provides DRM services for android EME implementation.
277d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)// TODO(qinmin): implement all the functions in this class.
2846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)class MEDIA_EXPORT MediaDrmBridge : public BrowserCdm {
297d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) public:
30424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  enum SecurityLevel {
31424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    SECURITY_LEVEL_NONE = 0,
32424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    SECURITY_LEVEL_1 = 1,
33424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    SECURITY_LEVEL_3 = 3,
34424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  };
35424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
3668043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  typedef base::Callback<void(bool)> ResetCredentialsCB;
3768043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)
387d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  virtual ~MediaDrmBridge();
397d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
403551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  // Checks whether MediaDRM is available.
41a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // All other static methods check IsAvailable() internally. There's no need
42a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // to check IsAvailable() explicitly before calling them.
437d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  static bool IsAvailable();
447d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
45a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  static bool IsSecurityLevelSupported(const std::string& key_system,
465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                       SecurityLevel security_level);
4758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
48c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // Checks whether |key_system| is supported.
49c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  static bool IsKeySystemSupported(const std::string& key_system);
50c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch
51f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // Returns the list of the platform-supported key system names that
52f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // are not handled by Chrome explicitly.
53f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  static std::vector<std::string> GetPlatformKeySystemNames();
54f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
55c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // Checks whether |key_system| is supported with |container_mime_type|.
56c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // |container_mime_type| must not be empty.
57a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  static bool IsKeySystemSupportedWithType(
58a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      const std::string& key_system,
59a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      const std::string& container_mime_type);
6058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  static bool IsSecureDecoderRequired(SecurityLevel security_level);
6258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
633551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  static bool RegisterMediaDrmBridge(JNIEnv* env);
643551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
65a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // Returns a MediaDrmBridge instance if |key_system| is supported, or a NULL
665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // pointer otherwise.
67cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  static scoped_ptr<MediaDrmBridge> Create(
68cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      const std::string& key_system,
69cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      const SessionCreatedCB& session_created_cb,
70cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      const SessionMessageCB& session_message_cb,
71cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      const SessionReadyCB& session_ready_cb,
72cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      const SessionClosedCB& session_closed_cb,
73cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      const SessionErrorCB& session_error_cb);
74cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
75cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // Returns a MediaDrmBridge instance if |key_system| is supported, or a NULL
76cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // otherwise. No session callbacks are provided. This is used when we need to
77cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // use MediaDrmBridge without creating any sessions.
78cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  static scoped_ptr<MediaDrmBridge> CreateSessionless(
79cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      const std::string& key_system);
805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Returns true if |security_level| is successfully set, or false otherwise.
825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Call this function right after Create() and before any other calls.
835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Note:
845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // - If this function is not called, the default security level of the device
855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  //   will be used.
865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // - It's recommended to call this function only once on a MediaDrmBridge
875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  //   object. Calling this function multiples times may cause errors.
885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  bool SetSecurityLevel(SecurityLevel security_level);
895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
9046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  // BrowserCdm implementations.
91a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  virtual bool CreateSession(uint32 session_id,
925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                             const std::string& content_type,
93a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                             const uint8* init_data,
94a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                             int init_data_length) OVERRIDE;
955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual void LoadSession(uint32 session_id,
965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                           const std::string& web_session_id) OVERRIDE;
97a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  virtual void UpdateSession(uint32 session_id,
98a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                             const uint8* response,
99a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                             int response_length) OVERRIDE;
100a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  virtual void ReleaseSession(uint32 session_id) OVERRIDE;
10146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  virtual int RegisterPlayer(const base::Closure& new_key_cb,
10246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)                             const base::Closure& cdm_unset_cb) OVERRIDE;
10346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  virtual void UnregisterPlayer(int registration_id) OVERRIDE;
1047d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
105424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // Returns a MediaCrypto object if it's already created. Returns a null object
106424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // otherwise.
107424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  base::android::ScopedJavaLocalRef<jobject> GetMediaCrypto();
108424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
109424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // Sets callback which will be called when MediaCrypto is ready.
110424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // If |closure| is null, previously set callback will be cleared.
111424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  void SetMediaCryptoReadyCB(const base::Closure& closure);
112424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
113424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // Called after a MediaCrypto object is created.
114a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  void OnMediaCryptoReady(JNIEnv* env, jobject j_media_drm);
115a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
116a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // Callbacks for firing session events.
117a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  void OnSessionCreated(JNIEnv* env,
118a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                        jobject j_media_drm,
119a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                        jint j_session_id,
120a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                        jstring j_web_session_id);
121a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  void OnSessionMessage(JNIEnv* env,
122a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                        jobject j_media_drm,
123a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                        jint j_session_id,
124a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                        jbyteArray j_message,
125a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                        jstring j_destination_url);
126a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  void OnSessionReady(JNIEnv* env, jobject j_media_drm, jint j_session_id);
127a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  void OnSessionClosed(JNIEnv* env, jobject j_media_drm, jint j_session_id);
128a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  void OnSessionError(JNIEnv* env, jobject j_media_drm, jint j_session_id);
1293551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
13068043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  // Reset the device credentials.
13168043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  void ResetDeviceCredentials(const ResetCredentialsCB& callback);
13268043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)
13368043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  // Called by the java object when credential reset is completed.
13468043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  void OnResetDeviceCredentialsCompleted(JNIEnv* env, jobject, bool success);
13568043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)
136424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // Helper function to determine whether a protected surface is needed for the
137424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // video playback.
138424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  bool IsProtectedSurfaceRequired();
1397d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
1407d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) private:
141cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  MediaDrmBridge(const std::vector<uint8>& scheme_uuid,
142cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                 const SessionCreatedCB& session_created_cb,
143cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                 const SessionMessageCB& session_message_cb,
144cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                 const SessionReadyCB& session_ready_cb,
145cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                 const SessionClosedCB& session_closed_cb,
146cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                 const SessionErrorCB& session_error_cb);
147eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
148424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // Get the security level of the media.
149424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  SecurityLevel GetSecurityLevel();
150424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
151eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // UUID of the key system.
1523551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  std::vector<uint8> scheme_uuid_;
1533551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
1543551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  // Java MediaDrm instance.
1553551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  base::android::ScopedJavaGlobalRef<jobject> j_media_drm_;
156eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
157cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // Callbacks for firing session events.
158cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  SessionCreatedCB session_created_cb_;
159cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  SessionMessageCB session_message_cb_;
160cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  SessionReadyCB session_ready_cb_;
161cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  SessionClosedCB session_closed_cb_;
162cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  SessionErrorCB session_error_cb_;
163eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
164424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  base::Closure media_crypto_ready_cb_;
165424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
16668043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  ResetCredentialsCB reset_credentials_cb_;
16768043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)
16846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  PlayerTrackerImpl player_tracker_;
16946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
1707d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(MediaDrmBridge);
1717d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)};
1727d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
1737d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)}  // namespace media
1747d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
1757d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#endif  // MEDIA_BASE_ANDROID_MEDIA_DRM_BRIDGE_H_
176