This repository was archived by the owner on Jan 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathAampCurlStore.h
289 lines (247 loc) · 9.36 KB
/
AampCurlStore.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/*
* If not stated otherwise in this file or this component's license file the
* following copyright and licenses apply:
*
* Copyright 2022 RDK Management
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @file AampCurlStore.h
* @brief Advanced Adaptive Media Player (AAMP) Curl store
*/
#ifndef AAMPCURLSTORE_H
#define AAMPCURLSTORE_H
#include "priv_aamp.h"
#include <map>
#include <iterator>
#include <vector>
#include <glib.h>
#define eCURL_MAX_AGE_TIME ( (300) * (1000) ) /**< 5 mins - 300 secs - Max age for a connection */
/**
* @enum AampCurlStoreErrorCode
* @brief Error codes returned by curlstore
*/
enum AampCurlStoreErrorCode
{
eCURL_STORE_HOST_NOT_AVAILABLE,
eCURL_STORE_SOCK_NOT_AVAILABLE,
eCURL_STORE_HOST_SOCK_AVAILABLE
};
/**
* @struct curldatasharelock
* @brief locks used when lock/unlock callback occurs for different shared data
*/
typedef struct curldatasharelock
{
pthread_mutex_t mCurlSharedlock;
pthread_mutex_t mDnsCurlShareMutex;
pthread_mutex_t mSslCurlShareMutex;
curldatasharelock():mCurlSharedlock(), mDnsCurlShareMutex(),mSslCurlShareMutex()
{
pthread_mutex_init(&mCurlSharedlock, NULL);
pthread_mutex_init(&mDnsCurlShareMutex, NULL);
pthread_mutex_init(&mSslCurlShareMutex, NULL);
}
}CurlDataShareLock;
typedef struct curlstruct
{
CURL *curl;
int curlId;
long long eHdlTimestamp;
curlstruct():curl(NULL), eHdlTimestamp(0),curlId(0){}
}CurlHandleStruct;
/**
* @struct curlstorestruct
* @brief structure to store curl easy, shared handle & locks for a host
*/
typedef struct curlstorestruct
{
std::deque<CurlHandleStruct> mFreeQ;
CURLSH* mCurlShared;
CurlDataShareLock *pstShareLocks;
unsigned int mCurlStoreUserCount;
long long timestamp;
curlstorestruct():mCurlShared(NULL), pstShareLocks(NULL), timestamp(0), mCurlStoreUserCount(0), mFreeQ()
{}
//Disabled for now
curlstorestruct(const curlstorestruct&) = delete;
curlstorestruct& operator=(const curlstorestruct&) = delete;
}CurlSocketStoreStruct;
/**
* @class CurlStore
* @brief Singleton curlstore to save/reuse curl handles
*/
class CurlStore
{
private:
static CurlStore *CurlInstance;
static pthread_mutex_t mCurlInstLock;
static int MaxCurlSockStore;
typedef std::unordered_map <std::string, CurlSocketStoreStruct*> CurlSockData ;
typedef std::unordered_map <std::string, CurlSocketStoreStruct*>::iterator CurlSockDataIter;
CurlSockData umCurlSockDataStore;
protected:
CurlStore( ):umCurlSockDataStore(){}
~CurlStore( ){}
public:
/**
* @param[in] hostname - hostname part from url
* @param[in] CurlIndex - Index of Curl instance
* @param[out] curl - curl easy handle from curl store.
* @return AampCurlStoreErrorCode enum type
*/
AampCurlStoreErrorCode GetFromCurlStore ( const std::string &hostname, AampCurlInstance CurlIndex, CURL **curl );
/**
* @param[in] hostname - hostname part from url
* @param[in] CurlIndex - Index of Curl instance
* @param[in] count - No of curl handles
* @param[out] priv - curl easy handle from curl store will get stored in priv instance
* @return AampCurlStoreErrorCode enum type
*/
AampCurlStoreErrorCode GetFromCurlStoreBulk ( const std::string &hostname, AampCurlInstance CurlIndex, int count, void *priv, bool HostCurlFd );
/**
* @param[in] hostname - hostname part from url
* @param[in] CurlIndex - Index of Curl instance
* @param[in] curl - curl easy handle to save in curl store.
* @return void
*/
void KeepInCurlStore ( const std::string &hostname, AampCurlInstance CurlIndex, CURL *curl );
/**
* @param[in] hostname - hostname part from url
* @param[in] CurlIndex - Index of Curl instance
* @param[in] count - No of curl handles
* @param[out] priv - curl easy handles in priv instance, saved in curl store
* @return void
*/
void KeepInCurlStoreBulk ( const std::string &hostname, AampCurlInstance CurlIndex, int count, void *priv, bool HostCurlFd );
/**
* @param void
* @return void
*/
void RemoveCurlSock ( void );
/**
* @param trace - true to print curl store data, otherwise false.
* @return void
*/
void ShowCurlStoreData ( bool trace = true );
/**
* @param[out] privContext - priv aamp instance in which created curl handles will be assigned
* @param[in] startIdx - Index of Curl instance
* @param[in] instanceCount - No of curl handles
* @param[in] proxyName - proxy name
* @return void
*/
void CurlInit(void *privContext, AampCurlInstance startIdx, unsigned int instanceCount, std::string proxyName, const std::string &remotehost=std::string("") );
/**
* @param[out] privContext - priv aamp instance from which curl handles will be terminated or stored
* @param[in] startIdx - Index of Curl instance
* @param[in] instanceCount - No of curl handles
* @return void
*/
void CurlTerm(void *privContext, AampCurlInstance startIdx, unsigned int instanceCount, const std::string &remotehost=std::string(""));
/**
* @param[in] pAamp - Private aamp instance
* @param[in] url - request url
* @param[in] startIdx - Index of curl instance.
* @return - curl easy handle
*/
CURL* GetCurlHandle(void *pAamp, std::string url, AampCurlInstance startIdx );
/**
* @param[in] pAamp - Private aamp instance
* @param[in] url - request url
* @param[in] startIdx - Index of curl instance.
* @param[in] curl - curl handle to be saved
* @return void
*/
void SaveCurlHandle ( void *pAamp, std::string url, AampCurlInstance startIdx, CURL *curl );
/**
* @param[in] hostname - Host name to create a curl store
* @return - Curl store struct pointer
*/
CurlSocketStoreStruct *CreateCurlStore ( const std::string &hostname );
/**
* @param[in] privContext - Aamp context
* @param[in] proxyName - Network proxy Name
* @param[in] instId - Curl instance id
* @return - Curl easy handle
*/
CURL* CurlEasyInitWithOpt ( void *privContext, const std::string &proxyName, int instId );
/**
* @param[in] CurlSock - Curl socket struct
* @param[in] instId - Curl instance id
* @return - Curl easy handle
*/
CURL* GetCurlHandleFromFreeQ ( CurlSocketStoreStruct *CurlSock, int instId );
// Copy constructor and Copy assignment disabled
CurlStore(const CurlStore&) = delete;
CurlStore& operator=(const CurlStore&) = delete;
/**
* @param[in] pContext - Private aamp instance
* @return CurlStore - Singleton instance object
*/
static CurlStore *GetCurlStoreInstance(void* pContext);
};
/**
* @struct CurlCallbackContext
* @brief context during curl callbacks
*/
struct CurlCallbackContext
{
PrivateInstanceAAMP *aamp;
MediaType fileType;
std::vector<std::string> allResponseHeadersForErrorLogging;
GrowableBuffer *buffer;
httpRespHeaderData *responseHeaderData;
long bitrate;
bool downloadIsEncoded;
//represents transfer-encoding based download
bool chunkedDownload;
std::string remoteUrl;
size_t contentLength;
long long downloadStartTime;
CurlCallbackContext() : aamp(NULL), buffer(NULL), responseHeaderData(NULL),bitrate(0),downloadIsEncoded(false), chunkedDownload(false), fileType(eMEDIATYPE_DEFAULT), remoteUrl(""), allResponseHeadersForErrorLogging{""}, contentLength(0),downloadStartTime(-1)
{
}
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){}
~CurlCallbackContext() {}
CurlCallbackContext(const CurlCallbackContext &other) = delete;
CurlCallbackContext& operator=(const CurlCallbackContext& other) = delete;
};
/**
* @struct CurlProgressCbContext
* @brief context during curl progress callbacks
*/
struct CurlProgressCbContext
{
PrivateInstanceAAMP *aamp;
MediaType fileType;
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) {}
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) {}
~CurlProgressCbContext() {}
CurlProgressCbContext(const CurlProgressCbContext &other) = delete;
CurlProgressCbContext& operator=(const CurlProgressCbContext& other) = delete;
long long downloadStartTime;
long long downloadUpdatedTime;
long startTimeout;
long stallTimeout;
long lowBWTimeout;
double downloadSize;
CurlAbortReason abortReason;
double downloadNow;
long long downloadNowUpdatedTime;
bool dlStarted;
int fragmentDurationMs;
std::string remoteUrl;
};
#endif //AAMPCURLSTORE_H