OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "media/base/android/media_drm_bridge.h" | 5 #include "media/base/android/media_drm_bridge.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/android/build_info.h" | 9 #include "base/android/build_info.h" |
10 #include "base/android/jni_array.h" | 10 #include "base/android/jni_array.h" |
11 #include "base/android/jni_string.h" | 11 #include "base/android/jni_string.h" |
12 #include "base/callback_helpers.h" | 12 #include "base/callback_helpers.h" |
13 #include "base/containers/hash_tables.h" | 13 #include "base/containers/hash_tables.h" |
14 #include "base/lazy_instance.h" | 14 #include "base/lazy_instance.h" |
15 #include "base/location.h" | 15 #include "base/location.h" |
16 #include "base/logging.h" | 16 #include "base/logging.h" |
17 #include "base/message_loop/message_loop_proxy.h" | 17 #include "base/message_loop/message_loop_proxy.h" |
18 #include "base/strings/string_util.h" | 18 #include "base/strings/string_util.h" |
19 #include "base/sys_byteorder.h" | 19 #include "base/sys_byteorder.h" |
20 #include "base/sys_info.h" | 20 #include "base/sys_info.h" |
21 #include "jni/MediaDrmBridge_jni.h" | 21 #include "jni/MediaDrmBridge_jni.h" |
| 22 #include "media/base/cdm_key_information.h" |
22 | 23 |
23 #include "widevine_cdm_version.h" // In SHARED_INTERMEDIATE_DIR. | 24 #include "widevine_cdm_version.h" // In SHARED_INTERMEDIATE_DIR. |
24 | 25 |
25 using base::android::AttachCurrentThread; | 26 using base::android::AttachCurrentThread; |
26 using base::android::ConvertUTF8ToJavaString; | 27 using base::android::ConvertUTF8ToJavaString; |
27 using base::android::ConvertJavaStringToUTF8; | 28 using base::android::ConvertJavaStringToUTF8; |
28 using base::android::JavaByteArrayToByteVector; | 29 using base::android::JavaByteArrayToByteVector; |
29 using base::android::ScopedJavaLocalRef; | 30 using base::android::ScopedJavaLocalRef; |
30 | 31 |
31 namespace media { | 32 namespace media { |
32 | 33 |
33 static uint32 ReadUint32(const uint8_t* data) { | 34 namespace { |
| 35 |
| 36 // DrmBridge supports session expiration event but doesn't provide detailed |
| 37 // status for each key ID, which is required by the EME spec. Use a dummy key ID |
| 38 // here to report session expiration info. |
| 39 const char kDummyKeyId[] = "Dummy Key Id"; |
| 40 |
| 41 uint32 ReadUint32(const uint8_t* data) { |
34 uint32 value = 0; | 42 uint32 value = 0; |
35 for (int i = 0; i < 4; ++i) | 43 for (int i = 0; i < 4; ++i) |
36 value = (value << 8) | data[i]; | 44 value = (value << 8) | data[i]; |
37 return value; | 45 return value; |
38 } | 46 } |
39 | 47 |
40 static uint64 ReadUint64(const uint8_t* data) { | 48 uint64 ReadUint64(const uint8_t* data) { |
41 uint64 value = 0; | 49 uint64 value = 0; |
42 for (int i = 0; i < 8; ++i) | 50 for (int i = 0; i < 8; ++i) |
43 value = (value << 8) | data[i]; | 51 value = (value << 8) | data[i]; |
44 return value; | 52 return value; |
45 } | 53 } |
46 | 54 |
| 55 // Returns string session ID from jbyteArray (byte[] in Java). |
| 56 std::string GetSessionId(JNIEnv* env, jbyteArray j_session_id) { |
| 57 std::vector<uint8> session_id_vector; |
| 58 JavaByteArrayToByteVector(env, j_session_id, &session_id_vector); |
| 59 return std::string(session_id_vector.begin(), session_id_vector.end()); |
| 60 } |
| 61 |
47 // The structure of an ISO CENC Protection System Specific Header (PSSH) box is | 62 // The structure of an ISO CENC Protection System Specific Header (PSSH) box is |
48 // as follows. (See ISO/IEC FDIS 23001-7:2011(E).) | 63 // as follows. (See ISO/IEC FDIS 23001-7:2011(E).) |
49 // Note: ISO boxes use big-endian values. | 64 // Note: ISO boxes use big-endian values. |
50 // | 65 // |
51 // PSSH { | 66 // PSSH { |
52 // uint32 Size | 67 // uint32 Size |
53 // uint32 Type | 68 // uint32 Type |
54 // uint64 LargeSize # Field is only present if value(Size) == 1. | 69 // uint64 LargeSize # Field is only present if value(Size) == 1. |
55 // uint32 VersionAndFlags | 70 // uint32 VersionAndFlags |
56 // uint8[16] SystemId | 71 // uint8[16] SystemId |
57 // uint32 DataSize | 72 // uint32 DataSize |
58 // uint8[DataSize] Data | 73 // uint8[DataSize] Data |
59 // } | 74 // } |
60 const int kBoxHeaderSize = 8; // Box's header contains Size and Type. | 75 const int kBoxHeaderSize = 8; // Box's header contains Size and Type. |
61 const int kBoxLargeSizeSize = 8; | 76 const int kBoxLargeSizeSize = 8; |
62 const int kPsshVersionFlagSize = 4; | 77 const int kPsshVersionFlagSize = 4; |
63 const int kPsshSystemIdSize = 16; | 78 const int kPsshSystemIdSize = 16; |
64 const int kPsshDataSizeSize = 4; | 79 const int kPsshDataSizeSize = 4; |
65 const uint32 kTencType = 0x74656e63; | 80 const uint32 kTencType = 0x74656e63; |
66 const uint32 kPsshType = 0x70737368; | 81 const uint32 kPsshType = 0x70737368; |
67 const uint8 kWidevineUuid[16] = { | 82 const uint8 kWidevineUuid[16] = { |
68 0xED, 0xEF, 0x8B, 0xA9, 0x79, 0xD6, 0x4A, 0xCE, | 83 0xED, 0xEF, 0x8B, 0xA9, 0x79, 0xD6, 0x4A, 0xCE, |
69 0xA3, 0xC8, 0x27, 0xDC, 0xD5, 0x1D, 0x21, 0xED }; | 84 0xA3, 0xC8, 0x27, 0xDC, 0xD5, 0x1D, 0x21, 0xED }; |
70 | 85 |
71 typedef std::vector<uint8> UUID; | 86 typedef std::vector<uint8> UUID; |
72 | 87 |
73 class KeySystemUuidManager { | |
74 public: | |
75 KeySystemUuidManager(); | |
76 UUID GetUUID(const std::string& key_system); | |
77 void AddMapping(const std::string& key_system, const UUID& uuid); | |
78 std::vector<std::string> GetPlatformKeySystemNames(); | |
79 | |
80 private: | |
81 typedef base::hash_map<std::string, UUID> KeySystemUuidMap; | |
82 | |
83 KeySystemUuidMap key_system_uuid_map_; | |
84 | |
85 DISALLOW_COPY_AND_ASSIGN(KeySystemUuidManager); | |
86 }; | |
87 | |
88 KeySystemUuidManager::KeySystemUuidManager() { | |
89 // Widevine is always supported in Android. | |
90 key_system_uuid_map_[kWidevineKeySystem] = | |
91 UUID(kWidevineUuid, kWidevineUuid + arraysize(kWidevineUuid)); | |
92 } | |
93 | |
94 UUID KeySystemUuidManager::GetUUID(const std::string& key_system) { | |
95 KeySystemUuidMap::iterator it = key_system_uuid_map_.find(key_system); | |
96 if (it == key_system_uuid_map_.end()) | |
97 return UUID(); | |
98 return it->second; | |
99 } | |
100 | |
101 void KeySystemUuidManager::AddMapping(const std::string& key_system, | |
102 const UUID& uuid) { | |
103 KeySystemUuidMap::iterator it = key_system_uuid_map_.find(key_system); | |
104 DCHECK(it == key_system_uuid_map_.end()) | |
105 << "Shouldn't overwrite an existing key system."; | |
106 if (it != key_system_uuid_map_.end()) | |
107 return; | |
108 key_system_uuid_map_[key_system] = uuid; | |
109 } | |
110 | |
111 std::vector<std::string> KeySystemUuidManager::GetPlatformKeySystemNames() { | |
112 std::vector<std::string> key_systems; | |
113 for (KeySystemUuidMap::iterator it = key_system_uuid_map_.begin(); | |
114 it != key_system_uuid_map_.end(); ++it) { | |
115 // Rule out the key system handled by Chrome explicitly. | |
116 if (it->first != kWidevineKeySystem) | |
117 key_systems.push_back(it->first); | |
118 } | |
119 return key_systems; | |
120 } | |
121 | |
122 base::LazyInstance<KeySystemUuidManager>::Leaky g_key_system_uuid_manager = | |
123 LAZY_INSTANCE_INITIALIZER; | |
124 | |
125 // Tries to find a PSSH box whose "SystemId" is |uuid| in |data|, parses the | 88 // Tries to find a PSSH box whose "SystemId" is |uuid| in |data|, parses the |
126 // "Data" of the box and put it in |pssh_data|. Returns true if such a box is | 89 // "Data" of the box and put it in |pssh_data|. Returns true if such a box is |
127 // found and successfully parsed. Returns false otherwise. | 90 // found and successfully parsed. Returns false otherwise. |
128 // Notes: | 91 // Notes: |
129 // 1, If multiple PSSH boxes are found,the "Data" of the first matching PSSH box | 92 // 1, If multiple PSSH boxes are found,the "Data" of the first matching PSSH box |
130 // will be set in |pssh_data|. | 93 // will be set in |pssh_data|. |
131 // 2, Only PSSH and TENC boxes are allowed in |data|. TENC boxes are skipped. | 94 // 2, Only PSSH and TENC boxes are allowed in |data|. TENC boxes are skipped. |
132 static bool GetPsshData(const uint8* data, int data_size, | 95 bool GetPsshData(const uint8* data, |
133 const UUID& uuid, | 96 int data_size, |
134 std::vector<uint8>* pssh_data) { | 97 const UUID& uuid, |
| 98 std::vector<uint8>* pssh_data) { |
135 const uint8* cur = data; | 99 const uint8* cur = data; |
136 const uint8* data_end = data + data_size; | 100 const uint8* data_end = data + data_size; |
137 int bytes_left = data_size; | 101 int bytes_left = data_size; |
138 | 102 |
139 while (bytes_left > 0) { | 103 while (bytes_left > 0) { |
140 const uint8* box_head = cur; | 104 const uint8* box_head = cur; |
141 | 105 |
142 if (bytes_left < kBoxHeaderSize) | 106 if (bytes_left < kBoxHeaderSize) |
143 return false; | 107 return false; |
144 | 108 |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
199 if (box_end < cur + data_size) | 163 if (box_end < cur + data_size) |
200 return false; | 164 return false; |
201 | 165 |
202 pssh_data->assign(cur, cur + data_size); | 166 pssh_data->assign(cur, cur + data_size); |
203 return true; | 167 return true; |
204 } | 168 } |
205 | 169 |
206 return false; | 170 return false; |
207 } | 171 } |
208 | 172 |
209 static MediaDrmBridge::SecurityLevel GetSecurityLevelFromString( | 173 class KeySystemUuidManager { |
210 const std::string& security_level_str) { | 174 public: |
211 if (0 == security_level_str.compare("L1")) | 175 KeySystemUuidManager(); |
212 return MediaDrmBridge::SECURITY_LEVEL_1; | 176 UUID GetUUID(const std::string& key_system); |
213 if (0 == security_level_str.compare("L3")) | 177 void AddMapping(const std::string& key_system, const UUID& uuid); |
214 return MediaDrmBridge::SECURITY_LEVEL_3; | 178 std::vector<std::string> GetPlatformKeySystemNames(); |
215 DCHECK(security_level_str.empty()); | 179 |
216 return MediaDrmBridge::SECURITY_LEVEL_NONE; | 180 private: |
| 181 typedef base::hash_map<std::string, UUID> KeySystemUuidMap; |
| 182 |
| 183 KeySystemUuidMap key_system_uuid_map_; |
| 184 |
| 185 DISALLOW_COPY_AND_ASSIGN(KeySystemUuidManager); |
| 186 }; |
| 187 |
| 188 KeySystemUuidManager::KeySystemUuidManager() { |
| 189 // Widevine is always supported in Android. |
| 190 key_system_uuid_map_[kWidevineKeySystem] = |
| 191 UUID(kWidevineUuid, kWidevineUuid + arraysize(kWidevineUuid)); |
217 } | 192 } |
218 | 193 |
219 static std::string GetSecurityLevelString( | 194 UUID KeySystemUuidManager::GetUUID(const std::string& key_system) { |
220 MediaDrmBridge::SecurityLevel security_level) { | 195 KeySystemUuidMap::iterator it = key_system_uuid_map_.find(key_system); |
221 switch (security_level) { | 196 if (it == key_system_uuid_map_.end()) |
222 case MediaDrmBridge::SECURITY_LEVEL_NONE: | 197 return UUID(); |
223 return ""; | 198 return it->second; |
224 case MediaDrmBridge::SECURITY_LEVEL_1: | 199 } |
225 return "L1"; | 200 |
226 case MediaDrmBridge::SECURITY_LEVEL_3: | 201 void KeySystemUuidManager::AddMapping(const std::string& key_system, |
227 return "L3"; | 202 const UUID& uuid) { |
| 203 KeySystemUuidMap::iterator it = key_system_uuid_map_.find(key_system); |
| 204 DCHECK(it == key_system_uuid_map_.end()) |
| 205 << "Shouldn't overwrite an existing key system."; |
| 206 if (it != key_system_uuid_map_.end()) |
| 207 return; |
| 208 key_system_uuid_map_[key_system] = uuid; |
| 209 } |
| 210 |
| 211 std::vector<std::string> KeySystemUuidManager::GetPlatformKeySystemNames() { |
| 212 std::vector<std::string> key_systems; |
| 213 for (KeySystemUuidMap::iterator it = key_system_uuid_map_.begin(); |
| 214 it != key_system_uuid_map_.end(); ++it) { |
| 215 // Rule out the key system handled by Chrome explicitly. |
| 216 if (it->first != kWidevineKeySystem) |
| 217 key_systems.push_back(it->first); |
228 } | 218 } |
229 return ""; | 219 return key_systems; |
230 } | 220 } |
231 | 221 |
| 222 base::LazyInstance<KeySystemUuidManager>::Leaky g_key_system_uuid_manager = |
| 223 LAZY_INSTANCE_INITIALIZER; |
| 224 |
232 // Checks whether |key_system| is supported with |container_mime_type|. Only | 225 // Checks whether |key_system| is supported with |container_mime_type|. Only |
233 // checks |key_system| support if |container_mime_type| is empty. | 226 // checks |key_system| support if |container_mime_type| is empty. |
234 // TODO(xhwang): The |container_mime_type| is not the same as contentType in | 227 // TODO(xhwang): The |container_mime_type| is not the same as contentType in |
235 // the EME spec. Revisit this once the spec issue with initData type is | 228 // the EME spec. Revisit this once the spec issue with initData type is |
236 // resolved. | 229 // resolved. |
237 static bool IsKeySystemSupportedWithTypeImpl( | 230 bool IsKeySystemSupportedWithTypeImpl(const std::string& key_system, |
238 const std::string& key_system, | 231 const std::string& container_mime_type) { |
239 const std::string& container_mime_type) { | |
240 if (!MediaDrmBridge::IsAvailable()) | 232 if (!MediaDrmBridge::IsAvailable()) |
241 return false; | 233 return false; |
242 | 234 |
243 UUID scheme_uuid = g_key_system_uuid_manager.Get().GetUUID(key_system); | 235 UUID scheme_uuid = g_key_system_uuid_manager.Get().GetUUID(key_system); |
244 if (scheme_uuid.empty()) | 236 if (scheme_uuid.empty()) |
245 return false; | 237 return false; |
246 | 238 |
247 JNIEnv* env = AttachCurrentThread(); | 239 JNIEnv* env = AttachCurrentThread(); |
248 ScopedJavaLocalRef<jbyteArray> j_scheme_uuid = | 240 ScopedJavaLocalRef<jbyteArray> j_scheme_uuid = |
249 base::android::ToJavaByteArray(env, &scheme_uuid[0], scheme_uuid.size()); | 241 base::android::ToJavaByteArray(env, &scheme_uuid[0], scheme_uuid.size()); |
250 ScopedJavaLocalRef<jstring> j_container_mime_type = | 242 ScopedJavaLocalRef<jstring> j_container_mime_type = |
251 ConvertUTF8ToJavaString(env, container_mime_type); | 243 ConvertUTF8ToJavaString(env, container_mime_type); |
252 return Java_MediaDrmBridge_isCryptoSchemeSupported( | 244 return Java_MediaDrmBridge_isCryptoSchemeSupported( |
253 env, j_scheme_uuid.obj(), j_container_mime_type.obj()); | 245 env, j_scheme_uuid.obj(), j_container_mime_type.obj()); |
254 } | 246 } |
255 | 247 |
| 248 MediaDrmBridge::SecurityLevel GetSecurityLevelFromString( |
| 249 const std::string& security_level_str) { |
| 250 if (0 == security_level_str.compare("L1")) |
| 251 return MediaDrmBridge::SECURITY_LEVEL_1; |
| 252 if (0 == security_level_str.compare("L3")) |
| 253 return MediaDrmBridge::SECURITY_LEVEL_3; |
| 254 DCHECK(security_level_str.empty()); |
| 255 return MediaDrmBridge::SECURITY_LEVEL_NONE; |
| 256 } |
| 257 |
| 258 std::string GetSecurityLevelString( |
| 259 MediaDrmBridge::SecurityLevel security_level) { |
| 260 switch (security_level) { |
| 261 case MediaDrmBridge::SECURITY_LEVEL_NONE: |
| 262 return ""; |
| 263 case MediaDrmBridge::SECURITY_LEVEL_1: |
| 264 return "L1"; |
| 265 case MediaDrmBridge::SECURITY_LEVEL_3: |
| 266 return "L3"; |
| 267 } |
| 268 return ""; |
| 269 } |
| 270 |
| 271 } // namespace |
| 272 |
| 273 // Called by Java. |
| 274 static void AddKeySystemUuidMapping(JNIEnv* env, |
| 275 jclass clazz, |
| 276 jstring j_key_system, |
| 277 jobject j_buffer) { |
| 278 std::string key_system = ConvertJavaStringToUTF8(env, j_key_system); |
| 279 uint8* buffer = static_cast<uint8*>(env->GetDirectBufferAddress(j_buffer)); |
| 280 UUID uuid(buffer, buffer + 16); |
| 281 g_key_system_uuid_manager.Get().AddMapping(key_system, uuid); |
| 282 } |
| 283 |
256 // static | 284 // static |
257 bool MediaDrmBridge::IsAvailable() { | 285 bool MediaDrmBridge::IsAvailable() { |
258 if (base::android::BuildInfo::GetInstance()->sdk_int() < 19) | 286 if (base::android::BuildInfo::GetInstance()->sdk_int() < 19) |
259 return false; | 287 return false; |
260 | 288 |
261 int32 os_major_version = 0; | 289 int32 os_major_version = 0; |
262 int32 os_minor_version = 0; | 290 int32 os_minor_version = 0; |
263 int32 os_bugfix_version = 0; | 291 int32 os_bugfix_version = 0; |
264 base::SysInfo::OperatingSystemVersionNumbers(&os_major_version, | 292 base::SysInfo::OperatingSystemVersionNumbers(&os_major_version, |
265 &os_minor_version, | 293 &os_minor_version, |
(...skipping 10 matching lines...) Expand all Loading... |
276 return SECURITY_LEVEL_1 == security_level; | 304 return SECURITY_LEVEL_1 == security_level; |
277 } | 305 } |
278 | 306 |
279 // static | 307 // static |
280 bool MediaDrmBridge::IsSecurityLevelSupported(const std::string& key_system, | 308 bool MediaDrmBridge::IsSecurityLevelSupported(const std::string& key_system, |
281 SecurityLevel security_level) { | 309 SecurityLevel security_level) { |
282 if (!IsAvailable()) | 310 if (!IsAvailable()) |
283 return false; | 311 return false; |
284 | 312 |
285 scoped_ptr<MediaDrmBridge> media_drm_bridge = | 313 scoped_ptr<MediaDrmBridge> media_drm_bridge = |
286 MediaDrmBridge::CreateSessionless(key_system); | 314 MediaDrmBridge::CreateWithoutSessionSupport(key_system); |
287 if (!media_drm_bridge) | 315 if (!media_drm_bridge) |
288 return false; | 316 return false; |
289 | 317 |
290 return media_drm_bridge->SetSecurityLevel(security_level); | 318 return media_drm_bridge->SetSecurityLevel(security_level); |
291 } | 319 } |
292 | 320 |
293 static void AddKeySystemUuidMapping(JNIEnv* env, jclass clazz, | |
294 jstring j_key_system, | |
295 jobject j_buffer) { | |
296 std::string key_system = ConvertJavaStringToUTF8(env, j_key_system); | |
297 uint8* buffer = static_cast<uint8*>(env->GetDirectBufferAddress(j_buffer)); | |
298 UUID uuid(buffer, buffer + 16); | |
299 g_key_system_uuid_manager.Get().AddMapping(key_system, uuid); | |
300 } | |
301 | |
302 // static | 321 // static |
303 std::vector<std::string> MediaDrmBridge::GetPlatformKeySystemNames() { | 322 std::vector<std::string> MediaDrmBridge::GetPlatformKeySystemNames() { |
304 return g_key_system_uuid_manager.Get().GetPlatformKeySystemNames(); | 323 return g_key_system_uuid_manager.Get().GetPlatformKeySystemNames(); |
305 } | 324 } |
306 | 325 |
307 // static | 326 // static |
308 bool MediaDrmBridge::IsKeySystemSupported(const std::string& key_system) { | 327 bool MediaDrmBridge::IsKeySystemSupported(const std::string& key_system) { |
309 DCHECK(!key_system.empty()); | 328 DCHECK(!key_system.empty()); |
310 return IsKeySystemSupportedWithTypeImpl(key_system, ""); | 329 return IsKeySystemSupportedWithTypeImpl(key_system, ""); |
311 } | 330 } |
312 | 331 |
313 // static | 332 // static |
314 bool MediaDrmBridge::IsKeySystemSupportedWithType( | 333 bool MediaDrmBridge::IsKeySystemSupportedWithType( |
315 const std::string& key_system, | 334 const std::string& key_system, |
316 const std::string& container_mime_type) { | 335 const std::string& container_mime_type) { |
317 DCHECK(!key_system.empty() && !container_mime_type.empty()); | 336 DCHECK(!key_system.empty() && !container_mime_type.empty()); |
318 return IsKeySystemSupportedWithTypeImpl(key_system, container_mime_type); | 337 return IsKeySystemSupportedWithTypeImpl(key_system, container_mime_type); |
319 } | 338 } |
320 | 339 |
321 bool MediaDrmBridge::RegisterMediaDrmBridge(JNIEnv* env) { | 340 bool MediaDrmBridge::RegisterMediaDrmBridge(JNIEnv* env) { |
322 return RegisterNativesImpl(env); | 341 return RegisterNativesImpl(env); |
323 } | 342 } |
324 | 343 |
325 MediaDrmBridge::MediaDrmBridge(const std::vector<uint8>& scheme_uuid, | 344 MediaDrmBridge::MediaDrmBridge( |
326 const SessionCreatedCB& session_created_cb, | 345 const std::vector<uint8>& scheme_uuid, |
327 const SessionMessageCB& session_message_cb, | 346 const SessionMessageCB& session_message_cb, |
328 const SessionReadyCB& session_ready_cb, | 347 const SessionClosedCB& session_closed_cb, |
329 const SessionClosedCB& session_closed_cb, | 348 const SessionErrorCB& session_error_cb, |
330 const SessionErrorCB& session_error_cb) | 349 const SessionKeysChangeCB& session_keys_change_cb) |
331 : scheme_uuid_(scheme_uuid), | 350 : scheme_uuid_(scheme_uuid), |
332 session_created_cb_(session_created_cb), | |
333 session_message_cb_(session_message_cb), | 351 session_message_cb_(session_message_cb), |
334 session_ready_cb_(session_ready_cb), | |
335 session_closed_cb_(session_closed_cb), | 352 session_closed_cb_(session_closed_cb), |
336 session_error_cb_(session_error_cb) { | 353 session_error_cb_(session_error_cb), |
| 354 session_keys_change_cb_(session_keys_change_cb) { |
337 JNIEnv* env = AttachCurrentThread(); | 355 JNIEnv* env = AttachCurrentThread(); |
338 CHECK(env); | 356 CHECK(env); |
339 | 357 |
340 ScopedJavaLocalRef<jbyteArray> j_scheme_uuid = | 358 ScopedJavaLocalRef<jbyteArray> j_scheme_uuid = |
341 base::android::ToJavaByteArray(env, &scheme_uuid[0], scheme_uuid.size()); | 359 base::android::ToJavaByteArray(env, &scheme_uuid[0], scheme_uuid.size()); |
342 j_media_drm_.Reset(Java_MediaDrmBridge_create( | 360 j_media_drm_.Reset(Java_MediaDrmBridge_create( |
343 env, j_scheme_uuid.obj(), reinterpret_cast<intptr_t>(this))); | 361 env, j_scheme_uuid.obj(), reinterpret_cast<intptr_t>(this))); |
344 } | 362 } |
345 | 363 |
346 MediaDrmBridge::~MediaDrmBridge() { | 364 MediaDrmBridge::~MediaDrmBridge() { |
347 JNIEnv* env = AttachCurrentThread(); | 365 JNIEnv* env = AttachCurrentThread(); |
348 player_tracker_.NotifyCdmUnset(); | 366 player_tracker_.NotifyCdmUnset(); |
349 if (!j_media_drm_.is_null()) | 367 if (!j_media_drm_.is_null()) |
350 Java_MediaDrmBridge_release(env, j_media_drm_.obj()); | 368 Java_MediaDrmBridge_release(env, j_media_drm_.obj()); |
351 } | 369 } |
352 | 370 |
353 // static | 371 // static |
| 372 // TODO(xhwang): Enable SessionExpirationUpdateCB when it is supported. |
354 scoped_ptr<MediaDrmBridge> MediaDrmBridge::Create( | 373 scoped_ptr<MediaDrmBridge> MediaDrmBridge::Create( |
355 const std::string& key_system, | 374 const std::string& key_system, |
356 const SessionCreatedCB& session_created_cb, | |
357 const SessionMessageCB& session_message_cb, | 375 const SessionMessageCB& session_message_cb, |
358 const SessionReadyCB& session_ready_cb, | |
359 const SessionClosedCB& session_closed_cb, | 376 const SessionClosedCB& session_closed_cb, |
360 const SessionErrorCB& session_error_cb) { | 377 const SessionErrorCB& session_error_cb, |
| 378 const SessionKeysChangeCB& session_keys_change_cb, |
| 379 const SessionExpirationUpdateCB& /* session_expiration_update_cb */) { |
361 scoped_ptr<MediaDrmBridge> media_drm_bridge; | 380 scoped_ptr<MediaDrmBridge> media_drm_bridge; |
362 if (!IsAvailable()) | 381 if (!IsAvailable()) |
363 return media_drm_bridge.Pass(); | 382 return media_drm_bridge.Pass(); |
364 | 383 |
365 UUID scheme_uuid = g_key_system_uuid_manager.Get().GetUUID(key_system); | 384 UUID scheme_uuid = g_key_system_uuid_manager.Get().GetUUID(key_system); |
366 if (scheme_uuid.empty()) | 385 if (scheme_uuid.empty()) |
367 return media_drm_bridge.Pass(); | 386 return media_drm_bridge.Pass(); |
368 | 387 |
369 media_drm_bridge.reset(new MediaDrmBridge(scheme_uuid, | 388 media_drm_bridge.reset(new MediaDrmBridge(scheme_uuid, session_message_cb, |
370 session_created_cb, | 389 session_closed_cb, session_error_cb, |
371 session_message_cb, | 390 session_keys_change_cb)); |
372 session_ready_cb, | |
373 session_closed_cb, | |
374 session_error_cb)); | |
375 | 391 |
376 if (media_drm_bridge->j_media_drm_.is_null()) | 392 if (media_drm_bridge->j_media_drm_.is_null()) |
377 media_drm_bridge.reset(); | 393 media_drm_bridge.reset(); |
378 | 394 |
379 return media_drm_bridge.Pass(); | 395 return media_drm_bridge.Pass(); |
380 } | 396 } |
381 | 397 |
382 // static | 398 // static |
383 scoped_ptr<MediaDrmBridge> MediaDrmBridge::CreateSessionless( | 399 scoped_ptr<MediaDrmBridge> MediaDrmBridge::CreateWithoutSessionSupport( |
384 const std::string& key_system) { | 400 const std::string& key_system) { |
385 return MediaDrmBridge::Create(key_system, | 401 return MediaDrmBridge::Create( |
386 SessionCreatedCB(), | 402 key_system, SessionMessageCB(), SessionClosedCB(), SessionErrorCB(), |
387 SessionMessageCB(), | 403 SessionKeysChangeCB(), SessionExpirationUpdateCB()); |
388 SessionReadyCB(), | |
389 SessionClosedCB(), | |
390 SessionErrorCB()); | |
391 } | 404 } |
392 | 405 |
393 bool MediaDrmBridge::SetSecurityLevel(SecurityLevel security_level) { | 406 bool MediaDrmBridge::SetSecurityLevel(SecurityLevel security_level) { |
394 JNIEnv* env = AttachCurrentThread(); | 407 JNIEnv* env = AttachCurrentThread(); |
395 | 408 |
396 std::string security_level_str = GetSecurityLevelString(security_level); | 409 std::string security_level_str = GetSecurityLevelString(security_level); |
397 if (security_level_str.empty()) | 410 if (security_level_str.empty()) |
398 return false; | 411 return false; |
399 | 412 |
400 ScopedJavaLocalRef<jstring> j_security_level = | 413 ScopedJavaLocalRef<jstring> j_security_level = |
401 ConvertUTF8ToJavaString(env, security_level_str); | 414 ConvertUTF8ToJavaString(env, security_level_str); |
402 return Java_MediaDrmBridge_setSecurityLevel( | 415 return Java_MediaDrmBridge_setSecurityLevel( |
403 env, j_media_drm_.obj(), j_security_level.obj()); | 416 env, j_media_drm_.obj(), j_security_level.obj()); |
404 } | 417 } |
405 | 418 |
406 bool MediaDrmBridge::CreateSession(uint32 session_id, | 419 void MediaDrmBridge::SetServerCertificate( |
407 const std::string& content_type, | 420 const uint8* certificate_data, |
408 const uint8* init_data, | 421 int certificate_data_length, |
409 int init_data_length) { | 422 scoped_ptr<media::SimpleCdmPromise> promise) { |
| 423 promise->reject(NOT_SUPPORTED_ERROR, 0, |
| 424 "SetServerCertificate() is not supported."); |
| 425 } |
| 426 |
| 427 void MediaDrmBridge::CreateSessionAndGenerateRequest( |
| 428 SessionType session_type, |
| 429 const std::string& init_data_type, |
| 430 const uint8* init_data, |
| 431 int init_data_length, |
| 432 scoped_ptr<media::NewSessionCdmPromise> promise) { |
410 DVLOG(1) << __FUNCTION__; | 433 DVLOG(1) << __FUNCTION__; |
411 | 434 |
412 DCHECK(!session_created_cb_.is_null()) | 435 if (session_type != media::MediaKeys::TEMPORARY_SESSION) { |
413 << "CreateSession called on a sessionless MediaDrmBridge object."; | 436 promise->reject(NOT_SUPPORTED_ERROR, 0, |
| 437 "Only the temporary session type is supported."); |
| 438 return; |
| 439 } |
414 | 440 |
415 JNIEnv* env = AttachCurrentThread(); | 441 JNIEnv* env = AttachCurrentThread(); |
416 ScopedJavaLocalRef<jbyteArray> j_init_data; | 442 ScopedJavaLocalRef<jbyteArray> j_init_data; |
417 // Caller should always use "video/*" content types. | 443 // Caller should always use "video/*" content types. |
418 DCHECK_EQ(0u, content_type.find("video/")); | 444 DCHECK_EQ(0u, init_data_type.find("video/")); |
419 | 445 |
420 // Widevine MediaDrm plugin only accepts the "data" part of the PSSH box as | 446 // Widevine MediaDrm plugin only accepts the "data" part of the PSSH box as |
421 // the init data when using MP4 container. | 447 // the init data when using MP4 container. |
422 if (std::equal(scheme_uuid_.begin(), scheme_uuid_.end(), kWidevineUuid) && | 448 if (std::equal(scheme_uuid_.begin(), scheme_uuid_.end(), kWidevineUuid) && |
423 content_type == "video/mp4") { | 449 init_data_type == "video/mp4") { |
424 std::vector<uint8> pssh_data; | 450 std::vector<uint8> pssh_data; |
425 if (!GetPsshData(init_data, init_data_length, scheme_uuid_, &pssh_data)) | 451 if (!GetPsshData(init_data, init_data_length, scheme_uuid_, &pssh_data)) { |
426 return false; | 452 promise->reject(INVALID_ACCESS_ERROR, 0, "Invalid PSSH data."); |
| 453 return; |
| 454 } |
427 j_init_data = | 455 j_init_data = |
428 base::android::ToJavaByteArray(env, &pssh_data[0], pssh_data.size()); | 456 base::android::ToJavaByteArray(env, &pssh_data[0], pssh_data.size()); |
429 } else { | 457 } else { |
430 j_init_data = | 458 j_init_data = |
431 base::android::ToJavaByteArray(env, init_data, init_data_length); | 459 base::android::ToJavaByteArray(env, init_data, init_data_length); |
432 } | 460 } |
433 | 461 |
434 ScopedJavaLocalRef<jstring> j_mime = | 462 ScopedJavaLocalRef<jstring> j_mime = |
435 ConvertUTF8ToJavaString(env, content_type); | 463 ConvertUTF8ToJavaString(env, init_data_type); |
436 Java_MediaDrmBridge_createSession( | 464 uint32_t promise_id = cdm_promise_adapter_.SavePromise(promise.Pass()); |
437 env, j_media_drm_.obj(), session_id, j_init_data.obj(), j_mime.obj()); | 465 Java_MediaDrmBridge_createSession(env, j_media_drm_.obj(), j_init_data.obj(), |
438 return true; | 466 j_mime.obj(), promise_id); |
439 } | 467 } |
440 | 468 |
441 void MediaDrmBridge::LoadSession(uint32 session_id, | 469 void MediaDrmBridge::LoadSession( |
442 const std::string& web_session_id) { | 470 SessionType session_type, |
443 // MediaDrmBridge doesn't support loading sessions. | 471 const std::string& session_id, |
444 NOTREACHED(); | 472 scoped_ptr<media::NewSessionCdmPromise> promise) { |
| 473 promise->reject(NOT_SUPPORTED_ERROR, 0, "LoadSession() is not supported."); |
445 } | 474 } |
446 | 475 |
447 void MediaDrmBridge::UpdateSession(uint32 session_id, | 476 void MediaDrmBridge::UpdateSession( |
448 const uint8* response, | 477 const std::string& session_id, |
449 int response_length) { | 478 const uint8* response, |
| 479 int response_length, |
| 480 scoped_ptr<media::SimpleCdmPromise> promise) { |
450 DVLOG(1) << __FUNCTION__; | 481 DVLOG(1) << __FUNCTION__; |
451 | 482 |
452 DCHECK(!session_ready_cb_.is_null()) | |
453 << __FUNCTION__ << " called on a sessionless MediaDrmBridge object."; | |
454 | |
455 JNIEnv* env = AttachCurrentThread(); | 483 JNIEnv* env = AttachCurrentThread(); |
456 ScopedJavaLocalRef<jbyteArray> j_response = | 484 ScopedJavaLocalRef<jbyteArray> j_response = |
457 base::android::ToJavaByteArray(env, response, response_length); | 485 base::android::ToJavaByteArray(env, response, response_length); |
458 Java_MediaDrmBridge_updateSession( | 486 ScopedJavaLocalRef<jbyteArray> j_session_id = base::android::ToJavaByteArray( |
459 env, j_media_drm_.obj(), session_id, j_response.obj()); | 487 env, reinterpret_cast<const uint8_t*>(session_id.data()), |
| 488 session_id.size()); |
| 489 uint32_t promise_id = cdm_promise_adapter_.SavePromise(promise.Pass()); |
| 490 Java_MediaDrmBridge_updateSession(env, j_media_drm_.obj(), j_session_id.obj(), |
| 491 j_response.obj(), promise_id); |
460 } | 492 } |
461 | 493 |
462 void MediaDrmBridge::ReleaseSession(uint32 session_id) { | 494 void MediaDrmBridge::CloseSession(const std::string& session_id, |
| 495 scoped_ptr<media::SimpleCdmPromise> promise) { |
463 DVLOG(1) << __FUNCTION__; | 496 DVLOG(1) << __FUNCTION__; |
| 497 JNIEnv* env = AttachCurrentThread(); |
| 498 ScopedJavaLocalRef<jbyteArray> j_session_id = base::android::ToJavaByteArray( |
| 499 env, reinterpret_cast<const uint8_t*>(session_id.data()), |
| 500 session_id.size()); |
| 501 uint32_t promise_id = cdm_promise_adapter_.SavePromise(promise.Pass()); |
| 502 Java_MediaDrmBridge_closeSession(env, j_media_drm_.obj(), j_session_id.obj(), |
| 503 promise_id); |
| 504 } |
464 | 505 |
465 DCHECK(!session_closed_cb_.is_null()) | 506 void MediaDrmBridge::RemoveSession( |
466 << __FUNCTION__ << " called on a sessionless MediaDrmBridge object."; | 507 const std::string& session_id, |
| 508 scoped_ptr<media::SimpleCdmPromise> promise) { |
| 509 promise->reject(NOT_SUPPORTED_ERROR, 0, "RemoveSession() is not supported."); |
| 510 } |
467 | 511 |
468 JNIEnv* env = AttachCurrentThread(); | 512 CdmContext* MediaDrmBridge::GetCdmContext() { |
469 Java_MediaDrmBridge_releaseSession(env, j_media_drm_.obj(), session_id); | 513 NOTREACHED(); |
| 514 return nullptr; |
470 } | 515 } |
471 | 516 |
472 int MediaDrmBridge::RegisterPlayer(const base::Closure& new_key_cb, | 517 int MediaDrmBridge::RegisterPlayer(const base::Closure& new_key_cb, |
473 const base::Closure& cdm_unset_cb) { | 518 const base::Closure& cdm_unset_cb) { |
474 return player_tracker_.RegisterPlayer(new_key_cb, cdm_unset_cb); | 519 return player_tracker_.RegisterPlayer(new_key_cb, cdm_unset_cb); |
475 } | 520 } |
476 | 521 |
477 void MediaDrmBridge::UnregisterPlayer(int registration_id) { | 522 void MediaDrmBridge::UnregisterPlayer(int registration_id) { |
478 player_tracker_.UnregisterPlayer(registration_id); | 523 player_tracker_.UnregisterPlayer(registration_id); |
479 } | 524 } |
(...skipping 13 matching lines...) Expand all Loading... |
493 | 538 |
494 media_crypto_ready_cb_ = closure; | 539 media_crypto_ready_cb_ = closure; |
495 } | 540 } |
496 | 541 |
497 void MediaDrmBridge::OnMediaCryptoReady(JNIEnv* env, jobject) { | 542 void MediaDrmBridge::OnMediaCryptoReady(JNIEnv* env, jobject) { |
498 DCHECK(!GetMediaCrypto().is_null()); | 543 DCHECK(!GetMediaCrypto().is_null()); |
499 if (!media_crypto_ready_cb_.is_null()) | 544 if (!media_crypto_ready_cb_.is_null()) |
500 base::ResetAndReturn(&media_crypto_ready_cb_).Run(); | 545 base::ResetAndReturn(&media_crypto_ready_cb_).Run(); |
501 } | 546 } |
502 | 547 |
503 void MediaDrmBridge::OnSessionCreated(JNIEnv* env, | 548 void MediaDrmBridge::OnPromiseResolved(JNIEnv* env, |
504 jobject j_media_drm, | 549 jobject j_media_drm, |
505 jint j_session_id, | 550 jint j_promise_id) { |
506 jstring j_web_session_id) { | 551 cdm_promise_adapter_.ResolvePromise(j_promise_id); |
507 uint32 session_id = j_session_id; | 552 } |
508 std::string web_session_id = ConvertJavaStringToUTF8(env, j_web_session_id); | 553 |
509 session_created_cb_.Run(session_id, web_session_id); | 554 void MediaDrmBridge::OnPromiseResolvedWithSession(JNIEnv* env, |
| 555 jobject j_media_drm, |
| 556 jint j_promise_id, |
| 557 jbyteArray j_session_id) { |
| 558 cdm_promise_adapter_.ResolvePromise(j_promise_id, |
| 559 GetSessionId(env, j_session_id)); |
| 560 } |
| 561 |
| 562 void MediaDrmBridge::OnPromiseRejected(JNIEnv* env, |
| 563 jobject j_media_drm, |
| 564 jint j_promise_id, |
| 565 jstring j_error_message) { |
| 566 std::string error_message = ConvertJavaStringToUTF8(env, j_error_message); |
| 567 cdm_promise_adapter_.RejectPromise(j_promise_id, MediaKeys::UNKNOWN_ERROR, 0, |
| 568 error_message); |
510 } | 569 } |
511 | 570 |
512 void MediaDrmBridge::OnSessionMessage(JNIEnv* env, | 571 void MediaDrmBridge::OnSessionMessage(JNIEnv* env, |
513 jobject j_media_drm, | 572 jobject j_media_drm, |
514 jint j_session_id, | 573 jbyteArray j_session_id, |
515 jbyteArray j_message, | 574 jbyteArray j_message, |
516 jstring j_destination_url) { | 575 jstring j_legacy_destination_url) { |
517 uint32 session_id = j_session_id; | |
518 std::vector<uint8> message; | 576 std::vector<uint8> message; |
519 JavaByteArrayToByteVector(env, j_message, &message); | 577 JavaByteArrayToByteVector(env, j_message, &message); |
520 GURL destination_gurl = GURL(ConvertJavaStringToUTF8(env, j_destination_url)); | 578 GURL legacy_destination_url = |
521 if (!destination_gurl.is_valid() && !destination_gurl.is_empty()) { | 579 GURL(ConvertJavaStringToUTF8(env, j_legacy_destination_url)); |
522 DLOG(WARNING) << "SessionMessage destination_url is invalid : " | 580 // Note: Message type is not supported in MediaDrm. Do our best guess here. |
523 << destination_gurl.possibly_invalid_spec(); | 581 media::MediaKeys::MessageType message_type = |
524 destination_gurl = GURL::EmptyGURL(); // Replace invalid destination_url. | 582 legacy_destination_url.is_empty() ? media::MediaKeys::LICENSE_REQUEST |
525 } | 583 : media::MediaKeys::LICENSE_RENEWAL; |
526 session_message_cb_.Run(session_id, message, destination_gurl); | |
527 } | |
528 | 584 |
529 void MediaDrmBridge::OnSessionReady(JNIEnv* env, | 585 session_message_cb_.Run(GetSessionId(env, j_session_id), message_type, |
530 jobject j_media_drm, | 586 message, legacy_destination_url); |
531 jint j_session_id) { | |
532 uint32 session_id = j_session_id; | |
533 session_ready_cb_.Run(session_id); | |
534 // TODO(xhwang/jrummell): Move this when usableKeyIds/keyschange are | |
535 // implemented. | |
536 player_tracker_.NotifyNewKey(); | |
537 } | 587 } |
538 | 588 |
539 void MediaDrmBridge::OnSessionClosed(JNIEnv* env, | 589 void MediaDrmBridge::OnSessionClosed(JNIEnv* env, |
540 jobject j_media_drm, | 590 jobject j_media_drm, |
541 jint j_session_id) { | 591 jbyteArray j_session_id) { |
542 uint32 session_id = j_session_id; | 592 session_closed_cb_.Run(GetSessionId(env, j_session_id)); |
543 session_closed_cb_.Run(session_id); | |
544 } | 593 } |
545 | 594 |
546 void MediaDrmBridge::OnSessionError(JNIEnv* env, | 595 void MediaDrmBridge::OnSessionKeysChange(JNIEnv* env, |
547 jobject j_media_drm, | 596 jobject j_media_drm, |
548 jint j_session_id) { | 597 jbyteArray j_session_id, |
549 uint32 session_id = j_session_id; | 598 bool has_additional_usable_key, |
550 session_error_cb_.Run(session_id, MediaKeys::kUnknownError, 0); | 599 jint j_key_status) { |
| 600 if (has_additional_usable_key) |
| 601 player_tracker_.NotifyNewKey(); |
| 602 |
| 603 scoped_ptr<CdmKeyInformation> cdm_key_information(new CdmKeyInformation()); |
| 604 cdm_key_information->key_id.assign(kDummyKeyId, |
| 605 kDummyKeyId + sizeof(kDummyKeyId)); |
| 606 cdm_key_information->status = |
| 607 static_cast<CdmKeyInformation::KeyStatus>(j_key_status); |
| 608 CdmKeysInfo cdm_keys_info; |
| 609 cdm_keys_info.push_back(cdm_key_information.release()); |
| 610 |
| 611 session_keys_change_cb_.Run(GetSessionId(env, j_session_id), |
| 612 has_additional_usable_key, cdm_keys_info.Pass()); |
| 613 } |
| 614 |
| 615 void MediaDrmBridge::OnLegacySessionError(JNIEnv* env, |
| 616 jobject j_media_drm, |
| 617 jbyteArray j_session_id, |
| 618 jstring j_error_message) { |
| 619 std::string error_message = ConvertJavaStringToUTF8(env, j_error_message); |
| 620 session_error_cb_.Run(GetSessionId(env, j_session_id), |
| 621 MediaKeys::UNKNOWN_ERROR, 0, error_message); |
551 } | 622 } |
552 | 623 |
553 ScopedJavaLocalRef<jobject> MediaDrmBridge::GetMediaCrypto() { | 624 ScopedJavaLocalRef<jobject> MediaDrmBridge::GetMediaCrypto() { |
554 JNIEnv* env = AttachCurrentThread(); | 625 JNIEnv* env = AttachCurrentThread(); |
555 return Java_MediaDrmBridge_getMediaCrypto(env, j_media_drm_.obj()); | 626 return Java_MediaDrmBridge_getMediaCrypto(env, j_media_drm_.obj()); |
556 } | 627 } |
557 | 628 |
558 MediaDrmBridge::SecurityLevel MediaDrmBridge::GetSecurityLevel() { | 629 MediaDrmBridge::SecurityLevel MediaDrmBridge::GetSecurityLevel() { |
559 JNIEnv* env = AttachCurrentThread(); | 630 JNIEnv* env = AttachCurrentThread(); |
560 ScopedJavaLocalRef<jstring> j_security_level = | 631 ScopedJavaLocalRef<jstring> j_security_level = |
(...skipping 14 matching lines...) Expand all Loading... |
575 JNIEnv* env = AttachCurrentThread(); | 646 JNIEnv* env = AttachCurrentThread(); |
576 Java_MediaDrmBridge_resetDeviceCredentials(env, j_media_drm_.obj()); | 647 Java_MediaDrmBridge_resetDeviceCredentials(env, j_media_drm_.obj()); |
577 } | 648 } |
578 | 649 |
579 void MediaDrmBridge::OnResetDeviceCredentialsCompleted( | 650 void MediaDrmBridge::OnResetDeviceCredentialsCompleted( |
580 JNIEnv* env, jobject, bool success) { | 651 JNIEnv* env, jobject, bool success) { |
581 base::ResetAndReturn(&reset_credentials_cb_).Run(success); | 652 base::ResetAndReturn(&reset_credentials_cb_).Run(success); |
582 } | 653 } |
583 | 654 |
584 } // namespace media | 655 } // namespace media |
OLD | NEW |