Line data Source code
1 : /*
2 : * If not stated otherwise in this file or this component's LICENSE file the
3 : * following copyright and licenses apply:
4 : *
5 : * Copyright 2022 Sky UK
6 : *
7 : * Licensed under the Apache License, Version 2.0 (the "License");
8 : * you may not use this file except in compliance with the License.
9 : * You may obtain a copy of the License at
10 : *
11 : * http://www.apache.org/licenses/LICENSE-2.0
12 : *
13 : * Unless required by applicable law or agreed to in writing, software
14 : * distributed under the License is distributed on an "AS IS" BASIS,
15 : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 : * See the License for the specific language governing permissions and
17 : * limitations under the License.
18 : */
19 :
20 : #include <algorithm>
21 : #include <exception>
22 : #include <memory>
23 : #include <string>
24 : #include <utility>
25 : #include <vector>
26 :
27 : #include "CdmService.h"
28 : #include "RialtoServerLogging.h"
29 :
30 : namespace firebolt::rialto::server::service
31 : {
32 96 : CdmService::CdmService(std::shared_ptr<IMediaKeysServerInternalFactory> &&mediaKeysFactory,
33 96 : std::shared_ptr<IMediaKeysCapabilitiesFactory> &&mediaKeysCapabilitiesFactory)
34 96 : : m_mediaKeysFactory{mediaKeysFactory}, m_mediaKeysCapabilitiesFactory{mediaKeysCapabilitiesFactory},
35 192 : m_isActive{false}
36 : {
37 96 : RIALTO_SERVER_LOG_DEBUG("CdmService is constructed");
38 : }
39 :
40 96 : CdmService::~CdmService()
41 : {
42 96 : RIALTO_SERVER_LOG_DEBUG("CdmService is destructed");
43 : }
44 :
45 91 : bool CdmService::switchToActive()
46 : {
47 91 : RIALTO_SERVER_LOG_INFO("Switching SessionServer to Active state.");
48 91 : m_isActive = true;
49 91 : return true;
50 : }
51 :
52 2 : void CdmService::switchToInactive()
53 : {
54 2 : RIALTO_SERVER_LOG_INFO("Switching SessionServer to Inactive state. Cleaning resources...");
55 2 : m_isActive = false;
56 :
57 : {
58 2 : std::lock_guard<std::mutex> lock{m_mediaKeysMutex};
59 2 : m_mediaKeys.clear();
60 : }
61 : }
62 :
63 62 : bool CdmService::createMediaKeys(int mediaKeysHandle, std::string keySystem)
64 : {
65 62 : RIALTO_SERVER_LOG_DEBUG("CdmService requested to create new media keys handle: %d", mediaKeysHandle);
66 62 : if (!m_isActive)
67 : {
68 2 : RIALTO_SERVER_LOG_ERROR("Skip to create media keys handle: %d - Session Server in Inactive state",
69 : mediaKeysHandle);
70 2 : return false;
71 : }
72 :
73 : {
74 60 : std::lock_guard<std::mutex> lock{m_mediaKeysMutex};
75 60 : if (m_mediaKeys.find(mediaKeysHandle) != m_mediaKeys.end())
76 : {
77 1 : RIALTO_SERVER_LOG_ERROR("Media keys handle: %d already exists", mediaKeysHandle);
78 1 : return false;
79 : }
80 59 : m_mediaKeys.emplace(std::make_pair(mediaKeysHandle, m_mediaKeysFactory->createMediaKeysServerInternal(keySystem)));
81 59 : if (!m_mediaKeys.at(mediaKeysHandle))
82 : {
83 1 : RIALTO_SERVER_LOG_ERROR("Could not create MediaKeys for media keys handle: %d", mediaKeysHandle);
84 1 : m_mediaKeys.erase(mediaKeysHandle);
85 1 : return false;
86 : }
87 60 : }
88 :
89 58 : RIALTO_SERVER_LOG_INFO("New media keys handle: %d created", mediaKeysHandle);
90 58 : return true;
91 : }
92 :
93 56 : bool CdmService::destroyMediaKeys(int mediaKeysHandle)
94 : {
95 56 : RIALTO_SERVER_LOG_DEBUG("CdmService requested to destroy media keys handle: %d", mediaKeysHandle);
96 :
97 : {
98 56 : std::lock_guard<std::mutex> lock{m_mediaKeysMutex};
99 56 : auto mediaKeysIter = m_mediaKeys.find(mediaKeysHandle);
100 56 : if (mediaKeysIter == m_mediaKeys.end())
101 : {
102 2 : RIALTO_SERVER_LOG_ERROR("Media keys handle: %d does not exists", mediaKeysHandle);
103 2 : return false;
104 : }
105 68 : for (auto it = m_sessionInfo.begin(); it != m_sessionInfo.end();)
106 : {
107 14 : if (it->second.mediaKeysHandle == mediaKeysHandle)
108 : {
109 14 : it = m_sessionInfo.erase(it);
110 : }
111 : else
112 : {
113 0 : ++it;
114 : }
115 : }
116 54 : m_mediaKeys.erase(mediaKeysIter);
117 56 : }
118 :
119 54 : RIALTO_SERVER_LOG_INFO("Media keys handle: %d destroyed", mediaKeysHandle);
120 54 : return true;
121 : }
122 :
123 21 : MediaKeyErrorStatus CdmService::createKeySession(int mediaKeysHandle, KeySessionType sessionType,
124 : const std::shared_ptr<IMediaKeysClient> &client, int32_t &keySessionId)
125 : {
126 21 : RIALTO_SERVER_LOG_DEBUG("CdmService requested to create key session: %d", mediaKeysHandle);
127 :
128 21 : std::lock_guard<std::mutex> lock{m_mediaKeysMutex};
129 21 : auto mediaKeysIter = m_mediaKeys.find(mediaKeysHandle);
130 21 : if (mediaKeysIter == m_mediaKeys.end())
131 : {
132 1 : RIALTO_SERVER_LOG_ERROR("Media keys handle: %d does not exists", mediaKeysHandle);
133 1 : return MediaKeyErrorStatus::FAIL;
134 : }
135 :
136 20 : MediaKeyErrorStatus status = mediaKeysIter->second->createKeySession(sessionType, client, keySessionId);
137 20 : if (MediaKeyErrorStatus::OK == status)
138 : {
139 19 : if (m_mediaKeysClients.find(keySessionId) != m_mediaKeysClients.end())
140 : {
141 1 : RIALTO_SERVER_LOG_ERROR("Media keys client for key session: %d already exists", keySessionId);
142 1 : static_cast<void>(removeKeySessionInternal(mediaKeysHandle, keySessionId));
143 1 : return MediaKeyErrorStatus::FAIL;
144 : }
145 18 : m_sessionInfo.emplace(std::make_pair(keySessionId, MediaKeySessionInfo{mediaKeysHandle}));
146 18 : m_mediaKeysClients.emplace(std::make_pair(keySessionId, client));
147 : }
148 :
149 19 : return status;
150 21 : }
151 :
152 4 : MediaKeyErrorStatus CdmService::generateRequest(int mediaKeysHandle, int32_t keySessionId, InitDataType initDataType,
153 : const std::vector<uint8_t> &initData,
154 : const LimitedDurationLicense &ldlState)
155 : {
156 4 : RIALTO_SERVER_LOG_DEBUG("CdmService requested to generate request: %d", mediaKeysHandle);
157 :
158 4 : std::lock_guard<std::mutex> lock{m_mediaKeysMutex};
159 4 : auto mediaKeysIter = m_mediaKeys.find(mediaKeysHandle);
160 4 : if (mediaKeysIter == m_mediaKeys.end())
161 : {
162 1 : RIALTO_SERVER_LOG_ERROR("Media keys handle: %d does not exists", mediaKeysHandle);
163 1 : return MediaKeyErrorStatus::FAIL;
164 : }
165 3 : if (LimitedDurationLicense::NOT_SPECIFIED != ldlState && m_sessionInfo.find(keySessionId) != m_sessionInfo.end())
166 : {
167 1 : m_sessionInfo[keySessionId].isExtendedInterfaceUsed = true;
168 : }
169 3 : return mediaKeysIter->second->generateRequest(keySessionId, initDataType, initData, ldlState);
170 4 : }
171 :
172 3 : MediaKeyErrorStatus CdmService::loadSession(int mediaKeysHandle, int32_t keySessionId)
173 : {
174 3 : RIALTO_SERVER_LOG_DEBUG("CdmService requested to load session: %d", mediaKeysHandle);
175 :
176 3 : std::lock_guard<std::mutex> lock{m_mediaKeysMutex};
177 3 : auto mediaKeysIter = m_mediaKeys.find(mediaKeysHandle);
178 3 : if (mediaKeysIter == m_mediaKeys.end())
179 : {
180 1 : RIALTO_SERVER_LOG_ERROR("Media keys handle: %d does not exists", mediaKeysHandle);
181 1 : return MediaKeyErrorStatus::FAIL;
182 : }
183 2 : return mediaKeysIter->second->loadSession(keySessionId);
184 3 : }
185 :
186 3 : MediaKeyErrorStatus CdmService::updateSession(int mediaKeysHandle, int32_t keySessionId,
187 : const std::vector<uint8_t> &responseData)
188 : {
189 3 : RIALTO_SERVER_LOG_DEBUG("CdmService requested to update session: %d", mediaKeysHandle);
190 :
191 3 : std::lock_guard<std::mutex> lock{m_mediaKeysMutex};
192 3 : auto mediaKeysIter = m_mediaKeys.find(mediaKeysHandle);
193 3 : if (mediaKeysIter == m_mediaKeys.end())
194 : {
195 1 : RIALTO_SERVER_LOG_ERROR("Media keys handle: %d does not exists", mediaKeysHandle);
196 1 : return MediaKeyErrorStatus::FAIL;
197 : }
198 2 : return mediaKeysIter->second->updateSession(keySessionId, responseData);
199 3 : }
200 :
201 5 : MediaKeyErrorStatus CdmService::closeKeySession(int mediaKeysHandle, int32_t keySessionId)
202 : {
203 5 : RIALTO_SERVER_LOG_DEBUG("CdmService requested to close key session: %d", mediaKeysHandle);
204 :
205 5 : std::lock_guard<std::mutex> lock{m_mediaKeysMutex};
206 5 : auto mediaKeysHandleIter{m_sessionInfo.find(keySessionId)};
207 5 : if (mediaKeysHandleIter == m_sessionInfo.end())
208 : {
209 1 : RIALTO_SERVER_LOG_ERROR("Media keys handle for mksId: %d does not exists", keySessionId);
210 1 : return MediaKeyErrorStatus::FAIL;
211 : }
212 4 : if (mediaKeysHandleIter->second.refCounter > 0)
213 : {
214 2 : RIALTO_SERVER_LOG_INFO("Deferring closing of mksId %d", keySessionId);
215 2 : mediaKeysHandleIter->second.shouldBeClosed = true;
216 2 : return MediaKeyErrorStatus::OK;
217 : }
218 2 : return m_mediaKeys[mediaKeysHandleIter->second.mediaKeysHandle]->closeKeySession(keySessionId);
219 5 : }
220 :
221 3 : MediaKeyErrorStatus CdmService::removeKeySession(int mediaKeysHandle, int32_t keySessionId)
222 : {
223 3 : RIALTO_SERVER_LOG_DEBUG("CdmService requested to remove key session: %d", mediaKeysHandle);
224 :
225 3 : std::lock_guard<std::mutex> lock{m_mediaKeysMutex};
226 6 : return removeKeySessionInternal(mediaKeysHandle, keySessionId);
227 3 : }
228 :
229 4 : MediaKeyErrorStatus CdmService::removeKeySessionInternal(int mediaKeysHandle, int32_t keySessionId)
230 : {
231 4 : auto mediaKeysIter = m_mediaKeys.find(mediaKeysHandle);
232 4 : if (mediaKeysIter == m_mediaKeys.end())
233 : {
234 1 : RIALTO_SERVER_LOG_ERROR("Media keys handle: %d does not exists", mediaKeysHandle);
235 1 : return MediaKeyErrorStatus::FAIL;
236 : }
237 :
238 3 : MediaKeyErrorStatus status = mediaKeysIter->second->removeKeySession(keySessionId);
239 3 : if (MediaKeyErrorStatus::OK == status)
240 : {
241 2 : auto mediaKeysClientsIter = m_mediaKeysClients.find(keySessionId);
242 2 : if (mediaKeysClientsIter != m_mediaKeysClients.end())
243 : {
244 1 : m_mediaKeysClients.erase(mediaKeysClientsIter);
245 : }
246 : }
247 :
248 3 : return status;
249 : }
250 :
251 3 : MediaKeyErrorStatus CdmService::getCdmKeySessionId(int mediaKeysHandle, int32_t keySessionId, std::string &cdmKeySessionId)
252 : {
253 3 : RIALTO_SERVER_LOG_DEBUG("CdmService requested to get cdm key session id: %d", mediaKeysHandle);
254 :
255 3 : std::lock_guard<std::mutex> lock{m_mediaKeysMutex};
256 3 : auto mediaKeysIter = m_mediaKeys.find(mediaKeysHandle);
257 3 : if (mediaKeysIter == m_mediaKeys.end())
258 : {
259 1 : RIALTO_SERVER_LOG_ERROR("Media keys handle: %d does not exists", mediaKeysHandle);
260 1 : return MediaKeyErrorStatus::FAIL;
261 : }
262 :
263 2 : MediaKeyErrorStatus status = mediaKeysIter->second->getCdmKeySessionId(keySessionId, cdmKeySessionId);
264 2 : return status;
265 3 : }
266 :
267 3 : bool CdmService::containsKey(int mediaKeysHandle, int32_t keySessionId, const std::vector<uint8_t> &keyId)
268 : {
269 3 : RIALTO_SERVER_LOG_DEBUG("CdmService requested to check if key is present: %d", mediaKeysHandle);
270 :
271 3 : std::lock_guard<std::mutex> lock{m_mediaKeysMutex};
272 3 : auto mediaKeysIter = m_mediaKeys.find(mediaKeysHandle);
273 3 : if (mediaKeysIter == m_mediaKeys.end())
274 : {
275 1 : RIALTO_SERVER_LOG_ERROR("Media keys handle: %d does not exists", mediaKeysHandle);
276 1 : return false;
277 : }
278 :
279 2 : return mediaKeysIter->second->containsKey(keySessionId, keyId);
280 3 : }
281 :
282 3 : MediaKeyErrorStatus CdmService::setDrmHeader(int mediaKeysHandle, int32_t keySessionId,
283 : const std::vector<uint8_t> &requestData)
284 : {
285 3 : RIALTO_SERVER_LOG_DEBUG("CdmService requested to set drm header: %d", mediaKeysHandle);
286 :
287 3 : std::lock_guard<std::mutex> lock{m_mediaKeysMutex};
288 3 : auto mediaKeysIter = m_mediaKeys.find(mediaKeysHandle);
289 3 : if (mediaKeysIter == m_mediaKeys.end())
290 : {
291 1 : RIALTO_SERVER_LOG_ERROR("Media keys handle: %d does not exists", mediaKeysHandle);
292 1 : return MediaKeyErrorStatus::FAIL;
293 : }
294 2 : if (m_sessionInfo.find(keySessionId) != m_sessionInfo.end())
295 : {
296 1 : m_sessionInfo[keySessionId].isExtendedInterfaceUsed = true;
297 : }
298 2 : return mediaKeysIter->second->setDrmHeader(keySessionId, requestData);
299 3 : }
300 :
301 3 : MediaKeyErrorStatus CdmService::deleteDrmStore(int mediaKeysHandle)
302 : {
303 3 : RIALTO_SERVER_LOG_DEBUG("CdmService requested to delete drm store: %d", mediaKeysHandle);
304 :
305 3 : std::lock_guard<std::mutex> lock{m_mediaKeysMutex};
306 3 : auto mediaKeysIter = m_mediaKeys.find(mediaKeysHandle);
307 3 : if (mediaKeysIter == m_mediaKeys.end())
308 : {
309 1 : RIALTO_SERVER_LOG_ERROR("Media keys handle: %d does not exists", mediaKeysHandle);
310 1 : return MediaKeyErrorStatus::FAIL;
311 : }
312 2 : return mediaKeysIter->second->deleteDrmStore();
313 3 : }
314 :
315 3 : MediaKeyErrorStatus CdmService::deleteKeyStore(int mediaKeysHandle)
316 : {
317 3 : RIALTO_SERVER_LOG_DEBUG("CdmService requested to delete key store: %d", mediaKeysHandle);
318 :
319 3 : std::lock_guard<std::mutex> lock{m_mediaKeysMutex};
320 3 : auto mediaKeysIter = m_mediaKeys.find(mediaKeysHandle);
321 3 : if (mediaKeysIter == m_mediaKeys.end())
322 : {
323 1 : RIALTO_SERVER_LOG_ERROR("Media keys handle: %d does not exists", mediaKeysHandle);
324 1 : return MediaKeyErrorStatus::FAIL;
325 : }
326 :
327 2 : return mediaKeysIter->second->deleteKeyStore();
328 3 : }
329 :
330 3 : MediaKeyErrorStatus CdmService::getDrmStoreHash(int mediaKeysHandle, std::vector<unsigned char> &drmStoreHash)
331 : {
332 3 : RIALTO_SERVER_LOG_DEBUG("CdmService requested to get drm store hash: %d", mediaKeysHandle);
333 :
334 3 : std::lock_guard<std::mutex> lock{m_mediaKeysMutex};
335 3 : auto mediaKeysIter = m_mediaKeys.find(mediaKeysHandle);
336 3 : if (mediaKeysIter == m_mediaKeys.end())
337 : {
338 1 : RIALTO_SERVER_LOG_ERROR("Media keys handle: %d does not exists", mediaKeysHandle);
339 1 : return MediaKeyErrorStatus::FAIL;
340 : }
341 2 : return mediaKeysIter->second->getDrmStoreHash(drmStoreHash);
342 3 : }
343 :
344 3 : MediaKeyErrorStatus CdmService::getKeyStoreHash(int mediaKeysHandle, std::vector<unsigned char> &keyStoreHash)
345 : {
346 3 : RIALTO_SERVER_LOG_DEBUG("CdmService requested to get key store hash: %d", mediaKeysHandle);
347 :
348 3 : std::lock_guard<std::mutex> lock{m_mediaKeysMutex};
349 3 : auto mediaKeysIter = m_mediaKeys.find(mediaKeysHandle);
350 3 : if (mediaKeysIter == m_mediaKeys.end())
351 : {
352 1 : RIALTO_SERVER_LOG_ERROR("Media keys handle: %d does not exists", mediaKeysHandle);
353 1 : return MediaKeyErrorStatus::FAIL;
354 : }
355 2 : return mediaKeysIter->second->getKeyStoreHash(keyStoreHash);
356 3 : }
357 :
358 3 : MediaKeyErrorStatus CdmService::getLdlSessionsLimit(int mediaKeysHandle, uint32_t &ldlLimit)
359 : {
360 3 : RIALTO_SERVER_LOG_DEBUG("CdmService requested to get ldl sessions limit: %d", mediaKeysHandle);
361 :
362 3 : std::lock_guard<std::mutex> lock{m_mediaKeysMutex};
363 3 : auto mediaKeysIter = m_mediaKeys.find(mediaKeysHandle);
364 3 : if (mediaKeysIter == m_mediaKeys.end())
365 : {
366 1 : RIALTO_SERVER_LOG_ERROR("Media keys handle: %d does not exists", mediaKeysHandle);
367 1 : return MediaKeyErrorStatus::FAIL;
368 : }
369 2 : return mediaKeysIter->second->getLdlSessionsLimit(ldlLimit);
370 3 : }
371 :
372 3 : MediaKeyErrorStatus CdmService::getLastDrmError(int mediaKeysHandle, int32_t keySessionId, uint32_t &errorCode)
373 : {
374 3 : RIALTO_SERVER_LOG_DEBUG("CdmService requested to get last drm error: %d", mediaKeysHandle);
375 :
376 3 : std::lock_guard<std::mutex> lock{m_mediaKeysMutex};
377 3 : auto mediaKeysIter = m_mediaKeys.find(mediaKeysHandle);
378 3 : if (mediaKeysIter == m_mediaKeys.end())
379 : {
380 1 : RIALTO_SERVER_LOG_ERROR("Media keys handle: %d does not exists", mediaKeysHandle);
381 1 : return MediaKeyErrorStatus::FAIL;
382 : }
383 2 : return mediaKeysIter->second->getLastDrmError(keySessionId, errorCode);
384 3 : }
385 :
386 3 : MediaKeyErrorStatus CdmService::getDrmTime(int mediaKeysHandle, uint64_t &drmTime)
387 : {
388 3 : RIALTO_SERVER_LOG_DEBUG("CdmService requested to get drm time: %d", mediaKeysHandle);
389 :
390 3 : std::lock_guard<std::mutex> lock{m_mediaKeysMutex};
391 3 : auto mediaKeysIter = m_mediaKeys.find(mediaKeysHandle);
392 3 : if (mediaKeysIter == m_mediaKeys.end())
393 : {
394 1 : RIALTO_SERVER_LOG_ERROR("Media keys handle: %d does not exists", mediaKeysHandle);
395 1 : return MediaKeyErrorStatus::FAIL;
396 : }
397 2 : return mediaKeysIter->second->getDrmTime(drmTime);
398 3 : }
399 :
400 4 : MediaKeyErrorStatus CdmService::releaseKeySession(int mediaKeysHandle, int32_t keySessionId)
401 : {
402 4 : RIALTO_SERVER_LOG_DEBUG("CdmService requested to release key session: %d", mediaKeysHandle);
403 :
404 4 : std::lock_guard<std::mutex> lock{m_mediaKeysMutex};
405 4 : auto mediaKeysHandleIter{m_sessionInfo.find(keySessionId)};
406 4 : if (mediaKeysHandleIter == m_sessionInfo.end())
407 : {
408 1 : RIALTO_SERVER_LOG_ERROR("Media keys handle for mksId: %d does not exists", keySessionId);
409 1 : return MediaKeyErrorStatus::FAIL;
410 : }
411 3 : if (mediaKeysHandleIter->second.refCounter > 0)
412 : {
413 1 : RIALTO_SERVER_LOG_INFO("Deferring releasing of key session %d", keySessionId);
414 1 : mediaKeysHandleIter->second.shouldBeReleased = true;
415 1 : return MediaKeyErrorStatus::OK;
416 : }
417 2 : const auto result = m_mediaKeys[mediaKeysHandleIter->second.mediaKeysHandle]->releaseKeySession(keySessionId);
418 2 : m_sessionInfo.erase(keySessionId);
419 2 : return result;
420 4 : }
421 :
422 3 : std::vector<std::string> CdmService::getSupportedKeySystems()
423 : {
424 3 : RIALTO_SERVER_LOG_DEBUG("CdmService requested to getSupportedKeySystems");
425 :
426 3 : if (!m_isActive)
427 : {
428 1 : RIALTO_SERVER_LOG_ERROR("Skip to get supported key systems: Session Server in Inactive state");
429 1 : return {};
430 : }
431 :
432 2 : auto mediaKeysCapabilities = m_mediaKeysCapabilitiesFactory->getMediaKeysCapabilities();
433 2 : if (!mediaKeysCapabilities)
434 : {
435 1 : RIALTO_SERVER_LOG_ERROR("Failed to create the mediaKeysCapabilities object");
436 1 : return {};
437 : }
438 1 : return mediaKeysCapabilities->getSupportedKeySystems();
439 2 : }
440 :
441 3 : bool CdmService::supportsKeySystem(const std::string &keySystem)
442 : {
443 3 : RIALTO_SERVER_LOG_DEBUG("CdmService requested to supportsKeySystem");
444 :
445 3 : if (!m_isActive)
446 : {
447 1 : RIALTO_SERVER_LOG_ERROR("Skip to get supported key systems: Session Server in Inactive state");
448 1 : return false;
449 : }
450 :
451 2 : auto mediaKeysCapabilities = m_mediaKeysCapabilitiesFactory->getMediaKeysCapabilities();
452 2 : if (!mediaKeysCapabilities)
453 : {
454 1 : RIALTO_SERVER_LOG_ERROR("Failed to create the mediaKeysCapabilities object");
455 1 : return false;
456 : }
457 1 : return mediaKeysCapabilities->supportsKeySystem(keySystem);
458 2 : }
459 :
460 4 : bool CdmService::getSupportedKeySystemVersion(const std::string &keySystem, std::string &version)
461 : {
462 4 : RIALTO_SERVER_LOG_DEBUG("CdmService requested to getSupportedKeySystemVersion");
463 :
464 4 : if (!m_isActive)
465 : {
466 1 : RIALTO_SERVER_LOG_ERROR("Skip to get supported key systems: Session Server in Inactive state");
467 1 : return false;
468 : }
469 :
470 3 : auto mediaKeysCapabilities = m_mediaKeysCapabilitiesFactory->getMediaKeysCapabilities();
471 3 : if (!mediaKeysCapabilities)
472 : {
473 1 : RIALTO_SERVER_LOG_ERROR("Failed to create the mediaKeysCapabilities object");
474 1 : return false;
475 : }
476 2 : return mediaKeysCapabilities->getSupportedKeySystemVersion(keySystem, version);
477 3 : }
478 :
479 3 : bool CdmService::isServerCertificateSupported(const std::string &keySystem)
480 : {
481 3 : RIALTO_SERVER_LOG_DEBUG("CdmService requested to isServerCertificateSupported");
482 :
483 3 : if (!m_isActive)
484 : {
485 1 : RIALTO_SERVER_LOG_ERROR("Skip to check if server cert is supported: Session Server in Inactive state");
486 1 : return false;
487 : }
488 :
489 2 : auto mediaKeysCapabilities = m_mediaKeysCapabilitiesFactory->getMediaKeysCapabilities();
490 2 : if (!mediaKeysCapabilities)
491 : {
492 1 : RIALTO_SERVER_LOG_ERROR("Failed to create the mediaKeysCapabilities object");
493 1 : return false;
494 : }
495 1 : return mediaKeysCapabilities->isServerCertificateSupported(keySystem);
496 2 : }
497 :
498 4 : MediaKeyErrorStatus CdmService::decrypt(int32_t keySessionId, GstBuffer *encrypted, GstCaps *caps)
499 : {
500 4 : RIALTO_SERVER_LOG_DEBUG("CdmService requested to decrypt, key session id: %d", keySessionId);
501 :
502 4 : std::lock_guard<std::mutex> lock{m_mediaKeysMutex};
503 4 : auto mediaKeysHandleIter{m_sessionInfo.find(keySessionId)};
504 4 : if (mediaKeysHandleIter == m_sessionInfo.end())
505 : {
506 2 : RIALTO_SERVER_LOG_ERROR("Media keys handle for mksId: %d does not exists", keySessionId);
507 2 : return MediaKeyErrorStatus::FAIL;
508 : }
509 2 : return m_mediaKeys[mediaKeysHandleIter->second.mediaKeysHandle]->decrypt(keySessionId, encrypted, caps);
510 4 : }
511 :
512 7 : bool CdmService::isExtendedInterfaceUsed(int32_t keySessionId)
513 : {
514 7 : RIALTO_SERVER_LOG_DEBUG("CdmService requested to check if extended interface is used, key session id: %d",
515 : keySessionId);
516 :
517 7 : std::lock_guard<std::mutex> lock{m_mediaKeysMutex};
518 7 : auto mediaKeysHandleIter{m_sessionInfo.find(keySessionId)};
519 7 : if (mediaKeysHandleIter == m_sessionInfo.end())
520 : {
521 3 : RIALTO_SERVER_LOG_ERROR("Media keys handle for mksId: %d does not exists", keySessionId);
522 3 : return false;
523 : }
524 4 : return mediaKeysHandleIter->second.isExtendedInterfaceUsed;
525 7 : }
526 :
527 4 : MediaKeyErrorStatus CdmService::selectKeyId(int32_t keySessionId, const std::vector<uint8_t> &keyId)
528 : {
529 4 : RIALTO_SERVER_LOG_DEBUG("CdmService requested to select key id, key session id: %d", keySessionId);
530 :
531 4 : std::lock_guard<std::mutex> lock{m_mediaKeysMutex};
532 4 : auto mediaKeysHandleIter{m_sessionInfo.find(keySessionId)};
533 4 : if (mediaKeysHandleIter == m_sessionInfo.end())
534 : {
535 2 : RIALTO_SERVER_LOG_ERROR("Media keys handle for mksId: %d does not exists", keySessionId);
536 2 : return MediaKeyErrorStatus::FAIL;
537 : }
538 2 : if (m_sessionInfo.find(keySessionId) != m_sessionInfo.end())
539 : {
540 2 : m_sessionInfo[keySessionId].isExtendedInterfaceUsed = true;
541 : }
542 2 : return m_mediaKeys[mediaKeysHandleIter->second.mediaKeysHandle]->selectKeyId(keySessionId, keyId);
543 4 : }
544 :
545 5 : void CdmService::incrementSessionIdUsageCounter(int32_t keySessionId)
546 : {
547 5 : std::lock_guard<std::mutex> lock{m_mediaKeysMutex};
548 5 : auto mediaKeysHandleIter{m_sessionInfo.find(keySessionId)};
549 5 : if (mediaKeysHandleIter == m_sessionInfo.end())
550 : {
551 1 : RIALTO_SERVER_LOG_ERROR("Media keys handle for mksId: %d does not exists", keySessionId);
552 1 : return;
553 : }
554 4 : ++mediaKeysHandleIter->second.refCounter;
555 5 : }
556 :
557 5 : void CdmService::decrementSessionIdUsageCounter(int32_t keySessionId)
558 : {
559 5 : std::lock_guard<std::mutex> lock{m_mediaKeysMutex};
560 5 : auto mediaKeysHandleIter{m_sessionInfo.find(keySessionId)};
561 5 : if (mediaKeysHandleIter == m_sessionInfo.end())
562 : {
563 1 : RIALTO_SERVER_LOG_ERROR("Media keys handle for mksId: %d does not exists", keySessionId);
564 1 : return;
565 : }
566 4 : if (mediaKeysHandleIter->second.refCounter > 0)
567 : {
568 3 : --mediaKeysHandleIter->second.refCounter;
569 : }
570 4 : if (mediaKeysHandleIter->second.refCounter == 0)
571 : {
572 4 : if (mediaKeysHandleIter->second.shouldBeClosed)
573 : {
574 2 : RIALTO_SERVER_LOG_INFO("Deferred closing of mksId %d", keySessionId);
575 2 : if (MediaKeyErrorStatus::OK !=
576 2 : m_mediaKeys[mediaKeysHandleIter->second.mediaKeysHandle]->closeKeySession(keySessionId))
577 : {
578 1 : RIALTO_SERVER_LOG_ERROR("Failed to close the key session %d", keySessionId);
579 : }
580 : }
581 4 : if (mediaKeysHandleIter->second.shouldBeReleased)
582 : {
583 1 : RIALTO_SERVER_LOG_INFO("Deferred releasing of mksId %d", keySessionId);
584 1 : m_mediaKeys[mediaKeysHandleIter->second.mediaKeysHandle]->releaseKeySession(keySessionId);
585 1 : m_sessionInfo.erase(keySessionId);
586 : }
587 : }
588 5 : }
589 :
590 1 : void CdmService::ping(const std::shared_ptr<IHeartbeatProcedure> &heartbeatProcedure)
591 : {
592 1 : std::lock_guard<std::mutex> lock{m_mediaKeysMutex};
593 2 : for (const auto &mediaKeyPair : m_mediaKeys)
594 : {
595 1 : auto &mediaKeys = mediaKeyPair.second;
596 1 : mediaKeys->ping(heartbeatProcedure->createHandler());
597 : }
598 : }
599 :
600 3 : MediaKeyErrorStatus CdmService::getMetricSystemData(int mediaKeysHandle, std::vector<uint8_t> &buffer)
601 : {
602 3 : RIALTO_SERVER_LOG_DEBUG("CdmService requested to get metric system data: %d", mediaKeysHandle);
603 :
604 3 : std::lock_guard<std::mutex> lock{m_mediaKeysMutex};
605 3 : auto mediaKeysIter = m_mediaKeys.find(mediaKeysHandle);
606 3 : if (mediaKeysIter == m_mediaKeys.end())
607 : {
608 1 : RIALTO_SERVER_LOG_ERROR("Media keys handle: %d does not exists", mediaKeysHandle);
609 1 : return MediaKeyErrorStatus::FAIL;
610 : }
611 2 : return mediaKeysIter->second->getMetricSystemData(buffer);
612 3 : }
613 : } // namespace firebolt::rialto::server::service
|