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 <stdexcept>
21 :
22 : #include "MediaKeysServerInternal.h"
23 : #include "RialtoServerLogging.h"
24 :
25 : namespace firebolt::rialto
26 : {
27 1 : std::shared_ptr<IMediaKeysFactory> IMediaKeysFactory::createFactory()
28 : {
29 1 : return server::IMediaKeysServerInternalFactory::createFactory();
30 : }
31 : } // namespace firebolt::rialto
32 :
33 : namespace firebolt::rialto::server
34 : {
35 64 : int32_t generateSessionId()
36 : {
37 : static int32_t keySessionId{0};
38 64 : return keySessionId++;
39 : }
40 :
41 2 : std::shared_ptr<IMediaKeysServerInternalFactory> IMediaKeysServerInternalFactory::createFactory()
42 : {
43 2 : std::shared_ptr<IMediaKeysServerInternalFactory> factory;
44 :
45 : try
46 : {
47 2 : factory = std::make_shared<MediaKeysServerInternalFactory>();
48 : }
49 0 : catch (const std::exception &e)
50 : {
51 0 : RIALTO_SERVER_LOG_ERROR("Failed to create the media keys factory, reason: %s", e.what());
52 : }
53 :
54 2 : return factory;
55 : }
56 :
57 1 : std::unique_ptr<IMediaKeys> MediaKeysServerInternalFactory::createMediaKeys(const std::string &keySystem) const
58 : {
59 1 : RIALTO_SERVER_LOG_ERROR("This function can't be used by rialto server. Please use createMediaKeysServerInternal");
60 1 : return nullptr;
61 : }
62 :
63 : std::unique_ptr<IMediaKeysServerInternal>
64 1 : MediaKeysServerInternalFactory::createMediaKeysServerInternal(const std::string &keySystem) const
65 : {
66 1 : std::unique_ptr<IMediaKeysServerInternal> mediaKeys;
67 : try
68 : {
69 1 : mediaKeys = std::make_unique<server::MediaKeysServerInternal>(keySystem,
70 3 : server::IMainThreadFactory::createFactory(),
71 3 : wrappers::IOcdmSystemFactory::createFactory(),
72 3 : server::IMediaKeySessionFactory::createFactory());
73 : }
74 1 : catch (const std::exception &e)
75 : {
76 1 : RIALTO_SERVER_LOG_ERROR("Failed to create the media keys, reason: %s", e.what());
77 : }
78 :
79 1 : return mediaKeys;
80 : }
81 : }; // namespace firebolt::rialto::server
82 :
83 : namespace firebolt::rialto::server
84 : {
85 69 : MediaKeysServerInternal::MediaKeysServerInternal(
86 : const std::string &keySystem, const std::shared_ptr<IMainThreadFactory> &mainThreadFactory,
87 : std::shared_ptr<firebolt::rialto::wrappers::IOcdmSystemFactory> ocdmSystemFactory,
88 69 : std::shared_ptr<IMediaKeySessionFactory> mediaKeySessionFactory)
89 69 : : m_mediaKeySessionFactory(mediaKeySessionFactory), m_keySystem(keySystem)
90 : {
91 69 : RIALTO_SERVER_LOG_DEBUG("entry:");
92 :
93 69 : m_mainThread = mainThreadFactory->getMainThread();
94 69 : if (!m_mainThread)
95 : {
96 1 : throw std::runtime_error("Failed to get the main thread");
97 : }
98 68 : m_mainThreadClientId = m_mainThread->registerClient();
99 :
100 68 : if (!ocdmSystemFactory)
101 : {
102 1 : throw std::runtime_error("No ocdmSystemFactory");
103 : }
104 :
105 67 : bool result = false;
106 67 : auto task = [&]()
107 : {
108 67 : m_ocdmSystem = ocdmSystemFactory->createOcdmSystem(keySystem);
109 67 : if (!m_ocdmSystem)
110 : {
111 1 : RIALTO_SERVER_LOG_ERROR("Ocdm system could not be created");
112 : }
113 : else
114 : {
115 66 : result = true;
116 : }
117 134 : };
118 :
119 67 : m_mainThread->enqueueTaskAndWait(m_mainThreadClientId, task);
120 67 : if (!result)
121 : {
122 1 : throw std::runtime_error("MediaKeys construction failed");
123 : }
124 84 : }
125 :
126 198 : MediaKeysServerInternal::~MediaKeysServerInternal()
127 : {
128 66 : RIALTO_SERVER_LOG_DEBUG("entry:");
129 :
130 66 : auto task = [&]()
131 : {
132 66 : m_ocdmSystem.reset();
133 :
134 66 : m_mainThread->unregisterClient(m_mainThreadClientId);
135 132 : };
136 :
137 66 : m_mainThread->enqueueTaskAndWait(m_mainThreadClientId, task);
138 132 : }
139 :
140 3 : MediaKeyErrorStatus MediaKeysServerInternal::selectKeyId(int32_t keySessionId, const std::vector<uint8_t> &keyId)
141 : {
142 3 : RIALTO_SERVER_LOG_DEBUG("entry:");
143 :
144 : MediaKeyErrorStatus status;
145 3 : auto task = [&]() { status = selectKeyIdInternal(keySessionId, keyId); };
146 :
147 3 : m_mainThread->enqueueTaskAndWait(m_mainThreadClientId, task);
148 3 : return status;
149 : }
150 :
151 3 : MediaKeyErrorStatus MediaKeysServerInternal::selectKeyIdInternal(int32_t keySessionId, const std::vector<uint8_t> &keyId)
152 : {
153 3 : auto sessionIter = m_mediaKeySessions.find(keySessionId);
154 3 : if (sessionIter == m_mediaKeySessions.end())
155 : {
156 1 : RIALTO_SERVER_LOG_ERROR("Failed to find the session %d", keySessionId);
157 1 : return MediaKeyErrorStatus::BAD_SESSION_ID;
158 : }
159 :
160 2 : MediaKeyErrorStatus status = sessionIter->second.mediaKeySession->selectKeyId(keyId);
161 2 : if (MediaKeyErrorStatus::OK != status)
162 : {
163 1 : RIALTO_SERVER_LOG_ERROR("Failed to select key id");
164 1 : return status;
165 : }
166 :
167 1 : return status;
168 : }
169 :
170 3 : bool MediaKeysServerInternal::containsKey(int32_t keySessionId, const std::vector<uint8_t> &keyId)
171 : {
172 3 : RIALTO_SERVER_LOG_DEBUG("entry:");
173 :
174 3 : bool result{false};
175 3 : auto task = [&]() { result = containsKeyInternal(keySessionId, keyId); };
176 :
177 3 : m_mainThread->enqueueTaskAndWait(m_mainThreadClientId, task);
178 3 : return result;
179 : }
180 :
181 3 : bool MediaKeysServerInternal::containsKeyInternal(int32_t keySessionId, const std::vector<uint8_t> &keyId)
182 : {
183 3 : auto sessionIter = m_mediaKeySessions.find(keySessionId);
184 3 : if (sessionIter == m_mediaKeySessions.end())
185 : {
186 1 : RIALTO_SERVER_LOG_ERROR("Failed to find the session %d", keySessionId);
187 1 : return false;
188 : }
189 :
190 2 : return sessionIter->second.mediaKeySession->containsKey(keyId);
191 : }
192 :
193 64 : MediaKeyErrorStatus MediaKeysServerInternal::createKeySession(KeySessionType sessionType,
194 : std::weak_ptr<IMediaKeysClient> client, bool isLDL,
195 : int32_t &keySessionId)
196 : {
197 64 : RIALTO_SERVER_LOG_DEBUG("entry:");
198 :
199 : MediaKeyErrorStatus status;
200 64 : auto task = [&]() { status = createKeySessionInternal(sessionType, client, isLDL, keySessionId); };
201 :
202 64 : m_mainThread->enqueueTaskAndWait(m_mainThreadClientId, task);
203 64 : return status;
204 : }
205 :
206 64 : MediaKeyErrorStatus MediaKeysServerInternal::createKeySessionInternal(KeySessionType sessionType,
207 : std::weak_ptr<IMediaKeysClient> client,
208 : bool isLDL, int32_t &keySessionId)
209 : {
210 64 : int32_t keySessionIdTemp = generateSessionId();
211 : std::unique_ptr<IMediaKeySession> mediaKeySession =
212 128 : m_mediaKeySessionFactory->createMediaKeySession(m_keySystem, keySessionIdTemp, *m_ocdmSystem, sessionType,
213 128 : client, isLDL);
214 64 : if (!mediaKeySession)
215 : {
216 1 : RIALTO_SERVER_LOG_ERROR("Failed to create a new media key session");
217 1 : return MediaKeyErrorStatus::FAIL;
218 : }
219 63 : keySessionId = keySessionIdTemp;
220 63 : m_mediaKeySessions.emplace(std::make_pair(keySessionId, MediaKeySessionUsage{std::move(mediaKeySession)}));
221 :
222 63 : return MediaKeyErrorStatus::OK;
223 64 : }
224 :
225 3 : MediaKeyErrorStatus MediaKeysServerInternal::generateRequest(int32_t keySessionId, InitDataType initDataType,
226 : const std::vector<uint8_t> &initData)
227 : {
228 3 : RIALTO_SERVER_LOG_DEBUG("entry:");
229 :
230 : MediaKeyErrorStatus status;
231 3 : auto task = [&]() { status = generateRequestInternal(keySessionId, initDataType, initData); };
232 :
233 3 : m_mainThread->enqueueTaskAndWait(m_mainThreadClientId, task);
234 3 : return status;
235 : }
236 :
237 3 : MediaKeyErrorStatus MediaKeysServerInternal::generateRequestInternal(int32_t keySessionId, InitDataType initDataType,
238 : const std::vector<uint8_t> &initData)
239 : {
240 3 : auto sessionIter = m_mediaKeySessions.find(keySessionId);
241 3 : if (sessionIter == m_mediaKeySessions.end())
242 : {
243 1 : RIALTO_SERVER_LOG_ERROR("Failed to find the session %d", keySessionId);
244 1 : return MediaKeyErrorStatus::BAD_SESSION_ID;
245 : }
246 :
247 2 : MediaKeyErrorStatus status = sessionIter->second.mediaKeySession->generateRequest(initDataType, initData);
248 2 : if (MediaKeyErrorStatus::OK != status)
249 : {
250 1 : RIALTO_SERVER_LOG_ERROR("Failed to generate request for the key session %d", keySessionId);
251 1 : return status;
252 : }
253 1 : return status;
254 : }
255 :
256 3 : MediaKeyErrorStatus MediaKeysServerInternal::loadSession(int32_t keySessionId)
257 : {
258 3 : RIALTO_SERVER_LOG_DEBUG("entry:");
259 :
260 : MediaKeyErrorStatus status;
261 3 : auto task = [&]() { status = loadSessionInternal(keySessionId); };
262 :
263 3 : m_mainThread->enqueueTaskAndWait(m_mainThreadClientId, task);
264 3 : return status;
265 : }
266 :
267 3 : MediaKeyErrorStatus MediaKeysServerInternal::loadSessionInternal(int32_t keySessionId)
268 : {
269 3 : auto sessionIter = m_mediaKeySessions.find(keySessionId);
270 3 : if (sessionIter == m_mediaKeySessions.end())
271 : {
272 1 : RIALTO_SERVER_LOG_ERROR("Failed to find the session %d", keySessionId);
273 1 : return MediaKeyErrorStatus::BAD_SESSION_ID;
274 : }
275 :
276 2 : MediaKeyErrorStatus status = sessionIter->second.mediaKeySession->loadSession();
277 2 : if (MediaKeyErrorStatus::OK != status)
278 : {
279 1 : RIALTO_SERVER_LOG_ERROR("Failed to load the session %d", keySessionId);
280 1 : return status;
281 : }
282 1 : return status;
283 : }
284 :
285 3 : MediaKeyErrorStatus MediaKeysServerInternal::updateSession(int32_t keySessionId, const std::vector<uint8_t> &responseData)
286 : {
287 3 : RIALTO_SERVER_LOG_DEBUG("entry:");
288 :
289 : MediaKeyErrorStatus status;
290 3 : auto task = [&]() { status = updateSessionInternal(keySessionId, responseData); };
291 :
292 3 : m_mainThread->enqueueTaskAndWait(m_mainThreadClientId, task);
293 3 : return status;
294 : }
295 :
296 3 : MediaKeyErrorStatus MediaKeysServerInternal::updateSessionInternal(int32_t keySessionId,
297 : const std::vector<uint8_t> &responseData)
298 : {
299 3 : auto sessionIter = m_mediaKeySessions.find(keySessionId);
300 3 : if (sessionIter == m_mediaKeySessions.end())
301 : {
302 1 : RIALTO_SERVER_LOG_ERROR("Failed to find the session %d", keySessionId);
303 1 : return MediaKeyErrorStatus::BAD_SESSION_ID;
304 : }
305 :
306 2 : MediaKeyErrorStatus status = sessionIter->second.mediaKeySession->updateSession(responseData);
307 2 : if (MediaKeyErrorStatus::OK != status)
308 : {
309 1 : RIALTO_SERVER_LOG_ERROR("Failed to update the session %d", keySessionId);
310 1 : return status;
311 : }
312 1 : return status;
313 : }
314 :
315 3 : MediaKeyErrorStatus MediaKeysServerInternal::setDrmHeader(int32_t keySessionId, const std::vector<uint8_t> &requestData)
316 : {
317 3 : RIALTO_SERVER_LOG_DEBUG("entry:");
318 :
319 : MediaKeyErrorStatus status;
320 3 : auto task = [&]() { status = setDrmHeaderInternal(keySessionId, requestData); };
321 :
322 3 : m_mainThread->enqueueTaskAndWait(m_mainThreadClientId, task);
323 3 : return status;
324 : }
325 :
326 3 : MediaKeyErrorStatus MediaKeysServerInternal::setDrmHeaderInternal(int32_t keySessionId,
327 : const std::vector<uint8_t> &requestData)
328 : {
329 3 : auto sessionIter = m_mediaKeySessions.find(keySessionId);
330 3 : if (sessionIter == m_mediaKeySessions.end())
331 : {
332 1 : RIALTO_SERVER_LOG_ERROR("Failed to find the session %d", keySessionId);
333 1 : return MediaKeyErrorStatus::BAD_SESSION_ID;
334 : }
335 :
336 2 : MediaKeyErrorStatus status = sessionIter->second.mediaKeySession->setDrmHeader(requestData);
337 2 : if (MediaKeyErrorStatus::OK != status)
338 : {
339 1 : RIALTO_SERVER_LOG_ERROR("Failed to set drm header");
340 1 : return status;
341 : }
342 :
343 1 : return status;
344 : }
345 :
346 8 : MediaKeyErrorStatus MediaKeysServerInternal::closeKeySession(int32_t keySessionId)
347 : {
348 8 : RIALTO_SERVER_LOG_DEBUG("entry:");
349 :
350 : MediaKeyErrorStatus status;
351 8 : auto task = [&]() { status = closeKeySessionInternal(keySessionId); };
352 :
353 8 : m_mainThread->enqueueTaskAndWait(m_mainThreadClientId, task);
354 8 : return status;
355 : }
356 :
357 8 : MediaKeyErrorStatus MediaKeysServerInternal::closeKeySessionInternal(int32_t keySessionId)
358 : {
359 8 : auto sessionIter = m_mediaKeySessions.find(keySessionId);
360 8 : if (sessionIter == m_mediaKeySessions.end())
361 : {
362 1 : RIALTO_SERVER_LOG_ERROR("Failed to find the session %d", keySessionId);
363 1 : return MediaKeyErrorStatus::BAD_SESSION_ID;
364 : }
365 :
366 7 : if (sessionIter->second.bufCounter == 0)
367 : {
368 3 : MediaKeyErrorStatus status = sessionIter->second.mediaKeySession->closeKeySession();
369 3 : if (MediaKeyErrorStatus::OK != status)
370 : {
371 1 : RIALTO_SERVER_LOG_ERROR("Failed to close the key session %d", keySessionId);
372 1 : return status;
373 : }
374 2 : return status;
375 : }
376 :
377 4 : RIALTO_SERVER_LOG_INFO("Deferring closing of key session %d", keySessionId);
378 4 : sessionIter->second.shouldBeClosed = true;
379 4 : return MediaKeyErrorStatus::OK;
380 : }
381 :
382 11 : MediaKeyErrorStatus MediaKeysServerInternal::removeKeySession(int32_t keySessionId)
383 : {
384 11 : RIALTO_SERVER_LOG_DEBUG("entry:");
385 :
386 : MediaKeyErrorStatus status;
387 11 : auto task = [&]() { status = removeKeySessionInternal(keySessionId); };
388 :
389 11 : m_mainThread->enqueueTaskAndWait(m_mainThreadClientId, task);
390 11 : return status;
391 : }
392 :
393 11 : MediaKeyErrorStatus MediaKeysServerInternal::removeKeySessionInternal(int32_t keySessionId)
394 : {
395 11 : auto sessionIter = m_mediaKeySessions.find(keySessionId);
396 11 : if (sessionIter == m_mediaKeySessions.end())
397 : {
398 4 : RIALTO_SERVER_LOG_ERROR("Failed to find the session %d", keySessionId);
399 4 : return MediaKeyErrorStatus::BAD_SESSION_ID;
400 : }
401 :
402 7 : MediaKeyErrorStatus status = sessionIter->second.mediaKeySession->removeKeySession();
403 7 : if (MediaKeyErrorStatus::OK != status)
404 : {
405 1 : RIALTO_SERVER_LOG_ERROR("Failed to remove the key session %d", keySessionId);
406 1 : return status;
407 : }
408 6 : return status;
409 : }
410 :
411 2 : MediaKeyErrorStatus MediaKeysServerInternal::deleteDrmStore()
412 : {
413 2 : RIALTO_SERVER_LOG_DEBUG("entry:");
414 :
415 : MediaKeyErrorStatus status;
416 2 : auto task = [&]() { status = m_ocdmSystem->deleteSecureStore(); };
417 :
418 2 : m_mainThread->enqueueTaskAndWait(m_mainThreadClientId, task);
419 2 : return status;
420 : }
421 :
422 2 : MediaKeyErrorStatus MediaKeysServerInternal::deleteKeyStore()
423 : {
424 2 : RIALTO_SERVER_LOG_DEBUG("entry:");
425 :
426 : MediaKeyErrorStatus status;
427 2 : auto task = [&]() { status = m_ocdmSystem->deleteKeyStore(); };
428 :
429 2 : m_mainThread->enqueueTaskAndWait(m_mainThreadClientId, task);
430 2 : return status;
431 : }
432 :
433 2 : MediaKeyErrorStatus MediaKeysServerInternal::getDrmStoreHash(std::vector<unsigned char> &drmStoreHash)
434 : {
435 2 : RIALTO_SERVER_LOG_DEBUG("entry:");
436 2 : constexpr size_t kHashSize{256};
437 2 : drmStoreHash.resize(kHashSize);
438 : MediaKeyErrorStatus status;
439 2 : auto task = [&]() { status = m_ocdmSystem->getSecureStoreHash(&drmStoreHash[0], kHashSize); };
440 2 : m_mainThread->enqueueTaskAndWait(m_mainThreadClientId, task);
441 2 : return status;
442 : }
443 :
444 2 : MediaKeyErrorStatus MediaKeysServerInternal::getKeyStoreHash(std::vector<unsigned char> &keyStoreHash)
445 : {
446 2 : RIALTO_SERVER_LOG_DEBUG("entry:");
447 2 : constexpr size_t kHashSize{256};
448 2 : keyStoreHash.resize(kHashSize);
449 : MediaKeyErrorStatus status;
450 2 : auto task = [&]() { status = m_ocdmSystem->getKeyStoreHash(&keyStoreHash[0], kHashSize); };
451 2 : m_mainThread->enqueueTaskAndWait(m_mainThreadClientId, task);
452 2 : return status;
453 : }
454 :
455 2 : MediaKeyErrorStatus MediaKeysServerInternal::getLdlSessionsLimit(uint32_t &ldlLimit)
456 : {
457 2 : RIALTO_SERVER_LOG_DEBUG("entry:");
458 :
459 : MediaKeyErrorStatus status;
460 2 : auto task = [&]() { status = m_ocdmSystem->getLdlSessionsLimit(&ldlLimit); };
461 :
462 2 : m_mainThread->enqueueTaskAndWait(m_mainThreadClientId, task);
463 2 : return status;
464 : }
465 :
466 3 : MediaKeyErrorStatus MediaKeysServerInternal::getLastDrmError(int32_t keySessionId, uint32_t &errorCode)
467 : {
468 3 : RIALTO_SERVER_LOG_DEBUG("entry:");
469 :
470 : MediaKeyErrorStatus status;
471 3 : auto task = [&]() { status = getLastDrmErrorInternal(keySessionId, errorCode); };
472 :
473 3 : m_mainThread->enqueueTaskAndWait(m_mainThreadClientId, task);
474 3 : return status;
475 : }
476 :
477 3 : MediaKeyErrorStatus MediaKeysServerInternal::getLastDrmErrorInternal(int32_t keySessionId, uint32_t &errorCode)
478 : {
479 3 : auto sessionIter = m_mediaKeySessions.find(keySessionId);
480 3 : if (sessionIter == m_mediaKeySessions.end())
481 : {
482 1 : RIALTO_SERVER_LOG_ERROR("Failed to find the session %d", keySessionId);
483 1 : return MediaKeyErrorStatus::BAD_SESSION_ID;
484 : }
485 :
486 2 : MediaKeyErrorStatus status = sessionIter->second.mediaKeySession->getLastDrmError(errorCode);
487 2 : if (MediaKeyErrorStatus::OK != status)
488 : {
489 1 : RIALTO_SERVER_LOG_ERROR("Failed to get last drm error");
490 : }
491 :
492 2 : return status;
493 : }
494 :
495 2 : MediaKeyErrorStatus MediaKeysServerInternal::getDrmTime(uint64_t &drmTime)
496 : {
497 : MediaKeyErrorStatus status;
498 2 : auto task = [&]() { status = m_ocdmSystem->getDrmTime(&drmTime); };
499 :
500 2 : m_mainThread->enqueueTaskAndWait(m_mainThreadClientId, task);
501 2 : return status;
502 : }
503 :
504 3 : MediaKeyErrorStatus MediaKeysServerInternal::getCdmKeySessionId(int32_t keySessionId, std::string &cdmKeySessionId)
505 : {
506 3 : RIALTO_SERVER_LOG_DEBUG("entry:");
507 :
508 : MediaKeyErrorStatus status;
509 3 : auto task = [&]() { status = getCdmKeySessionIdInternal(keySessionId, cdmKeySessionId); };
510 :
511 3 : m_mainThread->enqueueTaskAndWait(m_mainThreadClientId, task);
512 3 : return status;
513 : }
514 :
515 7 : MediaKeyErrorStatus MediaKeysServerInternal::releaseKeySession(int32_t keySessionId)
516 : {
517 7 : RIALTO_SERVER_LOG_DEBUG("entry:");
518 :
519 : MediaKeyErrorStatus status;
520 7 : auto task = [&]() { status = releaseKeySessionInternal(keySessionId); };
521 :
522 7 : m_mainThread->enqueueTaskAndWait(m_mainThreadClientId, task);
523 7 : return status;
524 : }
525 :
526 7 : MediaKeyErrorStatus MediaKeysServerInternal::releaseKeySessionInternal(int32_t keySessionId)
527 : {
528 7 : auto sessionIter = m_mediaKeySessions.find(keySessionId);
529 7 : if (sessionIter == m_mediaKeySessions.end())
530 : {
531 1 : RIALTO_SERVER_LOG_ERROR("Failed to find the session %d", keySessionId);
532 1 : return MediaKeyErrorStatus::BAD_SESSION_ID;
533 : }
534 :
535 6 : if (sessionIter->second.bufCounter == 0)
536 : {
537 2 : m_mediaKeySessions.erase(sessionIter);
538 2 : return MediaKeyErrorStatus::OK;
539 : }
540 :
541 4 : RIALTO_SERVER_LOG_INFO("Deferring releasing of key session %d", keySessionId);
542 4 : sessionIter->second.shouldBeReleased = true;
543 4 : return MediaKeyErrorStatus::OK;
544 : }
545 :
546 3 : MediaKeyErrorStatus MediaKeysServerInternal::getCdmKeySessionIdInternal(int32_t keySessionId, std::string &cdmKeySessionId)
547 : {
548 3 : auto sessionIter = m_mediaKeySessions.find(keySessionId);
549 3 : if (sessionIter == m_mediaKeySessions.end())
550 : {
551 1 : RIALTO_SERVER_LOG_ERROR("Failed to find the session %d", keySessionId);
552 1 : return MediaKeyErrorStatus::BAD_SESSION_ID;
553 : }
554 :
555 2 : MediaKeyErrorStatus status = sessionIter->second.mediaKeySession->getCdmKeySessionId(cdmKeySessionId);
556 2 : if (MediaKeyErrorStatus::OK != status)
557 : {
558 1 : RIALTO_SERVER_LOG_ERROR("Failed to get cdm key session id");
559 1 : return status;
560 : }
561 :
562 1 : return status;
563 : }
564 :
565 3 : MediaKeyErrorStatus MediaKeysServerInternal::decrypt(int32_t keySessionId, GstBuffer *encrypted, GstCaps *caps)
566 : {
567 3 : RIALTO_SERVER_LOG_DEBUG("entry:");
568 :
569 : MediaKeyErrorStatus status;
570 3 : auto task = [&]() { status = decryptInternal(keySessionId, encrypted, caps); };
571 :
572 3 : m_mainThread->enqueueTaskAndWait(m_mainThreadClientId, task);
573 3 : return status;
574 : }
575 :
576 3 : MediaKeyErrorStatus MediaKeysServerInternal::decryptInternal(int32_t keySessionId, GstBuffer *encrypted, GstCaps *caps)
577 : {
578 3 : auto sessionIter = m_mediaKeySessions.find(keySessionId);
579 3 : if (sessionIter == m_mediaKeySessions.end())
580 : {
581 1 : RIALTO_SERVER_LOG_ERROR("Failed to find the session %d", keySessionId);
582 1 : return MediaKeyErrorStatus::BAD_SESSION_ID;
583 : }
584 :
585 2 : MediaKeyErrorStatus status = sessionIter->second.mediaKeySession->decrypt(encrypted, caps);
586 2 : if (MediaKeyErrorStatus::OK != status)
587 : {
588 1 : RIALTO_SERVER_LOG_ERROR("Failed to decrypt buffer.");
589 1 : return status;
590 : }
591 :
592 1 : return status;
593 : }
594 :
595 1 : bool MediaKeysServerInternal::hasSession(int32_t keySessionId) const
596 : {
597 1 : RIALTO_SERVER_LOG_DEBUG("entry:");
598 :
599 : bool result;
600 1 : auto task = [&]() { result = m_mediaKeySessions.find(keySessionId) != m_mediaKeySessions.end(); };
601 :
602 1 : m_mainThread->enqueueTaskAndWait(m_mainThreadClientId, task);
603 1 : return result;
604 : }
605 :
606 3 : bool MediaKeysServerInternal::isNetflixPlayreadyKeySystem(int32_t keySessionId) const
607 : {
608 3 : RIALTO_SERVER_LOG_DEBUG("entry:");
609 : bool result;
610 3 : auto task = [&]() { result = isNetflixPlayreadyKeySystemInternal(keySessionId); };
611 :
612 3 : m_mainThread->enqueueTaskAndWait(m_mainThreadClientId, task);
613 3 : return result;
614 : }
615 :
616 3 : bool MediaKeysServerInternal::isNetflixPlayreadyKeySystemInternal(int32_t keySessionId) const
617 : {
618 3 : auto sessionIter = m_mediaKeySessions.find(keySessionId);
619 3 : if (sessionIter == m_mediaKeySessions.end())
620 : {
621 1 : RIALTO_SERVER_LOG_ERROR("Failed to find the session %d", keySessionId);
622 1 : return false;
623 : }
624 2 : return sessionIter->second.mediaKeySession->isNetflixPlayreadyKeySystem();
625 : }
626 :
627 12 : void MediaKeysServerInternal::incrementSessionIdUsageCounter(int32_t keySessionId)
628 : {
629 12 : RIALTO_SERVER_LOG_DEBUG("entry:");
630 12 : auto task = [&]() { incrementSessionIdUsageCounterInternal(keySessionId); };
631 :
632 12 : m_mainThread->enqueueTaskAndWait(m_mainThreadClientId, task);
633 : }
634 :
635 12 : void MediaKeysServerInternal::incrementSessionIdUsageCounterInternal(int32_t keySessionId)
636 : {
637 12 : auto sessionIter = m_mediaKeySessions.find(keySessionId);
638 12 : if (sessionIter == m_mediaKeySessions.end())
639 : {
640 2 : RIALTO_SERVER_LOG_ERROR("Failed to find the session %d", keySessionId);
641 2 : return;
642 : }
643 :
644 10 : sessionIter->second.bufCounter++;
645 : }
646 :
647 8 : void MediaKeysServerInternal::decrementSessionIdUsageCounter(int32_t keySessionId)
648 : {
649 8 : RIALTO_SERVER_LOG_DEBUG("entry:");
650 8 : auto task = [&]() { decrementSessionIdUsageCounterInternal(keySessionId); };
651 :
652 8 : m_mainThread->enqueueTaskAndWait(m_mainThreadClientId, task);
653 : }
654 :
655 8 : void MediaKeysServerInternal::decrementSessionIdUsageCounterInternal(int32_t keySessionId)
656 : {
657 8 : auto sessionIter = m_mediaKeySessions.find(keySessionId);
658 8 : if (sessionIter == m_mediaKeySessions.end())
659 : {
660 2 : RIALTO_SERVER_LOG_ERROR("Failed to find the session %d", keySessionId);
661 2 : return;
662 : }
663 :
664 6 : if (sessionIter->second.bufCounter > 0)
665 : {
666 4 : sessionIter->second.bufCounter--;
667 : }
668 :
669 6 : if (sessionIter->second.bufCounter == 0)
670 : {
671 4 : if (sessionIter->second.shouldBeClosed)
672 : {
673 1 : RIALTO_SERVER_LOG_INFO("Deferred closing of mksId %d", keySessionId);
674 1 : MediaKeyErrorStatus status = sessionIter->second.mediaKeySession->closeKeySession();
675 1 : if (MediaKeyErrorStatus::OK != status)
676 : {
677 0 : RIALTO_SERVER_LOG_ERROR("Failed to close the key session %d", keySessionId);
678 : }
679 : }
680 4 : if (sessionIter->second.shouldBeReleased)
681 : {
682 1 : m_mediaKeySessions.erase(sessionIter);
683 : }
684 : }
685 : }
686 :
687 1 : void MediaKeysServerInternal::ping(std::unique_ptr<IHeartbeatHandler> &&heartbeatHandler)
688 : {
689 1 : RIALTO_SERVER_LOG_DEBUG("entry:");
690 1 : auto task = [&]() { heartbeatHandler.reset(); };
691 :
692 1 : m_mainThread->enqueueTaskAndWait(m_mainThreadClientId, task);
693 : }
694 : }; // namespace firebolt::rialto::server
|