Merge branch 'tizen_3.0' into tizen
[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 <dlog.h>
29 #include <mm_player_ini.h>
30 #include <mm_types.h>
31 #include <mm_error.h>
32 #include <mm_message.h>
33
34 #ifdef __cplusplus
35         extern "C" {
36 #endif
37
38 #ifdef LOG_TAG
39 #undef LOG_TAG
40 #endif
41 #define LOG_TAG "MM_PLAYER"
42
43 /* general */
44 #ifndef ARRAY_SIZE
45 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
46 #endif
47
48 #define MMPLAYER_MAX_INT (2147483647)
49
50 #define MMPLAYER_FREEIF(x) \
51 if (x) \
52         g_free(x); \
53 x = NULL;
54
55 #define MMPLAYER_GET_ATTRS(x_player) ((mm_player_t*)x_player)->attrs
56
57 /* command */
58 #define MMPLAYER_CMD_LOCK(x_player)                          g_mutex_lock(&((mm_player_t *)x_player)->cmd_lock)
59 #define MMPLAYER_CMD_TRYLOCK(x_player)                       g_mutex_trylock(&((mm_player_t *)x_player)->cmd_lock)
60 #define MMPLAYER_CMD_UNLOCK(x_player)                        g_mutex_unlock(&((mm_player_t*)x_player)->cmd_lock)
61
62 /* playback */
63 #define MMPLAYER_PLAYBACK_LOCK(x_player)                     g_mutex_lock(&((mm_player_t *)x_player)->playback_lock)
64 #define MMPLAYER_PLAYBACK_UNLOCK(x_player)                   g_mutex_unlock(&((mm_player_t*)x_player)->playback_lock)
65
66 /* capture thread */
67 #define MMPLAYER_CAPTURE_THREAD_LOCK(x_player)               g_mutex_lock(&((mm_player_t *)x_player)->capture_thread_mutex)
68 #define MMPLAYER_CAPTURE_THREAD_UNLOCK(x_player)             g_mutex_unlock(&((mm_player_t *)x_player)->capture_thread_mutex)
69 #define MMPLAYER_CAPTURE_THREAD_WAIT(x_player)               g_cond_wait(&((mm_player_t *)x_player)->capture_thread_cond, &((mm_player_t *)x_player)->capture_thread_mutex)
70 #define MMPLAYER_CAPTURE_THREAD_SIGNAL(x_player)             g_cond_signal(&((mm_player_t *)x_player)->capture_thread_cond);
71
72 /* next play thread */
73 #define MMPLAYER_NEXT_PLAY_THREAD_LOCK(x_player)             g_mutex_lock(&((mm_player_t *)x_player)->next_play_thread_mutex)
74 #define MMPLAYER_NEXT_PLAY_THREAD_UNLOCK(x_player)           g_mutex_unlock(&((mm_player_t *)x_player)->next_play_thread_mutex)
75 #define MMPLAYER_NEXT_PLAY_THREAD_WAIT(x_player)             g_cond_wait(&((mm_player_t *)x_player)->next_play_thread_cond, &((mm_player_t *)x_player)->next_play_thread_mutex)
76 #define MMPLAYER_NEXT_PLAY_THREAD_SIGNAL(x_player)           g_cond_signal(&((mm_player_t *)x_player)->next_play_thread_cond);
77
78 /* repeat thread */
79 #define MMPLAYER_REPEAT_THREAD_LOCK(x_player)                g_mutex_lock(&((mm_player_t *)x_player)->repeat_thread_mutex)
80 #define MMPLAYER_REPEAT_THREAD_UNLOCK(x_player)              g_mutex_unlock(&((mm_player_t *)x_player)->repeat_thread_mutex)
81 #define MMPLAYER_REPEAT_THREAD_WAIT(x_player)                g_cond_wait(&((mm_player_t *)x_player)->repeat_thread_cond, &((mm_player_t *)x_player)->repeat_thread_mutex)
82 #define MMPLAYER_REPEAT_THREAD_SIGNAL(x_player)              g_cond_signal(&((mm_player_t *)x_player)->repeat_thread_cond);
83
84 /* handling fakesink */
85 #define MMPLAYER_FSINK_LOCK(x_player)                        g_mutex_lock(&((mm_player_t *)x_player)->fsink_lock)
86 #define MMPLAYER_FSINK_UNLOCK(x_player)                      g_mutex_unlock(&((mm_player_t *)x_player)->fsink_lock)
87
88
89 /* handling update tag */
90 #define MMPLAYER_UPDATE_TAG_LOCK(x_player)                   g_mutex_lock(&((mm_player_t *)x_player)->update_tag_lock)
91 #define MMPLAYER_UPDATE_TAG_UNLOCK(x_player)                 g_mutex_unlock(&((mm_player_t *)x_player)->update_tag_lock)
92
93 /* video stream bo */
94 #define MMPLAYER_VIDEO_BO_LOCK(x_player)                     g_mutex_lock(&((mm_player_t *)x_player)->video_bo_mutex)
95 #define MMPLAYER_VIDEO_BO_UNLOCK(x_player)                   g_mutex_unlock(&((mm_player_t *)x_player)->video_bo_mutex)
96 #define MMPLAYER_VIDEO_BO_WAIT(x_player)                     g_cond_wait(&((mm_player_t *)x_player)->video_bo_cond, &((mm_player_t *)x_player)->video_bo_mutex)
97 #define MMPLAYER_VIDEO_BO_WAIT_UNTIL(x_player, end_time)     g_cond_wait_until(&((mm_player_t *)x_player)->video_bo_cond, &((mm_player_t *)x_player)->video_bo_mutex, end_time);
98 #define MMPLAYER_VIDEO_BO_SIGNAL(x_player)                   g_cond_signal(&((mm_player_t *)x_player)->video_bo_cond);
99
100 /* media stream lock */
101 #define MMPLAYER_MEDIA_STREAM_CALLBACK_LOCK(x_player)        g_mutex_lock(&((mm_player_t *)x_player)->media_stream_cb_lock)
102 #define MMPLAYER_MEDIA_STREAM_CALLBACK_UNLOCK(x_player)      g_mutex_unlock(&((mm_player_t *)x_player)->media_stream_cb_lock)
103
104 #if 0
105 #define MMPLAYER_FENTER();                                      LOGD("<ENTER>");
106 #define MMPLAYER_FLEAVE();                                      LOGD("<LEAVE>");
107 #else
108 #define MMPLAYER_FENTER();
109 #define MMPLAYER_FLEAVE();
110 #endif
111
112 #define MAX_SOUND_DEVICE_LEN    18
113
114 /* element linking */
115 #ifdef GST_EXT_PAD_LINK_UNCHECKED
116 #define GST_ELEMENT_LINK_FILTERED       gst_element_link_filtered_unchecked
117 #define GST_ELEMENT_LINK_MANY           gst_element_link_many_unchecked
118 #define GST_ELEMENT_LINK                        gst_element_link_unchecked
119 #define GST_ELEMENT_LINK_PADS           gst_element_link_pads_unchecked
120 #define GST_PAD_LINK                            gst_pad_link_unchecked
121 #else
122 #define GST_ELEMENT_LINK_FILTERED       gst_element_link_filtered
123 #define GST_ELEMENT_LINK_MANY           gst_element_link_many
124 #define GST_ELEMENT_LINK                        gst_element_link
125 #define GST_ELEMENT_UNLINK                      gst_element_unlink
126 #define GST_ELEMENT_LINK_PADS           gst_element_link_pads
127 #define GST_PAD_LINK                            gst_pad_link
128 #endif
129
130 #define MMPLAYER_RETURN_IF_FAIL(expr) \
131 if (!(expr)) { \
132         LOGW("faild [%s]", #expr); \
133         return; \
134 }
135
136 #define MMPLAYER_RETURN_VAL_IF_FAIL(expr, var) \
137 if (!(expr)) { \
138         LOGW("faild [%s]", #expr); \
139         return (var); \
140 }
141
142 /* debug caps string */
143 #define MMPLAYER_LOG_GST_CAPS_TYPE(x_caps) \
144 do { \
145         gchar* caps_type = NULL; \
146         caps_type = gst_caps_to_string(x_caps); \
147         LOGD("caps: %s\n", caps_type); \
148         MMPLAYER_FREEIF(caps_type) \
149 } while (0)
150
151 /* message posting */
152 #define MMPLAYER_POST_MSG(x_player, x_msgtype, x_msg_param) \
153 LOGD("posting %s to application\n", #x_msgtype); \
154 __mmplayer_post_message(x_player, x_msgtype, x_msg_param);
155
156 /* setting player state */
157 #define MMPLAYER_SET_STATE(x_player, x_state) \
158 LOGD("update state machine to %d\n", x_state); \
159 __mmplayer_set_state(x_player, x_state);
160
161 #define MMPLAYER_CHECK_STATE(x_player, x_command) \
162 LOGD("checking player state before doing %s\n", #x_command); \
163 switch (__mmplayer_check_state(x_player, x_command)) { \
164 case MM_ERROR_PLAYER_INVALID_STATE: \
165         return MM_ERROR_PLAYER_INVALID_STATE; \
166         break; \
167         /* NOTE : for robustness of player. we won't treat it as an error */ \
168 case MM_ERROR_PLAYER_NO_OP: \
169         return MM_ERROR_NONE; \
170         break; \
171 case MM_ERROR_PLAYER_DOING_SEEK: \
172         return MM_ERROR_PLAYER_DOING_SEEK; \
173 default: \
174         break; \
175 }
176
177 /* setting element state */
178 #define MMPLAYER_ELEMENT_SET_STATE(x_element, x_state) \
179 LOGD("setting state [%s:%d] to [%s]\n", #x_state, x_state, GST_ELEMENT_NAME(x_element)); \
180 if (GST_STATE_CHANGE_FAILURE == gst_element_set_state(x_element, x_state)) { \
181         LOGE("failed to set state %s to %s\n", #x_state, GST_ELEMENT_NAME(x_element)); \
182         goto STATE_CHANGE_FAILED; \
183 }
184
185 #define MMPLAYER_CHECK_NULL(x_var) \
186 if (!x_var) { \
187         LOGE("[%s] is NULL\n", #x_var); \
188         goto ERROR; \
189 }
190
191 #define MMPLAYER_CHECK_CMD_IF_EXIT(x_player) \
192 if (x_player->cmd == MMPLAYER_COMMAND_UNREALIZE || x_player->cmd == MMPLAYER_COMMAND_DESTROY) { \
193         LOGD("it's exit state...\n");\
194         goto ERROR;  \
195 }
196
197 /* generating dot */
198 #define MMPLAYER_GENERATE_DOT_IF_ENABLED(x_player, x_name) \
199 if (x_player->ini.generate_dot) { \
200         GST_DEBUG_BIN_TO_DOT_FILE(GST_BIN(player->pipeline->mainbin[MMPLAYER_M_PIPE].gst), \
201         GST_DEBUG_GRAPH_SHOW_ALL, x_name); \
202 }
203
204 /* signal manipulation */
205 #define MMPLAYER_SIGNAL_CONNECT(x_player, x_object, x_type, x_signal, x_callback, x_arg) \
206 do { \
207         MMPlayerSignalItem* item = NULL; \
208         item = (MMPlayerSignalItem*) g_malloc(sizeof(MMPlayerSignalItem)); \
209         if (!item) { \
210                 LOGE("cannot connect signal [%s]\n", x_signal); \
211         } else { \
212                 item->obj = G_OBJECT(x_object); \
213                 item->sig = g_signal_connect(G_OBJECT(x_object), x_signal, \
214                                         x_callback, x_arg); \
215                 if ((x_type >= MM_PLAYER_SIGNAL_TYPE_AUTOPLUG) && (x_type < MM_PLAYER_SIGNAL_TYPE_MAX)) \
216                         x_player->signals[x_type] = g_list_append(x_player->signals[x_type], item); \
217                 else \
218                         LOGE("wrong signal type [%d]\n", x_type); \
219         } \
220 } while (0);
221
222 /* release element resource */
223 #define MMPLAYER_RELEASE_ELEMENT(x_player, x_bin, x_id) \
224 do { \
225         if (x_bin[x_id].gst) { \
226                 gst_element_set_state(x_bin[x_id].gst, GST_STATE_NULL); \
227                 gst_bin_remove(GST_BIN(x_player->pipeline->mainbin[MMPLAYER_M_PIPE].gst), x_bin[x_id].gst); \
228                 x_bin[x_id].gst = NULL; \
229                 LOGD("release done [element %d]", x_id); \
230         } \
231 } while (0)
232
233 /* state */
234 #define MMPLAYER_PREV_STATE(x_player) ((mm_player_t*)x_player)->prev_state
235 #define MMPLAYER_CURRENT_STATE(x_player) ((mm_player_t*)x_player)->state
236 #define MMPLAYER_PENDING_STATE(x_player) ((mm_player_t*)x_player)->pending_state
237 #define MMPLAYER_TARGET_STATE(x_player) ((mm_player_t*)x_player)->target_state
238 #define MMPLAYER_STATE_GET_NAME(state) __get_state_name(state)
239
240 #define MMPLAYER_PRINT_STATE(x_player) \
241 LOGD("-- prev %s, current %s, pending %s, target %s --\n", \
242         MMPLAYER_STATE_GET_NAME(MMPLAYER_PREV_STATE(x_player)), \
243         MMPLAYER_STATE_GET_NAME(MMPLAYER_CURRENT_STATE(x_player)), \
244         MMPLAYER_STATE_GET_NAME(MMPLAYER_PENDING_STATE(x_player)), \
245         MMPLAYER_STATE_GET_NAME(MMPLAYER_TARGET_STATE(x_player)));
246
247 #define MMPLAYER_STATE_CHANGE_TIMEOUT(x_player)  ((mm_player_t*)x_player)->state_change_timeout
248
249 /* streaming */
250 #define MMPLAYER_IS_STREAMING(x_player) __is_streaming(x_player)
251 #define MMPLAYER_IS_RTSP_STREAMING(x_player) __is_rtsp_streaming(x_player)
252 #define MMPLAYER_IS_WFD_STREAMING(x_player) __is_wfd_streaming(x_player)
253 #define MMPLAYER_IS_HTTP_STREAMING(x_player) __is_http_streaming(x_player)
254 #define MMPLAYER_IS_HTTP_PD(x_player) __is_http_progressive_down(x_player)
255 #define MMPLAYER_IS_HTTP_LIVE_STREAMING(x_player) __is_http_live_streaming(x_player)
256 #define MMPLAYER_IS_LIVE_STREAMING(x_player) __is_live_streaming(x_player)
257 #define MMPLAYER_IS_DASH_STREAMING(x_player) __is_dash_streaming(x_player)
258 #define MMPLAYER_IS_SMOOTH_STREAMING(x_player) __is_smooth_streaming(x_player)
259 #define MMPLAYER_IS_MS_BUFF_SRC(x_player) __is_ms_buff_src(x_player)
260
261 #define MMPLAYER_URL_HAS_DASH_SUFFIX(x_player) __has_suffix(x_player, "mpd")
262 #define MMPLAYER_URL_HAS_HLS_SUFFIX(x_player) __has_suffix(x_player, "m3u8")
263
264 /* etc */
265 #define MMF_PLAYER_FILE_BACKUP_PATH "/tmp/media_temp."
266 #define MMPLAYER_PT_IS_AUDIO(x_pt) (strstr(x_pt, "_97") || strstr(x_pt, "audio"))
267 #define MMPLAYER_PT_IS_VIDEO(x_pt) (strstr(x_pt, "_96") || strstr(x_pt, "video"))
268
269 #define MMPLAYER_VIDEO_SINK_CHECK(x_player) \
270 do { \
271         MMPLAYER_RETURN_VAL_IF_FAIL(x_player && \
272                 x_player->pipeline && \
273                 x_player->pipeline->videobin && \
274                 x_player->pipeline->videobin[MMPLAYER_V_SINK].gst, \
275                 MM_ERROR_PLAYER_NOT_INITIALIZED); \
276 } while (0)
277
278 enum {
279         MMPLAYER_DISPLAY_NULL = 0,
280         MMPLAYER_DISPLAY_HDMI_ACTIVE,
281         MMPLAYER_DISPLAY_MIRRORING_ACTIVE,
282 };
283
284 bool util_is_sdp_file(const char *path);
285 int util_get_rank_increase(const char *factory_class);
286 int util_factory_rank_compare(GstPluginFeature *f1, GstPluginFeature *f2); // @
287 int util_exist_file_path(const char *file_path);
288 bool util_write_file_backup(const char *backup_path, char *data_ptr, int data_size);
289 bool util_remove_file_backup(const char *backup_path); /* For Midi Player */
290 int util_is_midi_type_by_mem(void *mem, int size);
291 int util_is_midi_type_by_file(const char *file_path);
292 char** util_get_cookie_list(const char *cookies);
293 bool util_check_valid_url(const char *proxy);
294 const char* util_get_charset(const char *file_path);
295 int util_get_pixtype(unsigned int fourcc);
296
297 #ifdef __cplusplus
298         }
299 #endif
300
301 #endif /* __MM_PLAYER_UTILS_H__ */
302