4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
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>
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
24 #include "mm_player_pd.h"
25 #include "mm_player_utils.h"
26 #include "mm_player_priv.h"
28 /*---------------------------------------------------------------------------------------
29 | LOCAL FUNCTION PROTOTYPES: |
30 ---------------------------------------------------------------------------------------*/
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);
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);
39 /*=======================================================================================
40 | FUNCTION DEFINITIONS |
41 =======================================================================================*/
43 __pd_downloader_callback(GstBus *bus, GstMessage *msg, gpointer data)
45 mm_player_t * player = NULL;
46 mm_player_pd_t *pd = NULL;
51 /* chech player handle */
52 return_val_if_fail ( data, MM_ERROR_INVALID_ARGUMENT );
54 player = MM_PLAYER_CAST((MMHandleType)data);
56 /* get PD downloader handle */
57 pd = MM_PLAYER_GET_PD((MMHandleType)data);
59 return_val_if_fail ( pd, MM_ERROR_INVALID_ARGUMENT );
61 // g_print("%s\n", GST_MESSAGE_TYPE_NAME(msg));
63 switch ( GST_MESSAGE_TYPE( msg ) )
67 debug_log("PD Downloader EOS received....\n");
69 g_object_set (G_OBJECT (pd->playback_pipeline_src), "eos", TRUE, NULL);
71 /* notify application that download is completed */
72 __pd_downloader_post_message(player, MM_MESSAGE_PD_DOWNLOADER_END, NULL);
74 #ifdef PD_SELF_DOWNLOAD
75 _mmplayer_unrealize_pd_downloader ((MMHandleType)data);
80 case GST_MESSAGE_ERROR:
85 GstMessage *new_msg = NULL;
88 gst_message_parse_error( msg, &error, &debug );
89 debug_error ("GST_MESSAGE_ERROR = %s\n", debug);
91 new_msg = gst_message_new_error (GST_OBJECT_CAST (pd->playback_pipeline_src), error, debug);
93 /* notify application that pd has any error */
94 ret = gst_element_post_message (pd->playback_pipeline_src, new_msg);
96 _mmplayer_unrealize_pd_downloader ((MMHandleType)data);
100 case GST_MESSAGE_WARNING:
103 GError* error = NULL;
105 gst_message_parse_warning(msg, &error, &debug);
106 debug_warning("warning : %s\n", error->message);
107 debug_warning("debug : %s\n", debug);
109 MMPLAYER_FREEIF(debug);
110 g_error_free( error);
114 case GST_MESSAGE_STATE_CHANGED:
116 GstState old_state, new_state;
119 /* get old and new state */
120 gst_message_parse_state_changed (msg, &old_state, &new_state, NULL);
122 if (old_state == new_state)
125 /* we only care about pipeline state changes */
126 if (GST_MESSAGE_SRC (msg) != GST_OBJECT (pd->downloader_pipeline))
129 src_name = gst_object_get_name (msg->src);
130 debug_log ("%s changed state from %s to %s", src_name,
131 gst_element_state_get_name (old_state),
132 gst_element_state_get_name (new_state));
137 case GST_STATE_VOID_PENDING:
139 case GST_STATE_READY:
140 case GST_STATE_PAUSED:
143 case GST_STATE_PLAYING:
144 /* notify application that download is stated */
145 __pd_downloader_post_message(player, MM_MESSAGE_PD_DOWNLOADER_START, NULL);
154 case GST_MESSAGE_DURATION:
156 GstFormat fmt= GST_FORMAT_BYTES;
160 /* get total size of download file, (bytes) */
161 if ( ! gst_element_query_duration( pd->downloader_pipeline, &fmt, &size ) )
164 GstMessage *new_msg = NULL;
166 err = g_error_new (GST_STREAM_ERROR, GST_STREAM_ERROR_FAILED, "can't get total size");
167 new_msg = gst_message_new_error (GST_OBJECT_CAST (pd->playback_pipeline_src), err, NULL);
168 gst_element_post_message (pd->playback_pipeline_src, new_msg);
172 // TODO: check if playback pipeline is closed well or not
173 g_object_set (G_OBJECT (pd->playback_pipeline_src), "eos", TRUE, NULL);
175 _mmplayer_unrealize_pd_downloader ((MMHandleType)data);
177 debug_error("failed to query total size for download\n");
181 pd->total_size = size;
183 debug_log("PD total size : %lld bytes\n", size);
188 debug_warning("unhandled message\n");
198 gboolean __pd_downloader_post_message(mm_player_t * player, enum MMMessageType msgtype, MMMessageParamType* param)
202 return_val_if_fail( player, FALSE );
204 if ( !player->pd_msg_cb )
206 debug_warning("no msg callback. can't post\n");
210 player->pd_msg_cb(msgtype, param, player->pd_msg_cb_param);
218 gboolean _mmplayer_get_pd_downloader_status(MMHandleType handle, guint64 *current_pos, guint64 *total_size)
222 mm_player_pd_t * pd = NULL;
225 return_val_if_fail(handle, MM_ERROR_INVALID_ARGUMENT);
227 pd = MM_PLAYER_GET_PD(handle);
229 return_val_if_fail(pd, MM_ERROR_INVALID_ARGUMENT);
230 return_val_if_fail(pd->downloader_pipeline, MM_ERROR_INVALID_ARGUMENT);
232 if ( !pd->total_size )
234 debug_warning("not ready to get total size\n");
238 g_object_get(pd->downloader_sink, "current-bytes", &bytes, NULL);
240 debug_log("PD status : %lld / %lld\n", bytes, pd->total_size);
242 *current_pos = bytes;
243 *total_size = pd->total_size;
251 mm_player_pd_t * _mmplayer_create_pd_downloader()
255 mm_player_pd_t * pd = NULL;
257 /* create PD handle */
258 pd = (mm_player_pd_t *) malloc (sizeof (mm_player_pd_t));
261 debug_error ("Failed to create pd downloader handle...\n");
271 gboolean _mmplayer_destroy_pd_downloader (MMHandleType handle)
275 mm_player_pd_t * pd = NULL;
277 return_val_if_fail ( handle, MM_ERROR_INVALID_ARGUMENT );
279 pd = MM_PLAYER_GET_PD(handle);
281 if (pd && pd->downloader_pipeline)
282 _mmplayer_unrealize_pd_downloader (handle);
284 /* release PD handle */
293 gboolean _mmplayer_realize_pd_downloader (MMHandleType handle, gchar *src_uri, gchar *dst_uri, GstElement *pushsrc)
297 mm_player_pd_t * pd = NULL;
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 );
304 pd = MM_PLAYER_GET_PD(handle);
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;
318 gboolean _mmplayer_start_pd_downloader (MMHandleType handle)
321 gboolean bret = FALSE;
322 GstStateChangeReturn sret = GST_STATE_CHANGE_SUCCESS;
324 GstState pending_state;
328 mm_player_pd_t * pd = NULL;
330 return_val_if_fail ( handle, MM_ERROR_INVALID_ARGUMENT );
332 pd = MM_PLAYER_GET_PD(handle);
335 pd->downloader_pipeline = gst_pipeline_new ("PD Downloader");
336 if (NULL == pd->downloader_pipeline)
338 debug_error ("Can't create PD download pipeline...");
343 pd->downloader_src = gst_element_factory_make ("souphttpsrc", "PD HTTP download source");
344 if (NULL == pd->downloader_src)
346 debug_error ("Can't create PD download src...");
351 pd->downloader_queue = gst_element_factory_make ("queue", "PD download queue");
352 if (NULL == pd->downloader_queue)
354 debug_error ("Can't create PD download queue...");
359 pd->downloader_sink = gst_element_factory_make ("filesink", "PD download sink");
360 if (NULL == pd->downloader_sink)
362 debug_error ("Can't create PD download sink...");
366 g_object_set(pd->downloader_sink, "sync", FALSE, NULL);
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,
373 bret = gst_element_link_many (pd->downloader_src, pd->downloader_queue, pd->downloader_sink, NULL);
376 debug_error ("Can't link elements src and sink...");
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);
385 /* Set URI on HTTP source */
386 g_object_set (G_OBJECT (pd->downloader_src), "location", pd->path_read_from, NULL);
388 /* set file download location on filesink*/
389 g_object_set (G_OBJECT (pd->downloader_sink), "location", pd->location_to_save, NULL);
391 debug_log ("src location = %s, save location = %s\n", pd->path_read_from, pd->location_to_save);
393 /* Start to download */
394 sret = gst_element_set_state (pd->downloader_pipeline, GST_STATE_PLAYING);
395 if (GST_STATE_CHANGE_FAILURE == sret)
397 debug_error ("PD download pipeline failed to go to PLAYING state...");
401 debug_log ("set_state :: sret = %d\n", sret);
403 sret = gst_element_get_state (pd->downloader_pipeline, &cur_state, &pending_state, GST_CLOCK_TIME_NONE);
404 if (GST_STATE_CHANGE_FAILURE == sret)
406 debug_error ("PD download pipeline failed to do get_state...");
410 debug_log ("get-state :: sret = %d\n", sret);
418 gboolean _mmplayer_unrealize_pd_downloader (MMHandleType handle)
422 mm_player_pd_t * pd = NULL;
424 return_val_if_fail ( handle, FALSE );
426 pd = MM_PLAYER_GET_PD(handle);
428 return_val_if_fail ( pd && pd->downloader_pipeline, FALSE );
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);
433 gst_object_unref (G_OBJECT (pd->downloader_pipeline));
434 pd->downloader_pipeline = NULL;
437 MMPLAYER_FREEIF(pd->path_read_from);
438 MMPLAYER_FREEIF(pd->location_to_save);
446 gint _mm_player_set_pd_downloader_message_cb(MMHandleType handle, MMMessageCallback callback, gpointer user_param)
450 mm_player_t * player = NULL;
452 return_val_if_fail ( handle, MM_ERROR_INVALID_ARGUMENT );
454 player = MM_PLAYER_CAST((MMHandleType)handle);
456 /* PD callback can be set as soon as player handle is created.
457 * So, player handle must have it.
459 player->pd_msg_cb = callback;
460 player->pd_msg_cb_param = user_param;
462 debug_log("msg_cb : 0x%x msg_cb_param : 0x%x\n", (guint)callback, (guint)user_param);
466 return MM_ERROR_NONE;