RDK Documentation (Open Sourced RDK Components)
jsutils.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  * @file jsutils.h
21  * @brief JavaScript util functions for AAMP
22  */
23 
24 #ifndef __AAMP_JSUTILS_H__
25 #define __AAMP_JSUTILS_H__
26 
27 #include "main_aamp.h"
28 #include "AampUtils.h"
29 
30 #include <JavaScriptCore/JavaScript.h>
31 #include <stdio.h>
32 #include <string.h>
33 #include <vector>
34 
35 
36 #define PLAYER_ID_NA -5
37 
38 /**Defining LOG macros for logging with Playerid in consideration**/
39 #define LOG_INFO(AAMP_JS_OBJECT,FORMAT, ...) if(AAMP_JS_OBJECT && AAMP_JS_OBJECT->bInfoEnabled) { jsBindingLogprintf(( (NULL != AAMP_JS_OBJECT)? AAMP_JS_OBJECT->iPlayerId : PLAYER_ID_NA ),"INFO",__FUNCTION__,__LINE__,FORMAT, ##__VA_ARGS__); }
40 #define LOG_ERROR(AAMP_JS_OBJECT,FORMAT, ...) jsBindingLogprintf(( (NULL != AAMP_JS_OBJECT)?AAMP_JS_OBJECT->iPlayerId : PLAYER_ID_NA ),"ERR",__FUNCTION__,__LINE__,FORMAT, ##__VA_ARGS__)
41 #define LOG_WARN(AAMP_JS_OBJECT,FORMAT, ...) jsBindingLogprintf(((NULL != AAMP_JS_OBJECT)?AAMP_JS_OBJECT->iPlayerId : PLAYER_ID_NA ),"WARN",__FUNCTION__,__LINE__,FORMAT, ##__VA_ARGS__)
42 
43 /**Defining LOG macros where AAMP-JS object is not relevant**/
44 #define LOG_WARN_EX(FORMAT, ...) jsBindingLogprintf(PLAYER_ID_NA,"WARN",__FUNCTION__,__LINE__,FORMAT, ##__VA_ARGS__)
45 #define LOG_ERROR_EX(FORMAT, ...) jsBindingLogprintf(PLAYER_ID_NA,"ERR",__FUNCTION__,__LINE__,FORMAT, ##__VA_ARGS__)
46 
47 #ifdef TRACE
48 #define LOG_TRACE(FORMAT, ...) jsBindingLogprintf(PLAYER_ID_NA,"TRACE",__FUNCTION__,__LINE__,FORMAT, ##__VA_ARGS__)
49 
50 #else
51 #define LOG_TRACE(FORMAT, ...)
52 #endif
53 
54 #define EXCEPTION_ERR_MSG_MAX_LEN 1024
55 
56 #ifndef MUTE_SUBTITLES_TRACKID
57 #define MUTE_SUBTITLES_TRACKID (-1) /* match priv_aamp.h */
58 #endif
59 
60 
61 /**
62  * @enum ErrorCode
63  * @brief JavaScript error codes
64  */
66 {
67  AAMPJS_INVALID_ARGUMENT = -1,
68  AAMPJS_MISSING_OBJECT = -2,
69  AAMPJS_GENERIC_ERROR = -3
70 };
71 
72 /**
73  * @fn aamp_CStringToJSValue
74  * @param[in] context JS execution context
75  * @param[in] sz C string
76  * @retval JSValue that is the converted JSString
77  */
78 JSValueRef aamp_CStringToJSValue(JSContextRef context, const char* sz);
79 
80 /**
81  * @fn aamp_JSValueToCString
82  * @param[in] context JS execution context
83  * @param[in] value JSValue of JSString to be converted
84  * @param[out] exception pointer to a JSValueRef in which to store an exception, if any
85  * @retval converted C string
86  */
87 char* aamp_JSValueToCString(JSContextRef context, JSValueRef value, JSValueRef* exception);
88 
89 /**
90  * @fn aamp_JSValueToJSONCString
91  * @param[in] context JS execution context
92  * @param[in] value JSValue of JSString to be converted
93  * @param[out] exception pointer to a JSValueRef in which to store an exception, if any
94  * @retval converted C string
95  */
96 char* aamp_JSValueToJSONCString(JSContextRef context, JSValueRef value, JSValueRef* exception);
97 
98 /**
99  * @fn aamp_JSValueIsArray
100  * @param[in] context JS exception context
101  * @param[in] value JSValue to check if array or not
102  * @retval true if JSValue is an array
103  */
104 bool aamp_JSValueIsArray(JSContextRef context, JSValueRef value);
105 
106 /**
107  * @fn aamp_StringArrayToCStringArray
108  * @param[in] context JS execution context
109  * @param[in] arrayRef JSValue of an array of JSString
110  * @retval converted array of C strings
111  */
112 std::vector<std::string> aamp_StringArrayToCStringArray(JSContextRef context, JSValueRef arrayRef);
113 
114 /**
115  * @fn aamp_GetException
116  * @param[in] context JS exception context
117  * @param[in] error error/exception code
118  * @param[in] additionalInfo additional error description
119  * @retval JSValue object with exception details
120  */
121 JSValueRef aamp_GetException(JSContextRef context, ErrorCode error, const char *additionalInfo);
122 
123 /**
124  * @fn aamp_getEventTypeFromName
125  * @param[in] szName JS event name
126  * @retval AAMPEventType of corresponding AAMP event
127  */
128 AAMPEventType aamp_getEventTypeFromName(const char* szName);
129 
130 /**
131  * @fn aamp_dispatchEventToJS
132  * @param[in] context JS execution context
133  * @param[in] callback function to which event has to be dispatched as an arg
134  * @param[in] event the JS event to be dispatched
135  */
136 void aamp_dispatchEventToJS(JSContextRef context, JSObjectRef callback, JSObjectRef event);
137 
138 /**
139  * @fn aampPlayer_getEventTypeFromName
140  * @param[in] szName JS event name
141  * @retval AAMPEventType of corresponding AAMP event
142  */
144 
145 /**
146  * @fn aampPlayer_getNameFromEventType
147  * @param[in] type AAMP event type
148  * @retval JS event name corresponding to AAMP event
149  */
151 
152 /**
153  * @fn aamp_CreateTimedMetadataJSObject
154  * @param[in] context JS execution context
155  * @param[in] timeMS time in milliseconds, mostly metadata position in playlist
156  * @param[in] szName name of the metadata tag
157  * @param[in] szContent metadata associated with the tag
158  * @param[in] id adbreak/reservation ID if its a adbreak metadata
159  * @param[in] durationMS duration of ad break if its a adbreak metadata
160  * @retval JSObject of TimedMetadata generated
161  */
162 JSObjectRef aamp_CreateTimedMetadataJSObject(JSContextRef context, long long timeMS, const char* szName, const char* szContent, const char* id, double durationMS);
163 
164 
165 void jsBindingLogprintf(int playerId,const char* levelstr,const char* functionName, int line,const char *format, ...);
166 
167 #endif /* __AAMP_JSUTILS_H__ */
main_aamp.h
Types and APIs exposed by the AAMP player.
aampPlayer_getNameFromEventType
const char * aampPlayer_getNameFromEventType(AAMPEventType type)
Convert AAMP event type to JS event string (AAMPMediaPlayer)
Definition: jsutils.cpp:377
aamp_GetException
JSValueRef aamp_GetException(JSContextRef context, ErrorCode error, const char *additionalInfo)
Generate a JSValue object with the exception details.
Definition: jsutils.cpp:278
aamp_CStringToJSValue
JSValueRef aamp_CStringToJSValue(JSContextRef context, const char *sz)
Convert C string to JSString.
Definition: jsutils.cpp:152
aamp_CreateTimedMetadataJSObject
JSObjectRef aamp_CreateTimedMetadataJSObject(JSContextRef context, long long timeMS, const char *szName, const char *szContent, const char *id, double durationMS)
Create a TimedMetadata JS object with args passed. Sample input "#EXT-X-CUE:ID=eae90713-db8e,...
Definition: jsutils.cpp:396
ErrorCode
ErrorCode
JavaScript error codes.
Definition: jsutils.h:65
aamp_JSValueToCString
char * aamp_JSValueToCString(JSContextRef context, JSValueRef value, JSValueRef *exception)
Convert JSString to C string.
Definition: jsutils.cpp:164
aamp_getEventTypeFromName
AAMPEventType aamp_getEventTypeFromName(const char *szName)
Convert JS event name to AAMP event type.
Definition: jsutils.cpp:324
aamp_dispatchEventToJS
void aamp_dispatchEventToJS(JSContextRef context, JSObjectRef callback, JSObjectRef event)
To dispatch a JS event.
Definition: jsutils.cpp:344
aamp_JSValueToJSONCString
char * aamp_JSValueToJSONCString(JSContextRef context, JSValueRef value, JSValueRef *exception)
Convert JSString to JSON C string.
Definition: jsutils.cpp:177
aamp_StringArrayToCStringArray
std::vector< std::string > aamp_StringArrayToCStringArray(JSContextRef context, JSValueRef arrayRef)
Convert an array of JSString to an array of C strings.
Definition: jsutils.cpp:213
aampPlayer_getEventTypeFromName
AAMPEventType aampPlayer_getEventTypeFromName(const char *szName)
Convert JS event name to AAMP event type (AAMPMediaPlayer)
Definition: jsutils.cpp:356
AAMPEventType
AAMPEventType
Type of the events sending to the JSPP player.
Definition: AampEvent.h:44
jsBindingLogprintf
void jsBindingLogprintf(int playerId, const char *levelstr, const char *functionName, int line, const char *format,...)
Print logs to console / log file.
Definition: jsutils.cpp:542
aamp_JSValueIsArray
bool aamp_JSValueIsArray(JSContextRef context, JSValueRef value)
Check if a JSValue object is array or not.
Definition: jsutils.cpp:190