OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 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 #ifndef MEDIA_CDM_PPAPI_CDM_WRAPPER_H_ | 5 #ifndef MEDIA_CDM_PPAPI_CDM_WRAPPER_H_ |
6 #define MEDIA_CDM_PPAPI_CDM_WRAPPER_H_ | 6 #define MEDIA_CDM_PPAPI_CDM_WRAPPER_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <queue> | 9 #include <queue> |
10 #include <string> | 10 #include <string> |
(...skipping 24 matching lines...) Expand all Loading... |
35 // (just a shim layer in most cases), everything is done in this header file. | 35 // (just a shim layer in most cases), everything is done in this header file. |
36 class CdmWrapper { | 36 class CdmWrapper { |
37 public: | 37 public: |
38 static CdmWrapper* Create(const char* key_system, | 38 static CdmWrapper* Create(const char* key_system, |
39 uint32_t key_system_size, | 39 uint32_t key_system_size, |
40 GetCdmHostFunc get_cdm_host_func, | 40 GetCdmHostFunc get_cdm_host_func, |
41 void* user_data); | 41 void* user_data); |
42 | 42 |
43 virtual ~CdmWrapper() {}; | 43 virtual ~CdmWrapper() {}; |
44 | 44 |
45 virtual void CreateSession(uint32_t session_id, | 45 virtual void CreateSession(uint32_t promise_id, |
46 const char* content_type, | 46 const char* init_data_type, |
47 uint32_t content_type_size, | 47 uint32_t init_data_type_size, |
48 const uint8_t* init_data, | 48 const uint8_t* init_data, |
49 uint32_t init_data_size) = 0; | 49 uint32_t init_data_size, |
50 virtual void LoadSession(uint32_t session_id, | 50 cdm::SessionType session_type) = 0; |
| 51 virtual void LoadSession(uint32_t promise_id, |
51 const char* web_session_id, | 52 const char* web_session_id, |
52 uint32_t web_session_id_size) = 0; | 53 uint32_t web_session_id_size) = 0; |
53 virtual void UpdateSession(uint32_t session_id, | 54 virtual void UpdateSession(uint32_t promise_id, |
| 55 const char* web_session_id, |
| 56 uint32_t web_session_id_size, |
54 const uint8_t* response, | 57 const uint8_t* response, |
55 uint32_t response_size) = 0; | 58 uint32_t response_size) = 0; |
56 virtual void ReleaseSession(uint32_t session_id) = 0; | 59 virtual void ReleaseSession(uint32_t promise_id, |
| 60 const char* web_session_id, |
| 61 uint32_t web_session_id_size) = 0; |
57 virtual void TimerExpired(void* context) = 0; | 62 virtual void TimerExpired(void* context) = 0; |
58 virtual cdm::Status Decrypt(const cdm::InputBuffer& encrypted_buffer, | 63 virtual cdm::Status Decrypt(const cdm::InputBuffer& encrypted_buffer, |
59 cdm::DecryptedBlock* decrypted_buffer) = 0; | 64 cdm::DecryptedBlock* decrypted_buffer) = 0; |
60 virtual cdm::Status InitializeAudioDecoder( | 65 virtual cdm::Status InitializeAudioDecoder( |
61 const cdm::AudioDecoderConfig& audio_decoder_config) = 0; | 66 const cdm::AudioDecoderConfig& audio_decoder_config) = 0; |
62 virtual cdm::Status InitializeVideoDecoder( | 67 virtual cdm::Status InitializeVideoDecoder( |
63 const cdm::VideoDecoderConfig& video_decoder_config) = 0; | 68 const cdm::VideoDecoderConfig& video_decoder_config) = 0; |
64 virtual void DeinitializeDecoder(cdm::StreamType decoder_type) = 0; | 69 virtual void DeinitializeDecoder(cdm::StreamType decoder_type) = 0; |
65 virtual void ResetDecoder(cdm::StreamType decoder_type) = 0; | 70 virtual void ResetDecoder(cdm::StreamType decoder_type) = 0; |
66 virtual cdm::Status DecryptAndDecodeFrame( | 71 virtual cdm::Status DecryptAndDecodeFrame( |
67 const cdm::InputBuffer& encrypted_buffer, | 72 const cdm::InputBuffer& encrypted_buffer, |
68 cdm::VideoFrame* video_frame) = 0; | 73 cdm::VideoFrame* video_frame) = 0; |
69 virtual cdm::Status DecryptAndDecodeSamples( | 74 virtual cdm::Status DecryptAndDecodeSamples( |
70 const cdm::InputBuffer& encrypted_buffer, | 75 const cdm::InputBuffer& encrypted_buffer, |
71 cdm::AudioFrames* audio_frames) = 0; | 76 cdm::AudioFrames* audio_frames) = 0; |
72 virtual void OnPlatformChallengeResponse( | 77 virtual void OnPlatformChallengeResponse( |
73 const cdm::PlatformChallengeResponse& response) = 0; | 78 const cdm::PlatformChallengeResponse& response) = 0; |
74 virtual void OnQueryOutputProtectionStatus( | 79 virtual void OnQueryOutputProtectionStatus( |
75 uint32_t link_mask, | 80 uint32_t link_mask, |
76 uint32_t output_protection_mask) = 0; | 81 uint32_t output_protection_mask) = 0; |
77 | 82 |
| 83 // Helper function for the cdm::Host_4 methods. Calls to CreateSession(), |
| 84 // LoadSession(), UpdateSession(), and ReleaseSession() pass in promise ids, |
| 85 // but the CDM interface needs session ids. For create and load, we need to |
| 86 // create a new session_id to pass to the CDM. For update and release, we need |
| 87 // to look up |web_session_id| and convert it into the existing |session_id|. |
| 88 // Since the callbacks don't come through this interface, cdm_adapter needs to |
| 89 // create the mapping (and delete it on release). |
| 90 // TODO(jrummell): Remove these once Host_4 interface is removed. |
| 91 virtual uint32_t LookupPromiseId(uint32_t session_id) = 0; |
| 92 virtual void AssignWebSessionId(uint32_t session_id, |
| 93 const char* web_session_id, |
| 94 uint32_t web_session_id_size) = 0; |
| 95 virtual std::string LookupWebSessionId(uint32_t session_id) = 0; |
| 96 virtual void DropWebSessionId(std::string web_session_id) = 0; |
| 97 |
78 protected: | 98 protected: |
79 CdmWrapper() {} | 99 CdmWrapper() {} |
80 | 100 |
81 private: | 101 private: |
82 DISALLOW_COPY_AND_ASSIGN(CdmWrapper); | 102 DISALLOW_COPY_AND_ASSIGN(CdmWrapper); |
83 }; | 103 }; |
84 | 104 |
85 // Template class that does the CdmWrapper -> CdmInterface conversion. Default | 105 // Template class that does the CdmWrapper -> CdmInterface conversion. Default |
86 // implementations are provided. Any methods that need special treatment should | 106 // implementations are provided. Any methods that need special treatment should |
87 // be specialized. | 107 // be specialized. |
(...skipping 11 matching lines...) Expand all Loading... |
99 return NULL; | 119 return NULL; |
100 | 120 |
101 return new CdmWrapperImpl<CdmInterface>( | 121 return new CdmWrapperImpl<CdmInterface>( |
102 static_cast<CdmInterface*>(cdm_instance)); | 122 static_cast<CdmInterface*>(cdm_instance)); |
103 } | 123 } |
104 | 124 |
105 virtual ~CdmWrapperImpl() { | 125 virtual ~CdmWrapperImpl() { |
106 cdm_->Destroy(); | 126 cdm_->Destroy(); |
107 } | 127 } |
108 | 128 |
109 virtual void CreateSession(uint32_t session_id, | 129 virtual void CreateSession(uint32_t promise_id, |
110 const char* content_type, | 130 const char* init_data_type, |
111 uint32_t content_type_size, | 131 uint32_t init_data_type_size, |
112 const uint8_t* init_data, | 132 const uint8_t* init_data, |
113 uint32_t init_data_size) OVERRIDE { | 133 uint32_t init_data_size, |
114 cdm_->CreateSession( | 134 cdm::SessionType session_type) OVERRIDE { |
115 session_id, content_type, content_type_size, init_data, init_data_size); | 135 cdm_->CreateSession(promise_id, |
| 136 init_data_type, |
| 137 init_data_type_size, |
| 138 init_data, |
| 139 init_data_size, |
| 140 session_type); |
116 } | 141 } |
117 | 142 |
118 virtual void LoadSession(uint32_t session_id, | 143 virtual void LoadSession(uint32_t promise_id, |
119 const char* web_session_id, | 144 const char* web_session_id, |
120 uint32_t web_session_id_size) OVERRIDE { | 145 uint32_t web_session_id_size) OVERRIDE { |
121 cdm_->LoadSession(session_id, web_session_id, web_session_id_size); | 146 cdm_->LoadSession(promise_id, web_session_id, web_session_id_size); |
122 } | 147 } |
123 | 148 |
124 virtual void UpdateSession(uint32_t session_id, | 149 virtual void UpdateSession(uint32_t promise_id, |
| 150 const char* web_session_id, |
| 151 uint32_t web_session_id_size, |
125 const uint8_t* response, | 152 const uint8_t* response, |
126 uint32_t response_size) OVERRIDE { | 153 uint32_t response_size) OVERRIDE { |
127 cdm_->UpdateSession(session_id, response, response_size); | 154 cdm_->UpdateSession(promise_id, |
| 155 web_session_id, |
| 156 web_session_id_size, |
| 157 response, |
| 158 response_size); |
128 } | 159 } |
129 | 160 |
130 virtual void ReleaseSession(uint32_t session_id) OVERRIDE { | 161 virtual void ReleaseSession(uint32_t promise_id, |
131 cdm_->ReleaseSession(session_id); | 162 const char* web_session_id, |
| 163 uint32_t web_session_id_size) OVERRIDE { |
| 164 cdm_->ReleaseSession(promise_id, web_session_id, web_session_id_size); |
132 } | 165 } |
133 | 166 |
134 virtual void TimerExpired(void* context) OVERRIDE { | 167 virtual void TimerExpired(void* context) OVERRIDE { |
135 cdm_->TimerExpired(context); | 168 cdm_->TimerExpired(context); |
136 } | 169 } |
137 | 170 |
138 virtual cdm::Status Decrypt(const cdm::InputBuffer& encrypted_buffer, | 171 virtual cdm::Status Decrypt(const cdm::InputBuffer& encrypted_buffer, |
139 cdm::DecryptedBlock* decrypted_buffer) OVERRIDE { | 172 cdm::DecryptedBlock* decrypted_buffer) OVERRIDE { |
140 return cdm_->Decrypt(encrypted_buffer, decrypted_buffer); | 173 return cdm_->Decrypt(encrypted_buffer, decrypted_buffer); |
141 } | 174 } |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
174 const cdm::PlatformChallengeResponse& response) OVERRIDE { | 207 const cdm::PlatformChallengeResponse& response) OVERRIDE { |
175 cdm_->OnPlatformChallengeResponse(response); | 208 cdm_->OnPlatformChallengeResponse(response); |
176 } | 209 } |
177 | 210 |
178 virtual void OnQueryOutputProtectionStatus( | 211 virtual void OnQueryOutputProtectionStatus( |
179 uint32_t link_mask, | 212 uint32_t link_mask, |
180 uint32_t output_protection_mask) OVERRIDE { | 213 uint32_t output_protection_mask) OVERRIDE { |
181 cdm_->OnQueryOutputProtectionStatus(link_mask, output_protection_mask); | 214 cdm_->OnQueryOutputProtectionStatus(link_mask, output_protection_mask); |
182 } | 215 } |
183 | 216 |
| 217 uint32_t CreateSessionId() { |
| 218 return next_session_id_++; |
| 219 } |
| 220 |
| 221 void RegisterPromise(uint32_t session_id, uint32_t promise_id) { |
| 222 PP_DCHECK(promise_to_session_id_map_.find(session_id) == |
| 223 promise_to_session_id_map_.end()); |
| 224 promise_to_session_id_map_.insert(std::make_pair(session_id, promise_id)); |
| 225 } |
| 226 |
| 227 virtual uint32_t LookupPromiseId(uint32_t session_id) { |
| 228 std::map<uint32_t, uint32_t>::iterator it = |
| 229 promise_to_session_id_map_.find(session_id); |
| 230 if (it == promise_to_session_id_map_.end()) |
| 231 return 0; |
| 232 uint32_t promise_id = it->second; |
| 233 promise_to_session_id_map_.erase(it); |
| 234 return promise_id; |
| 235 } |
| 236 |
| 237 virtual void AssignWebSessionId(uint32_t session_id, |
| 238 const char* web_session_id, |
| 239 uint32_t web_session_id_size) { |
| 240 web_session_to_session_id_map_.insert(std::make_pair( |
| 241 std::string(web_session_id, web_session_id_size), session_id)); |
| 242 } |
| 243 |
| 244 uint32_t LookupSessionId(std::string web_session_id) { |
| 245 return web_session_to_session_id_map_.find(web_session_id)->second; |
| 246 } |
| 247 |
| 248 virtual std::string LookupWebSessionId(uint32_t session_id) { |
| 249 std::map<std::string, uint32_t>::iterator it; |
| 250 for (it = web_session_to_session_id_map_.begin(); |
| 251 it != web_session_to_session_id_map_.end(); |
| 252 ++it) { |
| 253 if (it->second == session_id) |
| 254 return it->first; |
| 255 } |
| 256 PP_NOTREACHED(); |
| 257 return std::string(); |
| 258 } |
| 259 |
| 260 virtual void DropWebSessionId(std::string web_session_id) { |
| 261 web_session_to_session_id_map_.erase(web_session_id); |
| 262 } |
| 263 |
184 private: | 264 private: |
185 CdmWrapperImpl(CdmInterface* cdm) : cdm_(cdm) { | 265 CdmWrapperImpl(CdmInterface* cdm) : cdm_(cdm), next_session_id_(100) { |
186 PP_DCHECK(cdm_); | 266 PP_DCHECK(cdm_); |
187 } | 267 } |
188 | 268 |
189 CdmInterface* cdm_; | 269 CdmInterface* cdm_; |
190 | 270 |
| 271 std::map<uint32_t, uint32_t> promise_to_session_id_map_; |
| 272 uint32_t next_session_id_; |
| 273 std::map<std::string, uint32_t> web_session_to_session_id_map_; |
| 274 |
191 DISALLOW_COPY_AND_ASSIGN(CdmWrapperImpl); | 275 DISALLOW_COPY_AND_ASSIGN(CdmWrapperImpl); |
192 }; | 276 }; |
193 | 277 |
| 278 // Overrides for the cdm::Host_4 methods. Calls to CreateSession(), |
| 279 // LoadSession(), UpdateSession(), and ReleaseSession() pass in promise ids, |
| 280 // but the CDM interface needs session ids. For create and load, we need to |
| 281 // create a new session_id to pass to the CDM. For update and release, we need |
| 282 // to look up |web_session_id| and convert it into the existing |session_id|. |
| 283 // Since the callbacks don't come through this interface, cdm_adapter needs to |
| 284 // create the mapping (and delete it on release). |
| 285 // TODO(jrummell): Remove these once Host_4 interface is removed. |
| 286 |
| 287 template <> |
| 288 void CdmWrapperImpl<cdm::ContentDecryptionModule_4>::CreateSession( |
| 289 uint32_t promise_id, |
| 290 const char* init_data_type, |
| 291 uint32_t init_data_type_size, |
| 292 const uint8_t* init_data, |
| 293 uint32_t init_data_size, |
| 294 cdm::SessionType session_type) { |
| 295 uint32_t session_id = CreateSessionId(); |
| 296 RegisterPromise(session_id, promise_id); |
| 297 cdm_->CreateSession(session_id, |
| 298 init_data_type, |
| 299 init_data_type_size, |
| 300 init_data, |
| 301 init_data_size); |
| 302 } |
| 303 |
| 304 template <> |
| 305 void CdmWrapperImpl<cdm::ContentDecryptionModule_4>::LoadSession( |
| 306 uint32_t promise_id, |
| 307 const char* web_session_id, |
| 308 uint32_t web_session_id_size) { |
| 309 uint32_t session_id = CreateSessionId(); |
| 310 RegisterPromise(session_id, promise_id); |
| 311 cdm_->LoadSession(session_id, web_session_id, web_session_id_size); |
| 312 } |
| 313 |
| 314 template <> |
| 315 void CdmWrapperImpl<cdm::ContentDecryptionModule_4>::UpdateSession( |
| 316 uint32_t promise_id, |
| 317 const char* web_session_id, |
| 318 uint32_t web_session_id_size, |
| 319 const uint8_t* response, |
| 320 uint32_t response_size) { |
| 321 std::string web_session_str(web_session_id, web_session_id_size); |
| 322 uint32_t session_id = LookupSessionId(web_session_str); |
| 323 RegisterPromise(session_id, promise_id); |
| 324 cdm_->UpdateSession(session_id, response, response_size); |
| 325 } |
| 326 |
| 327 template <> |
| 328 void CdmWrapperImpl<cdm::ContentDecryptionModule_4>::ReleaseSession( |
| 329 uint32_t promise_id, |
| 330 const char* web_session_id, |
| 331 uint32_t web_session_id_size) { |
| 332 std::string web_session_str(web_session_id, web_session_id_size); |
| 333 uint32_t session_id = LookupSessionId(web_session_str); |
| 334 RegisterPromise(session_id, promise_id); |
| 335 cdm_->ReleaseSession(session_id); |
| 336 } |
| 337 |
194 CdmWrapper* CdmWrapper::Create(const char* key_system, | 338 CdmWrapper* CdmWrapper::Create(const char* key_system, |
195 uint32_t key_system_size, | 339 uint32_t key_system_size, |
196 GetCdmHostFunc get_cdm_host_func, | 340 GetCdmHostFunc get_cdm_host_func, |
197 void* user_data) { | 341 void* user_data) { |
198 COMPILE_ASSERT(cdm::ContentDecryptionModule::kVersion == | 342 COMPILE_ASSERT(cdm::ContentDecryptionModule::kVersion == |
199 cdm::ContentDecryptionModule_4::kVersion, | 343 cdm::ContentDecryptionModule_5::kVersion, |
200 update_code_below); | 344 update_code_below); |
201 | 345 |
202 // Ensure IsSupportedCdmInterfaceVersion() matches this implementation. | 346 // Ensure IsSupportedCdmInterfaceVersion() matches this implementation. |
203 // Always update this DCHECK when updating this function. | 347 // Always update this DCHECK when updating this function. |
204 // If this check fails, update this function and DCHECK or update | 348 // If this check fails, update this function and DCHECK or update |
205 // IsSupportedCdmInterfaceVersion(). | 349 // IsSupportedCdmInterfaceVersion(). |
206 PP_DCHECK( | 350 PP_DCHECK( |
207 !IsSupportedCdmInterfaceVersion(cdm::ContentDecryptionModule::kVersion + | 351 !IsSupportedCdmInterfaceVersion(cdm::ContentDecryptionModule::kVersion + |
208 1) && | 352 1) && |
209 IsSupportedCdmInterfaceVersion(cdm::ContentDecryptionModule::kVersion) && | 353 IsSupportedCdmInterfaceVersion(cdm::ContentDecryptionModule::kVersion) && |
210 !IsSupportedCdmInterfaceVersion(cdm::ContentDecryptionModule::kVersion - | 354 IsSupportedCdmInterfaceVersion( |
| 355 cdm::ContentDecryptionModule_4::kVersion) && |
| 356 !IsSupportedCdmInterfaceVersion(cdm::ContentDecryptionModule_4::kVersion - |
211 1)); | 357 1)); |
212 | 358 |
213 // Try to create the CDM using the latest CDM interface version. | 359 // Try to create the CDM using the latest CDM interface version. |
214 CdmWrapper* cdm_wrapper = | 360 CdmWrapper* cdm_wrapper = |
215 CdmWrapperImpl<cdm::ContentDecryptionModule>::Create( | 361 CdmWrapperImpl<cdm::ContentDecryptionModule>::Create( |
216 key_system, key_system_size, get_cdm_host_func, user_data); | 362 key_system, key_system_size, get_cdm_host_func, user_data); |
| 363 if (cdm_wrapper) |
| 364 return cdm_wrapper; |
217 | 365 |
218 // If |cdm_wrapper| is NULL, try to create the CDM using older supported | 366 // If |cdm_wrapper| is NULL, try to create the CDM using older supported |
219 // versions of the CDM interface. | 367 // versions of the CDM interface. |
220 // No older versions of CDM interface supported. | 368 cdm_wrapper = CdmWrapperImpl<cdm::ContentDecryptionModule_4>::Create( |
221 | 369 key_system, key_system_size, get_cdm_host_func, user_data); |
222 return cdm_wrapper; | 370 return cdm_wrapper; |
223 } | 371 } |
224 | 372 |
225 // When updating the CdmAdapter, ensure you've updated the CdmWrapper to contain | 373 // When updating the CdmAdapter, ensure you've updated the CdmWrapper to contain |
226 // stub implementations for new or modified methods that the older CDM interface | 374 // stub implementations for new or modified methods that the older CDM interface |
227 // does not have. | 375 // does not have. |
228 // Also update supported_cdm_versions.h. | 376 // Also update supported_cdm_versions.h. |
229 COMPILE_ASSERT(cdm::ContentDecryptionModule::kVersion == | 377 COMPILE_ASSERT(cdm::ContentDecryptionModule::kVersion == |
230 cdm::ContentDecryptionModule_4::kVersion, | 378 cdm::ContentDecryptionModule_5::kVersion, |
231 ensure_cdm_wrapper_templates_have_old_version_support); | 379 ensure_cdm_wrapper_templates_have_old_version_support); |
232 | 380 |
233 } // namespace media | 381 } // namespace media |
234 | 382 |
235 #endif // MEDIA_CDM_PPAPI_CDM_WRAPPER_H_ | 383 #endif // MEDIA_CDM_PPAPI_CDM_WRAPPER_H_ |
OLD | NEW |