2.0 beta init
[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 __MMF_PLAYER_UTILS_H__
24 #define __MMF_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
52 #define MMPLAYER_MSG_POST_LOCK(x_player)        g_mutex_lock( ((mm_player_t*)x_player)->msg_cb_lock )
53 #define MMPLAYER_MSG_POST_UNLOCK(x_player)      g_mutex_unlock( ((mm_player_t*)x_player)->msg_cb_lock )
54
55 #define MMPLAYER_GET_ATTRS(x_player)            ((mm_player_t*)x_player)->attrs
56
57 /* sbs : for bluetooth */
58 #define MAX_SOUND_DEVICE_LEN    18      
59
60 /* element linking */
61 #ifdef GST_EXT_PAD_LINK_UNCHECKED
62 #define GST_ELEMENT_LINK_FILTERED       gst_element_link_filtered_unchecked
63 #define GST_ELEMENT_LINK_MANY           gst_element_link_many_unchecked
64 #define GST_ELEMENT_LINK                        gst_element_link_unchecked
65 #define GST_ELEMENT_LINK_PADS           gst_element_link_pads_unchecked
66 #define GST_PAD_LINK                            gst_pad_link_unchecked
67 #else
68 #define GST_ELEMENT_LINK_FILTERED       gst_element_link_filtered
69 #define GST_ELEMENT_LINK_MANY           gst_element_link_many
70 #define GST_ELEMENT_LINK                        gst_element_link
71 #define GST_ELEMENT_LINK_PADS           gst_element_link_pads
72 #define GST_PAD_LINK                            gst_pad_link
73 #endif
74
75 /* debug caps string */
76 #define MMPLAYER_LOG_GST_CAPS_TYPE(x_caps) \
77 do \
78 { \
79         gchar* caps_type = NULL; \
80         caps_type = gst_caps_to_string(x_caps); \
81         debug_log ("caps: %s\n", caps_type ); \
82         MMPLAYER_FREEIF (caps_type) \
83 } while (0);
84
85 /* message posting */
86 #define MMPLAYER_POST_MSG( x_player, x_msgtype, x_msg_param ) \
87 debug_log("posting %s to application\n", #x_msgtype); \
88 __mmplayer_post_message(x_player, x_msgtype, x_msg_param);
89
90 /* setting player state */
91 #define MMPLAYER_SET_STATE( x_player, x_state ) \
92 debug_log("setting player state to %d\n", x_state); \
93 __mmplayer_set_state(x_player, x_state);
94
95
96 #define MMPLAYER_CHECK_STATE_RETURN_IF_FAIL( x_player, x_command ) \
97 debug_log("checking player state before doing %s\n", #x_command); \
98 switch ( __mmplayer_check_state(x_player, x_command) ) \
99 { \
100         case MM_ERROR_PLAYER_INVALID_STATE: \
101                 return MM_ERROR_PLAYER_INVALID_STATE; \
102         break; \
103         /* NOTE : for robustness of player. we won't treat it as an error */ \
104         case MM_ERROR_PLAYER_NO_OP: \
105                 return MM_ERROR_NONE; \
106         break; \
107         default: \
108         break; \
109 }
110
111 /* setting element state */ 
112 #define MMPLAYER_ELEMENT_SET_STATE( x_element, x_state ) \
113 debug_log("setting state [%s:%d] to [%s]\n", #x_state, x_state, GST_ELEMENT_NAME( x_element ) ); \
114 if ( GST_STATE_CHANGE_FAILURE == gst_element_set_state ( x_element, x_state) ) \
115 { \
116         debug_error("failed to set state %s to %s\n", #x_state, GST_ELEMENT_NAME( x_element )); \
117         goto STATE_CHANGE_FAILED; \
118 }
119
120 #define MMPLAYER_CHECK_NULL( x_var ) \
121 if ( ! x_var ) \
122 { \
123         debug_error("[%s] is NULL\n", #x_var ); \
124         goto ERROR; \
125 }
126
127 #define MMPLAYER_CHECK_CMD_IF_EXIT( x_player ) \
128 if ( x_player->cmd == MMPLAYER_COMMAND_UNREALIZE || x_player->cmd == MMPLAYER_COMMAND_DESTROY ) \
129 { \
130         debug_log("it's exit state...\n");\
131         goto ERROR;  \
132 }
133 /* volume */
134 /* 
135 |----|-------|-------|-------|-------|
136 |Res. | HFK(7)  |  BT(7)  |  E.J(7)  | SPK(7) |
137 |----|-------|-------|-------|-------|
138 */
139
140 /* 090424 Fix me : Currently volume is 0~9, so bt volume can be only 0.0 ~ 0.9 */
141 #define GET_VOLUME_BT(volume) (volume/10.)
142
143 #if 0
144 #define GET_VOLUME_SPK(volume) ((volume) & 0x7F)
145 #define GET_VOLUME_EARJACK(volume) ((volume >> 7)& 0x7F)
146 #define GET_VOLUME_HFK(volume) ((volume >> 21) & 0x7F)
147
148 #define SET_VOLUME_SPK(volume,input) (volume |= (input &0x7F))
149 #define SET_VOLUME_EARJACK(volume,input) (volume |= ((input & 0x7F)<<7))
150 #define SET_VOLUME_BT(volume,input) (volume |= ((input & 0x7F)<<14))
151 #define SET_VOLUME_HFK(volume,input) (volume |= ((input & 0x7F)<<21))
152 #endif
153
154
155 /* pad probe for pipeilne debugging */
156 gboolean __util_gst_pad_probe(GstPad *pad, GstBuffer *buffer, gpointer u_data);
157
158 #define MM_PROBE_DEFAULT                        (0)
159 #define MM_PROBE_TIMESTAMP                      (1)
160 #define MM_PROBE_BUFFERSIZE                     (1 << 1)
161 #define MM_PROBE_CAPS                           (1 << 2)
162 #define MM_PROBE_BUFFER_DURATION        (1 << 3)
163 #define MM_PROBE_DROP_BUFFER            (1 << 4)
164 #define MM_PROBE_CLOCK_TIME                     (1 << 5)
165 /* ... add more */
166
167 /* messages are treated as warnings bcz those code should not be checked in. 
168  * and no error handling will supported for same manner. 
169  */
170 #define MMPLAYER_ADD_PROBE(x_pad, x_flag) \
171 debug_warning("adding pad probe\n"); \
172 if ( ! gst_pad_add_buffer_probe(x_pad, \
173         G_CALLBACK(__util_gst_pad_probe), \
174         (gpointer)x_flag) ) \
175 { \
176         debug_error("failed to add pad probe\n"); \
177 }
178
179
180 /* generating dot */
181 #define MMPLAYER_GENERATE_DOT_IF_ENABLED( x_player, x_name ) \
182 if ( PLAYER_INI()->generate_dot ) \
183 { \
184         debug_log("generating dot file(%s)\n", #x_name); \
185         GST_DEBUG_BIN_TO_DOT_FILE (GST_BIN (player->pipeline->mainbin[MMPLAYER_M_PIPE].gst), \
186         GST_DEBUG_GRAPH_SHOW_ALL, x_name); \
187 }
188
189 /* signal manipulation */
190 #define MMPLAYER_SIGNAL_CONNECT( x_player, x_object, x_signal, x_callback, x_arg ) \
191 do \
192 { \
193         MMPlayerSignalItem* item = NULL; \
194         item = (MMPlayerSignalItem*) g_malloc( sizeof (MMPlayerSignalItem) ); \
195         if ( ! item ) \
196         { \
197                 debug_error("cannot connect signal [%s]\n", x_signal ); \
198         } \
199         else \
200         { \
201                 item->obj = G_OBJECT( x_object ); \
202                 item->sig = g_signal_connect( G_OBJECT(x_object), x_signal, \
203                                         x_callback, x_arg ); \
204                 x_player->signals = g_list_append(x_player->signals, item); \
205         } \
206 } while ( 0 );
207
208
209 /* state */
210 #define MMPLAYER_PREV_STATE(x_player)           ((mm_player_t*)x_player)->prev_state 
211 #define MMPLAYER_CURRENT_STATE(x_player)                ((mm_player_t*)x_player)->state 
212 #define         MMPLAYER_PENDING_STATE(x_player)                ((mm_player_t*)x_player)->pending_state 
213 #define         MMPLAYER_TARGET_STATE(x_player)         ((mm_player_t*)x_player)->target_state 
214 #define         MMPLAYER_STATE_GET_NAME(state) __get_state_name(state)
215
216 #define         MMPLAYER_PRINT_STATE(x_player) \
217 debug_log("-----------------------PLAYER STATE-------------------------\n"); \
218 debug_log(" prev %s, current %s, pending %s, target %s \n", \
219         MMPLAYER_STATE_GET_NAME(MMPLAYER_PREV_STATE(x_player)), \
220         MMPLAYER_STATE_GET_NAME(MMPLAYER_CURRENT_STATE(x_player)), \
221         MMPLAYER_STATE_GET_NAME(MMPLAYER_PENDING_STATE(x_player)), \
222         MMPLAYER_STATE_GET_NAME(MMPLAYER_TARGET_STATE(x_player))); \
223 debug_log("------------------------------------------------------------\n"); 
224
225
226 #define         MMPLAYER_STATE_CHANGE_TIMEOUT(x_player )         ((mm_player_t*)x_player)->state_change_timeout 
227
228 /* streaming */
229 #define MMPLAYER_IS_STREAMING(x_player)                         __is_streaming(x_player)
230 #define MMPLAYER_IS_RTSP_STREAMING(x_player)    __is_rtsp_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
236 /* etc */
237 #define MMF_PLAYER_FILE_BACKUP_PATH             "/tmp/media_temp."
238 #define         MMPLAYER_PT_IS_AUDIO( x_pt )            ( strstr(x_pt, "_97") || strstr(x_pt, "audio") )
239 #define         MMPLAYER_PT_IS_VIDEO( x_pt )            ( strstr(x_pt, "_96") || strstr(x_pt, "video") )
240
241 bool util_is_sdp_file ( const char *path );
242 int64_t uti_get_time ( void );
243 int util_get_rank_increase ( const char *factory_class );
244 int util_factory_rank_compare(GstPluginFeature *f1, GstPluginFeature *f2); // @
245
246
247 bool util_exist_file_path(const char *file_path);
248 bool util_write_file_backup(const char *backup_path, char *data_ptr, int data_size);
249 bool util_remove_file_backup(const char *backup_path); /* For Midi Player */
250
251 int util_is_midi_type_by_mem(void *mem, int size);
252 int util_is_midi_type_by_file(const char *file_path);
253
254 char** util_get_cookie_list ( const char *cookies );
255 bool util_check_valid_url ( const char *proxy );
256
257 #ifdef __cplusplus
258         }
259 #endif
260
261 #endif /* __MMF_PLAYER_UTILS_H__ */
262