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 "MediaCommon.h"
23 : #include "MediaKeysCapabilities.h"
24 : #include "MediaKeysCommon.h"
25 : #include "RialtoServerLogging.h"
26 :
27 : namespace
28 : {
29 : /**
30 : * @brief Coverts MediaKeyErrorStatus to string.
31 : */
32 1 : const char *toString(const firebolt::rialto::MediaKeyErrorStatus &status)
33 : {
34 1 : switch (status)
35 : {
36 0 : case firebolt::rialto::MediaKeyErrorStatus::OK:
37 0 : return "OK";
38 1 : case firebolt::rialto::MediaKeyErrorStatus::FAIL:
39 1 : return "FAIL";
40 0 : case firebolt::rialto::MediaKeyErrorStatus::BAD_SESSION_ID:
41 0 : return "BAD_SESSION_ID";
42 0 : case firebolt::rialto::MediaKeyErrorStatus::NOT_SUPPORTED:
43 0 : return "NOT_SUPPORTED";
44 0 : case firebolt::rialto::MediaKeyErrorStatus::INVALID_STATE:
45 0 : return "INVALID_STATE";
46 : }
47 0 : return "Unknown";
48 : }
49 : } // namespace
50 :
51 : namespace firebolt::rialto
52 : {
53 1 : std::shared_ptr<IMediaKeysCapabilitiesFactory> IMediaKeysCapabilitiesFactory::createFactory()
54 : {
55 1 : std::shared_ptr<IMediaKeysCapabilitiesFactory> factory;
56 :
57 : try
58 : {
59 1 : factory = std::make_shared<MediaKeysCapabilitiesFactory>();
60 : }
61 0 : catch (const std::exception &e)
62 : {
63 0 : RIALTO_SERVER_LOG_ERROR("Failed to create the media keys capabilities factory, reason: %s", e.what());
64 : }
65 :
66 1 : return factory;
67 : }
68 :
69 1 : std::shared_ptr<IMediaKeysCapabilities> MediaKeysCapabilitiesFactory::getMediaKeysCapabilities() const
70 : {
71 1 : std::shared_ptr<IMediaKeysCapabilities> mediaKeysCapabilities;
72 : try
73 : {
74 : mediaKeysCapabilities =
75 2 : std::make_shared<server::MediaKeysCapabilities>(wrappers::IOcdmFactory::createFactory(),
76 3 : wrappers::IOcdmSystemFactory::createFactory());
77 : }
78 1 : catch (const std::exception &e)
79 : {
80 1 : RIALTO_SERVER_LOG_ERROR("Failed to create the media keys capabilities, reason: %s", e.what());
81 : }
82 :
83 1 : return mediaKeysCapabilities;
84 : }
85 :
86 : }; // namespace firebolt::rialto
87 :
88 : namespace firebolt::rialto::server
89 : {
90 12 : MediaKeysCapabilities::MediaKeysCapabilities(std::shared_ptr<firebolt::rialto::wrappers::IOcdmFactory> ocdmFactory,
91 12 : std::shared_ptr<firebolt::rialto::wrappers::IOcdmSystemFactory> ocdmSystemFactory)
92 12 : : m_ocdmSystemFactory{ocdmSystemFactory}
93 : {
94 12 : RIALTO_SERVER_LOG_DEBUG("entry:");
95 :
96 12 : if (!ocdmFactory)
97 : {
98 1 : throw std::runtime_error("ocdmFactory invalid");
99 : }
100 :
101 11 : m_ocdm = ocdmFactory->getOcdm();
102 11 : if (!m_ocdm)
103 : {
104 1 : throw std::runtime_error("Ocdm could not be fetched");
105 : }
106 16 : }
107 :
108 10 : MediaKeysCapabilities::~MediaKeysCapabilities()
109 : {
110 10 : RIALTO_SERVER_LOG_DEBUG("entry:");
111 : }
112 :
113 1 : std::vector<std::string> MediaKeysCapabilities::getSupportedKeySystems()
114 : {
115 1 : std::vector<std::string> supportedKeySystemVector;
116 4 : for (auto it = kSupportedKeySystems.begin(); it != kSupportedKeySystems.end(); it++)
117 : {
118 3 : MediaKeyErrorStatus status = m_ocdm->isTypeSupported(*it);
119 3 : if (MediaKeyErrorStatus::OK == status)
120 : {
121 2 : supportedKeySystemVector.push_back(*it);
122 : }
123 : }
124 :
125 1 : return supportedKeySystemVector;
126 : }
127 :
128 2 : bool MediaKeysCapabilities::supportsKeySystem(const std::string &keySystem)
129 : {
130 2 : MediaKeyErrorStatus status = m_ocdm->isTypeSupported(keySystem);
131 2 : if (MediaKeyErrorStatus::OK != status)
132 : {
133 1 : return false;
134 : }
135 1 : return true;
136 : }
137 :
138 3 : bool MediaKeysCapabilities::getSupportedKeySystemVersion(const std::string &keySystem, std::string &version)
139 : {
140 : std::shared_ptr<firebolt::rialto::wrappers::IOcdmSystem> ocdmSystem =
141 3 : m_ocdmSystemFactory->createOcdmSystem(keySystem);
142 3 : if (!ocdmSystem)
143 : {
144 1 : RIALTO_SERVER_LOG_ERROR("Failed to create the ocdm system object");
145 1 : version = "";
146 1 : return false;
147 : }
148 :
149 2 : MediaKeyErrorStatus status = ocdmSystem->getVersion(version);
150 2 : if (MediaKeyErrorStatus::OK != status)
151 : {
152 1 : RIALTO_SERVER_LOG_ERROR("Ocdm getVersion failed with status %s", toString(status));
153 1 : version = "";
154 1 : return false;
155 : }
156 1 : return true;
157 3 : }
158 :
159 3 : bool MediaKeysCapabilities::isServerCertificateSupported(const std::string &keySystem)
160 : {
161 : std::shared_ptr<firebolt::rialto::wrappers::IOcdmSystem> ocdmSystem =
162 3 : m_ocdmSystemFactory->createOcdmSystem(keySystem);
163 3 : if (!ocdmSystem)
164 : {
165 1 : RIALTO_SERVER_LOG_ERROR("Failed to create the ocdm system object");
166 1 : return false;
167 : }
168 2 : return ocdmSystem->supportsServerCertificate();
169 3 : }
170 :
171 : }; // namespace firebolt::rialto::server
|