merge with master
[platform/core/multimedia/libmm-player.git] / src / include / mm_player_utils.h
1 /*
2  * libmm-player
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: JongHyuk Choi <jhchoi.choi@samsung.com>, YeJin Cho <cho.yejin@samsung.com>, YoungHwan An <younghwan_.an@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21
22 #ifndef __MMF_PLAYER_UTILS_H__
23 #define __MMF_PLAYER_UTILS_H__
24
25 #include <glib.h>
26 #include <gst/gst.h>
27 #include <mm_player_ini.h>
28 #include <mm_types.h>
29 #include <mm_error.h>
30 #include <mm_message.h>
31
32 #ifdef __cplusplus
33         extern "C" {
34 #endif
35
36 /* general */
37 #ifndef ARRAY_SIZE
38 #define ARRAY_SIZE(arr)         (sizeof(arr) / sizeof((arr)[0]))
39 #endif
40
41 #define MMPLAYER_MAX_INT        (2147483647)
42
43 #define MMPLAYER_FREEIF(x) \
44 if ( x ) \
45         g_free( x ); \
46 x = NULL;
47
48 #define MMPLAYER_CMD_LOCK(x_player) \
49 do \
50 { \
51         GMutex* cmd_lock = ((mm_player_t *)x_player)->cmd_lock; \
52         if (cmd_lock) \
53                 g_mutex_lock(cmd_lock); \
54         else \
55         { \
56                 debug_log("no command lock"); \
57                 return MM_ERROR_PLAYER_NOT_INITIALIZED; \
58         } \
59 } while (0);
60
61 #define MMPLAYER_CMD_UNLOCK(x_player)   g_mutex_unlock( ((mm_player_t*)x_player)->cmd_lock )
62
63 #define MMPLAYER_MSG_POST_LOCK(x_player)        g_mutex_lock( ((mm_player_t*)x_player)->msg_cb_lock )
64 #define MMPLAYER_MSG_POST_UNLOCK(x_player)      g_mutex_unlock( ((mm_player_t*)x_player)->msg_cb_lock )
65
66 #define MMPLAYER_GET_ATTRS(x_player)            ((mm_player_t*)x_player)->attrs
67
68 #define MAX_SOUND_DEVICE_LEN    18      
69
70 /* element linking */
71 #ifdef GST_EXT_PAD_LINK_UNCHECKED
72 #define GST_ELEMENT_LINK_FILTERED       gst_element_link_filtered_unchecked
73 #define GST_ELEMENT_LINK_MANY           gst_element_link_many_unchecked
74 #define GST_ELEMENT_LINK                        gst_element_link_unchecked
75 #define GST_ELEMENT_LINK_PADS           gst_element_link_pads_unchecked
76 #define GST_PAD_LINK                            gst_pad_link_unchecked
77 #else
78 #define GST_ELEMENT_LINK_FILTERED       gst_element_link_filtered
79 #define GST_ELEMENT_LINK_MANY           gst_element_link_many
80 #define GST_ELEMENT_LINK                        gst_element_link
81 #define GST_ELEMENT_UNLINK                      gst_element_unlink
82 #define GST_ELEMENT_LINK_PADS           gst_element_link_pads
83 #define GST_PAD_LINK                            gst_pad_link
84 #endif
85
86 /* debug caps string */
87 #define MMPLAYER_LOG_GST_CAPS_TYPE(x_caps) \
88 do \
89 { \
90         gchar* caps_type = NULL; \
91         caps_type = gst_caps_to_string(x_caps); \
92         debug_log ("caps: %s\n", caps_type ); \
93         MMPLAYER_FREEIF (caps_type) \
94 } while (0);
95
96 /* message posting */
97 #define MMPLAYER_POST_MSG( x_player, x_msgtype, x_msg_param ) \
98 debug_log("posting %s to application\n", #x_msgtype); \
99 __mmplayer_post_message(x_player, x_msgtype, x_msg_param);
100
101 /* setting player state */
102 #define MMPLAYER_SET_STATE( x_player, x_state ) \
103 debug_log("setting state machine to %d\n", x_state); \
104 __mmplayer_set_state(x_player, x_state);
105
106
107 #define MMPLAYER_CHECK_STATE_RETURN_IF_FAIL( x_player, x_command ) \
108 debug_log("checking player state before doing %s\n", #x_command); \
109 switch ( __mmplayer_check_state(x_player, x_command) ) \
110 { \
111         case MM_ERROR_PLAYER_INVALID_STATE: \
112                 return MM_ERROR_PLAYER_INVALID_STATE; \
113         break; \
114         /* NOTE : for robustness of player. we won't treat it as an error */ \
115         case MM_ERROR_PLAYER_NO_OP: \
116                 return MM_ERROR_NONE; \
117         break; \
118         case MM_ERROR_PLAYER_DOING_SEEK: \
119                 return MM_ERROR_PLAYER_DOING_SEEK; \
120         default: \
121         break; \
122 }
123
124 /* setting element state */ 
125 #define MMPLAYER_ELEMENT_SET_STATE( x_element, x_state ) \
126 debug_log("setting state [%s:%d] to [%s]\n", #x_state, x_state, GST_ELEMENT_NAME( x_element ) ); \
127 if ( GST_STATE_CHANGE_FAILURE == gst_element_set_state ( x_element, x_state) ) \
128 { \
129         debug_error("failed to set state %s to %s\n", #x_state, GST_ELEMENT_NAME( x_element )); \
130         goto STATE_CHANGE_FAILED; \
131 }
132
133 #define MMPLAYER_CHECK_NULL( x_var ) \
134 if ( ! x_var ) \
135 { \
136         debug_error("[%s] is NULL\n", #x_var ); \
137         goto ERROR; \
138 }
139
140 #define MMPLAYER_CHECK_CMD_IF_EXIT( x_player ) \
141 if ( x_player->cmd == MMPLAYER_COMMAND_UNREALIZE || x_player->cmd == MMPLAYER_COMMAND_DESTROY ) \
142 { \
143         debug_log("it's exit state...\n");\
144         goto ERROR;  \
145 }
146 /* volume */
147 /* 
148 |----|-------|-------|-------|-------|
149 |Res. | HFK(7)  |  BT(7)  |  E.J(7)  | SPK(7) |
150 |----|-------|-------|-------|-------|
151 */
152
153 /* 090424 Fix me : Currently volume is 0~9, so bt volume can be only 0.0 ~ 0.9 */
154 #define GET_VOLUME_BT(volume) (volume/10.)
155
156 #if 0
157 #define GET_VOLUME_SPK(volume) ((volume) & 0x7F)
158 #define GET_VOLUME_EARJACK(volume) ((volume >> 7)& 0x7F)
159 #define GET_VOLUME_HFK(volume) ((volume >> 21) & 0x7F)
160
161 #define SET_VOLUME_SPK(volume,input) (volume |= (input &0x7F))
162 #define SET_VOLUME_EARJACK(volume,input) (volume |= ((input & 0x7F)<<7))
163 #define SET_VOLUME_BT(volume,input) (volume |= ((input & 0x7F)<<14))
164 #define SET_VOLUME_HFK(volume,input) (volume |= ((input & 0x7F)<<21))
165 #endif
166
167
168 /* pad probe for pipeilne debugging */
169 gboolean __util_gst_pad_probe(GstPad *pad, GstBuffer *buffer, gpointer u_data);
170
171 #define MM_PROBE_DEFAULT                        (0)
172 #define MM_PROBE_TIMESTAMP                      (1)
173 #define MM_PROBE_BUFFERSIZE                     (1 << 1)
174 #define MM_PROBE_CAPS                           (1 << 2)
175 #define MM_PROBE_BUFFER_DURATION        (1 << 3)
176 #define MM_PROBE_DROP_BUFFER            (1 << 4)
177 #define MM_PROBE_CLOCK_TIME                     (1 << 5)
178 /* ... add more */
179
180 /* messages are treated as warnings bcz those code should not be checked in. 
181  * and no error handling will supported for same manner. 
182  */
183 #define MMPLAYER_ADD_PROBE(x_pad, x_flag) \
184 debug_warning("adding pad probe\n"); \
185 if ( ! gst_pad_add_buffer_probe(x_pad, \
186         G_CALLBACK(__util_gst_pad_probe), \
187         (gpointer)x_flag) ) \
188 { \
189         debug_error("failed to add pad probe\n"); \
190 }
191
192
193 /* generating dot */
194 #define MMPLAYER_GENERATE_DOT_IF_ENABLED( x_player, x_name ) \
195 if ( PLAYER_INI()->generate_dot ) \
196 { \
197         debug_log("generating dot file(%s)\n", #x_name); \
198         GST_DEBUG_BIN_TO_DOT_FILE (GST_BIN (player->pipeline->mainbin[MMPLAYER_M_PIPE].gst), \
199         GST_DEBUG_GRAPH_SHOW_ALL, x_name); \
200 }
201
202 /* signal manipulation */
203 #define MMPLAYER_SIGNAL_CONNECT( x_player, x_object, x_signal, x_callback, x_arg ) \
204 do \
205 { \
206         MMPlayerSignalItem* item = NULL; \
207         item = (MMPlayerSignalItem*) g_malloc( sizeof (MMPlayerSignalItem) ); \
208         if ( ! item ) \
209         { \
210                 debug_error("cannot connect signal [%s]\n", x_signal ); \
211         } \
212         else \
213         { \
214                 item->obj = G_OBJECT( x_object ); \
215                 item->sig = g_signal_connect( G_OBJECT(x_object), x_signal, \
216                                         x_callback, x_arg ); \
217                 x_player->signals = g_list_append(x_player->signals, item); \
218         } \
219 } while ( 0 );
220
221
222 /* state */
223 #define MMPLAYER_PREV_STATE(x_player)           ((mm_player_t*)x_player)->prev_state 
224 #define MMPLAYER_CURRENT_STATE(x_player)                ((mm_player_t*)x_player)->state 
225 #define         MMPLAYER_PENDING_STATE(x_player)                ((mm_player_t*)x_player)->pending_state 
226 #define         MMPLAYER_TARGET_STATE(x_player)         ((mm_player_t*)x_player)->target_state 
227 #define         MMPLAYER_STATE_GET_NAME(state) __get_state_name(state)
228
229 #define         MMPLAYER_PRINT_STATE(x_player) \
230 debug_log("-----------------------PLAYER STATE-------------------------\n"); \
231 debug_log(" prev %s, current %s, pending %s, target %s \n", \
232         MMPLAYER_STATE_GET_NAME(MMPLAYER_PREV_STATE(x_player)), \
233         MMPLAYER_STATE_GET_NAME(MMPLAYER_CURRENT_STATE(x_player)), \
234         MMPLAYER_STATE_GET_NAME(MMPLAYER_PENDING_STATE(x_player)), \
235         MMPLAYER_STATE_GET_NAME(MMPLAYER_TARGET_STATE(x_player))); \
236 debug_log("------------------------------------------------------------\n"); 
237
238
239 #define         MMPLAYER_STATE_CHANGE_TIMEOUT(x_player )         ((mm_player_t*)x_player)->state_change_timeout 
240
241 /* streaming */
242 #define MMPLAYER_IS_STREAMING(x_player)                         __is_streaming(x_player)
243 #define MMPLAYER_IS_RTSP_STREAMING(x_player)    __is_rtsp_streaming(x_player)
244 #define MMPLAYER_IS_HTTP_STREAMING(x_player)    __is_http_streaming(x_player)
245 #define MMPLAYER_IS_HTTP_PD(x_player)                   __is_http_progressive_down(x_player)
246 #define MMPLAYER_IS_HTTP_LIVE_STREAMING(x_player)  __is_http_live_streaming(x_player)
247 #define MMPLAYER_IS_LIVE_STREAMING(x_player)    __is_live_streaming(x_player)
248
249 /* etc */
250 #define MMF_PLAYER_FILE_BACKUP_PATH             "/tmp/media_temp."
251 #define         MMPLAYER_PT_IS_AUDIO( x_pt )            ( strstr(x_pt, "_97") || strstr(x_pt, "audio") )
252 #define         MMPLAYER_PT_IS_VIDEO( x_pt )            ( strstr(x_pt, "_96") || strstr(x_pt, "video") )
253
254 bool util_is_sdp_file ( const char *path );
255 int64_t uti_get_time ( void );
256 int util_get_rank_increase ( const char *factory_class );
257 int util_factory_rank_compare(GstPluginFeature *f1, GstPluginFeature *f2); // @
258
259
260 bool util_exist_file_path(const char *file_path);
261 bool util_write_file_backup(const char *backup_path, char *data_ptr, int data_size);
262 bool util_remove_file_backup(const char *backup_path); /* For Midi Player */
263
264 int util_is_midi_type_by_mem(void *mem, int size);
265 int util_is_midi_type_by_file(const char *file_path);
266
267 char** util_get_cookie_list ( const char *cookies );
268 bool util_check_valid_url ( const char *proxy );
269
270 char* util_get_charset(const char *file_path);
271
272 #ifdef __cplusplus
273         }
274 #endif
275
276 #endif /* __MMF_PLAYER_UTILS_H__ */
277