RDK Documentation (Open Sourced RDK Components)
AampDRMSessionManager.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 2018 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 
21 /**
22 * @file AampDRMSessionManager.h
23 * @brief Header file for DRM session manager
24 */
25 #ifndef AampDRMSessionManager_h
26 #define AampDRMSessionManager_h
27 
28 #include "aampdrmsessionfactory.h"
29 #include "AampDrmSession.h"
30 #include "AampDRMutils.h"
31 #include "priv_aamp.h"
32 #include "main_aamp.h"
33 #include <string>
34 #include <curl/curl.h>
35 #include "AampDrmHelper.h"
36 
37 #ifdef USE_SECCLIENT
38 #include "sec_client.h"
39 #endif
40 
41 #define VIDEO_SESSION 0
42 #define AUDIO_SESSION 1
43 
44 /**
45  * @struct DrmSessionCacheInfo
46  * @brief Drm Session Cache Information for keeping single DRM session always.
47  */
48 typedef struct DrmSessionCacheInfo{
49  pthread_t createDRMSessionThreadID; /**< Thread Id for DrM Session thread */
50  bool drmSessionThreadStarted; /**< DRM Session start flag to identify the DRM Session thread running */
52 
53 /**
54  * @struct DrmSessionDataInfo
55  * @brief Drm Session Data Information
56  * for storing in a pool from parser.
57  */
58 typedef struct DrmSessionDataInfo{
59  struct DrmSessionParams* sessionData; /**< DRM Session Data */
60  bool isProcessedLicenseAcquire; /**< Flag to avoid multiple acquire for a key */
61  unsigned char *processedKeyId; /**< Pointer to store last processed key Id */
62  int processedKeyIdLen; /**< Last processed key Id size */
64 
65 /**
66  * @struct DrmSessionContext
67  * @brief To store drmSession and keyId data.
68  */
70 {
71  std::vector<uint8_t> data;
72  pthread_mutex_t sessionMutex;
73  AampDrmSession * drmSession;
74 
75  DrmSessionContext() : sessionMutex(PTHREAD_MUTEX_INITIALIZER), drmSession(NULL),data()
76  {
77  }
78  DrmSessionContext(const DrmSessionContext& other) : sessionMutex(other.sessionMutex), data(), drmSession()
79  {
80  // Releases memory allocated after destructing any of these objects
81  drmSession = other.drmSession;
82  data = other.data;
83  }
84  DrmSessionContext& operator=(const DrmSessionContext& other)
85  {
86  sessionMutex = other.sessionMutex;
87  data = other.data;
88  drmSession = other.drmSession;
89  return *this;
90  }
92  {
93  pthread_mutex_destroy(&sessionMutex);
94  }
95 };
96 
97 /**
98  * @struct DrmSessionParams
99  * @brief Holds data regarding drm session
100  */
102 {
103  DrmSessionParams() : initData(NULL), initDataLen(0), stream_type(eMEDIATYPE_DEFAULT),
104  aamp(NULL), drmType(eDRM_NONE), drmHelper()
105  {};
106  DrmSessionParams(const DrmSessionParams&) = delete;
107  DrmSessionParams& operator=(const DrmSessionParams&) = delete;
108  unsigned char *initData;
109  int initDataLen;
110  MediaType stream_type;
111  PrivateInstanceAAMP *aamp;
112  DRMSystems drmType;
113  std::shared_ptr<AampDrmHelper> drmHelper;
114 };
115 
116 /**
117  * @struct KeyID
118  * @brief Structure to hold, keyId and session creation time for
119  * keyId
120  */
121 struct KeyID
122 {
123  std::vector<std::vector<uint8_t>> data;
124  long long creationTime;
125  bool isFailedKeyId;
126  bool isPrimaryKeyId;
127 
128  KeyID();
129 };
130 
131 /**
132  * @brief Enum to represent session manager state.
133  * Session manager would abort any createDrmSession
134  * request if in eSESSIONMGR_INACTIVE state.
135  */
136 typedef enum{
137  eSESSIONMGR_INACTIVE, /**< DRM Session mgr is inactive */
138  eSESSIONMGR_ACTIVE /**< DRM session mgr is active */
140 
141 /**
142  * @class AampDRMSessionManager
143  * @brief Controller for managing DRM sessions.
144  */
146 {
147 
148 private:
149  AampLogManager *mLogObj;
150  DrmSessionContext *drmSessionContexts;
151  KeyID *cachedKeyIDs;
152  char* accessToken;
153  int accessTokenLen;
154  SessionMgrState sessionMgrState;
155  pthread_mutex_t accessTokenMutex;
156  pthread_mutex_t cachedKeyMutex;
157  pthread_mutex_t mDrmSessionLock;
158  bool curlSessionAbort;
159  bool licenseRequestAbort;
160  bool mEnableAccessAtrributes;
161  int mMaxDRMSessions;
162 #ifdef USE_SECMANAGER
163  int64_t mSessionId;
164 #endif
165  /**
166  * @brief Copy constructor disabled
167  *
168  */
170  /**
171  * @brief assignment operator disabled
172  *
173  */
175  /**
176  * @fn write_callback
177  * @param[in] ptr - Pointer to received data.
178  * @param[in] size, nmemb - Size of received data (size * nmemb).
179  * @param[out] userdata - Pointer to buffer where the received data is copied.
180  * @return returns the number of bytes processed.
181  */
182  static size_t write_callback(char *ptr, size_t size, size_t nmemb,
183  void *userdata);
184  /**
185  * @brief
186  * @param clientp app-specific as optionally set with CURLOPT_PROGRESSDATA
187  * @param dltotal total bytes expected to download
188  * @param dlnow downloaded bytes so far
189  * @param ultotal total bytes expected to upload
190  * @param ulnow uploaded bytes so far
191  * @retval Return non-zero for CURLE_ABORTED_BY_CALLBACK
192  */
193  static int progress_callback(void *clientp, double dltotal,
194  double dlnow, double ultotal, double ulnow );
195 public:
196 
197  /**
198  * @fn AampDRMSessionManager
199  */
200  AampDRMSessionManager(AampLogManager *logObj, int maxDrmSessions);
201 
202  void initializeDrmSessions();
203 
204  /**
205  * @fn ~AampDRMSessionManager
206  */
208  /**
209  * @fn createDrmSession
210  * @param[in] systemId - UUID of the DRM system.
211  * @param[in] initDataPtr - Pointer to PSSH data.
212  * @param[in] dataLength - Length of PSSH data.
213  * @param[in] streamType - Whether audio or video.
214  * @param[in] contentMetadata - Pointer to content meta data, when content meta data
215  * is already extracted during manifest parsing. Used when content meta data
216  * is available as part of another PSSH header, like DRM Agnostic PSSH
217  * header.
218  * @param[in] aamp - Pointer to PrivateInstanceAAMP, for DRM related profiling.
219  * @retval error_code - Gets updated with proper error code, if session creation fails.
220  * No NULL checks are done for error_code, caller should pass a valid pointer.
221  */
222  AampDrmSession * createDrmSession(const char* systemId, MediaFormat mediaFormat,
223  const unsigned char * initDataPtr, uint16_t dataLength, MediaType streamType,
224  PrivateInstanceAAMP* aamp, DrmMetaDataEventPtr e, const unsigned char *contentMetadata = nullptr,
225  bool isPrimarySession = false);
226  /**
227  * @fn createDrmSession
228  * @return AampdrmSession
229  */
230  AampDrmSession* createDrmSession(std::shared_ptr<AampDrmHelper> drmHelper, DrmMetaDataEventPtr eventHandle, PrivateInstanceAAMP* aampInstance, MediaType streamType);
231 
232 #if defined(USE_SECCLIENT) || defined(USE_SECMANAGER)
233  DrmData * getLicenseSec(const AampLicenseRequest &licenseRequest, std::shared_ptr<AampDrmHelper> drmHelper,
234  const AampChallengeInfo& challengeInfo, PrivateInstanceAAMP* aampInstance, int32_t *httpCode, int32_t *httpExtStatusCode, DrmMetaDataEventPtr eventHandle);
235 #endif
236  /**
237  * @fn getLicense
238  *
239  * @param[in] licRequest - License request details (URL, headers etc.)
240  * @param[out] httpError - Gets updated with http error; default -1.
241  * @param[in] isContentMetadataAvailable - Flag to indicate whether MSO specific headers
242  * are to be used.
243  * @param[in] licenseProxy - Proxy to use for license requests.
244  * @return Structure holding DRM license key and it's length; NULL and 0 if request fails
245  * @note Memory for license key is dynamically allocated, deallocation
246  * should be handled at the caller side.
247  * customHeader ownership should be taken up by getLicense function
248  *
249  */
250  DrmData * getLicense(AampLicenseRequest &licRequest, int32_t *httpError, MediaType streamType, PrivateInstanceAAMP* aamp, bool isContentMetadataAvailable = false, std::string licenseProxy="");
251  /**
252  * @fn IsKeyIdUsable
253  * @param[in] keyIdArray - key Id extracted from pssh data
254  * @return bool - true if key is not cached/cached with no failure,
255  * false if keyId is already marked as failed.
256  */
257  bool IsKeyIdUsable(std::vector<uint8_t> keyIdArray);
258  /**
259  * @fn clearSessionData
260  *
261  * @return void.
262  */
263  void clearSessionData();
264  /**
265  * @fn clearAccessToken
266  *
267  * @return void.
268  */
269  void clearAccessToken();
270  /**
271  * @fn clearFailedKeyIds
272  *
273  * @return void.
274  */
275  void clearFailedKeyIds();
276  /**
277  * @fn clearDrmSession
278  *
279  * @param forceClearSession clear the drm session irrespective of failed keys if LicenseCaching is false.
280  * @return void.
281  */
282  void clearDrmSession(bool forceClearSession = false);
283 
284  void setVideoWindowSize(int width, int height);
285 
286  void setPlaybackSpeedState(int speed, double position, bool delayNeeded = false);
287  /**
288  * @fn setSessionMgrState
289  * @param state session manager sate to be set
290  * @return void.
291  */
293 
294  /**
295  * @fn getSessionMgrState
296  * @return session manager state.
297  */
299  /**
300  * @fn setCurlAbort
301  * @param isAbort bool flag to curl abort
302  * @return void.
303  */
304  void setCurlAbort(bool isAbort);
305  /**
306  * @fn getCurlAbort
307  * @return bool flag curlSessionAbort.
308  */
309  bool getCurlAbort(void);
310  /**
311  * @fn setLicenseRequestAbort
312  * @param isAbort bool flag to curl abort
313  * @return void
314  */
315  void setLicenseRequestAbort(bool isAbort);
316  /**
317  * @fn getAccessToken
318  *
319  * @param[out] tokenLength - Gets updated with accessToken length.
320  * @return Pointer to accessToken.
321  * @note AccessToken memory is dynamically allocated, deallocation
322  * should be handled at the caller side.
323  */
324  const char* getAccessToken(int &tokenLength, long &error_code ,bool bSslPeerVerify);
325  /**
326  * @fn getDrmSession
327  * @return index to the selected drmSessionContext which has been selected
328  */
329  KeyState getDrmSession(std::shared_ptr<AampDrmHelper> drmHelper, int &selectedSlot, DrmMetaDataEventPtr eventHandle, PrivateInstanceAAMP* aampInstance, bool isPrimarySession = false);
330  /**
331  * @fn initializeDrmSession
332  */
333  KeyState initializeDrmSession(std::shared_ptr<AampDrmHelper> drmHelper, int sessionSlot, DrmMetaDataEventPtr eventHandle, PrivateInstanceAAMP* aampInstance);
334  /**
335  * @fn acquireLicense
336  */
337  KeyState acquireLicense(std::shared_ptr<AampDrmHelper> drmHelper, int sessionSlot, int &cdmError,
338  DrmMetaDataEventPtr eventHandle, PrivateInstanceAAMP* aampInstance, MediaType streamType);
339 
340  KeyState handleLicenseResponse(std::shared_ptr<AampDrmHelper> drmHelper, int sessionSlot, int &cdmError,
341  int32_t httpResponseCode, int32_t httpExtResponseCode, shared_ptr<DrmData> licenseResponse, DrmMetaDataEventPtr eventHandle, PrivateInstanceAAMP* aampInstance);
342 
343  KeyState processLicenseResponse(std::shared_ptr<AampDrmHelper> drmHelper, int sessionSlot, int &cdmError,
344  shared_ptr<DrmData> licenseResponse, DrmMetaDataEventPtr eventHandle, PrivateInstanceAAMP* aampInstance);
345  /**
346  * @fn configureLicenseServerParameters
347  */
348  bool configureLicenseServerParameters(std::shared_ptr<AampDrmHelper> drmHelper, AampLicenseRequest& licRequest,
349  std::string &licenseServerProxy, const AampChallengeInfo& challengeInfo, PrivateInstanceAAMP* aampInstance);
350  /**
351  * @fn notifyCleanup
352  */
353  void notifyCleanup();
354 
355  /**
356  * @fn ContentProtectionDataUpdate
357  */
358  void ContentProtectionDataUpdate(PrivateInstanceAAMP* aampInstance, std::vector<uint8_t> keyId, MediaType streamType);
359 };
360 
361 /**
362  * @struct writeCallbackData
363  * @brief structure to hold DRM data to write
364  */
365 
366 typedef struct writeCallbackData{
367  DrmData *data ;
368  AampDRMSessionManager* mDRMSessionManager;
370 
371 #endif
AampDRMSessionManager::getDrmSession
KeyState getDrmSession(std::shared_ptr< AampDrmHelper > drmHelper, int &selectedSlot, DrmMetaDataEventPtr eventHandle, PrivateInstanceAAMP *aampInstance, bool isPrimarySession=false)
Create a DRM Session using the Drm Helper Determine a slot in the drmSession Contexts which can be us...
Definition: AampDRMSessionManager.cpp:1019
AampDRMSessionManager::AampDRMSessionManager
AampDRMSessionManager(const AampDRMSessionManager &)=delete
Copy constructor disabled.
AampDrmHelper.h
Implented DRM helper functionalities.
DrmSessionDataInfo::isProcessedLicenseAcquire
bool isProcessedLicenseAcquire
Definition: AampDRMSessionManager.h:60
AampDRMSessionManager::createDrmSession
AampDrmSession * createDrmSession(const char *systemId, MediaFormat mediaFormat, const unsigned char *initDataPtr, uint16_t dataLength, MediaType streamType, PrivateInstanceAAMP *aamp, DrmMetaDataEventPtr e, const unsigned char *contentMetadata=nullptr, bool isPrimarySession=false)
Creates and/or returns the DRM session corresponding to keyId (Present in initDataPtr) AampDRMSession...
Definition: AampDRMSessionManager.cpp:875
AampDRMSessionManager::operator=
AampDRMSessionManager & operator=(const AampDRMSessionManager &)=delete
assignment operator disabled
DrmSessionDataInfo
Drm Session Data Information for storing in a pool from parser.
Definition: AampDRMSessionManager.h:58
DrmData
To hold DRM key, license request etc.
Definition: AampDrmData.h:32
DrmSessionParams
Holds data regarding drm session.
Definition: AampDRMSessionManager.h:101
AampDRMSessionManager::setSessionMgrState
void setSessionMgrState(SessionMgrState state)
Set Session manager state.
Definition: AampDRMSessionManager.cpp:183
main_aamp.h
Types and APIs exposed by the AAMP player.
AampDRMSessionManager::getCurlAbort
bool getCurlAbort(void)
Get Session abort flag.
Definition: AampDRMSessionManager.cpp:206
eSESSIONMGR_ACTIVE
@ eSESSIONMGR_ACTIVE
Definition: AampDRMSessionManager.h:138
AampDRMSessionManager
Controller for managing DRM sessions.
Definition: AampDRMSessionManager.h:145
DrmSessionCacheInfo::createDRMSessionThreadID
pthread_t createDRMSessionThreadID
Definition: AampDRMSessionManager.h:49
AampDRMSessionManager::notifyCleanup
void notifyCleanup()
Resethe current seesion ID.
Definition: AampDRMSessionManager.cpp:1645
writeCallbackData
structure to hold DRM data to write
Definition: AampDRMSessionManager.h:366
AampDRMSessionManager::clearDrmSession
void clearDrmSession(bool forceClearSession=false)
Clean up the Session Data if license key acquisition failed or if LicenseCaching is false.
Definition: AampDRMSessionManager.cpp:257
AampDRMSessionManager::getAccessToken
const char * getAccessToken(int &tokenLength, long &error_code, bool bSslPeerVerify)
Get the accessToken from authService.
Definition: AampDRMSessionManager.cpp:388
AampLogManager
AampLogManager Class.
Definition: AampLogManager.h:150
AampDRMSessionManager::getLicense
DrmData * getLicense(AampLicenseRequest &licRequest, int32_t *httpError, MediaType streamType, PrivateInstanceAAMP *aamp, bool isContentMetadataAvailable=false, std::string licenseProxy="")
Get DRM license key from DRM server.
Definition: AampDRMSessionManager.cpp:670
eMEDIATYPE_DEFAULT
@ eMEDIATYPE_DEFAULT
Definition: AampMediaType.h:58
AampDRMSessionManager::write_callback
static size_t write_callback(char *ptr, size_t size, size_t nmemb, void *userdata)
Curl write callback, used to get the curl o/p from DRM license, accessToken curl requests.
Definition: AampDRMSessionManager.cpp:333
SessionMgrState
SessionMgrState
Enum to represent session manager state. Session manager would abort any createDrmSession request if ...
Definition: AampDRMSessionManager.h:136
AampLicenseRequest
Holds the data to get the License.
Definition: AampDrmHelper.h:57
eSESSIONMGR_INACTIVE
@ eSESSIONMGR_INACTIVE
Definition: AampDRMSessionManager.h:137
AampDRMSessionManager::getSessionMgrState
SessionMgrState getSessionMgrState()
Get Session manager state.
Definition: AampDRMSessionManager.cpp:191
AampDRMSessionManager::clearFailedKeyIds
void clearFailedKeyIds()
Clean up the failed keyIds.
Definition: AampDRMSessionManager.cpp:222
AampDRMSessionManager::IsKeyIdUsable
bool IsKeyIdUsable(std::vector< uint8_t > keyIdArray)
Get DRM license key from DRM server.
Definition: AampDRMSessionManager.cpp:482
MediaType
MediaType
Media types.
Definition: AampMediaType.h:37
DrmSessionDataInfo::sessionData
struct DrmSessionParams * sessionData
Definition: AampDRMSessionManager.h:59
DrmSessionDataInfo::processedKeyId
unsigned char * processedKeyId
Definition: AampDRMSessionManager.h:61
AampDRMSessionManager::configureLicenseServerParameters
bool configureLicenseServerParameters(std::shared_ptr< AampDrmHelper > drmHelper, AampLicenseRequest &licRequest, std::string &licenseServerProxy, const AampChallengeInfo &challengeInfo, PrivateInstanceAAMP *aampInstance)
Configure the Drm license server parameters for URL/proxy and custom http request headers.
Definition: AampDRMSessionManager.cpp:1585
aampdrmsessionfactory.h
Header file for AampDrmSessionFactory.
AampDrmSession
Base class for DRM sessions.
Definition: AampDrmSession.h:69
AampDRMSessionManager::setCurlAbort
void setCurlAbort(bool isAbort)
Set Session abort flag.
Definition: AampDRMSessionManager.cpp:199
AampDRMutils.h
Context-free common utility functions.
priv_aamp.h
Private functions and types used internally by AAMP.
AampDRMSessionManager::progress_callback
static int progress_callback(void *clientp, double dltotal, double dlnow, double ultotal, double ulnow)
Definition: AampDRMSessionManager.cpp:308
AampDRMSessionManager::clearSessionData
void clearSessionData()
Clean up the memory used by session variables.
Definition: AampDRMSessionManager.cpp:155
KeyID
Structure to hold, keyId and session creation time for keyId.
Definition: AampDRMSessionManager.h:121
PrivateInstanceAAMP
Class representing the AAMP player's private instance, which is not exposed to outside world.
Definition: priv_aamp.h:640
AampDRMSessionManager::initializeDrmSession
KeyState initializeDrmSession(std::shared_ptr< AampDrmHelper > drmHelper, int sessionSlot, DrmMetaDataEventPtr eventHandle, PrivateInstanceAAMP *aampInstance)
Initialize the Drm System with InitData(PSSH)
Definition: AampDRMSessionManager.cpp:1232
AampDRMSessionManager::acquireLicense
KeyState acquireLicense(std::shared_ptr< AampDrmHelper > drmHelper, int sessionSlot, int &cdmError, DrmMetaDataEventPtr eventHandle, PrivateInstanceAAMP *aampInstance, MediaType streamType)
sent license challenge to the DRM server and provide the respone to CDM
Definition: AampDRMSessionManager.cpp:1265
DrmSessionContext
To store drmSession and keyId data.
Definition: AampDRMSessionManager.h:69
AampDRMSessionManager::setLicenseRequestAbort
void setLicenseRequestAbort(bool isAbort)
Get Session abort flag.
Definition: AampDRMSessionManager.cpp:213
eDRM_NONE
@ eDRM_NONE
Definition: AampDrmSystems.h:35
KeyState
KeyState
DRM session states.
Definition: AampDrmSession.h:54
DrmSessionDataInfo::processedKeyIdLen
int processedKeyIdLen
Definition: AampDRMSessionManager.h:62
DrmSessionCacheInfo::drmSessionThreadStarted
bool drmSessionThreadStarted
Definition: AampDRMSessionManager.h:50
DRMSystems
DRMSystems
DRM system types.
Definition: AampDrmSystems.h:33
AampDRMSessionManager::clearAccessToken
void clearAccessToken()
Clean up the memory for accessToken.
Definition: AampDRMSessionManager.cpp:244
AampDRMSessionManager::~AampDRMSessionManager
~AampDRMSessionManager()
AampDRMSessionManager Destructor.
Definition: AampDRMSessionManager.cpp:141
AampChallengeInfo
Aamp challenge info to get the License.
Definition: AampDrmHelper.h:44
MediaFormat
MediaFormat
Media format types.
Definition: AampDrmMediaFormat.h:32
DrmSessionCacheInfo
Drm Session Cache Information for keeping single DRM session always.
Definition: AampDRMSessionManager.h:48
AampDrmSession.h
Header file for AampDrmSession.