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