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 #include "content/renderer/media/android/proxy_media_keys.h" | 5 #include "content/renderer/media/android/proxy_media_keys.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "content/renderer/media/android/renderer_media_player_manager.h" | 11 #include "content/renderer/media/android/renderer_media_player_manager.h" |
12 #include "content/renderer/media/crypto/key_systems.h" | 12 #include "content/renderer/media/crypto/key_systems.h" |
| 13 #include "media/base/cdm_promise.h" |
13 | 14 |
14 namespace content { | 15 namespace content { |
15 | 16 |
16 int ProxyMediaKeys::next_cdm_id_ = | 17 int ProxyMediaKeys::next_cdm_id_ = |
17 RendererMediaPlayerManager::kInvalidCdmId + 1; | 18 RendererMediaPlayerManager::kInvalidCdmId + 1; |
18 | 19 |
19 scoped_ptr<ProxyMediaKeys> ProxyMediaKeys::Create( | 20 scoped_ptr<ProxyMediaKeys> ProxyMediaKeys::Create( |
20 const std::string& key_system, | 21 const std::string& key_system, |
21 const GURL& security_origin, | 22 const GURL& security_origin, |
22 RendererMediaPlayerManager* manager, | 23 RendererMediaPlayerManager* manager, |
23 const media::SessionCreatedCB& session_created_cb, | |
24 const media::SessionMessageCB& session_message_cb, | 24 const media::SessionMessageCB& session_message_cb, |
25 const media::SessionReadyCB& session_ready_cb, | 25 const media::SessionReadyCB& session_ready_cb, |
26 const media::SessionClosedCB& session_closed_cb, | 26 const media::SessionClosedCB& session_closed_cb, |
27 const media::SessionErrorCB& session_error_cb) { | 27 const media::SessionErrorCB& session_error_cb) { |
28 DCHECK(manager); | 28 DCHECK(manager); |
29 scoped_ptr<ProxyMediaKeys> proxy_media_keys( | 29 scoped_ptr<ProxyMediaKeys> proxy_media_keys( |
30 new ProxyMediaKeys(manager, | 30 new ProxyMediaKeys(manager, |
31 session_created_cb, | |
32 session_message_cb, | 31 session_message_cb, |
33 session_ready_cb, | 32 session_ready_cb, |
34 session_closed_cb, | 33 session_closed_cb, |
35 session_error_cb)); | 34 session_error_cb)); |
36 proxy_media_keys->InitializeCdm(key_system, security_origin); | 35 proxy_media_keys->InitializeCdm(key_system, security_origin); |
37 return proxy_media_keys.Pass(); | 36 return proxy_media_keys.Pass(); |
38 } | 37 } |
39 | 38 |
40 ProxyMediaKeys::~ProxyMediaKeys() { | 39 ProxyMediaKeys::~ProxyMediaKeys() { |
41 manager_->DestroyCdm(cdm_id_); | 40 manager_->DestroyCdm(cdm_id_); |
| 41 |
| 42 // Reject any outstanding promises. |
| 43 for (SimplePromiseMap::iterator it = |
| 44 session_id_to_change_session_promise_map_.begin(); |
| 45 it != session_id_to_change_session_promise_map_.end(); |
| 46 ++it) { |
| 47 it->second->reject(media::MediaKeys::MEDIA_KEYS_EXCEPTION_ABORT_ERROR, |
| 48 0, |
| 49 "The operation was aborted."); |
| 50 } |
| 51 for (NewSessionPromiseMap::iterator it = |
| 52 session_id_to_new_session_promise_map_.begin(); |
| 53 it != session_id_to_new_session_promise_map_.end(); |
| 54 ++it) { |
| 55 it->second->reject(media::MediaKeys::MEDIA_KEYS_EXCEPTION_ABORT_ERROR, |
| 56 0, |
| 57 "The operation was aborted."); |
| 58 } |
42 } | 59 } |
43 | 60 |
44 bool ProxyMediaKeys::CreateSession(uint32 session_id, | 61 void ProxyMediaKeys::CreateSession( |
45 const std::string& content_type, | 62 const std::string& init_data_type, |
46 const uint8* init_data, | 63 const uint8* init_data, |
47 int init_data_length) { | 64 int init_data_length, |
| 65 SessionType session_type, |
| 66 scoped_ptr<media::NewSessionCdmPromise> promise) { |
48 // TODO(xhwang): Move these checks up to blink and DCHECK here. | 67 // TODO(xhwang): Move these checks up to blink and DCHECK here. |
49 // See http://crbug.com/342510 | 68 // See http://crbug.com/342510 |
50 CdmHostMsg_CreateSession_ContentType session_type; | 69 CdmHostMsg_CreateSession_ContentType create_session_content_type; |
51 if (content_type == "audio/mp4" || content_type == "video/mp4") { | 70 if (init_data_type == "audio/mp4" || init_data_type == "video/mp4") { |
52 session_type = CREATE_SESSION_TYPE_MP4; | 71 create_session_content_type = CREATE_SESSION_TYPE_MP4; |
53 } else if (content_type == "audio/webm" || content_type == "video/webm") { | 72 } else if (init_data_type == "audio/webm" || init_data_type == "video/webm") { |
54 session_type = CREATE_SESSION_TYPE_WEBM; | 73 create_session_content_type = CREATE_SESSION_TYPE_WEBM; |
55 } else { | 74 } else { |
56 DLOG(ERROR) << "Unsupported EME CreateSession content type of " | 75 DLOG(ERROR) << "Unsupported EME CreateSession content type of " |
57 << content_type; | 76 << init_data_type; |
58 return false; | 77 promise->reject( |
| 78 MEDIA_KEYS_EXCEPTION_NOT_SUPPORTED_ERROR, |
| 79 0, |
| 80 "Unsupported EME CreateSession init data type of " + init_data_type); |
| 81 return; |
59 } | 82 } |
60 | 83 |
| 84 uint32 session_id = CreateSessionId(); |
| 85 RegisterNewSessionPromise(session_id, promise.Pass()); |
61 manager_->CreateSession( | 86 manager_->CreateSession( |
62 cdm_id_, | 87 cdm_id_, |
63 session_id, | 88 session_id, |
64 session_type, | 89 create_session_content_type, |
65 std::vector<uint8>(init_data, init_data + init_data_length)); | 90 std::vector<uint8>(init_data, init_data + init_data_length)); |
66 return true; | |
67 } | 91 } |
68 | 92 |
69 void ProxyMediaKeys::LoadSession(uint32 session_id, | 93 void ProxyMediaKeys::LoadSession( |
70 const std::string& web_session_id) { | 94 const std::string& web_session_id, |
| 95 scoped_ptr<media::NewSessionCdmPromise> promise) { |
71 // TODO(xhwang): Check key system and platform support for LoadSession in | 96 // TODO(xhwang): Check key system and platform support for LoadSession in |
72 // blink and add NOTREACHED() here. | 97 // blink and add NOTREACHED() here. |
73 DLOG(ERROR) << "ProxyMediaKeys doesn't support session loading."; | 98 DLOG(ERROR) << "ProxyMediaKeys doesn't support session loading."; |
74 OnSessionError(session_id, media::MediaKeys::kUnknownError, 0); | 99 promise->reject(MEDIA_KEYS_EXCEPTION_NOT_SUPPORTED_ERROR, |
| 100 0, |
| 101 "LoadSession() is not implemented."); |
75 } | 102 } |
76 | 103 |
77 void ProxyMediaKeys::UpdateSession(uint32 session_id, | 104 void ProxyMediaKeys::UpdateSession( |
78 const uint8* response, | 105 const std::string& web_session_id, |
79 int response_length) { | 106 const uint8* response, |
| 107 int response_length, |
| 108 scoped_ptr<media::SimpleCdmPromise> promise) { |
| 109 uint32 session_id = LookupSessionId(web_session_id); |
| 110 RegisterSimplePromise(session_id, promise.Pass()); |
80 manager_->UpdateSession( | 111 manager_->UpdateSession( |
81 cdm_id_, | 112 cdm_id_, |
82 session_id, | 113 session_id, |
83 std::vector<uint8>(response, response + response_length)); | 114 std::vector<uint8>(response, response + response_length)); |
84 } | 115 } |
85 | 116 |
86 void ProxyMediaKeys::ReleaseSession(uint32 session_id) { | 117 void ProxyMediaKeys::ReleaseSession( |
| 118 const std::string& web_session_id, |
| 119 scoped_ptr<media::SimpleCdmPromise> promise) { |
| 120 uint32 session_id = LookupSessionId(web_session_id); |
| 121 RegisterSimplePromise(session_id, promise.Pass()); |
87 manager_->ReleaseSession(cdm_id_, session_id); | 122 manager_->ReleaseSession(cdm_id_, session_id); |
88 } | 123 } |
89 | 124 |
90 void ProxyMediaKeys::OnSessionCreated(uint32 session_id, | 125 void ProxyMediaKeys::OnSessionCreated(uint32 session_id, |
91 const std::string& web_session_id) { | 126 const std::string& web_session_id) { |
92 session_created_cb_.Run(session_id, web_session_id); | 127 AssignWebSessionId(session_id, web_session_id); |
| 128 scoped_ptr<media::NewSessionCdmPromise> promise = |
| 129 TakeNewSessionPromise(session_id); |
| 130 promise->resolve(web_session_id); |
93 } | 131 } |
94 | 132 |
95 void ProxyMediaKeys::OnSessionMessage(uint32 session_id, | 133 void ProxyMediaKeys::OnSessionMessage(uint32 session_id, |
96 const std::vector<uint8>& message, | 134 const std::vector<uint8>& message, |
97 const std::string& destination_url) { | 135 const std::string& destination_url) { |
98 session_message_cb_.Run(session_id, message, destination_url); | 136 session_message_cb_.Run( |
| 137 LookupWebSessionId(session_id), message, destination_url); |
99 } | 138 } |
100 | 139 |
101 void ProxyMediaKeys::OnSessionReady(uint32 session_id) { | 140 void ProxyMediaKeys::OnSessionReady(uint32 session_id) { |
102 session_ready_cb_.Run(session_id); | 141 std::string web_session_id = LookupWebSessionId(session_id); |
| 142 scoped_ptr<media::SimpleCdmPromise> promise = TakeSimplePromise(session_id); |
| 143 if (promise) { |
| 144 promise->resolve(); |
| 145 } else { |
| 146 // Still needed for keyadded. |
| 147 session_ready_cb_.Run(web_session_id); |
| 148 } |
103 } | 149 } |
104 | 150 |
105 void ProxyMediaKeys::OnSessionClosed(uint32 session_id) { | 151 void ProxyMediaKeys::OnSessionClosed(uint32 session_id) { |
106 session_closed_cb_.Run(session_id); | 152 std::string web_session_id = LookupWebSessionId(session_id); |
| 153 DropWebSessionId(web_session_id); |
| 154 scoped_ptr<media::SimpleCdmPromise> promise = TakeSimplePromise(session_id); |
| 155 if (promise) { |
| 156 promise->resolve(); |
| 157 } else { |
| 158 // It is possible for the CDM to close a session independent of a |
| 159 // Release() request. |
| 160 session_closed_cb_.Run(web_session_id); |
| 161 } |
107 } | 162 } |
108 | 163 |
109 void ProxyMediaKeys::OnSessionError(uint32 session_id, | 164 void ProxyMediaKeys::OnSessionError(uint32 session_id, |
110 media::MediaKeys::KeyError error_code, | 165 media::MediaKeys::KeyError error_code, |
111 uint32 system_code) { | 166 uint32 system_code) { |
112 session_error_cb_.Run(session_id, error_code, system_code); | 167 std::string web_session_id = LookupWebSessionId(session_id); |
| 168 media::MediaKeys::Exception exception_code; |
| 169 switch (error_code) { |
| 170 case media::MediaKeys::kClientError: |
| 171 exception_code = media::MediaKeys::MEDIA_KEYS_EXCEPTION_CLIENT_ERROR; |
| 172 break; |
| 173 case media::MediaKeys::kOutputError: |
| 174 exception_code = media::MediaKeys::MEDIA_KEYS_EXCEPTION_OUTPUT_ERROR; |
| 175 break; |
| 176 case media::MediaKeys::kUnknownError: |
| 177 default: |
| 178 exception_code = media::MediaKeys::MEDIA_KEYS_EXCEPTION_UNKNOWN_ERROR; |
| 179 break; |
| 180 } |
| 181 |
| 182 scoped_ptr<media::NewSessionCdmPromise> promise = |
| 183 TakeNewSessionPromise(session_id); |
| 184 if (promise) { |
| 185 promise->reject(exception_code, system_code, std::string()); |
| 186 } else { |
| 187 // Maybe an error for other set of promises. |
| 188 scoped_ptr<media::SimpleCdmPromise> promise2 = |
| 189 TakeSimplePromise(session_id); |
| 190 if (promise2) { |
| 191 promise2->reject(exception_code, system_code, std::string()); |
| 192 } else { |
| 193 // Errors generally happen in response to a request, but it is possible |
| 194 // for something bad to happen in the CDM and it needs to tell the client. |
| 195 session_error_cb_.Run( |
| 196 web_session_id, exception_code, system_code, std::string()); |
| 197 } |
| 198 } |
113 } | 199 } |
114 | 200 |
115 int ProxyMediaKeys::GetCdmId() const { | 201 int ProxyMediaKeys::GetCdmId() const { |
116 return cdm_id_; | 202 return cdm_id_; |
117 } | 203 } |
118 | 204 |
119 ProxyMediaKeys::ProxyMediaKeys( | 205 ProxyMediaKeys::ProxyMediaKeys( |
120 RendererMediaPlayerManager* manager, | 206 RendererMediaPlayerManager* manager, |
121 const media::SessionCreatedCB& session_created_cb, | |
122 const media::SessionMessageCB& session_message_cb, | 207 const media::SessionMessageCB& session_message_cb, |
123 const media::SessionReadyCB& session_ready_cb, | 208 const media::SessionReadyCB& session_ready_cb, |
124 const media::SessionClosedCB& session_closed_cb, | 209 const media::SessionClosedCB& session_closed_cb, |
125 const media::SessionErrorCB& session_error_cb) | 210 const media::SessionErrorCB& session_error_cb) |
126 : manager_(manager), | 211 : manager_(manager), |
127 cdm_id_(next_cdm_id_++), | 212 cdm_id_(next_cdm_id_++), |
128 session_created_cb_(session_created_cb), | |
129 session_message_cb_(session_message_cb), | 213 session_message_cb_(session_message_cb), |
130 session_ready_cb_(session_ready_cb), | 214 session_ready_cb_(session_ready_cb), |
131 session_closed_cb_(session_closed_cb), | 215 session_closed_cb_(session_closed_cb), |
132 session_error_cb_(session_error_cb) { | 216 session_error_cb_(session_error_cb), |
| 217 next_session_id_(1) { |
133 } | 218 } |
134 | 219 |
135 void ProxyMediaKeys::InitializeCdm(const std::string& key_system, | 220 void ProxyMediaKeys::InitializeCdm(const std::string& key_system, |
136 const GURL& security_origin) { | 221 const GURL& security_origin) { |
137 manager_->InitializeCdm(cdm_id_, this, key_system, security_origin); | 222 manager_->InitializeCdm(cdm_id_, this, key_system, security_origin); |
138 } | 223 } |
139 | 224 |
| 225 uint32_t ProxyMediaKeys::CreateSessionId() { |
| 226 return next_session_id_++; |
| 227 } |
| 228 |
| 229 void ProxyMediaKeys::AssignWebSessionId(uint32_t session_id, |
| 230 const std::string& web_session_id) { |
| 231 web_session_to_session_id_map_.insert( |
| 232 std::make_pair(web_session_id, session_id)); |
| 233 } |
| 234 |
| 235 uint32_t ProxyMediaKeys::LookupSessionId(const std::string& web_session_id) { |
| 236 return web_session_to_session_id_map_.find(web_session_id)->second; |
| 237 } |
| 238 |
| 239 std::string ProxyMediaKeys::LookupWebSessionId(uint32_t session_id) { |
| 240 for (SessionIdMap::iterator it = web_session_to_session_id_map_.begin(); |
| 241 it != web_session_to_session_id_map_.end(); |
| 242 ++it) { |
| 243 if (it->second == session_id) |
| 244 return it->first; |
| 245 } |
| 246 // Possible to get an error creating a session, so no |web_session_id| |
| 247 // available. |
| 248 return std::string(); |
| 249 } |
| 250 |
| 251 void ProxyMediaKeys::DropWebSessionId(std::string web_session_id) { |
| 252 web_session_to_session_id_map_.erase(web_session_id); |
| 253 } |
| 254 |
| 255 void ProxyMediaKeys::RegisterSimplePromise( |
| 256 uint32_t session_id, |
| 257 scoped_ptr<media::SimpleCdmPromise> promise) { |
| 258 // Should only be one promise outstanding for any |session_id|. |
| 259 DCHECK(session_id_to_change_session_promise_map_.find(session_id) == |
| 260 session_id_to_change_session_promise_map_.end()); |
| 261 session_id_to_change_session_promise_map_.insert( |
| 262 std::make_pair(session_id, promise.release())); |
| 263 } |
| 264 |
| 265 scoped_ptr<media::SimpleCdmPromise> ProxyMediaKeys::TakeSimplePromise( |
| 266 uint32_t session_id) { |
| 267 SimplePromiseMap::iterator it = |
| 268 session_id_to_change_session_promise_map_.find(session_id); |
| 269 // May not be a promise associated with this session for asynchronous events. |
| 270 if (it == session_id_to_change_session_promise_map_.end()) |
| 271 return scoped_ptr<media::SimpleCdmPromise>(); |
| 272 scoped_ptr<media::SimpleCdmPromise> result(it->second); |
| 273 session_id_to_change_session_promise_map_.erase(it); |
| 274 return result.Pass(); |
| 275 } |
| 276 |
| 277 void ProxyMediaKeys::RegisterNewSessionPromise( |
| 278 uint32_t session_id, |
| 279 scoped_ptr<media::NewSessionCdmPromise> promise) { |
| 280 // Should only be one promise outstanding for any |session_id|. |
| 281 DCHECK(session_id_to_new_session_promise_map_.find(session_id) == |
| 282 session_id_to_new_session_promise_map_.end()); |
| 283 session_id_to_new_session_promise_map_.insert( |
| 284 std::make_pair(session_id, promise.release())); |
| 285 } |
| 286 |
| 287 scoped_ptr<media::NewSessionCdmPromise> ProxyMediaKeys::TakeNewSessionPromise( |
| 288 uint32_t session_id) { |
| 289 NewSessionPromiseMap::iterator it = |
| 290 session_id_to_new_session_promise_map_.find(session_id); |
| 291 // May not be a promise associated with this session for asynchronous events. |
| 292 if (it == session_id_to_new_session_promise_map_.end()) |
| 293 return scoped_ptr<media::NewSessionCdmPromise>(); |
| 294 scoped_ptr<media::NewSessionCdmPromise> result(it->second); |
| 295 session_id_to_new_session_promise_map_.erase(it); |
| 296 return result.Pass(); |
| 297 } |
| 298 |
140 } // namespace content | 299 } // namespace content |
OLD | NEW |