fix prevent issue #453398
[platform/core/multimedia/libmm-player.git] / src / mm_player_pd.c
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>, naveen cherukuri <naveen.ch@samsung.com>,
7  * YeJin Cho <cho.yejin@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 #include <mm_debug.h>
23 #include <mm_error.h>
24 #include "mm_player_pd.h"
25 #include "mm_player_utils.h"
26 #include "mm_player_priv.h"
27
28 /*---------------------------------------------------------------------------------------
29 |    LOCAL FUNCTION PROTOTYPES:                                                                                                                 |
30 ---------------------------------------------------------------------------------------*/
31
32 /* It's callback to process whenever there is some changes in PD downloader. */
33 static gboolean __pd_downloader_callback(GstBus *bus, GstMessage *msg, gpointer data);
34
35 /* This function posts messages to application. */
36 /* Currently, MM_MESSAGE_PD_DOWNLOADER_START and MM_MESSAGE_PD_DOWNLOADER_END are used. */
37 static gboolean __pd_downloader_post_message(mm_player_t * player, enum MMMessageType msgtype, MMMessageParamType* param);
38
39 /*=======================================================================================
40 |  FUNCTION DEFINITIONS                                                                                                                                                       |
41 =======================================================================================*/
42 static gboolean
43 __pd_downloader_callback(GstBus *bus, GstMessage *msg, gpointer data)
44 {
45         mm_player_t * player = NULL;
46         mm_player_pd_t *pd = NULL;
47         gboolean bret = TRUE;
48
49         MMPLAYER_FENTER();
50
51         /* chech player handle */
52         return_val_if_fail ( data, MM_ERROR_INVALID_ARGUMENT );
53
54         player = MM_PLAYER_CAST((MMHandleType)data);
55
56         /* get PD downloader handle */
57         pd = MM_PLAYER_GET_PD((MMHandleType)data);
58
59         return_val_if_fail ( pd, MM_ERROR_INVALID_ARGUMENT );
60
61 //      g_print("%s\n", GST_MESSAGE_TYPE_NAME(msg));
62
63         switch ( GST_MESSAGE_TYPE( msg ) )
64         {
65                 case GST_MESSAGE_EOS:
66                         {
67                                 debug_log("PD Downloader EOS received....\n");
68
69                                 g_object_set (G_OBJECT (pd->playback_pipeline_src), "eos", TRUE, NULL);
70
71                                 /* notify application that download is completed */
72                                 __pd_downloader_post_message(player, MM_MESSAGE_PD_DOWNLOADER_END, NULL);
73
74                                 #ifdef PD_SELF_DOWNLOAD
75                                 _mmplayer_unrealize_pd_downloader ((MMHandleType)data);
76                                 #endif
77                         }
78                         break;
79
80                 case GST_MESSAGE_ERROR:
81                         {
82                                 GError *error = NULL;
83                                 gchar* debug = NULL;
84                                 GstMessage *new_msg = NULL;
85
86                                 /* get error code */
87                                 gst_message_parse_error( msg, &error, &debug );
88                                 debug_error ("GST_MESSAGE_ERROR = %s\n", debug);
89
90                                 new_msg = gst_message_new_error (GST_OBJECT_CAST (pd->playback_pipeline_src), error, debug);
91
92                                 /* notify application that pd has any error */
93                                 gst_element_post_message (pd->playback_pipeline_src, new_msg);
94
95                                 _mmplayer_unrealize_pd_downloader ((MMHandleType)data);
96                                 MMPLAYER_FREEIF(debug);
97                                 g_error_free( error);
98                         }
99                         break;
100
101                 case GST_MESSAGE_WARNING:
102                         {
103                                 char* debug = NULL;
104                                 GError* error = NULL;
105
106                                 gst_message_parse_warning(msg, &error, &debug);
107                                 debug_warning("warning : %s\n", error->message);
108                                 debug_warning("debug : %s\n", debug);
109
110                                 MMPLAYER_FREEIF(debug);
111                                 g_error_free( error);
112                         }
113                         break;
114
115                 case GST_MESSAGE_STATE_CHANGED:
116                 {
117                         GstState old_state, new_state;
118                         gchar *src_name;
119
120                         /* get old and new state */
121                         gst_message_parse_state_changed (msg, &old_state, &new_state, NULL);
122
123                         if (old_state == new_state)
124                                 break;
125
126                         /* we only care about pipeline state changes */
127                         if (GST_MESSAGE_SRC (msg) != GST_OBJECT (pd->downloader_pipeline))
128                                 break;
129
130                         src_name = gst_object_get_name (msg->src);
131                         debug_log ("%s changed state from %s to %s", src_name,
132                                 gst_element_state_get_name (old_state),
133                                 gst_element_state_get_name (new_state));
134                         g_free (src_name);
135
136                         switch(new_state)
137                         {
138                                 case GST_STATE_VOID_PENDING:
139                                 case GST_STATE_NULL:
140                                 case GST_STATE_READY:
141                                 case GST_STATE_PAUSED:
142                                         break;
143
144                                 case GST_STATE_PLAYING:
145                                         /* notify application that download is stated */
146                                         __pd_downloader_post_message(player, MM_MESSAGE_PD_DOWNLOADER_START, NULL);
147                                         break;
148
149                                 default:
150                                         break;
151                         }
152                 }
153                 break;
154
155                 case GST_MESSAGE_DURATION:
156                 {
157                         gint64 size = 0LL;
158
159                         /* get total size  of download file, (bytes) */
160                         if ( ! gst_element_query_duration( pd->downloader_pipeline, GST_FORMAT_BYTES, &size ) )
161                         {
162                                 GError *err = NULL;
163                                 GstMessage *new_msg = NULL;
164
165                                 err = g_error_new (GST_STREAM_ERROR, GST_STREAM_ERROR_FAILED, "can't get total size");
166                                 new_msg = gst_message_new_error (GST_OBJECT_CAST (pd->playback_pipeline_src), err, NULL);
167                                 gst_element_post_message (pd->playback_pipeline_src, new_msg);
168
169                                 g_error_free (err);
170
171                                 // TODO: check if playback pipeline is closed well or not
172                                 g_object_set (G_OBJECT (pd->playback_pipeline_src), "eos", TRUE, NULL);
173
174                                 _mmplayer_unrealize_pd_downloader ((MMHandleType)data);
175
176                                 debug_error("failed to query total size for download\n");
177                                 break;
178                         }
179
180                         pd->total_size = size;
181
182                         debug_log("PD total size : %lld bytes\n", size);
183                 }
184                 break;
185
186                 default:
187                         debug_warning("unhandled message\n");
188                         break;
189         }
190
191         MMPLAYER_FLEAVE();
192
193         return bret;
194 }
195
196
197 gboolean __pd_downloader_post_message(mm_player_t * player, enum MMMessageType msgtype, MMMessageParamType* param)
198 {
199         MMPLAYER_FENTER();
200
201         return_val_if_fail( player, FALSE );
202
203         if ( !player->pd_msg_cb )
204         {
205                 debug_warning("no msg callback. can't post\n");
206                 return FALSE;
207         }
208
209         player->pd_msg_cb(msgtype, param, player->pd_msg_cb_param);
210
211         MMPLAYER_FLEAVE();
212
213         return TRUE;
214 }
215
216
217 gboolean _mmplayer_get_pd_downloader_status(MMHandleType handle, guint64 *current_pos, guint64 *total_size)
218 {
219         MMPLAYER_FENTER();
220
221         mm_player_pd_t * pd = NULL;
222         guint64 bytes = 0;
223
224         return_val_if_fail(handle, MM_ERROR_INVALID_ARGUMENT);
225
226         pd = MM_PLAYER_GET_PD(handle);
227
228         return_val_if_fail(pd, MM_ERROR_INVALID_ARGUMENT);
229         return_val_if_fail(pd->downloader_pipeline, MM_ERROR_PLAYER_INVALID_STATE);
230
231         if ( !pd->total_size )
232         {
233                 debug_warning("not ready to get total size\n");
234                 return FALSE;
235         }
236
237         g_object_get(pd->downloader_sink, "current-bytes", &bytes, NULL);
238
239         debug_log("PD status : %lld / %lld\n", bytes, pd->total_size);
240
241         *current_pos = bytes;
242         *total_size = pd->total_size;
243
244         MMPLAYER_FLEAVE();
245
246         return TRUE;
247 }
248
249
250 mm_player_pd_t * _mmplayer_create_pd_downloader()
251 {
252         MMPLAYER_FENTER();
253
254         mm_player_pd_t * pd = NULL;
255
256         /* create PD handle */
257         pd = (mm_player_pd_t *) malloc (sizeof (mm_player_pd_t));
258         if ( !pd )
259         {
260                 debug_error ("Failed to create pd downloader handle...\n");
261                 return FALSE;
262         }
263         memset( pd, 0, sizeof (mm_player_pd_t));
264
265         MMPLAYER_FLEAVE();
266
267         return pd;
268 }
269
270
271 gboolean _mmplayer_destroy_pd_downloader (MMHandleType handle)
272 {
273         MMPLAYER_FENTER();
274
275         mm_player_pd_t * pd = NULL;
276
277         return_val_if_fail ( handle, MM_ERROR_INVALID_ARGUMENT );
278
279         pd = MM_PLAYER_GET_PD(handle);
280
281         if (pd && pd->downloader_pipeline)
282                 _mmplayer_unrealize_pd_downloader (handle);
283
284         /* release PD handle */
285         MMPLAYER_FREEIF(pd);
286
287         MMPLAYER_FLEAVE();
288
289         return TRUE;
290 }
291
292
293 gboolean _mmplayer_realize_pd_downloader (MMHandleType handle, gchar *src_uri, gchar *dst_uri, GstElement *pushsrc)
294 {
295         MMPLAYER_FENTER();
296
297         mm_player_pd_t * pd = NULL;
298
299         return_val_if_fail ( handle, MM_ERROR_INVALID_ARGUMENT );
300         return_val_if_fail ( src_uri, MM_ERROR_INVALID_ARGUMENT );
301         return_val_if_fail ( dst_uri, MM_ERROR_INVALID_ARGUMENT );
302         return_val_if_fail ( pushsrc, MM_ERROR_INVALID_ARGUMENT );
303
304         pd = MM_PLAYER_GET_PD(handle);
305
306         /* initialize */
307         pd->path_read_from = g_strdup (src_uri);
308         pd->location_to_save = g_strdup (dst_uri);
309         pd->playback_pipeline_src = pushsrc;
310         pd->total_size = 0LL;
311
312         MMPLAYER_FLEAVE();
313
314         return TRUE;
315 }
316
317
318 gboolean _mmplayer_start_pd_downloader (MMHandleType handle)
319 {
320         GstBus* bus = NULL;
321         gboolean bret = FALSE;
322         GstStateChangeReturn sret = GST_STATE_CHANGE_SUCCESS;
323         GstState cur_state;
324         GstState pending_state;
325
326         MMPLAYER_FENTER();
327
328         mm_player_pd_t * pd = NULL;
329
330         return_val_if_fail ( handle, MM_ERROR_INVALID_ARGUMENT );
331
332         pd = MM_PLAYER_GET_PD(handle);
333
334         /* pipeline */
335         pd->downloader_pipeline = gst_pipeline_new ("PD Downloader");
336         if (NULL == pd->downloader_pipeline)
337         {
338                 debug_error ("Can't create PD download pipeline...");
339                 return FALSE;
340         }
341
342         /* source */
343         pd->downloader_src = gst_element_factory_make ("souphttpsrc", "PD HTTP download source");
344         if (NULL == pd->downloader_src)
345         {
346                 debug_error ("Can't create PD download src...");
347                 return FALSE;
348         }
349
350         /* queue */
351         pd->downloader_queue = gst_element_factory_make ("queue", "PD download queue");
352         if (NULL == pd->downloader_queue)
353         {
354                 debug_error ("Can't create PD download queue...");
355                 return FALSE;
356         }
357
358         /* filesink */
359         pd->downloader_sink = gst_element_factory_make ("filesink", "PD download sink");
360         if (NULL == pd->downloader_sink)
361         {
362                 debug_error ("Can't create PD download sink...");
363                 return FALSE;
364         }
365
366         g_object_set(pd->downloader_sink, "sync", FALSE, NULL);
367
368         /* Add to bin and link */
369         gst_bin_add_many (GST_BIN (pd->downloader_pipeline),
370                                         pd->downloader_src, pd->downloader_queue, pd->downloader_sink,
371                                         NULL);
372
373         bret = gst_element_link_many (pd->downloader_src, pd->downloader_queue, pd->downloader_sink, NULL);
374         if (FALSE == bret)
375         {
376                 debug_error ("Can't link elements src and sink...");
377                 return FALSE;
378         }
379
380         /* Get Bus and set callback to watch */
381         bus = gst_pipeline_get_bus (GST_PIPELINE (pd->downloader_pipeline));
382         gst_bus_add_watch (bus, __pd_downloader_callback, (gpointer)handle);
383         gst_object_unref (bus);
384
385         /* Set URI on HTTP source */
386         g_object_set (G_OBJECT (pd->downloader_src), "location", pd->path_read_from, NULL);
387
388         /* set file download location on filesink*/
389         g_object_set (G_OBJECT (pd->downloader_sink), "location", pd->location_to_save, NULL);
390
391         secure_debug_log ("src location = %s, save location = %s\n", pd->path_read_from, pd->location_to_save);
392
393         /* Start to download */
394         sret = gst_element_set_state (pd->downloader_pipeline, GST_STATE_PLAYING);
395         if (GST_STATE_CHANGE_FAILURE == sret)
396         {
397                 debug_error ("PD download pipeline failed to go to PLAYING state...");
398                 return FALSE;
399         }
400
401         debug_log ("set_state :: sret = %d\n", sret);
402
403         sret = gst_element_get_state (pd->downloader_pipeline, &cur_state, &pending_state, GST_CLOCK_TIME_NONE);
404         if (GST_STATE_CHANGE_FAILURE == sret)
405         {
406                 debug_error ("PD download pipeline failed to do get_state...");
407                 return FALSE;
408         }
409
410         debug_log ("get-state :: sret = %d\n", sret);
411
412         MMPLAYER_FLEAVE();
413
414         return TRUE;
415 }
416
417
418 gboolean _mmplayer_unrealize_pd_downloader (MMHandleType handle)
419 {
420         MMPLAYER_FENTER();
421
422         mm_player_pd_t * pd = NULL;
423
424         return_val_if_fail ( handle, FALSE );
425
426         pd = MM_PLAYER_GET_PD(handle);
427
428         return_val_if_fail ( pd && pd->downloader_pipeline, FALSE );
429
430         gst_element_set_state (pd->downloader_pipeline, GST_STATE_NULL);
431         gst_element_get_state (pd->downloader_pipeline, NULL, NULL, GST_CLOCK_TIME_NONE);
432
433         gst_object_unref (G_OBJECT (pd->downloader_pipeline));
434         pd->downloader_pipeline = NULL;
435
436         /* free */
437         MMPLAYER_FREEIF(pd->path_read_from);
438         MMPLAYER_FREEIF(pd->location_to_save);
439
440         MMPLAYER_FLEAVE();
441
442         return TRUE;
443 }
444
445
446 gint _mm_player_set_pd_downloader_message_cb(MMHandleType handle, MMMessageCallback callback, gpointer user_param)
447 {
448         MMPLAYER_FENTER();
449
450         mm_player_t * player = NULL;
451
452         return_val_if_fail ( handle, MM_ERROR_INVALID_ARGUMENT );
453
454         player = MM_PLAYER_CAST((MMHandleType)handle);
455
456         /* PD callback can be set as soon as player handle is created.
457           * So, player handle must have it.
458           */
459         player->pd_msg_cb = callback;
460         player->pd_msg_cb_param = user_param;
461
462         debug_log("msg_cb : 0x%x     msg_cb_param : 0x%x\n", (guint)callback, (guint)user_param);
463
464         MMPLAYER_FLEAVE();
465
466         return MM_ERROR_NONE;
467 }