1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chromecast/media/cdm/browser_cdm_cast.h"
8 #include "base/location.h"
9 #include "base/single_thread_task_runner.h"
10 #include "media/base/cdm_key_information.h"
11 #include "media/base/cdm_promise.h"
12 #include "media/cdm/player_tracker_impl.h"
14 namespace chromecast
{
17 BrowserCdmCast::BrowserCdmCast() {
18 thread_checker_
.DetachFromThread();
21 BrowserCdmCast::~BrowserCdmCast() {
22 DCHECK(thread_checker_
.CalledOnValidThread());
23 DCHECK(player_tracker_impl_
.get());
24 player_tracker_impl_
->NotifyCdmUnset();
27 void BrowserCdmCast::Initialize(
28 const ::media::SessionMessageCB
& session_message_cb
,
29 const ::media::SessionClosedCB
& session_closed_cb
,
30 const ::media::LegacySessionErrorCB
& legacy_session_error_cb
,
31 const ::media::SessionKeysChangeCB
& session_keys_change_cb
,
32 const ::media::SessionExpirationUpdateCB
& session_expiration_update_cb
) {
33 DCHECK(thread_checker_
.CalledOnValidThread());
35 player_tracker_impl_
.reset(new ::media::PlayerTrackerImpl
);
37 session_message_cb_
= session_message_cb
;
38 session_closed_cb_
= session_closed_cb
;
39 legacy_session_error_cb_
= legacy_session_error_cb
;
40 session_keys_change_cb_
= session_keys_change_cb
;
41 session_expiration_update_cb_
= session_expiration_update_cb
;
46 int BrowserCdmCast::RegisterPlayer(const base::Closure
& new_key_cb
,
47 const base::Closure
& cdm_unset_cb
) {
48 DCHECK(thread_checker_
.CalledOnValidThread());
49 return player_tracker_impl_
->RegisterPlayer(new_key_cb
, cdm_unset_cb
);
52 void BrowserCdmCast::UnregisterPlayer(int registration_id
) {
53 DCHECK(thread_checker_
.CalledOnValidThread());
54 player_tracker_impl_
->UnregisterPlayer(registration_id
);
57 ::media::CdmContext
* BrowserCdmCast::GetCdmContext() {
62 void BrowserCdmCast::OnSessionMessage(
63 const std::string
& session_id
,
64 const std::vector
<uint8_t>& message
,
65 const GURL
& destination_url
,
66 ::media::MediaKeys::MessageType message_type
) {
67 session_message_cb_
.Run(session_id
,
73 void BrowserCdmCast::OnSessionClosed(const std::string
& session_id
) {
74 session_closed_cb_
.Run(session_id
);
77 void BrowserCdmCast::OnSessionKeysChange(
78 const std::string
& session_id
,
79 const ::media::KeyIdAndKeyPairs
& keys
) {
80 ::media::CdmKeysInfo cdm_keys_info
;
81 for (const std::pair
<std::string
, std::string
>& key
: keys
) {
82 scoped_ptr
< ::media::CdmKeyInformation
> cdm_key_information(
83 new ::media::CdmKeyInformation());
84 cdm_key_information
->key_id
.assign(key
.first
.begin(), key
.first
.end());
85 cdm_keys_info
.push_back(cdm_key_information
.release());
87 session_keys_change_cb_
.Run(session_id
, true, cdm_keys_info
.Pass());
89 player_tracker_impl_
->NotifyNewKey();
92 // A macro runs current member function on |task_runner_| thread.
93 #define FORWARD_ON_CDM_THREAD(param_fn, ...) \
94 task_runner_->PostTask( \
96 base::Bind(&BrowserCdmCast::param_fn, \
97 base::Unretained(browser_cdm_cast_.get()), ##__VA_ARGS__))
99 BrowserCdmCastUi::BrowserCdmCastUi(
100 scoped_ptr
<BrowserCdmCast
> browser_cdm_cast
,
101 const scoped_refptr
<base::SingleThreadTaskRunner
>& task_runner
)
102 : browser_cdm_cast_(browser_cdm_cast
.Pass()), task_runner_(task_runner
) {
105 BrowserCdmCastUi::~BrowserCdmCastUi() {
106 DCHECK(thread_checker_
.CalledOnValidThread());
107 task_runner_
->DeleteSoon(FROM_HERE
, browser_cdm_cast_
.release());
110 int BrowserCdmCastUi::RegisterPlayer(const base::Closure
& new_key_cb
,
111 const base::Closure
& cdm_unset_cb
) {
112 NOTREACHED() << "RegisterPlayer should be called on BrowserCdmCast";
116 void BrowserCdmCastUi::UnregisterPlayer(int registration_id
) {
117 NOTREACHED() << "UnregisterPlayer should be called on BrowserCdmCast";
120 BrowserCdmCast
* BrowserCdmCastUi::browser_cdm_cast() const {
121 DCHECK(thread_checker_
.CalledOnValidThread());
122 return browser_cdm_cast_
.get();
125 void BrowserCdmCastUi::SetServerCertificate(
126 const std::vector
<uint8_t>& certificate
,
127 scoped_ptr
<::media::SimpleCdmPromise
> promise
) {
128 DCHECK(thread_checker_
.CalledOnValidThread());
129 FORWARD_ON_CDM_THREAD(
130 SetServerCertificate
,
132 base::Passed(&promise
));
135 void BrowserCdmCastUi::CreateSessionAndGenerateRequest(
136 ::media::MediaKeys::SessionType session_type
,
137 ::media::EmeInitDataType init_data_type
,
138 const std::vector
<uint8_t>& init_data
,
139 scoped_ptr
<::media::NewSessionCdmPromise
> promise
) {
140 DCHECK(thread_checker_
.CalledOnValidThread());
141 FORWARD_ON_CDM_THREAD(
142 CreateSessionAndGenerateRequest
,
146 base::Passed(&promise
));
149 void BrowserCdmCastUi::LoadSession(
150 ::media::MediaKeys::SessionType session_type
,
151 const std::string
& session_id
,
152 scoped_ptr
<::media::NewSessionCdmPromise
> promise
) {
153 DCHECK(thread_checker_
.CalledOnValidThread());
154 FORWARD_ON_CDM_THREAD(
155 LoadSession
, session_type
, session_id
, base::Passed(&promise
));
158 void BrowserCdmCastUi::UpdateSession(
159 const std::string
& session_id
,
160 const std::vector
<uint8_t>& response
,
161 scoped_ptr
<::media::SimpleCdmPromise
> promise
) {
162 DCHECK(thread_checker_
.CalledOnValidThread());
163 FORWARD_ON_CDM_THREAD(
167 base::Passed(&promise
));
170 void BrowserCdmCastUi::CloseSession(
171 const std::string
& session_id
,
172 scoped_ptr
<::media::SimpleCdmPromise
> promise
) {
173 DCHECK(thread_checker_
.CalledOnValidThread());
174 FORWARD_ON_CDM_THREAD(CloseSession
, session_id
, base::Passed(&promise
));
177 void BrowserCdmCastUi::RemoveSession(
178 const std::string
& session_id
,
179 scoped_ptr
<::media::SimpleCdmPromise
> promise
) {
180 DCHECK(thread_checker_
.CalledOnValidThread());
181 FORWARD_ON_CDM_THREAD(RemoveSession
, session_id
, base::Passed(&promise
));
184 ::media::CdmContext
* BrowserCdmCastUi::GetCdmContext() {
189 // A default empty implementation for subclasses that don't need to provide
190 // any key system specific initialization.
191 void BrowserCdmCast::InitializeInternal() {
195 } // namespace chromecast