1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chromecast/media/cdm/browser_cdm_cast.h"
#include <utility>
#include "base/bind.h"
#include "base/location.h"
#include "base/single_thread_task_runner.h"
#include "base/thread_task_runner_handle.h"
#include "media/base/cdm_key_information.h"
#include "media/base/cdm_promise.h"
#include "media/cdm/player_tracker_impl.h"
namespace chromecast {
namespace media {
namespace {
// media::CdmPromiseTemplate implementation that wraps a promise so as to
// allow passing to other threads.
template <typename... T>
class CdmPromiseInternal : public ::media::CdmPromiseTemplate<T...> {
public:
CdmPromiseInternal(scoped_ptr<::media::CdmPromiseTemplate<T...>> promise)
: task_runner_(base::ThreadTaskRunnerHandle::Get()),
promise_(std::move(promise)) {}
~CdmPromiseInternal() final {
// Promise must be resolved or rejected before destruction.
DCHECK(!promise_);
}
// CdmPromiseTemplate<> implementation.
void resolve(const T&... result) final;
void reject(::media::MediaKeys::Exception exception,
uint32_t system_code,
const std::string& error_message) final {
MarkPromiseSettled();
task_runner_->PostTask(
FROM_HERE,
base::Bind(&::media::CdmPromiseTemplate<T...>::reject,
base::Owned(promise_.release()),
exception, system_code, error_message));
}
private:
using ::media::CdmPromiseTemplate<T...>::MarkPromiseSettled;
scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
scoped_ptr<::media::CdmPromiseTemplate<T...>> promise_;
};
template <typename... T>
void CdmPromiseInternal<T...>::resolve(const T&... result) {
MarkPromiseSettled();
task_runner_->PostTask(
FROM_HERE,
base::Bind(&::media::CdmPromiseTemplate<T...>::resolve,
base::Owned(promise_.release()),
result...));
}
template <typename... T>
scoped_ptr<CdmPromiseInternal<T...>> BindPromiseToCurrentLoop(
scoped_ptr<::media::CdmPromiseTemplate<T...>> promise) {
return make_scoped_ptr(new CdmPromiseInternal<T...>(std::move(promise)));
}
} // namespace
BrowserCdmCast::BrowserCdmCast() {
thread_checker_.DetachFromThread();
}
BrowserCdmCast::~BrowserCdmCast() {
DCHECK(thread_checker_.CalledOnValidThread());
DCHECK(player_tracker_impl_.get());
player_tracker_impl_->NotifyCdmUnset();
}
void BrowserCdmCast::Initialize(
const ::media::SessionMessageCB& session_message_cb,
const ::media::SessionClosedCB& session_closed_cb,
const ::media::LegacySessionErrorCB& legacy_session_error_cb,
const ::media::SessionKeysChangeCB& session_keys_change_cb,
const ::media::SessionExpirationUpdateCB& session_expiration_update_cb) {
DCHECK(thread_checker_.CalledOnValidThread());
player_tracker_impl_.reset(new ::media::PlayerTrackerImpl());
session_message_cb_ = session_message_cb;
session_closed_cb_ = session_closed_cb;
legacy_session_error_cb_ = legacy_session_error_cb;
session_keys_change_cb_ = session_keys_change_cb;
session_expiration_update_cb_ = session_expiration_update_cb;
InitializeInternal();
}
int BrowserCdmCast::RegisterPlayer(const base::Closure& new_key_cb,
const base::Closure& cdm_unset_cb) {
DCHECK(thread_checker_.CalledOnValidThread());
return player_tracker_impl_->RegisterPlayer(new_key_cb, cdm_unset_cb);
}
void BrowserCdmCast::UnregisterPlayer(int registration_id) {
DCHECK(thread_checker_.CalledOnValidThread());
player_tracker_impl_->UnregisterPlayer(registration_id);
}
void BrowserCdmCast::OnSessionMessage(
const std::string& session_id,
const std::vector<uint8_t>& message,
const GURL& destination_url,
::media::MediaKeys::MessageType message_type) {
session_message_cb_.Run(session_id,
message_type,
message,
destination_url);
}
void BrowserCdmCast::OnSessionClosed(const std::string& session_id) {
session_closed_cb_.Run(session_id);
}
void BrowserCdmCast::OnSessionKeysChange(const std::string& session_id,
bool newly_usable_keys,
::media::CdmKeysInfo keys_info) {
session_keys_change_cb_.Run(session_id, newly_usable_keys,
std::move(keys_info));
if (newly_usable_keys)
player_tracker_impl_->NotifyNewKey();
}
void BrowserCdmCast::KeyIdAndKeyPairsToInfo(
const ::media::KeyIdAndKeyPairs& keys,
::media::CdmKeysInfo* keys_info) {
DCHECK(keys_info);
for (const std::pair<std::string, std::string>& key : keys) {
scoped_ptr<::media::CdmKeyInformation> cdm_key_information(
new ::media::CdmKeyInformation(key.first,
::media::CdmKeyInformation::USABLE, 0));
keys_info->push_back(cdm_key_information.release());
}
}
// A macro runs current member function on |task_runner_| thread.
#define FORWARD_ON_CDM_THREAD(param_fn, ...) \
task_runner_->PostTask( \
FROM_HERE, \
base::Bind(&BrowserCdmCast::param_fn, \
base::Unretained(browser_cdm_cast_.get()), ##__VA_ARGS__))
BrowserCdmCastUi::BrowserCdmCastUi(
const scoped_refptr<BrowserCdmCast>& browser_cdm_cast,
const scoped_refptr<base::SingleThreadTaskRunner>& task_runner)
: browser_cdm_cast_(browser_cdm_cast), task_runner_(task_runner) {}
BrowserCdmCastUi::~BrowserCdmCastUi() {
DCHECK(thread_checker_.CalledOnValidThread());
browser_cdm_cast_->AddRef();
BrowserCdmCast* raw_cdm = browser_cdm_cast_.get();
browser_cdm_cast_ = nullptr;
task_runner_->ReleaseSoon(FROM_HERE, raw_cdm);
}
BrowserCdmCast* BrowserCdmCastUi::browser_cdm_cast() const {
DCHECK(thread_checker_.CalledOnValidThread());
return browser_cdm_cast_.get();
}
void BrowserCdmCastUi::SetServerCertificate(
const std::vector<uint8_t>& certificate,
scoped_ptr<::media::SimpleCdmPromise> promise) {
DCHECK(thread_checker_.CalledOnValidThread());
FORWARD_ON_CDM_THREAD(
SetServerCertificate, certificate,
base::Passed(BindPromiseToCurrentLoop(std::move(promise))));
}
void BrowserCdmCastUi::CreateSessionAndGenerateRequest(
::media::MediaKeys::SessionType session_type,
::media::EmeInitDataType init_data_type,
const std::vector<uint8_t>& init_data,
scoped_ptr<::media::NewSessionCdmPromise> promise) {
DCHECK(thread_checker_.CalledOnValidThread());
FORWARD_ON_CDM_THREAD(
CreateSessionAndGenerateRequest, session_type, init_data_type, init_data,
base::Passed(BindPromiseToCurrentLoop(std::move(promise))));
}
void BrowserCdmCastUi::LoadSession(
::media::MediaKeys::SessionType session_type,
const std::string& session_id,
scoped_ptr<::media::NewSessionCdmPromise> promise) {
DCHECK(thread_checker_.CalledOnValidThread());
FORWARD_ON_CDM_THREAD(
LoadSession, session_type, session_id,
base::Passed(BindPromiseToCurrentLoop(std::move(promise))));
}
void BrowserCdmCastUi::UpdateSession(
const std::string& session_id,
const std::vector<uint8_t>& response,
scoped_ptr<::media::SimpleCdmPromise> promise) {
DCHECK(thread_checker_.CalledOnValidThread());
FORWARD_ON_CDM_THREAD(
UpdateSession, session_id, response,
base::Passed(BindPromiseToCurrentLoop(std::move(promise))));
}
void BrowserCdmCastUi::CloseSession(
const std::string& session_id,
scoped_ptr<::media::SimpleCdmPromise> promise) {
DCHECK(thread_checker_.CalledOnValidThread());
FORWARD_ON_CDM_THREAD(
CloseSession, session_id,
base::Passed(BindPromiseToCurrentLoop(std::move(promise))));
}
void BrowserCdmCastUi::RemoveSession(
const std::string& session_id,
scoped_ptr<::media::SimpleCdmPromise> promise) {
DCHECK(thread_checker_.CalledOnValidThread());
FORWARD_ON_CDM_THREAD(
RemoveSession, session_id,
base::Passed(BindPromiseToCurrentLoop(std::move(promise))));
}
// A default empty implementation for subclasses that don't need to provide
// any key system specific initialization.
void BrowserCdmCast::InitializeInternal() {
}
} // namespace media
} // namespace chromecast
|