RDK Documentation (Open Sourced RDK Components)
AampDrmHelper.h
Go to the documentation of this file.
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 2020 RDK Management
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 _AAMP_DRM_HELPER_H
21 #define _AAMP_DRM_HELPER_H
22 
23 /**
24  * @file AampDrmHelper.h
25  * @brief Implented DRM helper functionalities
26  */
27 
28 #include <string>
29 #include <memory>
30 #include <vector>
31 #include <unordered_map>
32 #include <cstdint>
33 
34 #include "AampDrmData.h"
35 #include "AampDrmInfo.h"
36 #include "AampDRMutils.h"
37 #include "AampMemorySystem.h"
38 
39 /**
40  * @struct AampChallengeInfo
41  * @brief Aamp challenge info to get the License
42  */
43 
45 {
47  std::shared_ptr<DrmData> data; /**< Challenge data returned from the DRM system */
48  std::string url; /**< Challenge URL returned from the DRM system */
49  std::string accessToken; /**< Access token required for the license request (if applicable) */
50 };
51 
52 /**
53  * @struct AampLicenseRequest
54  * @brief Holds the data to get the License
55  */
56 
58 {
59  AampLicenseRequest() : method(), url(), payload(), headers(),licenseAnonymousRequest(false)
60  {}
62  {
63  DRM_RETRIEVE, /**< Don't fetch the license, it will be handled externally by the DRM */
64  GET, /**< Fetch license via HTTP GET request */
65  POST /**< Fetch license via HTTP POST request */
66  };
67 
68  LicenseMethod method;
69  bool licenseAnonymousRequest;
70  std::string url;
71  std::string payload;
72  std::unordered_map<std::string, std::vector<std::string>> headers;
73 };
74 
75 /**
76  * @class AampDrmHelper
77  * @brief AampDRM helper to handle DRM operations
78  */
80 {
81 public:
82  const uint32_t TIMEOUT_SECONDS;
83  const std::string EMPTY_DRM_METADATA;
84 
85  const std::string EMPTY_STRING;
86  AampLogManager *mLogObj;
87  AampDrmHelper(const struct DrmInfo drmInfo, AampLogManager *logObj) : mLogObj(logObj), mDrmInfo(drmInfo), TIMEOUT_SECONDS(5000U), EMPTY_DRM_METADATA(), EMPTY_STRING() ,bOutputProtectionEnabled(false) {};
88  AampDrmHelper(const AampDrmHelper&) = delete;
89  AampDrmHelper& operator=(const AampDrmHelper&) = delete;
90 
91  /**
92  * @brief Returns the OCDM system ID of the helper
93  * @return the OCDM system ID
94  */
95  virtual const std::string& ocdmSystemId() const = 0;
96 
97  /**
98  *
99  * @param initData the Init Data to send to the CDM
100  */
101  virtual void createInitData(std::vector<uint8_t>& initData) const = 0;
102 
103  /**
104  * @brief Parse the optional PSSH data
105  * @param initData The init data from the PSSH
106  * @param initDataLen the length of initData
107  * @return
108  */
109  virtual bool parsePssh(const uint8_t* initData, uint32_t initDataLen) = 0;
110 
111  /**
112  * @brief Determine if the DRM system needs to be in the clear or encrypted
113  * @return true if the data is clear, false if it should remain in the TEE
114  */
115  virtual bool isClearDecrypt() const = 0;
116 
117  /**
118  * @brief Determine whether HDCP 2.2 protection is required to be active
119  * @return true if HDCP 2.2 protection is required, false otherwise
120  */
121  virtual bool isHdcp22Required() const { return bOutputProtectionEnabled; };
122 
123  /**
124  * @brief Returns the content specific DRM metadata
125  * @return the DRM metadata
126  */
127  virtual const std::string& getDrmMetaData() const {return EMPTY_DRM_METADATA;}
128 
129  /**
130  * @brief Sets the content specific DRM metadata
131  * @param the DRM metadata
132  */
133  virtual void setDrmMetaData(const std::string& metaData) { }
134 
135  /**
136  * @brief Sets the defualt keyID
137  * @param the DRM cencData data
138  */
139  virtual void setDefaultKeyID(const std::string& cencData) { }
140 
141  /**
142  * @brief Returns the DRM codec type for the helper, used in trace
143  * @return the DRM codec type
144  */
145  virtual int getDrmCodecType() const { return 0; }
146 
147  /**
148  * @brief Get the amount of time in milliseconds to wait before aborting the wait
149  * for the license_challenge message to be received
150  * Default is TWO Seconds - 2000
151  * @return the time to wait in milliseconds
152  */
153  virtual uint32_t licenseGenerateTimeout() const { return TIMEOUT_SECONDS; }
154 
155  /**
156  * @brief Get the amount of time in milliseconds to wait before aborting the wait
157  * for the key_updated message to be received
158  * Default is TWO Seconds - 2000
159  * @return the time to wait in milliseconds
160  */
161  virtual uint32_t keyProcessTimeout() const { return TIMEOUT_SECONDS; }
162 
163  /**
164  * @brief Get the key ID
165  * @param keyID The key ID as a vector of binary data
166  */
167  virtual void getKey(std::vector<uint8_t>& keyID) const = 0;
168 
169  /**
170  * @brief Get the key IDs
171  * @param keyIDs The map containing Key ID vector of binary data
172  */
173  virtual void getKeys(std::map<int, std::vector<uint8_t>>& keyIDs) const {};
174 
175  /**
176  * @brief Get the UUID
177  * @return the UUID
178  */
179  virtual const std::string& getUuid() const { return mDrmInfo.systemUUID; };
180 
181  /**
182  * @brief Determines if the DRM itself fetches the license or if AAMP should use
183  * its own internal HTTP client to fetch the license
184  * Returning 'true' removes AAMP calling generateLicenseRequest() on the CDM
185  * Default is to return false
186  * @return true if the DRM acquires the license, false if AAMP should do it
187  */
188  virtual bool isExternalLicense() const { return false; };
189 
190  /**
191  * @brief Generate the request details for the DRM license
192  * @param challengeInfo challenge information from the DRM system necessary to construct the license request
193  * @param licenseRequest license request data to populate
194  */
195  virtual void generateLicenseRequest(const AampChallengeInfo& challengeInfo, AampLicenseRequest& licenseRequest) const = 0;
196 
197  /**
198  * @brief Transform the license response from the server into the necessary format for OCDM
199  * @param licenseResponse license response from the server to transform
200  */
201  virtual void transformLicenseResponse(std::shared_ptr<DrmData> licenseResponse) const {};
202 
203  /**
204  * @brief Get the memory system used to transform data for transmission
205  * @return the memory system, or null if to send it as is to the ocdm wrapper
206  */
207  virtual AAMPMemorySystem* getMemorySystem() { return nullptr; }
208 
209  /**
210  * @fn compare
211  * @return true if the two helpers can be considered the same, false otherwise
212  */
213  virtual bool compare(std::shared_ptr<AampDrmHelper> other);
214 
215  /**
216  * @brief Cancels a DRM session
217  */
218  virtual void cancelDrmSession() { };
219 
220  /**
221  * @brief Checks if the helper can cancel a session, or if the caller should do it
222  * @return true if the helper can cancel
223  */
224  virtual bool canCancelDrmSession() { return false; }
225 
226  /**
227  * @brief Gets the friendly display name of the DRM
228  * @return friendly name
229  */
230  virtual const std::string& friendlyName() const { return EMPTY_STRING; }
231 
232  /**
233  * @brief Set Output protection flag for the drmHelper
234  * @return None
235  */
236  void setOutputProtectionFlag(bool bValue) { bOutputProtectionEnabled = bValue;}
237 public:
238  virtual ~AampDrmHelper() {};
239 
240 protected:
241  const DrmInfo mDrmInfo;
242  bool bOutputProtectionEnabled;
243 };
244 
245 /**
246  * @class AampDrmHelperFactory
247  * @brief Helper class to Maintain DRM data
248  */
249 
251 {
252 public:
253  /**
254  * @brief Default weighting of a helper factory.
255  * Nominal scale of 0 to DEFAULT_WEIGHTING * 2
256  * Larger weightings have lower priority
257  */
258  static const int DEFAULT_WEIGHTING = 50;
259 
260  /**
261  * @brief Determines if a helper class provides the identified DRM
262  * @param drmInfo DrmInfo built by the HLS manifest parser
263  * @return true if this helper provides that DRM
264  */
265  virtual bool isDRM(const struct DrmInfo& drmInfo) const = 0;
266 
267  /**
268  * @brief Build a helper class to support the identified DRM
269  * @param drmInfo DrmInfo built by the HLS manifest parser
270  * @return the helper
271  */
272  virtual std::shared_ptr<AampDrmHelper> createHelper(const struct DrmInfo& drmInfo, AampLogManager *logObj=NULL) const = 0;
273 
274  /**
275  * @brief Adds the system IDs supported by the DRM to a vector
276  * Used by the GStreamer plugins to advertise the DRM upstream to the pipeline
277  * @param systemIds the vector to use
278  */
279  virtual void appendSystemId(std::vector<std::string>& systemIds) const = 0;
280 
281  /**
282  * @brief Get the weighting for this helper factory, which determines its priority
283  * @return weighting value
284  */
285  int getWeighting() { return mWeighting; }
286 
287  virtual ~AampDrmHelperFactory() {};
288 
289 protected:
290  AampDrmHelperFactory(int weighting = DEFAULT_WEIGHTING);
291  int mWeighting;
292 };
293 
294 
295 /**
296  * @class AampDrmHelperEngine
297  * @brief Helper Engine for Aamp DRM operations
298  */
300 {
301 private:
302  std::vector<AampDrmHelperFactory* > factories;
303 
304 public:
305  /**
306  * @brief AampDrmHelperEngine constructor
307  */
308  AampDrmHelperEngine() : factories() {};
309  /**
310  * @fn hasDRM
311  * @param systemId the UUID from the PSSH or manifest
312  * @param drmInfo DrmInfo built by the HLS manifest parser
313  * @return true if a DRM was found, false otherwise
314  */
315  bool hasDRM(const struct DrmInfo& drmInfo) const;
316 
317  /**
318  * @fn createHelper
319  * @param drmInfo DrmInfo built by the HLS manifest parser
320  * @return the helper
321  */
322  std::shared_ptr<AampDrmHelper> createHelper(const struct DrmInfo& drmInfo, AampLogManager *logObj=NULL) const;
323 
324  /**
325  * @fn getSystemIds
326  * @param ids vector to populate with supported IDs
327  */
328  void getSystemIds(std::vector<std::string>& ids) const;
329 
330  /**
331  * @fn getInstance
332  * @return DRM Helper Engine instance
333  */
335 
336  /**
337  * @fn registerFactory
338  * @param factory helper factory instance to register
339  */
340  void registerFactory(AampDrmHelperFactory* factory);
341 };
342 
343 #endif //_AAMP_DRM_HELPER_H
AampDrmHelper::compare
virtual bool compare(std::shared_ptr< AampDrmHelper > other)
Compare against another helper instance.
Definition: AampDrmHelper.cpp:34
AampDrmHelperFactory::appendSystemId
virtual void appendSystemId(std::vector< std::string > &systemIds) const =0
Adds the system IDs supported by the DRM to a vector Used by the GStreamer plugins to advertise the D...
AampDrmData.h
File holds DRM License data.
AampDrmHelper::isHdcp22Required
virtual bool isHdcp22Required() const
Determine whether HDCP 2.2 protection is required to be active.
Definition: AampDrmHelper.h:121
AampDrmHelperEngine::getInstance
static AampDrmHelperEngine & getInstance()
Get an instance of the DRM Helper Engine.
Definition: AampDrmHelperFactory.cpp:37
AampDrmHelper::getUuid
virtual const std::string & getUuid() const
Get the UUID.
Definition: AampDrmHelper.h:179
AampDrmHelper::friendlyName
virtual const std::string & friendlyName() const
Gets the friendly display name of the DRM.
Definition: AampDrmHelper.h:230
AampDrmHelper::transformLicenseResponse
virtual void transformLicenseResponse(std::shared_ptr< DrmData > licenseResponse) const
Transform the license response from the server into the necessary format for OCDM.
Definition: AampDrmHelper.h:201
AampDrmHelperFactory::AampDrmHelperFactory
AampDrmHelperFactory(int weighting=DEFAULT_WEIGHTING)
AampDrmHelperFactory constructor.
Definition: AampDrmHelperFactory.cpp:86
AampDrmHelper::generateLicenseRequest
virtual void generateLicenseRequest(const AampChallengeInfo &challengeInfo, AampLicenseRequest &licenseRequest) const =0
Generate the request details for the DRM license.
AampLicenseRequest::LicenseMethod
LicenseMethod
Definition: AampDrmHelper.h:61
AampChallengeInfo::data
std::shared_ptr< DrmData > data
Definition: AampDrmHelper.h:46
AampDrmHelper::licenseGenerateTimeout
virtual uint32_t licenseGenerateTimeout() const
Get the amount of time in milliseconds to wait before aborting the wait for the license_challenge mes...
Definition: AampDrmHelper.h:153
AampChallengeInfo::accessToken
std::string accessToken
Definition: AampDrmHelper.h:49
AampDrmHelperFactory::getWeighting
int getWeighting()
Get the weighting for this helper factory, which determines its priority.
Definition: AampDrmHelper.h:285
AampDrmHelper::isExternalLicense
virtual bool isExternalLicense() const
Determines if the DRM itself fetches the license or if AAMP should use its own internal HTTP client t...
Definition: AampDrmHelper.h:188
AampDrmHelperFactory::DEFAULT_WEIGHTING
static const int DEFAULT_WEIGHTING
Default weighting of a helper factory. Nominal scale of 0 to DEFAULT_WEIGHTING * 2 Larger weightings ...
Definition: AampDrmHelper.h:258
AampLogManager
AampLogManager Class.
Definition: AampLogManager.h:150
AampDrmInfo.h
DRM license information for Aamp.
AampLicenseRequest::DRM_RETRIEVE
@ DRM_RETRIEVE
Definition: AampDrmHelper.h:63
AampLicenseRequest
Holds the data to get the License.
Definition: AampDrmHelper.h:57
AampLicenseRequest::GET
@ GET
Definition: AampDrmHelper.h:64
DrmInfo::systemUUID
std::string systemUUID
Definition: AampDrmInfo.h:89
DrmInfo
DRM information required to decrypt.
Definition: AampDrmInfo.h:47
AampDrmHelper::getKeys
virtual void getKeys(std::map< int, std::vector< uint8_t >> &keyIDs) const
Get the key IDs.
Definition: AampDrmHelper.h:173
AampChallengeInfo::url
std::string url
Definition: AampDrmHelper.h:48
AampDrmHelper::getMemorySystem
virtual AAMPMemorySystem * getMemorySystem()
Get the memory system used to transform data for transmission.
Definition: AampDrmHelper.h:207
AampDrmHelper::ocdmSystemId
virtual const std::string & ocdmSystemId() const =0
Returns the OCDM system ID of the helper.
AampDrmHelper::getDrmCodecType
virtual int getDrmCodecType() const
Returns the DRM codec type for the helper, used in trace.
Definition: AampDrmHelper.h:145
AampDrmHelper::setDrmMetaData
virtual void setDrmMetaData(const std::string &metaData)
Sets the content specific DRM metadata.
Definition: AampDrmHelper.h:133
AampDrmHelper::cancelDrmSession
virtual void cancelDrmSession()
Cancels a DRM session.
Definition: AampDrmHelper.h:218
AampDrmHelperEngine
Helper Engine for Aamp DRM operations.
Definition: AampDrmHelper.h:299
AampDRMutils.h
Context-free common utility functions.
AampDrmHelperFactory
Helper class to Maintain DRM data.
Definition: AampDrmHelper.h:250
AampDrmHelper::isClearDecrypt
virtual bool isClearDecrypt() const =0
Determine if the DRM system needs to be in the clear or encrypted.
AampDrmHelper::parsePssh
virtual bool parsePssh(const uint8_t *initData, uint32_t initDataLen)=0
Parse the optional PSSH data.
AampDrmHelperFactory::isDRM
virtual bool isDRM(const struct DrmInfo &drmInfo) const =0
Determines if a helper class provides the identified DRM.
AampDrmHelperFactory::createHelper
virtual std::shared_ptr< AampDrmHelper > createHelper(const struct DrmInfo &drmInfo, AampLogManager *logObj=NULL) const =0
Build a helper class to support the identified DRM.
AampDrmHelper::getDrmMetaData
virtual const std::string & getDrmMetaData() const
Returns the content specific DRM metadata.
Definition: AampDrmHelper.h:127
AampDrmHelper::keyProcessTimeout
virtual uint32_t keyProcessTimeout() const
Get the amount of time in milliseconds to wait before aborting the wait for the key_updated message t...
Definition: AampDrmHelper.h:161
AampLicenseRequest::POST
@ POST
Definition: AampDrmHelper.h:65
AampDrmHelper::getKey
virtual void getKey(std::vector< uint8_t > &keyID) const =0
Get the key ID.
AampDrmHelper::createInitData
virtual void createInitData(std::vector< uint8_t > &initData) const =0
AampDrmHelperEngine::hasDRM
bool hasDRM(const struct DrmInfo &drmInfo) const
Determines whether the helper engine has a DRM helper available for the specified DrmInfo.
Definition: AampDrmHelper.h:308
AampMemorySystem.h
Memory handler for Aamp DRM process.
AampDrmHelper::setOutputProtectionFlag
void setOutputProtectionFlag(bool bValue)
Set Output protection flag for the drmHelper.
Definition: AampDrmHelper.h:236
AampDrmHelper::setDefaultKeyID
virtual void setDefaultKeyID(const std::string &cencData)
Sets the defualt keyID.
Definition: AampDrmHelper.h:139
AampDrmHelperEngine::getSystemIds
void getSystemIds(std::vector< std::string > &ids) const
Get the supported OCDM system IDs.
Definition: AampDrmHelperFactory.cpp:57
AampDrmHelper
AampDRM helper to handle DRM operations.
Definition: AampDrmHelper.h:79
AampDrmHelperEngine::registerFactory
void registerFactory(AampDrmHelperFactory *factory)
Register a Helper Factory.
Definition: AampDrmHelperFactory.cpp:47
AAMPMemorySystem
Handles the operations for AAMP memory managemnts.
Definition: AampMemorySystem.h:37
AampChallengeInfo
Aamp challenge info to get the License.
Definition: AampDrmHelper.h:44
AampDrmHelperEngine::AampDrmHelperEngine
AampDrmHelperEngine()
AampDrmHelperEngine constructor.
Definition: AampDrmHelper.h:308
AampDrmHelperEngine::createHelper
std::shared_ptr< AampDrmHelper > createHelper(const struct DrmInfo &drmInfo, AampLogManager *logObj=NULL) const
Build a helper class to support the identified DRM.
Definition: AampDrmHelperFactory.cpp:69
AampDrmHelper::canCancelDrmSession
virtual bool canCancelDrmSession()
Checks if the helper can cancel a session, or if the caller should do it.
Definition: AampDrmHelper.h:224