[0.6.262] resolve block issue during switching stream
[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 #include <limits.h>
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 #include "mm_player_priv.h"
34
35 #ifdef __cplusplus
36         extern "C" {
37 #endif
38
39 #ifdef LOG_TAG
40 #undef LOG_TAG
41 #endif
42 #define LOG_TAG "MM_PLAYER"
43
44 /* general */
45 #ifndef ARRAY_SIZE
46 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
47 #endif
48
49 #define MMPLAYER_MIN_INT (INT_MIN)
50 #define MMPLAYER_MAX_INT (INT_MAX)
51
52 #define MMPLAYER_FREEIF(x) \
53         do {    \
54                 if (x) { \
55                         g_free(x); \
56                         x = NULL;       \
57                 } \
58         } while (0)
59
60 #define MMPLAYER_GET_ATTRS(x_player) ((mmplayer_t *)x_player)->attrs
61
62 /* command */
63 #define MMPLAYER_CMD_LOCK_INIT(x_player)                     _mmplayer_cmd_lock_init((mmplayer_t *)x_player)
64 #define MMPLAYER_CMD_LOCK(x_player)                          _mmplayer_cmd_lock((mmplayer_t *)x_player)
65 #define MMPLAYER_CMD_TRYLOCK(x_player)                       _mmplayer_cmd_trylock((mmplayer_t *)x_player)
66 #define MMPLAYER_CMD_UNLOCK(x_player)                        _mmplayer_cmd_unlock((mmplayer_t *)x_player)
67 #define MMPLAYER_CMD_LOCK_DEINIT(x_player)                   _mmplayer_cmd_lock_deinit((mmplayer_t *)x_player)
68
69 /* pipeline reconfigure */
70 #define MMPLAYER_RECONFIGURE_LOCK(x_player)                  g_mutex_lock(&((mmplayer_t *)x_player)->reconfigure_lock)
71 #define MMPLAYER_RECONFIGURE_UNLOCK(x_player)                g_mutex_unlock(&((mmplayer_t *)x_player)->reconfigure_lock)
72 #define MMPLAYER_RECONFIGURE_WAIT(x_player)                  g_cond_wait(&((mmplayer_t *)x_player)->reconfigure_cond, &((mmplayer_t *)x_player)->reconfigure_lock)
73 #define MMPLAYER_RECONFIGURE_SIGNAL(x_player)                g_cond_signal(&((mmplayer_t *)x_player)->reconfigure_cond);
74
75 /* capture thread */
76 #define MMPLAYER_CAPTURE_THREAD_LOCK(x_player)               g_mutex_lock(&((mmplayer_t *)x_player)->capture_thread_mutex)
77 #define MMPLAYER_CAPTURE_THREAD_UNLOCK(x_player)             g_mutex_unlock(&((mmplayer_t *)x_player)->capture_thread_mutex)
78 #define MMPLAYER_CAPTURE_THREAD_WAIT(x_player)               g_cond_wait(&((mmplayer_t *)x_player)->capture_thread_cond, &((mmplayer_t *)x_player)->capture_thread_mutex)
79 #define MMPLAYER_CAPTURE_THREAD_SIGNAL(x_player)             g_cond_signal(&((mmplayer_t *)x_player)->capture_thread_cond);
80
81 /* gapless play thread */
82 #define MMPLAYER_GAPLESS_PLAY_THREAD_LOCK(x_player)             g_mutex_lock(&((mmplayer_t *)x_player)->gapless_play_thread_mutex)
83 #define MMPLAYER_GAPLESS_PLAY_THREAD_UNLOCK(x_player)           g_mutex_unlock(&((mmplayer_t *)x_player)->gapless_play_thread_mutex)
84 #define MMPLAYER_GAPLESS_PLAY_THREAD_WAIT(x_player)             g_cond_wait(&((mmplayer_t *)x_player)->gapless_play_thread_cond, &((mmplayer_t *)x_player)->gapless_play_thread_mutex)
85 #define MMPLAYER_GAPLESS_PLAY_THREAD_SIGNAL(x_player)           g_cond_signal(&((mmplayer_t *)x_player)->gapless_play_thread_cond);
86
87 /* gst bus msg thread */
88 #define MMPLAYER_BUS_MSG_THREAD_LOCK(x_player)                   g_mutex_lock(&((mmplayer_t *)x_player)->bus_msg_thread_mutex)
89 #define MMPLAYER_BUS_MSG_THREAD_UNLOCK(x_player)                 g_mutex_unlock(&((mmplayer_t *)x_player)->bus_msg_thread_mutex)
90 #define MMPLAYER_BUS_MSG_THREAD_WAIT(x_player)                   g_cond_wait(&((mmplayer_t *)x_player)->bus_msg_thread_cond, &((mmplayer_t *)x_player)->bus_msg_thread_mutex)
91 #define MMPLAYER_BUS_MSG_THREAD_WAIT_UNTIL(x_player, end_time)   g_cond_wait_until(&((mmplayer_t *)x_player)->bus_msg_thread_cond, &((mmplayer_t *)x_player)->bus_msg_thread_mutex, end_time)
92 #define MMPLAYER_BUS_MSG_THREAD_SIGNAL(x_player)                 g_cond_signal(&((mmplayer_t *)x_player)->bus_msg_thread_cond);
93
94 /* gst bus watcher thread */
95 #define MMPLAYER_BUS_WATCHER_LOCK(x_player)                   g_mutex_lock(&((mmplayer_t *)x_player)->bus_watcher_mutex)
96 #define MMPLAYER_BUS_WATCHER_UNLOCK(x_player)                 g_mutex_unlock(&((mmplayer_t *)x_player)->bus_watcher_mutex)
97 #define MMPLAYER_BUS_WATCHER_WAIT_UNTIL(x_player, end_time)   g_cond_wait_until(&((mmplayer_t *)x_player)->bus_watcher_cond, &((mmplayer_t *)x_player)->bus_watcher_mutex, end_time)
98 #define MMPLAYER_BUS_WATCHER_SIGNAL(x_player)                 g_cond_signal(&((mmplayer_t *)x_player)->bus_watcher_cond);
99
100 /* handling fakesink */
101 #define MMPLAYER_FSINK_LOCK(x_player)                        g_mutex_lock(&((mmplayer_t *)x_player)->fsink_lock)
102 #define MMPLAYER_FSINK_UNLOCK(x_player)                      g_mutex_unlock(&((mmplayer_t *)x_player)->fsink_lock)
103
104 /* handling update tag */
105 #define MMPLAYER_UPDATE_TAG_LOCK(x_player)                   g_mutex_lock(&((mmplayer_t *)x_player)->update_tag_lock)
106 #define MMPLAYER_UPDATE_TAG_UNLOCK(x_player)                 g_mutex_unlock(&((mmplayer_t *)x_player)->update_tag_lock)
107
108 /* video stream bo */
109 #define MMPLAYER_VIDEO_BO_LOCK(x_player)                     g_mutex_lock(&((mmplayer_t *)x_player)->video_bo_mutex)
110 #define MMPLAYER_VIDEO_BO_UNLOCK(x_player)                   g_mutex_unlock(&((mmplayer_t *)x_player)->video_bo_mutex)
111 #define MMPLAYER_VIDEO_BO_WAIT(x_player)                     g_cond_wait(&((mmplayer_t *)x_player)->video_bo_cond, &((mmplayer_t *)x_player)->video_bo_mutex)
112 #define MMPLAYER_VIDEO_BO_WAIT_UNTIL(x_player, end_time)     g_cond_wait_until(&((mmplayer_t *)x_player)->video_bo_cond, &((mmplayer_t *)x_player)->video_bo_mutex, end_time)
113 #define MMPLAYER_VIDEO_BO_SIGNAL(x_player)                   g_cond_signal(&((mmplayer_t *)x_player)->video_bo_cond);
114
115 /* subtitle info */
116 #define MMPLAYER_SUBTITLE_INFO_LOCK(x_player)                     g_mutex_lock(&((mmplayer_t *)x_player)->subtitle_info_mutex)
117 #define MMPLAYER_SUBTITLE_INFO_UNLOCK(x_player)                   g_mutex_unlock(&((mmplayer_t *)x_player)->subtitle_info_mutex)
118 #define MMPLAYER_SUBTITLE_INFO_WAIT(x_player)                     g_cond_wait(&((mmplayer_t *)x_player)->subtitle_info_cond, &((mmplayer_t *)x_player)->subtitle_info_mutex)
119 #define MMPLAYER_SUBTITLE_INFO_WAIT_UNTIL(x_player, end_time)     g_cond_wait_until(&((mmplayer_t *)x_player)->subtitle_info_cond, &((mmplayer_t *)x_player)->subtitle_info_mutex, end_time)
120 #define MMPLAYER_SUBTITLE_INFO_SIGNAL(x_player)                   g_cond_signal(&((mmplayer_t *)x_player)->subtitle_info_cond);
121
122 #if 0
123 #define MMPLAYER_FENTER();                                      LOGD("<ENTER>");
124 #define MMPLAYER_FLEAVE();                                      LOGD("<LEAVE>");
125 #else
126 #define MMPLAYER_FENTER();
127 #define MMPLAYER_FLEAVE();
128 #endif
129
130 #define MMPLAYER_RETURN_IF_FAIL(expr) \
131         do { \
132                 if (!(expr)) { \
133                         LOGW("failed [%s]", #expr); \
134                         return; \
135                 } \
136         } while (0)
137
138 #define MMPLAYER_RETURN_VAL_IF_FAIL(expr, var) \
139         do { \
140                 if (!(expr)) { \
141                         LOGW("failed [%s]", #expr); \
142                         return (var); \
143                 } \
144         } while (0)
145
146 /* debug caps string */
147 #define MMPLAYER_LOG_GST_CAPS_TYPE(x_caps) \
148         do { \
149                 gchar *caps_type = NULL; \
150                 if (x_caps) { \
151                         caps_type = gst_caps_to_string(x_caps); \
152                         LOGD("caps: %s", caps_type); \
153                         MMPLAYER_FREEIF(caps_type); \
154                 } else {\
155                         LOGW("caps is null"); \
156                 } \
157         } while (0)
158
159 /* message posting */
160 #define MMPLAYER_POST_MSG(x_player, x_msgtype, x_msg_param) \
161         do { \
162                 LOGD("[handle: %p] posting %s to application", x_player, #x_msgtype); \
163                 _mmplayer_post_message(x_player, x_msgtype, x_msg_param); \
164         } while (0)
165
166 /* setting player state */
167 #define MMPLAYER_SET_STATE(x_player, x_state) \
168         do { \
169                 LOGD("[handle: %p] update state machine to %d", x_player, x_state); \
170                 _mmplayer_set_state(x_player, x_state); \
171         } while (0)
172
173 #define MMPLAYER_CHECK_STATE(x_player, x_command) \
174         do {    \
175                 LOGD("[handle: %p] checking player state before doing %s", x_player, #x_command); \
176                 switch (_mmplayer_check_state(x_player, x_command)) { \
177                 case MM_ERROR_PLAYER_INVALID_STATE: \
178                         return MM_ERROR_PLAYER_INVALID_STATE; \
179                         break; \
180                         /* NOTE : for robustness of player. we won't treat it as an error */ \
181                 case MM_ERROR_PLAYER_NO_OP: \
182                         return MM_ERROR_NONE; \
183                         break; \
184                 case MM_ERROR_PLAYER_DOING_SEEK: \
185                         return MM_ERROR_PLAYER_DOING_SEEK; \
186                 default: \
187                         break; \
188                 }       \
189         } while (0)
190
191 /* setting element state */
192 #define MMPLAYER_ELEMENT_SET_STATE(x_element, x_state) \
193         do {    \
194                 LOGD("setting state [%s:%d] to [%s]", #x_state, x_state, GST_ELEMENT_NAME(x_element)); \
195                 if (GST_STATE_CHANGE_FAILURE == gst_element_set_state(x_element, x_state)) { \
196                         LOGE("failed to set state %s to %s", #x_state, GST_ELEMENT_NAME(x_element)); \
197                         goto STATE_CHANGE_FAILED; \
198                 }       \
199         } while (0)
200
201 #define MMPLAYER_CHECK_NULL(x_var) \
202         do { \
203                 if (!x_var) { \
204                         LOGE("[%s] is NULL", #x_var); \
205                         goto ERROR; \
206                 } \
207         } while (0)
208
209 /* generating dot */
210 #define MMPLAYER_GENERATE_DOT_IF_ENABLED(x_player, x_name) \
211         do { \
212                 if (x_player->ini.generate_dot) { \
213                         GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS(GST_BIN(player->pipeline->mainbin[MMPLAYER_M_PIPE].gst), \
214                         GST_DEBUG_GRAPH_SHOW_ALL, x_name); \
215                 } \
216         } while (0)
217
218 /* signal manipulation */
219 #define MMPLAYER_SIGNAL_CONNECT(x_player, x_object, x_type, x_signal, x_callback, x_arg) \
220         do { \
221                 mmplayer_signal_item_t *item = NULL; \
222                 item = (mmplayer_signal_item_t *)g_malloc(sizeof(mmplayer_signal_item_t)); \
223                 if (!item) { \
224                         LOGE("cannot connect signal [%s]", x_signal); \
225                 } else { \
226                         item->obj = G_OBJECT(x_object); \
227                         item->sig = g_signal_connect(G_OBJECT(x_object), x_signal, \
228                                                 x_callback, x_arg); \
229                         if ((x_type >= MM_PLAYER_SIGNAL_TYPE_AUTOPLUG) && (x_type < MM_PLAYER_SIGNAL_TYPE_MAX)) \
230                                 x_player->signals[x_type] = g_list_append(x_player->signals[x_type], item); \
231                         else \
232                                 LOGE("wrong signal type [%d]", x_type); \
233                 } \
234         } while (0)
235
236 /* create element */
237 #define MMPLAYER_CREATE_ELEMENT(x_bin, x_id, x_factory, x_name, x_bucket, x_player) \
238         do {\
239                 x_bin[x_id].id = x_id;\
240                 x_bin[x_id].gst = gst_element_factory_make(x_factory, x_name);\
241                 if (!x_bin[x_id].gst) {\
242                         LOGE("failed to create %s", x_factory);\
243                         goto ERROR;\
244                 } else {\
245                         if (x_player->ini.set_dump_element_flag)\
246                                 __mmplayer_add_dump_buffer_probe(x_player, x_bin[x_id].gst);\
247                 } \
248                 x_bucket = g_list_append(x_bucket, &x_bin[x_id]);\
249         } while (0);
250
251 /* release element resource */
252 #define MMPLAYER_RELEASE_ELEMENT(x_player, x_bin, x_id) \
253         do { \
254                 if (x_bin[x_id].gst) { \
255                         gst_element_set_state(x_bin[x_id].gst, GST_STATE_NULL); \
256                         gst_bin_remove(GST_BIN(x_player->pipeline->mainbin[MMPLAYER_M_PIPE].gst), x_bin[x_id].gst); \
257                         x_bin[x_id].gst = NULL; \
258                         LOGD("release done [element %d]", x_id); \
259                 } \
260         } while (0)
261
262 /* state */
263 #define MMPLAYER_PREV_STATE(x_player) ((mmplayer_t *)x_player)->prev_state
264 #define MMPLAYER_CURRENT_STATE(x_player) ((mmplayer_t *)x_player)->state
265 #define MMPLAYER_PENDING_STATE(x_player) ((mmplayer_t *)x_player)->pending_state
266 #define MMPLAYER_TARGET_STATE(x_player) ((mmplayer_t *)x_player)->target_state
267 #define MMPLAYER_STATE_GET_NAME(state) _mmplayer_get_state_name(state)
268
269 #define MMPLAYER_PRINT_STATE(x_player) \
270         do { \
271                 LOGD("-- prev %s, current %s, pending %s, target %s --", \
272                         MMPLAYER_STATE_GET_NAME(MMPLAYER_PREV_STATE(x_player)), \
273                         MMPLAYER_STATE_GET_NAME(MMPLAYER_CURRENT_STATE(x_player)), \
274                         MMPLAYER_STATE_GET_NAME(MMPLAYER_PENDING_STATE(x_player)), \
275                         MMPLAYER_STATE_GET_NAME(MMPLAYER_TARGET_STATE(x_player))); \
276         } while (0)
277
278 #define MMPLAYER_STATE_CHANGE_TIMEOUT(x_player) ((mmplayer_t *)x_player)->state_change_timeout
279
280 /* streaming */
281 #define MMPLAYER_IS_STREAMING(x_player) _mmplayer_is_streaming(x_player)
282 #define MMPLAYER_IS_RTSP_STREAMING(x_player) _mmplayer_is_rtsp_streaming(x_player)
283 #define MMPLAYER_IS_HTTP_STREAMING(x_player) _mmplayer_is_http_streaming(x_player)
284 #define MMPLAYER_IS_HTTP_LIVE_STREAMING(x_player) _mmplayer_is_http_live_streaming(x_player)
285 #define MMPLAYER_IS_LIVE_STREAMING(x_player) _mmplayer_is_live_streaming(x_player)
286 #define MMPLAYER_IS_DASH_STREAMING(x_player) _mmplayer_is_dash_streaming(x_player)
287 #define MMPLAYER_IS_SMOOTH_STREAMING(x_player) _mmplayer_is_smooth_streaming(x_player)
288 #define MMPLAYER_IS_MS_BUFF_SRC(x_player) _mmplayer_is_ms_buff_src(x_player)
289
290 #define MMPLAYER_URL_HAS_DASH_SUFFIX(x_player) _mmplayer_has_suffix(x_player, ".mpd")
291 #define MMPLAYER_URL_HAS_HLS_SUFFIX(x_player) _mmplayer_has_suffix(x_player, ".m3u8")
292
293 #define MMPLAYER_STREAM_TYPE_GET_NAME(type) _mmplayer_get_stream_type_name(type)
294
295 #define MMPLAYER_USE_DECODEBIN(x_player) _mmplayer_use_decodebin(x_player)
296
297 /*===========================================================================================
298 |                                                                                                                                                                                       |
299 |  GLOBAL FUNCTION PROTOTYPES                                                                                                                           |
300 |                                                                                                                                                                                       |
301 ========================================================================================== */
302
303 const gchar *_mmplayer_get_state_name(int state);
304 const gchar *_mmplayer_get_stream_type_name(int type);
305
306 gboolean _mmplayer_is_streaming(mmplayer_t *player);
307 gboolean _mmplayer_is_rtsp_streaming(mmplayer_t *player);
308 gboolean _mmplayer_is_live_streaming(mmplayer_t *player);
309 gboolean _mmplayer_is_http_streaming(mmplayer_t *player);
310 gboolean _mmplayer_is_http_live_streaming(mmplayer_t *player);
311 gboolean _mmplayer_is_dash_streaming(mmplayer_t *player);
312 gboolean _mmplayer_is_smooth_streaming(mmplayer_t *player);
313 gboolean _mmplayer_is_ms_buff_src(mmplayer_t *player);
314 gboolean _mmplayer_has_suffix(mmplayer_t *player, const gchar *suffix);
315 gboolean _mmplayer_is_videosink_ready(mmplayer_t *player, int surface_type);
316 gboolean _mmplayer_use_decodebin(mmplayer_t *player);
317
318 gboolean _mmplayer_post_message(mmplayer_t *player, enum MMMessageType msgtype, MMMessageParamType *param);
319 gboolean _mmplayer_dump_pipeline_state(mmplayer_t *player);
320
321 bool _mmplayer_is_sdp_file(const char *path);
322 int _mmplayer_exist_file_path(const char *file_path);
323 char **_mmplayer_get_cookie_list(const char *cookies);
324 const char *_mmplayer_get_charset(const char *file_path);
325 int _mmplayer_get_pixtype(unsigned int fourcc);
326 bool _mmplayer_get_storage_info(const char *path, mmplayer_storage_info_t *storage_info);
327
328 /* for command lock */
329 void _mmplayer_cmd_lock_init(mmplayer_t *player);
330 void _mmplayer_cmd_lock_deinit(mmplayer_t *player);
331 void _mmplayer_cmd_lock(mmplayer_t *player);
332 gboolean _mmplayer_cmd_trylock(mmplayer_t *player);
333 void _mmplayer_cmd_unlock(mmplayer_t *player);
334 media_format_mimetype_e _mmplayer_convert_audio_pcm_str_to_media_format_mime(const gchar *audio_pcm_str);
335
336 #ifdef __cplusplus
337         }
338 #endif
339
340 #endif /* __MM_PLAYER_UTILS_H__ */