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