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