Chromium Code Reviews| Index: content/renderer/media/android/proxy_media_keys.cc |
| diff --git a/content/renderer/media/android/proxy_media_keys.cc b/content/renderer/media/android/proxy_media_keys.cc |
| index 68175c8e46630e44d49230268de548afe30dc481..65d0d5d9846b9953d0404f64cd56df024d752ee3 100644 |
| --- a/content/renderer/media/android/proxy_media_keys.cc |
| +++ b/content/renderer/media/android/proxy_media_keys.cc |
| @@ -20,7 +20,6 @@ scoped_ptr<ProxyMediaKeys> ProxyMediaKeys::Create( |
| const std::string& key_system, |
| const GURL& security_origin, |
| RendererMediaPlayerManager* manager, |
| - const media::SessionCreatedCB& session_created_cb, |
| const media::SessionMessageCB& session_message_cb, |
| const media::SessionReadyCB& session_ready_cb, |
| const media::SessionClosedCB& session_closed_cb, |
| @@ -28,7 +27,6 @@ scoped_ptr<ProxyMediaKeys> ProxyMediaKeys::Create( |
| DCHECK(manager); |
| scoped_ptr<ProxyMediaKeys> proxy_media_keys( |
| new ProxyMediaKeys(manager, |
| - session_created_cb, |
| session_message_cb, |
| session_ready_cb, |
| session_closed_cb, |
| @@ -39,77 +37,141 @@ scoped_ptr<ProxyMediaKeys> ProxyMediaKeys::Create( |
| ProxyMediaKeys::~ProxyMediaKeys() { |
| manager_->DestroyCdm(cdm_id_); |
| + |
| + // Reject any outstanding promises. |
| + for (PromiseMap::iterator it = session_id_to_promise_map_.begin(); |
| + it != session_id_to_promise_map_.end(); |
| + ++it) { |
| + it->second->reject("AbortError", 0, "The operation was aborted."); |
| + } |
|
jrummell
2014/05/08 23:37:46
Need to do the second list of promises as well.
jrummell
2014/05/09 00:55:23
Done.
|
| } |
| -bool ProxyMediaKeys::CreateSession(uint32 session_id, |
| - const std::string& content_type, |
| - const uint8* init_data, |
| - int init_data_length) { |
| +void ProxyMediaKeys::CreateSession( |
| + const std::string& init_data_type, |
| + const uint8* init_data, |
| + int init_data_length, |
| + SessionType session_type, |
| + scoped_ptr<media::CdmPromise<std::string> > promise) { |
| // TODO(xhwang): Move these checks up to blink and DCHECK here. |
| // See http://crbug.com/342510 |
| - CdmHostMsg_CreateSession_ContentType session_type; |
| - if (content_type == "audio/mp4" || content_type == "video/mp4") { |
| - session_type = CREATE_SESSION_TYPE_MP4; |
| - } else if (content_type == "audio/webm" || content_type == "video/webm") { |
| - session_type = CREATE_SESSION_TYPE_WEBM; |
| + CdmHostMsg_CreateSession_ContentType create_session_content_type; |
| + if (init_data_type == "audio/mp4" || init_data_type == "video/mp4") { |
| + create_session_content_type = CREATE_SESSION_TYPE_MP4; |
| + } else if (init_data_type == "audio/webm" || init_data_type == "video/webm") { |
| + create_session_content_type = CREATE_SESSION_TYPE_WEBM; |
| } else { |
| DLOG(ERROR) << "Unsupported EME CreateSession content type of " |
| - << content_type; |
| - return false; |
| + << init_data_type; |
| + promise->reject( |
| + MEDIA_KEYS_EXCEPTION_NOT_SUPPORTED_ERROR, |
| + 0, |
| + "Unsupported EME CreateSession init data type of " + init_data_type); |
| + return; |
| } |
| + uint32 session_id = CreateSessionId(); |
| + RegisterSessionPromise(session_id, promise.Pass()); |
| manager_->CreateSession( |
| cdm_id_, |
| session_id, |
| - session_type, |
| + create_session_content_type, |
| std::vector<uint8>(init_data, init_data + init_data_length)); |
| - return true; |
| } |
| -void ProxyMediaKeys::LoadSession(uint32 session_id, |
| - const std::string& web_session_id) { |
| +void ProxyMediaKeys::LoadSession( |
| + const std::string& web_session_id, |
| + scoped_ptr<media::CdmPromise<std::string> > promise) { |
| // TODO(xhwang): Check key system and platform support for LoadSession in |
| // blink and add NOTREACHED() here. |
| DLOG(ERROR) << "ProxyMediaKeys doesn't support session loading."; |
| - OnSessionError(session_id, media::MediaKeys::kUnknownError, 0); |
| + promise->reject(MEDIA_KEYS_EXCEPTION_NOT_SUPPORTED_ERROR, |
| + 0, |
| + "LoadSession() is not implemented."); |
| } |
| -void ProxyMediaKeys::UpdateSession(uint32 session_id, |
| - const uint8* response, |
| - int response_length) { |
| +void ProxyMediaKeys::UpdateSession( |
| + const std::string& web_session_id, |
| + const uint8* response, |
| + int response_length, |
| + scoped_ptr<media::CdmPromise<void> > promise) { |
| + uint32 session_id = LookupSessionId(web_session_id); |
| + RegisterVoidPromise(session_id, promise.Pass()); |
| manager_->UpdateSession( |
| cdm_id_, |
| session_id, |
| std::vector<uint8>(response, response + response_length)); |
| } |
| -void ProxyMediaKeys::ReleaseSession(uint32 session_id) { |
| +void ProxyMediaKeys::ReleaseSession( |
| + const std::string& web_session_id, |
| + scoped_ptr<media::CdmPromise<void> > promise) { |
| + uint32 session_id = LookupSessionId(web_session_id); |
| + RegisterVoidPromise(session_id, promise.Pass()); |
| manager_->ReleaseSession(cdm_id_, session_id); |
| } |
| void ProxyMediaKeys::OnSessionCreated(uint32 session_id, |
| const std::string& web_session_id) { |
| - session_created_cb_.Run(session_id, web_session_id); |
| + AssignWebSessionId(session_id, web_session_id); |
| + scoped_ptr<media::CdmPromise<std::string> > promise = |
| + RetrieveSessionPromise(session_id); |
| + promise->resolve(web_session_id); |
| } |
| void ProxyMediaKeys::OnSessionMessage(uint32 session_id, |
| const std::vector<uint8>& message, |
| const std::string& destination_url) { |
| - session_message_cb_.Run(session_id, message, destination_url); |
| + session_message_cb_.Run( |
| + LookupWebSessionId(session_id), message, destination_url); |
| } |
| void ProxyMediaKeys::OnSessionReady(uint32 session_id) { |
| - session_ready_cb_.Run(session_id); |
| + std::string web_session_id = LookupWebSessionId(session_id); |
| + scoped_ptr<media::CdmPromise<void> > promise = |
| + RetrieveVoidPromise(session_id); |
| + if (promise) { |
| + promise->resolve(); |
| + } else { |
| + // Still needed for keyadded. |
| + session_ready_cb_.Run(web_session_id); |
| + } |
| } |
| void ProxyMediaKeys::OnSessionClosed(uint32 session_id) { |
| - session_closed_cb_.Run(session_id); |
| + std::string web_session_id = LookupWebSessionId(session_id); |
| + DropWebSessionId(web_session_id); |
| + scoped_ptr<media::CdmPromise<void> > promise = |
| + RetrieveVoidPromise(session_id); |
| + if (promise) { |
| + promise->resolve(); |
| + } else { |
| + // It is possible for the CDM to close a session independent of a |
| + // Release() request. |
| + session_closed_cb_.Run(web_session_id); |
| + } |
| } |
| void ProxyMediaKeys::OnSessionError(uint32 session_id, |
| media::MediaKeys::KeyError error_code, |
| uint32 system_code) { |
| - session_error_cb_.Run(session_id, error_code, system_code); |
| + std::string web_session_id = LookupWebSessionId(session_id); |
| + scoped_ptr<media::CdmPromise<std::string> > promise = |
| + RetrieveSessionPromise(session_id); |
| + if (promise) { |
| + promise->reject("InvalidStateError", 0, std::string()); |
| + } else { |
| + // Maybe an error for other set of promises. |
| + scoped_ptr<media::CdmPromise<void> > promise2 = |
| + RetrieveVoidPromise(session_id); |
| + if (promise2) { |
| + promise2->reject("InvalidStateError", 0, std::string()); |
| + } else { |
| + // Errors generally happen in response to a request, but it is possible |
| + // for something bad to happen in the CDM and it needs to tell the client. |
| + session_error_cb_.Run( |
| + web_session_id, "InvalidStateError", system_code, std::string()); |
| + } |
| + } |
| } |
| int ProxyMediaKeys::GetCdmId() const { |
| @@ -118,18 +180,17 @@ int ProxyMediaKeys::GetCdmId() const { |
| ProxyMediaKeys::ProxyMediaKeys( |
| RendererMediaPlayerManager* manager, |
| - const media::SessionCreatedCB& session_created_cb, |
| const media::SessionMessageCB& session_message_cb, |
| const media::SessionReadyCB& session_ready_cb, |
| const media::SessionClosedCB& session_closed_cb, |
| const media::SessionErrorCB& session_error_cb) |
| : manager_(manager), |
| cdm_id_(next_cdm_id_++), |
| - session_created_cb_(session_created_cb), |
| session_message_cb_(session_message_cb), |
| session_ready_cb_(session_ready_cb), |
| session_closed_cb_(session_closed_cb), |
| - session_error_cb_(session_error_cb) { |
| + session_error_cb_(session_error_cb), |
| + next_session_id_(1) { |
| } |
| void ProxyMediaKeys::InitializeCdm(const std::string& key_system, |
| @@ -137,4 +198,77 @@ void ProxyMediaKeys::InitializeCdm(const std::string& key_system, |
| manager_->InitializeCdm(cdm_id_, this, key_system, security_origin); |
| } |
| +uint32_t ProxyMediaKeys::CreateSessionId() { |
| + return next_session_id_++; |
| +} |
| + |
| +void ProxyMediaKeys::AssignWebSessionId(uint32_t session_id, |
| + const std::string& web_session_id) { |
| + web_session_to_session_id_map_.insert( |
| + std::make_pair(web_session_id, session_id)); |
| +} |
| + |
| +uint32_t ProxyMediaKeys::LookupSessionId(const std::string& web_session_id) { |
| + return web_session_to_session_id_map_.find(web_session_id)->second; |
| +} |
| + |
| +std::string ProxyMediaKeys::LookupWebSessionId(uint32_t session_id) { |
| + for (SessionMap::iterator it = web_session_to_session_id_map_.begin(); |
| + it != web_session_to_session_id_map_.end(); |
| + ++it) { |
| + if (it->second == session_id) |
| + return it->first; |
| + } |
| + // Possible to get an error creating a session, so no |web_session_id| |
| + // available. |
| + return std::string(); |
| +} |
| + |
| +void ProxyMediaKeys::DropWebSessionId(std::string web_session_id) { |
| + web_session_to_session_id_map_.erase(web_session_id); |
| +} |
| + |
| +void ProxyMediaKeys::RegisterVoidPromise( |
| + uint32_t session_id, |
| + scoped_ptr<media::CdmPromise<void> > promise) { |
| + // Should only be one promise outstanding for any |session_id|. |
| + DCHECK(session_id_to_promise_map_.find(session_id) == |
| + session_id_to_promise_map_.end()); |
| + session_id_to_promise_map_.insert( |
| + std::make_pair(session_id, promise.release())); |
| +} |
| + |
| +scoped_ptr<media::CdmPromise<void> > ProxyMediaKeys::RetrieveVoidPromise( |
| + uint32_t session_id) { |
| + PromiseMap::iterator it = session_id_to_promise_map_.find(session_id); |
| + // May not be a promise associated with this session for asynchronous events. |
| + if (it == session_id_to_promise_map_.end()) |
| + return scoped_ptr<media::CdmPromise<void> >(); |
| + scoped_ptr<media::CdmPromise<void> > result(it->second); |
| + session_id_to_promise_map_.erase(it); |
| + return result.Pass(); |
| +} |
| + |
| +void ProxyMediaKeys::RegisterSessionPromise( |
| + uint32_t session_id, |
| + scoped_ptr<media::CdmPromise<std::string> > promise) { |
| + // Should only be one promise outstanding for any |session_id|. |
| + DCHECK(session_id_to_new_session_promise_map_.find(session_id) == |
| + session_id_to_new_session_promise_map_.end()); |
| + session_id_to_new_session_promise_map_.insert( |
| + std::make_pair(session_id, promise.release())); |
| +} |
| + |
| +scoped_ptr<media::CdmPromise<std::string> > |
| +ProxyMediaKeys::RetrieveSessionPromise(uint32_t session_id) { |
| + PromiseMap::iterator it = |
| + session_id_to_new_session_promise_map_.find(session_id); |
| + // May not be a promise associated with this session for asynchronous events. |
| + if (it == session_id_to_new_session_promise_map_.end()) |
| + return scoped_ptr<media::CdmPromise<std::string> >(); |
| + scoped_ptr<media::CdmPromise<std::string> > result(it->second); |
| + session_id_to_new_session_promise_map_.erase(it); |
| + return result.Pass(); |
| +} |
| + |
| } // namespace content |