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 : #ifndef FIREBOLT_RIALTO_SERVER_MEDIA_KEYS_SERVER_INTERNAL_H_
21 : #define FIREBOLT_RIALTO_SERVER_MEDIA_KEYS_SERVER_INTERNAL_H_
22 :
23 : #include "IMainThread.h"
24 : #include "IMediaKeySession.h"
25 : #include "IMediaKeysServerInternal.h"
26 : #include "IOcdmSystem.h"
27 : #include <map>
28 : #include <memory>
29 : #include <string>
30 : #include <vector>
31 :
32 : namespace firebolt::rialto::server
33 : {
34 : /**
35 : * @brief IMediaKeys factory class definition.
36 : */
37 : class MediaKeysServerInternalFactory : public IMediaKeysServerInternalFactory
38 : {
39 : public:
40 2 : MediaKeysServerInternalFactory() = default;
41 2 : ~MediaKeysServerInternalFactory() override = default;
42 :
43 : std::unique_ptr<IMediaKeys> createMediaKeys(const std::string &keySystem) const override;
44 : std::unique_ptr<IMediaKeysServerInternal> createMediaKeysServerInternal(const std::string &keySystem) const override;
45 : };
46 :
47 : }; // namespace firebolt::rialto::server
48 :
49 : namespace firebolt::rialto::server
50 : {
51 : /**
52 : * @brief The definition of the MediaKeysServerInternal.
53 : */
54 : class MediaKeysServerInternal : public IMediaKeysServerInternal
55 : {
56 : public:
57 : /**
58 : * @brief The constructor.
59 : *
60 : * @param[in] keySystem : The key system for which to create a Media Keys instance.
61 : * @param[in] mainThreadFactory : The main thread factory.
62 : * @param[in] ocdmSystemFactory : The ocdm system factory.
63 : * @param[in] mediaKeySessionFactory : The media key session factory.
64 : *
65 : */
66 : MediaKeysServerInternal(const std::string &keySystem, const std::shared_ptr<IMainThreadFactory> &mainThreadFactory,
67 : std::shared_ptr<firebolt::rialto::wrappers::IOcdmSystemFactory> ocdmSystemFactory,
68 : std::shared_ptr<IMediaKeySessionFactory> mediaKeySessionFactory);
69 :
70 : /**
71 : * @brief Virtual destructor.
72 : */
73 : virtual ~MediaKeysServerInternal();
74 :
75 : MediaKeyErrorStatus selectKeyId(int32_t keySessionId, const std::vector<uint8_t> &keyId) override;
76 :
77 : bool containsKey(int32_t keySessionId, const std::vector<uint8_t> &keyId) override;
78 :
79 : MediaKeyErrorStatus createKeySession(KeySessionType sessionType, std::weak_ptr<IMediaKeysClient> client,
80 : int32_t &keySessionId) override;
81 :
82 : MediaKeyErrorStatus generateRequest(int32_t keySessionId, InitDataType initDataType,
83 : const std::vector<uint8_t> &initData,
84 : const LimitedDurationLicense &ldlState) override;
85 :
86 : MediaKeyErrorStatus loadSession(int32_t keySessionId) override;
87 :
88 : MediaKeyErrorStatus updateSession(int32_t keySessionId, const std::vector<uint8_t> &responseData) override;
89 :
90 : MediaKeyErrorStatus setDrmHeader(int32_t keySessionId, const std::vector<uint8_t> &requestData) override;
91 :
92 : MediaKeyErrorStatus closeKeySession(int32_t keySessionId) override;
93 :
94 : MediaKeyErrorStatus removeKeySession(int32_t keySessionId) override;
95 :
96 : MediaKeyErrorStatus deleteDrmStore() override;
97 :
98 : MediaKeyErrorStatus deleteKeyStore() override;
99 :
100 : MediaKeyErrorStatus getDrmStoreHash(std::vector<unsigned char> &drmStoreHash) override;
101 :
102 : MediaKeyErrorStatus getKeyStoreHash(std::vector<unsigned char> &keyStoreHash) override;
103 :
104 : MediaKeyErrorStatus getLdlSessionsLimit(uint32_t &ldlLimit) override;
105 :
106 : MediaKeyErrorStatus getLastDrmError(int32_t keySessionId, uint32_t &errorCode) override;
107 :
108 : MediaKeyErrorStatus getDrmTime(uint64_t &drmTime) override;
109 :
110 : MediaKeyErrorStatus getCdmKeySessionId(int32_t keySessionId, std::string &cdmKeySessionId) override;
111 :
112 : MediaKeyErrorStatus releaseKeySession(int32_t keySessionId) override;
113 :
114 : MediaKeyErrorStatus decrypt(int32_t keySessionId, GstBuffer *encrypted, GstCaps *caps) override;
115 :
116 : MediaKeyErrorStatus getMetricSystemData(std::vector<uint8_t> &buffer) override;
117 :
118 : void ping(std::unique_ptr<IHeartbeatHandler> &&heartbeatHandler) override;
119 :
120 : private:
121 : /**
122 : * @brief The mainThread object.
123 : */
124 : std::shared_ptr<IMainThread> m_mainThread;
125 :
126 : /**
127 : * @brief The factory for creating MediaKeySessions.
128 : */
129 : std::shared_ptr<IMediaKeySessionFactory> m_mediaKeySessionFactory;
130 :
131 : /**
132 : * @brief The IOcdmSystem instance.
133 : */
134 : std::shared_ptr<firebolt::rialto::wrappers::IOcdmSystem> m_ocdmSystem;
135 :
136 : /**
137 : * @brief Map containing created sessions.
138 : */
139 : std::map<int32_t, std::unique_ptr<IMediaKeySession>> m_mediaKeySessions;
140 :
141 : /**
142 : * @brief KeySystem type of the MediaKeysServerInternal.
143 : */
144 : const std::string m_kKeySystem;
145 :
146 : /**
147 : * @brief This objects id registered on the main thread
148 : */
149 : uint32_t m_mainThreadClientId;
150 :
151 : /**
152 : * @brief Creates a session internally, only to be called on the main thread.
153 : *
154 : * @param[in] sessionType : The session type.
155 : * @param[in] client : Client object for callbacks
156 : * @param[out] keySessionId: The key session id
157 : *
158 : * @retval an error status.
159 : */
160 : MediaKeyErrorStatus createKeySessionInternal(KeySessionType sessionType, std::weak_ptr<IMediaKeysClient> client,
161 : int32_t &keySessionId);
162 :
163 : /**
164 : * @brief Generate internally, only to be called on the main thread.
165 : *
166 : * @param[in] keySessionId : The key session id for the session.
167 : * @param[in] initDataType : The init data type.
168 : * @param[in] initData : The init data.
169 : * @param[in] ldlState : The Limited Duration License state. Most of key systems do not need this parameter.
170 : *
171 : * @retval an error status.
172 : */
173 : MediaKeyErrorStatus generateRequestInternal(int32_t keySessionId, InitDataType initDataType,
174 : const std::vector<uint8_t> &initData,
175 : const LimitedDurationLicense &ldlState);
176 :
177 : /**
178 : * @brief Load internally, only to be called on the main thread.
179 : *
180 : * @param[in] keySessionId : The key session id for the session.
181 : *
182 : * @retval an error status.
183 : */
184 : MediaKeyErrorStatus loadSessionInternal(int32_t keySessionId);
185 :
186 : /**
187 : * @brief Update internally, only to be called on the main thread.
188 : *
189 : * @param[in] keySessionId : The key session id for the session.
190 : * @param[in] responseData : The license response data.
191 : *
192 : * @retval an error status.
193 : */
194 : MediaKeyErrorStatus updateSessionInternal(int32_t keySessionId, const std::vector<uint8_t> &responseData);
195 :
196 : /**
197 : * @brief Close a key session internally, only to be called on the main thread.
198 : *
199 : * @param[in] keySessionId : The key session id.
200 : *
201 : * @retval an error status.
202 : */
203 : MediaKeyErrorStatus closeKeySessionInternal(int32_t keySessionId);
204 :
205 : /**
206 : * @brief Removes a key session internally, only to be called on the main thread.
207 : *
208 : * @param[in] keySessionId : The key session id.
209 : *
210 : * @retval an error status.
211 : */
212 : MediaKeyErrorStatus removeKeySessionInternal(int32_t keySessionId);
213 :
214 : /**
215 : * @brief Get the key session id internally, only to be called on the main thread.
216 : *
217 : * @param[in] keySessionId : The key session id for the session.
218 : * @param[out] cdmKeySessionId : The internal CDM key session ID
219 : *
220 : * @retval an error status.
221 : */
222 : MediaKeyErrorStatus getCdmKeySessionIdInternal(int32_t keySessionId, std::string &cdmKeySessionId);
223 :
224 : /**
225 : * @brief Decrypt internally, only to be called on the main thread.
226 : *
227 : * @param[in] keySessionId : The session id for the session.
228 : * @param[in] encrypted : Gstreamer buffer containing encrypted data and related meta data. If applicable,
229 : * decrypted data will be stored here after this call returns.
230 : * @param[in] caps : The gst caps of buffer.
231 : *
232 : * @retval an error status.
233 : */
234 : MediaKeyErrorStatus decryptInternal(int32_t keySessionId, GstBuffer *encrypted, GstCaps *caps);
235 :
236 : /**
237 : * @brief Selects the specified keyId for the key session internally, only to be called on the main thread.
238 : *
239 : * @param[in] keySessionId : The key session id for the session.
240 : * @param[in] keyId : The key id to select.
241 : *
242 : * @retval an error status.
243 : */
244 : MediaKeyErrorStatus selectKeyIdInternal(int32_t keySessionId, const std::vector<uint8_t> &keyId);
245 :
246 : /**
247 : * @brief Returns true if the Key Session object contains the specified key internally,
248 : * only to be called on the main thread.
249 : *
250 : * @param[in] keySessionId : The key session id for the session.
251 : * @param[in] keyId : The key id.
252 : *
253 : * @retval true if it contains the key.
254 : */
255 : bool containsKeyInternal(int32_t keySessionId, const std::vector<uint8_t> &keyId);
256 :
257 : /**
258 : * @brief Set DRM Header for a key session internally, only to be called on the main thread.
259 : *
260 : * @param[in] keySessionId : The session id for the session.
261 : * @param[in] requestData : The request data.
262 : *
263 : * @retval an error status.
264 : */
265 : MediaKeyErrorStatus setDrmHeaderInternal(int32_t keySessionId, const std::vector<uint8_t> &requestData);
266 :
267 : /**
268 : * @brief Get the last cdm specific DRM error code internally, only to be called on the main thread.
269 : *
270 : * @param[in] keySessionId : The key session id.
271 : * @param[out] errorCode : the error code.
272 : *
273 : * @retval the return status value.
274 : */
275 : MediaKeyErrorStatus getLastDrmErrorInternal(int32_t keySessionId, uint32_t &errorCode);
276 :
277 : /**
278 : * @brief Releases a key session internally, only to be called on the main thread.
279 : *
280 : * @param[in] keySessionId : The key session id.
281 : *
282 : * @retval an error status.
283 : */
284 : MediaKeyErrorStatus releaseKeySessionInternal(int32_t keySessionId);
285 : };
286 :
287 : }; // namespace firebolt::rialto::server
288 :
289 : #endif // FIREBOLT_RIALTO_SERVER_MEDIA_KEYS_SERVER_INTERNAL_H_
|