forked from WebPlatformForEmbedded/OCDM-Widevine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MediaSystem.cpp
233 lines (168 loc) · 6.45 KB
/
MediaSystem.cpp
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
/*
* Copyright 2016-2017 TATA ELXSI
* Copyright 2016-2017 Metrological
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "MediaSession.h"
#include "HostImplementation.h"
#include <assert.h>
#include <iostream>
#include <sstream>
#include <sys/utsname.h>
namespace CDMi {
class WideVine : public IMediaKeys, public widevine::Cdm::IEventListener
{
private:
WideVine (const WideVine&) = delete;
WideVine& operator= (const WideVine&) = delete;
typedef std::map<std::string, MediaKeySession*> SessionMap;
public:
WideVine()
: _adminLock()
, _cdm(nullptr)
, _host()
, _sessions() {
widevine::Cdm::ClientInfo client_info;
// Set client info that denotes this as the test suite:
client_info.product_name = "WPEFramework";
client_info.company_name = "www.metrological.com";
client_info.model_name = "www";
#if defined(__linux__)
client_info.device_name = "Linux";
{
struct utsname name;
if (!uname(&name)) {
client_info.arch_name = name.machine;
}
}
#else
client_info.device_name = "unknown";
#endif
client_info.build_info = __DATE__;
// widevine::Cdm::DeviceCertificateRequest cert_request;
if (widevine::Cdm::kSuccess == widevine::Cdm::initialize(
widevine::Cdm::kNoSecureOutput, client_info, &_host, &_host, &_host, static_cast<widevine::Cdm::LogLevel>(0))) {
// Setting the last parameter to true, requres serviceCertificates so the requests can be encrypted. Currently badly supported
// in the EME tests, so turn of for now :-)
_cdm = widevine::Cdm::create(this, &_host, false);
}
}
virtual ~WideVine() {
assert (_mediaKeySession == nullptr);
_adminLock.Lock();
SessionMap::iterator index (_sessions.begin());
while (index != _sessions.end()) {
delete index->second;
index++;
}
_sessions.clear();
_adminLock.Unlock();
if (_cdm != nullptr) {
delete _cdm;
}
}
virtual CDMi_RESULT CreateMediaKeySession(
int32_t licenseType,
const char *f_pwszInitDataType,
const uint8_t *f_pbInitData,
uint32_t f_cbInitData,
const uint8_t *f_pbCDMData,
uint32_t f_cbCDMData,
IMediaKeySession **f_ppiMediaKeySession) {
CDMi_RESULT dr = CDMi_S_FALSE;
*f_ppiMediaKeySession = nullptr;
MediaKeySession* mediaKeySession = new MediaKeySession(_cdm, licenseType);
dr = mediaKeySession->Init(licenseType,
f_pwszInitDataType,
f_pbInitData,
f_cbInitData,
f_pbCDMData,
f_cbCDMData);
if (dr != CDMi_SUCCESS) {
delete mediaKeySession;
}
else {
std::string sessionId (mediaKeySession->GetSessionId());
_sessions.insert(std::pair<std::string, MediaKeySession*>(sessionId, mediaKeySession));
*f_ppiMediaKeySession = mediaKeySession;
}
return dr;
}
virtual CDMi_RESULT SetServerCertificate(
const uint8_t *f_pbServerCertificate,
uint32_t f_cbServerCertificate) {
CDMi_RESULT dr = CDMi_S_FALSE;
std::string serverCertificate(reinterpret_cast<const char*>(f_pbServerCertificate), f_cbServerCertificate);
if (widevine::Cdm::kSuccess == _cdm->setServiceCertificate(serverCertificate)) {
dr = CDMi_SUCCESS;
}
return dr;
}
virtual CDMi_RESULT DestroyMediaKeySession(
IMediaKeySession *f_piMediaKeySession) {
std::string sessionId (f_piMediaKeySession->GetSessionId());
_adminLock.Lock();
SessionMap::iterator index (_sessions.find(sessionId));
if (index != _sessions.end()) {
_sessions.erase(index);
}
_adminLock.Unlock();
delete f_piMediaKeySession;
return CDMi_SUCCESS;
}
virtual void onMessage(const std::string& session_id,
widevine::Cdm::MessageType f_messageType,
const std::string& f_message) {
_adminLock.Lock();
SessionMap::iterator index (_sessions.find(session_id));
if (index != _sessions.end()) index->second->onMessage(f_messageType, f_message);
_adminLock.Unlock();
}
virtual void onKeyStatusesChange(const std::string& session_id) {
_adminLock.Lock();
SessionMap::iterator index (_sessions.find(session_id));
if (index != _sessions.end()) index->second->onKeyStatusChange();
_adminLock.Unlock();
}
virtual void onRemoveComplete(const std::string& session_id) {
_adminLock.Lock();
SessionMap::iterator index (_sessions.find(session_id));
if (index != _sessions.end()) index->second->onRemoveComplete();
_adminLock.Unlock();
}
// Called when a deferred action has completed.
virtual void onDeferredComplete(const std::string& session_id, widevine::Cdm::Status result) {
_adminLock.Lock();
SessionMap::iterator index (_sessions.find(session_id));
if (index != _sessions.end()) index->second->onDeferredComplete(result);
_adminLock.Unlock();
}
// Called when the CDM requires a new device certificate
virtual void onDirectIndividualizationRequest(const std::string& session_id, const std::string& request) {
_adminLock.Lock();
SessionMap::iterator index (_sessions.find(session_id));
if (index != _sessions.end()) index->second->onDirectIndividualizationRequest(request);
_adminLock.Unlock();
}
private:
WPEFramework::Core::CriticalSection _adminLock;
widevine::Cdm* _cdm;
HostImplementation _host;
SessionMap _sessions;
};
static SystemFactoryType<WideVine> g_instance({"video/webm", "video/mp4", "audio/webm", "audio/mp4"});
} // namespace CDMi
CDMi::ISystemFactory* GetSystemFactory() {
return (&CDMi::g_instance);
}