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 : const std::shared_ptr<firebolt::rialto::wrappers::IOcdmSystemFactory> &ocdmSystemFactory,
68 : const 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, const std::string &cdmKeySessionId) 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 : * @param[in] cdmKeySessionId : The internal CDM key session ID.
182 : *
183 : * @retval an error status.
184 : */
185 : MediaKeyErrorStatus loadSessionInternal(int32_t keySessionId, const std::string &cdmKeySessionId);
186 :
187 : /**
188 : * @brief Update internally, only to be called on the main thread.
189 : *
190 : * @param[in] keySessionId : The key session id for the session.
191 : * @param[in] responseData : The license response data.
192 : *
193 : * @retval an error status.
194 : */
195 : MediaKeyErrorStatus updateSessionInternal(int32_t keySessionId, const std::vector<uint8_t> &responseData);
196 :
197 : /**
198 : * @brief Close a key session internally, only to be called on the main thread.
199 : *
200 : * @param[in] keySessionId : The key session id.
201 : *
202 : * @retval an error status.
203 : */
204 : MediaKeyErrorStatus closeKeySessionInternal(int32_t keySessionId);
205 :
206 : /**
207 : * @brief Removes a key session internally, only to be called on the main thread.
208 : *
209 : * @param[in] keySessionId : The key session id.
210 : *
211 : * @retval an error status.
212 : */
213 : MediaKeyErrorStatus removeKeySessionInternal(int32_t keySessionId);
214 :
215 : /**
216 : * @brief Get the key session id internally, only to be called on the main thread.
217 : *
218 : * @param[in] keySessionId : The key session id for the session.
219 : * @param[out] cdmKeySessionId : The internal CDM key session ID
220 : *
221 : * @retval an error status.
222 : */
223 : MediaKeyErrorStatus getCdmKeySessionIdInternal(int32_t keySessionId, std::string &cdmKeySessionId);
224 :
225 : /**
226 : * @brief Decrypt internally, only to be called on the main thread.
227 : *
228 : * @param[in] keySessionId : The session id for the session.
229 : * @param[in] encrypted : Gstreamer buffer containing encrypted data and related meta data. If applicable,
230 : * decrypted data will be stored here after this call returns.
231 : * @param[in] caps : The gst caps of buffer.
232 : *
233 : * @retval an error status.
234 : */
235 : MediaKeyErrorStatus decryptInternal(int32_t keySessionId, GstBuffer *encrypted, GstCaps *caps);
236 :
237 : /**
238 : * @brief Selects the specified keyId for the key session internally, only to be called on the main thread.
239 : *
240 : * @param[in] keySessionId : The key session id for the session.
241 : * @param[in] keyId : The key id to select.
242 : *
243 : * @retval an error status.
244 : */
245 : MediaKeyErrorStatus selectKeyIdInternal(int32_t keySessionId, const std::vector<uint8_t> &keyId);
246 :
247 : /**
248 : * @brief Returns true if the Key Session object contains the specified key internally,
249 : * only to be called on the main thread.
250 : *
251 : * @param[in] keySessionId : The key session id for the session.
252 : * @param[in] keyId : The key id.
253 : *
254 : * @retval true if it contains the key.
255 : */
256 : bool containsKeyInternal(int32_t keySessionId, const std::vector<uint8_t> &keyId);
257 :
258 : /**
259 : * @brief Set DRM Header for a key session internally, only to be called on the main thread.
260 : *
261 : * @param[in] keySessionId : The session id for the session.
262 : * @param[in] requestData : The request data.
263 : *
264 : * @retval an error status.
265 : */
266 : MediaKeyErrorStatus setDrmHeaderInternal(int32_t keySessionId, const std::vector<uint8_t> &requestData);
267 :
268 : /**
269 : * @brief Get the last cdm specific DRM error code internally, only to be called on the main thread.
270 : *
271 : * @param[in] keySessionId : The key session id.
272 : * @param[out] errorCode : the error code.
273 : *
274 : * @retval the return status value.
275 : */
276 : MediaKeyErrorStatus getLastDrmErrorInternal(int32_t keySessionId, uint32_t &errorCode);
277 :
278 : /**
279 : * @brief Releases a key session internally, only to be called on the main thread.
280 : *
281 : * @param[in] keySessionId : The key session id.
282 : *
283 : * @retval an error status.
284 : */
285 : MediaKeyErrorStatus releaseKeySessionInternal(int32_t keySessionId);
286 : };
287 :
288 : }; // namespace firebolt::rialto::server
289 :
290 : #endif // FIREBOLT_RIALTO_SERVER_MEDIA_KEYS_SERVER_INTERNAL_H_
|