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