add resource limit return during player create
[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_CMD_LOCK(x_player) g_mutex_lock(&((mm_player_t *)x_player)->cmd_lock)
56 #define MMPLAYER_CMD_UNLOCK(x_player)   g_mutex_unlock( &((mm_player_t*)x_player)->cmd_lock )
57 #define MMPLAYER_PLAYBACK_LOCK(x_player) g_mutex_lock(&((mm_player_t *)x_player)->playback_lock)
58 #define MMPLAYER_PLAYBACK_UNLOCK(x_player) g_mutex_unlock( &((mm_player_t*)x_player)->playback_lock )
59 #define MMPLAYER_GET_ATTRS(x_player)            ((mm_player_t*)x_player)->attrs
60
61 #if 0
62 #define MMPLAYER_FENTER();                                      LOGD("<ENTER>");
63 #define MMPLAYER_FLEAVE();                                      LOGD("<LEAVE>");
64 #else
65 #define MMPLAYER_FENTER();
66 #define MMPLAYER_FLEAVE();
67 #endif
68
69 #define MAX_SOUND_DEVICE_LEN    18
70
71 /* element linking */
72 #ifdef GST_EXT_PAD_LINK_UNCHECKED
73 #define GST_ELEMENT_LINK_FILTERED       gst_element_link_filtered_unchecked
74 #define GST_ELEMENT_LINK_MANY           gst_element_link_many_unchecked
75 #define GST_ELEMENT_LINK                        gst_element_link_unchecked
76 #define GST_ELEMENT_LINK_PADS           gst_element_link_pads_unchecked
77 #define GST_PAD_LINK                            gst_pad_link_unchecked
78 #else
79 #define GST_ELEMENT_LINK_FILTERED       gst_element_link_filtered
80 #define GST_ELEMENT_LINK_MANY           gst_element_link_many
81 #define GST_ELEMENT_LINK                        gst_element_link
82 #define GST_ELEMENT_UNLINK                      gst_element_unlink
83 #define GST_ELEMENT_LINK_PADS           gst_element_link_pads
84 #define GST_PAD_LINK                            gst_pad_link
85 #endif
86
87 #define MMPLAYER_RETURN_IF_FAIL(expr) \
88                 if(!(expr)) { \
89                         LOGW("faild [%s]", #expr); \
90                         return; \
91                 }
92
93 #define MMPLAYER_RETURN_VAL_IF_FAIL(expr, var) \
94                 if(!(expr)) { \
95                         LOGW("faild [%s]", #expr); \
96                         return (var); \
97                 }
98
99 /* debug caps string */
100 #define MMPLAYER_LOG_GST_CAPS_TYPE(x_caps) \
101 do \
102 { \
103         gchar* caps_type = NULL; \
104         caps_type = gst_caps_to_string(x_caps); \
105         LOGD ("caps: %s\n", caps_type ); \
106         MMPLAYER_FREEIF (caps_type) \
107 } while (0)
108
109 /* message posting */
110 #define MMPLAYER_POST_MSG( x_player, x_msgtype, x_msg_param ) \
111 LOGD("posting %s to application\n", #x_msgtype); \
112 __mmplayer_post_message(x_player, x_msgtype, x_msg_param);
113
114 /* setting player state */
115 #define MMPLAYER_SET_STATE( x_player, x_state ) \
116 LOGD("update state machine to %d\n", x_state); \
117 __mmplayer_set_state(x_player, x_state);
118
119 #define MMPLAYER_CHECK_STATE( x_player, x_command ) \
120 LOGD("checking player state before doing %s\n", #x_command); \
121 switch ( __mmplayer_check_state(x_player, x_command) ) \
122 { \
123         case MM_ERROR_PLAYER_INVALID_STATE: \
124                 return MM_ERROR_PLAYER_INVALID_STATE; \
125         break; \
126         /* NOTE : for robustness of player. we won't treat it as an error */ \
127         case MM_ERROR_PLAYER_NO_OP: \
128                 return MM_ERROR_NONE; \
129         break; \
130         case MM_ERROR_PLAYER_DOING_SEEK: \
131                 return MM_ERROR_PLAYER_DOING_SEEK; \
132         default: \
133         break; \
134 }
135
136 /* setting element state */
137 #define MMPLAYER_ELEMENT_SET_STATE( x_element, x_state ) \
138 LOGD("setting state [%s:%d] to [%s]\n", #x_state, x_state, GST_ELEMENT_NAME( x_element ) ); \
139 if ( GST_STATE_CHANGE_FAILURE == gst_element_set_state ( x_element, x_state) ) \
140 { \
141         LOGE("failed to set state %s to %s\n", #x_state, GST_ELEMENT_NAME( x_element )); \
142         goto STATE_CHANGE_FAILED; \
143 }
144
145 #define MMPLAYER_CHECK_NULL( x_var ) \
146 if ( ! x_var ) \
147 { \
148         LOGE("[%s] is NULL\n", #x_var ); \
149         goto ERROR; \
150 }
151
152 #define MMPLAYER_CHECK_CMD_IF_EXIT( x_player ) \
153 if ( x_player->cmd == MMPLAYER_COMMAND_UNREALIZE || x_player->cmd == MMPLAYER_COMMAND_DESTROY ) \
154 { \
155         LOGD("it's exit state...\n");\
156         goto ERROR;  \
157 }
158
159 /* generating dot */
160 #define MMPLAYER_GENERATE_DOT_IF_ENABLED( x_player, x_name ) \
161 if ( x_player->ini.generate_dot ) \
162 { \
163         GST_DEBUG_BIN_TO_DOT_FILE (GST_BIN (player->pipeline->mainbin[MMPLAYER_M_PIPE].gst), \
164         GST_DEBUG_GRAPH_SHOW_ALL, x_name); \
165 }
166
167 /* signal manipulation */
168 #define MMPLAYER_SIGNAL_CONNECT( x_player, x_object, x_type, x_signal, x_callback, x_arg ) \
169 do \
170 { \
171         MMPlayerSignalItem* item = NULL; \
172         item = (MMPlayerSignalItem*) g_malloc( sizeof (MMPlayerSignalItem) ); \
173         if ( ! item ) \
174         { \
175                 LOGE("cannot connect signal [%s]\n", x_signal ); \
176         } \
177         else \
178         { \
179                 item->obj = G_OBJECT( x_object ); \
180                 item->sig = g_signal_connect( G_OBJECT(x_object), x_signal, \
181                                         x_callback, x_arg ); \
182                 if ((x_type >= MM_PLAYER_SIGNAL_TYPE_AUTOPLUG) && (x_type < MM_PLAYER_SIGNAL_TYPE_MAX)) \
183                         x_player->signals[x_type] = g_list_append(x_player->signals[x_type], item); \
184                 else \
185                         LOGE("wrong signal type [%d]\n", x_type ); \
186         } \
187 } while ( 0 );
188
189 /* release element resource */
190 #define MMPLAYER_RELEASE_ELEMENT( x_player, x_bin, x_id ) \
191 do \
192 { \
193         if (x_bin[x_id].gst) \
194         { \
195                 gst_element_set_state(x_bin[x_id].gst, GST_STATE_NULL); \
196                 gst_bin_remove(GST_BIN(x_player->pipeline->mainbin[MMPLAYER_M_PIPE].gst), x_bin[x_id].gst); \
197                 x_bin[x_id].gst = NULL; \
198                 LOGD("release done [element %d]", x_id); \
199         } \
200 } while ( 0 )
201
202 /* state */
203 #define MMPLAYER_PREV_STATE(x_player)           ((mm_player_t*)x_player)->prev_state
204 #define MMPLAYER_CURRENT_STATE(x_player)                ((mm_player_t*)x_player)->state
205 #define         MMPLAYER_PENDING_STATE(x_player)                ((mm_player_t*)x_player)->pending_state
206 #define         MMPLAYER_TARGET_STATE(x_player)         ((mm_player_t*)x_player)->target_state
207 #define         MMPLAYER_STATE_GET_NAME(state) __get_state_name(state)
208
209 #define         MMPLAYER_PRINT_STATE(x_player) \
210 LOGD("-- prev %s, current %s, pending %s, target %s --\n", \
211         MMPLAYER_STATE_GET_NAME(MMPLAYER_PREV_STATE(x_player)), \
212         MMPLAYER_STATE_GET_NAME(MMPLAYER_CURRENT_STATE(x_player)), \
213         MMPLAYER_STATE_GET_NAME(MMPLAYER_PENDING_STATE(x_player)), \
214         MMPLAYER_STATE_GET_NAME(MMPLAYER_TARGET_STATE(x_player)));
215
216 #define         MMPLAYER_STATE_CHANGE_TIMEOUT(x_player )         ((mm_player_t*)x_player)->state_change_timeout
217
218 /* streaming */
219 #define MMPLAYER_IS_STREAMING(x_player)                         __is_streaming(x_player)
220 #define MMPLAYER_IS_RTSP_STREAMING(x_player)    __is_rtsp_streaming(x_player)
221 #define MMPLAYER_IS_WFD_STREAMING(x_player)     __is_wfd_streaming(x_player)
222 #define MMPLAYER_IS_HTTP_STREAMING(x_player)    __is_http_streaming(x_player)
223 #define MMPLAYER_IS_HTTP_PD(x_player)                   __is_http_progressive_down(x_player)
224 #define MMPLAYER_IS_HTTP_LIVE_STREAMING(x_player)  __is_http_live_streaming(x_player)
225 #define MMPLAYER_IS_LIVE_STREAMING(x_player)    __is_live_streaming(x_player)
226 #define MMPLAYER_IS_DASH_STREAMING(x_player)    __is_dash_streaming(x_player)
227 #define MMPLAYER_IS_SMOOTH_STREAMING(x_player)  __is_smooth_streaming(x_player)
228 #define MMPLAYER_IS_MS_BUFF_SRC(x_player)               __is_ms_buff_src(x_player)
229
230 #define MMPLAYER_URL_HAS_DASH_SUFFIX(x_player) __has_suffix(x_player, "mpd")
231 #define MMPLAYER_URL_HAS_HLS_SUFFIX(x_player) __has_suffix(x_player, "m3u8")
232
233 /* etc */
234 #define MMF_PLAYER_FILE_BACKUP_PATH             "/tmp/media_temp."
235 #define         MMPLAYER_PT_IS_AUDIO( x_pt )            ( strstr(x_pt, "_97") || strstr(x_pt, "audio") )
236 #define         MMPLAYER_PT_IS_VIDEO( x_pt )            ( strstr(x_pt, "_96") || strstr(x_pt, "video") )
237
238 #define MMPLAYER_VIDEO_SINK_CHECK(x_player) \
239 do \
240 { \
241         MMPLAYER_RETURN_VAL_IF_FAIL ( x_player && \
242                 x_player->pipeline && \
243                 x_player->pipeline->videobin && \
244                 x_player->pipeline->videobin[MMPLAYER_V_SINK].gst, \
245                 MM_ERROR_PLAYER_NOT_INITIALIZED ); \
246 } while(0)
247
248 enum
249 {
250         MMPLAYER_DISPLAY_NULL = 0,
251         MMPLAYER_DISPLAY_HDMI_ACTIVE,
252         MMPLAYER_DISPLAY_MIRRORING_ACTIVE,
253 };
254
255 bool util_is_sdp_file ( const char *path );
256 int util_get_rank_increase ( const char *factory_class );
257 int util_factory_rank_compare(GstPluginFeature *f1, GstPluginFeature *f2); // @
258 int util_exist_file_path(const char *file_path);
259 bool util_write_file_backup(const char *backup_path, char *data_ptr, int data_size);
260 bool util_remove_file_backup(const char *backup_path); /* For Midi Player */
261 int util_is_midi_type_by_mem(void *mem, int size);
262 int util_is_midi_type_by_file(const char *file_path);
263 char** util_get_cookie_list ( const char *cookies );
264 bool util_check_valid_url ( const char *proxy );
265 const char* util_get_charset(const char *file_path);
266 int util_get_pixtype(unsigned int fourcc);
267
268 #ifdef __cplusplus
269         }
270 #endif
271
272 #endif /* __MM_PLAYER_UTILS_H__ */
273