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(std::shared_ptr<firebolt::rialto::wrappers::IOcdmFactory> ocdmFactory,
95 12 : std::shared_ptr<firebolt::rialto::wrappers::IOcdmSystemFactory> ocdmSystemFactory)
96 12 : : m_ocdmSystemFactory{ocdmSystemFactory}
97 : {
98 12 : RIALTO_SERVER_LOG_DEBUG("entry:");
99 :
100 12 : if (!ocdmFactory)
101 : {
102 1 : throw std::runtime_error("ocdmFactory invalid");
103 : }
104 :
105 11 : m_ocdm = ocdmFactory->getOcdm();
106 11 : if (!m_ocdm)
107 : {
108 1 : throw std::runtime_error("Ocdm could not be fetched");
109 : }
110 16 : }
111 :
112 10 : MediaKeysCapabilities::~MediaKeysCapabilities()
113 : {
114 10 : RIALTO_SERVER_LOG_DEBUG("entry:");
115 : }
116 :
117 1 : std::vector<std::string> MediaKeysCapabilities::getSupportedKeySystems()
118 : {
119 1 : std::vector<std::string> supportedKeySystemVector;
120 4 : for (auto it = kSupportedKeySystems.begin(); it != kSupportedKeySystems.end(); it++)
121 : {
122 3 : MediaKeyErrorStatus status = m_ocdm->isTypeSupported(*it);
123 3 : if (MediaKeyErrorStatus::OK == status)
124 : {
125 2 : supportedKeySystemVector.push_back(*it);
126 : }
127 : }
128 :
129 1 : return supportedKeySystemVector;
130 : }
131 :
132 2 : bool MediaKeysCapabilities::supportsKeySystem(const std::string &keySystem)
133 : {
134 2 : MediaKeyErrorStatus status = m_ocdm->isTypeSupported(keySystem);
135 2 : if (MediaKeyErrorStatus::OK != status)
136 : {
137 1 : return false;
138 : }
139 1 : return true;
140 : }
141 :
142 3 : bool MediaKeysCapabilities::getSupportedKeySystemVersion(const std::string &keySystem, std::string &version)
143 : {
144 : std::shared_ptr<firebolt::rialto::wrappers::IOcdmSystem> ocdmSystem =
145 3 : m_ocdmSystemFactory->createOcdmSystem(keySystem);
146 3 : if (!ocdmSystem)
147 : {
148 1 : RIALTO_SERVER_LOG_ERROR("Failed to create the ocdm system object");
149 1 : version = "";
150 1 : return false;
151 : }
152 :
153 2 : MediaKeyErrorStatus status = ocdmSystem->getVersion(version);
154 2 : if (MediaKeyErrorStatus::OK != status)
155 : {
156 1 : RIALTO_SERVER_LOG_ERROR("Ocdm getVersion failed with status %s", toString(status));
157 1 : version = "";
158 1 : return false;
159 : }
160 1 : return true;
161 3 : }
162 :
163 3 : bool MediaKeysCapabilities::isServerCertificateSupported(const std::string &keySystem)
164 : {
165 : std::shared_ptr<firebolt::rialto::wrappers::IOcdmSystem> ocdmSystem =
166 3 : m_ocdmSystemFactory->createOcdmSystem(keySystem);
167 3 : if (!ocdmSystem)
168 : {
169 1 : RIALTO_SERVER_LOG_ERROR("Failed to create the ocdm system object");
170 1 : return false;
171 : }
172 2 : return ocdmSystem->supportsServerCertificate();
173 3 : }
174 :
175 : }; // namespace firebolt::rialto::server
|