RDK Documentation (Open Sourced RDK Components)
AampCurlStore.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 2022 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  * @file AampCurlStore.h
22  * @brief Advanced Adaptive Media Player (AAMP) Curl store
23  */
24 
25 #ifndef AAMPCURLSTORE_H
26 #define AAMPCURLSTORE_H
27 
28 #include "priv_aamp.h"
29 #include <map>
30 #include <iterator>
31 #include <vector>
32 #include <glib.h>
33 
34 #define eCURL_MAX_AGE_TIME ( (300) * (1000) ) /**< 5 mins - 300 secs - Max age for a connection */
35 
36 /**
37  * @enum AampCurlStoreErrorCode
38  * @brief Error codes returned by curlstore
39  */
41 {
42  eCURL_STORE_HOST_NOT_AVAILABLE,
43  eCURL_STORE_SOCK_NOT_AVAILABLE,
44  eCURL_STORE_HOST_SOCK_AVAILABLE
45 };
46 
47 /**
48  * @struct curldatasharelock
49  * @brief locks used when lock/unlock callback occurs for different shared data
50  */
51 typedef struct curldatasharelock
52 {
53  pthread_mutex_t mCurlSharedlock;
54  pthread_mutex_t mDnsCurlShareMutex;
55  pthread_mutex_t mSslCurlShareMutex;
56 
57  curldatasharelock():mCurlSharedlock(), mDnsCurlShareMutex(),mSslCurlShareMutex()
58  {
59  pthread_mutex_init(&mCurlSharedlock, NULL);
60  pthread_mutex_init(&mDnsCurlShareMutex, NULL);
61  pthread_mutex_init(&mSslCurlShareMutex, NULL);
62  }
64 
65 typedef struct curlstruct
66 {
67  CURL *curl;
68  int curlId;
69  long long eHdlTimestamp;
70 
71  curlstruct():curl(NULL), eHdlTimestamp(0),curlId(0){}
73 
74 /**
75  * @struct curlstorestruct
76  * @brief structure to store curl easy, shared handle & locks for a host
77  */
78 typedef struct curlstorestruct
79 {
80  std::deque<CurlHandleStruct> mFreeQ;
81  CURLSH* mCurlShared;
82  CurlDataShareLock *pstShareLocks;
83 
84  unsigned int mCurlStoreUserCount;
85  long long timestamp;
86 
87  curlstorestruct():mCurlShared(NULL), pstShareLocks(NULL), timestamp(0), mCurlStoreUserCount(0), mFreeQ()
88  {}
89 
90  //Disabled for now
91  curlstorestruct(const curlstorestruct&) = delete;
92  curlstorestruct& operator=(const curlstorestruct&) = delete;
93 
95 
96 /**
97  * @class CurlStore
98  * @brief Singleton curlstore to save/reuse curl handles
99  */
101 {
102 private:
103  static CurlStore *CurlInstance;
104  static pthread_mutex_t mCurlInstLock;
105  static int MaxCurlSockStore;
106 
107  typedef std::unordered_map <std::string, CurlSocketStoreStruct*> CurlSockData ;
108  typedef std::unordered_map <std::string, CurlSocketStoreStruct*>::iterator CurlSockDataIter;
109  CurlSockData umCurlSockDataStore;
110 
111 protected:
112  CurlStore( ):umCurlSockDataStore(){}
113  ~CurlStore( ){}
114 
115 public:
116  /**
117  * @param[in] hostname - hostname part from url
118  * @param[in] CurlIndex - Index of Curl instance
119  * @param[out] curl - curl easy handle from curl store.
120  * @return AampCurlStoreErrorCode enum type
121  */
122  AampCurlStoreErrorCode GetFromCurlStore ( const std::string &hostname, AampCurlInstance CurlIndex, CURL **curl );
123 
124  /**
125  * @param[in] hostname - hostname part from url
126  * @param[in] CurlIndex - Index of Curl instance
127  * @param[in] count - No of curl handles
128  * @param[out] priv - curl easy handle from curl store will get stored in priv instance
129  * @return AampCurlStoreErrorCode enum type
130  */
131  AampCurlStoreErrorCode GetFromCurlStoreBulk ( const std::string &hostname, AampCurlInstance CurlIndex, int count, void *priv, bool HostCurlFd );
132 
133  /**
134  * @param[in] hostname - hostname part from url
135  * @param[in] CurlIndex - Index of Curl instance
136  * @param[in] curl - curl easy handle to save in curl store.
137  * @return void
138  */
139  void KeepInCurlStore ( const std::string &hostname, AampCurlInstance CurlIndex, CURL *curl );
140 
141  /**
142  * @param[in] hostname - hostname part from url
143  * @param[in] CurlIndex - Index of Curl instance
144  * @param[in] count - No of curl handles
145  * @param[out] priv - curl easy handles in priv instance, saved in curl store
146  * @return void
147  */
148  void KeepInCurlStoreBulk ( const std::string &hostname, AampCurlInstance CurlIndex, int count, void *priv, bool HostCurlFd );
149 
150  /**
151  * @param void
152  * @return void
153  */
154  void RemoveCurlSock ( void );
155 
156  /**
157  * @param trace - true to print curl store data, otherwise false.
158  * @return void
159  */
160  void ShowCurlStoreData ( bool trace = true );
161 
162  /**
163  * @param[out] privContext - priv aamp instance in which created curl handles will be assigned
164  * @param[in] startIdx - Index of Curl instance
165  * @param[in] instanceCount - No of curl handles
166  * @param[in] proxyName - proxy name
167  * @return void
168  */
169  void CurlInit(void *privContext, AampCurlInstance startIdx, unsigned int instanceCount, std::string proxyName, const std::string &remotehost=std::string("") );
170 
171  /**
172  * @param[out] privContext - priv aamp instance from which curl handles will be terminated or stored
173  * @param[in] startIdx - Index of Curl instance
174  * @param[in] instanceCount - No of curl handles
175  * @return void
176  */
177  void CurlTerm(void *privContext, AampCurlInstance startIdx, unsigned int instanceCount, const std::string &remotehost=std::string(""));
178 
179  /**
180  * @param[in] pAamp - Private aamp instance
181  * @param[in] url - request url
182  * @param[in] startIdx - Index of curl instance.
183  * @return - curl easy handle
184  */
185  CURL* GetCurlHandle(void *pAamp, std::string url, AampCurlInstance startIdx );
186 
187  /**
188  * @param[in] pAamp - Private aamp instance
189  * @param[in] url - request url
190  * @param[in] startIdx - Index of curl instance.
191  * @param[in] curl - curl handle to be saved
192  * @return void
193  */
194  void SaveCurlHandle ( void *pAamp, std::string url, AampCurlInstance startIdx, CURL *curl );
195 
196  /**
197  * @param[in] hostname - Host name to create a curl store
198  * @return - Curl store struct pointer
199  */
200  CurlSocketStoreStruct *CreateCurlStore ( const std::string &hostname );
201 
202  /**
203  * @param[in] privContext - Aamp context
204  * @param[in] proxyName - Network proxy Name
205  * @param[in] instId - Curl instance id
206  * @return - Curl easy handle
207  */
208  CURL* CurlEasyInitWithOpt ( void *privContext, const std::string &proxyName, int instId );
209 
210  /**
211  * @param[in] CurlSock - Curl socket struct
212  * @param[in] instId - Curl instance id
213  * @return - Curl easy handle
214  */
215  CURL* GetCurlHandleFromFreeQ ( CurlSocketStoreStruct *CurlSock, int instId );
216 
217  // Copy constructor and Copy assignment disabled
218  CurlStore(const CurlStore&) = delete;
219  CurlStore& operator=(const CurlStore&) = delete;
220 
221  /**
222  * @param[in] pContext - Private aamp instance
223  * @return CurlStore - Singleton instance object
224  */
225  static CurlStore *GetCurlStoreInstance(void* pContext);
226 };
227 
228 /**
229  * @struct CurlCallbackContext
230  * @brief context during curl callbacks
231  */
233 {
234  PrivateInstanceAAMP *aamp;
235  MediaType fileType;
236  std::vector<std::string> allResponseHeadersForErrorLogging;
237  GrowableBuffer *buffer;
238  httpRespHeaderData *responseHeaderData;
239  long bitrate;
240  bool downloadIsEncoded;
241  //represents transfer-encoding based download
242  bool chunkedDownload;
243  std::string remoteUrl;
244  size_t contentLength;
245  long long downloadStartTime;
246 
247  CurlCallbackContext() : aamp(NULL), buffer(NULL), responseHeaderData(NULL),bitrate(0),downloadIsEncoded(false), chunkedDownload(false), fileType(eMEDIATYPE_DEFAULT), remoteUrl(""), allResponseHeadersForErrorLogging{""}, contentLength(0),downloadStartTime(-1)
248  {
249 
250  }
251  CurlCallbackContext(PrivateInstanceAAMP *_aamp, GrowableBuffer *_buffer) : aamp(_aamp), buffer(_buffer), responseHeaderData(NULL),bitrate(0),downloadIsEncoded(false), chunkedDownload(false), fileType(eMEDIATYPE_DEFAULT), remoteUrl(""), allResponseHeadersForErrorLogging{""}, contentLength(0),downloadStartTime(-1){}
252 
253  ~CurlCallbackContext() {}
254 
255  CurlCallbackContext(const CurlCallbackContext &other) = delete;
256  CurlCallbackContext& operator=(const CurlCallbackContext& other) = delete;
257 };
258 
259 /**
260  * @struct CurlProgressCbContext
261  * @brief context during curl progress callbacks
262  */
264 {
265  PrivateInstanceAAMP *aamp;
266  MediaType fileType;
267  CurlProgressCbContext() : aamp(NULL), fileType(eMEDIATYPE_DEFAULT), downloadStartTime(-1), abortReason(eCURL_ABORT_REASON_NONE), downloadUpdatedTime(-1), startTimeout(-1), stallTimeout(-1), downloadSize(-1), downloadNow(-1), downloadNowUpdatedTime(-1), dlStarted(false), fragmentDurationMs(-1), remoteUrl(""), lowBWTimeout(-1) {}
268  CurlProgressCbContext(PrivateInstanceAAMP *_aamp, long long _downloadStartTime) : aamp(_aamp), fileType(eMEDIATYPE_DEFAULT),downloadStartTime(_downloadStartTime), abortReason(eCURL_ABORT_REASON_NONE), downloadUpdatedTime(-1), startTimeout(-1), stallTimeout(-1), downloadSize(-1), downloadNow(-1), downloadNowUpdatedTime(-1), dlStarted(false), fragmentDurationMs(-1), remoteUrl(""), lowBWTimeout(-1) {}
269 
271 
272  CurlProgressCbContext(const CurlProgressCbContext &other) = delete;
273  CurlProgressCbContext& operator=(const CurlProgressCbContext& other) = delete;
274 
275  long long downloadStartTime;
276  long long downloadUpdatedTime;
277  long startTimeout;
278  long stallTimeout;
279  long lowBWTimeout;
280  double downloadSize;
281  CurlAbortReason abortReason;
282  double downloadNow;
283  long long downloadNowUpdatedTime;
284  bool dlStarted;
285  int fragmentDurationMs;
286  std::string remoteUrl;
287 };
288 
289 #endif //AAMPCURLSTORE_H
CurlProgressCbContext
context during curl progress callbacks
Definition: AampCurlStore.h:263
curlstruct
Definition: AampCurlStore.h:65
CurlStore::CurlTerm
void CurlTerm(void *privContext, AampCurlInstance startIdx, unsigned int instanceCount, const std::string &remotehost=std::string(""))
Definition: AampCurlStore.cpp:468
CurlStore::CurlInit
void CurlInit(void *privContext, AampCurlInstance startIdx, unsigned int instanceCount, std::string proxyName, const std::string &remotehost=std::string(""))
Definition: AampCurlStore.cpp:393
httpRespHeaderData
To store Set Cookie: headers and X-Reason headers in HTTP Response.
Definition: priv_aamp.h:462
CurlStore::GetFromCurlStore
AampCurlStoreErrorCode GetFromCurlStore(const std::string &hostname, AampCurlInstance CurlIndex, CURL **curl)
Definition: AampCurlStore.cpp:669
curldatasharelock
locks used when lock/unlock callback occurs for different shared data
Definition: AampCurlStore.h:51
CurlStore::MaxCurlSockStore
static int MaxCurlSockStore
Definition: AampCurlStore.h:105
CurlStore::CurlEasyInitWithOpt
CURL * CurlEasyInitWithOpt(void *privContext, const std::string &proxyName, int instId)
Definition: AampCurlStore.cpp:323
eMEDIATYPE_DEFAULT
@ eMEDIATYPE_DEFAULT
Definition: AampMediaType.h:58
CurlStore::GetCurlHandleFromFreeQ
CURL * GetCurlHandleFromFreeQ(CurlSocketStoreStruct *CurlSock, int instId)
Definition: AampCurlStore.cpp:545
CurlStore::RemoveCurlSock
void RemoveCurlSock(void)
Definition: AampCurlStore.cpp:812
CurlCallbackContext
context during curl callbacks
Definition: AampCurlStore.h:232
MediaType
MediaType
Media types.
Definition: AampMediaType.h:37
CurlStore::CreateCurlStore
CurlSocketStoreStruct * CreateCurlStore(const std::string &hostname)
Definition: AampCurlStore.cpp:238
CurlStore::KeepInCurlStoreBulk
void KeepInCurlStoreBulk(const std::string &hostname, AampCurlInstance CurlIndex, int count, void *priv, bool HostCurlFd)
Definition: AampCurlStore.cpp:729
priv_aamp.h
Private functions and types used internally by AAMP.
CurlStore::GetCurlHandle
CURL * GetCurlHandle(void *pAamp, std::string url, AampCurlInstance startIdx)
Definition: AampCurlStore.cpp:276
AampCurlInstance
AampCurlInstance
Enumeration for Curl Instances.
Definition: priv_aamp.h:156
GrowableBuffer
Structure of GrowableBuffer.
Definition: AampMemoryUtils.h:39
PrivateInstanceAAMP
Class representing the AAMP player's private instance, which is not exposed to outside world.
Definition: priv_aamp.h:640
CurlStore::GetCurlStoreInstance
static CurlStore * GetCurlStoreInstance(void *pContext)
Definition: AampCurlStore.cpp:514
CurlStore::KeepInCurlStore
void KeepInCurlStore(const std::string &hostname, AampCurlInstance CurlIndex, CURL *curl)
Definition: AampCurlStore.cpp:782
CurlStore::GetFromCurlStoreBulk
AampCurlStoreErrorCode GetFromCurlStoreBulk(const std::string &hostname, AampCurlInstance CurlIndex, int count, void *priv, bool HostCurlFd)
Definition: AampCurlStore.cpp:590
AampCurlStoreErrorCode
AampCurlStoreErrorCode
Error codes returned by curlstore.
Definition: AampCurlStore.h:40
CurlStore::ShowCurlStoreData
void ShowCurlStoreData(bool trace=true)
Definition: AampCurlStore.cpp:873
CurlAbortReason
CurlAbortReason
Http Header Type.
Definition: priv_aamp.h:240
CurlStore
Singleton curlstore to save/reuse curl handles.
Definition: AampCurlStore.h:100
CurlStore::SaveCurlHandle
void SaveCurlHandle(void *pAamp, std::string url, AampCurlInstance startIdx, CURL *curl)
Definition: AampCurlStore.cpp:301
curlstorestruct
structure to store curl easy, shared handle & locks for a host
Definition: AampCurlStore.h:78