Support negative position for setting display roi area
[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 } while (0)
58
59 #define MMPLAYER_GET_ATTRS(x_player) ((mm_player_t*)x_player)->attrs
60
61 /* command */
62 #define MMPLAYER_CMD_LOCK(x_player)                          g_mutex_lock(&((mm_player_t *)x_player)->cmd_lock)
63 #define MMPLAYER_CMD_TRYLOCK(x_player)                       g_mutex_trylock(&((mm_player_t *)x_player)->cmd_lock)
64 #define MMPLAYER_CMD_UNLOCK(x_player)                        g_mutex_unlock(&((mm_player_t*)x_player)->cmd_lock)
65
66 /* playback */
67 #define MMPLAYER_PLAYBACK_LOCK(x_player)                     g_mutex_lock(&((mm_player_t *)x_player)->playback_lock)
68 #define MMPLAYER_PLAYBACK_UNLOCK(x_player)                   g_mutex_unlock(&((mm_player_t*)x_player)->playback_lock)
69
70 /* capture thread */
71 #define MMPLAYER_CAPTURE_THREAD_LOCK(x_player)               g_mutex_lock(&((mm_player_t *)x_player)->capture_thread_mutex)
72 #define MMPLAYER_CAPTURE_THREAD_UNLOCK(x_player)             g_mutex_unlock(&((mm_player_t *)x_player)->capture_thread_mutex)
73 #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)
74 #define MMPLAYER_CAPTURE_THREAD_SIGNAL(x_player)             g_cond_signal(&((mm_player_t *)x_player)->capture_thread_cond);
75
76 /* gapless play thread */
77 #define MMPLAYER_GAPLESS_PLAY_THREAD_LOCK(x_player)             g_mutex_lock(&((mm_player_t *)x_player)->gapless_play_thread_mutex)
78 #define MMPLAYER_GAPLESS_PLAY_THREAD_UNLOCK(x_player)           g_mutex_unlock(&((mm_player_t *)x_player)->gapless_play_thread_mutex)
79 #define MMPLAYER_GAPLESS_PLAY_THREAD_WAIT(x_player)             g_cond_wait(&((mm_player_t *)x_player)->gapless_play_thread_cond, &((mm_player_t *)x_player)->gapless_play_thread_mutex)
80 #define MMPLAYER_GAPLESS_PLAY_THREAD_SIGNAL(x_player)           g_cond_signal(&((mm_player_t *)x_player)->gapless_play_thread_cond);
81
82 /* gst bus msg thread */
83 #define MMPLAYER_BUS_MSG_THREAD_LOCK(x_player)                   g_mutex_lock(&((mm_player_t *)x_player)->bus_msg_thread_mutex)
84 #define MMPLAYER_BUS_MSG_THREAD_UNLOCK(x_player)                 g_mutex_unlock(&((mm_player_t *)x_player)->bus_msg_thread_mutex)
85 #define MMPLAYER_BUS_MSG_THREAD_WAIT(x_player)                   g_cond_wait(&((mm_player_t *)x_player)->bus_msg_thread_cond, &((mm_player_t *)x_player)->bus_msg_thread_mutex)
86 #define MMPLAYER_BUS_MSG_THREAD_WAIT_UNTIL(x_player, end_time)   g_cond_wait_until(&((mm_player_t *)x_player)->bus_msg_thread_cond, &((mm_player_t *)x_player)->bus_msg_thread_mutex, end_time)
87 #define MMPLAYER_BUS_MSG_THREAD_SIGNAL(x_player)                 g_cond_signal(&((mm_player_t *)x_player)->bus_msg_thread_cond);
88
89 /* handling fakesink */
90 #define MMPLAYER_FSINK_LOCK(x_player)                        g_mutex_lock(&((mm_player_t *)x_player)->fsink_lock)
91 #define MMPLAYER_FSINK_UNLOCK(x_player)                      g_mutex_unlock(&((mm_player_t *)x_player)->fsink_lock)
92
93 /* handling update tag */
94 #define MMPLAYER_UPDATE_TAG_LOCK(x_player)                   g_mutex_lock(&((mm_player_t *)x_player)->update_tag_lock)
95 #define MMPLAYER_UPDATE_TAG_UNLOCK(x_player)                 g_mutex_unlock(&((mm_player_t *)x_player)->update_tag_lock)
96
97 /* video stream bo */
98 #define MMPLAYER_VIDEO_BO_LOCK(x_player)                     g_mutex_lock(&((mm_player_t *)x_player)->video_bo_mutex)
99 #define MMPLAYER_VIDEO_BO_UNLOCK(x_player)                   g_mutex_unlock(&((mm_player_t *)x_player)->video_bo_mutex)
100 #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)
101 #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)
102 #define MMPLAYER_VIDEO_BO_SIGNAL(x_player)                   g_cond_signal(&((mm_player_t *)x_player)->video_bo_cond);
103
104 /* media stream lock */
105 #define MMPLAYER_MEDIA_STREAM_CALLBACK_LOCK(x_player)        g_mutex_lock(&((mm_player_t *)x_player)->media_stream_cb_lock)
106 #define MMPLAYER_MEDIA_STREAM_CALLBACK_UNLOCK(x_player)      g_mutex_unlock(&((mm_player_t *)x_player)->media_stream_cb_lock)
107
108 /* subtitle info */
109 #define MMPLAYER_SUBTITLE_INFO_LOCK(x_player)                     g_mutex_lock(&((mm_player_t *)x_player)->subtitle_info_mutex)
110 #define MMPLAYER_SUBTITLE_INFO_UNLOCK(x_player)                   g_mutex_unlock(&((mm_player_t *)x_player)->subtitle_info_mutex)
111 #define MMPLAYER_SUBTITLE_INFO_WAIT(x_player)                     g_cond_wait(&((mm_player_t *)x_player)->subtitle_info_cond, &((mm_player_t *)x_player)->subtitle_info_mutex)
112 #define MMPLAYER_SUBTITLE_INFO_WAIT_UNTIL(x_player, end_time)     g_cond_wait_until(&((mm_player_t *)x_player)->subtitle_info_cond, &((mm_player_t *)x_player)->subtitle_info_mutex, end_time)
113 #define MMPLAYER_SUBTITLE_INFO_SIGNAL(x_player)                   g_cond_signal(&((mm_player_t *)x_player)->subtitle_info_cond);
114
115 #if 0
116 #define MMPLAYER_FENTER();                                      LOGD("<ENTER>");
117 #define MMPLAYER_FLEAVE();                                      LOGD("<LEAVE>");
118 #else
119 #define MMPLAYER_FENTER();
120 #define MMPLAYER_FLEAVE();
121 #endif
122
123 #define MMPLAYER_RETURN_IF_FAIL(expr) \
124 do { \
125         if (!(expr)) { \
126                 LOGW("failed [%s]", #expr); \
127                 return; \
128         } \
129 } while (0)
130
131 #define MMPLAYER_RETURN_VAL_IF_FAIL(expr, var) \
132 do { \
133         if (!(expr)) { \
134                 LOGW("failed [%s]", #expr); \
135                 return (var); \
136         } \
137 } while (0)
138
139 /* debug caps string */
140 #define MMPLAYER_LOG_GST_CAPS_TYPE(x_caps) \
141 do { \
142         gchar *caps_type = NULL; \
143         if (x_caps) { \
144                 caps_type = gst_caps_to_string(x_caps); \
145                 LOGD("caps: %s", caps_type); \
146                 MMPLAYER_FREEIF(caps_type); \
147         } else {\
148                 LOGW("caps is null"); \
149         } \
150 } while (0)
151
152 /* message posting */
153 #define MMPLAYER_POST_MSG(x_player, x_msgtype, x_msg_param) \
154 do { \
155         LOGD("[handle: %p] posting %s to application", x_player, #x_msgtype); \
156         __mmplayer_post_message(x_player, x_msgtype, x_msg_param); \
157 } while (0)
158
159 /* setting player state */
160 #define MMPLAYER_SET_STATE(x_player, x_state) \
161 do { \
162         LOGD("[handle: %p] update state machine to %d", x_player, x_state); \
163         __mmplayer_set_state(x_player, x_state); \
164 } while (0)
165
166 #define MMPLAYER_CHECK_STATE(x_player, x_command) \
167 do {    \
168         LOGD("[handle: %p] checking player state before doing %s", x_player, #x_command); \
169         switch (__mmplayer_check_state(x_player, x_command)) { \
170         case MM_ERROR_PLAYER_INVALID_STATE: \
171                 return MM_ERROR_PLAYER_INVALID_STATE; \
172                 break; \
173                 /* NOTE : for robustness of player. we won't treat it as an error */ \
174         case MM_ERROR_PLAYER_NO_OP: \
175                 return MM_ERROR_NONE; \
176                 break; \
177         case MM_ERROR_PLAYER_DOING_SEEK: \
178                 return MM_ERROR_PLAYER_DOING_SEEK; \
179         default: \
180                 break; \
181         }       \
182 } while (0)
183
184 /* setting element state */
185 #define MMPLAYER_ELEMENT_SET_STATE(x_element, x_state) \
186 do {    \
187         LOGD("setting state [%s:%d] to [%s]", #x_state, x_state, GST_ELEMENT_NAME(x_element)); \
188         if (GST_STATE_CHANGE_FAILURE == gst_element_set_state(x_element, x_state)) { \
189                 LOGE("failed to set state %s to %s", #x_state, GST_ELEMENT_NAME(x_element)); \
190                 goto STATE_CHANGE_FAILED; \
191         }       \
192 } while (0)
193
194 #define MMPLAYER_CHECK_NULL(x_var) \
195 if (!x_var) { \
196         LOGE("[%s] is NULL", #x_var); \
197         goto ERROR; \
198 }
199
200 /* generating dot */
201 #define MMPLAYER_GENERATE_DOT_IF_ENABLED(x_player, x_name) \
202 if (x_player->ini.generate_dot) { \
203         GST_DEBUG_BIN_TO_DOT_FILE(GST_BIN(player->pipeline->mainbin[MMPLAYER_M_PIPE].gst), \
204         GST_DEBUG_GRAPH_SHOW_ALL, x_name); \
205 }
206
207 /* signal manipulation */
208 #define MMPLAYER_SIGNAL_CONNECT(x_player, x_object, x_type, x_signal, x_callback, x_arg) \
209 do { \
210         MMPlayerSignalItem *item = NULL; \
211         item = (MMPlayerSignalItem*) g_malloc(sizeof(MMPlayerSignalItem)); \
212         if (!item) { \
213                 LOGE("cannot connect signal [%s]", x_signal); \
214         } else { \
215                 item->obj = G_OBJECT(x_object); \
216                 item->sig = g_signal_connect(G_OBJECT(x_object), x_signal, \
217                                         x_callback, x_arg); \
218                 if ((x_type >= MM_PLAYER_SIGNAL_TYPE_AUTOPLUG) && (x_type < MM_PLAYER_SIGNAL_TYPE_MAX)) \
219                         x_player->signals[x_type] = g_list_append(x_player->signals[x_type], item); \
220                 else \
221                         LOGE("wrong signal type [%d]", x_type); \
222         } \
223 } while (0)
224
225 /* release element resource */
226 #define MMPLAYER_RELEASE_ELEMENT(x_player, x_bin, x_id) \
227 do { \
228         if (x_bin[x_id].gst) { \
229                 gst_element_set_state(x_bin[x_id].gst, GST_STATE_NULL); \
230                 gst_bin_remove(GST_BIN(x_player->pipeline->mainbin[MMPLAYER_M_PIPE].gst), x_bin[x_id].gst); \
231                 x_bin[x_id].gst = NULL; \
232                 LOGD("release done [element %d]", x_id); \
233         } \
234 } while (0)
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 LOGD("-- prev %s, current %s, pending %s, target %s --", \
245         MMPLAYER_STATE_GET_NAME(MMPLAYER_PREV_STATE(x_player)), \
246         MMPLAYER_STATE_GET_NAME(MMPLAYER_CURRENT_STATE(x_player)), \
247         MMPLAYER_STATE_GET_NAME(MMPLAYER_PENDING_STATE(x_player)), \
248         MMPLAYER_STATE_GET_NAME(MMPLAYER_TARGET_STATE(x_player)));
249
250 #define MMPLAYER_STATE_CHANGE_TIMEOUT(x_player)  ((mm_player_t*)x_player)->state_change_timeout
251
252 /* streaming */
253 #define MMPLAYER_IS_STREAMING(x_player) __is_streaming(x_player)
254 #define MMPLAYER_IS_RTSP_STREAMING(x_player) __is_rtsp_streaming(x_player)
255 #define MMPLAYER_IS_HTTP_STREAMING(x_player) __is_http_streaming(x_player)
256 #define MMPLAYER_IS_HTTP_LIVE_STREAMING(x_player) __is_http_live_streaming(x_player)
257 #define MMPLAYER_IS_LIVE_STREAMING(x_player) __is_live_streaming(x_player)
258 #define MMPLAYER_IS_DASH_STREAMING(x_player) __is_dash_streaming(x_player)
259 #define MMPLAYER_IS_SMOOTH_STREAMING(x_player) __is_smooth_streaming(x_player)
260 #define MMPLAYER_IS_MS_BUFF_SRC(x_player) __is_ms_buff_src(x_player)
261
262 #define MMPLAYER_URL_HAS_DASH_SUFFIX(x_player) __has_suffix(x_player, "mpd")
263 #define MMPLAYER_URL_HAS_HLS_SUFFIX(x_player) __has_suffix(x_player, "m3u8")
264
265 #define MMPLAYER_STREAM_TYPE_GET_NAME(type) __get_stream_type_name(type)
266
267 /*===========================================================================================
268 |                                                                                                                                                                                       |
269 |  GLOBAL FUNCTION PROTOTYPES                                                                                                                           |
270 |                                                                                                                                                                                       |
271 ========================================================================================== */
272
273 const gchar* __get_state_name(int state);
274 const gchar* __get_stream_type_name(int type);
275
276 gboolean __is_streaming(mm_player_t* player);
277 gboolean __is_rtsp_streaming(mm_player_t* player);
278 gboolean __is_live_streaming(mm_player_t* player);
279 gboolean __is_http_streaming(mm_player_t* player);
280 gboolean __is_http_live_streaming(mm_player_t* player);
281 gboolean __is_dash_streaming(mm_player_t* player);
282 gboolean __is_smooth_streaming(mm_player_t* player);
283 gboolean __is_ms_buff_src(mm_player_t* player);
284 gboolean __has_suffix(mm_player_t* player, const gchar* suffix);
285
286 gboolean __mmplayer_post_message(mm_player_t* player, enum MMMessageType msgtype, MMMessageParamType* param);
287 gboolean __mmplayer_dump_pipeline_state(mm_player_t* player);
288
289 bool util_is_sdp_file(const char *path);
290 int util_exist_file_path(const char *file_path);
291 char** util_get_cookie_list(const char *cookies);
292 const char* util_get_charset(const char *file_path);
293 int util_get_pixtype(unsigned int fourcc);
294 bool util_get_storage_info(const char *path, MMPlayerStorageInfo *storage_info);
295
296 #ifdef __cplusplus
297         }
298 #endif
299
300 #endif /* __MM_PLAYER_UTILS_H__ */
301