RDK Documentation (Open Sourced RDK Components)
AampUtils.h
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 AampDRMutils.h
23 * @brief Context-free common utility functions.
24 */
25 
26 #ifndef __AAMP_UTILS_H__
27 #define __AAMP_UTILS_H__
28 
29 #include "AampDrmSystems.h"
30 #include "main_aamp.h"
31 #include "iso639map.h"
32 
33 #include <string>
34 #include <sstream>
35 #include <chrono>
36 
37 #define NOW_SYSTEM_TS_MS std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count() /**< Getting current system clock in milliseconds */
38 #define NOW_STEADY_TS_MS std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now().time_since_epoch()).count() /**< Getting current steady clock in milliseconds */
39 
40 #define ARRAY_SIZE(A) (sizeof(A)/sizeof(A[0]))
41 
42 //Delete non-array object
43 #define SAFE_DELETE(ptr) { delete(ptr); ptr = NULL; }
44 //Delete Array object
45 #define SAFE_DELETE_ARRAY(ptr) { delete [] ptr; ptr = NULL; }
46 
47 /** FHD height*/
48 #define AAMP_FHD_HEIGHT (1080)
49 
50 /**
51 * @struct FormatMap
52 * @brief FormatMap structure for stream codec/format information
53 */
54 struct FormatMap
55 {
56  const char* codec;
57  StreamOutputFormat format;
58 };
59 
60 /*
61 * @fn GetAudioFormatStringForCodec
62 * @brief Function to get audio codec string from the map.
63 *
64 * @param[in] input Audio codec type
65 * @return Audio codec string
66 */
67 const char * GetAudioFormatStringForCodec ( StreamOutputFormat input);
68 
69 /*
70 * @fn GetAudioFormatForCodec
71 * @brief Function to get audio codec from the map.
72 *
73 * @param[in] Audio codec string
74 * @return Audio codec map
75 */
76 const FormatMap * GetAudioFormatForCodec( const char *codecs );
77 
78 /*
79 * @fn GetVideoFormatForCodec
80 * @brief Function to get video codec from the map.
81 *
82 * @param[in] Video codec string
83 * @return Video codec map
84 */
85 const FormatMap * GetVideoFormatForCodec( const char *codecs );
86 
87 /**
88  * @fn aamp_GetCurrentTimeMS
89  *
90  */
91 long long aamp_GetCurrentTimeMS(void); //TODO: Use NOW_STEADY_TS_MS/NOW_SYSTEM_TS_MS instead
92 
93 /**
94  * @fn aamp_IsAbsoluteURL
95  *
96  * @param[in] url - Input URL
97  */
98  bool aamp_IsAbsoluteURL( const std::string &url );
99 
100 /**
101  * @fn aamp_getHostFromURL
102  *
103  * @param[in] url - Input URL
104  * @retval host of input url
105  */
106 std::string aamp_getHostFromURL(std::string url);
107 
108 /**
109  * @fn aamp_IsLocalHost
110  *
111  * @param[in] Hostname - Hostname parsed from url
112  * @retval true if localhost false otherwise.
113  */
114 bool aamp_IsLocalHost ( std::string Hostname );
115 
116 /**
117  * @fn aamp_ResolveURL
118  *
119  * @param[out] dst - Created URL
120  * @param[in] base - Base URL
121  * @param[in] uri - File path
122  * @param[in] bPropagateUriParams - flag to use base uri params
123  * @retval void
124  */
125 void aamp_ResolveURL(std::string& dst, std::string base, const char *uri , bool bPropagateUriParams);
126 
127 /**
128  * @fn aamp_StartsWith
129  *
130  * @param[in] inputStr - Input string
131  * @param[in] prefix - substring to be searched
132  */
133 bool aamp_StartsWith( const char *inputStr, const char *prefix);
134 
135 /**
136  * @fn aamp_Base64_URL_Encode
137  * @param src pointer to first byte of binary data to be encoded
138  * @param len number of bytes to encode
139  */
140 char *aamp_Base64_URL_Encode(const unsigned char *src, size_t len);
141 
142 /**
143  * @fn aamp_Base64_URL_Decode
144  * @param src pointer to cstring containing base64-URL-encoded data
145  * @param len receives byte length of returned pointer, or zero upon failure
146  * @param srcLen source data length
147  */
148 unsigned char *aamp_Base64_URL_Decode(const char *src, size_t *len, size_t srcLen);
149 
150 /**
151  * @brief unescape uri-encoded uri parameter
152  * @param uriParam string to un-escape
153  */
154 void aamp_DecodeUrlParameter( std::string &uriParam );
155 /**
156  * @fn ISO8601DateTimeToUTCSeconds
157  * @param ptr ISO8601 string
158  */
159 double ISO8601DateTimeToUTCSeconds(const char *ptr);
160 
161 /**
162  * @fn aamp_PostJsonRPC
163  * @param[in] id string containing player id
164  * @param[in] method string containing JSON method
165  * @param[in] params string containing params:value pair list
166  */
167 std::string aamp_PostJsonRPC( std::string id, std::string method, std::string params );
168 
169 /**
170  * @fn aamp_GetDeferTimeMs
171  *
172  * @param maxTimeSeconds Maximum time allowed for deferred license acquisition
173  */
174 int aamp_GetDeferTimeMs(long maxTimeSeconds);
175 
176 /**
177  * @fn GetDrmSystemName
178  * @param drmSystem drm system
179  */
180 const char * GetDrmSystemName(DRMSystems drmSystem);
181 
182 /**
183  * @fn GetDrmSystem
184  * @param drmSystemID - Id of the DRM system, empty string if not supported
185  */
186 DRMSystems GetDrmSystem(std::string drmSystemID);
187 
188 /**
189  * @fn GetDrmSystemID
190  * @param drmSystem - drm system
191  */
192 const char * GetDrmSystemID(DRMSystems drmSystem);
193 
194 /**
195  * @fn UrlEncode
196  *
197  * @param[in] inStr - Input URL
198  * @param[out] outStr - Encoded URL
199  */
200 void UrlEncode(std::string inStr, std::string &outStr);
201 
202 /**
203  * @fn trim
204  * @param[in][out] src Buffer containing string
205  */
206 void trim(std::string& src);
207 
208 /**
209  * @fn Getiso639map_NormalizeLanguageCode
210  * @param[in] lang - Language in string format
211  * @param[in] preferFormat - Preferred language foramt
212  */
213 std::string Getiso639map_NormalizeLanguageCode(std::string lang, LangCodePreference preferFormat );
214 
215 /**
216  * @fn aamp_GetTimespec
217  * @param[in] timeInMs
218  */
219 struct timespec aamp_GetTimespec(int timeInMs);
220 
221 /**
222  * @fn aamp_WriteFile
223  * @param fileName - out file name
224  * @param data - buffer
225  * @param len - length of buffer
226  * @param fileType - Media type of file
227  * @param count - for manifest or playlist update
228  * @param prefix - prefix name
229  */
230 bool aamp_WriteFile(std::string fileName, const char* data, size_t len, MediaType &fileType, unsigned int count,const char *prefix);
231 /**
232  * @fn getHarvestConfigForMedia
233  * @param fileType - meida file type
234  */
235 int getHarvestConfigForMedia(MediaType fileType);
236 /**
237  * @fn getWorkingTrickplayRate
238  * @param rate input rate
239  */
240 float getWorkingTrickplayRate(float rate);
241 
242 /**
243  * @fn getPseudoTrickplayRate
244  * @param rate working rate
245  */
246 float getPseudoTrickplayRate(float rate);
247 
248 /**
249  * @fn getDefaultHarvestPath
250  * @param[in] value - harvest path
251  * @return void
252  */
253 
254 void getDefaultHarvestPath(std::string &value);
255 
256 /**
257  * @fn stream2hex
258  * @param[in] str input string
259  * @param[out] hexstr output hex string
260  * @param[in] capital - Boolean to enable capital letter conversion flag
261  */
262 void stream2hex(const std::string str, std::string& hexstr, bool capital = false);
263 
264 /**
265  * @fn mssleep
266  * @param milliseconds Time to sleep
267  */
268 void mssleep(int milliseconds);
269 
270 #define MAX_RANGE_STRING_CHARS 128
271 
272 #define WRITE_HASCII( DST, BYTE ) \
273 { \
274  *DST++ = "0123456789abcdef"[BYTE>>4]; \
275  *DST++ = "0123456789abcdef"[BYTE&0xf]; \
276 }
277 
278 #endif /* __AAMP_UTILS_H__ */
StreamOutputFormat
StreamOutputFormat
Media output format.
Definition: main_aamp.h:106
iso639map.h
ISO639 is a standard with representation of names for languages.
aamp_PostJsonRPC
std::string aamp_PostJsonRPC(std::string id, std::string method, std::string params)
aamp_PostJsonRPC posts JSONRPC data
Definition: AampUtils.cpp:422
main_aamp.h
Types and APIs exposed by the AAMP player.
UrlEncode
void UrlEncode(std::string inStr, std::string &outStr)
Encode URL.
Definition: AampUtils.cpp:678
AampDrmSystems.h
Define DRM types.
Getiso639map_NormalizeLanguageCode
std::string Getiso639map_NormalizeLanguageCode(std::string lang, LangCodePreference preferLangFormat)
To get the preferred iso639mapped language code.
Definition: AampUtils.cpp:725
getHarvestConfigForMedia
int getHarvestConfigForMedia(MediaType fileType)
Get harvest config corresponds to Media type.
Definition: AampUtils.cpp:807
aamp_StartsWith
bool aamp_StartsWith(const char *inputStr, const char *prefix)
Check if string start with a prefix.
Definition: AampUtils.cpp:284
aamp_DecodeUrlParameter
void aamp_DecodeUrlParameter(std::string &uriParam)
unescape uri-encoded uri parameter
Definition: AampUtils.cpp:366
GetDrmSystemName
const char * GetDrmSystemName(DRMSystems drmSystem)
Get name of DRM system.
Definition: AampUtils.cpp:625
aamp_WriteFile
bool aamp_WriteFile(std::string fileName, const char *data, size_t len, MediaType &fileType, unsigned int count, const char *prefix)
Write - file to storage.
Definition: AampUtils.cpp:882
MediaType
MediaType
Media types.
Definition: AampMediaType.h:37
aamp_IsLocalHost
bool aamp_IsLocalHost(std::string Hostname)
check is local or not from given hostname
Definition: AampUtils.cpp:267
stream2hex
void stream2hex(const std::string str, std::string &hexstr, bool capital)
Convert string of chars to its representative string of hex numbers.
Definition: AampUtils.cpp:1006
aamp_GetTimespec
struct timespec aamp_GetTimespec(int timeInMs)
To get the timespec.
Definition: AampUtils.cpp:741
trim
void trim(std::string &src)
Trim a string.
Definition: AampUtils.cpp:710
getWorkingTrickplayRate
float getWorkingTrickplayRate(float rate)
Get compatible trickplay for 6s cadense of iframe track from the given rates.
Definition: AampUtils.cpp:944
getPseudoTrickplayRate
float getPseudoTrickplayRate(float rate)
Get reverse map the working rates to the rates given by platform player.
Definition: AampUtils.cpp:975
aamp_GetDeferTimeMs
int aamp_GetDeferTimeMs(long maxTimeSeconds)
Get time to defer DRM acquisition.
Definition: AampUtils.cpp:539
aamp_getHostFromURL
std::string aamp_getHostFromURL(std::string url)
Extract host string from url.
Definition: AampUtils.cpp:232
aamp_Base64_URL_Decode
unsigned char * aamp_Base64_URL_Decode(const char *src, size_t *len, size_t srcLen)
decode base64 URL encoded data to binary equivalent
Definition: AampUtils.cpp:340
getDefaultHarvestPath
void getDefaultHarvestPath(std::string &value)
Get harvest path to dump the files.
Definition: AampUtils.cpp:102
ISO8601DateTimeToUTCSeconds
double ISO8601DateTimeToUTCSeconds(const char *ptr)
Parse date time from ISO8601 string and return value in seconds.
Definition: AampUtils.cpp:387
aamp_Base64_URL_Encode
char * aamp_Base64_URL_Encode(const unsigned char *src, size_t len)
convert blob of binary data to ascii base64-URL-encoded equivalent
Definition: AampUtils.cpp:304
GetDrmSystemID
const char * GetDrmSystemID(DRMSystems drmSystem)
Get ID of DRM system.
Definition: AampUtils.cpp:649
aamp_GetCurrentTimeMS
long long aamp_GetCurrentTimeMS(void)
Get current time from epoch is milliseconds.
Definition: AampUtils.cpp:92
aamp_ResolveURL
void aamp_ResolveURL(std::string &dst, std::string base, const char *uri, bool bPropagateUriParams)
Resolve file URL from the base and file path.
Definition: AampUtils.cpp:157
DRMSystems
DRMSystems
DRM system types.
Definition: AampDrmSystems.h:33
mssleep
void mssleep(int milliseconds)
Sleep for given milliseconds.
Definition: AampUtils.cpp:1021
FormatMap
FormatMap structure for stream codec/format information.
Definition: AampUtils.h:54
LangCodePreference
LangCodePreference
Language Code Preference types.
Definition: main_aamp.h:165
aamp_IsAbsoluteURL
bool aamp_IsAbsoluteURL(const std::string &url)
distinguish between absolute and relative urls
Definition: AampUtils.cpp:221
GetDrmSystem
DRMSystems GetDrmSystem(std::string drmSystemID)
Get DRM system from ID.
Definition: AampUtils.cpp:601