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 : struct MediaKeySessionUsage
58 : {
59 : std::unique_ptr<IMediaKeySession> mediaKeySession;
60 : uint32_t bufCounter = 0;
61 : bool shouldBeClosed = false;
62 : bool shouldBeReleased = false;
63 : };
64 : /**
65 : * @brief The constructor.
66 : *
67 : * @param[in] keySystem : The key system for which to create a Media Keys instance.
68 : * @param[in] mainThreadFactory : The main thread factory.
69 : * @param[in] ocdmSystemFactory : The ocdm system factory.
70 : * @param[in] mediaKeySessionFactory : The media key session factory.
71 : *
72 : */
73 : MediaKeysServerInternal(const std::string &keySystem, const std::shared_ptr<IMainThreadFactory> &mainThreadFactory,
74 : std::shared_ptr<firebolt::rialto::wrappers::IOcdmSystemFactory> ocdmSystemFactory,
75 : std::shared_ptr<IMediaKeySessionFactory> mediaKeySessionFactory);
76 :
77 : /**
78 : * @brief Virtual destructor.
79 : */
80 : virtual ~MediaKeysServerInternal();
81 :
82 : MediaKeyErrorStatus selectKeyId(int32_t keySessionId, const std::vector<uint8_t> &keyId) override;
83 :
84 : bool containsKey(int32_t keySessionId, const std::vector<uint8_t> &keyId) override;
85 :
86 : MediaKeyErrorStatus createKeySession(KeySessionType sessionType, std::weak_ptr<IMediaKeysClient> client, bool isLDL,
87 : int32_t &keySessionId) override;
88 :
89 : MediaKeyErrorStatus generateRequest(int32_t keySessionId, InitDataType initDataType,
90 : const std::vector<uint8_t> &initData) override;
91 :
92 : MediaKeyErrorStatus loadSession(int32_t keySessionId) override;
93 :
94 : MediaKeyErrorStatus updateSession(int32_t keySessionId, const std::vector<uint8_t> &responseData) override;
95 :
96 : MediaKeyErrorStatus setDrmHeader(int32_t keySessionId, const std::vector<uint8_t> &requestData) override;
97 :
98 : MediaKeyErrorStatus closeKeySession(int32_t keySessionId) override;
99 :
100 : MediaKeyErrorStatus removeKeySession(int32_t keySessionId) override;
101 :
102 : MediaKeyErrorStatus deleteDrmStore() override;
103 :
104 : MediaKeyErrorStatus deleteKeyStore() override;
105 :
106 : MediaKeyErrorStatus getDrmStoreHash(std::vector<unsigned char> &drmStoreHash) override;
107 :
108 : MediaKeyErrorStatus getKeyStoreHash(std::vector<unsigned char> &keyStoreHash) override;
109 :
110 : MediaKeyErrorStatus getLdlSessionsLimit(uint32_t &ldlLimit) override;
111 :
112 : MediaKeyErrorStatus getLastDrmError(int32_t keySessionId, uint32_t &errorCode) override;
113 :
114 : MediaKeyErrorStatus getDrmTime(uint64_t &drmTime) override;
115 :
116 : MediaKeyErrorStatus getCdmKeySessionId(int32_t keySessionId, std::string &cdmKeySessionId) override;
117 :
118 : MediaKeyErrorStatus releaseKeySession(int32_t keySessionId) override;
119 :
120 : MediaKeyErrorStatus decrypt(int32_t keySessionId, GstBuffer *encrypted, GstCaps *caps) override;
121 :
122 : MediaKeyErrorStatus getMetricSystemData(std::vector<uint8_t> &buffer) override;
123 :
124 : bool hasSession(int32_t keySessionId) const override;
125 :
126 : bool isNetflixPlayreadyKeySystem(int32_t keySessionId) const override;
127 :
128 : void incrementSessionIdUsageCounter(int32_t keySessionId) override;
129 : void decrementSessionIdUsageCounter(int32_t keySessionId) override;
130 : void ping(std::unique_ptr<IHeartbeatHandler> &&heartbeatHandler) override;
131 :
132 : private:
133 : /**
134 : * @brief The mainThread object.
135 : */
136 : std::shared_ptr<IMainThread> m_mainThread;
137 :
138 : /**
139 : * @brief The factory for creating MediaKeySessions.
140 : */
141 : std::shared_ptr<IMediaKeySessionFactory> m_mediaKeySessionFactory;
142 :
143 : /**
144 : * @brief The IOcdmSystem instance.
145 : */
146 : std::shared_ptr<firebolt::rialto::wrappers::IOcdmSystem> m_ocdmSystem;
147 :
148 : /**
149 : * @brief Map containing created sessions.
150 : */
151 : std::map<int32_t, MediaKeySessionUsage> m_mediaKeySessions;
152 :
153 : /**
154 : * @brief KeySystem type of the MediaKeysServerInternal.
155 : */
156 : const std::string m_keySystem;
157 :
158 : /**
159 : * @brief This objects id registered on the main thread
160 : */
161 : uint32_t m_mainThreadClientId;
162 :
163 : /**
164 : * @brief Creates a session internally, only to be called on the main thread.
165 : *
166 : * @param[in] sessionType : The session type.
167 : * @param[in] client : Client object for callbacks
168 : * @param[in] isLDL : Is this an LDL
169 : * @param[out] keySessionId: The key session id
170 : *
171 : * @retval an error status.
172 : */
173 : MediaKeyErrorStatus createKeySessionInternal(KeySessionType sessionType, std::weak_ptr<IMediaKeysClient> client,
174 : bool isLDL, int32_t &keySessionId);
175 :
176 : /**
177 : * @brief Generate internally, only to be called on the main thread.
178 : *
179 : * @param[in] keySessionId : The key session id for the session.
180 : * @param[in] initDataType : The init data type.
181 : * @param[in] initData : The init data.
182 : *
183 : * @retval an error status.
184 : */
185 : MediaKeyErrorStatus generateRequestInternal(int32_t keySessionId, InitDataType initDataType,
186 : const std::vector<uint8_t> &initData);
187 :
188 : /**
189 : * @brief Load internally, only to be called on the main thread.
190 : *
191 : * @param[in] keySessionId : The key session id for the session.
192 : *
193 : * @retval an error status.
194 : */
195 : MediaKeyErrorStatus loadSessionInternal(int32_t keySessionId);
196 :
197 : /**
198 : * @brief Update internally, only to be called on the main thread.
199 : *
200 : * @param[in] keySessionId : The key session id for the session.
201 : * @param[in] responseData : The license response data.
202 : *
203 : * @retval an error status.
204 : */
205 : MediaKeyErrorStatus updateSessionInternal(int32_t keySessionId, const std::vector<uint8_t> &responseData);
206 :
207 : /**
208 : * @brief Close a key session internally, only to be called on the main thread.
209 : *
210 : * @param[in] keySessionId : The key session id.
211 : *
212 : * @retval an error status.
213 : */
214 : MediaKeyErrorStatus closeKeySessionInternal(int32_t keySessionId);
215 :
216 : /**
217 : * @brief Removes a key session internally, only to be called on the main thread.
218 : *
219 : * @param[in] keySessionId : The key session id.
220 : *
221 : * @retval an error status.
222 : */
223 : MediaKeyErrorStatus removeKeySessionInternal(int32_t keySessionId);
224 :
225 : /**
226 : * @brief Get the key session id internally, only to be called on the main thread.
227 : *
228 : * @param[in] keySessionId : The key session id for the session.
229 : * @param[out] cdmKeySessionId : The internal CDM key session ID
230 : *
231 : * @retval an error status.
232 : */
233 : MediaKeyErrorStatus getCdmKeySessionIdInternal(int32_t keySessionId, std::string &cdmKeySessionId);
234 :
235 : /**
236 : * @brief Decrypt internally, only to be called on the main thread.
237 : *
238 : * @param[in] keySessionId : The session id for the session.
239 : * @param[in] encrypted : Gstreamer buffer containing encrypted data and related meta data. If applicable,
240 : * decrypted data will be stored here after this call returns.
241 : * @param[in] caps : The gst caps of buffer.
242 : *
243 : * @retval an error status.
244 : */
245 : MediaKeyErrorStatus decryptInternal(int32_t keySessionId, GstBuffer *encrypted, GstCaps *caps);
246 :
247 : /**
248 : * @brief Selects the specified keyId for the key session internally, 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 to select.
252 : *
253 : * @retval an error status.
254 : */
255 : MediaKeyErrorStatus selectKeyIdInternal(int32_t keySessionId, const std::vector<uint8_t> &keyId);
256 :
257 : /**
258 : * @brief Returns true if the Key Session object contains the specified key internally,
259 : * only to be called on the main thread.
260 : *
261 : * @param[in] keySessionId : The key session id for the session.
262 : * @param[in] keyId : The key id.
263 : *
264 : * @retval true if it contains the key.
265 : */
266 : bool containsKeyInternal(int32_t keySessionId, const std::vector<uint8_t> &keyId);
267 :
268 : /**
269 : * @brief Set DRM Header for a key session internally, only to be called on the main thread.
270 : *
271 : * @param[in] keySessionId : The session id for the session.
272 : * @param[in] requestData : The request data.
273 : *
274 : * @retval an error status.
275 : */
276 : MediaKeyErrorStatus setDrmHeaderInternal(int32_t keySessionId, const std::vector<uint8_t> &requestData);
277 :
278 : /**
279 : * @brief Get the last cdm specific DRM error code internally, only to be called on the main thread.
280 : *
281 : * @param[in] keySessionId : The key session id.
282 : * @param[out] errorCode : the error code.
283 : *
284 : * @retval the return status value.
285 : */
286 : MediaKeyErrorStatus getLastDrmErrorInternal(int32_t keySessionId, uint32_t &errorCode);
287 :
288 : /**
289 : * @brief Checks, if key system of media key session is Netflix Playready internally, only to be called on the main thread.
290 : *
291 : * @param[in] keySessionId : The session id for the session.
292 : *
293 : * @retval true if key system is Playready
294 : */
295 : bool isNetflixPlayreadyKeySystemInternal(int32_t keySessionId) const;
296 :
297 : /**
298 : * @brief Releases a key session internally, only to be called on the main thread.
299 : *
300 : * @param[in] keySessionId : The key session id.
301 : *
302 : * @retval an error status.
303 : */
304 : MediaKeyErrorStatus releaseKeySessionInternal(int32_t keySessionId);
305 :
306 : void incrementSessionIdUsageCounterInternal(int32_t keySessionId);
307 : void decrementSessionIdUsageCounterInternal(int32_t keySessionId);
308 : };
309 :
310 : }; // namespace firebolt::rialto::server
311 :
312 : #endif // FIREBOLT_RIALTO_SERVER_MEDIA_KEYS_SERVER_INTERNAL_H_
|