[0.6.280] remove unused caps ref
[platform/core/multimedia/libmm-player.git] / src / mm_player_priv.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>, 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 /*===========================================================================================
24 |                                                                                                                                                                                       |
25 |  INCLUDE FILES                                                                                                                                                        |
26 |                                                                                                                                                                                       |
27 ========================================================================================== */
28 #include <glib.h>
29 #include <gst/gst.h>
30 #include <gst/video/videooverlay.h>
31 #include <gst/audio/gstaudiobasesink.h>
32 #include <unistd.h>
33 #include <sys/stat.h>
34 #include <string.h>
35 #include <sys/time.h>
36 #include <stdlib.h>
37 #include <dlog.h>
38 #include <gio/gio.h>
39
40 #include <mm_error.h>
41 #include <mm_attrs.h>
42 #include <mm_sound.h>
43
44 #include "mm_player_priv.h"
45 #include "mm_player_ini.h"
46 #include "mm_player_capture.h"
47 #include "mm_player_utils.h"
48 #include "mm_player_tracks.h"
49 #include "mm_player_360.h"
50 #include "mm_player_gst.h"
51
52 #include <system_info.h>
53 #include <sound_manager.h>
54 #include <gst/allocators/gsttizenmemory.h>
55 #include <tbm_surface_internal.h>
56
57 /*===========================================================================================
58 |                                                                                                                                                                                       |
59 |  LOCAL DEFINITIONS AND DECLARATIONS FOR MODULE                                                                                        |
60 |                                                                                                                                                                                       |
61 ========================================================================================== */
62
63 /*---------------------------------------------------------------------------
64 |    GLOBAL CONSTANT DEFINITIONS:                                                                                       |
65 ---------------------------------------------------------------------------*/
66
67 /*---------------------------------------------------------------------------
68 |    IMPORTED VARIABLE DECLARATIONS:                                                                            |
69 ---------------------------------------------------------------------------*/
70
71 /*---------------------------------------------------------------------------
72 |    IMPORTED FUNCTION DECLARATIONS:                                                                            |
73 ---------------------------------------------------------------------------*/
74
75 /*---------------------------------------------------------------------------
76 |    LOCAL #defines:                                                                                                            |
77 ---------------------------------------------------------------------------*/
78 #define TRICK_PLAY_MUTE_THRESHOLD_MAX   2.0
79 #define TRICK_PLAY_MUTE_THRESHOLD_MIN   0.0
80
81 #define MM_VOLUME_FACTOR_DEFAULT                1.0
82 #define MM_VOLUME_FACTOR_MIN                    0
83 #define MM_VOLUME_FACTOR_MAX                    1.0
84
85 /* Don't need to sleep for sound fadeout
86  * fadeout related function will be deleted(Deprecated)
87  */
88 #define MM_PLAYER_FADEOUT_TIME_DEFAULT  0
89
90 #define DEFAULT_PLAYBACK_RATE                   1.0
91
92 #define PLAYER_DISPLAY_MODE_DST_ROI             5
93
94 #define ADAPTIVE_VARIANT_DEFAULT_VALUE -1 /* auto */
95
96 #define PLAYER_SPHERICAL_DEFAULT_YAW   0  /* sync from video360 plugin */
97 #define PLAYER_SPHERICAL_DEFAULT_PITCH 0
98 #define PLAYER_SPHERICAL_DEFAULT_H_FOV 120
99 #define PLAYER_SPHERICAL_DEFAULT_V_FOV 67
100
101 #define SPATIAL_AUDIO_CAPS             "audio/x-raw,format=S16LE,channels=4"
102 #define FEATURE_NAME_SPHERICAL_VIDEO   "http://tizen.org/feature/multimedia.player.spherical_video"
103
104 #define FAKE_SINK_MAX_LATENESS         G_GINT64_CONSTANT(20000000) /* set 20ms as waylandsink */
105
106 #define DEFAULT_PCM_OUT_FORMAT         "F32LE"
107 #define DEFAULT_PCM_OUT_SAMPLERATE     44100
108 #define DEFAULT_PCM_OUT_CHANNEL        2
109
110 /*---------------------------------------------------------------------------
111 |    LOCAL CONSTANT DEFINITIONS:                                                                                        |
112 ---------------------------------------------------------------------------*/
113
114 /*---------------------------------------------------------------------------
115 |    LOCAL DATA TYPE DEFINITIONS:                                                                                       |
116 ---------------------------------------------------------------------------*/
117 /* NOTE : GstAutoplugSelectResult is defined in gstplay-enum.h but not exposed
118  We are defining our own and will be removed when it actually exposed */
119 typedef enum {
120         GST_AUTOPLUG_SELECT_TRY,
121         GST_AUTOPLUG_SELECT_EXPOSE,
122         GST_AUTOPLUG_SELECT_SKIP
123 } GstAutoplugSelectResult;
124
125 /*---------------------------------------------------------------------------
126 |    GLOBAL VARIABLE DEFINITIONS:                                                                                       |
127 ---------------------------------------------------------------------------*/
128
129 /*---------------------------------------------------------------------------
130 |    LOCAL VARIABLE DEFINITIONS:                                                                                        |
131 ---------------------------------------------------------------------------*/
132 static sound_stream_info_h stream_info;
133
134 /*---------------------------------------------------------------------------
135 |    LOCAL FUNCTION PROTOTYPES:                                                                                         |
136 ---------------------------------------------------------------------------*/
137 static int              __mmplayer_gst_create_pipeline(mmplayer_t *player);
138 static int              __mmplayer_gst_destroy_pipeline(mmplayer_t *player);
139 static int              __mmplayer_gst_create_text_pipeline(mmplayer_t *player);
140 static int              __mmplayer_gst_create_video_sink_bin(mmplayer_t *player, GstCaps *caps, MMDisplaySurfaceType surface_type);
141 static int              __mmplayer_gst_create_audio_sink_bin(mmplayer_t *player);
142 static int              __mmplayer_gst_create_text_sink_bin(mmplayer_t *player);
143 static void             __mmplayer_gst_create_sink_bin(GstElement *decodebin, GstPad *pad, GstCaps *ref_caps, gpointer data);
144 static gboolean __mmplayer_create_sink_path(mmplayer_t *player, GstElement *combiner, mmplayer_track_type_e type, GstCaps *caps);
145 static gboolean __mmplayer_is_midi_type(gchar *str_caps);
146 static gboolean __mmplayer_is_only_mp3_type(gchar *str_caps);
147
148 static gboolean __mmplayer_update_subtitle(GstElement *object, GstBuffer *buffer, GstPad *pad, gpointer data);
149 static void             __mmplayer_release_misc(mmplayer_t *player);
150 static void             __mmplayer_release_misc_post(mmplayer_t *player);
151 static gboolean __mmplayer_init_gstreamer(mmplayer_t *player);
152 static void __mmplayer_video_stream_decoded_preroll_cb(GstElement *object, GstBuffer *buffer, GstPad *pad, gpointer data);
153 static void __mmplayer_video_stream_decoded_render_cb(GstElement *object, GstBuffer *buffer, GstPad *pad, gpointer data);
154 static GstPadProbeReturn __mmplayer_subtitle_adjust_position_probe(GstPad *pad, GstPadProbeInfo *info, gpointer u_data);
155 static int __mmplayer_change_selector_pad(mmplayer_t *player, mmplayer_track_type_e type, int index);
156
157 static gboolean __mmplayer_check_subtitle(mmplayer_t *player);
158 static int              __mmplayer_handle_missed_plugin(mmplayer_t *player);
159 static void             __mmplayer_add_sink(mmplayer_t *player, GstElement *sink, gboolean first);
160 static void             __mmplayer_del_sink(mmplayer_t *player, GstElement *sink);
161 static void             __mmplayer_release_signal_connection(mmplayer_t *player, mmplayer_signal_type_e type);
162 static gpointer __mmplayer_gapless_play_thread(gpointer data);
163 static gboolean __mmplayer_add_dump_buffer_probe(mmplayer_t *player, GstElement *element);
164 static GstPadProbeReturn __mmplayer_dump_buffer_probe_cb(GstPad *pad,  GstPadProbeInfo *info, gpointer u_data);
165 static void __mmplayer_release_dump_list(GList *dump_list);
166 static int              __mmplayer_gst_realize(mmplayer_t *player);
167 static int              __mmplayer_gst_unrealize(mmplayer_t *player);
168 static int              __mmplayer_gst_adjust_subtitle_position(mmplayer_t *player, int position);
169 static int              __mmplayer_gst_set_message_callback(mmplayer_t *player, MMMessageCallback callback, gpointer user_param);
170
171 /* util */
172 static gboolean __mmplayer_verify_gapless_play_path(mmplayer_t *player);
173 static void __mmplayer_check_pipeline_reconfigure_state(mmplayer_t *player);
174 static gboolean __mmplayer_deactivate_selector(mmplayer_t *player, mmplayer_track_type_e type);
175 static gboolean __mmplayer_deactivate_combiner(mmplayer_t *player, mmplayer_track_type_e type);
176 static void __mmplayer_deactivate_old_path(mmplayer_t *player);
177 static int __mmplayer_gst_create_plain_text_elements(mmplayer_t *player);
178 static guint32 _mmplayer_convert_fourcc_string_to_value(const gchar *format_name);
179 static void             __mmplayer_gst_caps_notify_cb(GstPad *pad, GParamSpec *unused, gpointer data);
180 static void             __mmplayer_audio_stream_send_data(mmplayer_t *player, mmplayer_audio_stream_buff_t *a_buffer);
181 static void             __mmplayer_initialize_storage_info(mmplayer_t *player, mmplayer_path_type_e path_type);
182 static gboolean __mmplayer_update_duration_value(mmplayer_t *player);
183 static gboolean __mmplayer_update_audio_attrs(mmplayer_t *player, MMHandleType attrs);
184 static gboolean __mmplayer_update_video_attrs(mmplayer_t *player, MMHandleType attrs);
185 static gboolean __mmplayer_update_bitrate_attrs(mmplayer_t *player, MMHandleType attrs);
186
187 static void __mmplayer_copy_uri_and_set_type(mmplayer_parse_profile_t *data, const char *uri, int uri_type);
188 static int __mmplayer_set_mem_uri(mmplayer_parse_profile_t *data, char *path, void *param);
189 static int __mmplayer_set_file_uri(mmplayer_parse_profile_t *data, const char *uri);
190
191 static mmplayer_video_decoded_data_info_t *__mmplayer_create_stream_from_pad(GstPad *pad);
192 static void __mmplayer_zerocopy_set_stride_elevation_bo(mmplayer_video_decoded_data_info_t *stream, GstMemory *mem);
193 static gboolean __mmplayer_swcodec_set_stride_elevation(mmplayer_video_decoded_data_info_t *stream);
194 static gboolean __mmplayer_swcodec_set_bo(mmplayer_t *player, mmplayer_video_decoded_data_info_t *stream, GstMemory *mem);
195
196 static void __mmplayer_set_pause_state(mmplayer_t *player);
197 static void __mmplayer_set_playing_state(mmplayer_t *player);
198 static int __mmplayer_switch_stream(mmplayer_t *player, mmplayer_track_type_e type, int index);
199 /*===========================================================================================
200 |                                                                                                                                                                                       |
201 |  FUNCTION DEFINITIONS                                                                                                                                         |
202 |                                                                                                                                                                                       |
203 ========================================================================================== */
204
205 /* This function should be called after the pipeline goes PAUSED or higher
206 state. */
207 gboolean
208 _mmplayer_update_content_attrs(mmplayer_t *player, enum content_attr_flag flag)
209 {
210         static gboolean has_duration = FALSE;
211         static gboolean has_video_attrs = FALSE;
212         static gboolean has_audio_attrs = FALSE;
213         static gboolean has_bitrate = FALSE;
214         gboolean missing_only = FALSE;
215         gboolean all = FALSE;
216         MMHandleType attrs = 0;
217
218         MMPLAYER_FENTER();
219
220         MMPLAYER_RETURN_VAL_IF_FAIL(player, FALSE);
221
222         /* check player state here */
223         if (MMPLAYER_CURRENT_STATE(player) != MM_PLAYER_STATE_PAUSED &&
224                 MMPLAYER_CURRENT_STATE(player) != MM_PLAYER_STATE_PLAYING) {
225                 /* give warning now only */
226                 LOGW("be careful. content attributes may not available in this state ");
227         }
228
229         /* get content attribute first */
230         attrs = MMPLAYER_GET_ATTRS(player);
231         if (!attrs) {
232                 LOGE("cannot get content attribute");
233                 return FALSE;
234         }
235
236         /* get update flag */
237
238         if (flag & ATTR_MISSING_ONLY) {
239                 missing_only = TRUE;
240                 LOGD("updating missed attr only");
241         }
242
243         if (flag & ATTR_ALL) {
244                 all = TRUE;
245                 has_duration = FALSE;
246                 has_video_attrs = FALSE;
247                 has_audio_attrs = FALSE;
248                 has_bitrate = FALSE;
249
250                 LOGD("updating all attrs");
251         }
252
253         if (missing_only && all) {
254                 LOGW("cannot use ATTR_MISSING_ONLY and ATTR_ALL. ignoring ATTR_MISSING_ONLY flag!");
255                 missing_only = FALSE;
256         }
257
258         if ((flag & ATTR_DURATION) || (!has_duration && missing_only) || all)
259                 has_duration = __mmplayer_update_duration_value(player);
260
261         if ((flag & ATTR_AUDIO) || (!has_audio_attrs && missing_only) || all)
262                 has_audio_attrs = __mmplayer_update_audio_attrs(player, attrs);
263
264         if ((flag & ATTR_VIDEO) || (!has_video_attrs && missing_only) || all)
265                 has_video_attrs = __mmplayer_update_video_attrs(player, attrs);
266
267         if ((flag & ATTR_BITRATE) || (!has_bitrate && missing_only) || all)
268                 has_bitrate = __mmplayer_update_bitrate_attrs(player, attrs);
269
270         MMPLAYER_FLEAVE();
271
272         return TRUE;
273 }
274
275 MMStreamingType
276 _mmplayer_get_stream_service_type(mmplayer_t *player)
277 {
278         MMStreamingType streaming_type = STREAMING_SERVICE_NONE;
279
280         MMPLAYER_FENTER();
281
282         MMPLAYER_RETURN_VAL_IF_FAIL(player &&
283                         player->pipeline &&
284                         player->pipeline->mainbin &&
285                         player->pipeline->mainbin[MMPLAYER_M_SRC].gst,
286                         STREAMING_SERVICE_NONE);
287
288         /* streaming service type if streaming */
289         if (!MMPLAYER_IS_STREAMING(player))
290                 return STREAMING_SERVICE_NONE;
291
292         streaming_type = (player->duration == 0) ?
293                 STREAMING_SERVICE_LIVE : STREAMING_SERVICE_VOD;
294
295         switch (streaming_type) {
296         case STREAMING_SERVICE_LIVE:
297                 LOGD("it's live streaming");
298                 break;
299         case STREAMING_SERVICE_VOD:
300                 LOGD("it's vod streaming");
301                 break;
302         default:
303                 LOGE("should not get here");
304                 break;
305         }
306
307         MMPLAYER_FLEAVE();
308
309         return streaming_type;
310 }
311
312 /* this function sets the player state and also report
313  * it to application by calling callback function
314  */
315 void
316 _mmplayer_set_state(mmplayer_t *player, int state)
317 {
318         MMMessageParamType msg = {0, };
319
320         MMPLAYER_RETURN_IF_FAIL(player);
321
322         if (MMPLAYER_CURRENT_STATE(player) == state) {
323                 LOGW("already same state(%s)", MMPLAYER_STATE_GET_NAME(state));
324                 MMPLAYER_PENDING_STATE(player) = MM_PLAYER_STATE_NONE;
325                 return;
326         }
327
328         /* update player states */
329         MMPLAYER_PREV_STATE(player) = MMPLAYER_CURRENT_STATE(player);
330         MMPLAYER_CURRENT_STATE(player) = state;
331
332         if (MMPLAYER_CURRENT_STATE(player) == MMPLAYER_PENDING_STATE(player))
333                 MMPLAYER_PENDING_STATE(player) = MM_PLAYER_STATE_NONE;
334
335         /* print state */
336         MMPLAYER_PRINT_STATE(player);
337
338         switch (MMPLAYER_CURRENT_STATE(player)) {
339         case MM_PLAYER_STATE_NULL:
340         case MM_PLAYER_STATE_READY:
341                 break;
342         case MM_PLAYER_STATE_PAUSED:
343                 __mmplayer_set_pause_state(player);
344                 break;
345         case MM_PLAYER_STATE_PLAYING:
346                 __mmplayer_set_playing_state(player);
347                 break;
348         case MM_PLAYER_STATE_NONE:
349         default:
350                 LOGW("invalid target state, there is nothing to do.");
351                 break;
352         }
353
354
355         /* post message to application */
356         if (MMPLAYER_TARGET_STATE(player) == state) {
357                 /* fill the message with state of player */
358                 msg.union_type = MM_MSG_UNION_STATE;
359                 msg.state.previous = MMPLAYER_PREV_STATE(player);
360                 msg.state.current = MMPLAYER_CURRENT_STATE(player);
361
362                 LOGD("player reach the target state (%s)", MMPLAYER_STATE_GET_NAME(MMPLAYER_TARGET_STATE(player)));
363
364                 /* state changed by resource callback */
365                 if (player->interrupted_by_resource)
366                         MMPLAYER_POST_MSG(player, MM_MESSAGE_STATE_INTERRUPTED, &msg);
367                 else /* state changed by usecase */
368                         MMPLAYER_POST_MSG(player, MM_MESSAGE_STATE_CHANGED, &msg);
369
370         } else {
371                 LOGD("intermediate state, do nothing.");
372                 MMPLAYER_PRINT_STATE(player);
373                 return;
374         }
375
376         if (MMPLAYER_CURRENT_STATE(player) == MM_PLAYER_STATE_PLAYING
377                 && !player->sent_bos) {
378                 MMPLAYER_POST_MSG(player, MM_MESSAGE_BEGIN_OF_STREAM, NULL);
379                 player->sent_bos = TRUE;
380         }
381
382         return;
383 }
384
385 int
386 _mmplayer_check_state(mmplayer_t *player, mmplayer_command_state_e command)
387 {
388         mmplayer_state_e current_state = MM_PLAYER_STATE_NUM;
389         mmplayer_state_e pending_state = MM_PLAYER_STATE_NUM;
390
391         MMPLAYER_RETURN_VAL_IF_FAIL(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
392 #ifdef __DEBUG__
393         LOGD("incoming command : %d ", command);
394 #endif
395         current_state = MMPLAYER_CURRENT_STATE(player);
396         pending_state = MMPLAYER_PENDING_STATE(player);
397
398         MMPLAYER_PRINT_STATE(player);
399
400         switch (command) {
401         case MMPLAYER_COMMAND_CREATE:
402         {
403                 MMPLAYER_TARGET_STATE(player) = MM_PLAYER_STATE_NULL;
404
405                 if (current_state == MM_PLAYER_STATE_NULL ||
406                         current_state == MM_PLAYER_STATE_READY ||
407                         current_state == MM_PLAYER_STATE_PAUSED ||
408                         current_state == MM_PLAYER_STATE_PLAYING)
409                         goto NO_OP;
410         }
411         break;
412
413         case MMPLAYER_COMMAND_DESTROY:
414         {
415                 /* destroy can called anytime */
416
417                 MMPLAYER_TARGET_STATE(player) = MM_PLAYER_STATE_NONE;
418         }
419         break;
420
421         case MMPLAYER_COMMAND_REALIZE:
422         {
423                 MMPLAYER_TARGET_STATE(player) = MM_PLAYER_STATE_READY;
424
425                 if (pending_state != MM_PLAYER_STATE_NONE) {
426                         goto INVALID_STATE;
427                 } else {
428                         /* need ready state to realize */
429                         if (current_state == MM_PLAYER_STATE_READY)
430                                 goto NO_OP;
431
432                         if (current_state != MM_PLAYER_STATE_NULL)
433                                 goto INVALID_STATE;
434                 }
435         }
436         break;
437
438         case MMPLAYER_COMMAND_UNREALIZE:
439         {
440                 MMPLAYER_TARGET_STATE(player) = MM_PLAYER_STATE_NULL;
441
442                 if (current_state == MM_PLAYER_STATE_NULL)
443                         goto NO_OP;
444         }
445         break;
446
447         case MMPLAYER_COMMAND_START:
448         {
449                 MMPLAYER_TARGET_STATE(player) = MM_PLAYER_STATE_PLAYING;
450
451                 if (pending_state == MM_PLAYER_STATE_NONE) {
452                         if (current_state == MM_PLAYER_STATE_PLAYING)
453                                 goto NO_OP;
454                         else if (current_state  != MM_PLAYER_STATE_READY &&
455                                 current_state != MM_PLAYER_STATE_PAUSED)
456                                 goto INVALID_STATE;
457                 } else if (pending_state == MM_PLAYER_STATE_PLAYING) {
458                         goto ALREADY_GOING;
459                 } else if (pending_state == MM_PLAYER_STATE_PAUSED) {
460                         LOGD("player is going to paused state, just change the pending state as playing");
461                 } else {
462                         goto INVALID_STATE;
463                 }
464         }
465         break;
466
467         case MMPLAYER_COMMAND_STOP:
468         {
469                 MMPLAYER_TARGET_STATE(player) = MM_PLAYER_STATE_READY;
470
471                 if (current_state == MM_PLAYER_STATE_READY)
472                         goto NO_OP;
473
474                 /* need playing/paused state to stop */
475                 if (current_state != MM_PLAYER_STATE_PLAYING &&
476                          current_state != MM_PLAYER_STATE_PAUSED)
477                         goto INVALID_STATE;
478         }
479         break;
480
481         case MMPLAYER_COMMAND_PAUSE:
482         {
483                 if (MMPLAYER_IS_LIVE_STREAMING(player))
484                         goto NO_OP;
485
486                 if (player->seek_state == MMPLAYER_SEEK_IN_PROGRESS)
487                         goto NOT_COMPLETED_SEEK;
488
489                 MMPLAYER_TARGET_STATE(player) = MM_PLAYER_STATE_PAUSED;
490
491                 if (pending_state == MM_PLAYER_STATE_NONE) {
492                         if (current_state == MM_PLAYER_STATE_PAUSED)
493                                 goto NO_OP;
494                         else if (current_state != MM_PLAYER_STATE_PLAYING && current_state != MM_PLAYER_STATE_READY) // support loading state of browser
495                                 goto INVALID_STATE;
496                 } else if (pending_state == MM_PLAYER_STATE_PAUSED) {
497                         goto ALREADY_GOING;
498                 } else if (pending_state == MM_PLAYER_STATE_PLAYING) {
499                         if (current_state == MM_PLAYER_STATE_PAUSED)
500                                 LOGD("player is PAUSED going to PLAYING, just change the pending state as PAUSED");
501                         else
502                                 goto INVALID_STATE;
503                 }
504         }
505         break;
506
507         case MMPLAYER_COMMAND_RESUME:
508         {
509                 if (player->seek_state == MMPLAYER_SEEK_IN_PROGRESS)
510                         goto NOT_COMPLETED_SEEK;
511
512                 MMPLAYER_TARGET_STATE(player) = MM_PLAYER_STATE_PLAYING;
513
514                 if (pending_state == MM_PLAYER_STATE_NONE) {
515                         if (current_state == MM_PLAYER_STATE_PLAYING)
516                                 goto NO_OP;
517                         else if (current_state != MM_PLAYER_STATE_PAUSED)
518                                 goto INVALID_STATE;
519                 } else if (pending_state == MM_PLAYER_STATE_PLAYING) {
520                         goto ALREADY_GOING;
521                 } else if (pending_state == MM_PLAYER_STATE_PAUSED) {
522                         LOGD("player is going to paused state, just change the pending state as playing");
523                 } else {
524                         goto INVALID_STATE;
525                 }
526         }
527                 break;
528
529         default:
530                 break;
531         }
532         player->cmd = command;
533
534         return MM_ERROR_NONE;
535
536 INVALID_STATE:
537         LOGW("since player is in wrong state(%s). it's not able to apply the command(%d)",
538                 MMPLAYER_STATE_GET_NAME(current_state), command);
539         return MM_ERROR_PLAYER_INVALID_STATE;
540
541 NOT_COMPLETED_SEEK:
542         LOGW("not completed seek");
543         return MM_ERROR_PLAYER_DOING_SEEK;
544
545 NO_OP:
546         LOGW("player is in the desired state(%s). doing noting", MMPLAYER_STATE_GET_NAME(current_state));
547         return MM_ERROR_PLAYER_NO_OP;
548
549 ALREADY_GOING:
550         LOGW("player is already going to %s, doing nothing", MMPLAYER_STATE_GET_NAME(pending_state));
551         return MM_ERROR_PLAYER_NO_OP;
552 }
553
554 int _mmplayer_acquire_hw_resource(mmplayer_t *player, mmplayer_resource_type_e type)
555 {
556         int rm_ret = MM_RESOURCE_MANAGER_ERROR_NONE;
557         mm_resource_manager_res_type_e rm_res_type = MM_RESOURCE_MANAGER_RES_TYPE_MAX;
558
559         switch (type) {
560                 case MMPLAYER_RESOURCE_TYPE_VIDEO_DECODER:
561                         rm_res_type = MM_RESOURCE_MANAGER_RES_TYPE_VIDEO_DECODER;
562                         break;
563                 case MMPLAYER_RESOURCE_TYPE_VIDEO_OVERLAY:
564                         rm_res_type = MM_RESOURCE_MANAGER_RES_TYPE_VIDEO_OVERLAY;
565                         break;
566                 case MMPLAYER_RESOURCE_TYPE_AUDIO_OFFLOAD:
567                         rm_res_type = MM_RESOURCE_MANAGER_RES_TYPE_AUDIO_OFFLOAD;
568                         break;
569                 default:
570                         LOGE("invalid mmplayer resource type %d", type);
571                         return MM_ERROR_PLAYER_INTERNAL;
572         }
573
574         if (player->hw_resource[type] != NULL) {
575                 LOGD("[%d type] resource was already acquired", type);
576                 return MM_ERROR_NONE;
577         }
578
579         LOGD("mark for acquire [%d type] resource", type);
580         rm_ret = mm_resource_manager_mark_for_acquire(player->resource_manager,
581                         rm_res_type, MM_RESOURCE_MANAGER_RES_VOLUME_FULL, &player->hw_resource[type]);
582         if (rm_ret != MM_RESOURCE_MANAGER_ERROR_NONE) {
583                 LOGE("failed to mark resource for acquire, ret(0x%x)", rm_ret);
584                 return MM_ERROR_PLAYER_INTERNAL;
585         }
586
587         LOGD("commit [%d type] resource", type);
588         rm_ret = mm_resource_manager_commit(player->resource_manager);
589         if (rm_ret != MM_RESOURCE_MANAGER_ERROR_NONE) {
590                 LOGE("failed to commit of resource, ret(0x%x)", rm_ret);
591                 return MM_ERROR_PLAYER_INTERNAL;
592         }
593
594         MMPLAYER_FLEAVE();
595         return MM_ERROR_NONE;
596 }
597
598 static void __mmplayer_destroy_hw_resource(mmplayer_t *player)
599 {
600         int rm_ret = MM_RESOURCE_MANAGER_ERROR_NONE;
601
602         MMPLAYER_RETURN_IF_FAIL(player);
603         MMPLAYER_RETURN_IF_FAIL(player->resource_manager);
604
605         rm_ret = mm_resource_manager_mark_all_for_release(player->resource_manager);
606         if (rm_ret != MM_RESOURCE_MANAGER_ERROR_NONE) {
607                 LOGW("failed to mark all for release of resource, ret(0x%x)", rm_ret);
608                 goto rm_destroy;
609         }
610
611         rm_ret = mm_resource_manager_commit(player->resource_manager);
612         if (rm_ret != MM_RESOURCE_MANAGER_ERROR_NONE)
613                 LOGW("failed to commit resource, ret(0x%x)", rm_ret);
614
615 rm_destroy:
616         /* de-initialize resource manager */
617         rm_ret = mm_resource_manager_destroy(player->resource_manager);
618         if (rm_ret != MM_RESOURCE_MANAGER_ERROR_NONE) {
619                 LOGW("failed to destroy resource manager, ret(0x%x)", rm_ret);
620                 return;
621         }
622
623         player->resource_manager = NULL;
624
625         LOGD("resource manager is destroyed");
626 }
627
628 static int __mmplayer_release_hw_resource(mmplayer_t *player, mmplayer_resource_type_e type)
629 {
630         int rm_ret = MM_RESOURCE_MANAGER_ERROR_NONE;
631
632         MMPLAYER_FENTER();
633
634         if (player->hw_resource[type] == NULL) {
635                 LOGD("there is no acquired [%d type] resource", type);
636                 return MM_ERROR_NONE;
637         }
638
639         LOGD("mark for release [%d type] resource", type);
640         rm_ret = mm_resource_manager_mark_for_release(player->resource_manager, player->hw_resource[type]);
641         if (rm_ret != MM_RESOURCE_MANAGER_ERROR_NONE) {
642                 LOGE("failed to mark resource for release, ret(0x%x)", rm_ret);
643                 return MM_ERROR_PLAYER_INTERNAL;
644         }
645
646         player->hw_resource[type] = NULL;
647
648         LOGD("commit [%d type] resource", type);
649         rm_ret = mm_resource_manager_commit(player->resource_manager);
650         if (rm_ret != MM_RESOURCE_MANAGER_ERROR_NONE) {
651                 LOGE("failed to commit resource, ret(0x%x)", rm_ret);
652                 return MM_ERROR_PLAYER_INTERNAL;
653         }
654
655         MMPLAYER_FLEAVE();
656         return MM_ERROR_NONE;
657 }
658
659 static void
660 __mmplayer_initialize_gapless_play(mmplayer_t *player)
661 {
662         int i;
663
664         MMPLAYER_FENTER();
665
666         player->smooth_streaming = FALSE;
667         player->videodec_linked = 0;
668         player->audiodec_linked = 0;
669         player->textsink_linked = 0;
670         player->is_external_subtitle_present = FALSE;
671         player->is_external_subtitle_added_now = FALSE;
672         player->not_supported_codec = MISSING_PLUGIN_NONE;
673         player->can_support_codec = FOUND_PLUGIN_NONE;
674         player->pending_seek.is_pending = false;
675         player->pending_seek.pos = 0;
676         player->msg_posted = FALSE;
677         player->has_many_types = FALSE;
678         player->no_more_pad = FALSE;
679         player->not_found_demuxer = 0;
680         player->seek_state = MMPLAYER_SEEK_NONE;
681         player->is_subtitle_force_drop = FALSE;
682         player->play_subtitle = FALSE;
683         player->adjust_subtitle_pos = 0;
684
685         player->total_bitrate = 0;
686         player->total_maximum_bitrate = 0;
687
688         _mmplayer_track_initialize(player);
689         __mmplayer_initialize_storage_info(player, MMPLAYER_PATH_MAX);
690
691         for (i = 0; i < MM_PLAYER_STREAM_COUNT_MAX; i++) {
692                 player->bitrate[i] = 0;
693                 player->maximum_bitrate[i] = 0;
694         }
695
696         if (player->v_stream_caps) {
697                 gst_caps_unref(player->v_stream_caps);
698                 player->v_stream_caps = NULL;
699         }
700
701         mm_player_set_attribute((MMHandleType)player, NULL, "content_video_found", 0, NULL);
702
703         /* clean found audio decoders */
704         if (player->audio_decoders) {
705                 g_list_free_full(player->audio_decoders, (GDestroyNotify)g_free);
706                 player->audio_decoders = NULL;
707         }
708
709         __mmplayer_release_hw_resource(player, MMPLAYER_RESOURCE_TYPE_VIDEO_DECODER);
710
711         MMPLAYER_FLEAVE();
712 }
713
714 void _mmplayer_set_reconfigure_state(mmplayer_t *player, gboolean state)
715 {
716         LOGI("set pipeline reconfigure state %d", state);
717         MMPLAYER_RECONFIGURE_LOCK(player);
718         player->gapless.reconfigure = state;
719         if (!state) /* wake up the waiting job */
720                 MMPLAYER_RECONFIGURE_SIGNAL(player);
721         MMPLAYER_RECONFIGURE_UNLOCK(player);
722 }
723
724 static gpointer
725 __mmplayer_gapless_play_thread(gpointer data)
726 {
727         mmplayer_t *player = (mmplayer_t *)data;
728         mmplayer_gst_element_t *mainbin = NULL;
729
730         MMPLAYER_RETURN_VAL_IF_FAIL(player, NULL);
731
732         MMPLAYER_GAPLESS_PLAY_THREAD_LOCK(player);
733         while (!player->gapless_play_thread_exit) {
734                 LOGD("gapless play thread started. waiting for signal.");
735                 MMPLAYER_GAPLESS_PLAY_THREAD_WAIT(player);
736
737                 LOGD("reconfigure pipeline for gapless play.");
738
739                 if (player->gapless_play_thread_exit) {
740                         _mmplayer_set_reconfigure_state(player, FALSE);
741                         LOGD("exiting gapless play thread");
742                         break;
743                 }
744
745                 mainbin = player->pipeline->mainbin;
746
747                 if (MMPLAYER_USE_DECODEBIN(player)) {
748                         MMPLAYER_RELEASE_ELEMENT(player, mainbin, MMPLAYER_M_MUXED_S_BUFFER);
749                         MMPLAYER_RELEASE_ELEMENT(player, mainbin, MMPLAYER_M_AUTOPLUG); /* decodebin */
750                         MMPLAYER_RELEASE_ELEMENT(player, mainbin, MMPLAYER_M_TYPEFIND);
751                         MMPLAYER_RELEASE_ELEMENT(player, mainbin, MMPLAYER_M_SRC);
752                 } else {
753                         MMPLAYER_RELEASE_ELEMENT(player, mainbin, MMPLAYER_M_AUTOPLUG); /* uridecodebin */
754                         mainbin[MMPLAYER_M_MUXED_S_BUFFER].gst = NULL;
755                         mainbin[MMPLAYER_M_SRC].gst = NULL;
756                 }
757
758                 /* Initialize Player values */
759                 __mmplayer_initialize_gapless_play(player);
760
761                 _mmplayer_activate_next_source(player, GST_STATE_PLAYING);
762         }
763         MMPLAYER_GAPLESS_PLAY_THREAD_UNLOCK(player);
764
765         return NULL;
766 }
767
768 static void
769 __mmplayer_remove_g_source_from_context(GMainContext *context, guint source_id)
770 {
771         GSource *source = NULL;
772
773         MMPLAYER_FENTER();
774
775         source = g_main_context_find_source_by_id(context, source_id);
776         if (source != NULL) {
777                 LOGW("context: %p, source id: %d, source: %p", context, source_id, source);
778                 g_source_destroy(source);
779         }
780
781         MMPLAYER_FLEAVE();
782 }
783
784 void
785 _mmplayer_watcher_removed_notify(gpointer data)
786 {
787         mmplayer_t *player = (mmplayer_t *)data;
788         MMPLAYER_RETURN_IF_FAIL(player);
789
790         MMPLAYER_BUS_WATCHER_LOCK(player);
791         player->bus_watcher = 0;
792         MMPLAYER_BUS_WATCHER_SIGNAL(player);
793         MMPLAYER_BUS_WATCHER_UNLOCK(player);
794 }
795
796 void
797 _mmplayer_bus_watcher_remove(MMHandleType hplayer)
798 {
799         mmplayer_t *player = (mmplayer_t *)hplayer;
800         gint64 end_time = 0;
801         MMPLAYER_FENTER();
802         MMPLAYER_RETURN_IF_FAIL(player);
803
804         /* disconnecting bus watch */
805         if (player->bus_watcher > 0) {
806                 __mmplayer_remove_g_source_from_context(player->context.thread_default, player->bus_watcher);
807                 MMPLAYER_BUS_WATCHER_LOCK(player);
808                 end_time = g_get_monotonic_time () + 2 * G_TIME_SPAN_SECOND;
809                 while (player->bus_watcher > 0) {
810                         if (!MMPLAYER_BUS_WATCHER_WAIT_UNTIL(player, end_time)) {
811                                 LOGW("MMPLAYER_BUS_WATCHER_WAIT_UNTIL() timeout has passed - bus_watcher (%d)",
812                                                 player->bus_watcher);
813                                 break;
814                         }
815                 }
816                 MMPLAYER_BUS_WATCHER_UNLOCK(player);
817                 g_mutex_clear(&player->bus_watcher_mutex);
818                 g_cond_clear(&player->bus_watcher_cond);
819         }
820
821         MMPLAYER_FLEAVE();
822 }
823
824 void
825 _mmplayer_bus_msg_thread_destroy(MMHandleType hplayer)
826 {
827         mmplayer_t *player = (mmplayer_t *)hplayer;
828         GstMessage *msg = NULL;
829         GQueue *queue = NULL;
830
831         MMPLAYER_FENTER();
832         MMPLAYER_RETURN_IF_FAIL(player);
833
834         /* destroy the gst bus msg thread */
835         if (player->bus_msg_thread) {
836                 MMPLAYER_BUS_MSG_THREAD_LOCK(player);
837                 player->bus_msg_thread_exit = TRUE;
838                 MMPLAYER_BUS_MSG_THREAD_SIGNAL(player);
839                 MMPLAYER_BUS_MSG_THREAD_UNLOCK(player);
840
841                 LOGD("gst bus msg thread exit.");
842                 g_thread_join(player->bus_msg_thread); /* can request cmd lock */
843                 player->bus_msg_thread = NULL;
844
845                 g_mutex_clear(&player->bus_msg_thread_mutex);
846                 g_cond_clear(&player->bus_msg_thread_cond);
847         }
848
849         g_mutex_lock(&player->bus_msg_q_lock);
850         queue = player->bus_msg_q;
851         while (!g_queue_is_empty(queue)) {
852                 msg = (GstMessage *)g_queue_pop_head(queue);
853                 if (!msg) {
854                         LOGW("msg is null");
855                         continue;
856                 }
857                 LOGW("remove remained %s msg", GST_MESSAGE_TYPE_NAME(msg));
858                 gst_message_unref(msg);
859         }
860         g_mutex_unlock(&player->bus_msg_q_lock);
861
862         MMPLAYER_FLEAVE();
863 }
864
865 gboolean
866 _mmplayer_gst_remove_fakesink(mmplayer_t *player, mmplayer_gst_element_t *fakesink)
867 {
868         GstElement *parent = NULL;
869
870         MMPLAYER_RETURN_VAL_IF_FAIL(player && player->pipeline, FALSE);
871         MMPLAYER_RETURN_VAL_IF_FAIL(fakesink && fakesink->gst, TRUE);
872
873         /* lock */
874         MMPLAYER_FSINK_LOCK(player);
875
876         /* get parent of fakesink */
877         parent = (GstElement *)gst_object_get_parent((GstObject *)fakesink->gst);
878         if (!parent) {
879                 LOGD("fakesink already removed");
880                 goto ERROR;
881         }
882
883         gst_element_set_locked_state(fakesink->gst, TRUE);
884
885         /* setting the state to NULL never returns async
886          * so no need to wait for completion of state transition
887          */
888         if (GST_STATE_CHANGE_FAILURE == gst_element_set_state(fakesink->gst, GST_STATE_NULL))
889                 LOGE("fakesink state change failure!");
890                 /* FIXIT : should I return here? or try to proceed to next? */
891                 /* return FALSE; */
892
893         /* remove fakesink from it's parent */
894         if (!gst_bin_remove(GST_BIN(parent), fakesink->gst)) {
895                 LOGE("failed to remove fakesink");
896
897                 gst_object_unref(parent);
898
899                 goto ERROR;
900         }
901
902         gst_object_unref(parent);
903
904         LOGD("state-holder removed");
905
906         gst_element_set_locked_state(fakesink->gst, FALSE);
907
908         MMPLAYER_FSINK_UNLOCK(player);
909         return TRUE;
910
911 ERROR:
912         if (fakesink->gst)
913                 gst_element_set_locked_state(fakesink->gst, FALSE);
914
915         MMPLAYER_FSINK_UNLOCK(player);
916         return FALSE;
917 }
918
919 static GstPadProbeReturn
920 __mmplayer_gst_selector_blocked(GstPad *pad, GstPadProbeInfo *info, gpointer data)
921 {
922         LOGD("pad(%s:%s) is blocked", GST_DEBUG_PAD_NAME(pad));
923         return GST_PAD_PROBE_OK;
924 }
925
926 static void
927 __mmplayer_gst_selector_update_start_time(mmplayer_t *player, mmplayer_track_type_e stream_type)
928 {
929         gint64 stop_running_time = 0;
930         gint64 position_running_time = 0;
931         gint64 position = 0;
932         gint idx = 0;
933
934         for (idx = MM_PLAYER_TRACK_TYPE_AUDIO; idx < MM_PLAYER_TRACK_TYPE_TEXT; idx++) {
935                 if ((player->gapless.update_segment[idx] == TRUE) ||
936                         !(player->track[idx].event_probe_id)) {
937 #ifdef __DEBUG__
938                         LOGW("[%d] skip", idx);
939 #endif
940                         continue;
941                 }
942
943                 if (GST_CLOCK_TIME_IS_VALID(player->gapless.segment[idx].stop)) {
944                         stop_running_time =
945                                 gst_segment_to_running_time(&player->gapless.segment[idx],
946                                                 GST_FORMAT_TIME, player->gapless.segment[idx].stop);
947                 } else if (GST_CLOCK_TIME_IS_VALID(player->gapless.segment[idx].duration)) {
948                         stop_running_time =
949                                 gst_segment_to_running_time(&player->gapless.segment[idx],
950                                                 GST_FORMAT_TIME, player->gapless.segment[idx].duration);
951                 } else {
952                         LOGD("duration: %"GST_TIME_FORMAT, GST_TIME_ARGS(player->duration));
953                         stop_running_time =
954                                 gst_segment_to_running_time(&player->gapless.segment[idx],
955                                                 GST_FORMAT_TIME, player->duration);
956                 }
957
958                 position_running_time =
959                         gst_segment_to_running_time(&player->gapless.segment[idx],
960                         GST_FORMAT_TIME, player->gapless.segment[idx].position);
961
962                 LOGD("[type:%d] time info %" GST_TIME_FORMAT " , %"
963                         GST_TIME_FORMAT" , %" GST_TIME_FORMAT,
964                         idx,
965                         GST_TIME_ARGS(stop_running_time),
966                         GST_TIME_ARGS(position_running_time),
967                         GST_TIME_ARGS(gst_segment_to_running_time(&player->gapless.segment[idx],
968                         GST_FORMAT_TIME, player->gapless.segment[idx].start)));
969
970                 position_running_time = MAX(position_running_time, stop_running_time);
971                 position_running_time -= gst_segment_to_running_time(&player->gapless.segment[idx],
972                                                                                 GST_FORMAT_TIME, player->gapless.segment[idx].start);
973                 position_running_time = MAX(0, position_running_time);
974                 position = MAX(position, position_running_time);
975         }
976
977         if (position != 0) {
978                 LOGD("[%d]GST_EVENT_STREAM_START: start_time from %" GST_TIME_FORMAT " to %" GST_TIME_FORMAT,
979                         stream_type, GST_TIME_ARGS(player->gapless.start_time[stream_type]),
980                         GST_TIME_ARGS(player->gapless.start_time[stream_type] + position));
981
982                 player->gapless.start_time[stream_type] += position;
983         }
984
985         return;
986 }
987
988 static GstPadProbeReturn
989 __mmplayer_gst_selector_event_probe(GstPad *pad, GstPadProbeInfo *info, gpointer data)
990 {
991         GstPadProbeReturn ret = GST_PAD_PROBE_OK;
992         GstEvent *event = GST_PAD_PROBE_INFO_DATA(info);
993         mmplayer_t *player = (mmplayer_t *)data;
994         GstCaps *caps = NULL;
995         GstStructure *str = NULL;
996         const gchar *name = NULL;
997         mmplayer_track_type_e stream_type = MM_PLAYER_TRACK_TYPE_VIDEO;
998         gboolean caps_ret = TRUE;
999
1000         if (GST_EVENT_TYPE(event) != GST_EVENT_STREAM_START &&
1001                 GST_EVENT_TYPE(event) != GST_EVENT_FLUSH_STOP &&
1002                 GST_EVENT_TYPE(event) != GST_EVENT_SEGMENT &&
1003                 GST_EVENT_TYPE(event) != GST_EVENT_EOS &&
1004                 GST_EVENT_TYPE(event) != GST_EVENT_QOS)
1005                 return ret;
1006
1007         MMPLAYER_GST_GET_CAPS_INFO_FROM_PAD(pad, caps, str, name, caps_ret);
1008         if (!caps_ret)
1009                 goto ERROR;
1010
1011         if (strstr(name, "audio")) {
1012                 stream_type = MM_PLAYER_TRACK_TYPE_AUDIO;
1013         } else if (strstr(name, "video")) {
1014                 stream_type = MM_PLAYER_TRACK_TYPE_VIDEO;
1015         } else {
1016                 /* text track is not supportable */
1017                 LOGE("invalid name %s", name);
1018                 goto ERROR;
1019         }
1020
1021         switch (GST_EVENT_TYPE(event)) {
1022         case GST_EVENT_EOS:
1023                 {
1024                         /* in case of gapless, drop eos event not to send it to sink */
1025                         if (player->gapless.reconfigure && !player->msg_posted) {
1026                                 LOGD("[%d] %s:%s EOS received but will be drop", stream_type, GST_DEBUG_PAD_NAME(pad));
1027                                 ret = GST_PAD_PROBE_DROP;
1028                         }
1029                         break;
1030                 }
1031         case GST_EVENT_STREAM_START:
1032                 {
1033                         __mmplayer_gst_selector_update_start_time(player, stream_type);
1034                         break;
1035                 }
1036         case GST_EVENT_FLUSH_STOP:
1037                 {
1038                         LOGD("[%d] GST_EVENT_FLUSH_STOP", stream_type);
1039                         gst_segment_init(&player->gapless.segment[stream_type], GST_FORMAT_UNDEFINED);
1040                         player->gapless.start_time[stream_type] = 0;
1041                         break;
1042                 }
1043         case GST_EVENT_SEGMENT:
1044                 {
1045                         GstSegment segment;
1046                         GstEvent *tmpev;
1047
1048                         LOGD("[%d] GST_EVENT_SEGMENT", stream_type);
1049                         gst_event_copy_segment(event, &segment);
1050
1051                         if (segment.format != GST_FORMAT_TIME)
1052                                 break;
1053
1054                         LOGD("segment base:%" GST_TIME_FORMAT ", offset:%" GST_TIME_FORMAT
1055                                  ", start:%" GST_TIME_FORMAT ", stop: %" GST_TIME_FORMAT
1056                                  ", time: %" GST_TIME_FORMAT ", pos: %" GST_TIME_FORMAT ", dur: %" GST_TIME_FORMAT,
1057                                 GST_TIME_ARGS(segment.base), GST_TIME_ARGS(segment.offset),
1058                                 GST_TIME_ARGS(segment.start), GST_TIME_ARGS(segment.stop),
1059                                 GST_TIME_ARGS(segment.time), GST_TIME_ARGS(segment.position), GST_TIME_ARGS(segment.duration));
1060
1061                         /* keep the all the segment ev to cover the seeking */
1062                         gst_segment_copy_into(&segment, &player->gapless.segment[stream_type]);
1063                         player->gapless.update_segment[stream_type] = TRUE;
1064
1065                         if (!player->gapless.running)
1066                                 break;
1067
1068                         player->gapless.segment[stream_type].base = player->gapless.start_time[stream_type];
1069
1070                         LOGD("[%d] new base: %" GST_TIME_FORMAT, stream_type, GST_TIME_ARGS(player->gapless.segment[stream_type].base));
1071
1072                         tmpev = gst_event_new_segment(&player->gapless.segment[stream_type]);
1073                         gst_event_set_seqnum(tmpev, gst_event_get_seqnum(event));
1074                         gst_event_unref(event);
1075                         GST_PAD_PROBE_INFO_DATA(info) = tmpev;
1076
1077                         break;
1078                 }
1079         case GST_EVENT_QOS:
1080                 {
1081                         gdouble proportion = 0.0;
1082                         GstClockTimeDiff diff = 0;
1083                         GstClockTime timestamp = 0;
1084                         gint64 running_time_diff = -1;
1085                         GstQOSType type = 0;
1086                         GstEvent *tmpev = NULL;
1087
1088                         running_time_diff = player->gapless.segment[stream_type].base;
1089
1090                         if (running_time_diff <= 0) /* don't need to adjust */
1091                                 break;
1092
1093                         gst_event_parse_qos(event, &type, &proportion, &diff, &timestamp);
1094                         gst_event_unref(event);
1095
1096                         if (timestamp < running_time_diff) {
1097                                 LOGW("QOS event from previous group");
1098                                 ret = GST_PAD_PROBE_DROP;
1099                                 break;
1100                         }
1101
1102 #ifdef __DEBUG__
1103                         LOGD("[%d] Adjusting QOS event: %" GST_TIME_FORMAT
1104                                  " - %" GST_TIME_FORMAT " = %" GST_TIME_FORMAT,
1105                                                 stream_type, GST_TIME_ARGS(timestamp),
1106                                                 GST_TIME_ARGS(running_time_diff),
1107                                                 GST_TIME_ARGS(timestamp - running_time_diff));
1108 #endif
1109
1110                         timestamp -= running_time_diff;
1111
1112                         /* That case is invalid for QoS events */
1113                         if (diff < 0 && -diff > timestamp) {
1114                                 LOGW("QOS event from previous group");
1115                                 ret = GST_PAD_PROBE_DROP;
1116                                 break;
1117                         }
1118
1119                         tmpev = gst_event_new_qos(GST_QOS_TYPE_UNDERFLOW, proportion, diff, timestamp);
1120                         GST_PAD_PROBE_INFO_DATA(info) = tmpev;
1121
1122                         break;
1123                 }
1124         default:
1125                 break;
1126         }
1127
1128 ERROR:
1129         if (caps)
1130                 gst_caps_unref(caps);
1131         return ret;
1132 }
1133
1134 /* create fakesink for audio or video path without audiobin or videobin */
1135 static void
1136 __mmplayer_gst_make_fakesink(mmplayer_t *player, GstPad *pad, const gchar *name)
1137 {
1138         GstElement *pipeline = NULL;
1139         GstElement *fakesink = NULL;
1140         GstPad *sinkpad = NULL;
1141
1142         MMPLAYER_FENTER();
1143         MMPLAYER_RETURN_IF_FAIL(player && player->pipeline && player->pipeline->mainbin);
1144
1145         pipeline = player->pipeline->mainbin[MMPLAYER_M_PIPE].gst;
1146
1147         /* fake sink */
1148         fakesink = gst_element_factory_make("fakesink", NULL);
1149         if (fakesink == NULL) {
1150                 LOGE("failed to create fakesink");
1151                 return;
1152         }
1153
1154         if (!gst_bin_add(GST_BIN(pipeline), fakesink)) {
1155                 LOGE("failed to add fakesink to pipeline");
1156                 goto ERROR;
1157         }
1158
1159         /* link */
1160         sinkpad = gst_element_get_static_pad(fakesink, "sink");
1161
1162         LOGD("pad link %s:%s - %s:%s", GST_DEBUG_PAD_NAME(pad), GST_DEBUG_PAD_NAME(sinkpad));
1163
1164         if (gst_pad_link(pad, sinkpad) != GST_PAD_LINK_OK) {
1165                 LOGE("failed to link fakesink");
1166                 goto ERROR;
1167         }
1168
1169         if (strstr(name, "video")) {
1170                 if (player->v_stream_caps) {
1171                         gst_caps_unref(player->v_stream_caps);
1172                         player->v_stream_caps = NULL;
1173                 }
1174                 if (player->ini.set_dump_element_flag)
1175                         __mmplayer_add_dump_buffer_probe(player, fakesink);
1176         }
1177
1178         g_object_set(G_OBJECT(fakesink), "sync", TRUE, NULL);
1179         gst_element_set_state(fakesink, GST_STATE_PAUSED);
1180
1181         /* store it as it's sink element */
1182         __mmplayer_add_sink(player, fakesink, FALSE);
1183
1184         if (sinkpad)
1185                 gst_object_unref(GST_OBJECT(sinkpad));
1186
1187         MMPLAYER_FLEAVE();
1188         return;
1189
1190 ERROR:
1191
1192         if (sinkpad)
1193                 gst_object_unref(GST_OBJECT(sinkpad));
1194
1195         if (fakesink) {
1196                 gst_element_set_state(fakesink, GST_STATE_NULL);
1197
1198                 if (!gst_bin_remove(GST_BIN(pipeline), fakesink))
1199                         gst_object_unref(GST_OBJECT(fakesink));
1200         }
1201
1202         return;
1203 }
1204
1205 static GstElement *
1206 __mmplayer_gst_make_concat(mmplayer_t *player, main_element_id_e elem_idx)
1207 {
1208         GstElement *pipeline = NULL;
1209         GstElement *concat = NULL;
1210
1211         MMPLAYER_FENTER();
1212         MMPLAYER_RETURN_VAL_IF_FAIL(player && player->pipeline && player->pipeline->mainbin, NULL);
1213
1214         concat = gst_element_factory_make("concat", NULL);
1215         if (!concat) {
1216                 LOGE("failed to create concat");
1217                 return NULL;
1218         }
1219
1220         gst_element_set_state(concat, GST_STATE_PAUSED);
1221
1222         pipeline = player->pipeline->mainbin[MMPLAYER_M_PIPE].gst;
1223         if (!gst_bin_add(GST_BIN(pipeline), concat)) {
1224                 LOGE("failed to add concat to pipeline");
1225                 gst_element_set_state(concat, GST_STATE_NULL);
1226                 gst_object_unref(GST_OBJECT(concat));
1227                 return NULL;
1228         }
1229
1230         LOGD("Create concat [%d] element", elem_idx);
1231
1232         player->pipeline->mainbin[elem_idx].id = elem_idx;
1233         player->pipeline->mainbin[elem_idx].gst = concat;
1234
1235         MMPLAYER_FLEAVE();
1236         return concat;
1237 }
1238
1239 static GstElement *
1240 __mmplayer_gst_make_selector(mmplayer_t *player, main_element_id_e elem_idx, mmplayer_track_type_e stream_type)
1241 {
1242         GstElement *pipeline = NULL;
1243         GstElement *selector = NULL;
1244         GstPad *srcpad = NULL;
1245
1246         MMPLAYER_FENTER();
1247         MMPLAYER_RETURN_VAL_IF_FAIL(player && player->pipeline && player->pipeline->mainbin, NULL);
1248
1249         selector = gst_element_factory_make("input-selector", NULL);
1250         if (!selector) {
1251                 LOGE("failed to create input-selector");
1252                 return NULL;
1253         }
1254         g_object_set(selector, "sync-streams", TRUE, NULL);
1255
1256         srcpad = gst_element_get_static_pad(selector, "src");
1257
1258         LOGD("blocking %s:%s", GST_DEBUG_PAD_NAME(srcpad));
1259         player->track[stream_type].block_id = gst_pad_add_probe(srcpad, GST_PAD_PROBE_TYPE_BLOCK_DOWNSTREAM,
1260                 __mmplayer_gst_selector_blocked, NULL, NULL);
1261         player->track[stream_type].event_probe_id = gst_pad_add_probe(srcpad, GST_PAD_PROBE_TYPE_EVENT_BOTH|GST_PAD_PROBE_TYPE_EVENT_FLUSH,
1262                 __mmplayer_gst_selector_event_probe, player, NULL);
1263
1264         gst_element_set_state(selector, GST_STATE_PAUSED);
1265
1266         pipeline = player->pipeline->mainbin[MMPLAYER_M_PIPE].gst;
1267         if (!gst_bin_add(GST_BIN(pipeline), selector)) {
1268                 LOGE("failed to add selector to pipeline");
1269
1270                 if (player->track[stream_type].block_id != 0)
1271                   gst_pad_remove_probe (srcpad, player->track[stream_type].block_id);
1272                 player->track[stream_type].block_id = 0;
1273
1274                 if (player->track[stream_type].event_probe_id != 0)
1275                   gst_pad_remove_probe (srcpad, player->track[stream_type].event_probe_id);
1276                 player->track[stream_type].event_probe_id = 0;
1277
1278                 gst_object_unref(GST_OBJECT(srcpad));
1279
1280                 gst_element_set_state(selector, GST_STATE_NULL);
1281                 gst_object_unref(GST_OBJECT(selector));
1282                 return NULL;
1283         }
1284
1285         gst_object_unref(GST_OBJECT(srcpad));
1286
1287         player->pipeline->mainbin[elem_idx].id = elem_idx;
1288         player->pipeline->mainbin[elem_idx].gst = selector;
1289
1290         MMPLAYER_FLEAVE();
1291         return selector;
1292 }
1293
1294 void
1295 _mmplayer_gst_decode_pad_added(GstElement *elem, GstPad *pad, gpointer data)
1296 {
1297         mmplayer_t *player = (mmplayer_t *)data;
1298         GstElement *combiner = NULL;
1299         GstCaps *caps = NULL;
1300         GstStructure *str = NULL;
1301         const gchar *name = NULL;
1302         GstPad *sinkpad = NULL;
1303         gboolean first_track = FALSE;
1304         gboolean caps_ret = TRUE;
1305
1306         main_element_id_e elem_idx = MMPLAYER_M_NUM;
1307         mmplayer_track_type_e stream_type = MM_PLAYER_TRACK_TYPE_AUDIO;
1308
1309         MMPLAYER_FENTER();
1310         MMPLAYER_RETURN_IF_FAIL(elem && pad);
1311         MMPLAYER_RETURN_IF_FAIL(player && player->pipeline && player->pipeline->mainbin);
1312
1313         LOGD("pad-added signal handling");
1314
1315         /* get mimetype from caps */
1316         MMPLAYER_GST_GET_CAPS_INFO_FROM_PAD(pad, caps, str, name, caps_ret);
1317         if (!caps_ret)
1318                 goto ERROR;
1319
1320         MMPLAYER_LOG_GST_CAPS_TYPE(caps);
1321 #ifdef __DEBUG__
1322         LOGD("detected mimetype : %s", name);
1323 #endif
1324
1325         if (strstr(name, "video")) {
1326                 gint stype = 0;
1327                 gchar *caps_str = NULL;
1328
1329                 caps_str = gst_caps_to_string(caps);
1330                 if (caps_str && (strstr(caps_str, "ST12") || strstr(caps_str, "SN12") ||
1331                         strstr(caps_str, "SN21") || strstr(caps_str, "S420") || strstr(caps_str, "SR32")))
1332                         player->set_mode.video_zc = true;
1333
1334                 MMPLAYER_FREEIF(caps_str);
1335
1336                 mm_player_set_attribute((MMHandleType)player, NULL, "content_video_found", TRUE, NULL);
1337                 mm_attrs_get_int_by_name(player->attrs, "display_surface_type", &stype);
1338
1339                 LOGD("surface type : %d", stype);
1340
1341                 if (MMPLAYER_IS_MS_BUFF_SRC(player)) {
1342                         __mmplayer_gst_create_sink_bin(elem, pad, caps, player);
1343                         goto DONE;
1344                 }
1345
1346                 /* in case of exporting video frame, it requires the 360 video filter.
1347                  * it will be handled in _no_more_pads(). */
1348                 if ((stype == MM_DISPLAY_SURFACE_NULL) && (!player->set_mode.video_export)) {
1349                         __mmplayer_gst_make_fakesink(player, pad, name);
1350                         goto DONE;
1351                 }
1352
1353                 if (MMPLAYER_USE_DECODEBIN(player)) {
1354                         LOGD("video selector is required");
1355                         elem_idx = MMPLAYER_M_V_INPUT_SELECTOR;
1356                 } else {
1357                         LOGD("video concat is required");
1358                         elem_idx = MMPLAYER_M_V_CONCAT;
1359                 }
1360                 stream_type = MM_PLAYER_TRACK_TYPE_VIDEO;
1361         } else if (strstr(name, "audio")) {
1362                 gint samplerate = 0;
1363                 gint channels = 0;
1364
1365                 if (MMPLAYER_IS_MS_BUFF_SRC(player) || player->build_audio_offload) {
1366                         if (player->build_audio_offload)
1367                                 player->no_more_pad = TRUE; /* remove state holder */
1368                         __mmplayer_gst_create_sink_bin(elem, pad, caps, player);
1369                         goto DONE;
1370                 }
1371
1372                 gst_structure_get_int(str, "rate", &samplerate);
1373                 gst_structure_get_int(str, "channels", &channels);
1374
1375                 if ((channels > 0 && samplerate == 0)) { /* exclude audio decoding */
1376                         __mmplayer_gst_make_fakesink(player, pad, name);
1377                         goto DONE;
1378                 }
1379                 if (MMPLAYER_USE_DECODEBIN(player)) {
1380                         LOGD("audio selector is required");
1381                         elem_idx = MMPLAYER_M_A_INPUT_SELECTOR;
1382                 } else {
1383                         LOGD("audio concat is required");
1384                         elem_idx = MMPLAYER_M_A_CONCAT;
1385                 }
1386                 stream_type = MM_PLAYER_TRACK_TYPE_AUDIO;
1387
1388         } else if (strstr(name, "text")) {
1389                 if (MMPLAYER_USE_DECODEBIN(player)) {
1390                         LOGD("text selector is required");
1391                         elem_idx = MMPLAYER_M_T_INPUT_SELECTOR;
1392                 } else {
1393                         LOGD("text concat is required");
1394                         elem_idx = MMPLAYER_M_T_CONCAT;
1395                 }
1396                 stream_type = MM_PLAYER_TRACK_TYPE_TEXT;
1397         } else {
1398                 LOGE("invalid caps info");
1399                 goto ERROR;
1400         }
1401
1402         /* check selector and create it */
1403         if (!(combiner = player->pipeline->mainbin[elem_idx].gst)) {
1404                 if (MMPLAYER_USE_DECODEBIN(player))
1405                         combiner = __mmplayer_gst_make_selector(player, elem_idx, stream_type);
1406                 else
1407                         combiner = __mmplayer_gst_make_concat(player, elem_idx);
1408
1409                 if (!combiner)
1410                         goto ERROR;
1411                 first_track = TRUE;
1412         } else {
1413                 LOGD("Combiner element is already created.");
1414         }
1415
1416         /* link */
1417         sinkpad = gst_element_request_pad_simple(combiner, "sink_%u");
1418
1419         LOGD("pad link: %s:%s - %s:%s", GST_DEBUG_PAD_NAME(pad), GST_DEBUG_PAD_NAME(sinkpad));
1420
1421         if (gst_pad_link(pad, sinkpad) != GST_PAD_LINK_OK) {
1422                 LOGE("failed to link combiner");
1423                 gst_object_unref(GST_OBJECT(combiner));
1424                 goto ERROR;
1425         }
1426
1427         if (first_track) {
1428                 if (MMPLAYER_USE_DECODEBIN(player)) {
1429                         LOGD("this track will be activated");
1430                         g_object_set(combiner, "active-pad", sinkpad, NULL);
1431                 }
1432         }
1433
1434         if (MMPLAYER_USE_DECODEBIN(player)) {
1435                 _mmplayer_track_update_stream(player, stream_type, sinkpad);
1436         } else {
1437                 /* apply the text track information */
1438                 if (stream_type == MM_PLAYER_TRACK_TYPE_TEXT)
1439                         mm_player_set_attribute((MMHandleType)player, NULL,
1440                                         "content_text_track_num", player->track[stream_type].total_track_num,
1441                                         "current_text_track_index", player->track[stream_type].active_track_index, NULL);
1442                 __mmplayer_create_sink_path(player, combiner, stream_type, caps);
1443         }
1444
1445 DONE:
1446 ERROR:
1447
1448         if (caps)
1449                 gst_caps_unref(caps);
1450
1451         if (sinkpad) {
1452                 gst_object_unref(GST_OBJECT(sinkpad));
1453                 sinkpad = NULL;
1454         }
1455
1456         MMPLAYER_GENERATE_DOT_IF_ENABLED(player, "pipeline-pad-added");
1457         return;
1458 }
1459
1460 static gboolean
1461 __mmplayer_create_sink_path(mmplayer_t *player, GstElement *combiner, mmplayer_track_type_e type, GstCaps *caps)
1462 {
1463         GstPad *srcpad = NULL;
1464
1465         MMPLAYER_FENTER();
1466         MMPLAYER_RETURN_VAL_IF_FAIL(player && player->pipeline, FALSE);
1467
1468         LOGD("type %d", type);
1469
1470         if (!combiner) {
1471                 LOGD("there is no %d track", type);
1472                 return TRUE;
1473         }
1474
1475         srcpad = gst_element_get_static_pad(combiner, "src");
1476         if (!srcpad) {
1477                 LOGE("failed to get srcpad from combiner");
1478                 return FALSE;
1479         }
1480
1481         LOGD("got pad %s:%s from combiner", GST_DEBUG_PAD_NAME(srcpad));
1482
1483         __mmplayer_gst_create_sink_bin(combiner, srcpad, caps, player);
1484
1485         LOGD("unblocking %s:%s", GST_DEBUG_PAD_NAME(srcpad));
1486         if (player->track[type].block_id) {
1487                 gst_pad_remove_probe(srcpad, player->track[type].block_id);
1488                 player->track[type].block_id = 0;
1489         }
1490
1491         if (srcpad) {
1492                 gst_object_unref(GST_OBJECT(srcpad));
1493                 srcpad = NULL;
1494         }
1495
1496         MMPLAYER_FLEAVE();
1497         return TRUE;
1498 }
1499
1500 static void
1501 __mmplayer_set_decode_track_info(mmplayer_t *player, mmplayer_track_type_e type)
1502 {
1503         gint active_index = 0;
1504
1505         MMPLAYER_FENTER();
1506         MMPLAYER_RETURN_IF_FAIL(player);
1507
1508         LOGD("type: %d, the num of track: %d", type, player->track[type].total_track_num);
1509
1510         /* change track to active pad */
1511         active_index = player->track[type].active_track_index;
1512         if ((active_index != DEFAULT_TRACK_INDEX) &&
1513                 (__mmplayer_change_selector_pad(player, type, active_index) != MM_ERROR_NONE)) {
1514                 LOGW("failed to change %d type track to %d", type, active_index);
1515                 player->track[type].active_track_index = DEFAULT_TRACK_INDEX;
1516                 return;
1517         }
1518
1519         if (type == MM_PLAYER_TRACK_TYPE_TEXT)
1520                 mm_player_set_attribute((MMHandleType)player, NULL,
1521                                 "content_text_track_num", player->track[type].total_track_num,
1522                                 "current_text_track_index", player->track[type].active_track_index, NULL);
1523
1524         MMPLAYER_FLEAVE();
1525         return;
1526 }
1527
1528 static gboolean
1529 __mmplayer_create_audio_sink_path(mmplayer_t *player, GstElement *audio_selector)
1530 {
1531         MMPLAYER_FENTER();
1532         MMPLAYER_RETURN_VAL_IF_FAIL(player && player->pipeline, FALSE);
1533
1534         if (!audio_selector) {
1535                 LOGD("there is no audio track, num_dynamic_pad %d", player->num_dynamic_pad);
1536
1537                 /* in case the source is changed, output can be changed. */
1538                 if ((player->pipeline->audiobin) && (player->pipeline->audiobin[MMPLAYER_A_BIN].gst)) {
1539                         LOGD("remove previous audiobin if it exist");
1540
1541                         __mmplayer_release_signal_connection(player, MM_PLAYER_SIGNAL_TYPE_AUDIOBIN);
1542                         __mmplayer_del_sink(player, player->pipeline->audiobin[MMPLAYER_A_SINK].gst);
1543
1544                         MMPLAYER_RELEASE_ELEMENT(player, player->pipeline->audiobin, MMPLAYER_A_BIN);
1545                         MMPLAYER_FREEIF(player->pipeline->audiobin);
1546                 }
1547
1548                 if (player->num_dynamic_pad == 0) /* FIXME: num_dynamic_pad is only for rtsp? */
1549                         _mmplayer_pipeline_complete(NULL, player);
1550
1551                 return TRUE;
1552         }
1553
1554         /* apply the audio track information */
1555         if (MMPLAYER_USE_DECODEBIN(player))
1556                 __mmplayer_set_decode_track_info(player, MM_PLAYER_TRACK_TYPE_AUDIO);
1557
1558         /* create audio sink path */
1559         if (!__mmplayer_create_sink_path(player, audio_selector, MM_PLAYER_TRACK_TYPE_AUDIO, NULL)) {
1560                 LOGE("failed to create audio sink path");
1561                 return FALSE;
1562         }
1563
1564         MMPLAYER_FLEAVE();
1565         return TRUE;
1566 }
1567
1568 static gboolean
1569 __mmplayer_create_text_sink_path(mmplayer_t *player, GstElement *text_selector)
1570 {
1571         MMPLAYER_FENTER();
1572         MMPLAYER_RETURN_VAL_IF_FAIL(player && text_selector, FALSE);
1573
1574         if (MMPLAYER_IS_MS_BUFF_SRC(player)) {
1575                 LOGD("text path is not supported");
1576                 return TRUE;
1577         }
1578
1579         /* apply the text track information */
1580         __mmplayer_set_decode_track_info(player, MM_PLAYER_TRACK_TYPE_TEXT);
1581
1582         if (player->track[MM_PLAYER_TRACK_TYPE_TEXT].total_track_num > 0)
1583                 player->has_closed_caption = TRUE;
1584
1585         /* create text decode path */
1586         player->no_more_pad = TRUE;
1587
1588         if (!__mmplayer_create_sink_path(player, text_selector, MM_PLAYER_TRACK_TYPE_TEXT, NULL)) {
1589                 LOGE("failed to create text sink path");
1590                 return FALSE;
1591         }
1592
1593         MMPLAYER_FLEAVE();
1594         return TRUE;
1595 }
1596
1597 static gboolean
1598 __mmplayer_gst_set_queue2_buffering(mmplayer_t *player)
1599 {
1600         gint64 dur_bytes = 0L;
1601
1602         MMPLAYER_FENTER();
1603         MMPLAYER_RETURN_VAL_IF_FAIL(player && player->pipeline &&
1604                 player->pipeline->mainbin && player->streamer, FALSE);
1605
1606         if (!gst_element_query_duration(player->pipeline->mainbin[MMPLAYER_M_SRC].gst, GST_FORMAT_BYTES, &dur_bytes))
1607                 LOGE("fail to get duration.");
1608
1609         /* there is no mq, enable use-buffering on queue2 (ex) wav streaming
1610          * use file information was already set on Q2 when it was created. */
1611         _mm_player_streaming_set_queue2(player->streamer,
1612                                         player->pipeline->mainbin[MMPLAYER_M_MUXED_S_BUFFER].gst,
1613                                         TRUE,                                                           /* use_buffering */
1614                                         MUXED_BUFFER_TYPE_MAX,                          /* use previous buffer type setting */
1615                                         ((dur_bytes > 0) ? ((guint64)dur_bytes) : 0));
1616
1617         MMPLAYER_FLEAVE();
1618         return TRUE;
1619 }
1620
1621 void
1622 _mmplayer_gst_decode_no_more_pads(GstElement *elem, gpointer data)
1623 {
1624         mmplayer_t *player = NULL;
1625         GstElement *video_selector = NULL;
1626         GstElement *audio_selector = NULL;
1627         GstElement *text_selector = NULL;
1628
1629         MMPLAYER_FENTER();
1630         player = (mmplayer_t *)data;
1631
1632         LOGD("no-more-pad signal handling");
1633
1634         if ((player->cmd == MMPLAYER_COMMAND_DESTROY) ||
1635                 (player->cmd == MMPLAYER_COMMAND_UNREALIZE)) {
1636                 LOGW("player is shutting down");
1637                 goto EXIT;
1638         }
1639
1640         if ((MMPLAYER_IS_HTTP_STREAMING(player)) &&
1641                 (!player->pipeline->mainbin[MMPLAYER_M_DEMUXED_S_BUFFER].gst) &&
1642                 (player->pipeline->mainbin[MMPLAYER_M_MUXED_S_BUFFER].gst)) {
1643                 if (!__mmplayer_gst_set_queue2_buffering(player)) {
1644                         LOGE("failed to set queue2 buffering");
1645                         goto EXIT;
1646                 }
1647         }
1648
1649         video_selector = player->pipeline->mainbin[MMPLAYER_M_V_INPUT_SELECTOR].gst;
1650         audio_selector = player->pipeline->mainbin[MMPLAYER_M_A_INPUT_SELECTOR].gst;
1651         text_selector = player->pipeline->mainbin[MMPLAYER_M_T_INPUT_SELECTOR].gst;
1652
1653         if (!video_selector && !audio_selector && !text_selector) {
1654                 LOGW("there is no selector");
1655                 player->no_more_pad = TRUE;
1656                 goto EXIT;
1657         }
1658
1659         /* create video path followed by video-select */
1660         if (video_selector && !audio_selector && !text_selector)
1661                 player->no_more_pad = TRUE;
1662
1663         if (!__mmplayer_create_sink_path(player, video_selector, MM_PLAYER_TRACK_TYPE_VIDEO, NULL))
1664                 goto EXIT;
1665
1666         /* create audio path followed by audio-select */
1667         if (audio_selector && !text_selector)
1668                 player->no_more_pad = TRUE;
1669
1670         if (!__mmplayer_create_audio_sink_path(player, audio_selector))
1671                 goto EXIT;
1672
1673         /* create text path followed by text-select */
1674         __mmplayer_create_text_sink_path(player, text_selector);
1675
1676 EXIT:
1677         _mmplayer_set_reconfigure_state(player, FALSE);
1678         MMPLAYER_FLEAVE();
1679 }
1680
1681 static gboolean
1682 __mmplayer_gst_add_sinkbin_to_pipeline(mmplayer_t *player, GstElement *sinkbin, GstPad *pad, gboolean reusing, gchar *sink_pad_name)
1683 {
1684         gboolean ret = FALSE;
1685         GstElement *pipeline = NULL;
1686         GstPad *sinkpad = NULL;
1687
1688         MMPLAYER_FENTER();
1689         MMPLAYER_RETURN_VAL_IF_FAIL(sinkbin && pad, FALSE);
1690         MMPLAYER_RETURN_VAL_IF_FAIL(player && player->pipeline && player->pipeline->mainbin, FALSE);
1691
1692         pipeline = player->pipeline->mainbin[MMPLAYER_M_PIPE].gst;
1693
1694         sinkpad = gst_element_get_static_pad(GST_ELEMENT(sinkbin), sink_pad_name);
1695         if (!sinkpad) {
1696                 LOGE("failed to get pad from sinkbin");
1697                 goto EXIT;
1698         }
1699
1700         if (reusing) {
1701                 /* link only */
1702                 if (gst_pad_link(pad, sinkpad) != GST_PAD_LINK_OK) {
1703                         LOGE("failed to link sinkbin for reusing");
1704                         goto EXIT; /* exit either pass or fail */
1705                 }
1706         } else {
1707                 /* warm up */
1708                 if (gst_element_set_state(sinkbin, GST_STATE_READY) == GST_STATE_CHANGE_FAILURE) {
1709                         LOGE("failed to set state(READY) to sinkbin");
1710                         goto EXIT;
1711                 }
1712
1713                 /* add */
1714                 if (!gst_bin_add(GST_BIN(pipeline), sinkbin)) {
1715                         LOGE("failed to add sinkbin to pipeline");
1716                         goto EXIT;
1717                 }
1718
1719                 /* link */
1720                 if (gst_pad_link(pad, sinkpad) != GST_PAD_LINK_OK) {
1721                         LOGE("failed to link %s:%s - %s:%s", GST_DEBUG_PAD_NAME(pad), GST_DEBUG_PAD_NAME(sinkpad));
1722                         goto EXIT;
1723                 }
1724
1725                 /* run */
1726                 if (gst_element_set_state(sinkbin, GST_STATE_PAUSED) == GST_STATE_CHANGE_FAILURE) {
1727                         LOGE("failed to set state(PAUSED) to sinkbin");
1728                         goto EXIT;
1729                 }
1730         }
1731
1732         ret = TRUE;
1733
1734 EXIT:
1735         if (sinkpad)
1736                 gst_object_unref(GST_OBJECT(sinkpad));
1737         sinkpad = NULL;
1738
1739         MMPLAYER_FLEAVE();
1740         return ret;
1741 }
1742
1743 static void
1744 __mmplayer_gst_create_sink_bin(GstElement *elem, GstPad *pad, GstCaps *ref_caps, gpointer data)
1745 {
1746         mmplayer_t *player = NULL;
1747         GstCaps *caps = NULL;
1748         gchar *caps_str = NULL;
1749         GstStructure *str = NULL;
1750         const gchar *name = NULL;
1751         GstElement *sinkbin = NULL;
1752         gboolean reusing = FALSE;
1753         gboolean caps_ret = TRUE;
1754         gchar *sink_pad_name = "sink";
1755
1756         /* check handles */
1757         player = (mmplayer_t *)data;
1758
1759         MMPLAYER_FENTER();
1760         MMPLAYER_RETURN_IF_FAIL(elem && pad);
1761         MMPLAYER_RETURN_IF_FAIL(player && player->pipeline && MMPLAYER_GET_ATTRS(player));
1762         MMPLAYER_GST_GET_CAPS_INFO_FROM_PAD(pad, caps, str, name, caps_ret);
1763         if (!caps_ret) {
1764                 MMPLAYER_GST_GET_CAPS_INFO(ref_caps, str, name, caps_ret);
1765                 if (!caps_ret)
1766                         goto ERROR;
1767                 if (caps)
1768                         gst_caps_unref(caps);
1769                 caps = gst_caps_ref(ref_caps);
1770         }
1771
1772         caps_str = gst_caps_to_string(caps);
1773 #ifdef __DEBUG__
1774         LOGD("detected mimetype : %s", name);
1775 #endif
1776         if (strstr(name, "audio")) {
1777                 if (player->pipeline->audiobin == NULL) {
1778                         const gchar *audio_format = gst_structure_get_string(str, "format");
1779                         if (audio_format) {
1780                                 LOGD("original audio format %s", audio_format);
1781                                 mm_player_set_attribute((MMHandleType)player, NULL,
1782                                                 "content_audio_format", audio_format, strlen(audio_format), NULL);
1783                         }
1784
1785                         if (__mmplayer_gst_create_audio_sink_bin(player) != MM_ERROR_NONE) {
1786                                 LOGE("failed to create audiobin. continuing without audio");
1787                                 goto ERROR;
1788                         }
1789
1790                         sinkbin = player->pipeline->audiobin[MMPLAYER_A_BIN].gst;
1791                         LOGD("creating audiobin success");
1792                 } else {
1793                         reusing = TRUE;
1794                         sinkbin = player->pipeline->audiobin[MMPLAYER_A_BIN].gst;
1795                         LOGD("reusing audiobin");
1796                         _mmplayer_update_content_attrs(player, ATTR_AUDIO);
1797                 }
1798         } else if (strstr(name, "video")) {
1799                 /* 1. zero copy is updated at _decode_pad_added()
1800                  * 2. NULL surface type is handled in _decode_pad_added() */
1801                 LOGD("zero copy %d", player->set_mode.video_zc);
1802                 if (player->pipeline->videobin == NULL) {
1803                         int surface_type = 0;
1804                         mm_attrs_get_int_by_name(player->attrs, "display_surface_type", &surface_type);
1805                         LOGD("display_surface_type (%d)", surface_type);
1806
1807                         if ((surface_type == MM_DISPLAY_SURFACE_OVERLAY || surface_type == MM_DISPLAY_SURFACE_OVERLAY_SYNC_UI) &&
1808                                 (_mmplayer_acquire_hw_resource(player, MMPLAYER_RESOURCE_TYPE_VIDEO_OVERLAY) != MM_ERROR_NONE)) {
1809                                 LOGE("failed to acquire video overlay resource");
1810                                 goto ERROR;
1811                         }
1812
1813                         player->interrupted_by_resource = FALSE;
1814
1815                         if (__mmplayer_gst_create_video_sink_bin(player, caps, surface_type) != MM_ERROR_NONE) {
1816                                 LOGE("failed to create videobin. continuing without video");
1817                                 goto ERROR;
1818                         }
1819
1820                         sinkbin = player->pipeline->videobin[MMPLAYER_V_BIN].gst;
1821                         LOGD("creating videosink bin success");
1822                 } else {
1823                         reusing = TRUE;
1824                         sinkbin = player->pipeline->videobin[MMPLAYER_V_BIN].gst;
1825                         LOGD("re-using videobin");
1826                         _mmplayer_update_content_attrs(player, ATTR_VIDEO);
1827                 }
1828         } else if (strstr(name, "text")) {
1829                 if (player->pipeline->textbin == NULL) {
1830                         if (__mmplayer_gst_create_text_sink_bin(player) != MM_ERROR_NONE) {
1831                                 LOGE("failed to create text sink bin. continuing without text");
1832                                 goto ERROR;
1833                         }
1834
1835                         sinkbin = player->pipeline->textbin[MMPLAYER_T_BIN].gst;
1836                         player->textsink_linked  = 1;
1837                         LOGD("creating textsink bin success");
1838                 } else {
1839                         if (!player->textsink_linked) {
1840                                 LOGD("re-using textbin");
1841                                 reusing = TRUE;
1842                                 sinkbin = player->pipeline->textbin[MMPLAYER_T_BIN].gst;
1843                                 player->textsink_linked  = 1;
1844                         } else {
1845                                 /* linked textbin exist which means that the external subtitle path exist already */
1846                                 LOGW("ignoring internal subtitle since external subtitle is available");
1847                         }
1848                 }
1849                 sink_pad_name = "text_sink";
1850         } else {
1851                 LOGW("unknown mime type %s, ignoring it", name);
1852                 goto ERROR;
1853         }
1854
1855         if (!__mmplayer_gst_add_sinkbin_to_pipeline(player, sinkbin, pad, reusing, sink_pad_name))
1856                 goto ERROR;
1857
1858         LOGD("[handle: %p] success to create and link sink bin", player);
1859
1860         /* FIXIT : we cannot hold callback for 'no-more-pad' signal because signal was emitted in
1861          * streaming task. if the task blocked, then buffer will not flow to the next element
1862          *(autoplugging element). so this is special hack for streaming. please try to remove it
1863          */
1864         /* dec stream count. we can remove fakesink if it's zero */
1865         if (player->num_dynamic_pad)
1866                 player->num_dynamic_pad--;
1867
1868         LOGD("no more pads: %d, stream count dec : %d(num of dynamic pad)", player->no_more_pad, player->num_dynamic_pad);
1869
1870         if ((player->no_more_pad) && (player->num_dynamic_pad == 0))
1871                 _mmplayer_pipeline_complete(NULL, player);
1872
1873 ERROR:
1874
1875         MMPLAYER_FREEIF(caps_str);
1876
1877         if (caps)
1878                 gst_caps_unref(caps);
1879
1880         return;
1881 }
1882
1883 static gboolean
1884 __mmplayer_get_property_value_for_rotation(mmplayer_t *player, int display_angle, int orientation, int *value)
1885 {
1886         int required_angle = 0; /* Angle required for straight view */
1887         int rotation_angle = 0;
1888
1889         MMPLAYER_RETURN_VAL_IF_FAIL(player, FALSE);
1890         MMPLAYER_RETURN_VAL_IF_FAIL(value, FALSE);
1891
1892         /* Counter clockwise */
1893         switch (orientation) {
1894         case 0:
1895                 required_angle = 0;
1896                 break;
1897         case 90:
1898                 required_angle = 270;
1899                 break;
1900         case 180:
1901                 required_angle = 180;
1902                 break;
1903         case 270:
1904                 required_angle = 90;
1905                 break;
1906         }
1907
1908         rotation_angle = display_angle + required_angle;
1909         if (rotation_angle >= 360)
1910                 rotation_angle -= 360;
1911
1912         /* check if supported or not */
1913         if (rotation_angle % 90) {
1914                 LOGD("not supported rotation angle = %d", rotation_angle);
1915                 return FALSE;
1916         }
1917
1918         switch (rotation_angle) {
1919         case 0:
1920                 *value = MM_DISPLAY_ROTATION_NONE;
1921                 break;
1922         case 90:
1923                 *value = MM_DISPLAY_ROTATION_90;
1924                 break;
1925         case 180:
1926                 *value = MM_DISPLAY_ROTATION_180;
1927                 break;
1928         case 270:
1929                 *value = MM_DISPLAY_ROTATION_270;
1930                 break;
1931         }
1932
1933         LOGD("setting rotation property value : %d", *value);
1934
1935         return TRUE;
1936 }
1937
1938 int
1939 _mmplayer_get_video_angle(mmplayer_t *player, int *display_angle, int *orientation)
1940 {
1941         int display_rotation = 0;
1942         gchar *org_orient = NULL;
1943         MMHandleType attrs = MMPLAYER_GET_ATTRS(player);
1944
1945         if (!attrs) {
1946                 LOGE("cannot get content attribute");
1947                 return MM_ERROR_PLAYER_INTERNAL;
1948         }
1949
1950         if (display_angle) {
1951                 /* update user rotation */
1952                 mm_attrs_get_int_by_name(attrs, "display_rotation", &display_rotation);
1953
1954                 /* Counter clockwise */
1955                 switch (display_rotation) {
1956                 case MM_DISPLAY_ROTATION_NONE:
1957                         *display_angle = 0;
1958                         break;
1959                 case MM_DISPLAY_ROTATION_90:
1960                         *display_angle = 90;
1961                         break;
1962                 case MM_DISPLAY_ROTATION_180:
1963                         *display_angle = 180;
1964                         break;
1965                 case MM_DISPLAY_ROTATION_270:
1966                         *display_angle = 270;
1967                         break;
1968                 default:
1969                         LOGW("wrong angle type : %d", display_rotation);
1970                         break;
1971                 }
1972                 LOGD("check user angle: %d", *display_angle);
1973         }
1974
1975         if (orientation) {
1976                 /* Counter clockwise */
1977                 mm_attrs_get_string_by_name(attrs, "content_video_orientation", &org_orient);
1978
1979                 if (org_orient) {
1980                         if (!strcmp(org_orient, "rotate-90"))
1981                                 *orientation = 90;
1982                         else if (!strcmp(org_orient, "rotate-180"))
1983                                 *orientation = 180;
1984                         else if (!strcmp(org_orient, "rotate-270"))
1985                                 *orientation = 270;
1986                         else
1987                                 LOGD("original rotation is %s", org_orient);
1988                 } else {
1989                         LOGD("content_video_orientation get fail");
1990                 }
1991
1992                 LOGD("check orientation: %d", *orientation);
1993         }
1994
1995         return MM_ERROR_NONE;
1996 }
1997
1998 static void __mmplayer_video_param_set_display_rotation(mmplayer_t *player)
1999 {
2000         int rotation_value = 0;
2001         int orientations = 0; // current supported angle values are 0, 90, 180, 270
2002         int display_angle = 0;
2003         MMPLAYER_FENTER();
2004
2005         /* check video sinkbin is created */
2006         if (!_mmplayer_is_videosink_ready(player, MM_DISPLAY_SURFACE_OVERLAY))
2007                 return;
2008
2009         _mmplayer_get_video_angle(player, &display_angle, &orientations);
2010
2011         /* get rotation value to set */
2012         __mmplayer_get_property_value_for_rotation(player, display_angle, orientations, &rotation_value);
2013         g_object_set(player->pipeline->videobin[MMPLAYER_V_SINK].gst, "rotate", rotation_value, NULL);
2014         LOGD("set video param : rotate %d", rotation_value);
2015 }
2016
2017 static void __mmplayer_video_param_set_display_visible(mmplayer_t *player)
2018 {
2019         MMHandleType attrs = 0;
2020         int visible = 0;
2021         MMPLAYER_FENTER();
2022
2023         /* check video sinkbin is created */
2024         if (!(_mmplayer_is_videosink_ready(player, MM_DISPLAY_SURFACE_OVERLAY) ||
2025                 _mmplayer_is_videosink_ready(player, MM_DISPLAY_SURFACE_OVERLAY_SYNC_UI)))
2026                 return;
2027
2028         attrs = MMPLAYER_GET_ATTRS(player);
2029         MMPLAYER_RETURN_IF_FAIL(attrs);
2030
2031         mm_attrs_get_int_by_name(attrs, "display_visible", &visible);
2032         g_object_set(player->pipeline->videobin[MMPLAYER_V_SINK].gst, "visible", visible, NULL);
2033         LOGD("set video param : visible %d", visible);
2034 }
2035
2036 static void __mmplayer_video_param_set_display_method(mmplayer_t *player)
2037 {
2038         MMHandleType attrs = 0;
2039         int display_method = 0;
2040         MMPLAYER_FENTER();
2041
2042         /* check video sinkbin is created */
2043         if (!_mmplayer_is_videosink_ready(player, MM_DISPLAY_SURFACE_OVERLAY))
2044                 return;
2045
2046         attrs = MMPLAYER_GET_ATTRS(player);
2047         MMPLAYER_RETURN_IF_FAIL(attrs);
2048
2049         mm_attrs_get_int_by_name(attrs, "display_method", &display_method);
2050         g_object_set(player->pipeline->videobin[MMPLAYER_V_SINK].gst, "display-geometry-method", display_method, NULL);
2051         LOGD("set video param : method %d", display_method);
2052 }
2053
2054 static void __mmplayer_video_param_set_video_roi_area(mmplayer_t *player)
2055 {
2056         MMHandleType attrs = 0;
2057         int handle = 0;
2058         MMPLAYER_FENTER();
2059
2060         /* check video sinkbin is created */
2061         if (!_mmplayer_is_videosink_ready(player, MM_DISPLAY_SURFACE_OVERLAY))
2062                 return;
2063
2064         attrs = MMPLAYER_GET_ATTRS(player);
2065         MMPLAYER_RETURN_IF_FAIL(attrs);
2066
2067         mm_attrs_get_int_by_name(attrs, "display_overlay", &handle);
2068         MMPLAYER_RETURN_IF_FAIL(handle);
2069
2070         gst_video_overlay_set_video_roi_area(
2071                  GST_VIDEO_OVERLAY(player->pipeline->videobin[MMPLAYER_V_SINK].gst),
2072                  player->video_roi.scale_x, player->video_roi.scale_y, player->video_roi.scale_width, player->video_roi.scale_height);
2073         LOGD("set video param : video roi area scale value: x(%f) y(%f) width(%f) height(%f)",
2074                 player->video_roi.scale_x, player->video_roi.scale_y, player->video_roi.scale_width, player->video_roi.scale_height);
2075 }
2076
2077 static void __mmplayer_video_param_set_roi_area(mmplayer_t *player)
2078 {
2079         MMHandleType attrs = 0;
2080         int handle = 0;
2081         /*set wl_display*/
2082         int win_roi_x = 0;
2083         int win_roi_y = 0;
2084         int win_roi_width = 0;
2085         int win_roi_height = 0;
2086         MMPLAYER_FENTER();
2087
2088         /* check video sinkbin is created */
2089         if (!_mmplayer_is_videosink_ready(player, MM_DISPLAY_SURFACE_OVERLAY))
2090                 return;
2091
2092         attrs = MMPLAYER_GET_ATTRS(player);
2093         MMPLAYER_RETURN_IF_FAIL(attrs);
2094
2095         mm_attrs_get_int_by_name(attrs, "display_overlay", &handle);
2096         MMPLAYER_RETURN_IF_FAIL(handle);
2097
2098         /* It should be set after setting window */
2099         mm_attrs_multiple_get(attrs, NULL,
2100                                 "display_win_roi_x", &win_roi_x,
2101                                 "display_win_roi_y", &win_roi_y,
2102                                 "display_win_roi_width", &win_roi_width,
2103                                 "display_win_roi_height", &win_roi_height, NULL);
2104
2105         /* After setting window handle, set display roi area */
2106         gst_video_overlay_set_display_roi_area(
2107                  GST_VIDEO_OVERLAY(player->pipeline->videobin[MMPLAYER_V_SINK].gst),
2108                  win_roi_x, win_roi_y, win_roi_width, win_roi_height);
2109         LOGD("set video param : roi area : x(%d) y(%d) width(%d) height(%d)",
2110                 win_roi_x, win_roi_y, win_roi_width, win_roi_height);
2111 }
2112
2113 static void __mmplayer_video_param_set_display_overlay_sync_ui(mmplayer_t *player)
2114 {
2115         MMHandleType attrs = 0;
2116         gchar *handle = NULL;
2117
2118         /* check video sinkbin is created */
2119         if (!_mmplayer_is_videosink_ready(player, MM_DISPLAY_SURFACE_OVERLAY_SYNC_UI))
2120                 return;
2121
2122         attrs = MMPLAYER_GET_ATTRS(player);
2123         MMPLAYER_RETURN_IF_FAIL(attrs);
2124
2125         /* common case if using overlay surface */
2126         mm_attrs_get_string_by_name(attrs, "exported_shell_handle", &handle);
2127         MMPLAYER_RETURN_IF_FAIL(handle);
2128
2129         gst_video_overlay_set_wl_window_exported_shell_handle(
2130                         GST_VIDEO_OVERLAY(player->pipeline->videobin[MMPLAYER_V_SINK].gst),
2131                         handle);
2132         LOGD("set video param: exported_shell_handle (%s)", handle);
2133 }
2134
2135 static void __mmplayer_video_param_set_display_overlay(mmplayer_t *player)
2136 {
2137         MMHandleType attrs = 0;
2138         int handle = 0;
2139
2140         /* check video sinkbin is created */
2141         if (!_mmplayer_is_videosink_ready(player, MM_DISPLAY_SURFACE_OVERLAY))
2142                 return;
2143
2144         attrs = MMPLAYER_GET_ATTRS(player);
2145         MMPLAYER_RETURN_IF_FAIL(attrs);
2146
2147         /* common case if using overlay surface */
2148         mm_attrs_get_int_by_name(attrs, "display_overlay", &handle);
2149         MMPLAYER_RETURN_IF_FAIL(handle);
2150
2151         /* default is using wl_surface_id */
2152         LOGD("set video param : wl_surface_id %d", handle);
2153         gst_video_overlay_set_wl_window_wl_surface_id(
2154                         GST_VIDEO_OVERLAY(player->pipeline->videobin[MMPLAYER_V_SINK].gst),
2155                         handle);
2156 }
2157
2158 int
2159 _mmplayer_update_video_overlay_param(mmplayer_t *player, const char *param_name)
2160 {
2161         gboolean update_all_param = FALSE;
2162         int curr_type = MM_DISPLAY_SURFACE_NUM;
2163
2164         MMPLAYER_FENTER();
2165
2166         if (!player || !player->pipeline || !player->pipeline->mainbin || !player->pipeline->videobin ||
2167                 !player->pipeline->videobin[MMPLAYER_V_BIN].gst ||
2168                 !player->pipeline->videobin[MMPLAYER_V_SINK].gst) {
2169                 LOGW("videosink is not ready yet");
2170                 return MM_ERROR_PLAYER_NOT_INITIALIZED;
2171         }
2172
2173         mm_attrs_get_int_by_name(player->attrs, "display_surface_type", &curr_type);
2174
2175         if (curr_type != MM_DISPLAY_SURFACE_OVERLAY && curr_type != MM_DISPLAY_SURFACE_OVERLAY_SYNC_UI) {
2176                 LOGE("current type(%d) is wrong", curr_type);
2177                 return MM_ERROR_PLAYER_INTERNAL;
2178         }
2179
2180         if (strcmp(player->ini.videosink_element_overlay, "tizenwlsink")) {
2181                 LOGE("invalid videosink [%s]", player->ini.videosink_element_overlay);
2182                 return MM_ERROR_PLAYER_INTERNAL;
2183         }
2184
2185         LOGD("param_name : %s", param_name);
2186         if (!g_strcmp0(param_name, "update_all_param"))
2187                 update_all_param = TRUE;
2188
2189         if (curr_type == MM_DISPLAY_SURFACE_OVERLAY_SYNC_UI) {
2190                 __mmplayer_video_param_set_display_overlay_sync_ui(player);
2191                 MMPLAYER_FLEAVE();
2192                 return MM_ERROR_NONE;
2193         }
2194         if (update_all_param || !g_strcmp0(param_name, "display_overlay"))
2195                 __mmplayer_video_param_set_display_overlay(player);
2196         if (update_all_param || !g_strcmp0(param_name, "display_method"))
2197                 __mmplayer_video_param_set_display_method(player);
2198         if (update_all_param || !g_strcmp0(param_name, "display_visible"))
2199                 __mmplayer_video_param_set_display_visible(player);
2200         if (update_all_param || !g_strcmp0(param_name, "display_rotation"))
2201                 __mmplayer_video_param_set_display_rotation(player);
2202         if (update_all_param || !g_strcmp0(param_name, "display_win_roi_x"))
2203                 __mmplayer_video_param_set_roi_area(player);
2204         if (update_all_param)
2205                 __mmplayer_video_param_set_video_roi_area(player);
2206
2207         MMPLAYER_FLEAVE();
2208         return MM_ERROR_NONE;
2209 }
2210
2211 static int __mmplayer_set_disable_overlay_option(mmplayer_t *player, bool disable)
2212 {
2213         gboolean disable_overlay = FALSE;
2214
2215         MMPLAYER_FENTER();
2216         MMPLAYER_RETURN_VAL_IF_FAIL(player->pipeline->videobin &&
2217                                                                 player->pipeline->videobin[MMPLAYER_V_SINK].gst,
2218                                                                 MM_ERROR_PLAYER_NO_OP); /* invalid op */
2219
2220         if (!g_object_class_find_property(G_OBJECT_GET_CLASS(player->pipeline->videobin[MMPLAYER_V_SINK].gst), "disable-overlay")) {
2221                 LOGW("Display control is not supported");
2222                 return MM_ERROR_PLAYER_INTERNAL;
2223         }
2224
2225         g_object_get(player->pipeline->videobin[MMPLAYER_V_SINK].gst, "disable-overlay", &disable_overlay, NULL);
2226
2227         if (disable == (bool)disable_overlay) {
2228                 LOGE("It's the same with current setting: (%d)", disable);
2229                 return MM_ERROR_NONE;
2230         }
2231
2232         if (disable) {
2233                 LOGE("disable overlay");
2234                 g_object_set(player->pipeline->videobin[MMPLAYER_V_SINK].gst, "disable-overlay", TRUE, NULL);
2235
2236                 /* release overlay resource */
2237                 if (__mmplayer_release_hw_resource(player, MMPLAYER_RESOURCE_TYPE_VIDEO_OVERLAY) != MM_ERROR_NONE) {
2238                         LOGE("failed to release overlay resource");
2239                         return MM_ERROR_PLAYER_INTERNAL;
2240                 }
2241         } else {
2242                 if (_mmplayer_acquire_hw_resource(player, MMPLAYER_RESOURCE_TYPE_VIDEO_OVERLAY) != MM_ERROR_NONE) {
2243                         LOGE("failed to acquire video overlay resource");
2244                         return MM_ERROR_PLAYER_INTERNAL;
2245                 }
2246                 player->interrupted_by_resource = FALSE;
2247
2248                 LOGD("enable overlay");
2249                 __mmplayer_video_param_set_display_overlay(player);
2250                 __mmplayer_video_param_set_display_overlay_sync_ui(player);
2251                 g_object_set(player->pipeline->videobin[MMPLAYER_V_SINK].gst, "disable-overlay", FALSE, NULL);
2252         }
2253
2254         MMPLAYER_FLEAVE();
2255         return MM_ERROR_NONE;
2256 }
2257
2258 int
2259 _mmplayer_set_audio_only(MMHandleType hplayer, bool audio_only)
2260 {
2261         int ret = MM_ERROR_NONE;
2262         mmplayer_t *player = (mmplayer_t *)hplayer;
2263
2264         MMPLAYER_FENTER();
2265         MMPLAYER_RETURN_VAL_IF_FAIL(player && player->pipeline, MM_ERROR_PLAYER_NOT_INITIALIZED);
2266
2267         if (MMPLAYER_USE_DECODEBIN(player)) {
2268                 ret = __mmplayer_set_disable_overlay_option(player, audio_only);
2269                 goto EXIT;
2270         }
2271
2272         if (audio_only) {
2273                 MMPLAYER_RETURN_VAL_IF_FAIL(player->pipeline->videobin &&
2274                                                                         player->pipeline->videobin[MMPLAYER_V_SINK].gst,
2275                                                                         MM_ERROR_PLAYER_NO_OP); /* invalid op */
2276
2277                 __mmplayer_del_sink(player, player->pipeline->videobin[MMPLAYER_V_SINK].gst);
2278
2279                 __mmplayer_switch_stream(player, MM_PLAYER_TRACK_TYPE_VIDEO, INVALID_TRACK_INDEX);
2280
2281                 /* release decoder resource */
2282                 if (__mmplayer_release_hw_resource(player, MMPLAYER_RESOURCE_TYPE_VIDEO_DECODER) != MM_ERROR_NONE) {
2283                         LOGE("failed to release video decoder resources");
2284                         return MM_ERROR_PLAYER_INTERNAL;
2285                 }
2286                 player->can_support_codec &= ~FOUND_PLUGIN_VIDEO;
2287         } else {
2288                 __mmplayer_switch_stream(player, MM_PLAYER_TRACK_TYPE_VIDEO, DEFAULT_TRACK_INDEX);
2289         }
2290
2291 EXIT:
2292         mm_player_set_attribute(hplayer, NULL, MM_PLAYER_AUDIO_ONLY, (int)audio_only, (char *)NULL);
2293
2294         MMPLAYER_FLEAVE();
2295         return ret;
2296 }
2297
2298 int
2299 _mmplayer_gst_element_link_bucket(GList *element_bucket)
2300 {
2301         GList *bucket = element_bucket;
2302         mmplayer_gst_element_t *element = NULL;
2303         mmplayer_gst_element_t *prv_element = NULL;
2304         GstElement *tee_element = NULL;
2305         gint successful_link_count = 0;
2306
2307         MMPLAYER_FENTER();
2308
2309         MMPLAYER_RETURN_VAL_IF_FAIL(element_bucket, -1);
2310
2311         prv_element = (mmplayer_gst_element_t *)bucket->data;
2312         bucket = bucket->next;
2313
2314         for (; bucket; bucket = bucket->next) {
2315                 element = (mmplayer_gst_element_t *)bucket->data;
2316
2317                 if (element && element->gst) {
2318                         if (prv_element && prv_element->gst) {
2319                                 if (strstr(GST_ELEMENT_NAME(element->gst), "audio-tee-queue") && strcmp(GST_ELEMENT_NAME(prv_element->gst), "audio-tee")) {
2320                                         if (tee_element) {
2321                                                 prv_element->gst = tee_element;
2322                                         } else {
2323                                                 LOGD("failed to make new audio branch - linking [%s] to [%s] is not supported",
2324                                                         GST_ELEMENT_NAME(GST_ELEMENT(prv_element->gst)),
2325                                                         GST_ELEMENT_NAME(GST_ELEMENT(element->gst)));
2326                                                 return -1;
2327                                         }
2328                                 }
2329                                 if (gst_element_link(GST_ELEMENT(prv_element->gst), GST_ELEMENT(element->gst))) {
2330                                         LOGD("linking [%s] to [%s] success",
2331                                                 GST_ELEMENT_NAME(GST_ELEMENT(prv_element->gst)),
2332                                                 GST_ELEMENT_NAME(GST_ELEMENT(element->gst)));
2333                                         successful_link_count++;
2334                                         if (!strcmp(GST_ELEMENT_NAME(prv_element->gst), "audio-tee")) {
2335                                                 LOGD("keep audio-tee element for next audio pipeline branch");
2336                                                 tee_element = prv_element->gst;
2337                                         }
2338                                 } else {
2339                                         LOGD("linking [%s] to [%s] failed",
2340                                                 GST_ELEMENT_NAME(GST_ELEMENT(prv_element->gst)),
2341                                                 GST_ELEMENT_NAME(GST_ELEMENT(element->gst)));
2342                                         return -1;
2343                                 }
2344                         }
2345                 }
2346
2347                 prv_element = element;
2348         }
2349
2350         MMPLAYER_FLEAVE();
2351
2352         return successful_link_count;
2353 }
2354
2355 int
2356 _mmplayer_gst_element_add_bucket_to_bin(GstBin *bin, GList *element_bucket)
2357 {
2358         GList *bucket = element_bucket;
2359         mmplayer_gst_element_t *element = NULL;
2360         int successful_add_count = 0;
2361
2362         MMPLAYER_FENTER();
2363
2364         MMPLAYER_RETURN_VAL_IF_FAIL(element_bucket, 0);
2365         MMPLAYER_RETURN_VAL_IF_FAIL(bin, 0);
2366
2367         for (; bucket; bucket = bucket->next) {
2368                 element = (mmplayer_gst_element_t *)bucket->data;
2369
2370                 if (element && element->gst) {
2371                         if (!gst_bin_add(bin, GST_ELEMENT(element->gst))) {
2372                                 LOGD("_mmplayer_gst_element_link_bucket : Adding element [%s]  to bin [%s] failed",
2373                                         GST_ELEMENT_NAME(GST_ELEMENT(element->gst)),
2374                                         GST_ELEMENT_NAME(GST_ELEMENT(bin)));
2375                                 return 0;
2376                         }
2377                         successful_add_count++;
2378                 }
2379         }
2380
2381         MMPLAYER_FLEAVE();
2382
2383         return successful_add_count;
2384 }
2385
2386 static void
2387 __mmplayer_gst_caps_notify_cb(GstPad *pad, GParamSpec *unused, gpointer data)
2388 {
2389         mmplayer_t *player = (mmplayer_t *)data;
2390         GstCaps *caps = NULL;
2391         GstStructure *str = NULL;
2392         const char *name;
2393         gboolean caps_ret = TRUE;
2394
2395         MMPLAYER_FENTER();
2396
2397         MMPLAYER_RETURN_IF_FAIL(pad);
2398         MMPLAYER_RETURN_IF_FAIL(unused);
2399         MMPLAYER_RETURN_IF_FAIL(data);
2400
2401         MMPLAYER_GST_GET_CAPS_INFO_FROM_PAD(pad, caps, str, name, caps_ret);
2402         if (!caps_ret)
2403                 goto ERROR;
2404
2405         LOGD("name = %s", name);
2406
2407         if (strstr(name, "audio")) {
2408                 _mmplayer_update_content_attrs(player, ATTR_AUDIO);
2409
2410                 if (player->audio_stream_changed_cb) {
2411                         LOGE("call the audio stream changed cb");
2412                         player->audio_stream_changed_cb(player->audio_stream_changed_cb_user_param);
2413                 }
2414         } else if (strstr(name, "video")) {
2415                 if ((name = gst_structure_get_string(str, "format")))
2416                         player->set_mode.video_zc = name[0] == 'S';
2417
2418                 _mmplayer_update_content_attrs(player, ATTR_VIDEO);
2419                 MMPLAYER_POST_MSG(player, MM_MESSAGE_VIDEO_STREAM_CHANGED, NULL);
2420         } else {
2421                 LOGW("invalid caps info");
2422         }
2423
2424 ERROR:
2425         if (caps)
2426                 gst_caps_unref(caps);
2427
2428         MMPLAYER_FLEAVE();
2429
2430         return;
2431 }
2432
2433 void
2434 _mmplayer_audio_stream_clear_buffer(mmplayer_t *player, gboolean send_all)
2435 {
2436         GList *l = NULL;
2437
2438         MMPLAYER_FENTER();
2439         MMPLAYER_RETURN_IF_FAIL(player);
2440
2441         if (player->audio_stream_buff_list) {
2442                 for (l = g_list_first(player->audio_stream_buff_list); l; l = g_list_next(l)) {
2443                         mmplayer_audio_stream_buff_t *tmp = (mmplayer_audio_stream_buff_t *)l->data;
2444                         if (tmp) {
2445                                 if (send_all) {
2446                                         LOGD("[%"G_GUINT64_FORMAT"] send remained data.", tmp->channel_mask);
2447                                         __mmplayer_audio_stream_send_data(player, tmp);
2448                                 }
2449                                 MMPLAYER_FREEIF(tmp->pcm_data);
2450                                 MMPLAYER_FREEIF(tmp);
2451                         }
2452                 }
2453                 g_list_free(player->audio_stream_buff_list);
2454                 player->audio_stream_buff_list = NULL;
2455         }
2456
2457         MMPLAYER_FLEAVE();
2458 }
2459
2460 static void
2461 __mmplayer_audio_stream_send_data(mmplayer_t *player, mmplayer_audio_stream_buff_t *a_buffer)
2462 {
2463         mmplayer_audio_decoded_data_info_t audio_stream = { 0, };
2464
2465         MMPLAYER_FENTER();
2466         MMPLAYER_RETURN_IF_FAIL(player && player->audio_decoded_cb);
2467
2468         audio_stream.bitrate = a_buffer->bitrate;
2469         audio_stream.channel = a_buffer->channel;
2470         audio_stream.channel_mask = a_buffer->channel_mask;
2471         audio_stream.data_size = a_buffer->data_size;
2472         audio_stream.data = a_buffer->pcm_data;
2473         audio_stream.pcm_format = a_buffer->pcm_format;
2474 #ifdef __DEBUG__
2475         LOGD("[%"G_GUINT64_FORMAT"] send data size:%d, %p", audio_stream.channel_mask, audio_stream.data_size, player->audio_decoded_cb_user_param);
2476 #endif
2477         player->audio_decoded_cb(&audio_stream, player->audio_decoded_cb_user_param);
2478
2479         MMPLAYER_FLEAVE();
2480 }
2481
2482 static void
2483 __mmplayer_audio_stream_decoded_render_cb(GstElement *object, GstBuffer *buffer, GstPad *pad, gpointer data)
2484 {
2485         mmplayer_t *player = (mmplayer_t *)data;
2486         const gchar *pcm_format = NULL;
2487         gint channel = 0;
2488         gint rate = 0;
2489         guint64 channel_mask = 0;
2490         void *a_data = NULL;
2491         gint a_size = 0;
2492         mmplayer_audio_stream_buff_t *a_buffer = NULL;
2493         GstMapInfo mapinfo = GST_MAP_INFO_INIT;
2494         GList *l = NULL;
2495
2496         MMPLAYER_FENTER();
2497         MMPLAYER_RETURN_IF_FAIL(player && player->audio_decoded_cb);
2498
2499         gst_buffer_map(buffer, &mapinfo, GST_MAP_READ);
2500         a_data = mapinfo.data;
2501         a_size = mapinfo.size;
2502
2503         GstCaps *caps = gst_pad_get_current_caps(pad);
2504         GstStructure *structure = gst_caps_get_structure(caps, 0);
2505 #ifdef __DEBUG__
2506         MMPLAYER_LOG_GST_CAPS_TYPE(caps);
2507 #endif
2508         pcm_format = gst_structure_get_string(structure, "format");
2509         gst_structure_get_int(structure, "rate", &rate);
2510         gst_structure_get_int(structure, "channels", &channel);
2511         gst_structure_get(structure, "channel-mask", GST_TYPE_BITMASK, &channel_mask, NULL);
2512         gst_caps_unref(GST_CAPS(caps));
2513
2514         /* In case of the sync is false, use buffer list.              *
2515          * The num of buffer list depends on the num of audio channels */
2516         if (player->audio_stream_buff_list) {
2517                 for (l = g_list_first(player->audio_stream_buff_list); l; l = g_list_next(l)) {
2518                         mmplayer_audio_stream_buff_t *tmp = (mmplayer_audio_stream_buff_t *)l->data;
2519                         if (tmp) {
2520                                 if (channel_mask == tmp->channel_mask) {
2521 #ifdef __DEBUG__
2522                                         LOGD("[%"G_GUINT64_FORMAT"] total: %d, data: %d, buffer: %d", channel_mask, tmp->data_size, a_size, tmp->buff_size);
2523 #endif
2524                                         if (tmp->data_size + a_size < tmp->buff_size) {
2525                                                 memcpy(tmp->pcm_data + tmp->data_size, a_data, a_size);
2526                                                 tmp->data_size += a_size;
2527                                         } else {
2528                                                 /* send data to client */
2529                                                 __mmplayer_audio_stream_send_data(player, tmp);
2530
2531                                                 if (a_size > tmp->buff_size) {
2532                                                         LOGD("[%"G_GUINT64_FORMAT"] adj buffer size %d -> %d", channel_mask, tmp->buff_size, a_size);
2533                                                         tmp->pcm_data = g_realloc(tmp->pcm_data, a_size);
2534                                                         if (tmp->pcm_data == NULL) {
2535                                                                 LOGE("failed to realloc data.");
2536                                                                 goto DONE;
2537                                                         }
2538                                                         tmp->buff_size = a_size;
2539                                                 }
2540                                                 memset(tmp->pcm_data, 0x00, tmp->buff_size);
2541                                                 memcpy(tmp->pcm_data, a_data, a_size);
2542                                                 tmp->data_size = a_size;
2543                                         }
2544                                         goto DONE;
2545                                 }
2546                         } else {
2547                                 LOGE("data is empty in list.");
2548                                 goto DONE;
2549                         }
2550                 }
2551         }
2552
2553         /* create new audio stream data for newly found audio channel */
2554         a_buffer = (mmplayer_audio_stream_buff_t *)g_try_malloc0(sizeof(mmplayer_audio_stream_buff_t));
2555         if (a_buffer == NULL) {
2556                 LOGE("failed to alloc data.");
2557                 goto DONE;
2558         }
2559         a_buffer->bitrate = rate;
2560         a_buffer->channel = channel;
2561         a_buffer->channel_mask = channel_mask;
2562         a_buffer->data_size = a_size;
2563         a_buffer->pcm_format = _mmplayer_convert_audio_pcm_str_to_media_format_mime(pcm_format);
2564
2565         if (player->audio_extract_opt & MM_PLAYER_AUDIO_EXTRACT_NO_SYNC_WITH_CLOCK) {
2566                 /* If sync is FALSE, use buffer list to reduce the IPC. */
2567                 a_buffer->buff_size = (a_size > player->ini.pcm_buffer_size) ? (a_size) : (player->ini.pcm_buffer_size);
2568                 a_buffer->pcm_data = g_try_malloc(a_buffer->buff_size);
2569                 if (a_buffer->pcm_data == NULL) {
2570                         LOGE("failed to alloc data.");
2571                         MMPLAYER_FREEIF(a_buffer);
2572                         goto DONE;
2573                 }
2574                 memcpy(a_buffer->pcm_data, a_data, a_size);
2575 #ifdef __DEBUG__
2576                 LOGD("new [%"G_GUINT64_FORMAT"] total:%d buff:%d", channel_mask, a_buffer->data_size, a_buffer->buff_size);
2577 #endif
2578                 player->audio_stream_buff_list = g_list_append(player->audio_stream_buff_list, a_buffer);
2579         } else {
2580                 /* If sync is TRUE, send data directly. */
2581                 a_buffer->pcm_data = a_data;
2582                 __mmplayer_audio_stream_send_data(player, a_buffer);
2583                 MMPLAYER_FREEIF(a_buffer);
2584         }
2585
2586 DONE:
2587         gst_buffer_unmap(buffer, &mapinfo);
2588         MMPLAYER_FLEAVE();
2589 }
2590
2591 static void
2592 __mmplayer_gst_audio_deinterleave_pad_added(GstElement *elem, GstPad *pad, gpointer data)
2593 {
2594         mmplayer_t *player = (mmplayer_t *)data;
2595         mmplayer_gst_element_t *audiobin = player->pipeline->audiobin;
2596         GstPad *sinkpad = NULL;
2597         GstElement *queue = NULL, *sink = NULL;
2598
2599         MMPLAYER_FENTER();
2600         MMPLAYER_RETURN_IF_FAIL(player && player->pipeline && player->pipeline->mainbin);
2601
2602         queue = gst_element_factory_make("queue", NULL);
2603         if (queue == NULL) {
2604                 LOGD("fail make queue");
2605                 goto ERROR;
2606         }
2607
2608         sink = gst_element_factory_make("fakesink", NULL);
2609         if (sink == NULL) {
2610                 LOGD("fail make fakesink");
2611                 goto ERROR;
2612         }
2613
2614         gst_bin_add_many(GST_BIN(audiobin[MMPLAYER_A_BIN].gst), queue, sink, NULL);
2615
2616         if (!gst_element_link_pads_full(queue, "src", sink, "sink", GST_PAD_LINK_CHECK_NOTHING)) {
2617                 LOGW("failed to link queue & sink");
2618                 goto ERROR;
2619         }
2620
2621         sinkpad = gst_element_get_static_pad(queue, "sink");
2622
2623         if (GST_PAD_LINK_OK != gst_pad_link(pad, sinkpad)) {
2624                 LOGW("failed to link [%s:%s] to queue", GST_DEBUG_PAD_NAME(pad));
2625                 goto ERROR;
2626         }
2627
2628         LOGE("audio_extract_opt : 0x%X", player->audio_extract_opt);
2629
2630         gst_object_unref(sinkpad);
2631         if (!(player->audio_extract_opt & MM_PLAYER_AUDIO_EXTRACT_NO_SYNC_WITH_CLOCK))
2632                 g_object_set(sink, "sync", TRUE, NULL);
2633         g_object_set(sink, "signal-handoffs", TRUE, NULL);
2634
2635         /* keep the first sink reference only */
2636         if (!audiobin[MMPLAYER_A_SINK].gst) {
2637                 audiobin[MMPLAYER_A_SINK].id = MMPLAYER_A_SINK;
2638                 audiobin[MMPLAYER_A_SINK].gst = sink;
2639         }
2640
2641
2642         _mmplayer_add_signal_connection(player,
2643                 G_OBJECT(sink),
2644                 MM_PLAYER_SIGNAL_TYPE_AUDIOBIN,
2645                 "handoff",
2646                 G_CALLBACK(__mmplayer_audio_stream_decoded_render_cb),
2647                 (gpointer)player);
2648
2649         __mmplayer_add_sink(player, sink, FALSE);
2650
2651         if (gst_element_sync_state_with_parent(queue) == GST_STATE_CHANGE_FAILURE) {
2652                 LOGE("failed to sync state");
2653                 goto ERROR;
2654         }
2655
2656         if (gst_element_sync_state_with_parent(sink) == GST_STATE_CHANGE_FAILURE) {
2657                 LOGE("failed to sync state");
2658                 goto ERROR;
2659         }
2660
2661         MMPLAYER_FLEAVE();
2662         return;
2663
2664 ERROR:
2665         LOGE("__mmplayer_gst_audio_deinterleave_pad_added ERROR");
2666         if (queue) {
2667                 gst_object_unref(GST_OBJECT(queue));
2668                 queue = NULL;
2669         }
2670         if (sink) {
2671                 gst_object_unref(GST_OBJECT(sink));
2672                 sink = NULL;
2673         }
2674         if (sinkpad) {
2675                 gst_object_unref(GST_OBJECT(sinkpad));
2676                 sinkpad = NULL;
2677         }
2678
2679         return;
2680 }
2681
2682 void
2683 __mmplayer_gst_audio_deinterleave_no_more_pads(GstElement* object, gpointer data)
2684 {
2685         mmplayer_t *player = (mmplayer_t *)data;
2686
2687         MMPLAYER_FENTER();
2688         MMPLAYER_RETURN_IF_FAIL(player && player->pipeline && player->pipeline->mainbin);
2689
2690         player->no_more_pad = TRUE;
2691         _mmplayer_pipeline_complete(NULL, player);
2692
2693         MMPLAYER_FLEAVE();
2694         return;
2695 }
2696
2697 void
2698 __mmplayer_gst_set_pulsesink_property(mmplayer_t *player)
2699 {
2700         #define MAX_PROPS_LEN 128
2701         mmplayer_gst_element_t *audiobin = NULL;
2702         gint latency_mode = 0;
2703         gchar *stream_type = NULL;
2704         gchar *latency = NULL;
2705         gint stream_id = 0;
2706         gchar stream_props[MAX_PROPS_LEN] = {0,};
2707         GstStructure *props = NULL;
2708
2709         /* set volume table
2710          * It should be set after player creation through attribute.
2711          * But, it can not be changed during playing.
2712          */
2713         MMPLAYER_FENTER();
2714         MMPLAYER_RETURN_IF_FAIL(player && player->pipeline && player->pipeline->audiobin);
2715
2716         audiobin = player->pipeline->audiobin;
2717
2718         g_object_set(G_OBJECT(audiobin[MMPLAYER_A_SINK].gst), "volume", player->sound.volume, NULL);
2719         if (player->sound.mute) {
2720                 LOGD("mute enabled");
2721                 g_object_set(G_OBJECT(audiobin[MMPLAYER_A_SINK].gst), "mute", player->sound.mute, NULL);
2722         }
2723
2724         mm_attrs_get_int_by_name(player->attrs, "sound_stream_index", &stream_id);
2725         mm_attrs_get_string_by_name(player->attrs, "sound_stream_type", &stream_type);
2726
2727         if (!stream_type)
2728                 snprintf(stream_props, sizeof(stream_props) - 1,
2729                                 "props,application.process.id.origin=%d", player->client_pid);
2730         else
2731                 snprintf(stream_props, sizeof(stream_props) - 1,
2732                                 "props,media.role=%s, media.parent_id=%d, application.process.id.origin=%d",
2733                                 stream_type, stream_id, player->client_pid);
2734
2735         props = gst_structure_from_string(stream_props, NULL);
2736         g_object_set(audiobin[MMPLAYER_A_SINK].gst, "stream-properties", props, NULL);
2737         LOGI("props result[%s].", stream_props);
2738         gst_structure_free(props);
2739
2740         mm_attrs_get_int_by_name(player->attrs, "sound_latency_mode", &latency_mode);
2741
2742         switch (latency_mode) {
2743         case AUDIO_LATENCY_MODE_LOW:
2744                 latency = g_strdup("low");
2745                 break;
2746         case AUDIO_LATENCY_MODE_MID:
2747                 latency = g_strdup("mid");
2748                 break;
2749         case AUDIO_LATENCY_MODE_HIGH:
2750                 latency = g_strdup("high");
2751                 break;
2752         default:
2753                 latency = g_strdup("mid");
2754                 break;
2755         };
2756
2757         g_object_set(audiobin[MMPLAYER_A_SINK].gst, "latency", latency, NULL);
2758
2759         LOGD("audiosink property - latency=%s", latency);
2760
2761         MMPLAYER_FREEIF(latency);
2762
2763         MMPLAYER_FLEAVE();
2764 }
2765
2766 int
2767 __mmplayer_gst_set_openalsink_property(mmplayer_t *player)
2768 {
2769         mmplayer_gst_element_t *audiobin = NULL;
2770
2771         MMPLAYER_FENTER();
2772         MMPLAYER_RETURN_VAL_IF_FAIL(player && player->pipeline &&
2773                                 player->pipeline->audiobin, MM_ERROR_PLAYER_NOT_INITIALIZED);
2774
2775         audiobin = player->pipeline->audiobin;
2776
2777         g_object_set(G_OBJECT(audiobin[MMPLAYER_A_SINK].gst), "source-ambisonics-type", 1, NULL);
2778         if (sound_manager_create_stream_information(SOUND_STREAM_TYPE_MEDIA, NULL, NULL, &stream_info)) {
2779                 LOGE("failed to create media stream info");
2780                 return MM_ERROR_PLAYER_INTERNAL;
2781         }
2782
2783         g_object_set(G_OBJECT(audiobin[MMPLAYER_A_SINK].gst), "stream-info", stream_info, NULL);
2784
2785         if (player->video360_yaw_radians <= M_PI &&
2786                         player->video360_yaw_radians >= -M_PI &&
2787                         player->video360_pitch_radians <= M_PI_2 &&
2788                         player->video360_pitch_radians >= -M_PI_2) {
2789                 g_object_set(G_OBJECT(audiobin[MMPLAYER_A_SINK].gst),
2790                                 "source-orientation-y", (int)(player->video360_yaw_radians * 180.0 / M_PI),
2791                                 "source-orientation-x", (int)(player->video360_pitch_radians * 180.0 / M_PI), NULL);
2792         } else if (player->video360_metadata.init_view_heading || player->video360_metadata.init_view_pitch) {
2793                 g_object_set(G_OBJECT(audiobin[MMPLAYER_A_SINK].gst),
2794                                 "source-orientation-y", player->video360_metadata.init_view_heading,
2795                                 "source-orientation-x", player->video360_metadata.init_view_pitch, NULL);
2796         }
2797
2798         MMPLAYER_FLEAVE();
2799         return MM_ERROR_NONE;
2800 }
2801
2802 static int
2803 __mmplayer_gst_make_audio_playback_sink(mmplayer_t *player, GList **bucket)
2804 {
2805         mmplayer_gst_element_t *audiobin = NULL;
2806         GstPad *sink_pad = NULL;
2807         GstCaps *acaps = NULL;
2808         gint channels = 0;
2809         int pitch_control = 0;
2810         double pitch_value = 1.0;
2811
2812         MMPLAYER_FENTER();
2813         MMPLAYER_RETURN_VAL_IF_FAIL(player && player->pipeline &&
2814                                 player->pipeline->audiobin, MM_ERROR_PLAYER_NOT_INITIALIZED);
2815
2816         audiobin = player->pipeline->audiobin;
2817
2818         LOGD("make element for normal audio playback");
2819
2820         /* audio bin structure for playback. {} means optional.
2821            optional : pitch, audioeq, custom audioeq, openalsink for 360 audio content
2822
2823          * src - ... - {aconv - pitch} - aconv - rgvolume - resample - volume -
2824                         {audioeq} - {custom audioeq} - pulsesink or {aconv - capsfilter - openalsink}
2825          */
2826
2827         /* for pitch control */
2828         mm_attrs_multiple_get(player->attrs, NULL,
2829                                 MM_PLAYER_PITCH_CONTROL, &pitch_control,
2830                                 MM_PLAYER_PITCH_VALUE, &pitch_value,
2831                                 NULL);
2832
2833         LOGD("pitch %d / %1.3f", pitch_control, pitch_value);
2834         if (pitch_control && (player->videodec_linked == 0)) {
2835                 GstElementFactory *factory;
2836
2837                 factory = gst_element_factory_find("pitch");
2838                 if (factory) {
2839                         gst_object_unref(factory);
2840
2841                         /* converter */
2842                         MMPLAYER_CREATE_ELEMENT(audiobin, MMPLAYER_A_CONV_PITCH, "audioconvert", "audio convert pitch", *bucket, player);
2843
2844                         /* pitch */
2845                         MMPLAYER_CREATE_ELEMENT(audiobin, MMPLAYER_A_PITCH, "pitch", "audio pitch", *bucket, player);
2846                         g_object_set(G_OBJECT(audiobin[MMPLAYER_A_PITCH].gst), "pitch", (gdouble)pitch_value, NULL);
2847                 } else {
2848                         LOGW("there is no pitch element");
2849                 }
2850         }
2851
2852         /* converter */
2853         MMPLAYER_CREATE_ELEMENT(audiobin, MMPLAYER_A_CONV, "audioconvert", "audio converter", *bucket, player);
2854
2855         /* replaygain volume */
2856         MMPLAYER_CREATE_ELEMENT(audiobin, MMPLAYER_A_RGVOL, "rgvolume", "audio rgvolume", *bucket, player);
2857         if (player->sound.rg_enable)
2858                 g_object_set(G_OBJECT(audiobin[MMPLAYER_A_RGVOL].gst), "enable-rgvolume", TRUE, NULL);
2859         else
2860                 g_object_set(G_OBJECT(audiobin[MMPLAYER_A_RGVOL].gst), "enable-rgvolume", FALSE, NULL);
2861
2862         /* resampler */
2863         MMPLAYER_CREATE_ELEMENT(audiobin, MMPLAYER_A_RESAMPLER,  player->ini.audioresampler_element, "audio resampler", *bucket, player);
2864
2865         if (g_strrstr(player->ini.audiosink_element, "openalsink")) {
2866                 /* currently, only openalsink uses volume element */
2867                 MMPLAYER_CREATE_ELEMENT(audiobin, MMPLAYER_A_VOL, "volume", "volume", *bucket, player);
2868                 g_object_set(G_OBJECT(audiobin[MMPLAYER_A_VOL].gst), "volume", player->sound.volume, NULL);
2869
2870                 if (player->sound.mute) {
2871                         LOGD("mute enabled");
2872                         g_object_set(G_OBJECT(audiobin[MMPLAYER_A_VOL].gst), "mute", player->sound.mute, NULL);
2873                 }
2874         }
2875
2876         mm_attrs_get_int_by_name(player->attrs, "content_audio_channels", &channels);
2877
2878         /* audio effect element. if audio effect is enabled */
2879         if ((strcmp(player->ini.audioeffect_element, ""))
2880                 && (channels <= 2)
2881                 && (player->ini.use_audio_effect_preset || player->ini.use_audio_effect_custom)) {
2882                 MMPLAYER_CREATE_ELEMENT(audiobin, MMPLAYER_A_FILTER, player->ini.audioeffect_element, "audio effect filter", *bucket, player);
2883
2884                 LOGD("audio effect config. bypass = %d, effect type = %d", player->bypass_audio_effect, player->audio_effect_info.effect_type);
2885
2886                 if ((!player->bypass_audio_effect)
2887                         && (player->ini.use_audio_effect_preset || player->ini.use_audio_effect_custom)) {
2888                         if (player->audio_effect_info.effect_type == MM_AUDIO_EFFECT_TYPE_CUSTOM) {
2889                                 if (!_mmplayer_audio_effect_custom_apply(player))
2890                                         LOGI("apply audio effect(custom) setting success");
2891                         }
2892                 }
2893
2894                 if ((strcmp(player->ini.audioeffect_element_custom, ""))
2895                         && (player->set_mode.rich_audio)) {
2896                         MMPLAYER_CREATE_ELEMENT(audiobin, MMPLAYER_A_FILTER_SEC, player->ini.audioeffect_element_custom, "audio effect filter custom", *bucket, player);
2897                 }
2898         }
2899
2900         /* create audio sink */
2901         LOGD("spherical %d, channels %d, ambisonic type %d, format %d, order %d",
2902                         player->is_content_spherical, channels, player->video360_metadata.ambisonic_type,
2903                         player->video360_metadata.ambisonic_format, player->video360_metadata.ambisonic_order);
2904
2905         /* Note: qtdemux converts audio metadata defaults to openalsink defaults. */
2906         if (player->is_360_feature_enabled &&
2907                 player->is_content_spherical &&
2908                 channels == 4 &&
2909                 player->video360_metadata.ambisonic_type == MMFILE_AMBISONIC_TYPE_PERIPHONIC &&
2910                 player->video360_metadata.ambisonic_format == MMFILE_AMBISONIC_FORMAT_AMB &&
2911                 player->video360_metadata.ambisonic_order == MMFILE_AMBISONIC_ORDER_FOA) {
2912
2913                 strncpy(player->ini.audiosink_element, "openalsink", PLAYER_INI_MAX_STRLEN - 1);
2914
2915                 MMPLAYER_CREATE_ELEMENT(audiobin, MMPLAYER_A_CONV_BFORMAT, "audioconvert", "audio-converter-bformat", *bucket, player);
2916
2917                 MMPLAYER_CREATE_ELEMENT(audiobin, MMPLAYER_A_CAPS_360, "capsfilter", "audio-caps-filter", *bucket, player);
2918                 acaps = gst_caps_from_string(SPATIAL_AUDIO_CAPS);
2919                 g_object_set(G_OBJECT(audiobin[MMPLAYER_A_CAPS_360].gst), "caps", acaps, NULL);
2920                 gst_caps_unref(acaps);
2921
2922                 MMPLAYER_CREATE_ELEMENT(audiobin, MMPLAYER_A_SINK, "openalsink", "audiosink", *bucket, player);
2923
2924                 player->is_openal_plugin_used = TRUE;
2925         } else {
2926                 if (player->is_360_feature_enabled && player->is_content_spherical)
2927                         LOGW("Audio track isn't of the ambisonic type and can't be played back as a spatial sound.");
2928                 MMPLAYER_CREATE_ELEMENT(audiobin, MMPLAYER_A_SINK, player->ini.audiosink_element, "audiosink", *bucket, player);
2929         }
2930
2931         if ((MMPLAYER_IS_RTSP_STREAMING(player)) ||
2932                 (player->videodec_linked && player->ini.use_system_clock)) {
2933                 LOGD("system clock will be used.");
2934                 g_object_set(G_OBJECT(audiobin[MMPLAYER_A_SINK].gst), "provide-clock", FALSE,  NULL);
2935         }
2936
2937         if (g_strrstr(player->ini.audiosink_element, "pulsesink")) {
2938                 __mmplayer_gst_set_pulsesink_property(player);
2939         } else if (g_strrstr(player->ini.audiosink_element, "openalsink")) {
2940                 if (__mmplayer_gst_set_openalsink_property(player) != MM_ERROR_NONE)
2941                         goto ERROR;
2942         }
2943
2944         /* qos on */
2945         g_object_set(G_OBJECT(audiobin[MMPLAYER_A_SINK].gst), "qos", TRUE, NULL);       /* qos on */
2946         g_object_set(G_OBJECT(audiobin[MMPLAYER_A_SINK].gst), "slave-method", GST_AUDIO_BASE_SINK_SLAVE_NONE, NULL);
2947
2948         sink_pad = gst_element_get_static_pad(audiobin[MMPLAYER_A_SINK].gst, "sink");
2949         _mmplayer_add_signal_connection(player, G_OBJECT(sink_pad), MM_PLAYER_SIGNAL_TYPE_AUDIOBIN,
2950                                 "notify::caps", G_CALLBACK(__mmplayer_gst_caps_notify_cb), (gpointer)player);
2951         gst_object_unref(GST_OBJECT(sink_pad));
2952
2953         __mmplayer_add_sink(player, audiobin[MMPLAYER_A_SINK].gst, FALSE);
2954
2955         MMPLAYER_FLEAVE();
2956         return MM_ERROR_NONE;
2957
2958 ERROR: /* MMPLAYER_CREATE_ELEMENT */
2959         MMPLAYER_FLEAVE();
2960         return MM_ERROR_PLAYER_INTERNAL;
2961 }
2962
2963 static int
2964 __mmplayer_gst_make_audio_extract_sink(mmplayer_t *player, GList **bucket)
2965 {
2966         mmplayer_gst_element_t *audiobin = NULL;
2967         enum audio_element_id extract_sink_id = MMPLAYER_A_SINK;
2968
2969         gchar *dst_format = NULL;
2970         int dst_len = 0;
2971         int dst_samplerate = 0;
2972         int dst_channels = 0;
2973         GstCaps *caps = NULL;
2974         char *caps_str = NULL;
2975
2976         MMPLAYER_FENTER();
2977         MMPLAYER_RETURN_VAL_IF_FAIL(player && player->pipeline &&
2978                                 player->pipeline->audiobin, MM_ERROR_PLAYER_NOT_INITIALIZED);
2979
2980         audiobin = player->pipeline->audiobin;
2981
2982         LOGD("make element for audio extract, option = 0x%X", player->audio_extract_opt);
2983
2984         /* audio bin structure according to the mmplayer_audio_extract_opt_e.
2985
2986            [case 1] extract interleave audio pcm without playback
2987                                 : MM_PLAYER_AUDIO_EXTRACT_DEFAULT (sync)
2988                                   MM_PLAYER_AUDIO_EXTRACT_NO_SYNC_WITH_CLOCK (non sync)
2989
2990                                 * src - ... - aconv - resample - capsfilter - fakesink (sync or not)
2991
2992            [case 2] deinterleave for each channel without playback
2993                                 : MM_PLAYER_AUDIO_EXTRACT_DEINTERLEAVE (sync)
2994                                   MM_PLAYER_AUDIO_EXTRACT_NO_SYNC_AND_DEINTERLEAVE (non sync)
2995
2996                                 * src - ... - aconv - resample - capsfilter - deinterleave - fakesink (sync or not)
2997                                                                                                                                                    - fakesink (sync or not)
2998                                                                                                                                                    - ...      (sync or not)
2999
3000            [case 3] [case 1(sync only)] + playback
3001                                 : MM_PLAYER_AUDIO_EXTRACT_WITH_PLAYBACK
3002
3003                                 * src - ... - tee - queue1 - playback path
3004                                                                   - queue2 - [case1 pipeline with sync]
3005
3006            [case 4] [case 2(sync only)] + playback
3007                                 : MM_PLAYER_AUDIO_EXTRACT_DEINTERLEAVE_WITH_PLAYBACK
3008
3009                                 * src - ... - tee - queue1 - playback path
3010                                                                   - queue2 - [case2 pipeline with sync]
3011
3012          */
3013
3014         /* 1. create tee and playback path
3015               'tee' should be added at first to copy the decoded stream
3016          */
3017         if (player->audio_extract_opt & MM_PLAYER_AUDIO_EXTRACT_WITH_PLAYBACK) {
3018                 MMPLAYER_CREATE_ELEMENT(audiobin, MMPLAYER_A_TEE, "tee", "audio-tee", *bucket, player);
3019                 g_object_set(G_OBJECT(audiobin[MMPLAYER_A_TEE].gst), "num-src-pads", 2, NULL);
3020
3021                 /* tee - path 1 : for playback path */
3022                 MMPLAYER_CREATE_ELEMENT(audiobin, MMPLAYER_A_TEE_Q1, "queue", "audio-tee-queue1", *bucket, player);
3023                 __mmplayer_gst_make_audio_playback_sink(player, bucket);
3024
3025                 /* tee - path 2 : for extract path */
3026                 MMPLAYER_CREATE_ELEMENT(audiobin, MMPLAYER_A_TEE_Q2, "queue", "audio-tee-queue2", *bucket, player);
3027                 extract_sink_id = MMPLAYER_A_EXTRACT_SINK; /* there is another playback sink */
3028         }
3029
3030         /* if there is tee, 'tee - path 2' is linked here */
3031         /* converter */
3032         MMPLAYER_CREATE_ELEMENT(audiobin, MMPLAYER_A_EXTRACT_CONV, "audioconvert", "audio-ext-conv", *bucket, player);
3033
3034         /* resampler */
3035         MMPLAYER_CREATE_ELEMENT(audiobin, MMPLAYER_A_EXTRACT_RESAMPLER,  player->ini.audioresampler_element, "audio-ext-resampler", *bucket, player);
3036
3037         /* 2. decide the extract pcm format */
3038         mm_attrs_multiple_get(player->attrs, NULL,
3039                                 MM_PLAYER_PCM_EXT_FORMAT, &dst_format, &dst_len,
3040                                 MM_PLAYER_PCM_EXT_SAMPLERATE, &dst_samplerate,
3041                                 MM_PLAYER_PCM_EXT_CHANNELS, &dst_channels,
3042                                 NULL);
3043
3044         LOGD("required extract pcm format - format: %s(%d), samplerate : %d, channel: %d",
3045                         dst_format, dst_len, dst_samplerate, dst_channels);
3046
3047         if (dst_format == NULL || dst_len == 0 || dst_samplerate == 0 || dst_channels == 0) {
3048                 mm_attrs_multiple_get(player->attrs, NULL,
3049                                         "content_audio_format", &dst_format, &dst_len, /* get string and len */
3050                                         "content_audio_samplerate", &dst_samplerate,
3051                                         "content_audio_channels", &dst_channels,
3052                                         NULL);
3053
3054                 LOGD("apply the decoded pcm format - format: %s(%d), samplerate : %d, channel: %d",
3055                                 dst_format, dst_len, dst_samplerate, dst_channels);
3056
3057                 /* If there is no enough information, set it to platform default value. */
3058                 if (dst_format == NULL || _mmplayer_convert_audio_pcm_str_to_media_format_mime(dst_format) == MEDIA_FORMAT_MAX) {
3059                         LOGD("set platform default format");
3060                         dst_format = DEFAULT_PCM_OUT_FORMAT;
3061                 }
3062                 if (dst_samplerate <= 0) dst_samplerate = DEFAULT_PCM_OUT_SAMPLERATE;
3063                 if (dst_channels <= 0)   dst_channels = DEFAULT_PCM_OUT_CHANNEL;
3064         }
3065
3066         /* 3. create capsfilter */
3067         MMPLAYER_CREATE_ELEMENT(audiobin, MMPLAYER_A_EXTRACT_CAPS, "capsfilter", "audio-ext-caps", *bucket, player);
3068         caps = gst_caps_new_simple("audio/x-raw",
3069                         "format", G_TYPE_STRING, dst_format,
3070                         "rate", G_TYPE_INT, dst_samplerate,
3071                         "channels", G_TYPE_INT, dst_channels,
3072                         NULL);
3073
3074         caps_str = gst_caps_to_string(caps);
3075         LOGD("new caps : %s", caps_str);
3076
3077         g_object_set(GST_ELEMENT(audiobin[MMPLAYER_A_EXTRACT_CAPS].gst), "caps", caps, NULL);
3078
3079         /* clean */
3080         gst_caps_unref(caps);
3081         MMPLAYER_FREEIF(caps_str);
3082
3083         /* 4-1. create deinterleave to extract pcm for each channel */
3084         if (player->audio_extract_opt & MM_PLAYER_AUDIO_EXTRACT_DEINTERLEAVE) {
3085                 MMPLAYER_CREATE_ELEMENT(audiobin, MMPLAYER_A_EXTRACT_DEINTERLEAVE, "deinterleave", "deinterleave", *bucket, player);
3086                 g_object_set(G_OBJECT(audiobin[MMPLAYER_A_EXTRACT_DEINTERLEAVE].gst), "keep-positions", TRUE, NULL);
3087
3088                 /* audiosink will be added after getting signal for each channel */
3089                 _mmplayer_add_signal_connection(player, G_OBJECT(audiobin[MMPLAYER_A_EXTRACT_DEINTERLEAVE].gst),
3090                                 MM_PLAYER_SIGNAL_TYPE_AUTOPLUG, "pad-added", G_CALLBACK(__mmplayer_gst_audio_deinterleave_pad_added), (gpointer)player);
3091                 _mmplayer_add_signal_connection(player, G_OBJECT(audiobin[MMPLAYER_A_EXTRACT_DEINTERLEAVE].gst),
3092                                 MM_PLAYER_SIGNAL_TYPE_AUTOPLUG, "no-more-pads", G_CALLBACK(__mmplayer_gst_audio_deinterleave_no_more_pads), (gpointer)player);
3093                 player->no_more_pad = FALSE;
3094         } else {
3095         /* 4-2. create fakesink to extract interleaved pcm */
3096                 LOGD("add audio fakesink for interleaved audio");
3097                 MMPLAYER_CREATE_ELEMENT(audiobin, extract_sink_id, "fakesink", "fakeaudiosink", *bucket, player);
3098                 if (!(player->audio_extract_opt & MM_PLAYER_AUDIO_EXTRACT_NO_SYNC_WITH_CLOCK))
3099                         g_object_set(G_OBJECT(audiobin[extract_sink_id].gst), "sync", TRUE, NULL);
3100                 g_object_set(G_OBJECT(audiobin[extract_sink_id].gst), "signal-handoffs", TRUE, NULL);
3101
3102                 _mmplayer_add_signal_connection(player,
3103                         G_OBJECT(audiobin[extract_sink_id].gst),
3104                         MM_PLAYER_SIGNAL_TYPE_AUDIOBIN,
3105                         "handoff",
3106                         G_CALLBACK(__mmplayer_audio_stream_decoded_render_cb),
3107                         (gpointer)player);
3108
3109                 __mmplayer_add_sink(player, audiobin[extract_sink_id].gst, FALSE);
3110         }
3111
3112         MMPLAYER_FLEAVE();
3113         return MM_ERROR_NONE;
3114
3115 ERROR: /* MMPLAYER_CREATE_ELEMENT */
3116         MMPLAYER_FLEAVE();
3117         return MM_ERROR_PLAYER_INTERNAL;
3118 }
3119
3120 static int
3121 __mmplayer_gst_make_audio_bin_element(mmplayer_t *player, GList **bucket)
3122 {
3123         int ret = MM_ERROR_NONE;
3124         mmplayer_gst_element_t *audiobin = NULL;
3125         GList *element_bucket = NULL;
3126
3127         MMPLAYER_FENTER();
3128         MMPLAYER_RETURN_VAL_IF_FAIL(player && player->pipeline &&
3129                                 player->pipeline->audiobin, MM_ERROR_PLAYER_NOT_INITIALIZED);
3130
3131         audiobin = player->pipeline->audiobin;
3132
3133         if (player->build_audio_offload) { /* skip all the audio filters */
3134                 LOGD("create audio offload sink : %s", player->ini.audio_offload_sink_element);
3135
3136                 MMPLAYER_CREATE_ELEMENT(audiobin, MMPLAYER_A_SINK, player->ini.audio_offload_sink_element, "audiosink", element_bucket, player);
3137                 g_object_set(G_OBJECT(audiobin[MMPLAYER_A_SINK].gst), "sync", TRUE,
3138                                 "volume", player->sound.volume, "mute", player->sound.mute, NULL);
3139
3140                 __mmplayer_add_sink(player, audiobin[MMPLAYER_A_SINK].gst, FALSE);
3141                 goto DONE;
3142         }
3143
3144         /* FIXME: need to mention the supportable condition at API reference */
3145         if (player->audio_decoded_cb && (!MMPLAYER_IS_RTSP_STREAMING(player)))
3146                 ret = __mmplayer_gst_make_audio_extract_sink(player, &element_bucket);
3147         else
3148                 ret = __mmplayer_gst_make_audio_playback_sink(player, &element_bucket);
3149
3150         if (ret != MM_ERROR_NONE)
3151                 goto ERROR;
3152 DONE:
3153         LOGD("success to make audio bin element");
3154         *bucket = element_bucket;
3155
3156         MMPLAYER_FLEAVE();
3157         return MM_ERROR_NONE;
3158
3159 ERROR:
3160         LOGE("failed to make audio bin element");
3161         g_list_free(element_bucket);
3162
3163         *bucket = NULL;
3164         MMPLAYER_FLEAVE();
3165         return MM_ERROR_PLAYER_INTERNAL;
3166 }
3167
3168 static int
3169 __mmplayer_gst_create_audio_sink_bin(mmplayer_t *player)
3170 {
3171         mmplayer_gst_element_t *first_element = NULL;
3172         mmplayer_gst_element_t *audiobin = NULL;
3173         GstPad *pad = NULL;
3174         GstPad *ghostpad = NULL;
3175         GList *element_bucket = NULL;
3176         int i = 0;
3177
3178         MMPLAYER_FENTER();
3179         MMPLAYER_RETURN_VAL_IF_FAIL(player && player->pipeline, MM_ERROR_PLAYER_NOT_INITIALIZED);
3180
3181         /* alloc handles */
3182         audiobin = (mmplayer_gst_element_t *)g_try_malloc0(sizeof(mmplayer_gst_element_t) * MMPLAYER_A_NUM);
3183         if (!audiobin) {
3184                 LOGE("failed to allocate memory for audiobin");
3185                 return MM_ERROR_PLAYER_NO_FREE_SPACE;
3186         }
3187
3188         /* create bin */
3189         audiobin[MMPLAYER_A_BIN].id = MMPLAYER_A_BIN;
3190         audiobin[MMPLAYER_A_BIN].gst = gst_bin_new("audiobin");
3191         if (!audiobin[MMPLAYER_A_BIN].gst) {
3192                 LOGE("failed to create audiobin");
3193                 goto ERROR;
3194         }
3195
3196         /* take it */
3197         player->pipeline->audiobin = audiobin;
3198
3199         /* create audio filters and audiosink */
3200         if (__mmplayer_gst_make_audio_bin_element(player, &element_bucket) != MM_ERROR_NONE)
3201                 goto ERROR;
3202
3203         /* adding created elements to bin */
3204         LOGD("adding created elements to bin");
3205         if (!_mmplayer_gst_element_add_bucket_to_bin(GST_BIN(audiobin[MMPLAYER_A_BIN].gst), element_bucket))
3206                 goto ERROR;
3207
3208         /* linking elements in the bucket by added order. */
3209         LOGD("Linking elements in the bucket by added order.");
3210         if (_mmplayer_gst_element_link_bucket(element_bucket) == -1)
3211                 goto ERROR;
3212
3213         /* get first element's sinkpad for creating ghostpad */
3214         first_element = (mmplayer_gst_element_t *)element_bucket->data;
3215         if (!first_element) {
3216                 LOGE("failed to get first elem");
3217                 goto ERROR;
3218         }
3219
3220         pad = gst_element_get_static_pad(GST_ELEMENT(first_element->gst), "sink");
3221         if (!pad) {
3222                 LOGE("failed to get pad from first element of audiobin");
3223                 goto ERROR;
3224         }
3225
3226         ghostpad = gst_ghost_pad_new("sink", pad);
3227         if (!ghostpad) {
3228                 LOGE("failed to create ghostpad");
3229                 goto ERROR;
3230         }
3231
3232         if (!gst_element_add_pad(audiobin[MMPLAYER_A_BIN].gst, ghostpad)) {
3233                 LOGE("failed to add ghostpad to audiobin");
3234                 goto ERROR;
3235         }
3236
3237         gst_object_unref(pad);
3238
3239         g_list_free(element_bucket);
3240         MMPLAYER_FLEAVE();
3241
3242         return MM_ERROR_NONE;
3243
3244 ERROR:
3245         LOGD("ERROR : releasing audiobin");
3246
3247         if (pad)
3248                 gst_object_unref(GST_OBJECT(pad));
3249
3250         if (ghostpad)
3251                 gst_object_unref(GST_OBJECT(ghostpad));
3252
3253         if (element_bucket)
3254                 g_list_free(element_bucket);
3255
3256         /* release element which are not added to bin */
3257         for (i = 1; i < MMPLAYER_A_NUM; i++) {
3258                 /* NOTE : skip bin */
3259                 if (audiobin[i].gst) {
3260                         GstObject *parent = NULL;
3261                         parent = gst_element_get_parent(audiobin[i].gst);
3262
3263                         if (!parent) {
3264                                 gst_object_unref(GST_OBJECT(audiobin[i].gst));
3265                                 audiobin[i].gst = NULL;
3266                         } else
3267                                 gst_object_unref(GST_OBJECT(parent));
3268                 }
3269         }
3270
3271         /* release audiobin with it's children */
3272         if (audiobin[MMPLAYER_A_BIN].gst)
3273                 gst_object_unref(GST_OBJECT(audiobin[MMPLAYER_A_BIN].gst));
3274
3275         MMPLAYER_FREEIF(audiobin);
3276
3277         player->pipeline->audiobin = NULL;
3278
3279         return MM_ERROR_PLAYER_INTERNAL;
3280 }
3281
3282 static guint32
3283 _mmplayer_convert_fourcc_string_to_value(const gchar *format_name)
3284 {
3285         return format_name[0] | (format_name[1] << 8) | (format_name[2] << 16) | (format_name[3] << 24);
3286 }
3287
3288 int
3289 _mmplayer_video_stream_release_bo(mmplayer_t *player, void *bo)
3290 {
3291         int ret = MM_ERROR_NONE;
3292         GList *l = NULL;
3293         MMPLAYER_RETURN_VAL_IF_FAIL(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
3294         MMPLAYER_RETURN_VAL_IF_FAIL(bo, MM_ERROR_INVALID_ARGUMENT);
3295
3296         MMPLAYER_VIDEO_BO_LOCK(player);
3297
3298         if (player->video_bo_list) {
3299                 for (l = g_list_first(player->video_bo_list); l; l = g_list_next(l)) {
3300                         mmplayer_video_bo_info_t *tmp = (mmplayer_video_bo_info_t *)l->data;
3301                         if (tmp && tmp->bo == bo) {
3302                                 tmp->used = FALSE;
3303                                 LOGD("release bo %p", bo);
3304                                 tbm_bo_unref(tmp->bo);
3305                                 MMPLAYER_VIDEO_BO_UNLOCK(player);
3306                                 MMPLAYER_VIDEO_BO_SIGNAL(player);
3307                                 return ret;
3308                         }
3309                 }
3310         } else {
3311                 /* hw codec is running or the list was reset for DRC. */
3312                 LOGW("there is no bo list.");
3313         }
3314         MMPLAYER_VIDEO_BO_UNLOCK(player);
3315
3316         LOGW("failed to find bo %p", bo);
3317         return ret;
3318 }
3319 static void
3320 __mmplayer_video_stream_bo_list_free(mmplayer_video_bo_info_t *tmp)
3321 {
3322         if (!tmp)
3323                 return;
3324
3325         if (tmp->bo)
3326                 tbm_bo_unref(tmp->bo);
3327         g_free(tmp);
3328 }
3329
3330 static void
3331 __mmplayer_video_stream_destroy_bo_list(mmplayer_t *player)
3332 {
3333         MMPLAYER_FENTER();
3334         MMPLAYER_RETURN_IF_FAIL(player);
3335
3336         MMPLAYER_VIDEO_BO_LOCK(player);
3337         if (player->video_bo_list) {
3338                 LOGD("destroy video_bo_list : %d", g_list_length(player->video_bo_list));
3339                 g_list_free_full(player->video_bo_list, (GDestroyNotify)__mmplayer_video_stream_bo_list_free);
3340                 player->video_bo_list = NULL;
3341         }
3342         player->video_bo_size = 0;
3343         MMPLAYER_VIDEO_BO_UNLOCK(player);
3344
3345         MMPLAYER_FLEAVE();
3346         return;
3347 }
3348
3349 static void *
3350 __mmplayer_video_stream_get_bo(mmplayer_t *player, int size)
3351 {
3352         GList *l = NULL;
3353         MMPLAYER_RETURN_VAL_IF_FAIL(player, NULL);
3354         gboolean ret = TRUE;
3355         gint64 end_time = 0;
3356
3357         /* check DRC, if it is, destroy the prev bo list to create again */
3358         if (player->video_bo_size != size) {
3359                 LOGD("video size is changed: %d -> %d", player->video_bo_size, size);
3360                 __mmplayer_video_stream_destroy_bo_list(player);
3361                 player->video_bo_size = size;
3362         }
3363
3364         MMPLAYER_VIDEO_BO_LOCK(player);
3365
3366         if ((!player->video_bo_list) ||
3367                 (g_list_length(player->video_bo_list) < player->ini.num_of_video_bo)) {
3368
3369                 /* create bo list */
3370                 int idx = 0;
3371                 LOGD("Create bo list for decoded video stream(num:%d)", player->ini.num_of_video_bo);
3372
3373                 if (player->video_bo_list) {
3374                         /* if bo list did not created all, try it again. */
3375                         idx = g_list_length(player->video_bo_list);
3376                         LOGD("bo list exist(len: %d)", idx);
3377                 }
3378
3379                 for (; idx < player->ini.num_of_video_bo; idx++) {
3380                         mmplayer_video_bo_info_t *bo_info = g_new(mmplayer_video_bo_info_t, 1);
3381                         if (!bo_info) {
3382                                 LOGE("Fail to alloc bo_info.");
3383                                 break;
3384                         }
3385                         bo_info->bo = tbm_bo_alloc(player->bufmgr, size, TBM_BO_DEFAULT);
3386                         if (!bo_info->bo) {
3387                                 LOGE("Fail to tbm_bo_alloc.");
3388                                 MMPLAYER_FREEIF(bo_info);
3389                                 break;
3390                         }
3391                         bo_info->used = FALSE;
3392                         player->video_bo_list = g_list_append(player->video_bo_list, bo_info);
3393                 }
3394
3395                 /* update video num buffers */
3396                 LOGD("video_num_buffers : %d", idx);
3397                 mm_player_set_attribute((MMHandleType)player, NULL,
3398                                 MM_PLAYER_VIDEO_BUFFER_TOTAL_SIZE, idx,
3399                                 MM_PLAYER_VIDEO_BUFFER_EXTRA_SIZE, MAX(DEFAULT_NUM_OF_V_OUT_BUFFER, (idx / 2)),
3400                                 NULL);
3401
3402                 if (idx == 0) {
3403                         MMPLAYER_VIDEO_BO_UNLOCK(player);
3404                         return NULL;
3405                 }
3406         }
3407
3408         if (player->ini.video_bo_timeout > 0)
3409                 end_time = g_get_monotonic_time() + player->ini.video_bo_timeout * G_TIME_SPAN_SECOND;
3410
3411         while (TRUE) {
3412                 /* get bo from list*/
3413                 for (l = g_list_first(player->video_bo_list); l; l = g_list_next(l)) {
3414                         mmplayer_video_bo_info_t *tmp = (mmplayer_video_bo_info_t *)l->data;
3415                         if (tmp && (tmp->used == FALSE)) {
3416                                 LOGD("found bo %p to use", tmp->bo);
3417                                 tmp->used = TRUE;
3418                                 MMPLAYER_VIDEO_BO_UNLOCK(player);
3419                                 return tbm_bo_ref(tmp->bo);
3420                         }
3421                 }
3422
3423                 if (player->ini.video_bo_timeout <= 0) {
3424                         MMPLAYER_VIDEO_BO_WAIT(player);
3425                 } else {
3426                         ret = MMPLAYER_VIDEO_BO_WAIT_UNTIL(player, end_time);
3427                         if (!ret) {
3428                                 LOGE("failed to get bo in %d timeout", player->ini.video_bo_timeout);
3429                                 break;
3430                         }
3431                 }
3432         }
3433
3434         MMPLAYER_VIDEO_BO_UNLOCK(player);
3435         return NULL;
3436 }
3437
3438 static void
3439 __mmplayer_video_stream_decoded_preroll_cb(GstElement *object, GstBuffer *buffer, GstPad *pad, gpointer data)
3440 {
3441         mmplayer_t *player = (mmplayer_t *)data;
3442         MMPLAYER_FENTER();
3443         MMPLAYER_RETURN_IF_FAIL(player && player->video_decoded_cb);
3444
3445         /* send prerolled pkt */
3446         player->video_stream_prerolled = false;
3447
3448         __mmplayer_video_stream_decoded_render_cb(object, buffer, pad, data);
3449
3450         /* not to send prerolled pkt again */
3451         player->video_stream_prerolled = true;
3452 }
3453
3454 static void
3455 __mmplayer_video_stream_decoded_render_cb(GstElement *object, GstBuffer *buffer, GstPad *pad, gpointer data)
3456 {
3457         mmplayer_t *player = (mmplayer_t *)data;
3458         mmplayer_video_decoded_data_info_t *stream = NULL;
3459         GstMemory *mem = NULL;
3460
3461         MMPLAYER_FENTER();
3462         MMPLAYER_RETURN_IF_FAIL(player);
3463         MMPLAYER_RETURN_IF_FAIL(player->video_decoded_cb);
3464
3465         if (player->video_stream_prerolled) {
3466                 player->video_stream_prerolled = false;
3467                 LOGD("skip the prerolled pkt not to send it again");
3468                 return;
3469         }
3470
3471         /* clear stream data structure */
3472         stream = __mmplayer_create_stream_from_pad(pad);
3473         if (!stream) {
3474                 LOGE("failed to alloc stream");
3475                 return;
3476         }
3477
3478         _mmplayer_get_video_angle(player, NULL, &stream->orientation);
3479
3480         /* set size and timestamp */
3481         mem = gst_buffer_peek_memory(buffer, 0);
3482         stream->length_total = gst_memory_get_sizes(mem, NULL, NULL);
3483         stream->timestamp = (unsigned int)(GST_TIME_AS_MSECONDS(GST_BUFFER_PTS(buffer))); /* nano sec -> milli sec */
3484
3485         /* check zero-copy */
3486         if (player->set_mode.video_zc &&
3487                 player->set_mode.video_export &&
3488                 gst_is_tizen_memory(mem)) {
3489                 __mmplayer_zerocopy_set_stride_elevation_bo(stream, mem);
3490                 stream->internal_buffer = gst_buffer_ref(buffer);
3491         } else { /* sw codec */
3492                 if (!__mmplayer_swcodec_set_stride_elevation(stream))
3493                         goto ERROR;
3494
3495                 if (!__mmplayer_swcodec_set_bo(player, stream, mem))
3496                         goto ERROR;
3497         }
3498
3499         if (!player->video_decoded_cb(stream, player->video_decoded_cb_user_param)) {
3500                 LOGE("failed to send video decoded data.");
3501                 goto ERROR;
3502         }
3503
3504         return;
3505
3506 ERROR:
3507         LOGE("release video stream resource.");
3508         if (gst_is_tizen_memory(mem)) {
3509                 int i = 0;
3510                 for (i = 0 ; i < MM_VIDEO_BUFFER_PLANE_MAX ; i++) {
3511                         if (stream->bo[i])
3512                                 tbm_bo_unref(stream->bo[i]);
3513                 }
3514
3515                 /* unref gst buffer */
3516                 if (stream->internal_buffer)
3517                         gst_buffer_unref(stream->internal_buffer);
3518         } else {
3519                 if (stream->bo[0])
3520                         _mmplayer_video_stream_release_bo(player, stream->bo[0]);
3521         }
3522         MMPLAYER_FREEIF(stream);
3523         return;
3524 }
3525
3526 static void
3527 __mmplayer_gst_set_video360_property(mmplayer_t *player)
3528 {
3529         mmplayer_gst_element_t *videobin = NULL;
3530
3531         MMPLAYER_FENTER();
3532         MMPLAYER_RETURN_IF_FAIL(player && player->pipeline && player->pipeline->videobin);
3533
3534         videobin = player->pipeline->videobin;
3535
3536         /* Set spatial media metadata and/or user settings to the element.
3537          * */
3538         g_object_set(G_OBJECT(videobin[MMPLAYER_V_360].gst),
3539                         "projection-type", player->video360_metadata.projection_type, NULL);
3540
3541         g_object_set(G_OBJECT(videobin[MMPLAYER_V_360].gst),
3542                         "stereo-mode", player->video360_metadata.stereo_mode, NULL);
3543
3544         if (player->video360_metadata.full_pano_width_pixels &&
3545                         player->video360_metadata.full_pano_height_pixels &&
3546                         player->video360_metadata.cropped_area_image_width &&
3547                         player->video360_metadata.cropped_area_image_height) {
3548                 g_object_set(G_OBJECT(videobin[MMPLAYER_V_360].gst),
3549                                 "projection-bounds-top", player->video360_metadata.cropped_area_top,
3550                                 "projection-bounds-bottom", player->video360_metadata.full_pano_height_pixels -
3551                                                 player->video360_metadata.cropped_area_top - player->video360_metadata.cropped_area_image_height,
3552                                 "projection-bounds-left", player->video360_metadata.cropped_area_left,
3553                                 "projection-bounds-right", player->video360_metadata.full_pano_width_pixels -
3554                                                 player->video360_metadata.cropped_area_left - player->video360_metadata.cropped_area_image_width,
3555                                 NULL);
3556         }
3557
3558         if (player->video360_horizontal_fov && player->video360_vertical_fov) {
3559                 g_object_set(G_OBJECT(videobin[MMPLAYER_V_360].gst),
3560                                 "horizontal-fov", player->video360_horizontal_fov,
3561                                 "vertical-fov", player->video360_vertical_fov, NULL);
3562         }
3563
3564         if (player->video360_zoom <= VIDEO360_MAX_ZOOM && player->video360_zoom > 1.0f) {
3565                 g_object_set(G_OBJECT(videobin[MMPLAYER_V_360].gst),
3566                                 "zoom", 1.0f / player->video360_zoom, NULL);
3567         }
3568
3569         if (player->video360_yaw_radians <= M_PI &&
3570                         player->video360_yaw_radians >= -M_PI &&
3571                         player->video360_pitch_radians <= M_PI_2 &&
3572                         player->video360_pitch_radians >= -M_PI_2) {
3573                 g_object_set(G_OBJECT(videobin[MMPLAYER_V_360].gst),
3574                                 "pose-yaw", (int)(player->video360_yaw_radians * 180.0 / M_PI),
3575                                 "pose-pitch", (int)(player->video360_pitch_radians * 180.0 / M_PI), NULL);
3576         } else if (player->video360_metadata.init_view_heading || player->video360_metadata.init_view_pitch) {
3577                 g_object_set(G_OBJECT(videobin[MMPLAYER_V_360].gst),
3578                                 "pose-yaw", player->video360_metadata.init_view_heading,
3579                                 "pose-pitch", player->video360_metadata.init_view_pitch, NULL);
3580         }
3581
3582         g_object_set(G_OBJECT(videobin[MMPLAYER_V_360].gst),
3583                         "passthrough", !player->is_video360_enabled, NULL);
3584
3585
3586         MMPLAYER_FLEAVE();
3587 }
3588
3589 static int
3590 __mmplayer_gst_create_video_filters(mmplayer_t *player, MMDisplaySurfaceType surface_type, GList **bucket)
3591 {
3592         gchar *video_csc = "videoconvert"; /* default colorspace converter */
3593         GList *element_bucket = NULL;
3594
3595         MMPLAYER_FENTER();
3596         MMPLAYER_RETURN_VAL_IF_FAIL(player && player->pipeline && player->pipeline->videobin, MM_ERROR_PLAYER_NOT_INITIALIZED);
3597
3598         /* create video360 filter */
3599         if (player->is_360_feature_enabled && player->is_content_spherical) {
3600                 LOGD("create video360 element");
3601                 MMPLAYER_CREATE_ELEMENT(player->pipeline->videobin, MMPLAYER_V_360, "video360", "video-360", element_bucket, player);
3602                 __mmplayer_gst_set_video360_property(player);
3603                 goto EXIT;
3604         }
3605
3606         if ((surface_type != MM_DISPLAY_SURFACE_OVERLAY &&
3607                 surface_type != MM_DISPLAY_SURFACE_OVERLAY_SYNC_UI) ||
3608                 player->set_mode.video_zc) {
3609                 LOGD("skip creating the videoconv and rotator");
3610                 return MM_ERROR_NONE;
3611         }
3612
3613         /* in case of sw codec & overlay surface type, except 360 playback.
3614          * if libav video decoder is selected, videoconvert is required to render the shm wl-buffer which support RGB only via tizenwlsink. */
3615         LOGD("create video converter: %s", video_csc);
3616         MMPLAYER_CREATE_ELEMENT(player->pipeline->videobin, MMPLAYER_V_CONV, video_csc, "video converter", element_bucket, player);
3617
3618 EXIT:
3619         *bucket = element_bucket;
3620         MMPLAYER_FLEAVE();
3621         return MM_ERROR_NONE;
3622
3623 ERROR: /* refer MMPLAYER_CREATE_ELEMENT */
3624         g_list_free(element_bucket);
3625
3626         *bucket = NULL;
3627         MMPLAYER_FLEAVE();
3628         return MM_ERROR_PLAYER_INTERNAL;
3629 }
3630
3631 static gchar *
3632 __mmplayer_get_videosink_factory_name(mmplayer_t *player, MMDisplaySurfaceType surface_type)
3633 {
3634         gchar *factory_name = NULL;
3635
3636         switch (surface_type) {
3637         case MM_DISPLAY_SURFACE_OVERLAY:
3638         /* fall through */
3639         case MM_DISPLAY_SURFACE_OVERLAY_SYNC_UI:
3640                 if (strlen(player->ini.videosink_element_overlay) > 0)
3641                         factory_name = player->ini.videosink_element_overlay;
3642                 break;
3643         case MM_DISPLAY_SURFACE_REMOTE:
3644         /* fall through */
3645         case MM_DISPLAY_SURFACE_NULL:
3646                 if (strlen(player->ini.videosink_element_fake) > 0)
3647                         factory_name = player->ini.videosink_element_fake;
3648                 break;
3649         default:
3650                 LOGE("unidentified surface type");
3651                 break;
3652         }
3653
3654         LOGD("surface_type %d, videosink is %s", surface_type, factory_name);
3655         return factory_name;
3656 }
3657
3658 static int
3659 __mmplayer_gst_set_videosink_property(mmplayer_t *player, MMDisplaySurfaceType surface_type)
3660 {
3661         gchar *factory_name = NULL;
3662         mmplayer_gst_element_t *videobin = NULL;
3663         MMHandleType attrs;
3664         int gapless = 0;
3665
3666         MMPLAYER_FENTER();
3667         MMPLAYER_RETURN_VAL_IF_FAIL(player && player->pipeline && player->pipeline->videobin, MM_ERROR_PLAYER_NOT_INITIALIZED);
3668
3669         videobin = player->pipeline->videobin;
3670         factory_name = GST_OBJECT_NAME(gst_element_get_factory(videobin[MMPLAYER_V_SINK].gst));
3671
3672         attrs = MMPLAYER_GET_ATTRS(player);
3673         if (!attrs) {
3674                 LOGE("cannot get content attribute");
3675                 return MM_ERROR_PLAYER_INTERNAL;
3676         }
3677
3678         if (surface_type == MM_DISPLAY_SURFACE_OVERLAY || surface_type == MM_DISPLAY_SURFACE_OVERLAY_SYNC_UI) {
3679                 bool use_tbm = (player->set_mode.video_zc || (player->is_360_feature_enabled && player->is_content_spherical));
3680                 if (strncmp(factory_name, "tizenwlsink", strlen(factory_name)) == 0) {
3681                         g_object_set(player->pipeline->videobin[MMPLAYER_V_SINK].gst,
3682                                 "use-tbm", use_tbm, NULL);
3683                 }
3684
3685                 if (_mmplayer_update_video_overlay_param(player, "update_all_param") != MM_ERROR_NONE)
3686                         return MM_ERROR_PLAYER_INTERNAL;
3687
3688                 LOGI("videosink factory name is %s use-tbm : %d", factory_name, use_tbm);
3689
3690         } else {
3691                 g_object_set(G_OBJECT(player->pipeline->videobin[MMPLAYER_V_SINK].gst),
3692                                                                                 "sync", TRUE, "max-lateness", FAKE_SINK_MAX_LATENESS, NULL);
3693         }
3694
3695         mm_attrs_get_int_by_name(attrs, MM_PLAYER_GAPLESS_MODE, &gapless);
3696         if (gapless > 0) {
3697                 LOGD("disable last-sample");
3698                 g_object_set(player->pipeline->videobin[MMPLAYER_V_SINK].gst, "enable-last-sample", FALSE, NULL);
3699         }
3700
3701         if (player->set_mode.video_export) {
3702                 int enable = 0;
3703                 mm_attrs_get_int_by_name(player->attrs, "enable_video_decoded_cb", &enable);
3704                 if (enable || (surface_type == MM_DISPLAY_SURFACE_REMOTE) || (surface_type == MM_DISPLAY_SURFACE_NULL))
3705                         g_object_set(G_OBJECT(player->pipeline->videobin[MMPLAYER_V_SINK].gst), "signal-handoffs", TRUE, NULL);
3706
3707                 _mmplayer_add_signal_connection(player,
3708                                                                 G_OBJECT(player->pipeline->videobin[MMPLAYER_V_SINK].gst),
3709                                                                 MM_PLAYER_SIGNAL_TYPE_VIDEOBIN,
3710                                                                 "handoff",
3711                                                                 G_CALLBACK(__mmplayer_video_stream_decoded_render_cb),
3712                                                                 (gpointer)player);
3713
3714                 _mmplayer_add_signal_connection(player,
3715                                                                 G_OBJECT(player->pipeline->videobin[MMPLAYER_V_SINK].gst),
3716                                                                 MM_PLAYER_SIGNAL_TYPE_VIDEOBIN,
3717                                                                 "preroll-handoff",
3718                                                                 G_CALLBACK(__mmplayer_video_stream_decoded_preroll_cb),
3719                                                                 (gpointer)player);
3720         }
3721
3722         if (videobin[MMPLAYER_V_SINK].gst) {
3723                 GstPad *sink_pad = NULL;
3724                 sink_pad = gst_element_get_static_pad(videobin[MMPLAYER_V_SINK].gst, "sink");
3725                 if (sink_pad) {
3726                         _mmplayer_add_signal_connection(player, G_OBJECT(sink_pad), MM_PLAYER_SIGNAL_TYPE_VIDEOBIN,
3727                                         "notify::caps", G_CALLBACK(__mmplayer_gst_caps_notify_cb), (gpointer)player);
3728                         gst_object_unref(GST_OBJECT(sink_pad));
3729                 } else {
3730                         LOGE("failed to get sink pad from videosink");
3731                 }
3732         }
3733
3734         return MM_ERROR_NONE;
3735 }
3736
3737 /**
3738  * VIDEO BIN
3739  * - video overlay surface(arm/x86) : tizenwlsink
3740  */
3741 static int
3742 __mmplayer_gst_create_video_sink_bin(mmplayer_t *player, GstCaps *caps, MMDisplaySurfaceType surface_type)
3743 {
3744         GstPad *pad = NULL;
3745         GList *element_bucket = NULL;
3746         mmplayer_gst_element_t *first_element = NULL;
3747         mmplayer_gst_element_t *videobin = NULL;
3748         gchar *videosink_factory_name = NULL;
3749
3750         MMPLAYER_FENTER();
3751         MMPLAYER_RETURN_VAL_IF_FAIL(player && player->pipeline, MM_ERROR_PLAYER_NOT_INITIALIZED);
3752
3753         /* alloc handles */
3754         videobin = (mmplayer_gst_element_t *)g_try_malloc0(sizeof(mmplayer_gst_element_t) * MMPLAYER_V_NUM);
3755         if (!videobin)
3756                 return MM_ERROR_PLAYER_NO_FREE_SPACE;
3757
3758         player->pipeline->videobin = videobin;
3759
3760         /* create bin */
3761         videobin[MMPLAYER_V_BIN].id = MMPLAYER_V_BIN;
3762         videobin[MMPLAYER_V_BIN].gst = gst_bin_new("videobin");
3763         if (!videobin[MMPLAYER_V_BIN].gst) {
3764                 LOGE("failed to create videobin");
3765                 goto ERROR;
3766         }
3767
3768         if (__mmplayer_gst_create_video_filters(player, surface_type, &element_bucket) != MM_ERROR_NONE)
3769                 goto ERROR;
3770
3771         videosink_factory_name = __mmplayer_get_videosink_factory_name(player, surface_type);
3772         MMPLAYER_CREATE_ELEMENT(videobin, MMPLAYER_V_SINK, videosink_factory_name, "videosink", element_bucket, player);
3773
3774         /* additional setting for sink plug-in */
3775         if (__mmplayer_gst_set_videosink_property(player, surface_type) != MM_ERROR_NONE) {
3776                 LOGE("failed to set video property");
3777                 goto ERROR;
3778         }
3779
3780         /* store it as it's sink element */
3781         __mmplayer_add_sink(player, videobin[MMPLAYER_V_SINK].gst, TRUE);
3782
3783         /* adding created elements to bin */
3784         if (!_mmplayer_gst_element_add_bucket_to_bin(GST_BIN(videobin[MMPLAYER_V_BIN].gst), element_bucket)) {
3785                 LOGE("failed to add elements");
3786                 goto ERROR;
3787         }
3788
3789         /* Linking elements in the bucket by added order */
3790         if (_mmplayer_gst_element_link_bucket(element_bucket) == -1) {
3791                 LOGE("failed to link elements");
3792                 goto ERROR;
3793         }
3794
3795         /* get first element's sinkpad for creating ghostpad */
3796         first_element = (mmplayer_gst_element_t *)element_bucket->data;
3797         if (!first_element) {
3798                 LOGE("failed to get first element from bucket");
3799                 goto ERROR;
3800         }
3801
3802         pad = gst_element_get_static_pad(GST_ELEMENT(first_element->gst), "sink");
3803         if (!pad) {
3804                 LOGE("failed to get pad from first element");
3805                 goto ERROR;
3806         }
3807
3808         /* create ghostpad */
3809         player->ghost_pad_for_videobin = gst_ghost_pad_new("sink", pad);
3810         if (!gst_element_add_pad(videobin[MMPLAYER_V_BIN].gst, player->ghost_pad_for_videobin)) {
3811                 LOGE("failed to add ghostpad to videobin");
3812                 goto ERROR;
3813         }
3814         gst_object_unref(pad);
3815
3816         /* done. free allocated variables */
3817         g_list_free(element_bucket);
3818
3819         MMPLAYER_FLEAVE();
3820
3821         return MM_ERROR_NONE;
3822
3823 ERROR:
3824         LOGE("ERROR : releasing videobin");
3825         g_list_free(element_bucket);
3826
3827         if (pad)
3828                 gst_object_unref(GST_OBJECT(pad));
3829
3830         /* release videobin with it's children */
3831         if (videobin[MMPLAYER_V_BIN].gst)
3832                 gst_object_unref(GST_OBJECT(videobin[MMPLAYER_V_BIN].gst));
3833
3834         MMPLAYER_FREEIF(videobin);
3835         player->pipeline->videobin = NULL;
3836
3837         return MM_ERROR_PLAYER_INTERNAL;
3838 }
3839
3840 static int
3841 __mmplayer_gst_create_plain_text_elements(mmplayer_t *player)
3842 {
3843         GList *element_bucket = NULL;
3844         mmplayer_gst_element_t *textbin = player->pipeline->textbin;
3845
3846         MMPLAYER_CREATE_ELEMENT(textbin, MMPLAYER_T_QUEUE, "queue", "text_queue", element_bucket, player);
3847         MMPLAYER_CREATE_ELEMENT(textbin, MMPLAYER_T_IDENTITY, "identity", "text_identity", element_bucket, player);
3848         g_object_set(G_OBJECT(textbin[MMPLAYER_T_IDENTITY].gst),
3849                                                         "signal-handoffs", FALSE,
3850                                                         NULL);
3851
3852         MMPLAYER_CREATE_ELEMENT(textbin, MMPLAYER_T_FAKE_SINK, "fakesink", "text_fakesink", element_bucket, player);
3853         _mmplayer_add_signal_connection(player,
3854                                                         G_OBJECT(textbin[MMPLAYER_T_FAKE_SINK].gst),
3855                                                         MM_PLAYER_SIGNAL_TYPE_TEXTBIN,
3856                                                         "handoff",
3857                                                         G_CALLBACK(__mmplayer_update_subtitle),
3858                                                         (gpointer)player);
3859
3860         g_object_set(G_OBJECT(textbin[MMPLAYER_T_FAKE_SINK].gst), "sync", TRUE,
3861                                                 "signal-handoffs", TRUE, "max-lateness", FAKE_SINK_MAX_LATENESS, NULL);
3862
3863         if (!player->play_subtitle) {
3864                 LOGD("add textbin sink as sink element of whole pipeline.");
3865                 __mmplayer_add_sink(player, GST_ELEMENT(textbin[MMPLAYER_T_FAKE_SINK].gst), FALSE);
3866         }
3867
3868         /* adding created elements to bin */
3869         LOGD("adding created elements to bin");
3870         if (!_mmplayer_gst_element_add_bucket_to_bin(GST_BIN(textbin[MMPLAYER_T_BIN].gst), element_bucket)) {
3871                 LOGE("failed to add elements");
3872                 goto ERROR;
3873         }
3874
3875         /* unset sink flag from textbin. not to hold eos when video data is shorter than subtitle */
3876         GST_OBJECT_FLAG_UNSET(textbin[MMPLAYER_T_BIN].gst, GST_ELEMENT_FLAG_SINK);
3877         GST_OBJECT_FLAG_UNSET(textbin[MMPLAYER_T_FAKE_SINK].gst, GST_ELEMENT_FLAG_SINK);
3878
3879         /* linking elements in the bucket by added order. */
3880         LOGD("Linking elements in the bucket by added order.");
3881         if (_mmplayer_gst_element_link_bucket(element_bucket) == -1) {
3882                 LOGE("failed to link elements");
3883                 goto ERROR;
3884         }
3885
3886         if (textbin[MMPLAYER_T_QUEUE].gst) {
3887                 GstPad *pad = NULL;
3888                 GstPad *ghostpad = NULL;
3889
3890                 pad = gst_element_get_static_pad(GST_ELEMENT(textbin[MMPLAYER_T_QUEUE].gst), "sink");
3891                 if (!pad) {
3892                         LOGE("failed to get sink pad of text queue");
3893                         goto ERROR;
3894                 }
3895
3896                 ghostpad = gst_ghost_pad_new("text_sink", pad);
3897                 gst_object_unref(pad);
3898
3899                 if (!ghostpad) {
3900                         LOGE("failed to create ghostpad of textbin");
3901                         goto ERROR;
3902                 }
3903
3904                 if (!gst_element_add_pad(textbin[MMPLAYER_T_BIN].gst, ghostpad)) {
3905                         LOGE("failed to add ghostpad to textbin");
3906                         gst_object_unref(ghostpad);
3907                         goto ERROR;
3908                 }
3909         }
3910
3911         g_list_free(element_bucket);
3912
3913         return MM_ERROR_NONE;
3914
3915 ERROR:
3916
3917         g_list_free(element_bucket);
3918
3919         if (!player->play_subtitle && textbin[MMPLAYER_T_FAKE_SINK].gst) {
3920                 LOGE("remove textbin sink from sink list");
3921                 __mmplayer_del_sink(player, textbin[MMPLAYER_T_FAKE_SINK].gst);
3922         }
3923
3924         /* release element at __mmplayer_gst_create_text_sink_bin */
3925         return MM_ERROR_PLAYER_INTERNAL;
3926 }
3927
3928 static int
3929 __mmplayer_gst_create_text_sink_bin(mmplayer_t *player)
3930 {
3931         mmplayer_gst_element_t *textbin = NULL;
3932         int surface_type = 0;
3933         gint i = 0;
3934
3935         MMPLAYER_FENTER();
3936
3937         MMPLAYER_RETURN_VAL_IF_FAIL(player && player->pipeline, MM_ERROR_PLAYER_NOT_INITIALIZED);
3938
3939         /* alloc handles */
3940         textbin = (mmplayer_gst_element_t *)g_try_malloc0(sizeof(mmplayer_gst_element_t) * MMPLAYER_T_NUM);
3941         if (!textbin) {
3942                 LOGE("failed to allocate memory for textbin");
3943                 return MM_ERROR_PLAYER_NO_FREE_SPACE;
3944         }
3945
3946         /* create bin */
3947         textbin[MMPLAYER_T_BIN].id = MMPLAYER_T_BIN;
3948         textbin[MMPLAYER_T_BIN].gst = gst_bin_new("textbin");
3949         if (!textbin[MMPLAYER_T_BIN].gst) {
3950                 LOGE("failed to create textbin");
3951                 goto ERROR;
3952         }
3953
3954         /* take it */
3955         player->pipeline->textbin = textbin;
3956
3957         /* fakesink */
3958         mm_attrs_get_int_by_name(player->attrs, "display_surface_type", &surface_type);
3959         LOGD("surface type for subtitle : %d", surface_type);
3960         switch (surface_type) {
3961         case MM_DISPLAY_SURFACE_OVERLAY:
3962         case MM_DISPLAY_SURFACE_NULL:
3963         case MM_DISPLAY_SURFACE_REMOTE:
3964                 if (__mmplayer_gst_create_plain_text_elements(player) != MM_ERROR_NONE) {
3965                         LOGE("failed to make plain text elements");
3966                         goto ERROR;
3967                 }
3968                 break;
3969         default:
3970                 goto ERROR;
3971                 break;
3972         }
3973
3974         MMPLAYER_FLEAVE();
3975
3976         return MM_ERROR_NONE;
3977
3978 ERROR:
3979
3980         LOGD("ERROR : releasing textbin");
3981
3982         /* release signal */
3983         __mmplayer_release_signal_connection(player, MM_PLAYER_SIGNAL_TYPE_TEXTBIN);
3984
3985         /* release element which are not added to bin */
3986         for (i = 1; i < MMPLAYER_T_NUM; i++) {
3987                 /* NOTE : skip bin */
3988                 if (textbin[i].gst) {
3989                         GstObject *parent = NULL;
3990                         parent = gst_element_get_parent(textbin[i].gst);
3991
3992                         if (!parent) {
3993                                 gst_object_unref(GST_OBJECT(textbin[i].gst));
3994                                 textbin[i].gst = NULL;
3995                         } else {
3996                                 gst_object_unref(GST_OBJECT(parent));
3997                         }
3998                 }
3999         }
4000
4001         /* release textbin with it's children */
4002         if (textbin[MMPLAYER_T_BIN].gst)
4003                 gst_object_unref(GST_OBJECT(textbin[MMPLAYER_T_BIN].gst));
4004
4005         MMPLAYER_FREEIF(textbin);
4006         player->pipeline->textbin = NULL;
4007
4008         MMPLAYER_FLEAVE();
4009         return MM_ERROR_PLAYER_INTERNAL;
4010 }
4011
4012 static int
4013 __mmplayer_gst_create_text_pipeline(mmplayer_t *player)
4014 {
4015         mmplayer_gst_element_t *mainbin = NULL;
4016         mmplayer_gst_element_t *textbin = NULL;
4017         MMHandleType attrs = 0;
4018         GstElement *subsrc = NULL;
4019         GstElement *subparse = NULL;
4020         gchar *subtitle_uri = NULL;
4021         const gchar *charset = NULL;
4022         GstPad *pad = NULL;
4023
4024         MMPLAYER_FENTER();
4025
4026         /* get mainbin */
4027         MMPLAYER_RETURN_VAL_IF_FAIL(player &&
4028                                                                 player->pipeline &&
4029                                                                 player->pipeline->mainbin, MM_ERROR_PLAYER_NOT_INITIALIZED);
4030
4031         mainbin = player->pipeline->mainbin;
4032
4033         attrs = MMPLAYER_GET_ATTRS(player);
4034         if (!attrs) {
4035                 LOGE("cannot get content attribute");
4036                 return MM_ERROR_PLAYER_INTERNAL;
4037         }
4038
4039         mm_attrs_get_string_by_name(attrs, "subtitle_uri", &subtitle_uri);
4040         if (!subtitle_uri || strlen(subtitle_uri) < 1) {
4041                 LOGE("subtitle uri is not proper filepath.");
4042                 return MM_ERROR_PLAYER_INVALID_URI;
4043         }
4044
4045         if (!_mmplayer_get_storage_info(subtitle_uri, &player->storage_info[MMPLAYER_PATH_TEXT])) {
4046                 LOGE("failed to get storage info of subtitle path");
4047                 return MM_ERROR_PLAYER_INVALID_URI;
4048         }
4049
4050         SECURE_LOGD("subtitle file path is [%s].", subtitle_uri);
4051
4052         MMPLAYER_SUBTITLE_INFO_LOCK(player);
4053         player->subtitle_language_list = NULL;
4054         MMPLAYER_SUBTITLE_INFO_UNLOCK(player);
4055
4056         /* create the subtitle source */
4057         subsrc = gst_element_factory_make("filesrc", "subtitle_source");
4058         if (!subsrc) {
4059                 LOGE("failed to create filesrc element");
4060                 goto ERROR;
4061         }
4062         g_object_set(G_OBJECT(subsrc), "location", subtitle_uri, NULL);
4063
4064         mainbin[MMPLAYER_M_SUBSRC].id = MMPLAYER_M_SUBSRC;
4065         mainbin[MMPLAYER_M_SUBSRC].gst = subsrc;
4066
4067         if (!gst_bin_add(GST_BIN(mainbin[MMPLAYER_M_PIPE].gst), subsrc)) {
4068                 LOGW("failed to add queue");
4069                 gst_object_unref(mainbin[MMPLAYER_M_SUBSRC].gst);
4070                 mainbin[MMPLAYER_M_SUBSRC].gst = NULL;
4071                 mainbin[MMPLAYER_M_SUBSRC].id = MMPLAYER_M_NUM;
4072                 goto ERROR;
4073         }
4074
4075         /* subparse */
4076         subparse = gst_element_factory_make("subparse", "subtitle_parser");
4077         if (!subparse) {
4078                 LOGE("failed to create subparse element");
4079                 goto ERROR;
4080         }
4081
4082         charset = _mmplayer_get_charset(subtitle_uri);
4083         if (charset) {
4084                 LOGD("detected charset is %s", charset);
4085                 g_object_set(G_OBJECT(subparse), "subtitle-encoding", charset, NULL);
4086         }
4087
4088         mainbin[MMPLAYER_M_SUBPARSE].id = MMPLAYER_M_SUBPARSE;
4089         mainbin[MMPLAYER_M_SUBPARSE].gst = subparse;
4090
4091         if (!gst_bin_add(GST_BIN(mainbin[MMPLAYER_M_PIPE].gst), subparse)) {
4092                 LOGW("failed to add subparse");
4093                 gst_object_unref(mainbin[MMPLAYER_M_SUBPARSE].gst);
4094                 mainbin[MMPLAYER_M_SUBPARSE].gst = NULL;
4095                 mainbin[MMPLAYER_M_SUBPARSE].id = MMPLAYER_M_NUM;
4096                 goto ERROR;
4097         }
4098
4099         if (!gst_element_link_pads(subsrc, "src", subparse, "sink")) {
4100                 LOGW("failed to link subsrc and subparse");
4101                 goto ERROR;
4102         }
4103
4104         player->play_subtitle = TRUE;
4105         player->adjust_subtitle_pos = 0;
4106
4107         LOGD("play subtitle using subtitle file");
4108
4109         if (player->pipeline->textbin == NULL) {
4110                 if (MM_ERROR_NONE != __mmplayer_gst_create_text_sink_bin(player)) {
4111                         LOGE("failed to create text sink bin. continuing without text");
4112                         goto ERROR;
4113                 }
4114
4115                 textbin = player->pipeline->textbin;
4116
4117                 if (!gst_bin_add(GST_BIN(mainbin[MMPLAYER_M_PIPE].gst), GST_ELEMENT(textbin[MMPLAYER_T_BIN].gst))) {
4118                         LOGW("failed to add textbin");
4119
4120                         /* release signal */
4121                         __mmplayer_release_signal_connection(player, MM_PLAYER_SIGNAL_TYPE_TEXTBIN);
4122
4123                         /* release textbin with it's children */
4124                         gst_object_unref(GST_OBJECT(textbin[MMPLAYER_T_BIN].gst));
4125                         MMPLAYER_FREEIF(player->pipeline->textbin);
4126                         player->pipeline->textbin = textbin = NULL;
4127                         goto ERROR;
4128                 }
4129
4130                 LOGD("link text input selector and textbin ghost pad");
4131
4132                 player->textsink_linked = 1;
4133                 player->external_text_idx = 0;
4134                 LOGI("textsink is linked");
4135         } else {
4136                 textbin = player->pipeline->textbin;
4137                 LOGD("text bin has been created. reuse it.");
4138                 player->external_text_idx = 1;
4139         }
4140
4141         if (!gst_element_link_pads(subparse, "src", textbin[MMPLAYER_T_BIN].gst, "text_sink")) {
4142                 LOGW("failed to link subparse and textbin");
4143                 goto ERROR;
4144         }
4145
4146         pad = gst_element_get_static_pad(textbin[MMPLAYER_T_FAKE_SINK].gst, "sink");
4147         if (!pad) {
4148                 LOGE("failed to get sink pad from textsink to probe data");
4149                 goto ERROR;
4150         }
4151
4152         gst_pad_add_probe(pad, GST_PAD_PROBE_TYPE_BUFFER,
4153                                 __mmplayer_subtitle_adjust_position_probe, player, NULL);
4154
4155         gst_object_unref(pad);
4156         pad = NULL;
4157
4158         /* create dot. for debugging */
4159         MMPLAYER_GENERATE_DOT_IF_ENABLED(player, "pipeline-with-subtitle");
4160         MMPLAYER_FLEAVE();
4161
4162         return MM_ERROR_NONE;
4163
4164 ERROR:
4165         /* release text pipeline resource */
4166         player->textsink_linked = 0;
4167
4168         /* release signal */
4169         __mmplayer_release_signal_connection(player, MM_PLAYER_SIGNAL_TYPE_TEXTBIN);
4170
4171         if (player->pipeline->textbin) {
4172                 LOGE("remove textbin");
4173
4174                 /* release textbin with it's children */
4175                 MMPLAYER_RELEASE_ELEMENT(player, player->pipeline->textbin, MMPLAYER_T_BIN);
4176                 MMPLAYER_FREEIF(player->pipeline->textbin);
4177                 player->pipeline->textbin = NULL;
4178
4179         }
4180
4181         /* release subtitle elem */
4182         MMPLAYER_RELEASE_ELEMENT(player, player->pipeline->mainbin, MMPLAYER_M_SUBSRC);
4183         MMPLAYER_RELEASE_ELEMENT(player, player->pipeline->mainbin, MMPLAYER_M_SUBPARSE);
4184
4185         return MM_ERROR_PLAYER_INTERNAL;
4186 }
4187
4188 gboolean
4189 __mmplayer_update_subtitle(GstElement *object, GstBuffer *buffer, GstPad *pad, gpointer data)
4190 {
4191         mmplayer_t *player = (mmplayer_t *)data;
4192         MMMessageParamType msg = {0, };
4193         GstClockTime duration = 0;
4194         gpointer text = NULL;
4195         guint text_size = 0;
4196         gboolean ret = TRUE;
4197         GstMapInfo mapinfo = GST_MAP_INFO_INIT;
4198
4199         MMPLAYER_FENTER();
4200
4201         MMPLAYER_RETURN_VAL_IF_FAIL(player, FALSE);
4202         MMPLAYER_RETURN_VAL_IF_FAIL(buffer, FALSE);
4203
4204         if (player->is_subtitle_force_drop) {
4205                 LOGW("subtitle is dropped forcedly.");
4206                 return ret;
4207         }
4208
4209         gst_buffer_map(buffer, &mapinfo, GST_MAP_READ);
4210         text = mapinfo.data;
4211         text_size = mapinfo.size;
4212
4213         if (player->set_mode.subtitle_off) {
4214                 LOGD("subtitle is OFF.");
4215                 return TRUE;
4216         }
4217
4218         if (!text || (text_size == 0)) {
4219                 LOGD("There is no subtitle to be displayed.");
4220                 return TRUE;
4221         }
4222
4223         msg.data = (void *)text;
4224
4225         duration = GST_BUFFER_DURATION(buffer);
4226
4227         if (!GST_CLOCK_TIME_IS_VALID(duration)) {
4228                 if (player->duration > GST_BUFFER_PTS(buffer))
4229                         duration = player->duration - GST_BUFFER_PTS(buffer);
4230                 else
4231                         duration = 0;
4232                 LOGI("subtitle duration is invalid, subtitle duration change "
4233                         "GST_CLOCK_TIME_NONE -> %" GST_TIME_FORMAT, GST_TIME_ARGS(duration));
4234         }
4235         msg.subtitle.duration = GST_TIME_AS_MSECONDS(duration);
4236
4237         LOGD("update subtitle : [%ld msec] %s", msg.subtitle.duration, (char *)msg.data);
4238
4239         MMPLAYER_POST_MSG(player, MM_MESSAGE_UPDATE_SUBTITLE, &msg);
4240         gst_buffer_unmap(buffer, &mapinfo);
4241
4242         MMPLAYER_FLEAVE();
4243
4244         return ret;
4245 }
4246
4247 static GstPadProbeReturn
4248 __mmplayer_subtitle_adjust_position_probe(GstPad *pad, GstPadProbeInfo *info, gpointer u_data)
4249 {
4250         mmplayer_t *player = (mmplayer_t *)u_data;
4251         GstClockTime cur_timestamp = 0;
4252         gint64 adjusted_timestamp = 0;
4253         GstBuffer *buffer = gst_pad_probe_info_get_buffer(info);
4254
4255         MMPLAYER_RETURN_VAL_IF_FAIL(player, FALSE);
4256
4257         if (player->set_mode.subtitle_off) {
4258                 LOGD("subtitle is OFF.");
4259                 return TRUE;
4260         }
4261
4262         if (player->adjust_subtitle_pos == 0) {
4263                 LOGD("nothing to do");
4264                 return TRUE;
4265         }
4266
4267         cur_timestamp = GST_BUFFER_TIMESTAMP(buffer);
4268         adjusted_timestamp = (gint64)cur_timestamp + ((gint64)player->adjust_subtitle_pos * G_GINT64_CONSTANT(1000000));
4269
4270         if (adjusted_timestamp < 0) {
4271                 LOGD("adjusted_timestamp under zero");
4272                 MMPLAYER_FLEAVE();
4273                 return FALSE;
4274         }
4275
4276         GST_BUFFER_TIMESTAMP(buffer) = (GstClockTime) adjusted_timestamp;
4277         LOGD("buffer timestamp changed %" GST_TIME_FORMAT " -> %" GST_TIME_FORMAT "",
4278                                 GST_TIME_ARGS(cur_timestamp),
4279                                 GST_TIME_ARGS(GST_BUFFER_TIMESTAMP(buffer)));
4280
4281         return GST_PAD_PROBE_OK;
4282 }
4283
4284 static int
4285 __mmplayer_gst_adjust_subtitle_position(mmplayer_t *player, int position)
4286 {
4287         MMPLAYER_FENTER();
4288
4289         /* check player and subtitlebin are created */
4290         MMPLAYER_RETURN_VAL_IF_FAIL(player && player->pipeline, MM_ERROR_PLAYER_NOT_INITIALIZED);
4291         MMPLAYER_RETURN_VAL_IF_FAIL(player->play_subtitle, MM_ERROR_NOT_SUPPORT_API);
4292
4293         if (position == 0) {
4294                 LOGD("nothing to do");
4295                 MMPLAYER_FLEAVE();
4296                 return MM_ERROR_NONE;
4297         }
4298
4299         /* check current position */
4300         player->adjust_subtitle_pos = position;
4301
4302         LOGD("save adjust_subtitle_pos in player");
4303
4304         MMPLAYER_FLEAVE();
4305
4306         return MM_ERROR_NONE;
4307 }
4308
4309 /**
4310  * This function is to create  audio or video pipeline for playing.
4311  *
4312  * @param       player          [in]    handle of player
4313  *
4314  * @return      This function returns zero on success.
4315  * @remark
4316  * @see
4317  */
4318 static int
4319 __mmplayer_gst_create_pipeline(mmplayer_t *player)
4320 {
4321         int ret = MM_ERROR_NONE;
4322         mmplayer_gst_element_t *mainbin = NULL;
4323         MMHandleType attrs = 0;
4324
4325         MMPLAYER_FENTER();
4326         MMPLAYER_RETURN_VAL_IF_FAIL(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
4327
4328         /* get profile attribute */
4329         attrs = MMPLAYER_GET_ATTRS(player);
4330         if (!attrs) {
4331                 LOGE("failed to get content attribute");
4332                 goto INIT_ERROR;
4333         }
4334
4335         /* create pipeline handles */
4336         if (player->pipeline) {
4337                 LOGE("pipeline should be released before create new one");
4338                 goto INIT_ERROR;
4339         }
4340
4341         player->pipeline = (mmplayer_pipeline_info_t *)g_malloc0(sizeof(mmplayer_pipeline_info_t));
4342
4343         /* create mainbin */
4344         mainbin = (mmplayer_gst_element_t *)g_try_malloc0(sizeof(mmplayer_gst_element_t) * MMPLAYER_M_NUM);
4345         if (mainbin == NULL)
4346                 goto INIT_ERROR;
4347
4348         /* create pipeline */
4349         mainbin[MMPLAYER_M_PIPE].id = MMPLAYER_M_PIPE;
4350         mainbin[MMPLAYER_M_PIPE].gst = gst_pipeline_new("player");
4351         if (!mainbin[MMPLAYER_M_PIPE].gst) {
4352                 LOGE("failed to create pipeline");
4353                 g_free(mainbin);
4354                 goto INIT_ERROR;
4355         }
4356
4357         player->pipeline->mainbin = mainbin;
4358
4359         /* create the source and decoder elements */
4360         if (MMPLAYER_IS_MS_BUFF_SRC(player)) {
4361                 ret = _mmplayer_gst_build_es_pipeline(player);
4362         } else {
4363                 if (MMPLAYER_USE_DECODEBIN(player))
4364                         ret = _mmplayer_gst_build_pipeline(player); /* TEMP: previous pipeline, will be removed.*/
4365                 else
4366                         ret = _mmplayer_gst_build_pipeline_with_src(player);
4367         }
4368
4369         if (ret != MM_ERROR_NONE) {
4370                 LOGE("failed to create some elements");
4371                 goto INIT_ERROR;
4372         }
4373
4374         /* Note : check whether subtitle attribute uri is set. If uri is set, then try to play subtitle file */
4375         if (__mmplayer_check_subtitle(player)
4376                 && (__mmplayer_gst_create_text_pipeline(player) != MM_ERROR_NONE))
4377                 LOGE("failed to create text pipeline");
4378
4379         /* add bus watch */
4380         ret = _mmplayer_gst_add_bus_watch(player);
4381         if (ret != MM_ERROR_NONE) {
4382                 LOGE("failed to add bus watch");
4383                 goto INIT_ERROR;
4384         }
4385
4386         MMPLAYER_FLEAVE();
4387         return MM_ERROR_NONE;
4388
4389 INIT_ERROR:
4390         _mmplayer_bus_watcher_remove(player);
4391         __mmplayer_gst_destroy_pipeline(player);
4392         return MM_ERROR_PLAYER_INTERNAL;
4393 }
4394
4395 static void
4396 __mmplayer_reset_gapless_state(mmplayer_t *player)
4397 {
4398         MMPLAYER_FENTER();
4399         MMPLAYER_RETURN_IF_FAIL(player
4400                 && player->pipeline
4401                 && player->pipeline->audiobin
4402                 && player->pipeline->audiobin[MMPLAYER_A_BIN].gst);
4403
4404         memset(&player->gapless, 0, sizeof(mmplayer_gapless_t));
4405
4406         MMPLAYER_FLEAVE();
4407         return;
4408 }
4409
4410 static int
4411 __mmplayer_gst_destroy_pipeline(mmplayer_t *player)
4412 {
4413         gint timeout = 0;
4414         int ret = MM_ERROR_NONE;
4415
4416         MMPLAYER_FENTER();
4417
4418         MMPLAYER_RETURN_VAL_IF_FAIL(player, MM_ERROR_INVALID_HANDLE);
4419
4420         /* cleanup stuffs */
4421         MMPLAYER_FREEIF(player->type_caps_str);
4422         player->no_more_pad = FALSE;
4423         player->num_dynamic_pad = 0;
4424
4425         MMPLAYER_SUBTITLE_INFO_LOCK(player);
4426         player->subtitle_language_list = NULL;
4427         MMPLAYER_SUBTITLE_INFO_UNLOCK(player);
4428
4429         MMPLAYER_RECONFIGURE_LOCK(player);
4430         __mmplayer_reset_gapless_state(player);
4431         MMPLAYER_RECONFIGURE_UNLOCK(player);
4432
4433         if (player->streamer) {
4434                 _mm_player_streaming_initialize(player->streamer, FALSE);
4435                 _mm_player_streaming_destroy(player->streamer);
4436                 player->streamer = NULL;
4437         }
4438
4439         /* cleanup unlinked mime type */
4440         MMPLAYER_FREEIF(player->unlinked_audio_mime);
4441         MMPLAYER_FREEIF(player->unlinked_video_mime);
4442         MMPLAYER_FREEIF(player->unlinked_demuxer_mime);
4443
4444         /* cleanup running stuffs */
4445         _mmplayer_cancel_eos_timer(player);
4446
4447         /* cleanup gst stuffs */
4448         if (player->pipeline) {
4449                 mmplayer_gst_element_t *mainbin = player->pipeline->mainbin;
4450                 GstTagList *tag_list = player->pipeline->tag_list;
4451
4452                 /* first we need to disconnect all signal hander */
4453                 __mmplayer_release_signal_connection(player, MM_PLAYER_SIGNAL_TYPE_ALL);
4454
4455                 if (mainbin) {
4456                         GstBus *bus = gst_pipeline_get_bus(GST_PIPELINE(mainbin[MMPLAYER_M_PIPE].gst));
4457                         gst_bus_set_sync_handler(bus, NULL, NULL, NULL);
4458                         gst_object_unref(bus);
4459
4460                         timeout = MMPLAYER_STATE_CHANGE_TIMEOUT(player);
4461                         ret = _mmplayer_gst_set_state(player, mainbin[MMPLAYER_M_PIPE].gst, GST_STATE_NULL, FALSE, timeout);
4462                         if (ret != MM_ERROR_NONE) {
4463                                 LOGE("fail to change state to NULL");
4464                                 return MM_ERROR_PLAYER_INTERNAL;
4465                         }
4466
4467                         LOGW("succeeded in changing state to NULL");
4468
4469                         gst_object_unref(GST_OBJECT(mainbin[MMPLAYER_M_PIPE].gst));
4470
4471                         /* free fakesink */
4472                         if (mainbin[MMPLAYER_M_SRC_FAKESINK].gst)
4473                                 gst_object_unref(GST_OBJECT(mainbin[MMPLAYER_M_SRC_FAKESINK].gst));
4474
4475                         MMPLAYER_FREEIF(player->pipeline->audiobin);
4476                         MMPLAYER_FREEIF(player->pipeline->videobin);
4477                         MMPLAYER_FREEIF(player->pipeline->textbin);
4478                         MMPLAYER_FREEIF(mainbin);
4479                 }
4480
4481                 if (tag_list)
4482                         gst_tag_list_unref(tag_list);
4483
4484                 MMPLAYER_FREEIF(player->pipeline);
4485         }
4486         MMPLAYER_FREEIF(player->album_art);
4487
4488         if (player->v_stream_caps) {
4489                 gst_caps_unref(player->v_stream_caps);
4490                 player->v_stream_caps = NULL;
4491         }
4492
4493         if (player->a_stream_caps) {
4494                 gst_caps_unref(player->a_stream_caps);
4495                 player->a_stream_caps = NULL;
4496         }
4497
4498         if (player->s_stream_caps) {
4499                 gst_caps_unref(player->s_stream_caps);
4500                 player->s_stream_caps = NULL;
4501         }
4502         _mmplayer_track_destroy(player);
4503
4504         if (player->sink_elements)
4505                 g_list_free(player->sink_elements);
4506         player->sink_elements = NULL;
4507
4508         if (player->bufmgr) {
4509                 tbm_bufmgr_deinit(player->bufmgr);
4510                 player->bufmgr = NULL;
4511         }
4512
4513         LOGW("finished destroy pipeline");
4514
4515         MMPLAYER_FLEAVE();
4516
4517         return ret;
4518 }
4519
4520 static int
4521 __mmplayer_gst_realize(mmplayer_t *player)
4522 {
4523         gint timeout = 0;
4524         int ret = MM_ERROR_NONE;
4525
4526         MMPLAYER_FENTER();
4527
4528         MMPLAYER_RETURN_VAL_IF_FAIL(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
4529
4530         MMPLAYER_PENDING_STATE(player) = MM_PLAYER_STATE_READY;
4531
4532         ret = __mmplayer_gst_create_pipeline(player);
4533         if (ret) {
4534                 LOGE("failed to create pipeline");
4535                 return ret;
4536         }
4537
4538         /* set pipeline state to READY */
4539         /* NOTE : state change to READY must be performed sync. */
4540         timeout = MMPLAYER_STATE_CHANGE_TIMEOUT(player);
4541         ret = _mmplayer_gst_set_state(player,
4542                                 player->pipeline->mainbin[MMPLAYER_M_PIPE].gst, GST_STATE_READY, FALSE, timeout);
4543
4544         if (ret != MM_ERROR_NONE) {
4545                 /* return error if failed to set state */
4546                 LOGE("failed to set READY state");
4547                 return ret;
4548         }
4549
4550         MMPLAYER_SET_STATE(player, MM_PLAYER_STATE_READY);
4551
4552         /* create dot before error-return. for debugging */
4553         MMPLAYER_GENERATE_DOT_IF_ENABLED(player, "pipeline-status-realize");
4554
4555         MMPLAYER_FLEAVE();
4556
4557         return ret;
4558 }
4559
4560 static int
4561 __mmplayer_gst_unrealize(mmplayer_t *player)
4562 {
4563         int ret = MM_ERROR_NONE;
4564
4565         MMPLAYER_FENTER();
4566
4567         MMPLAYER_RETURN_VAL_IF_FAIL(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
4568
4569         MMPLAYER_PENDING_STATE(player) = MM_PLAYER_STATE_NULL;
4570         MMPLAYER_PRINT_STATE(player);
4571
4572         /* release miscellaneous information */
4573         __mmplayer_release_misc(player);
4574
4575         /* destroy pipeline */
4576         ret = __mmplayer_gst_destroy_pipeline(player);
4577         if (ret != MM_ERROR_NONE) {
4578                 LOGE("failed to destroy pipeline");
4579                 return ret;
4580         }
4581
4582         /* release miscellaneous information.
4583            these info needs to be released after pipeline is destroyed. */
4584         __mmplayer_release_misc_post(player);
4585
4586         MMPLAYER_SET_STATE(player, MM_PLAYER_STATE_NULL);
4587
4588         MMPLAYER_FLEAVE();
4589
4590         return ret;
4591 }
4592
4593 static int
4594 __mmplayer_gst_set_message_callback(mmplayer_t *player, MMMessageCallback callback, gpointer user_param)
4595 {
4596         MMPLAYER_FENTER();
4597
4598         if (!player) {
4599                 LOGW("set_message_callback is called with invalid player handle");
4600                 return MM_ERROR_PLAYER_NOT_INITIALIZED;
4601         }
4602
4603         player->msg_cb = callback;
4604         player->msg_cb_param = user_param;
4605
4606         LOGD("msg_cb : %p     msg_cb_param : %p", callback, user_param);
4607
4608         MMPLAYER_FLEAVE();
4609
4610         return MM_ERROR_NONE;
4611 }
4612
4613 int
4614 _mmplayer_parse_profile(const char *uri, void *param, mmplayer_parse_profile_t *data)
4615 {
4616         int ret = MM_ERROR_NONE;
4617         char *path = NULL;
4618
4619         MMPLAYER_FENTER();
4620
4621         MMPLAYER_RETURN_VAL_IF_FAIL(uri, MM_ERROR_PLAYER_INVALID_URI);
4622         MMPLAYER_RETURN_VAL_IF_FAIL(data, MM_ERROR_PLAYER_INTERNAL);
4623         MMPLAYER_RETURN_VAL_IF_FAIL((strlen(uri) <= MM_MAX_URL_LEN), MM_ERROR_PLAYER_INVALID_URI);
4624
4625         memset(data, 0, sizeof(mmplayer_parse_profile_t));
4626
4627         if (strstr(uri, "es_buff://")) {
4628                 __mmplayer_copy_uri_and_set_type(data, uri, MM_PLAYER_URI_TYPE_MS_BUFF);
4629         } else if (strstr(uri, "rtsp://") || strstr(uri, "rtsps://") || strstr(uri, "rtspu://")) {
4630                 __mmplayer_copy_uri_and_set_type(data, uri, MM_PLAYER_URI_TYPE_URL_RTSP);
4631         } else if (strstr(uri, "http://") || strstr(uri, "https://")) {
4632                 gchar *tmp = NULL;
4633                 tmp = g_ascii_strdown(uri, strlen(uri));
4634                 if (tmp && (g_str_has_suffix(tmp, ".ism/manifest") || g_str_has_suffix(tmp, ".isml/manifest")))
4635                         __mmplayer_copy_uri_and_set_type(data, uri, MM_PLAYER_URI_TYPE_SS);
4636                 else
4637                         __mmplayer_copy_uri_and_set_type(data, uri, MM_PLAYER_URI_TYPE_URL_HTTP);
4638                 g_free(tmp);
4639         } else if (strstr(uri, "mms://")) {
4640                 __mmplayer_copy_uri_and_set_type(data, uri, MM_PLAYER_URI_TYPE_URL_MMS);
4641         } else if ((path = strstr(uri, "mem://"))) {
4642                 ret = __mmplayer_set_mem_uri(data, path, param);
4643         } else {
4644                 ret = __mmplayer_set_file_uri(data, uri);
4645         }
4646
4647         if (data->uri_type == MM_PLAYER_URI_TYPE_NONE)
4648                 ret = MM_ERROR_PLAYER_FILE_NOT_FOUND;
4649         else if (data->uri_type == MM_PLAYER_URI_TYPE_NO_PERMISSION)
4650                 ret = MM_ERROR_PLAYER_PERMISSION_DENIED;
4651
4652         /* dump parse result */
4653         SECURE_LOGW("incoming uri : %s", uri);
4654         LOGD("uri_type : %d, mem : %p, mem_size : %d, urgent : %s",
4655                 data->uri_type, data->input_mem.buf, data->input_mem.len, data->urgent);
4656
4657         MMPLAYER_FLEAVE();
4658
4659         return ret;
4660 }
4661
4662 static int
4663 __resource_release_cb(mm_resource_manager_h rm, mm_resource_manager_res_h res,
4664                 void *user_data)
4665 {
4666         mmplayer_t *player = NULL;
4667         MMMessageParamType msg = {0, };
4668         gint64 pos = 0;
4669         mmplayer_resource_type_e res_idx = MMPLAYER_RESOURCE_TYPE_MAX;
4670
4671         MMPLAYER_FENTER();
4672
4673         if (!user_data) {
4674                 LOGE("user_data is null");
4675                 return TRUE;
4676         }
4677
4678         player = (mmplayer_t *)user_data;
4679
4680         if (!player->pipeline || !player->attrs) {
4681                 LOGW("not initialized");
4682                 return TRUE;
4683         }
4684
4685         LOGD("cmd lock player, cmd state : %d", player->cmd);
4686         MMPLAYER_CMD_LOCK(player);
4687         LOGD("cmd locked player");
4688
4689         if (MMPLAYER_CURRENT_STATE(player) == MM_PLAYER_STATE_NULL
4690                 || MMPLAYER_CURRENT_STATE(player) == MM_PLAYER_STATE_NONE) {
4691                 LOGW("player already destroyed");
4692                 MMPLAYER_CMD_UNLOCK(player);
4693                 return TRUE;
4694         }
4695
4696         player->interrupted_by_resource = TRUE;
4697
4698         MMPLAYER_POST_MSG(player, MM_MESSAGE_INTERRUPT_STARTED, NULL);
4699
4700         /* get last play position */
4701         if (_mmplayer_gst_get_position(player, &pos) == MM_ERROR_NONE) {
4702                 msg.union_type = MM_MSG_UNION_TIME;
4703                 msg.time.elapsed = pos;
4704                 MMPLAYER_POST_MSG(player, MM_MESSAGE_PLAY_POSITION, &msg);
4705         } else {
4706                 LOGW("failed to get play position.");
4707         }
4708
4709         LOGD("video resource conflict so, resource will be freed by unrealizing");
4710         if (_mmplayer_unrealize((MMHandleType)player) != MM_ERROR_NONE)
4711                 LOGE("failed to unrealize");
4712
4713         MMPLAYER_CMD_UNLOCK(player);
4714
4715         for (res_idx = MMPLAYER_RESOURCE_TYPE_VIDEO_DECODER; res_idx < MMPLAYER_RESOURCE_TYPE_MAX; res_idx++) {
4716                 player->hw_resource[res_idx] = NULL;
4717         }
4718
4719         MMPLAYER_FLEAVE();
4720         return TRUE; /* release all the resources */
4721 }
4722
4723 static void
4724 __mmplayer_initialize_video_roi(mmplayer_t *player)
4725 {
4726         player->video_roi.scale_x = 0.0;
4727         player->video_roi.scale_y = 0.0;
4728         player->video_roi.scale_width = 1.0;
4729         player->video_roi.scale_height = 1.0;
4730 }
4731
4732 int
4733 _mmplayer_create_player(MMHandleType handle)
4734 {
4735         int ret = MM_ERROR_PLAYER_INTERNAL;
4736         bool enabled = false;
4737
4738         mmplayer_t *player = MM_PLAYER_CAST(handle);
4739
4740         MMPLAYER_FENTER();
4741
4742         MMPLAYER_RETURN_VAL_IF_FAIL(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
4743
4744         /* initialize player state */
4745         MMPLAYER_CURRENT_STATE(player) = MM_PLAYER_STATE_NONE;
4746         MMPLAYER_PREV_STATE(player) = MM_PLAYER_STATE_NONE;
4747         MMPLAYER_PENDING_STATE(player) = MM_PLAYER_STATE_NONE;
4748         MMPLAYER_TARGET_STATE(player) = MM_PLAYER_STATE_NONE;
4749
4750         /* check current state */
4751         MMPLAYER_CHECK_STATE(player, MMPLAYER_COMMAND_CREATE);
4752
4753         /* construct attributes */
4754         player->attrs = _mmplayer_construct_attribute(handle);
4755
4756         if (!player->attrs) {
4757                 LOGE("Failed to construct attributes");
4758                 return ret;
4759         }
4760
4761         /* initialize gstreamer with configured parameter */
4762         if (!__mmplayer_init_gstreamer(player)) {
4763                 LOGE("Initializing gstreamer failed");
4764                 _mmplayer_deconstruct_attribute(handle);
4765                 return ret;
4766         }
4767
4768         /* create lock. note that g_tread_init() has already called in gst_init() */
4769         g_mutex_init(&player->fsink_lock);
4770
4771         /* create update tag lock */
4772         g_mutex_init(&player->update_tag_lock);
4773
4774         /* create gapless play mutex */
4775         g_mutex_init(&player->gapless_play_thread_mutex);
4776
4777         /* create gapless play cond */
4778         g_cond_init(&player->gapless_play_thread_cond);
4779
4780         /* create gapless play thread */
4781         player->gapless_play_thread =
4782                 g_thread_try_new("gapless_play_thread", __mmplayer_gapless_play_thread, (gpointer)player, NULL);
4783         if (!player->gapless_play_thread) {
4784                 LOGE("failed to create gapless play thread");
4785                 ret = MM_ERROR_PLAYER_RESOURCE_LIMIT;
4786                 g_mutex_clear(&player->gapless_play_thread_mutex);
4787                 g_cond_clear(&player->gapless_play_thread_cond);
4788                 goto ERROR;
4789         }
4790
4791         player->bus_msg_q = g_queue_new();
4792         if (!player->bus_msg_q) {
4793                 LOGE("failed to create queue for bus_msg");
4794                 ret = MM_ERROR_PLAYER_RESOURCE_LIMIT;
4795                 goto ERROR;
4796         }
4797
4798         ret = _mmplayer_initialize_video_capture(player);
4799         if (ret != MM_ERROR_NONE) {
4800                 LOGW("video capture is not supported");
4801                 /* do not handle as error for headless profile */
4802         }
4803
4804         /* initialize resource manager */
4805         if (mm_resource_manager_create(MM_RESOURCE_MANAGER_APP_CLASS_MEDIA,
4806                 __resource_release_cb, player, &player->resource_manager)
4807                 != MM_RESOURCE_MANAGER_ERROR_NONE) {
4808                 LOGE("failed to create resource manager");
4809                 ret = MM_ERROR_PLAYER_INTERNAL;
4810                 goto ERROR;
4811         }
4812
4813         /* create video bo lock and cond */
4814         g_mutex_init(&player->video_bo_mutex);
4815         g_cond_init(&player->video_bo_cond);
4816
4817         /* create subtitle info lock and cond */
4818         g_mutex_init(&player->subtitle_info_mutex);
4819         g_cond_init(&player->subtitle_info_cond);
4820
4821         player->streaming_type = STREAMING_SERVICE_NONE;
4822
4823         /* give default value of audio effect setting */
4824         player->sound.volume = MM_VOLUME_FACTOR_DEFAULT;
4825         player->sound.rg_enable = false;
4826         player->playback_rate = DEFAULT_PLAYBACK_RATE;
4827
4828         player->play_subtitle = FALSE;
4829         player->has_closed_caption = FALSE;
4830         player->pending_resume = FALSE;
4831         if (player->ini.dump_element_keyword[0][0] == '\0')
4832                 player->ini.set_dump_element_flag = FALSE;
4833         else
4834                 player->ini.set_dump_element_flag = TRUE;
4835
4836         player->adaptive_info.limit.bandwidth = ADAPTIVE_VARIANT_DEFAULT_VALUE;
4837         player->adaptive_info.limit.width = ADAPTIVE_VARIANT_DEFAULT_VALUE;
4838         player->adaptive_info.limit.height = ADAPTIVE_VARIANT_DEFAULT_VALUE;
4839
4840         /* Set video360 settings to their defaults for just-created player.
4841          * */
4842
4843         player->is_360_feature_enabled = FALSE;
4844         if (SYSTEM_INFO_ERROR_NONE == system_info_get_platform_bool(FEATURE_NAME_SPHERICAL_VIDEO, &enabled)) {
4845                 LOGI("spherical feature info: %d", enabled);
4846                 if (enabled)
4847                         player->is_360_feature_enabled = TRUE;
4848         } else {
4849                 LOGE("failed to get spherical feature info");
4850         }
4851
4852         player->is_content_spherical = FALSE;
4853         player->is_video360_enabled = TRUE;
4854         player->video360_metadata.projection_type = VIDEO360_PROJECTION_TYPE_EQUIRECTANGULAR;
4855         player->video360_metadata.stereo_mode = VIDEO360_MODE_MONOSCOPIC;
4856         player->video360_yaw_radians = PLAYER_SPHERICAL_DEFAULT_YAW;
4857         player->video360_pitch_radians = PLAYER_SPHERICAL_DEFAULT_PITCH;
4858         player->video360_zoom = 1.0f;
4859         player->video360_horizontal_fov = PLAYER_SPHERICAL_DEFAULT_H_FOV;
4860         player->video360_vertical_fov = PLAYER_SPHERICAL_DEFAULT_V_FOV;
4861
4862         __mmplayer_initialize_video_roi(player);
4863
4864         /* set player state to null */
4865         MMPLAYER_STATE_CHANGE_TIMEOUT(player) = player->ini.localplayback_state_change_timeout;
4866         MMPLAYER_SET_STATE(player, MM_PLAYER_STATE_NULL);
4867
4868         MMPLAYER_FLEAVE();
4869
4870         return MM_ERROR_NONE;
4871
4872 ERROR:
4873         /* free lock */
4874         g_mutex_clear(&player->fsink_lock);
4875         /* free update tag lock */
4876         g_mutex_clear(&player->update_tag_lock);
4877         g_queue_free(player->bus_msg_q);
4878         player->bus_msg_q = NULL;
4879         /* free gapless play thread */
4880         if (player->gapless_play_thread) {
4881                 MMPLAYER_GAPLESS_PLAY_THREAD_LOCK(player);
4882                 player->gapless_play_thread_exit = TRUE;
4883                 MMPLAYER_GAPLESS_PLAY_THREAD_SIGNAL(player);
4884                 MMPLAYER_GAPLESS_PLAY_THREAD_UNLOCK(player);
4885
4886                 g_thread_join(player->gapless_play_thread);
4887                 player->gapless_play_thread = NULL;
4888
4889                 g_mutex_clear(&player->gapless_play_thread_mutex);
4890                 g_cond_clear(&player->gapless_play_thread_cond);
4891         }
4892
4893         /* release attributes */
4894         _mmplayer_deconstruct_attribute(handle);
4895
4896         MMPLAYER_FLEAVE();
4897
4898         return ret;
4899 }
4900
4901 static gboolean
4902 __mmplayer_init_gstreamer(mmplayer_t *player)
4903 {
4904         static gboolean initialized = FALSE;
4905         static const int max_argc = 50;
4906         gint *argc = NULL;
4907         gchar **argv = NULL;
4908         gchar **argv2 = NULL;
4909         GError *err = NULL;
4910         int i = 0;
4911         int arg_count = 0;
4912
4913         if (initialized) {
4914                 LOGD("gstreamer already initialized.");
4915                 return TRUE;
4916         }
4917
4918         /* alloc */
4919         argc = malloc(sizeof(int));
4920         argv = malloc(sizeof(gchar *) * max_argc);
4921         argv2 = malloc(sizeof(gchar *) * max_argc);
4922
4923         if (!argc || !argv || !argv2)
4924                 goto ERROR;
4925
4926         memset(argv, 0, sizeof(gchar *) * max_argc);
4927         memset(argv2, 0, sizeof(gchar *) * max_argc);
4928
4929         /* add initial */
4930         *argc = 1;
4931         argv[0] = g_strdup("mmplayer");
4932
4933         /* add gst_param */
4934         for (i = 0; i < 5; i++) {
4935                 /* FIXIT : num of param is now fixed to 5. make it dynamic */
4936                 if (strlen(player->ini.gst_param[i]) > 0) {
4937                         argv[*argc] = g_strdup(player->ini.gst_param[i]);
4938                         (*argc)++;
4939                 }
4940         }
4941
4942         /* we would not do fork for scanning plugins */
4943         argv[*argc] = g_strdup("--gst-disable-registry-fork");
4944         (*argc)++;
4945
4946         /* check disable registry scan */
4947         if (player->ini.skip_rescan) {
4948                 argv[*argc] = g_strdup("--gst-disable-registry-update");
4949                 (*argc)++;
4950         }
4951
4952         /* check disable segtrap */
4953         if (player->ini.disable_segtrap) {
4954                 argv[*argc] = g_strdup("--gst-disable-segtrap");
4955                 (*argc)++;
4956         }
4957
4958         LOGD("initializing gstreamer with following parameter");
4959         LOGD("argc : %d", *argc);
4960         arg_count = *argc;
4961
4962         for (i = 0; i < arg_count; i++) {
4963                 argv2[i] = argv[i];
4964                 LOGD("argv[%d] : %s", i, argv2[i]);
4965         }
4966
4967         /* initializing gstreamer */
4968         if (!gst_init_check(argc, &argv, &err)) {
4969                 LOGE("Could not initialize GStreamer: %s", err ? err->message : "unknown error occurred");
4970                 if (err)
4971                         g_error_free(err);
4972
4973                 goto ERROR;
4974         }
4975         /* release */
4976         for (i = 0; i < arg_count; i++) {
4977 #ifdef __DEBUG__
4978                 LOGD("release - argv[%d] : %s", i, argv2[i]);
4979 #endif
4980                 MMPLAYER_FREEIF(argv2[i]);
4981         }
4982
4983         MMPLAYER_FREEIF(argv);
4984         MMPLAYER_FREEIF(argv2);
4985         MMPLAYER_FREEIF(argc);
4986
4987         /* done */
4988         initialized = TRUE;
4989
4990         return TRUE;
4991
4992 ERROR:
4993
4994         /* release */
4995         for (i = 0; i < arg_count; i++) {
4996                 LOGD("free[%d] : %s", i, argv2[i]);
4997                 MMPLAYER_FREEIF(argv2[i]);
4998         }
4999
5000         MMPLAYER_FREEIF(argv);
5001         MMPLAYER_FREEIF(argv2);
5002         MMPLAYER_FREEIF(argc);
5003
5004         return FALSE;
5005 }
5006
5007 static void
5008 __mmplayer_check_async_state_transition(mmplayer_t *player)
5009 {
5010         GstState element_state = GST_STATE_VOID_PENDING;
5011         GstState element_pending_state = GST_STATE_VOID_PENDING;
5012         GstStateChangeReturn ret = GST_STATE_CHANGE_FAILURE;
5013         GstElement *element = NULL;
5014         gboolean async = FALSE;
5015
5016         /* check player handle */
5017         MMPLAYER_RETURN_IF_FAIL(player &&
5018                                                 player->pipeline &&
5019                                                 player->pipeline->mainbin &&
5020                                                 player->pipeline->mainbin[MMPLAYER_M_PIPE].gst);
5021
5022         if (player->attrs)
5023                 mm_attrs_get_int_by_name(player->attrs, "profile_prepare_async", &async);
5024
5025         if (!MMPLAYER_IS_MS_BUFF_SRC(player) && (async == FALSE)) {
5026                 LOGD("don't need to check the pipeline state");
5027                 return;
5028         }
5029
5030         MMPLAYER_PRINT_STATE(player);
5031
5032         /* wait for state transition */
5033         element = player->pipeline->mainbin[MMPLAYER_M_PIPE].gst;
5034         ret = gst_element_get_state(element, &element_state, &element_pending_state, 1 * GST_SECOND);
5035
5036         if (ret == GST_STATE_CHANGE_FAILURE) {
5037                 LOGE(" [%s] state : %s   pending : %s",
5038                         GST_ELEMENT_NAME(element),
5039                         gst_element_state_get_name(element_state),
5040                         gst_element_state_get_name(element_pending_state));
5041
5042                 /* dump state of all element */
5043                 _mmplayer_dump_pipeline_state(player);
5044
5045                 return;
5046         }
5047
5048         LOGD("[%s] element state has changed", GST_ELEMENT_NAME(element));
5049         return;
5050 }
5051
5052 int
5053 _mmplayer_destroy(MMHandleType handle)
5054 {
5055         mmplayer_t *player = MM_PLAYER_CAST(handle);
5056
5057         MMPLAYER_FENTER();
5058
5059         /* check player handle */
5060         MMPLAYER_RETURN_VAL_IF_FAIL(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
5061
5062         /* destroy can called at anytime */
5063         MMPLAYER_CHECK_STATE(player, MMPLAYER_COMMAND_DESTROY);
5064
5065         /* check async state transition */
5066         __mmplayer_check_async_state_transition(player);
5067
5068         /* release gapless play thread */
5069         if (player->gapless_play_thread) {
5070                 MMPLAYER_GAPLESS_PLAY_THREAD_LOCK(player);
5071                 player->gapless_play_thread_exit = TRUE;
5072                 MMPLAYER_GAPLESS_PLAY_THREAD_SIGNAL(player);
5073                 MMPLAYER_GAPLESS_PLAY_THREAD_UNLOCK(player);
5074
5075                 LOGD("waiting for gapless play thread exit");
5076                 g_thread_join(player->gapless_play_thread);
5077                 g_mutex_clear(&player->gapless_play_thread_mutex);
5078                 g_cond_clear(&player->gapless_play_thread_cond);
5079                 LOGD("gapless play thread released");
5080         }
5081
5082         _mmplayer_release_video_capture(player);
5083
5084         /* release miscellaneous information */
5085         __mmplayer_release_misc(player);
5086
5087         /* release pipeline */
5088         if (__mmplayer_gst_destroy_pipeline(player) != MM_ERROR_NONE) {
5089                 LOGE("failed to destroy pipeline");
5090                 return MM_ERROR_PLAYER_INTERNAL;
5091         }
5092
5093         __mmplayer_destroy_hw_resource(player);
5094
5095         g_queue_free(player->bus_msg_q);
5096
5097         /* release subtitle info lock and cond */
5098         g_mutex_clear(&player->subtitle_info_mutex);
5099         g_cond_clear(&player->subtitle_info_cond);
5100
5101         __mmplayer_release_dump_list(player->dump_list);
5102
5103         /* release miscellaneous information.
5104            these info needs to be released after pipeline is destroyed. */
5105         __mmplayer_release_misc_post(player);
5106
5107         /* release attributes */
5108         _mmplayer_deconstruct_attribute(handle);
5109
5110         if (player->uri_info.uri_list) {
5111                 g_list_free_full(player->uri_info.uri_list, (GDestroyNotify)g_free);
5112                 player->uri_info.uri_list = NULL;
5113         }
5114
5115         /* release lock */
5116         g_mutex_clear(&player->fsink_lock);
5117
5118         /* release lock */
5119         g_mutex_clear(&player->update_tag_lock);
5120
5121         /* release video bo lock and cond */
5122         g_mutex_clear(&player->video_bo_mutex);
5123         g_cond_clear(&player->video_bo_cond);
5124
5125         MMPLAYER_FLEAVE();
5126
5127         return MM_ERROR_NONE;
5128 }
5129
5130 int
5131 _mmplayer_realize(MMHandleType hplayer)
5132 {
5133         mmplayer_t *player = (mmplayer_t *)hplayer;
5134         int ret = MM_ERROR_NONE;
5135         char *uri = NULL;
5136         void *param = NULL;
5137         MMHandleType attrs = 0;
5138
5139         MMPLAYER_FENTER();
5140
5141         /* check player handle */
5142         MMPLAYER_RETURN_VAL_IF_FAIL(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
5143
5144         /* check current state */
5145         MMPLAYER_CHECK_STATE(player, MMPLAYER_COMMAND_REALIZE);
5146
5147         attrs = MMPLAYER_GET_ATTRS(player);
5148         if (!attrs) {
5149                 LOGE("fail to get attributes.");
5150                 return MM_ERROR_PLAYER_INTERNAL;
5151         }
5152         mm_attrs_get_string_by_name(attrs, "profile_uri", &uri);
5153         mm_attrs_get_data_by_name(attrs, "profile_user_param", &param);
5154
5155         if (player->profile.uri_type == MM_PLAYER_URI_TYPE_NONE) {
5156                 ret = _mmplayer_parse_profile((const char *)uri, param, &player->profile);
5157
5158                 if (ret != MM_ERROR_NONE) {
5159                         LOGE("failed to parse profile");
5160                         return ret;
5161                 }
5162         }
5163
5164         if (uri && (strstr(uri, "es_buff://"))) {
5165                 if (strstr(uri, "es_buff://push_mode"))
5166                         player->es_player_push_mode = TRUE;
5167                 else
5168                         player->es_player_push_mode = FALSE;
5169         }
5170
5171         if (player->profile.uri_type == MM_PLAYER_URI_TYPE_URL_MMS) {
5172                 LOGW("mms protocol is not supported format.");
5173                 return MM_ERROR_PLAYER_NOT_SUPPORTED_FORMAT;
5174         }
5175
5176         if (MMPLAYER_IS_STREAMING(player))
5177                 MMPLAYER_STATE_CHANGE_TIMEOUT(player) = player->ini.live_state_change_timeout;
5178         else
5179                 MMPLAYER_STATE_CHANGE_TIMEOUT(player) = player->ini.localplayback_state_change_timeout;
5180
5181         player->smooth_streaming = FALSE;
5182         player->videodec_linked  = 0;
5183         player->audiodec_linked  = 0;
5184         player->textsink_linked = 0;
5185         player->is_external_subtitle_present = FALSE;
5186         player->is_external_subtitle_added_now = FALSE;
5187         player->is_subtitle_off = FALSE; /* set the subtitle ON default */
5188         player->video360_metadata.is_spherical = -1;
5189         player->is_openal_plugin_used = FALSE;
5190         player->subtitle_language_list = NULL;
5191         player->is_subtitle_force_drop = FALSE;
5192
5193         _mmplayer_track_initialize(player);
5194         __mmplayer_initialize_storage_info(player, MMPLAYER_PATH_MAX);
5195
5196         if ((MMPLAYER_IS_STREAMING(player)) && (player->streamer == NULL)) {
5197                 gint prebuffer_ms = 0, rebuffer_ms = 0;
5198
5199                 player->streamer = _mm_player_streaming_create();
5200                 _mm_player_streaming_initialize(player->streamer, TRUE);
5201
5202                 mm_attrs_multiple_get(player->attrs, NULL,
5203                                 MM_PLAYER_PREBUFFER_MS, &prebuffer_ms,
5204                                 MM_PLAYER_REBUFFER_MS, &rebuffer_ms, NULL);
5205
5206                 if (prebuffer_ms > 0) {
5207                         prebuffer_ms = MAX(prebuffer_ms, 1000);
5208                         player->streamer->buffering_req.prebuffer_time = prebuffer_ms;
5209                 }
5210
5211                 if (rebuffer_ms > 0) {
5212                         player->streamer->buffering_req.mode = MM_PLAYER_BUFFERING_MODE_FIXED;
5213                         rebuffer_ms = MAX(rebuffer_ms, 1000);
5214                         player->streamer->buffering_req.rebuffer_time = rebuffer_ms;
5215                 }
5216
5217                 LOGD("buffering time %d ms, %d ms", player->streamer->buffering_req.prebuffer_time,
5218                                                                 player->streamer->buffering_req.rebuffer_time);
5219         }
5220
5221         /* realize pipeline */
5222         ret = __mmplayer_gst_realize(player);
5223         if (ret != MM_ERROR_NONE)
5224                 LOGE("fail to realize the player.");
5225
5226         MMPLAYER_BUS_MSG_THREAD_SIGNAL(player);
5227
5228         MMPLAYER_FLEAVE();
5229
5230         return ret;
5231 }
5232
5233 int
5234 _mmplayer_unrealize(MMHandleType hplayer)
5235 {
5236         mmplayer_t *player = (mmplayer_t *)hplayer;
5237         int ret = MM_ERROR_NONE;
5238         int rm_ret = MM_ERROR_NONE;
5239         mmplayer_resource_type_e res_idx = MMPLAYER_RESOURCE_TYPE_MAX;
5240
5241         MMPLAYER_FENTER();
5242
5243         MMPLAYER_RETURN_VAL_IF_FAIL(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
5244
5245         MMPLAYER_CMD_UNLOCK(player);
5246         _mmplayer_bus_watcher_remove(player);
5247         /* destroy the gst bus msg thread which is created during realize.
5248            this funct have to be called before getting cmd lock. */
5249         _mmplayer_bus_msg_thread_destroy(player);
5250         MMPLAYER_CMD_LOCK(player);
5251
5252         /* check current state */
5253         MMPLAYER_CHECK_STATE(player, MMPLAYER_COMMAND_UNREALIZE);
5254
5255         /* check async state transition */
5256         __mmplayer_check_async_state_transition(player);
5257
5258         /* unrealize pipeline */
5259         ret = __mmplayer_gst_unrealize(player);
5260
5261         for (res_idx = MMPLAYER_RESOURCE_TYPE_VIDEO_DECODER; res_idx < MMPLAYER_RESOURCE_TYPE_MAX; res_idx++) {
5262                 rm_ret = __mmplayer_release_hw_resource(player, res_idx);
5263                 if (rm_ret != MM_ERROR_NONE)
5264                         LOGE("failed to release [%d] resources", res_idx);
5265         }
5266
5267         player->interrupted_by_resource = FALSE;
5268
5269         MMPLAYER_FLEAVE();
5270         return ret;
5271 }
5272
5273 int
5274 _mmplayer_set_message_callback(MMHandleType hplayer, MMMessageCallback callback, gpointer user_param)
5275 {
5276         mmplayer_t *player = (mmplayer_t *)hplayer;
5277
5278         MMPLAYER_RETURN_VAL_IF_FAIL(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
5279
5280         return __mmplayer_gst_set_message_callback(player, callback, user_param);
5281 }
5282
5283 int
5284 _mmplayer_get_state(MMHandleType hplayer, int *state)
5285 {
5286         mmplayer_t *player = (mmplayer_t *)hplayer;
5287
5288         MMPLAYER_RETURN_VAL_IF_FAIL(state, MM_ERROR_INVALID_ARGUMENT);
5289
5290         *state = MMPLAYER_CURRENT_STATE(player);
5291
5292         return MM_ERROR_NONE;
5293 }
5294
5295 static int
5296 __mmplayer_gst_set_volume_property(mmplayer_t *player, const char *prop_name)
5297 {
5298         GstElement *vol_element = NULL;
5299         enum audio_element_id volume_elem_id = MMPLAYER_A_VOL;
5300
5301         MMPLAYER_FENTER();
5302         MMPLAYER_RETURN_VAL_IF_FAIL(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
5303         MMPLAYER_RETURN_VAL_IF_FAIL(prop_name, MM_ERROR_INVALID_ARGUMENT);
5304
5305         /* check pipeline handle */
5306         if (!player->pipeline || !player->pipeline->audiobin) {
5307                 LOGD("'%s' will be applied when audiobin is created", prop_name);
5308
5309                 /* NOTE : stored value will be used in create_audiobin
5310                  * returning MM_ERROR_NONE here makes application to able to
5311                  * set audio volume or mute at anytime.
5312                  */
5313                 return MM_ERROR_NONE;
5314         }
5315
5316         if (player->build_audio_offload || g_strrstr(player->ini.audiosink_element, "pulsesink"))
5317                 volume_elem_id = MMPLAYER_A_SINK;
5318
5319         vol_element = player->pipeline->audiobin[volume_elem_id].gst;
5320         if (!vol_element) {
5321                 LOGE("failed to get vol element %d", volume_elem_id);
5322                 return MM_ERROR_PLAYER_INTERNAL;
5323         }
5324
5325         LOGD("set '%s' property to element[%s]", prop_name, GST_ELEMENT_NAME(vol_element));
5326
5327         if (!g_object_class_find_property(G_OBJECT_GET_CLASS(vol_element), prop_name)) {
5328                 LOGE("there is no '%s' property", prop_name);
5329                 return MM_ERROR_PLAYER_INTERNAL;
5330         }
5331
5332         if (!strcmp(prop_name, "volume")) {
5333                 g_object_set(vol_element, "volume", player->sound.volume, NULL);
5334         } else if (!strcmp(prop_name, "mute")) {
5335                 g_object_set(vol_element, "mute", player->sound.mute, NULL);
5336         } else {
5337                 LOGE("invalid property %s", prop_name);
5338                 return MM_ERROR_PLAYER_INTERNAL;
5339         }
5340
5341         return MM_ERROR_NONE;
5342 }
5343
5344 int
5345 _mmplayer_set_volume(MMHandleType hplayer, float volume)
5346 {
5347         int ret = MM_ERROR_NONE;
5348         mmplayer_t *player = (mmplayer_t *)hplayer;
5349
5350         MMPLAYER_FENTER();
5351         MMPLAYER_RETURN_VAL_IF_FAIL(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
5352
5353         LOGD("volume = %f", volume);
5354
5355         /* invalid factor range or not */
5356         if (volume < MM_VOLUME_FACTOR_MIN || volume > MM_VOLUME_FACTOR_MAX) {
5357                 LOGE("Invalid volume value");
5358                 return MM_ERROR_INVALID_ARGUMENT;
5359         }
5360
5361         player->sound.volume = volume;
5362
5363         ret = __mmplayer_gst_set_volume_property(player, "volume");
5364
5365         MMPLAYER_FLEAVE();
5366         return ret;
5367 }
5368
5369 int
5370 _mmplayer_get_volume(MMHandleType hplayer, float *volume)
5371 {
5372         mmplayer_t *player = (mmplayer_t *)hplayer;
5373
5374         MMPLAYER_FENTER();
5375
5376         MMPLAYER_RETURN_VAL_IF_FAIL(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
5377         MMPLAYER_RETURN_VAL_IF_FAIL(volume, MM_ERROR_INVALID_ARGUMENT);
5378
5379         *volume = player->sound.volume;
5380
5381         LOGD("current vol = %f", *volume);
5382
5383         MMPLAYER_FLEAVE();
5384         return MM_ERROR_NONE;
5385 }
5386
5387 int
5388 _mmplayer_set_mute(MMHandleType hplayer, bool mute)
5389 {
5390         int ret = MM_ERROR_NONE;
5391         mmplayer_t *player = (mmplayer_t *)hplayer;
5392
5393         MMPLAYER_FENTER();
5394         MMPLAYER_RETURN_VAL_IF_FAIL(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
5395
5396         LOGD("mute = %d", mute);
5397
5398         player->sound.mute = mute;
5399
5400         ret = __mmplayer_gst_set_volume_property(player, "mute");
5401
5402         MMPLAYER_FLEAVE();
5403         return ret;
5404 }
5405
5406 int
5407 _mmplayer_get_mute(MMHandleType hplayer, bool *mute)
5408 {
5409         mmplayer_t *player = (mmplayer_t *)hplayer;
5410
5411         MMPLAYER_FENTER();
5412
5413         MMPLAYER_RETURN_VAL_IF_FAIL(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
5414         MMPLAYER_RETURN_VAL_IF_FAIL(mute, MM_ERROR_INVALID_ARGUMENT);
5415
5416         *mute = player->sound.mute;
5417
5418         LOGD("current mute = %d", *mute);
5419
5420         MMPLAYER_FLEAVE();
5421
5422         return MM_ERROR_NONE;
5423 }
5424
5425 int
5426 _mmplayer_set_audiostream_changed_cb(MMHandleType hplayer, mm_player_stream_changed_callback callback, void *user_param)
5427 {
5428         mmplayer_t *player = (mmplayer_t *)hplayer;
5429
5430         MMPLAYER_FENTER();
5431
5432         MMPLAYER_RETURN_VAL_IF_FAIL(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
5433
5434         player->audio_stream_changed_cb = callback;
5435         player->audio_stream_changed_cb_user_param = user_param;
5436         LOGD("Handle value is %p : %p", player, player->audio_stream_changed_cb);
5437
5438         MMPLAYER_FLEAVE();
5439
5440         return MM_ERROR_NONE;
5441 }
5442
5443 int
5444 _mmplayer_set_audio_decoded_cb(MMHandleType hplayer, mmplayer_audio_extract_opt_e opt, mm_player_audio_decoded_callback callback, void *user_param)
5445 {
5446         mmplayer_t *player = (mmplayer_t *)hplayer;
5447
5448         MMPLAYER_FENTER();
5449
5450         MMPLAYER_RETURN_VAL_IF_FAIL(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
5451
5452         player->audio_decoded_cb = callback;
5453         player->audio_decoded_cb_user_param = user_param;
5454         player->audio_extract_opt = opt;
5455         LOGD("handle: %p, cb: %p, opt: 0x%X", player, player->audio_decoded_cb, player->audio_extract_opt);
5456
5457         MMPLAYER_FLEAVE();
5458
5459         return MM_ERROR_NONE;
5460 }
5461
5462 int
5463 _mmplayer_set_video_decoded_cb(MMHandleType hplayer, mm_player_video_decoded_callback callback, void *user_param)
5464 {
5465         mmplayer_t *player = (mmplayer_t *)hplayer;
5466
5467         MMPLAYER_FENTER();
5468
5469         MMPLAYER_RETURN_VAL_IF_FAIL(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
5470
5471         if (callback && !player->bufmgr)
5472                 player->bufmgr = tbm_bufmgr_init(-1);
5473
5474         player->set_mode.video_export = (callback) ? true : false;
5475         player->video_decoded_cb = callback;
5476         player->video_decoded_cb_user_param = user_param;
5477
5478         LOGD("Stream cb Handle value is %p : %p, enable:%d", player, player->video_decoded_cb, player->set_mode.video_export);
5479
5480         MMPLAYER_FLEAVE();
5481
5482         return MM_ERROR_NONE;
5483 }
5484
5485 int
5486 _mmplayer_start(MMHandleType hplayer)
5487 {
5488         mmplayer_t *player = (mmplayer_t *)hplayer;
5489         gint ret = MM_ERROR_NONE;
5490
5491         MMPLAYER_FENTER();
5492
5493         MMPLAYER_RETURN_VAL_IF_FAIL(player && player->pipeline, MM_ERROR_PLAYER_NOT_INITIALIZED);
5494
5495         /* check current state */
5496         MMPLAYER_CHECK_STATE(player, MMPLAYER_COMMAND_START);
5497
5498         /* start pipeline */
5499         ret = _mmplayer_gst_start(player);
5500         if (ret != MM_ERROR_NONE)
5501                 LOGE("failed to start player.");
5502
5503         if ((player->streamer) && (player->streamer->buffering_state & MM_PLAYER_BUFFERING_IN_PROGRESS)) {
5504                 LOGD("force playing start even during buffering");
5505                 player->streamer->buffering_state = MM_PLAYER_BUFFERING_ABORT;
5506         }
5507
5508         MMPLAYER_FLEAVE();
5509
5510         return ret;
5511 }
5512
5513 /* NOTE: post "not supported codec message" to application
5514  * when one codec is not found during AUTOPLUGGING in MSL.
5515  * So, it's separated with error of __mmplayer_gst_bus_msg_callback().
5516  * And, if any codec is not found, don't send message here.
5517  * Because GST_ERROR_MESSAGE is posted by other plugin internally.
5518  */
5519 int
5520 __mmplayer_handle_missed_plugin(mmplayer_t *player)
5521 {
5522         MMMessageParamType msg_param;
5523         memset(&msg_param, 0, sizeof(MMMessageParamType));
5524         gboolean post_msg_direct = FALSE;
5525
5526         MMPLAYER_FENTER();
5527
5528         MMPLAYER_RETURN_VAL_IF_FAIL(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
5529
5530         LOGD("not_supported_codec = 0x%02x, can_support_codec = 0x%02x",
5531                         player->not_supported_codec, player->can_support_codec);
5532
5533         if (player->not_found_demuxer) {
5534                 msg_param.code = MM_ERROR_PLAYER_CODEC_NOT_FOUND;
5535                 msg_param.data = g_strdup_printf("%s", player->unlinked_demuxer_mime);
5536
5537                 MMPLAYER_POST_MSG(player, MM_MESSAGE_ERROR, &msg_param);
5538                 MMPLAYER_FREEIF(msg_param.data);
5539
5540                 return MM_ERROR_NONE;
5541         }
5542
5543         if (player->not_supported_codec) {
5544                 if (player->can_support_codec) {
5545                         // There is one codec to play
5546                         post_msg_direct = TRUE;
5547                 } else {
5548                         if (player->pipeline->audiobin) // Some content has only PCM data in container.
5549                                 post_msg_direct = TRUE;
5550                 }
5551
5552                 if (post_msg_direct) {
5553                         MMMessageParamType msg_param;
5554                         memset(&msg_param, 0, sizeof(MMMessageParamType));
5555
5556                         if (player->not_supported_codec ==  MISSING_PLUGIN_AUDIO) {
5557                                 LOGW("not found AUDIO codec, posting error code to application.");
5558
5559                                 msg_param.code = MM_ERROR_PLAYER_AUDIO_CODEC_NOT_FOUND;
5560                                 msg_param.data = g_strdup_printf("%s", player->unlinked_audio_mime);
5561                         } else if (player->not_supported_codec ==  MISSING_PLUGIN_VIDEO) {
5562                                 LOGW("not found VIDEO codec, posting error code to application.");
5563
5564                                 msg_param.code = MM_ERROR_PLAYER_VIDEO_CODEC_NOT_FOUND;
5565                                 msg_param.data = g_strdup_printf("%s", player->unlinked_video_mime);
5566                         }
5567
5568                         MMPLAYER_POST_MSG(player, MM_MESSAGE_ERROR, &msg_param);
5569
5570                         MMPLAYER_FREEIF(msg_param.data);
5571
5572                         return MM_ERROR_NONE;
5573                 } else {
5574                         // no any supported codec case
5575                         LOGW("not found any codec, posting error code to application.");
5576
5577                         if (player->not_supported_codec ==  MISSING_PLUGIN_AUDIO) {
5578                                 msg_param.code = MM_ERROR_PLAYER_AUDIO_CODEC_NOT_FOUND;
5579                                 msg_param.data = g_strdup_printf("%s", player->unlinked_audio_mime);
5580                         } else {
5581                                 msg_param.code = MM_ERROR_PLAYER_CODEC_NOT_FOUND;
5582                                 msg_param.data = g_strdup_printf("%s, %s", player->unlinked_video_mime, player->unlinked_audio_mime);
5583                         }
5584
5585                         MMPLAYER_POST_MSG(player, MM_MESSAGE_ERROR, &msg_param);
5586
5587                         MMPLAYER_FREEIF(msg_param.data);
5588                 }
5589         }
5590
5591         MMPLAYER_FLEAVE();
5592
5593         return MM_ERROR_NONE;
5594 }
5595
5596 static void __mmplayer_check_pipeline_reconfigure_state(mmplayer_t *player)
5597 {
5598         GstState element_state = GST_STATE_VOID_PENDING;
5599         GstState element_pending_state = GST_STATE_VOID_PENDING;
5600         GstStateChangeReturn result = GST_STATE_CHANGE_FAILURE;
5601         gint timeout = MMPLAYER_STATE_CHANGE_TIMEOUT(player);
5602
5603         MMPLAYER_RETURN_IF_FAIL(player && player->pipeline);
5604
5605         MMPLAYER_RECONFIGURE_LOCK(player);
5606         if (!player->gapless.reconfigure) {
5607                 MMPLAYER_RECONFIGURE_UNLOCK(player);
5608                 return;
5609         }
5610
5611         LOGI("reconfigure is under process");
5612         MMPLAYER_RECONFIGURE_WAIT(player);
5613         MMPLAYER_RECONFIGURE_UNLOCK(player);
5614         LOGI("reconfigure is completed.");
5615
5616         result = gst_element_get_state(player->pipeline->mainbin[MMPLAYER_M_PIPE].gst,
5617                                 &element_state, &element_pending_state, timeout * GST_SECOND);
5618         if (result == GST_STATE_CHANGE_FAILURE)
5619                 LOGW("failed to get pipeline state in %d sec", timeout);
5620
5621         return;
5622 }
5623
5624 /* NOTE : it should be able to call 'stop' anytime*/
5625 int
5626 _mmplayer_stop(MMHandleType hplayer)
5627 {
5628         mmplayer_t *player = (mmplayer_t *)hplayer;
5629         int ret = MM_ERROR_NONE;
5630
5631         MMPLAYER_FENTER();
5632
5633         MMPLAYER_RETURN_VAL_IF_FAIL(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
5634
5635         /* check current state */
5636         MMPLAYER_CHECK_STATE(player, MMPLAYER_COMMAND_STOP);
5637
5638         /* need to wait till the rebuilding pipeline is completed */
5639         __mmplayer_check_pipeline_reconfigure_state(player);
5640         MMPLAYER_RECONFIGURE_LOCK(player);
5641         __mmplayer_reset_gapless_state(player);
5642         MMPLAYER_RECONFIGURE_UNLOCK(player);
5643
5644         /* NOTE : application should not wait for EOS after calling STOP */
5645         _mmplayer_cancel_eos_timer(player);
5646
5647         /* reset */
5648         player->seek_state = MMPLAYER_SEEK_NONE;
5649
5650         /* stop pipeline */
5651         ret = _mmplayer_gst_stop(player);
5652
5653         if (ret != MM_ERROR_NONE)
5654                 LOGE("failed to stop player.");
5655
5656         MMPLAYER_FLEAVE();
5657
5658         return ret;
5659 }
5660
5661 int
5662 _mmplayer_pause(MMHandleType hplayer)
5663 {
5664         mmplayer_t *player = (mmplayer_t *)hplayer;
5665         gint64 pos_nsec = 0;
5666         gboolean async = FALSE;
5667         gint ret = MM_ERROR_NONE;
5668
5669         MMPLAYER_FENTER();
5670
5671         MMPLAYER_RETURN_VAL_IF_FAIL(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
5672
5673         /* check current state */
5674         MMPLAYER_CHECK_STATE(player, MMPLAYER_COMMAND_PAUSE);
5675
5676         /* check pipeline reconfigure state */
5677         __mmplayer_check_pipeline_reconfigure_state(player);
5678
5679         switch (MMPLAYER_CURRENT_STATE(player)) {
5680         case MM_PLAYER_STATE_READY:
5681                 {
5682                         /* check prepare async or not.
5683                          * In the case of streaming playback, it's recommended to avoid blocking wait.
5684                          */
5685                         mm_attrs_get_int_by_name(player->attrs, "profile_prepare_async", &async);
5686                         LOGD("prepare working mode : %s", (async ? "async" : "sync"));
5687
5688                         /* Changing back sync of rtspsrc to async */
5689                         if (MMPLAYER_IS_RTSP_STREAMING(player)) {
5690                                 LOGD("async prepare working mode for rtsp");
5691                                 async = TRUE;
5692                         }
5693                 }
5694                 break;
5695
5696         case MM_PLAYER_STATE_PLAYING:
5697                 {
5698                         /* NOTE : store current point to overcome some bad operation
5699                         *(returning zero when getting current position in paused state) of some
5700                         * elements
5701                         */
5702                         if (!gst_element_query_position(player->pipeline->mainbin[MMPLAYER_M_PIPE].gst, GST_FORMAT_TIME, &pos_nsec))
5703                                 LOGW("getting current position failed in paused");
5704
5705                         player->last_position = pos_nsec;
5706
5707                         /* For rtspsrc stack , npt-start value coming from server is used for finding the current position.
5708                            But when a rtsp clip (especially from Youtube Desktop View) is paused and kept for sometime,npt-start is still increasing.
5709                            This causes problem is position calculation during normal pause resume scenarios also.
5710                            Currently during pause , we are sending the current position to rtspsrc module for position saving. */
5711                         if ((MMPLAYER_IS_RTSP_STREAMING(player)) &&
5712                                 (_mmplayer_get_stream_service_type(player) == STREAMING_SERVICE_VOD)) {
5713                                 g_object_set(player->pipeline->mainbin[MMPLAYER_M_SRC].gst, "resume-position", player->last_position, NULL);
5714                         }
5715                 }
5716                 break;
5717         }
5718
5719         if (MMPLAYER_IS_MS_BUFF_SRC(player)) {
5720                 LOGD("doing async pause in case of ms buff src");
5721                 async = TRUE;
5722         }
5723
5724         /* pause pipeline */
5725         ret = _mmplayer_gst_pause(player, async);
5726         if (ret != MM_ERROR_NONE) {
5727                 LOGE("failed to pause player. ret : 0x%x", ret);
5728                 MMPLAYER_GENERATE_DOT_IF_ENABLED(player, "pipeline-pause-err");
5729                 return ret;
5730         }
5731
5732         if (MMPLAYER_PREV_STATE(player) == MM_PLAYER_STATE_READY && MMPLAYER_CURRENT_STATE(player) == MM_PLAYER_STATE_PAUSED) {
5733                 if (_mmplayer_update_video_overlay_param(player, "display_rotation") != MM_ERROR_NONE)
5734                         LOGE("failed to update display_rotation");
5735         }
5736
5737         MMPLAYER_FLEAVE();
5738         return MM_ERROR_NONE;
5739 }
5740
5741 /* in case of streaming, pause could take long time.*/
5742 int
5743 _mmplayer_abort_pause(MMHandleType hplayer)
5744 {
5745         mmplayer_t *player = (mmplayer_t *)hplayer;
5746         int ret = MM_ERROR_NONE;
5747
5748         MMPLAYER_FENTER();
5749
5750         MMPLAYER_RETURN_VAL_IF_FAIL(player &&
5751                                                 player->pipeline &&
5752                                                 player->pipeline->mainbin,
5753                                                 MM_ERROR_PLAYER_NOT_INITIALIZED);
5754
5755         if (player->pipeline->videobin && player->pipeline->videobin[MMPLAYER_V_BIN].gst) {
5756                 LOGD("set the videobin state to READY");
5757                 ret = _mmplayer_gst_set_state(player, player->pipeline->videobin[MMPLAYER_V_BIN].gst,
5758                                                         GST_STATE_READY, TRUE, MMPLAYER_STATE_CHANGE_TIMEOUT(player));
5759
5760         }
5761
5762         if (player->pipeline->audiobin && player->pipeline->audiobin[MMPLAYER_A_BIN].gst) {
5763                 LOGD("set the audiobin state to READY");
5764                 ret = _mmplayer_gst_set_state(player, player->pipeline->audiobin[MMPLAYER_A_BIN].gst,
5765                                                         GST_STATE_READY, TRUE, MMPLAYER_STATE_CHANGE_TIMEOUT(player));
5766
5767         }
5768
5769         LOGD("set the pipeline state to READY");
5770         ret = _mmplayer_gst_set_state(player, player->pipeline->mainbin[MMPLAYER_M_PIPE].gst,
5771                                                 GST_STATE_READY, FALSE, MMPLAYER_STATE_CHANGE_TIMEOUT(player));
5772
5773         if (ret != MM_ERROR_NONE) {
5774                 LOGE("fail to change state to READY");
5775                 return MM_ERROR_PLAYER_INTERNAL;
5776         }
5777
5778         LOGD("succeeded in changing state to READY");
5779         return ret;
5780 }
5781
5782 int
5783 _mmplayer_resume(MMHandleType hplayer)
5784 {
5785         mmplayer_t *player = (mmplayer_t *)hplayer;
5786         int ret = MM_ERROR_NONE;
5787         gboolean async = FALSE;
5788
5789         MMPLAYER_FENTER();
5790
5791         MMPLAYER_RETURN_VAL_IF_FAIL(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
5792
5793         if ((MMPLAYER_IS_RTSP_STREAMING(player))) {
5794                 if (player->is_external_subtitle_added_now) { /* after setting external subtitle, seeking and buffering is in progress. */
5795                         player->pending_resume = TRUE; /* will be resumed after finishing the buffering. */
5796                         return ret;
5797                 }
5798
5799                 /* Changing back sync mode rtspsrc to async */
5800                 LOGD("async resume for rtsp case");
5801                 async = TRUE;
5802         }
5803
5804         /* check current state */
5805         MMPLAYER_CHECK_STATE(player, MMPLAYER_COMMAND_RESUME);
5806
5807         ret = _mmplayer_gst_resume(player, async);
5808         if (ret != MM_ERROR_NONE)
5809                 LOGE("failed to resume player.");
5810
5811         if ((player->streamer) && (player->streamer->buffering_state & MM_PLAYER_BUFFERING_IN_PROGRESS)) {
5812                 LOGD("force resume even during buffering");
5813                 player->streamer->buffering_state = MM_PLAYER_BUFFERING_ABORT;
5814         }
5815
5816         MMPLAYER_FLEAVE();
5817
5818         return ret;
5819 }
5820
5821 int
5822 _mmplayer_set_playspeed(MMHandleType hplayer, float rate, bool streaming)
5823 {
5824         mmplayer_t *player = (mmplayer_t *)hplayer;
5825         gint64 pos_nsec = 0;
5826         int ret = MM_ERROR_NONE;
5827         bool mute = false;
5828         signed long long start = 0, stop = 0;
5829         mmplayer_state_e current_state = MM_PLAYER_STATE_NONE;
5830         MMPLAYER_FENTER();
5831
5832         MMPLAYER_RETURN_VAL_IF_FAIL(player && player->pipeline, MM_ERROR_PLAYER_NOT_INITIALIZED);
5833         MMPLAYER_RETURN_VAL_IF_FAIL(streaming || !MMPLAYER_IS_STREAMING(player), MM_ERROR_NOT_SUPPORT_API);
5834
5835         /* The sound of video is not supported under 0.0 and over 2.0. */
5836         if (rate >= TRICK_PLAY_MUTE_THRESHOLD_MAX || rate < TRICK_PLAY_MUTE_THRESHOLD_MIN) {
5837                 if (player->can_support_codec & FOUND_PLUGIN_VIDEO)
5838                         mute = true;
5839         }
5840         _mmplayer_set_mute(hplayer, mute);
5841
5842         if (player->playback_rate == rate)
5843                 return MM_ERROR_NONE;
5844
5845         /* If the position is reached at start potion during fast backward, EOS is posted.
5846          * So, This EOS have to be classified with it which is posted at reaching the end of stream.
5847          */
5848         player->playback_rate = rate;
5849
5850         current_state = MMPLAYER_CURRENT_STATE(player);
5851
5852         if (current_state != MM_PLAYER_STATE_PAUSED)
5853                 ret = gst_element_query_position(player->pipeline->mainbin[MMPLAYER_M_PIPE].gst, GST_FORMAT_TIME, &pos_nsec);
5854
5855         LOGD("pos_msec = %"GST_TIME_FORMAT" and ret = %d and state = %d", GST_TIME_ARGS(pos_nsec), ret, current_state);
5856
5857         if ((current_state == MM_PLAYER_STATE_PAUSED)
5858                 || (!ret) /*|| (player->last_position != 0 && pos_msec == 0)*/) {
5859                 LOGW("returning last point : %"G_GINT64_FORMAT, player->last_position);
5860                 pos_nsec = player->last_position;
5861         }
5862
5863         if (rate >= 0) {
5864                 start = pos_nsec;
5865                 stop = GST_CLOCK_TIME_NONE;
5866         } else {
5867                 start = GST_CLOCK_TIME_NONE;
5868                 stop = pos_nsec;
5869         }
5870
5871         if (!_mmplayer_gst_seek(player, player->pipeline->mainbin[MMPLAYER_M_PIPE].gst,
5872                                 player->playback_rate,
5873                                 GST_FORMAT_TIME,
5874                                 (GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_ACCURATE),
5875                                 GST_SEEK_TYPE_SET, start,
5876                                 GST_SEEK_TYPE_SET, stop)) {
5877                 LOGE("failed to set speed playback");
5878                 return MM_ERROR_PLAYER_SEEK;
5879         }
5880
5881         LOGD("succeeded to set speed playback as %0.1f", rate);
5882
5883         MMPLAYER_FLEAVE();
5884
5885         return MM_ERROR_NONE;;
5886 }
5887
5888 int
5889 _mmplayer_set_position(MMHandleType hplayer, gint64 position)
5890 {
5891         mmplayer_t *player = (mmplayer_t *)hplayer;
5892         int ret = MM_ERROR_NONE;
5893
5894         MMPLAYER_FENTER();
5895
5896         MMPLAYER_RETURN_VAL_IF_FAIL(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
5897
5898         /* check pipeline reconfigure state */
5899         __mmplayer_check_pipeline_reconfigure_state(player);
5900
5901         ret = _mmplayer_gst_set_position(player, position, FALSE);
5902
5903         MMPLAYER_FLEAVE();
5904
5905         return ret;
5906 }
5907
5908 int
5909 _mmplayer_get_duration(MMHandleType hplayer, gint64 *duration)
5910 {
5911         mmplayer_t *player = (mmplayer_t *)hplayer;
5912         int ret = MM_ERROR_NONE;
5913
5914         MMPLAYER_RETURN_VAL_IF_FAIL(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
5915         MMPLAYER_RETURN_VAL_IF_FAIL(duration, MM_ERROR_COMMON_INVALID_ARGUMENT);
5916
5917         if (g_strrstr(player->type_caps_str, "video/mpegts"))
5918                 __mmplayer_update_duration_value(player);
5919
5920         *duration = player->duration;
5921         return ret;
5922 }
5923
5924 int
5925 _mmplayer_get_buffer_position(MMHandleType hplayer, int *start_pos, int *end_pos)
5926 {
5927         mmplayer_t *player = (mmplayer_t *)hplayer;
5928         int ret = MM_ERROR_NONE;
5929
5930         MMPLAYER_RETURN_VAL_IF_FAIL(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
5931
5932         ret = _mmplayer_gst_get_buffer_position(player, start_pos, end_pos);
5933
5934         return ret;
5935 }
5936
5937 int
5938 _mmplayer_adjust_subtitle_position(MMHandleType hplayer, int position)
5939 {
5940         mmplayer_t *player = (mmplayer_t *)hplayer;
5941         int ret = MM_ERROR_NONE;
5942
5943         MMPLAYER_FENTER();
5944
5945         MMPLAYER_RETURN_VAL_IF_FAIL(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
5946
5947         ret = __mmplayer_gst_adjust_subtitle_position(player, position);
5948
5949         MMPLAYER_FLEAVE();
5950
5951         return ret;
5952 }
5953
5954 static gboolean
5955 __mmplayer_is_midi_type(gchar *str_caps)
5956 {
5957         if ((g_strrstr(str_caps, "audio/midi")) ||
5958                 (g_strrstr(str_caps, "application/x-gst_ff-mmf")) ||
5959                 (g_strrstr(str_caps, "application/x-smaf")) ||
5960                 (g_strrstr(str_caps, "audio/x-imelody")) ||
5961                 (g_strrstr(str_caps, "audio/mobile-xmf")) ||
5962                 (g_strrstr(str_caps, "audio/xmf")) ||
5963                 (g_strrstr(str_caps, "audio/mxmf"))) {
5964                 LOGD("midi");
5965                 return TRUE;
5966         }
5967
5968         return FALSE;
5969 }
5970
5971 static gboolean
5972 __mmplayer_is_only_mp3_type(gchar *str_caps)
5973 {
5974         if (g_strrstr(str_caps, "application/x-id3") ||
5975                 (g_strrstr(str_caps, "audio/mpeg") && g_strrstr(str_caps, "mpegversion=(int)1")))
5976                 return TRUE;
5977         return FALSE;
5978 }
5979
5980 void
5981 _mmplayer_set_audio_attrs(mmplayer_t *player, GstCaps *caps)
5982 {
5983         GstStructure *caps_structure = NULL;
5984         gint samplerate = 0;
5985         gint channels = 0;
5986
5987         MMPLAYER_FENTER();
5988         MMPLAYER_RETURN_IF_FAIL(player && caps);
5989
5990         caps_structure = gst_caps_get_structure(caps, 0);
5991
5992         /* set stream information */
5993         gst_structure_get_int(caps_structure, "rate", &samplerate);
5994         gst_structure_get_int(caps_structure, "channels", &channels);
5995
5996         mm_player_set_attribute((MMHandleType)player, NULL,
5997                         "content_audio_samplerate", samplerate,
5998                         "content_audio_channels", channels, NULL);
5999
6000         LOGD("audio samplerate : %d     channels : %d", samplerate, channels);
6001 }
6002
6003 static void
6004 __mmplayer_update_content_type_info(mmplayer_t *player)
6005 {
6006         MMPLAYER_FENTER();
6007         MMPLAYER_RETURN_IF_FAIL(player && player->type_caps_str);
6008
6009         if (__mmplayer_is_midi_type(player->type_caps_str)) {
6010                 player->bypass_audio_effect = TRUE;
6011                 return;
6012         }
6013
6014         if (!player->streamer) {
6015                 LOGD("no need to check streaming type");
6016                 return;
6017         }
6018
6019         if (g_strrstr(player->type_caps_str, "application/x-hls")) {
6020                 /* If it can't know exact type when it parses uri because of redirection case,
6021                  * it will be fixed by typefinder or when doing autoplugging.
6022                  */
6023                 player->profile.uri_type = MM_PLAYER_URI_TYPE_HLS;
6024                 player->streamer->is_adaptive_streaming = TRUE;
6025         } else if (g_strrstr(player->type_caps_str, "application/dash+xml")) {
6026                 player->profile.uri_type = MM_PLAYER_URI_TYPE_DASH;
6027                 player->streamer->is_adaptive_streaming = TRUE;
6028         }
6029
6030         /* in case of TS, fixed buffering mode should be used because player can not get exact duration time */
6031         if ((player->streamer->is_adaptive_streaming) || (g_strrstr(player->type_caps_str, "video/mpegts"))) {
6032                 player->streamer->buffering_req.mode = MM_PLAYER_BUFFERING_MODE_FIXED;
6033
6034                 if (player->streamer->buffering_req.rebuffer_time <= MIN_BUFFERING_TIME) { /* if user did not set the rebuffer value */
6035                         if (player->streamer->is_adaptive_streaming)
6036                                 player->streamer->buffering_req.rebuffer_time = DEFAULT_ADAPTIVE_REBUFFER_TIME;
6037                         else
6038                                 player->streamer->buffering_req.rebuffer_time = DEFAULT_REBUFFERING_TIME;
6039                 }
6040         }
6041
6042         LOGD("uri type : %d, %d", player->profile.uri_type, player->streamer->buffering_req.rebuffer_time);
6043         MMPLAYER_FLEAVE();
6044 }
6045
6046 void
6047 _mmplayer_typefind_have_type(GstElement *tf, guint probability,
6048         GstCaps *caps, gpointer data)
6049 {
6050         mmplayer_t *player = (mmplayer_t *)data;
6051
6052         MMPLAYER_FENTER();
6053
6054         MMPLAYER_RETURN_IF_FAIL(player && tf && caps);
6055
6056         MMPLAYER_LOG_GST_CAPS_TYPE(caps);
6057         MMPLAYER_FREEIF(player->type_caps_str);
6058         player->type_caps_str = gst_caps_to_string(caps);
6059         if (player->type_caps_str)
6060                 LOGD("[handle: %p] media type %s found, probability %d%% / %d",
6061                                 player, player->type_caps_str, probability, gst_caps_get_size(caps));
6062
6063         if ((!MMPLAYER_IS_RTSP_STREAMING(player)) &&
6064                 (g_strrstr(player->type_caps_str, "audio/x-raw-int"))) {
6065                 LOGE("not support media format");
6066
6067                 if (player->msg_posted == FALSE) {
6068                         MMMessageParamType msg_param;
6069                         memset(&msg_param, 0, sizeof(MMMessageParamType));
6070
6071                         msg_param.code = MM_ERROR_PLAYER_NOT_SUPPORTED_FORMAT;
6072                         MMPLAYER_POST_MSG(player, MM_MESSAGE_ERROR, &msg_param);
6073
6074                         /* don't post more if one was sent already */
6075                         player->msg_posted = TRUE;
6076                 }
6077                 return;
6078         }
6079
6080         __mmplayer_update_content_type_info(player);
6081
6082         if (!player->pipeline->mainbin[MMPLAYER_M_AUTOPLUG].gst) {
6083                 GstPad *pad = NULL;
6084
6085                 pad = gst_element_get_static_pad(tf, "src");
6086                 if (!pad) {
6087                         LOGE("fail to get typefind src pad.");
6088                         return;
6089                 }
6090
6091                 if (!_mmplayer_gst_create_decoder(player, pad, caps)) {
6092                         gboolean async = FALSE;
6093                         LOGE("failed to autoplug %s", player->type_caps_str);
6094
6095                         mm_attrs_get_int_by_name(player->attrs, "profile_prepare_async", &async);
6096
6097                         if (async && player->msg_posted == FALSE)
6098                                 __mmplayer_handle_missed_plugin(player);
6099                 }
6100                 gst_object_unref(GST_OBJECT(pad));
6101         }
6102         MMPLAYER_FLEAVE();
6103         return;
6104 }
6105
6106 GstElement *
6107 _mmplayer_gst_make_decodebin(mmplayer_t *player)
6108 {
6109         GstElement *decodebin = NULL;
6110
6111         MMPLAYER_FENTER();
6112
6113         /* create decodebin */
6114         decodebin = gst_element_factory_make("decodebin", NULL);
6115
6116         if (!decodebin) {
6117                 LOGE("fail to create decodebin");
6118                 goto ERROR;
6119         }
6120
6121         /* raw pad handling signal */
6122         _mmplayer_add_signal_connection(player, G_OBJECT(decodebin), MM_PLAYER_SIGNAL_TYPE_AUTOPLUG, "pad-added",
6123                                                 G_CALLBACK(_mmplayer_gst_decode_pad_added), (gpointer)player);
6124
6125         /* no-more-pad pad handling signal */
6126         _mmplayer_add_signal_connection(player, G_OBJECT(decodebin), MM_PLAYER_SIGNAL_TYPE_AUTOPLUG, "no-more-pads",
6127                                                 G_CALLBACK(_mmplayer_gst_decode_no_more_pads), (gpointer)player);
6128
6129         _mmplayer_add_signal_connection(player, G_OBJECT(decodebin), MM_PLAYER_SIGNAL_TYPE_AUTOPLUG, "pad-removed",
6130                                                 G_CALLBACK(_mmplayer_gst_decode_pad_removed), (gpointer)player);
6131
6132         /* This signal is emitted when a pad for which there is no further possible
6133            decoding is added to the decodebin.*/
6134         _mmplayer_add_signal_connection(player, G_OBJECT(decodebin), MM_PLAYER_SIGNAL_TYPE_AUTOPLUG, "unknown-type",
6135                                                 G_CALLBACK(_mmplayer_gst_decode_unknown_type), (gpointer)player);
6136
6137         /* This signal is emitted whenever decodebin finds a new stream. It is emitted
6138            before looking for any elements that can handle that stream.*/
6139         _mmplayer_add_signal_connection(player, G_OBJECT(decodebin), MM_PLAYER_SIGNAL_TYPE_AUTOPLUG, "autoplug-continue",
6140                                                 G_CALLBACK(_mmplayer_gst_decode_autoplug_continue), (gpointer)player);
6141
6142         if (player->need_video_dec_sorting || player->need_audio_dec_sorting)
6143                 _mmplayer_add_signal_connection(player, G_OBJECT(decodebin), MM_PLAYER_SIGNAL_TYPE_AUTOPLUG, "autoplug-sort",
6144                                                         G_CALLBACK(_mmplayer_gst_decode_autoplug_sort), (gpointer)player);
6145
6146         /* This signal is emitted whenever decodebin finds a new stream. It is emitted
6147            before looking for any elements that can handle that stream.*/
6148         _mmplayer_add_signal_connection(player, G_OBJECT(decodebin), MM_PLAYER_SIGNAL_TYPE_AUTOPLUG, "autoplug-select",
6149                                                 G_CALLBACK(_mmplayer_gst_decode_autoplug_select), (gpointer)player);
6150
6151         /* This signal is emitted once decodebin has finished decoding all the data.*/
6152         _mmplayer_add_signal_connection(player, G_OBJECT(decodebin), MM_PLAYER_SIGNAL_TYPE_AUTOPLUG, "drained",
6153                                                 G_CALLBACK(_mmplayer_gst_decode_drained), (gpointer)player);
6154
6155         /* This signal is emitted when a element is added to the bin.*/
6156         _mmplayer_add_signal_connection(player, G_OBJECT(decodebin), MM_PLAYER_SIGNAL_TYPE_AUTOPLUG, "element-added",
6157                                                 G_CALLBACK(_mmplayer_gst_element_added), (gpointer)player);
6158
6159 ERROR:
6160         return decodebin;
6161 }
6162
6163 static GstElement *
6164 __mmplayer_gst_make_queue2(mmplayer_t *player)
6165 {
6166         GstElement *queue2 = NULL;
6167         gint64 dur_bytes = 0L;
6168         mmplayer_gst_element_t *mainbin = NULL;
6169         muxed_buffer_type_e type = MUXED_BUFFER_TYPE_MEM_QUEUE;
6170
6171         MMPLAYER_FENTER();
6172         MMPLAYER_RETURN_VAL_IF_FAIL(player && player->pipeline && player->pipeline->mainbin, NULL);
6173
6174         mainbin = player->pipeline->mainbin;
6175
6176         queue2 = gst_element_factory_make("queue2", "queue2");
6177         if (!queue2) {
6178                 LOGE("failed to create buffering queue element");
6179                 return NULL;
6180         }
6181
6182         if (!gst_element_query_duration(mainbin[MMPLAYER_M_SRC].gst, GST_FORMAT_BYTES, &dur_bytes))
6183                 LOGW("failed to get duration from source %s", GST_ELEMENT_NAME(mainbin[MMPLAYER_M_SRC].gst));
6184
6185         LOGD("dur_bytes = %"G_GINT64_FORMAT, dur_bytes);
6186
6187         /* NOTE : in case of ts streaming, player could not get the correct duration info *
6188          *                skip the pull mode(file or ring buffering) setting. */
6189         if (dur_bytes > 0) {
6190                 if (!g_strrstr(player->type_caps_str, "video/mpegts")) {
6191                         type = MUXED_BUFFER_TYPE_MEM_RING_BUFFER;
6192                         player->streamer->ring_buffer_size = player->ini.http_ring_buffer_size;
6193                 }
6194         } else {
6195                 dur_bytes = 0;
6196         }
6197
6198         _mm_player_streaming_set_queue2(player->streamer,
6199                                                                         queue2,
6200                                                                         FALSE,
6201                                                                         type,
6202                                                                         (guint64)dur_bytes); /* no meaning at the moment */
6203
6204         return queue2;
6205 }
6206
6207 gboolean
6208 _mmplayer_gst_create_decoder(mmplayer_t *player, GstPad *srcpad, const GstCaps *caps)
6209 {
6210         mmplayer_gst_element_t *mainbin = NULL;
6211         GstElement *decodebin = NULL;
6212         GstElement *queue2 = NULL;
6213         GstPad *sinkpad = NULL;
6214         GstPad *qsrcpad = NULL;
6215
6216         MMPLAYER_FENTER();
6217         MMPLAYER_RETURN_VAL_IF_FAIL(player && player->pipeline && player->pipeline->mainbin, FALSE);
6218
6219         mainbin = player->pipeline->mainbin;
6220
6221         if (MMPLAYER_IS_HTTP_STREAMING(player)) {
6222
6223                 if (mainbin[MMPLAYER_M_MUXED_S_BUFFER].gst) {
6224                         LOGW("need to check: muxed buffer is not null");
6225                 }
6226
6227                 queue2 = __mmplayer_gst_make_queue2(player);
6228                 if (!queue2) {
6229                         LOGE("failed to make queue2");
6230                         goto ERROR;
6231                 }
6232
6233                 if (!gst_bin_add(GST_BIN(mainbin[MMPLAYER_M_PIPE].gst), queue2)) {
6234                         LOGE("failed to add buffering queue");
6235                         goto ERROR;
6236                 }
6237
6238                 sinkpad = gst_element_get_static_pad(queue2, "sink");
6239                 qsrcpad = gst_element_get_static_pad(queue2, "src");
6240
6241                 if (gst_pad_link(srcpad, sinkpad) != GST_PAD_LINK_OK) {
6242                         LOGE("failed to link [%s:%s]-[%s:%s]",
6243                                                 GST_DEBUG_PAD_NAME(srcpad), GST_DEBUG_PAD_NAME(sinkpad));
6244                         goto ERROR;
6245                 }
6246
6247                 if (gst_element_sync_state_with_parent(queue2) == GST_STATE_CHANGE_FAILURE) {
6248                         LOGE("failed to sync queue2 state with parent");
6249                         goto ERROR;
6250                 }
6251
6252                 mainbin[MMPLAYER_M_MUXED_S_BUFFER].id = MMPLAYER_M_MUXED_S_BUFFER;
6253                 mainbin[MMPLAYER_M_MUXED_S_BUFFER].gst = queue2;
6254
6255                 srcpad = qsrcpad;
6256
6257                 gst_object_unref(GST_OBJECT(sinkpad));
6258                 sinkpad = NULL;
6259         }
6260
6261         /* create decodebin */
6262         decodebin = _mmplayer_gst_make_decodebin(player);
6263         if (!decodebin) {
6264                 LOGE("failed to make decodebin");
6265                 goto ERROR;
6266         }
6267
6268         if (!gst_bin_add(GST_BIN(mainbin[MMPLAYER_M_PIPE].gst), decodebin)) {
6269                 LOGE("failed to add decodebin");
6270                 goto ERROR;
6271         }
6272
6273         /* to force caps on the decodebin element and avoid reparsing stuff by
6274         * typefind. It also avoids a deadlock in the way typefind activates pads in
6275         * the state change */
6276         g_object_set(decodebin, "sink-caps", caps, NULL);
6277
6278         sinkpad = gst_element_get_static_pad(decodebin, "sink");
6279
6280         if (GST_PAD_LINK_OK != gst_pad_link(srcpad, sinkpad)) {
6281                 LOGE("failed to link [%s:%s]-[%s:%s]",
6282                                 GST_DEBUG_PAD_NAME(srcpad), GST_DEBUG_PAD_NAME(sinkpad));
6283                 goto ERROR;
6284         }
6285
6286         gst_object_unref(GST_OBJECT(sinkpad));
6287         sinkpad = NULL;
6288         gst_object_unref(GST_OBJECT(qsrcpad));
6289         qsrcpad = NULL;
6290
6291         mainbin[MMPLAYER_M_AUTOPLUG].id = MMPLAYER_M_AUTOPLUG;
6292         mainbin[MMPLAYER_M_AUTOPLUG].gst = decodebin;
6293
6294         /* set decodebin property about buffer in streaming playback. *
6295          * in case of HLS/DASH, it does not need to have big buffer   *
6296          * because it is kind of adaptive streaming.                  */
6297         if (MMPLAYER_IS_HTTP_STREAMING(player) || MMPLAYER_IS_HTTP_LIVE_STREAMING(player) || MMPLAYER_IS_DASH_STREAMING(player)) {
6298                 gint init_buffering_time = DEFAULT_PREBUFFERING_TIME;
6299                 gint high_percent = 0;
6300
6301                 if (player->streamer->buffering_req.prebuffer_time > MIN_BUFFERING_TIME)
6302                         init_buffering_time = player->streamer->buffering_req.prebuffer_time;
6303
6304                 high_percent = (gint)ceil((gdouble)(init_buffering_time * 100) / MAX_BUFFER_SIZE_TIME);
6305
6306                 LOGD("buffering time %d, per: 1~%d", init_buffering_time, high_percent);
6307
6308                 g_object_set(G_OBJECT(decodebin), "use-buffering", TRUE,
6309                                                                                         "high-percent", high_percent,
6310                                                                                         "max-size-bytes", MAX_BUFFER_SIZE_BYTES,
6311                                                                                         "max-size-time", (guint64)(MAX_BUFFER_SIZE_TIME * GST_MSECOND),
6312                                                                                         "max-size-buffers", 0, NULL);  // disable or automatic
6313         }
6314
6315         if (gst_element_sync_state_with_parent(decodebin) == GST_STATE_CHANGE_FAILURE) {
6316                 LOGE("failed to sync decodebin state with parent");
6317                 goto ERROR;
6318         }
6319
6320         MMPLAYER_FLEAVE();
6321
6322         return TRUE;
6323
6324 ERROR:
6325
6326         if (sinkpad)
6327                 gst_object_unref(GST_OBJECT(sinkpad));
6328
6329         if (qsrcpad)
6330                 gst_object_unref(GST_OBJECT(qsrcpad));
6331
6332         if (queue2) {
6333                 /* NOTE : Trying to dispose element queue0, but it is in READY instead of the NULL state.
6334                  * You need to explicitly set elements to the NULL state before
6335                  * dropping the final reference, to allow them to clean up.
6336                  */
6337                 gst_element_set_state(queue2, GST_STATE_NULL);
6338
6339                 /* And, it still has a parent "player".
6340                  * You need to let the parent manage the object instead of unreffing the object directly.
6341                  */
6342                 if (!gst_bin_remove(GST_BIN(mainbin[MMPLAYER_M_PIPE].gst), queue2)) {
6343                         LOGE("failed to remove queue2");
6344                         gst_object_unref(queue2);
6345                 }
6346                 queue2 = NULL;
6347         }
6348
6349         if (decodebin) {
6350                 /* NOTE : Trying to dispose element queue0, but it is in READY instead of the NULL state.
6351                  * You need to explicitly set elements to the NULL state before
6352                  * dropping the final reference, to allow them to clean up.
6353                  */
6354                 gst_element_set_state(decodebin, GST_STATE_NULL);
6355
6356                 /* And, it still has a parent "player".
6357                  * You need to let the parent manage the object instead of unreffing the object directly.
6358                  */
6359
6360                 if (!gst_bin_remove(GST_BIN(mainbin[MMPLAYER_M_PIPE].gst), decodebin)) {
6361                         LOGE("failed to remove decodebin");
6362                         gst_object_unref(decodebin);
6363                 }
6364                 decodebin = NULL;
6365         }
6366
6367         return FALSE;
6368 }
6369
6370 int
6371 _mmplayer_update_not_supported_codec_info(mmplayer_t *player, const gchar *factory_class, const gchar *mime)
6372 {
6373         MMPLAYER_FENTER();
6374
6375         MMPLAYER_RETURN_VAL_IF_FAIL(player && player->pipeline, MM_ERROR_PLAYER_NOT_INITIALIZED);
6376         MMPLAYER_RETURN_VAL_IF_FAIL(mime, MM_ERROR_INVALID_ARGUMENT);
6377
6378         LOGD("class : %s, mime : %s", factory_class, mime);
6379
6380         /* add missing plugin */
6381         /* NOTE : msl should check missing plugin for image mime type.
6382          * Some motion jpeg clips can have playable audio track.
6383          * So, msl have to play audio after displaying popup written video format not supported.
6384          */
6385         if (!(player->pipeline->mainbin[MMPLAYER_M_DEMUX].gst)) {
6386                 if (!(player->can_support_codec | player->videodec_linked | player->audiodec_linked)) {
6387                         LOGD("not found demuxer");
6388                         player->not_found_demuxer = TRUE;
6389                         player->unlinked_demuxer_mime = g_strdup_printf("%s", mime);
6390
6391                         goto DONE;
6392                 }
6393         }
6394
6395         if (!g_strrstr(factory_class, "Demuxer")) {
6396                 if ((g_str_has_prefix(mime, "video")) || (g_str_has_prefix(mime, "image"))) {
6397                         LOGD("can support codec=0x%X, vdec_linked=%d, adec_linked=%d",
6398                                 player->can_support_codec, player->videodec_linked, player->audiodec_linked);
6399
6400                         /* check that clip have multi tracks or not */
6401                         if ((player->can_support_codec & FOUND_PLUGIN_VIDEO) && (player->videodec_linked)) {
6402                                 LOGD("video plugin is already linked");
6403                         } else {
6404                                 LOGW("add VIDEO to missing plugin");
6405                                 player->not_supported_codec |= MISSING_PLUGIN_VIDEO;
6406                                 player->unlinked_video_mime = g_strdup_printf("%s", mime);
6407                         }
6408                 } else if (g_str_has_prefix(mime, "audio")) {
6409                         if ((player->can_support_codec & FOUND_PLUGIN_AUDIO) && (player->audiodec_linked)) {
6410                                 LOGD("audio plugin is already linked");
6411                         } else {
6412                                 LOGW("add AUDIO to missing plugin");
6413                                 player->not_supported_codec |= MISSING_PLUGIN_AUDIO;
6414                                 player->unlinked_audio_mime = g_strdup_printf("%s", mime);
6415                         }
6416                 }
6417         }
6418
6419 DONE:
6420         MMPLAYER_FLEAVE();
6421
6422         return MM_ERROR_NONE;
6423 }
6424
6425 void
6426 _mmplayer_pipeline_complete(GstElement *decodebin, gpointer data)
6427 {
6428         mmplayer_t *player = (mmplayer_t *)data;
6429
6430         MMPLAYER_FENTER();
6431
6432         MMPLAYER_RETURN_IF_FAIL(player);
6433
6434         /* remove fakesink. */
6435         if (!_mmplayer_gst_remove_fakesink(player,
6436                                 &player->pipeline->mainbin[MMPLAYER_M_SRC_FAKESINK])) {
6437                 /* NOTE : _mmplayer_pipeline_complete() can be called several time. because
6438                  * signaling mechanism(pad-added, no-more-pad, new-decoded-pad) from various
6439                  * source element are not same. To overcome this situation, this function will called
6440                  * several places and several times. Therefore, this is not an error case.
6441                  */
6442                 return;
6443         }
6444
6445         LOGD("[handle: %p] pipeline has completely constructed", player);
6446
6447         if ((player->msg_posted == FALSE) &&
6448                 (player->cmd >= MMPLAYER_COMMAND_START))
6449                 __mmplayer_handle_missed_plugin(player);
6450
6451         MMPLAYER_GENERATE_DOT_IF_ENABLED(player, "pipeline-status-complete");
6452 }
6453
6454 static int
6455 __mmplayer_check_profile(void)
6456 {
6457         char *profileName;
6458         static int profile_tv = -1;
6459
6460         if (__builtin_expect(profile_tv != -1, 1))
6461                 return profile_tv;
6462
6463         system_info_get_platform_string("http://tizen.org/feature/profile", &profileName);
6464         switch (*profileName) {
6465         case 't':
6466         case 'T':
6467                 profile_tv = 1;
6468                 break;
6469         default:
6470                 profile_tv = 0;
6471                 break;
6472         }
6473         free(profileName);
6474
6475         return profile_tv;
6476 }
6477
6478 static gboolean
6479 __mmplayer_get_next_uri(mmplayer_t *player)
6480 {
6481         mmplayer_parse_profile_t profile;
6482         gint uri_idx = 0;
6483         guint num_of_list = 0;
6484         char *uri = NULL;
6485
6486         num_of_list = g_list_length(player->uri_info.uri_list);
6487         uri_idx = player->uri_info.uri_idx;
6488
6489         LOGD("num of uri list = %d, current uri idx %d", num_of_list, uri_idx);
6490         for (uri_idx++; uri_idx < num_of_list; uri_idx++) {
6491                 uri = g_list_nth_data(player->uri_info.uri_list, uri_idx);
6492                 if (!uri) {
6493                         LOGW("next uri does not exist");
6494                         continue;
6495                 }
6496
6497                 if (_mmplayer_parse_profile((const char *)uri, NULL, &profile) != MM_ERROR_NONE) {
6498                         LOGE("failed to parse profile");
6499                         continue;
6500                 }
6501
6502                 if ((profile.uri_type != MM_PLAYER_URI_TYPE_FILE) &&
6503                         (profile.uri_type != MM_PLAYER_URI_TYPE_URL_HTTP)) {
6504                         LOGW("uri type is not supported(%d)", profile.uri_type);
6505                         continue;
6506                 }
6507
6508                 LOGD("success to find next uri %d", uri_idx);
6509                 break;
6510         }
6511
6512         if (!uri || uri_idx == num_of_list) {
6513                 LOGE("failed to find next uri");
6514                 return FALSE;
6515         }
6516
6517         player->uri_info.uri_idx = uri_idx;
6518         if (mm_player_set_attribute((MMHandleType)player, NULL,
6519                         "profile_uri", uri, strlen(uri), NULL) != MM_ERROR_NONE) {
6520                 LOGE("failed to set attribute");
6521                 return FALSE;
6522         }
6523
6524         SECURE_LOGD("next playback uri: %s", uri);
6525         return TRUE;
6526 }
6527
6528 static gboolean
6529 __mmplayer_verify_gapless_play_path(mmplayer_t *player)
6530 {
6531 #define REPEAT_COUNT_INFINITE -1
6532 #define REPEAT_COUNT_MIN 2
6533 #define ORIGINAL_URI_ONLY 1
6534
6535         MMHandleType attrs = 0;
6536         gint video = 0;
6537         gint count = 0;
6538         gint gapless = 0;
6539         guint num_of_uri = 0;
6540         int profile_tv = -1;
6541
6542         MMPLAYER_FENTER();
6543
6544         LOGD("checking for gapless play option");
6545
6546         if (player->build_audio_offload) {
6547                 LOGE("offload path is not supportable.");
6548                 goto ERROR;
6549         }
6550
6551         if (player->pipeline->textbin) {
6552                 LOGE("subtitle path is enabled. gapless play is not supported.");
6553                 goto ERROR;
6554         }
6555
6556         attrs = MMPLAYER_GET_ATTRS(player);
6557         if (!attrs) {
6558                 LOGE("fail to get attributes.");
6559                 goto ERROR;
6560         }
6561
6562         mm_attrs_multiple_get(player->attrs, NULL,
6563                         "content_video_found", &video,
6564                         "profile_play_count", &count,
6565                         MM_PLAYER_GAPLESS_MODE, &gapless, NULL);
6566
6567         /* gapless playback is not supported in case of video at TV profile. */
6568         profile_tv = __mmplayer_check_profile();
6569         if (profile_tv && video) {
6570                 LOGW("not support video gapless playback");
6571                 goto ERROR;
6572         }
6573
6574         /* check repeat count in case of audio */
6575         if (!gapless &&
6576                 (video || (count != REPEAT_COUNT_INFINITE && count < REPEAT_COUNT_MIN))) {
6577                 LOGW("gapless is disabled");
6578                 goto ERROR;
6579         }
6580
6581         num_of_uri = g_list_length(player->uri_info.uri_list);
6582
6583         LOGD("repeat count = %d, num_of_list = %d", count, num_of_uri);
6584
6585         if (num_of_uri == ORIGINAL_URI_ONLY) {
6586                 /* audio looping path */
6587                 if (count >= REPEAT_COUNT_MIN) {
6588                         /* decrease play count */
6589                         /* we succeeded to rewind. update play count and then wait for next EOS */
6590                         count--;
6591                         mm_player_set_attribute((MMHandleType)player, NULL, "profile_play_count", count, NULL);
6592                 } else if (count != REPEAT_COUNT_INFINITE) {
6593                         LOGD("there is no next uri and no repeat");
6594                         goto ERROR;
6595                 }
6596                 LOGD("looping cnt %d", count);
6597         } else {
6598                 /* gapless playback path */
6599                 if (!__mmplayer_get_next_uri(player)) {
6600                         LOGE("failed to get next uri");
6601                         goto ERROR;
6602                 }
6603         }
6604         return TRUE;
6605
6606 ERROR:
6607         LOGE("unable to play gapless path. EOS will be posted soon");
6608         return FALSE;
6609 }
6610
6611 static void
6612 __mmplayer_remove_sinkpad (const GValue *item, gpointer user_data)
6613 {
6614         GstPad *sinkpad = g_value_get_object (item);
6615         GstElement *element = GST_ELEMENT(user_data);
6616         if (!sinkpad || !element) {
6617                 LOGE("invalid parameter");
6618                 return;
6619         }
6620
6621         LOGD("(%s)element release request pad(%s)", GST_ELEMENT_NAME(element), GST_PAD_NAME(sinkpad));
6622         gst_element_release_request_pad(element, GST_PAD(sinkpad));
6623 }
6624
6625 static gboolean
6626 __mmplayer_deactivate_combiner(mmplayer_t *player, mmplayer_track_type_e type)
6627 {
6628         mmplayer_gst_element_t *sinkbin = NULL;
6629         main_element_id_e concatId = MMPLAYER_M_NUM;
6630         main_element_id_e sinkId = MMPLAYER_M_NUM;
6631         gboolean send_notice = FALSE;
6632         GstElement *element;
6633         GstIterator *iter;
6634
6635         MMPLAYER_FENTER();
6636         MMPLAYER_RETURN_VAL_IF_FAIL(player, FALSE);
6637
6638         LOGD("type %d", type);
6639
6640         switch (type) {
6641         case MM_PLAYER_TRACK_TYPE_AUDIO:
6642                 concatId = MMPLAYER_M_A_CONCAT;
6643                 sinkId = MMPLAYER_A_BIN;
6644                 sinkbin = player->pipeline->audiobin;
6645                 break;
6646         case MM_PLAYER_TRACK_TYPE_VIDEO:
6647                 concatId = MMPLAYER_M_V_CONCAT;
6648                 sinkId = MMPLAYER_V_BIN;
6649                 sinkbin = player->pipeline->videobin;
6650                 send_notice = TRUE;
6651                 break;
6652         case MM_PLAYER_TRACK_TYPE_TEXT:
6653                 concatId = MMPLAYER_M_T_CONCAT;
6654                 sinkId = MMPLAYER_T_BIN;
6655                 sinkbin = player->pipeline->textbin;
6656                 break;
6657         default:
6658                 LOGE("requested type is not supportable");
6659                 return FALSE;
6660                 break;
6661         }
6662
6663         element = player->pipeline->mainbin[concatId].gst;
6664         if (!element)
6665                 return TRUE;
6666
6667         if ((sinkbin) && (sinkbin[sinkId].gst)) {
6668                 GstPad *srcpad = gst_element_get_static_pad(element, "src");
6669                 GstPad *sinkpad = gst_element_get_static_pad(sinkbin[sinkId].gst, "sink");
6670                 if (srcpad && sinkpad) {
6671                         /* after getting drained signal there is no data flows, so no need to do pad_block */
6672                         LOGD("unlink %s:%s, %s:%s", GST_DEBUG_PAD_NAME(srcpad), GST_DEBUG_PAD_NAME(sinkpad));
6673                         gst_pad_unlink(srcpad, sinkpad);
6674
6675                         /* send custom event to sink pad to handle it at video sink */
6676                         if (send_notice) {
6677                                 LOGD("send custom event to sinkpad");
6678                                 GstStructure *s = gst_structure_new_empty("tizen/flush-buffer");
6679                                 GstEvent *event = gst_event_new_custom(GST_EVENT_CUSTOM_DOWNSTREAM, s);
6680                                 gst_pad_send_event(sinkpad, event);
6681                         }
6682                 }
6683                 gst_object_unref(srcpad);
6684                 gst_object_unref(sinkpad);
6685         }
6686
6687         LOGD("release concat request pad");
6688         /* release and unref requests pad from the selector */
6689         iter = gst_element_iterate_sink_pads(element);
6690         while (gst_iterator_foreach(iter, __mmplayer_remove_sinkpad, element) == GST_ITERATOR_RESYNC)
6691                 gst_iterator_resync(iter);
6692         gst_iterator_free(iter);
6693
6694         return TRUE;
6695 }
6696
6697 static gboolean
6698 __mmplayer_deactivate_selector(mmplayer_t *player, mmplayer_track_type_e type)
6699 {
6700         mmplayer_track_t *selector = &player->track[type];
6701         mmplayer_gst_element_t *sinkbin = NULL;
6702         main_element_id_e selectorId = MMPLAYER_M_NUM;
6703         main_element_id_e sinkId = MMPLAYER_M_NUM;
6704         GstPad *srcpad = NULL;
6705         GstPad *sinkpad = NULL;
6706         gboolean send_notice = FALSE;
6707
6708         MMPLAYER_FENTER();
6709         MMPLAYER_RETURN_VAL_IF_FAIL(player, FALSE);
6710
6711         LOGD("type %d", type);
6712
6713         switch (type) {
6714         case MM_PLAYER_TRACK_TYPE_AUDIO:
6715                 selectorId = MMPLAYER_M_A_INPUT_SELECTOR;
6716                 sinkId = MMPLAYER_A_BIN;
6717                 sinkbin = player->pipeline->audiobin;
6718                 break;
6719         case MM_PLAYER_TRACK_TYPE_VIDEO:
6720                 selectorId = MMPLAYER_M_V_INPUT_SELECTOR;
6721                 sinkId = MMPLAYER_V_BIN;
6722                 sinkbin = player->pipeline->videobin;
6723                 send_notice = TRUE;
6724                 break;
6725         case MM_PLAYER_TRACK_TYPE_TEXT:
6726                 selectorId = MMPLAYER_M_T_INPUT_SELECTOR;
6727                 sinkId = MMPLAYER_T_BIN;
6728                 sinkbin = player->pipeline->textbin;
6729                 break;
6730         default:
6731                 LOGE("requested type is not supportable");
6732                 return FALSE;
6733                 break;
6734         }
6735
6736         if (player->pipeline->mainbin[selectorId].gst) {
6737                 gint n;
6738
6739                 srcpad = gst_element_get_static_pad(player->pipeline->mainbin[selectorId].gst, "src");
6740
6741                 if (selector->event_probe_id != 0)
6742                         gst_pad_remove_probe(srcpad, selector->event_probe_id);
6743                 selector->event_probe_id = 0;
6744
6745                 if ((sinkbin) && (sinkbin[sinkId].gst)) {
6746                         sinkpad = gst_element_get_static_pad(sinkbin[sinkId].gst, "sink");
6747
6748                         if (srcpad && sinkpad) {
6749                                 /* after getting drained signal there is no data flows, so no need to do pad_block */
6750                                 LOGD("unlink %s:%s, %s:%s", GST_DEBUG_PAD_NAME(srcpad), GST_DEBUG_PAD_NAME(sinkpad));
6751                                 gst_pad_unlink(srcpad, sinkpad);
6752
6753                                 /* send custom event to sink pad to handle it at video sink */
6754                                 if (send_notice) {
6755                                         LOGD("send custom event to sinkpad");
6756                                         GstStructure *s = gst_structure_new_empty("tizen/flush-buffer");
6757                                         GstEvent *event = gst_event_new_custom(GST_EVENT_CUSTOM_DOWNSTREAM, s);
6758                                         gst_pad_send_event(sinkpad, event);
6759                                 }
6760                         }
6761
6762                         gst_object_unref(sinkpad);
6763                         sinkpad = NULL;
6764                 }
6765                 gst_object_unref(srcpad);
6766                 srcpad = NULL;
6767
6768                 LOGD("selector release");
6769
6770                 /* release and unref requests pad from the selector */
6771                 for (n = 0; n < selector->streams->len; n++) {
6772                         GstPad *sinkpad = g_ptr_array_index(selector->streams, n);
6773                         gst_element_release_request_pad((player->pipeline->mainbin[selectorId].gst), sinkpad);
6774                 }
6775
6776                 g_ptr_array_set_size(selector->streams, 0);
6777
6778                 gst_element_set_state(player->pipeline->mainbin[selectorId].gst, GST_STATE_NULL);
6779                 if (!gst_bin_remove(GST_BIN_CAST(player->pipeline->mainbin[MMPLAYER_M_PIPE].gst),
6780                                                         player->pipeline->mainbin[selectorId].gst)) {
6781                         LOGE("failed to remove selector");
6782                         gst_object_unref(player->pipeline->mainbin[selectorId].gst);
6783                 }
6784
6785                 player->pipeline->mainbin[selectorId].gst = NULL;
6786                 selector = NULL;
6787         }
6788
6789         return TRUE;
6790 }
6791
6792 static void
6793 __mmplayer_deactivate_old_path(mmplayer_t *player)
6794 {
6795         MMPLAYER_FENTER();
6796         MMPLAYER_RETURN_IF_FAIL(player);
6797
6798         if (MMPLAYER_USE_DECODEBIN(player)) {
6799                 if ((!__mmplayer_deactivate_selector(player, MM_PLAYER_TRACK_TYPE_AUDIO)) ||
6800                         (!__mmplayer_deactivate_selector(player, MM_PLAYER_TRACK_TYPE_VIDEO)) ||
6801                         (!__mmplayer_deactivate_selector(player, MM_PLAYER_TRACK_TYPE_TEXT))) {
6802                         LOGE("deactivate selector error");
6803                         goto ERROR;
6804                 }
6805         } else {
6806                 if ((!__mmplayer_deactivate_combiner(player, MM_PLAYER_TRACK_TYPE_AUDIO)) ||
6807                         (!__mmplayer_deactivate_combiner(player, MM_PLAYER_TRACK_TYPE_VIDEO)) ||
6808                         (!__mmplayer_deactivate_combiner(player, MM_PLAYER_TRACK_TYPE_TEXT))) {
6809                         LOGE("deactivate concat error");
6810                         goto ERROR;
6811                 }
6812         }
6813
6814         _mmplayer_track_destroy(player);
6815         __mmplayer_release_signal_connection(player, MM_PLAYER_SIGNAL_TYPE_AUTOPLUG);
6816
6817         if (player->streamer) {
6818                 _mm_player_streaming_initialize(player->streamer, FALSE);
6819                 _mm_player_streaming_destroy(player->streamer);
6820                 player->streamer = NULL;
6821         }
6822
6823         MMPLAYER_GAPLESS_PLAY_THREAD_SIGNAL(player);
6824         MMPLAYER_FLEAVE();
6825         return;
6826
6827 ERROR:
6828
6829         if (!player->msg_posted) {
6830                 MMMessageParamType msg = {0,};
6831
6832                 /*post error*/
6833                 msg.code = MM_ERROR_PLAYER_INTERNAL;
6834                 LOGE("gapless_uri_play> deactivate error");
6835
6836                 MMPLAYER_POST_MSG(player, MM_MESSAGE_ERROR, &msg);
6837                 player->msg_posted = TRUE;
6838         }
6839         return;
6840 }
6841
6842 int
6843 _mmplayer_set_uri(MMHandleType hplayer, const char *uri)
6844 {
6845         int result = MM_ERROR_NONE;
6846         mmplayer_t *player = (mmplayer_t *)hplayer;
6847         MMPLAYER_FENTER();
6848
6849         MMPLAYER_RETURN_VAL_IF_FAIL(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
6850         MMPLAYER_RETURN_VAL_IF_FAIL(uri, MM_ERROR_INVALID_ARGUMENT);
6851
6852         if (mm_player_set_attribute(hplayer, NULL,
6853                         "profile_uri", uri, strlen(uri), NULL) != MM_ERROR_NONE) {
6854                 LOGE("failed to set attribute");
6855                 result = MM_ERROR_PLAYER_INTERNAL;
6856         } else {
6857                 if (_mmplayer_set_next_uri(hplayer, uri, TRUE) != MM_ERROR_NONE)
6858                         LOGE("failed to add the original uri in the uri list.");
6859         }
6860
6861         MMPLAYER_FLEAVE();
6862         return result;
6863 }
6864
6865 int
6866 _mmplayer_set_next_uri(MMHandleType hplayer, const char *uri, bool is_first_path)
6867 {
6868         mmplayer_t *player = (mmplayer_t *)hplayer;
6869         guint num_of_list = 0;
6870
6871         MMPLAYER_FENTER();
6872
6873         MMPLAYER_RETURN_VAL_IF_FAIL(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
6874         MMPLAYER_RETURN_VAL_IF_FAIL(uri, MM_ERROR_INVALID_ARGUMENT);
6875
6876         if (player->pipeline && player->pipeline->textbin) {
6877                 LOGE("subtitle path is enabled.");
6878                 return MM_ERROR_PLAYER_INVALID_STATE;
6879         }
6880
6881         num_of_list = g_list_length(player->uri_info.uri_list);
6882
6883         if (is_first_path) {
6884                 if (num_of_list == 0) {
6885                         player->uri_info.uri_list = g_list_append(player->uri_info.uri_list, g_strdup(uri));
6886                         SECURE_LOGD("add original path : %s", uri);
6887                 } else {
6888                         g_free(g_list_nth_data(player->uri_info.uri_list, 0));
6889                         player->uri_info.uri_list = g_list_prepend(
6890                                 g_list_delete_link(player->uri_info.uri_list, player->uri_info.uri_list), g_strdup(uri));
6891                         SECURE_LOGD("change original path : %s", uri);
6892                 }
6893         } else {
6894                 MMHandleType attrs = 0;
6895                 attrs = MMPLAYER_GET_ATTRS(player);
6896
6897                 if (num_of_list == 0) {
6898                         char *original_uri = NULL;
6899
6900                         if (attrs) {
6901                                 mm_attrs_get_string_by_name(attrs, "profile_uri", &original_uri);
6902
6903                                 if (!original_uri) {
6904                                         LOGE("there is no original uri.");
6905                                         return MM_ERROR_PLAYER_INVALID_STATE;
6906                                 }
6907
6908                                 player->uri_info.uri_list = g_list_append(player->uri_info.uri_list, g_strdup(original_uri));
6909                                 player->uri_info.uri_idx = 0;
6910
6911                                 SECURE_LOGD("add original path at first : %s", original_uri);
6912                         }
6913                 }
6914
6915                 player->uri_info.uri_list = g_list_append(player->uri_info.uri_list, g_strdup(uri));
6916                 SECURE_LOGD("add new path : %s(total num of list = %d)", uri, g_list_length(player->uri_info.uri_list));
6917         }
6918
6919         MMPLAYER_FLEAVE();
6920         return MM_ERROR_NONE;
6921 }
6922
6923 int
6924 _mmplayer_get_next_uri(MMHandleType hplayer, char **uri)
6925 {
6926         mmplayer_t *player = (mmplayer_t *)hplayer;
6927         char *next_uri = NULL;
6928         guint num_of_list = 0;
6929
6930         MMPLAYER_FENTER();
6931         MMPLAYER_RETURN_VAL_IF_FAIL(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
6932
6933         num_of_list = g_list_length(player->uri_info.uri_list);
6934
6935         if (num_of_list > 0) {
6936                 gint uri_idx = player->uri_info.uri_idx;
6937
6938                 if (uri_idx < num_of_list - 1)
6939                         uri_idx++;
6940                 else
6941                         uri_idx = 0;
6942
6943                 next_uri = g_list_nth_data(player->uri_info.uri_list, uri_idx);
6944                 LOGE("next uri idx : %d, uri = %s", uri_idx, next_uri);
6945
6946                 *uri = g_strdup(next_uri);
6947         }
6948
6949         MMPLAYER_FLEAVE();
6950         return MM_ERROR_NONE;
6951 }
6952
6953 void
6954 _mmplayer_gst_decode_unknown_type(GstElement *elem, GstPad *pad,
6955         GstCaps *caps, gpointer data)
6956 {
6957         mmplayer_t *player = (mmplayer_t *)data;
6958         const gchar *klass = NULL;
6959         const gchar *mime = NULL;
6960         gchar *caps_str = NULL;
6961
6962         klass = gst_element_factory_get_metadata(gst_element_get_factory(elem), GST_ELEMENT_METADATA_KLASS);
6963         mime = gst_structure_get_name(gst_caps_get_structure(caps, 0));
6964         caps_str = gst_caps_to_string(caps);
6965
6966         LOGW("unknown type of caps : %s from %s",
6967                                         caps_str, GST_ELEMENT_NAME(elem));
6968
6969         MMPLAYER_FREEIF(caps_str);
6970
6971         /* There is no available codec. */
6972         _mmplayer_update_not_supported_codec_info(player, klass, mime);
6973 }
6974
6975 gboolean
6976 _mmplayer_gst_decode_autoplug_continue(GstElement *bin, GstPad *pad,
6977         GstCaps *caps, gpointer data)
6978 {
6979         mmplayer_t *player = (mmplayer_t *)data;
6980         const char *mime = NULL;
6981         gboolean ret = TRUE;
6982
6983         MMPLAYER_LOG_GST_CAPS_TYPE(caps);
6984         mime = gst_structure_get_name(gst_caps_get_structure(caps, 0));
6985
6986         if (g_str_has_prefix(mime, "audio")) {
6987                 GstStructure *caps_structure = NULL;
6988                 gint samplerate = 0;
6989                 gint channels = 0;
6990                 gchar *caps_str = NULL;
6991
6992                 caps_structure = gst_caps_get_structure(caps, 0);
6993                 gst_structure_get_int(caps_structure, "rate", &samplerate);
6994                 gst_structure_get_int(caps_structure, "channels", &channels);
6995
6996                 if ((channels > 0 && samplerate == 0)) {
6997                         LOGD("exclude audio...");
6998                         ret = FALSE;
6999                 }
7000
7001                 caps_str = gst_caps_to_string(caps);
7002                 /* set it directly because not sent by TAG */
7003                 if (g_strrstr(caps_str, "mobile-xmf"))
7004                         mm_player_set_attribute((MMHandleType)player, NULL,
7005                                         "content_audio_codec", "mobile-xmf", strlen("mobile-xmf"), NULL);
7006
7007                 MMPLAYER_FREEIF(caps_str);
7008         } else if (g_str_has_prefix(mime, "video") && player->videodec_linked) {
7009                 if((MMPLAYER_IS_HTTP_LIVE_STREAMING(player)) || (MMPLAYER_IS_DASH_STREAMING(player))) {
7010                         LOGD("video is already linked, allow the stream switch");
7011                         ret = TRUE;
7012                 } else {
7013                         LOGD("video is already linked");
7014                         ret = FALSE;
7015                 }
7016         } else {
7017                 LOGD("found new stream");
7018         }
7019
7020         return ret;
7021 }
7022
7023 static gboolean
7024 __mmplayer_is_audio_offload_device_type(mmplayer_t *player)
7025 {
7026         gboolean ret = FALSE;
7027         GDBusConnection *conn = NULL;
7028         GError *err = NULL;
7029         GVariant *result = NULL;
7030         const gchar *dbus_device_type = NULL;
7031         const gchar *dbus_ret = NULL;
7032         gint idx = 0;
7033
7034         conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
7035         if (!conn || err) {
7036                 LOGE("failed g_bus_get_sync() (%s)", (err ? err->message : "null"));
7037                 g_error_free(err);
7038                 goto DONE;
7039         }
7040
7041         result = g_dbus_connection_call_sync(conn,
7042                                         "org.pulseaudio.Server",
7043                                         "/org/pulseaudio/StreamManager",
7044                                         "org.pulseaudio.StreamManager",
7045                                         "GetCurrentMediaRoutingPath",
7046                                         g_variant_new("(s)", "out"),
7047                                         G_VARIANT_TYPE("(ss)"),
7048                                         G_DBUS_CALL_FLAGS_NONE,
7049                                         2000,
7050                                         NULL,
7051                                         &err);
7052         if (!result || err) {
7053                 LOGE("failed g_dbus_connection_call_sync() (%s)", (err ? err->message : "null"));
7054                 g_error_free(err);
7055                 goto DONE;
7056         }
7057
7058         /* device type is listed in stream-map.json at mmfw-sysconf */
7059         g_variant_get(result, "(&s&s)", &dbus_device_type, &dbus_ret);
7060
7061         LOGI("g_dbus_connection_call_sync() success (%s, %s)", dbus_device_type, dbus_ret);
7062         if (strncmp("STREAM_MANAGER_RETURN_OK", dbus_ret, strlen(dbus_ret)))
7063                 goto DONE;
7064
7065         /* the device type is listed in ini file among audio-jack, bt-a2dp, usb-audio, builtin-speaker */
7066         for (idx = 0; player->ini.audio_offload_device_type[idx][0] != '\0'; idx++) {
7067                 if (strstr(dbus_device_type, player->ini.audio_offload_device_type[idx])) {
7068                         LOGD("audio offload is supportable");
7069                         ret = TRUE;
7070                         goto DONE;
7071                 }
7072         }
7073
7074         LOGD("audio offload is not supportable");
7075
7076 DONE:
7077         g_variant_unref(result);
7078         if (conn)
7079                 g_object_unref(conn);
7080
7081         return ret;
7082 }
7083
7084 static void __mmplayer_rebuild_audio_pipeline(mmplayer_t *player)
7085 {
7086         mmplayer_state_e current_state = MM_PLAYER_STATE_NONE;
7087         gint64 position = 0;
7088
7089         MMPLAYER_RETURN_IF_FAIL(player && player->attrs &&
7090                 player->pipeline && player->pipeline->mainbin);
7091
7092         MMPLAYER_CMD_LOCK(player);
7093         current_state = MMPLAYER_CURRENT_STATE(player);
7094
7095         if (!gst_element_query_position(player->pipeline->mainbin[MMPLAYER_M_PIPE].gst, GST_FORMAT_TIME, &position))
7096                 LOGW("getting current position failed in paused");
7097
7098         _mmplayer_unrealize((MMHandleType)player);
7099         _mmplayer_realize((MMHandleType)player);
7100
7101         _mmplayer_set_position((MMHandleType)player, position);
7102
7103         /* async not to be blocked in streaming case */
7104         mm_player_set_attribute((MMHandleType)player, NULL, "profile_prepare_async", TRUE, NULL);
7105
7106         _mmplayer_pause((MMHandleType)player);
7107
7108         if (current_state == MM_PLAYER_STATE_PLAYING)
7109                 _mmplayer_start((MMHandleType)player);
7110         MMPLAYER_CMD_UNLOCK(player);
7111
7112         LOGD("rebuilding audio pipeline is completed.");
7113 }
7114
7115 void __mmplayer_audio_device_connected_cb(MMSoundDevice_t device_h, bool is_connected, void *user_data)
7116 {
7117         mmplayer_t *player = (mmplayer_t *)user_data;
7118         mm_sound_device_type_e dev_type = MM_SOUND_DEVICE_TYPE_BUILTIN_SPEAKER;
7119         gboolean is_supportable = FALSE;
7120
7121         if (mm_sound_get_device_type(device_h, &dev_type) != MM_ERROR_NONE)
7122                 LOGW("failed to get device type");
7123         else
7124                 LOGD("dev type (%d), connected (%d)", dev_type, is_connected);
7125
7126         if ((dev_type != MM_SOUND_DEVICE_TYPE_BLUETOOTH_A2DP) &&
7127                 (dev_type != MM_SOUND_DEVICE_TYPE_AUDIOJACK) &&
7128                 (dev_type != MM_SOUND_DEVICE_TYPE_USB_AUDIO)) {
7129                 LOGD("ignore this dev connected info");
7130                 return;
7131         }
7132
7133         is_supportable = __mmplayer_is_audio_offload_device_type(player);
7134         if (player->build_audio_offload == is_supportable) {
7135                 LOGD("keep current pipeline without re-building");
7136                 return;
7137         }
7138
7139         /* rebuild pipeline */
7140         LOGD("re-build pipeline - offload: %d", is_supportable);
7141         player->build_audio_offload = FALSE;
7142         __mmplayer_rebuild_audio_pipeline(player);
7143
7144         return;
7145 }
7146
7147 static gboolean
7148 __mmplayer_add_audio_device_connected_cb(mmplayer_t *player)
7149 {
7150         unsigned int id = 0;
7151
7152         if (player->audio_device_cb_id != 0) {
7153                 LOGW("audio device connected cb was already added (%u)", player->audio_device_cb_id);
7154                 return TRUE;
7155         }
7156
7157         if (mm_sound_add_device_connected_callback(MM_SOUND_DEVICE_IO_DIRECTION_OUT_FLAG,
7158                                 __mmplayer_audio_device_connected_cb, player, &id) == MM_ERROR_NONE) {
7159                 LOGD("added device connected cb (%u)", id);
7160                 player->audio_device_cb_id = id;
7161         } else {
7162                 LOGW("failed to add device connected cb");
7163                 return FALSE;
7164         }
7165
7166         return TRUE;
7167 }
7168
7169 int _mmplayer_audio_offload_is_activated(MMHandleType hplayer, bool *activated)
7170 {
7171         mmplayer_t *player = (mmplayer_t *)hplayer;
7172
7173         MMPLAYER_FENTER();
7174         MMPLAYER_RETURN_VAL_IF_FAIL(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
7175         MMPLAYER_RETURN_VAL_IF_FAIL(activated, MM_ERROR_INVALID_ARGUMENT);
7176
7177         *activated = player->build_audio_offload;
7178
7179         LOGD("offload activated : %d", (int)*activated);
7180
7181         MMPLAYER_FLEAVE();
7182         return MM_ERROR_NONE;
7183 }
7184
7185 static gboolean
7186 __mmplayer_is_offload_supported_type(mmplayer_t *player)
7187 {
7188         /* NOTE :
7189            this function need to be updated according to the supported media format
7190            @see player->ini.audio_offload_media_format */
7191
7192         if (__mmplayer_is_only_mp3_type(player->type_caps_str)) {
7193                 LOGD("offload supportable media format type");
7194                 return TRUE;
7195         }
7196
7197         return FALSE;
7198 }
7199
7200 static gboolean
7201 __mmplayer_can_build_audio_offload_path(mmplayer_t *player)
7202 {
7203         gboolean ret = FALSE;
7204         GstElementFactory *factory = NULL;
7205
7206         MMPLAYER_FENTER();
7207         MMPLAYER_RETURN_VAL_IF_FAIL(player && player->attrs, FALSE);
7208
7209         LOGD("current stream : %s, sink: %s", player->type_caps_str, player->ini.audio_offload_sink_element);
7210         if (!__mmplayer_is_offload_supported_type(player))
7211                 goto DONE;
7212
7213         if (!strcmp(player->ini.audio_offload_sink_element, "")) {
7214                 LOGD("there is no audio offload sink");
7215                 goto DONE;
7216         }
7217
7218         if (player->ini.audio_offload_device_type[0][0] == '\0') {
7219                 LOGW("there is no audio device type to support offload");
7220                 goto DONE;
7221         }
7222
7223         factory = gst_element_factory_find(player->ini.audio_offload_sink_element);
7224         if (!factory) {
7225                 LOGW("there is no installed audio offload sink element");
7226                 goto DONE;
7227         }
7228         gst_object_unref(factory);
7229
7230         if (_mmplayer_acquire_hw_resource(player,
7231                         MMPLAYER_RESOURCE_TYPE_AUDIO_OFFLOAD) != MM_ERROR_NONE) {
7232                 LOGE("failed to acquire audio offload decoder resource");
7233                 goto DONE;
7234         }
7235
7236         if (!__mmplayer_add_audio_device_connected_cb(player))
7237                 goto DONE;
7238
7239         if (!__mmplayer_is_audio_offload_device_type(player))
7240                 goto DONE;
7241
7242         LOGD("audio offload can be built");
7243         ret = TRUE;
7244
7245 DONE:
7246         if (!ret)
7247                 __mmplayer_release_hw_resource(player, MMPLAYER_RESOURCE_TYPE_AUDIO_OFFLOAD);
7248
7249         MMPLAYER_FLEAVE();
7250         return ret;
7251 }
7252
7253 static GstAutoplugSelectResult
7254 __mmplayer_check_codec_info(mmplayer_t *player, const char *klass, GstCaps *caps, char *factory_name)
7255 {
7256         GstAutoplugSelectResult ret = GST_AUTOPLUG_SELECT_TRY;
7257         int audio_offload = 0;
7258
7259         if ((g_strrstr(klass, "Codec/Decoder/Audio"))) {
7260                 mm_attrs_get_int_by_name(player->attrs, MM_PLAYER_AUDIO_OFFLOAD, &audio_offload); /* user requirement */
7261
7262                 if (audio_offload && __mmplayer_can_build_audio_offload_path(player)) {
7263                         LOGD("expose audio path to build offload output path");
7264                         player->build_audio_offload = TRUE;
7265                         /* update codec info */
7266                         player->not_supported_codec &= MISSING_PLUGIN_VIDEO;
7267                         player->can_support_codec |= FOUND_PLUGIN_AUDIO;
7268                         player->audiodec_linked = 1;
7269
7270                         ret = GST_AUTOPLUG_SELECT_EXPOSE;
7271                         goto DONE;
7272                 }
7273
7274                 /* FIXME: If HW audio decoder is selected, related resource have to be acquired here.
7275                                   And need to consider the multi-track audio content.
7276                           There is no HW audio decoder in public. */
7277
7278                 /* set stream information */
7279                 if (!player->audiodec_linked)
7280                         _mmplayer_set_audio_attrs(player, caps);
7281
7282                 /* update codec info */
7283                 player->not_supported_codec &= MISSING_PLUGIN_VIDEO;
7284                 player->can_support_codec |= FOUND_PLUGIN_AUDIO;
7285                 player->audiodec_linked = 1;
7286
7287         } else if (g_strrstr(klass, "Codec/Decoder/Video")) {
7288
7289                 if ((strlen(player->ini.videocodec_element_hw) > 0) &&
7290                         (g_strrstr(factory_name, player->ini.videocodec_element_hw))) {
7291
7292                         /* mark video decoder for acquire */
7293                         if (player->hw_resource[MMPLAYER_RESOURCE_TYPE_VIDEO_DECODER] != NULL) {
7294                                 LOGW("video decoder resource is already acquired, skip it.");
7295                                 ret = GST_AUTOPLUG_SELECT_SKIP;
7296                                 goto DONE;
7297                         }
7298
7299                         if (_mmplayer_acquire_hw_resource(player, MMPLAYER_RESOURCE_TYPE_VIDEO_DECODER) != MM_ERROR_NONE) {
7300                                 LOGE("failed to acquire video decoder resource");
7301                                 ret = GST_AUTOPLUG_SELECT_SKIP;
7302                                 goto DONE;
7303                         }
7304                         player->interrupted_by_resource = FALSE;
7305                 }
7306
7307                 /* update codec info */
7308                 player->not_supported_codec &= MISSING_PLUGIN_AUDIO;
7309                 player->can_support_codec |= FOUND_PLUGIN_VIDEO;
7310                 player->videodec_linked = 1;
7311         }
7312
7313 DONE:
7314         return ret;
7315 }
7316
7317 GValueArray *
7318 _mmplayer_gst_decode_autoplug_sort(GstElement *bin,
7319                 GstPad *pad, GstCaps *caps, GValueArray *factories, gpointer data)
7320 {
7321 #define DEFAULT_IDX 0xFFFF
7322 #define MIN_FACTORY_NUM 2
7323         mmplayer_t *player = (mmplayer_t *)data;
7324         GValueArray *new_factories = NULL;
7325         GValue val = { 0, };
7326         GstElementFactory *factory = NULL;
7327         const gchar *klass = NULL;
7328         gchar *factory_name = NULL;
7329         guint hw_dec_idx = DEFAULT_IDX;
7330         guint first_sw_dec_idx = DEFAULT_IDX;
7331         guint last_sw_dec_idx = DEFAULT_IDX;
7332         guint new_pos = DEFAULT_IDX;
7333         guint rm_pos = DEFAULT_IDX;
7334         int audio_codec_type;
7335         int video_codec_type;
7336         mmplayer_codec_type_e codec_type = MM_PLAYER_CODEC_TYPE_DEFAULT;
7337
7338         if (factories->n_values < MIN_FACTORY_NUM)
7339                 return NULL;
7340
7341         mm_attrs_get_int_by_name(player->attrs, MM_PLAYER_VIDEO_CODEC_TYPE, &video_codec_type);
7342         mm_attrs_get_int_by_name(player->attrs, MM_PLAYER_AUDIO_CODEC_TYPE, &audio_codec_type);
7343
7344 #ifdef __DEBUG__
7345         LOGD("num of factory : %d, codec type %d, %d", factories->n_values, video_codec_type, audio_codec_type);
7346 #endif
7347         for (int i = 0 ; i < factories->n_values ; i++) {
7348                 gchar *hw_dec_info = NULL;
7349                 gchar (*sw_dec_info)[PLAYER_INI_MAX_STRLEN] = {NULL, };
7350
7351                 factory = g_value_get_object(g_value_array_get_nth(factories, i));
7352                 if (!factory) {
7353                         LOGW("failed to get factory object");
7354                         continue;
7355                 }
7356                 klass = gst_element_factory_get_klass(factory);
7357                 factory_name = GST_OBJECT_NAME(factory);
7358
7359 #ifdef __DEBUG__
7360                 LOGD("Klass [%s] Factory [%s]", klass, factory_name);
7361 #endif
7362                 if (g_strrstr(klass, "Codec/Decoder/Audio")) {
7363                         if (!player->need_audio_dec_sorting) {
7364                                 LOGD("sorting is not required");
7365                                 return NULL;
7366                         }
7367                         codec_type = audio_codec_type;
7368                         hw_dec_info = player->ini.audiocodec_element_hw;
7369                         sw_dec_info = player->ini.audiocodec_element_sw;
7370                 } else if (g_strrstr(klass, "Codec/Decoder/Video")) {
7371                         if (!player->need_video_dec_sorting) {
7372                                 LOGD("sorting is not required");
7373                                 return NULL;
7374                         }
7375                         codec_type = video_codec_type;
7376                         hw_dec_info = player->ini.videocodec_element_hw;
7377                         sw_dec_info = player->ini.videocodec_element_sw;
7378                 } else {
7379                         continue;
7380                 }
7381
7382                 if (g_strrstr(factory_name, hw_dec_info)) {
7383                         hw_dec_idx = i;
7384                 } else {
7385                         for (int j = 0; sw_dec_info[j][0] != '\0'; j++) {
7386                                 if (strstr(factory_name, sw_dec_info[j])) {
7387                                         last_sw_dec_idx = i;
7388                                         if (first_sw_dec_idx == DEFAULT_IDX) {
7389                                                 first_sw_dec_idx = i;
7390                                         }
7391                                 }
7392                         }
7393
7394                         if (first_sw_dec_idx == DEFAULT_IDX)
7395                                 LOGW("unknown codec %s", factory_name);
7396                 }
7397         }
7398
7399         if (hw_dec_idx == DEFAULT_IDX || first_sw_dec_idx == DEFAULT_IDX)
7400                 return NULL;
7401
7402         if (codec_type == MM_PLAYER_CODEC_TYPE_HW) {
7403                 if (hw_dec_idx < first_sw_dec_idx)
7404                         return NULL;
7405                 new_pos = first_sw_dec_idx;
7406                 rm_pos = hw_dec_idx + 1;
7407         } else if (codec_type == MM_PLAYER_CODEC_TYPE_SW) {
7408                 if (last_sw_dec_idx < hw_dec_idx)
7409                         return NULL;
7410                 new_pos = last_sw_dec_idx + 1;
7411                 rm_pos = hw_dec_idx;
7412         } else {
7413                 return NULL;
7414         }
7415
7416         /* change position - insert H/W decoder according to the new position */
7417         factory = g_value_get_object(g_value_array_get_nth(factories, hw_dec_idx));
7418         if (!factory) {
7419                 LOGW("failed to get factory object");
7420                 return NULL;
7421         }
7422         new_factories = g_value_array_copy(factories);
7423         g_value_init (&val, G_TYPE_OBJECT);
7424         g_value_set_object (&val, factory);
7425         g_value_array_insert(new_factories, new_pos, &val);
7426         g_value_unset (&val);
7427         g_value_array_remove(new_factories, rm_pos);    /* remove previous H/W element */
7428
7429         for (int i = 0 ; i < new_factories->n_values ; i++) {
7430                 factory = g_value_get_object(g_value_array_get_nth(new_factories, i));
7431                 if (factory)
7432                         LOGD("[Re-arranged] Klass [%s] Factory [%s]",
7433                                 gst_element_factory_get_klass(factory), GST_OBJECT_NAME (factory));
7434                 else
7435                         LOGE("[Re-arranged] failed to get factory object");
7436         }
7437
7438         return new_factories;
7439 }
7440
7441 gint
7442 _mmplayer_gst_decode_autoplug_select(GstElement *bin,  GstPad *pad,
7443         GstCaps *caps, GstElementFactory *factory, gpointer data)
7444 {
7445         GstAutoplugSelectResult result = GST_AUTOPLUG_SELECT_TRY;
7446         mmplayer_t *player = (mmplayer_t *)data;
7447
7448         gchar *factory_name = NULL;
7449         gchar *caps_str = NULL;
7450         const gchar *klass = NULL;
7451         gint idx = 0;
7452
7453         factory_name = GST_OBJECT_NAME(factory);
7454         klass = gst_element_factory_get_metadata(factory, GST_ELEMENT_METADATA_KLASS);
7455         caps_str = gst_caps_to_string(caps);
7456
7457         LOGD("[handle: %p] found new element [%s] to link", player, factory_name);
7458
7459         /* store type string */
7460         if (player->type_caps_str == NULL) {
7461                 player->type_caps_str = gst_caps_to_string(caps);
7462                 __mmplayer_update_content_type_info(player);
7463         }
7464
7465         /* filtering exclude keyword */
7466         for (idx = 0; player->ini.exclude_element_keyword[idx][0] != '\0'; idx++) {
7467                 if (strstr(factory_name, player->ini.exclude_element_keyword[idx])) {
7468                         LOGW("skipping [%s] by exclude keyword [%s]",
7469                                         factory_name, player->ini.exclude_element_keyword[idx]);
7470
7471                         result = GST_AUTOPLUG_SELECT_SKIP;
7472                         goto DONE;
7473                 }
7474         }
7475
7476         for (idx = 0; player->ini.unsupported_codec_keyword[idx][0] != '\0'; idx++) {
7477                 if (caps_str && strstr(caps_str, player->ini.unsupported_codec_keyword[idx])) {
7478                         LOGW("skipping [%s] by unsupported codec keyword [%s]",
7479                                 factory_name, player->ini.unsupported_codec_keyword[idx]);
7480                         result = GST_AUTOPLUG_SELECT_SKIP;
7481                         goto DONE;
7482                 }
7483         }
7484
7485         /* exclude webm format */
7486         /* NOTE : MSL have to post MM_ERROR_PLAYER_NOT_SUPPORTED_FORMAT
7487          * because webm format is not supportable.
7488          * If webm is disabled in "autoplug-continue", there is no state change
7489          * failure or error because the decodebin will expose the pad directly.
7490          * It make MSL invoke _prepare_async_callback.
7491          * So, we need to disable webm format in "autoplug-select" */
7492         if (caps_str && strstr(caps_str, "webm")) {
7493                 LOGW("webm is not supported");
7494                 result = GST_AUTOPLUG_SELECT_SKIP;
7495                 goto DONE;
7496         }
7497
7498         /* check factory class for filtering */
7499         /* NOTE : msl don't need to use image plugins.
7500          * So, those plugins should be skipped for error handling.
7501          */
7502         if (g_strrstr(klass, "Codec/Decoder/Image")) {
7503                 LOGD("skipping [%s] by not required", factory_name);
7504                 result = GST_AUTOPLUG_SELECT_SKIP;
7505                 goto DONE;
7506         }
7507
7508         if ((MMPLAYER_IS_MS_BUFF_SRC(player)) &&
7509                 (g_strrstr(klass, "Codec/Demuxer") || (g_strrstr(klass, "Codec/Parser")))) {
7510                 // TO CHECK : subtitle if needed, add subparse exception.
7511                 LOGD("skipping parser/demuxer [%s] in es player by not required", factory_name);
7512                 result = GST_AUTOPLUG_SELECT_SKIP;
7513                 goto DONE;
7514         }
7515
7516         if (g_strrstr(factory_name, "mpegpsdemux")) {
7517                 LOGD("skipping PS container - not support");
7518                 result = GST_AUTOPLUG_SELECT_SKIP;
7519                 goto DONE;
7520         }
7521
7522         if (g_strrstr(factory_name, "mssdemux"))
7523                 player->smooth_streaming = TRUE;
7524
7525         if ((g_strrstr(klass, "Codec/Parser/Converter/Video")) ||
7526                 (g_strrstr(klass, "Codec/Decoder/Video"))) {
7527                 gint stype = 0;
7528                 gint width = 0;
7529                 GstStructure *str = NULL;
7530                 mm_attrs_get_int_by_name(player->attrs, "display_surface_type", &stype);
7531
7532                 /* don't make video because of not required */
7533                 if ((stype == MM_DISPLAY_SURFACE_NULL) &&
7534                         (!player->set_mode.video_export)) {
7535                         LOGD("no need video decoding, expose pad");
7536                         result = GST_AUTOPLUG_SELECT_EXPOSE;
7537                         goto DONE;
7538                 }
7539
7540                 /* get w/h for omx state-tune */
7541                 /* FIXME: deprecated? */
7542                 str = gst_caps_get_structure(caps, 0);
7543                 gst_structure_get_int(str, "width", &width);
7544
7545                 if (width != 0) {
7546                         if (player->v_stream_caps) {
7547                                 gst_caps_unref(player->v_stream_caps);
7548                                 player->v_stream_caps = NULL;
7549                         }
7550
7551                         player->v_stream_caps = gst_caps_copy(caps);
7552                         LOGD("take caps for video state tune");
7553                         MMPLAYER_LOG_GST_CAPS_TYPE(player->v_stream_caps);
7554                 }
7555         }
7556
7557         if (g_strrstr(klass, "Codec/Decoder")) {
7558                 result = __mmplayer_check_codec_info(player, klass, caps, factory_name);
7559                 if (result != GST_AUTOPLUG_SELECT_TRY) {
7560                         LOGW("skip add decoder");
7561                         goto DONE;
7562                 }
7563         }
7564
7565 DONE:
7566         MMPLAYER_FREEIF(caps_str);
7567
7568         return result;
7569 }
7570
7571 void
7572 _mmplayer_gst_decode_pad_removed(GstElement *elem, GstPad *pad,
7573         gpointer data)
7574 {
7575         int ret = MM_ERROR_NONE;
7576         mmplayer_t *player = (mmplayer_t *)data;
7577         mmplayer_gst_element_t *mainbin = player->pipeline->mainbin;
7578         mmplayer_gst_element_t *videobin = player->pipeline->videobin;
7579         gint timeout = MMPLAYER_STATE_CHANGE_TIMEOUT(player);
7580
7581         MMPLAYER_FENTER();
7582         MMPLAYER_RETURN_IF_FAIL(player && player->pipeline && mainbin);
7583
7584         LOGD("decoded pad %s:%s removed", GST_DEBUG_PAD_NAME(pad));
7585
7586         if (MMPLAYER_USE_DECODEBIN(player))
7587                 return;
7588
7589         if (!videobin || !g_str_has_prefix(GST_PAD_NAME (pad), "video"))
7590                 return;
7591
7592         __mmplayer_release_signal_connection(player, MM_PLAYER_SIGNAL_TYPE_VIDEOBIN);
7593
7594         __mmplayer_del_sink(player, videobin[MMPLAYER_V_SINK].gst);
7595
7596         LOGD("remove videobin");
7597         ret = _mmplayer_gst_set_state(player, videobin[MMPLAYER_V_BIN].gst,
7598                                 GST_STATE_NULL, FALSE, timeout);
7599         if (ret != MM_ERROR_NONE) {
7600                 LOGE("fail to change state of videobin to NULL");
7601                 return;
7602         }
7603
7604         if (!gst_bin_remove(GST_BIN_CAST(mainbin[MMPLAYER_M_PIPE].gst), videobin[MMPLAYER_V_BIN].gst)) {
7605                 LOGE("failed to remove videobin");
7606                 gst_object_unref(GST_OBJECT(videobin[MMPLAYER_V_BIN].gst));
7607         }
7608
7609         LOGD("remove concat");
7610         ret = _mmplayer_gst_set_state(player, mainbin[MMPLAYER_M_V_CONCAT].gst,
7611                                 GST_STATE_NULL, FALSE, timeout);
7612         if (ret != MM_ERROR_NONE) {
7613                 LOGE("fail to change state of concat to NULL");
7614                 return;
7615         }
7616
7617         if (!gst_bin_remove(GST_BIN_CAST(mainbin[MMPLAYER_M_PIPE].gst), mainbin[MMPLAYER_M_V_CONCAT].gst)) {
7618                 LOGE("failed to remove video concat");
7619                 gst_object_unref(GST_OBJECT(mainbin[MMPLAYER_M_V_CONCAT].gst));
7620         }
7621
7622         mainbin[MMPLAYER_M_V_CONCAT].gst = NULL;
7623         mainbin[MMPLAYER_M_V_CONCAT].id = 0;
7624         MMPLAYER_FREEIF(player->pipeline->videobin);
7625
7626         ret = __mmplayer_release_hw_resource(player, MMPLAYER_RESOURCE_TYPE_VIDEO_OVERLAY);
7627         if (ret != MM_ERROR_NONE)
7628                 LOGE("failed to release overlay resources");
7629
7630         player->videodec_linked = 0;
7631
7632         MMPLAYER_GENERATE_DOT_IF_ENABLED(player, "pipeline-pad-removed");
7633         MMPLAYER_FLEAVE();
7634 }
7635
7636 void
7637 _mmplayer_gst_about_to_finish(GstElement *bin, gpointer data)
7638 {
7639         mmplayer_t *player = (mmplayer_t *)data;
7640
7641         MMPLAYER_FENTER();
7642         MMPLAYER_RETURN_IF_FAIL(player);
7643
7644         LOGD("got about to finish signal");
7645
7646         if (!MMPLAYER_CMD_TRYLOCK(player)) {
7647                 LOGW("Fail to get cmd lock");
7648                 return;
7649         }
7650
7651         if (!__mmplayer_verify_gapless_play_path(player)) {
7652                 LOGD("decoding is finished.");
7653                 MMPLAYER_CMD_UNLOCK(player);
7654                 return;
7655         }
7656
7657         _mmplayer_set_reconfigure_state(player, TRUE);
7658         MMPLAYER_CMD_UNLOCK(player);
7659
7660         MMPLAYER_POST_MSG(player, MM_MESSAGE_FLUSH_BUFFER, NULL);
7661         __mmplayer_deactivate_old_path(player);
7662
7663         MMPLAYER_FLEAVE();
7664 }
7665
7666 void
7667 _mmplayer_gst_decode_drained(GstElement *bin, gpointer data)
7668 {
7669         mmplayer_t *player = (mmplayer_t *)data;
7670         GstIterator *iter = NULL;
7671         GValue item = { 0, };
7672         GstPad *pad = NULL;
7673         gboolean done = FALSE;
7674         gboolean is_all_drained = TRUE;
7675
7676         MMPLAYER_FENTER();
7677         MMPLAYER_RETURN_IF_FAIL(player);
7678
7679         LOGD("got drained signal");
7680
7681         if (!MMPLAYER_CMD_TRYLOCK(player)) {
7682                 LOGW("Fail to get cmd lock");
7683                 return;
7684         }
7685
7686         if (!__mmplayer_verify_gapless_play_path(player)) {
7687                 LOGD("decoding is finished.");
7688                 MMPLAYER_CMD_UNLOCK(player);
7689                 return;
7690         }
7691
7692         _mmplayer_set_reconfigure_state(player, TRUE);
7693         MMPLAYER_CMD_UNLOCK(player);
7694
7695         /* check decodebin src pads whether they received EOS or not */
7696         iter = gst_element_iterate_src_pads(player->pipeline->mainbin[MMPLAYER_M_AUTOPLUG].gst);
7697
7698         while (!done) {
7699                 switch (gst_iterator_next(iter, &item)) {
7700                 case GST_ITERATOR_OK:
7701                         pad = g_value_get_object(&item);
7702                         if (pad && !GST_PAD_IS_EOS(pad)) {
7703                                 LOGW("[%s:%s] not received EOS yet.", GST_DEBUG_PAD_NAME(pad));
7704                                 is_all_drained = FALSE;
7705                                 break;
7706                         }
7707                         g_value_reset(&item);
7708                         break;
7709                 case GST_ITERATOR_RESYNC:
7710                         gst_iterator_resync(iter);
7711                         break;
7712                 case GST_ITERATOR_ERROR:
7713                 case GST_ITERATOR_DONE:
7714                         done = TRUE;
7715                         break;
7716                 }
7717         }
7718         g_value_unset(&item);
7719         gst_iterator_free(iter);
7720
7721         if (!is_all_drained) {
7722                 LOGD("Wait util the all pads get EOS.");
7723                 MMPLAYER_FLEAVE();
7724                 return;
7725         }
7726
7727         player->gapless.update_segment[MM_PLAYER_TRACK_TYPE_AUDIO] = FALSE;
7728         player->gapless.update_segment[MM_PLAYER_TRACK_TYPE_VIDEO] = FALSE;
7729
7730         /* deactivate pipeline except sinkbins to set up the new pipeline of next uri*/
7731         MMPLAYER_POST_MSG(player, MM_MESSAGE_FLUSH_BUFFER, NULL); /* post message for gapless */
7732         __mmplayer_deactivate_old_path(player);
7733
7734         MMPLAYER_FLEAVE();
7735 }
7736
7737 void
7738 _mmplayer_gst_element_added(GstBin *bin, GstElement *element, gpointer data)
7739 {
7740         mmplayer_t *player = (mmplayer_t *)data;
7741         const gchar *klass = NULL;
7742         gchar *factory_name = NULL;
7743
7744         klass = gst_element_factory_get_metadata(gst_element_get_factory(element), GST_ELEMENT_METADATA_KLASS);
7745         factory_name = GST_OBJECT_NAME(gst_element_get_factory(element));
7746
7747         LOGD("new elem klass: %s, factory_name: %s, new elem name : %s", klass, factory_name, GST_ELEMENT_NAME(element));
7748
7749         if (__mmplayer_add_dump_buffer_probe(player, element))
7750                 LOGD("add buffer probe");
7751
7752         if (g_strrstr(klass, "Decoder")) {
7753                 if (g_strrstr(klass, "Audio")) {
7754                         player->audio_decoders = g_list_append(player->audio_decoders,
7755                                                                                                                                         g_strdup(GST_ELEMENT_NAME(element)));
7756
7757                         /* update codec info */
7758                         player->not_supported_codec &= MISSING_PLUGIN_VIDEO;
7759                         player->can_support_codec |= FOUND_PLUGIN_AUDIO;
7760                         player->audiodec_linked = 1;
7761                 } else if (g_strrstr(klass, "Video")) {
7762                         GstElement *video_parse = player->pipeline->mainbin[MMPLAYER_M_V_PARSE].gst;
7763                         /* update codec info */
7764                         player->not_supported_codec &= MISSING_PLUGIN_AUDIO;
7765                         player->can_support_codec |= FOUND_PLUGIN_VIDEO;
7766                         player->videodec_linked = 1;
7767
7768                         if (video_parse) {
7769                                 GstPad *srcpad = gst_element_get_static_pad (video_parse, "src");
7770                                 if (srcpad) {
7771                                         GstCaps *caps = NULL;
7772                                         GstStructure *str = NULL;
7773                                         const gchar *name = NULL;
7774                                         gboolean caps_ret = TRUE;
7775
7776                                         MMPLAYER_GST_GET_CAPS_INFO_FROM_PAD (srcpad, caps, str, name, caps_ret);
7777                                         if (caps_ret && str) {
7778                                                 const gchar *stream_format = gst_structure_get_string (str, "stream-format");
7779                                                 if (stream_format && g_strrstr(stream_format, "byte-stream")) {
7780                                                         if ((g_object_class_find_property(G_OBJECT_GET_CLASS(video_parse), "config-interval"))) {
7781                                                                 g_object_set(G_OBJECT(video_parse), "config-interval", -1, NULL);
7782                                                                 LOGD("Send SPS and PPS Insertion every IDR frame");
7783                                                         }
7784                                                 }
7785                                         }
7786                                         gst_object_unref(GST_OBJECT(srcpad));
7787                                 }
7788                         }
7789                 }
7790         } else if (g_strrstr(klass, "Demuxer")) {
7791                 if (g_strrstr(klass, "Adaptive")) {
7792                         player->pipeline->mainbin[MMPLAYER_M_ADAPTIVE_DEMUX].id = MMPLAYER_M_ADAPTIVE_DEMUX;
7793                         player->pipeline->mainbin[MMPLAYER_M_ADAPTIVE_DEMUX].gst = element;
7794
7795                         MMPLAYER_FREEIF(player->type_caps_str);
7796
7797                         if (g_strrstr(factory_name, "hlsdemux")) {
7798                                 player->type_caps_str = g_strdup("application/x-hls");
7799                                 player->profile.uri_type = MM_PLAYER_URI_TYPE_HLS;
7800                         } else if (g_strrstr(factory_name, "dashdemux")) {
7801                                 player->type_caps_str = g_strdup("application/dash+xml");
7802                                 player->profile.uri_type = MM_PLAYER_URI_TYPE_DASH;
7803                         } else {
7804                                 LOGE("not supported type");
7805                                 return;
7806                         }
7807                         player->streamer->is_adaptive_streaming = TRUE;
7808
7809                         if (player->streamer->buffering_req.prebuffer_time <= MIN_BUFFERING_TIME)
7810                                 player->streamer->buffering_req.prebuffer_time = DEFAULT_PREBUFFERING_TIME;
7811
7812                         LOGD("max variant limit: %d, %d, %d, prebuffer time: %d ms",
7813                                 player->adaptive_info.limit.bandwidth,
7814                                 player->adaptive_info.limit.width,
7815                                 player->adaptive_info.limit.height,
7816                                 player->streamer->buffering_req.prebuffer_time);
7817
7818                         g_object_set(player->pipeline->mainbin[MMPLAYER_M_ADAPTIVE_DEMUX].gst,
7819                                 "max-bandwidth", player->adaptive_info.limit.bandwidth,
7820                                 "max-video-width", player->adaptive_info.limit.width,
7821                                 "max-video-height", player->adaptive_info.limit.height,
7822                                 "low-watermark-time", (guint64)(player->streamer->buffering_req.prebuffer_time * GST_MSECOND),
7823                                 NULL);
7824                 } else {
7825 #ifdef __DEBUG__
7826                         LOGD("plugged element is demuxer. take it");
7827 #endif
7828                         player->pipeline->mainbin[MMPLAYER_M_DEMUX].id = MMPLAYER_M_DEMUX;
7829                         player->pipeline->mainbin[MMPLAYER_M_DEMUX].gst = element;
7830                 }
7831         } else if (g_strrstr(klass, "Parser") && (g_strrstr(klass, "Video"))) {
7832                 player->pipeline->mainbin[MMPLAYER_M_V_PARSE].id = MMPLAYER_M_V_PARSE;
7833                 player->pipeline->mainbin[MMPLAYER_M_V_PARSE].gst = element;
7834         }
7835
7836         if (g_strrstr(factory_name, "mpegaudioparse")) {
7837                 if ((MMPLAYER_IS_HTTP_STREAMING(player)) &&
7838                         (__mmplayer_is_only_mp3_type(player->type_caps_str))) {
7839                         LOGD("[mpegaudioparse] set streaming pull mode.");
7840                         g_object_set(G_OBJECT(element), "http-pull-mp3dec", TRUE, NULL);
7841                 }
7842         } else if ((player->pipeline->mainbin[MMPLAYER_M_DEMUX].gst) &&
7843                 (g_strrstr(GST_ELEMENT_NAME(element), "multiqueue"))) {
7844                 LOGD("plugged element is multiqueue. take it %s", GST_ELEMENT_NAME(element));
7845
7846                 player->pipeline->mainbin[MMPLAYER_M_DEMUXED_S_BUFFER].id = MMPLAYER_M_DEMUXED_S_BUFFER;
7847                 player->pipeline->mainbin[MMPLAYER_M_DEMUXED_S_BUFFER].gst = element;
7848
7849                 if (MMPLAYER_IS_HTTP_STREAMING(player)) {
7850                         /* in case of multiqueue, max bytes size is defined with fixed value in mm_player_streaming.h*/
7851                         _mm_player_streaming_set_multiqueue(player->streamer, element);
7852                         _mm_player_streaming_sync_property(player->streamer, player->pipeline->mainbin[MMPLAYER_M_AUTOPLUG].gst);
7853                 }
7854         }
7855
7856         return;
7857 }
7858
7859 static void
7860 __mmplayer_release_misc(mmplayer_t *player)
7861 {
7862         int i;
7863         bool cur_mode = player->set_mode.rich_audio;
7864         MMPLAYER_FENTER();
7865
7866         MMPLAYER_RETURN_IF_FAIL(player);
7867
7868         player->sent_bos = FALSE;
7869         player->playback_rate = DEFAULT_PLAYBACK_RATE;
7870
7871         player->seek_state = MMPLAYER_SEEK_NONE;
7872
7873         player->total_bitrate = 0;
7874         player->total_maximum_bitrate = 0;
7875
7876         player->not_found_demuxer = 0;
7877
7878         player->last_position = 0;
7879         player->duration = 0;
7880         player->http_content_size = 0;
7881         player->not_supported_codec = MISSING_PLUGIN_NONE;
7882         player->can_support_codec = FOUND_PLUGIN_NONE;
7883         player->pending_seek.is_pending = false;
7884         player->pending_seek.pos = 0;
7885         player->msg_posted = FALSE;
7886         player->has_many_types = FALSE;
7887         player->is_subtitle_force_drop = FALSE;
7888         player->play_subtitle = FALSE;
7889         player->adjust_subtitle_pos = 0;
7890         player->has_closed_caption = FALSE;
7891         player->set_mode.video_export = false;
7892         player->profile.uri_type = MM_PLAYER_URI_TYPE_NONE;
7893         memset(&player->set_mode, 0, sizeof(mmplayer_setting_mode_t));
7894         /* recover mode */
7895         player->set_mode.rich_audio = cur_mode;
7896
7897         if (player->audio_device_cb_id > 0 &&
7898                 mm_sound_remove_device_connected_callback(player->audio_device_cb_id) != MM_ERROR_NONE)
7899                 LOGW("failed to remove audio device_connected_callback");
7900         player->audio_device_cb_id = 0;
7901
7902         for (i = 0; i < MM_PLAYER_STREAM_COUNT_MAX; i++) {
7903                 player->bitrate[i] = 0;
7904                 player->maximum_bitrate[i] = 0;
7905         }
7906
7907         /* free memory related to audio effect */
7908         MMPLAYER_FREEIF(player->audio_effect_info.custom_ext_level_for_plugin);
7909
7910         if (player->adaptive_info.var_list) {
7911                 g_list_free_full(player->adaptive_info.var_list, g_free);
7912                 player->adaptive_info.var_list = NULL;
7913         }
7914
7915         player->adaptive_info.limit.bandwidth = ADAPTIVE_VARIANT_DEFAULT_VALUE;
7916         player->adaptive_info.limit.width = ADAPTIVE_VARIANT_DEFAULT_VALUE;
7917         player->adaptive_info.limit.height = ADAPTIVE_VARIANT_DEFAULT_VALUE;
7918
7919         /* Reset video360 settings to their defaults in case if the pipeline is to be
7920          * re-created.
7921          * */
7922         player->video360_metadata.is_spherical = -1;
7923         player->is_openal_plugin_used = FALSE;
7924
7925         player->is_content_spherical = FALSE;
7926         player->is_video360_enabled = TRUE;
7927         player->video360_metadata.projection_type = VIDEO360_PROJECTION_TYPE_EQUIRECTANGULAR;
7928         player->video360_metadata.stereo_mode = VIDEO360_MODE_MONOSCOPIC;
7929         player->video360_yaw_radians = PLAYER_SPHERICAL_DEFAULT_YAW;
7930         player->video360_pitch_radians = PLAYER_SPHERICAL_DEFAULT_PITCH;
7931         player->video360_zoom = 1.0f;
7932         player->video360_horizontal_fov = PLAYER_SPHERICAL_DEFAULT_H_FOV;
7933         player->video360_vertical_fov = PLAYER_SPHERICAL_DEFAULT_V_FOV;
7934
7935         player->sound.rg_enable = false;
7936
7937         __mmplayer_initialize_video_roi(player);
7938         MMPLAYER_FLEAVE();
7939 }
7940
7941 static void
7942 __mmplayer_release_misc_post(mmplayer_t *player)
7943 {
7944         gchar *original_uri = NULL;
7945         MMPLAYER_FENTER();
7946
7947         /* player->pipeline is already released before. */
7948         MMPLAYER_RETURN_IF_FAIL(player);
7949
7950         player->video_decoded_cb = NULL;
7951         player->video_decoded_cb_user_param = NULL;
7952         player->video_stream_prerolled = false;
7953
7954         player->audio_decoded_cb = NULL;
7955         player->audio_decoded_cb_user_param = NULL;
7956         player->audio_extract_opt = MM_PLAYER_AUDIO_EXTRACT_DEFAULT;
7957
7958         player->audio_stream_changed_cb = NULL;
7959         player->audio_stream_changed_cb_user_param = NULL;
7960
7961         mm_player_set_attribute((MMHandleType)player, NULL,
7962                 "content_video_found", 0, MM_PLAYER_AUDIO_ONLY, 0, NULL);
7963
7964         /* clean found audio decoders */
7965         if (player->audio_decoders) {
7966                 g_list_free_full(player->audio_decoders, (GDestroyNotify)g_free);
7967                 player->audio_decoders = NULL;
7968         }
7969
7970         /* clean the uri list except original uri */
7971         if (player->uri_info.uri_list && g_list_length(player->uri_info.uri_list) > 1) {
7972                 GList *tmp = NULL;
7973                 original_uri = g_list_nth_data(player->uri_info.uri_list, 0);
7974                 tmp = g_list_remove_link(player->uri_info.uri_list, player->uri_info.uri_list);
7975                 g_list_free_full(tmp, (GDestroyNotify)g_free);
7976
7977                 if (!original_uri)
7978                         LOGW("failed to get original uri info");
7979
7980                 mm_player_set_attribute((MMHandleType)player, NULL, "profile_uri",
7981                                 original_uri, (original_uri) ? strlen(original_uri) : (0), NULL);
7982                 MMPLAYER_FREEIF(original_uri);
7983         }
7984
7985         /* clear the audio stream buffer list */
7986         _mmplayer_audio_stream_clear_buffer(player, FALSE);
7987
7988         /* clear the video stream bo list */
7989         __mmplayer_video_stream_destroy_bo_list(player);
7990         __mmplayer_initialize_storage_info(player, MMPLAYER_PATH_MAX);
7991
7992         if (player->profile.input_mem.buf) {
7993                 free(player->profile.input_mem.buf);
7994                 player->profile.input_mem.buf = NULL;
7995         }
7996         player->profile.input_mem.len = 0;
7997         player->profile.input_mem.offset = 0;
7998
7999         player->uri_info.uri_idx = 0;
8000         MMPLAYER_FLEAVE();
8001 }
8002
8003 gboolean
8004 __mmplayer_check_subtitle(mmplayer_t *player)
8005 {
8006         MMHandleType attrs = 0;
8007         char *subtitle_uri = NULL;
8008
8009         MMPLAYER_FENTER();
8010
8011         MMPLAYER_RETURN_VAL_IF_FAIL(player, FALSE);
8012
8013         /* get subtitle attribute */
8014         attrs = MMPLAYER_GET_ATTRS(player);
8015         if (!attrs)
8016                 return FALSE;
8017
8018         mm_attrs_get_string_by_name(attrs, "subtitle_uri", &subtitle_uri);
8019         if (!subtitle_uri || !strlen(subtitle_uri))
8020                 return FALSE;
8021
8022         SECURE_LOGD("subtitle uri is %s[%zu]", subtitle_uri, strlen(subtitle_uri));
8023         player->is_external_subtitle_present = TRUE;
8024
8025         MMPLAYER_FLEAVE();
8026
8027         return TRUE;
8028 }
8029
8030 void
8031 _mmplayer_cancel_eos_timer(mmplayer_t *player)
8032 {
8033         MMPLAYER_RETURN_IF_FAIL(player);
8034
8035         if (player->eos_timer) {
8036                 LOGD("cancel eos timer");
8037                 __mmplayer_remove_g_source_from_context(player->context.global_default, player->eos_timer);
8038                 player->eos_timer = 0;
8039         }
8040
8041         return;
8042 }
8043
8044 static void
8045 __mmplayer_add_sink(mmplayer_t *player, GstElement *sink, gboolean first)
8046 {
8047         MMPLAYER_FENTER();
8048
8049         MMPLAYER_RETURN_IF_FAIL(player);
8050         MMPLAYER_RETURN_IF_FAIL(sink);
8051
8052         if (first)
8053                 player->sink_elements = g_list_prepend(player->sink_elements, sink);
8054         else
8055                 player->sink_elements = g_list_append(player->sink_elements, sink);
8056
8057         MMPLAYER_FLEAVE();
8058 }
8059
8060 static void
8061 __mmplayer_del_sink(mmplayer_t *player, GstElement *sink)
8062 {
8063         MMPLAYER_FENTER();
8064
8065         MMPLAYER_RETURN_IF_FAIL(player);
8066         MMPLAYER_RETURN_IF_FAIL(sink);
8067
8068         player->sink_elements = g_list_remove(player->sink_elements, sink);
8069
8070         MMPLAYER_FLEAVE();
8071 }
8072
8073 void
8074 _mmplayer_add_signal_connection(mmplayer_t *player, GObject *object,
8075         mmplayer_signal_type_e type, const gchar *signal, GCallback cb_funct, gpointer u_data)
8076 {
8077         mmplayer_signal_item_t *item = NULL;
8078
8079         MMPLAYER_FENTER();
8080         MMPLAYER_RETURN_IF_FAIL(player);
8081
8082         if (type >= MM_PLAYER_SIGNAL_TYPE_MAX) {
8083                 LOGE("invalid signal type [%d]", type);
8084                 return;
8085         }
8086
8087         item = (mmplayer_signal_item_t *)g_try_malloc(sizeof(mmplayer_signal_item_t));
8088         if (!item) {
8089                 LOGE("cannot connect signal [%s]", signal);
8090                 return;
8091         }
8092
8093         item->obj = object;
8094         item->sig = g_signal_connect(object, signal, cb_funct, u_data);
8095         player->signals[type] = g_list_append(player->signals[type], item);
8096
8097         MMPLAYER_FLEAVE();
8098         return;
8099 }
8100
8101 /* NOTE : be careful with calling this api. please refer to below glib comment
8102  * glib comment : Note that there is a bug in GObject that makes this function much
8103  * less useful than it might seem otherwise. Once gobject is disposed, the callback
8104  * will no longer be called, but, the signal handler is not currently disconnected.
8105  * If the instance is itself being freed at the same time than this doesn't matter,
8106  * since the signal will automatically be removed, but if instance persists,
8107  * then the signal handler will leak. You should not remove the signal yourself
8108  * because in a future versions of GObject, the handler will automatically be
8109  * disconnected.
8110  *
8111  * It's possible to work around this problem in a way that will continue to work
8112  * with future versions of GObject by checking that the signal handler is still
8113  * connected before disconnected it:
8114  *
8115  *  if (g_signal_handler_is_connected(instance, id))
8116  *    g_signal_handler_disconnect(instance, id);
8117  */
8118 static void
8119 __mmplayer_release_signal_connection(mmplayer_t *player, mmplayer_signal_type_e type)
8120 {
8121         GList *sig_list = NULL;
8122         mmplayer_signal_item_t *item = NULL;
8123
8124         MMPLAYER_FENTER();
8125
8126         MMPLAYER_RETURN_IF_FAIL(player);
8127
8128         LOGD("release signals type : %d", type);
8129
8130         if (type >= MM_PLAYER_SIGNAL_TYPE_ALL) {
8131                 __mmplayer_release_signal_connection(player, MM_PLAYER_SIGNAL_TYPE_AUTOPLUG);
8132                 __mmplayer_release_signal_connection(player, MM_PLAYER_SIGNAL_TYPE_VIDEOBIN);
8133                 __mmplayer_release_signal_connection(player, MM_PLAYER_SIGNAL_TYPE_AUDIOBIN);
8134                 __mmplayer_release_signal_connection(player, MM_PLAYER_SIGNAL_TYPE_TEXTBIN);
8135                 __mmplayer_release_signal_connection(player, MM_PLAYER_SIGNAL_TYPE_OTHERS);
8136                 return;
8137         }
8138
8139         sig_list = player->signals[type];
8140
8141         for (; sig_list; sig_list = sig_list->next) {
8142                 item = sig_list->data;
8143
8144                 if (item && item->obj) {
8145                         if (g_signal_handler_is_connected(item->obj, item->sig))
8146                                 g_signal_handler_disconnect(item->obj, item->sig);
8147                 }
8148
8149                 MMPLAYER_FREEIF(item);
8150         }
8151
8152         g_list_free(player->signals[type]);
8153         player->signals[type] = NULL;
8154
8155         MMPLAYER_FLEAVE();
8156
8157         return;
8158 }
8159
8160 int
8161 _mmplayer_change_videosink(MMHandleType handle, MMDisplaySurfaceType surface_type, int wl_surface_id)
8162 {
8163         mmplayer_t *player = 0;
8164         int prev_display_surface_type = 0;
8165
8166         MMPLAYER_FENTER();
8167
8168         MMPLAYER_RETURN_VAL_IF_FAIL(handle, MM_ERROR_COMMON_INVALID_ARGUMENT);
8169
8170         player = MM_PLAYER_CAST(handle);
8171
8172         /* check video sinkbin is created */
8173         if (_mmplayer_is_videosink_ready(player, MM_DISPLAY_SURFACE_NUM)) {
8174                 LOGW("Videosink is already created");
8175                 return MM_ERROR_NONE;
8176         }
8177
8178         LOGD("videosink element is not yet ready");
8179
8180         if (surface_type >= MM_DISPLAY_SURFACE_NUM) {
8181                 LOGE("Not support this surface type(%d) for changing vidoesink", surface_type);
8182                 MMPLAYER_FLEAVE();
8183                 return MM_ERROR_INVALID_ARGUMENT;
8184         }
8185
8186         /* load previous attributes */
8187         if (player->attrs) {
8188                 mm_attrs_get_int_by_name(player->attrs, "display_surface_type", &prev_display_surface_type);
8189                 LOGD("[0: Video surface, 4: EVAS surface] previous surface type(%d), new surface type(%d)", prev_display_surface_type, surface_type);
8190                 if (prev_display_surface_type == surface_type) {
8191                         LOGD("incoming display surface type is same as previous one, do nothing..");
8192                         MMPLAYER_FLEAVE();
8193                         return MM_ERROR_NONE;
8194                 }
8195         } else {
8196                 LOGE("failed to load attributes");
8197                 MMPLAYER_FLEAVE();
8198                 return MM_ERROR_PLAYER_INTERNAL;
8199         }
8200
8201         /* videobin is not created yet, so we just set attributes related to display surface */
8202         LOGD("store display attribute for given surface type(%d)", surface_type);
8203         mm_player_set_attribute(handle, NULL, "display_surface_type", surface_type,
8204                         "display_overlay", wl_surface_id, NULL);
8205
8206         MMPLAYER_FLEAVE();
8207         return MM_ERROR_NONE;
8208 }
8209
8210 /* Note : if silent is true, then subtitle would not be displayed. :*/
8211 int
8212 _mmplayer_set_subtitle_silent(MMHandleType hplayer, int silent)
8213 {
8214         mmplayer_t *player = (mmplayer_t *)hplayer;
8215
8216         MMPLAYER_FENTER();
8217
8218         /* check player handle */
8219         MMPLAYER_RETURN_VAL_IF_FAIL(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
8220
8221         player->set_mode.subtitle_off = silent;
8222
8223         LOGD("subtitle is %s.", player->set_mode.subtitle_off ? "ON" : "OFF");
8224
8225         MMPLAYER_FLEAVE();
8226
8227         return MM_ERROR_NONE;
8228 }
8229
8230 int
8231 _mmplayer_sync_subtitle_pipeline(mmplayer_t *player)
8232 {
8233         mmplayer_gst_element_t *mainbin = NULL;
8234         mmplayer_gst_element_t *textbin = NULL;
8235         GstStateChangeReturn ret = GST_STATE_CHANGE_FAILURE;
8236         GstState current_state = GST_STATE_VOID_PENDING;
8237         GstState element_state = GST_STATE_VOID_PENDING;
8238         GstState element_pending_state = GST_STATE_VOID_PENDING;
8239         gint64 time = 0;
8240         GstEvent *event = NULL;
8241         int result = MM_ERROR_NONE;
8242
8243         GstClock *curr_clock = NULL;
8244         GstClockTime base_time, start_time, curr_time;
8245
8246
8247         MMPLAYER_FENTER();
8248
8249         /* check player handle */
8250         MMPLAYER_RETURN_VAL_IF_FAIL(player &&
8251                                                                 player->pipeline &&
8252                                                                 player->pipeline->mainbin &&
8253                                                                 player->pipeline->textbin, MM_ERROR_PLAYER_NOT_INITIALIZED);
8254
8255         mainbin = player->pipeline->mainbin;
8256         textbin = player->pipeline->textbin;
8257
8258         current_state = GST_STATE(mainbin[MMPLAYER_M_PIPE].gst);
8259
8260         // sync clock with current pipeline
8261         curr_clock = gst_element_get_clock(player->pipeline->mainbin[MMPLAYER_M_PIPE].gst);
8262         curr_time = gst_clock_get_time(curr_clock);
8263
8264         base_time = gst_element_get_base_time(GST_ELEMENT_CAST(player->pipeline->mainbin[MMPLAYER_M_PIPE].gst));
8265         start_time = gst_element_get_start_time(GST_ELEMENT_CAST(player->pipeline->mainbin[MMPLAYER_M_PIPE].gst));
8266
8267         LOGD("state: %d, base_time=%" GST_TIME_FORMAT " start_time=%" GST_TIME_FORMAT " curr_time=%" GST_TIME_FORMAT,
8268                 current_state, GST_TIME_ARGS(base_time), GST_TIME_ARGS(start_time), GST_TIME_ARGS(curr_time));
8269
8270         if (current_state > GST_STATE_READY) {
8271                 // sync state with current pipeline
8272                 gst_element_set_state(textbin[MMPLAYER_T_BIN].gst, GST_STATE_PAUSED);
8273                 gst_element_set_state(mainbin[MMPLAYER_M_SUBPARSE].gst, GST_STATE_PAUSED);
8274                 gst_element_set_state(mainbin[MMPLAYER_M_SUBSRC].gst, GST_STATE_PAUSED);
8275
8276                 ret = gst_element_get_state(mainbin[MMPLAYER_M_SUBSRC].gst, &element_state, &element_pending_state, 5 * GST_SECOND);
8277                 if (GST_STATE_CHANGE_FAILURE == ret) {
8278                         LOGE("fail to state change.");
8279                         result = MM_ERROR_PLAYER_INTERNAL;
8280                         if (curr_clock)
8281                                 gst_object_unref(curr_clock);
8282                         goto ERROR;
8283                 }
8284         }
8285         gst_element_set_base_time(textbin[MMPLAYER_T_BIN].gst, base_time);
8286         gst_element_set_start_time(textbin[MMPLAYER_T_BIN].gst, start_time);
8287
8288         if (curr_clock) {
8289                 gst_element_set_clock(textbin[MMPLAYER_T_BIN].gst, curr_clock);
8290                 gst_object_unref(curr_clock);
8291         }
8292
8293         // seek to current position
8294         if (!gst_element_query_position(mainbin[MMPLAYER_M_PIPE].gst, GST_FORMAT_TIME, &time)) {
8295                 result = MM_ERROR_PLAYER_INVALID_STATE;
8296                 LOGE("gst_element_query_position failed, invalid state");
8297                 goto ERROR;
8298         }
8299
8300         LOGD("seek time = %"G_GINT64_FORMAT", rate = %f", time, player->playback_rate);
8301         event = gst_event_new_seek(player->playback_rate, GST_FORMAT_TIME, (GstSeekFlags)(GST_SEEK_FLAG_FLUSH), GST_SEEK_TYPE_SET, time, GST_SEEK_TYPE_NONE, -1);
8302         if (event) {
8303                 _mmplayer_gst_send_event_to_sink(player, event);
8304         } else {
8305                 result = MM_ERROR_PLAYER_INTERNAL;
8306                 LOGE("gst_event_new_seek failed"); /* pipeline will got error and can not be recovered */
8307                 goto ERROR;
8308         }
8309
8310         /* sync state with current pipeline */
8311         gst_element_sync_state_with_parent(textbin[MMPLAYER_T_BIN].gst);
8312         gst_element_sync_state_with_parent(mainbin[MMPLAYER_M_SUBPARSE].gst);
8313         gst_element_sync_state_with_parent(mainbin[MMPLAYER_M_SUBSRC].gst);
8314
8315         return MM_ERROR_NONE;
8316
8317 ERROR:
8318         /* release text pipeline resource */
8319         player->textsink_linked = 0;
8320
8321         /* release signal */
8322         __mmplayer_release_signal_connection(player, MM_PLAYER_SIGNAL_TYPE_TEXTBIN);
8323
8324         /* release textbin with it's children */
8325         MMPLAYER_RELEASE_ELEMENT(player, player->pipeline->textbin, MMPLAYER_T_BIN);
8326         MMPLAYER_FREEIF(player->pipeline->textbin);
8327         player->pipeline->textbin = NULL;
8328
8329         /* release subtitle elem */
8330         MMPLAYER_RELEASE_ELEMENT(player, player->pipeline->mainbin, MMPLAYER_M_SUBSRC);
8331         MMPLAYER_RELEASE_ELEMENT(player, player->pipeline->mainbin, MMPLAYER_M_SUBPARSE);
8332
8333         return result;
8334 }
8335
8336 static int
8337 __mmplayer_change_external_subtitle_language(mmplayer_t *player, const char *filepath)
8338 {
8339         GstStateChangeReturn ret = GST_STATE_CHANGE_FAILURE;
8340         GstState current_state = GST_STATE_VOID_PENDING;
8341
8342         MMHandleType attrs = 0;
8343         mmplayer_gst_element_t *mainbin = NULL;
8344         mmplayer_gst_element_t *textbin = NULL;
8345
8346         gchar *subtitle_uri = NULL;
8347         int result = MM_ERROR_NONE;
8348         const gchar *charset = NULL;
8349
8350         MMPLAYER_FENTER();
8351
8352         /* check player handle */
8353         MMPLAYER_RETURN_VAL_IF_FAIL(player &&
8354                                                                 player->pipeline &&
8355                                                                 player->pipeline->mainbin &&
8356                                                                 player->pipeline->textbin, MM_ERROR_PLAYER_NOT_INITIALIZED);
8357         MMPLAYER_RETURN_VAL_IF_FAIL(filepath, MM_ERROR_COMMON_INVALID_ARGUMENT);
8358
8359         mainbin = player->pipeline->mainbin;
8360         textbin = player->pipeline->textbin;
8361
8362         current_state = GST_STATE(mainbin[MMPLAYER_M_PIPE].gst);
8363         if (current_state < GST_STATE_READY) {
8364                 result = MM_ERROR_PLAYER_INVALID_STATE;
8365                 LOGE("Pipeline is not in proper state");
8366                 goto EXIT;
8367         }
8368
8369         attrs = MMPLAYER_GET_ATTRS(player);
8370         if (!attrs) {
8371                 LOGE("cannot get content attribute");
8372                 result = MM_ERROR_PLAYER_INTERNAL;
8373                 goto EXIT;
8374         }
8375
8376         mm_attrs_get_string_by_name(attrs, "subtitle_uri", &subtitle_uri);
8377         if (!subtitle_uri || strlen(subtitle_uri) < 1) {
8378                 LOGE("subtitle uri is not proper filepath");
8379                 result = MM_ERROR_PLAYER_INVALID_URI;
8380                 goto EXIT;
8381         }
8382
8383         if (!_mmplayer_get_storage_info(filepath, &player->storage_info[MMPLAYER_PATH_TEXT])) {
8384                 LOGE("failed to get storage info of subtitle path");
8385                 result = MM_ERROR_PLAYER_INVALID_URI;
8386                 goto EXIT;
8387         }
8388
8389         SECURE_LOGD("old subtitle file path is [%s]", subtitle_uri);
8390         SECURE_LOGD("new subtitle file path is [%s]", filepath);
8391
8392         if (!strcmp(filepath, subtitle_uri)) {
8393                 LOGD("subtitle path is not changed");
8394                 goto EXIT;
8395         } else {
8396                 if (mm_player_set_attribute((MMHandleType)player, NULL,
8397                                 "subtitle_uri", filepath, strlen(filepath), NULL) != MM_ERROR_NONE) {
8398                         LOGE("failed to set attribute");
8399                         goto EXIT;
8400                 }
8401         }
8402
8403         //gst_pad_set_blocked_async(src-srcpad, TRUE)
8404         MMPLAYER_SUBTITLE_INFO_LOCK(player);
8405         player->subtitle_language_list = NULL;
8406         MMPLAYER_SUBTITLE_INFO_UNLOCK(player);
8407
8408         ret = gst_element_set_state(textbin[MMPLAYER_T_BIN].gst, GST_STATE_READY);
8409         if (ret != GST_STATE_CHANGE_SUCCESS) {
8410                 LOGE("failed to change state of textbin to READY");
8411                 result = MM_ERROR_PLAYER_INTERNAL;
8412                 goto EXIT;
8413         }
8414
8415         ret = gst_element_set_state(mainbin[MMPLAYER_M_SUBPARSE].gst, GST_STATE_READY);
8416         if (ret != GST_STATE_CHANGE_SUCCESS) {
8417                 LOGE("failed to change state of subparse to READY");
8418                 result = MM_ERROR_PLAYER_INTERNAL;
8419                 goto EXIT;
8420         }
8421
8422         ret = gst_element_set_state(mainbin[MMPLAYER_M_SUBSRC].gst, GST_STATE_READY);
8423         if (ret != GST_STATE_CHANGE_SUCCESS) {
8424                 LOGE("failed to change state of filesrc to READY");
8425                 result = MM_ERROR_PLAYER_INTERNAL;
8426                 goto EXIT;
8427         }
8428
8429         __mmplayer_initialize_storage_info(player, MMPLAYER_PATH_TEXT);
8430
8431         g_object_set(G_OBJECT(mainbin[MMPLAYER_M_SUBSRC].gst), "location", filepath, NULL);
8432
8433         charset = _mmplayer_get_charset(filepath);
8434         if (charset) {
8435                 LOGD("detected charset is %s", charset);
8436                 g_object_set(G_OBJECT(mainbin[MMPLAYER_M_SUBPARSE].gst), "subtitle-encoding", charset, NULL);
8437         }
8438
8439         result = _mmplayer_sync_subtitle_pipeline(player);
8440
8441 EXIT:
8442         MMPLAYER_FLEAVE();
8443         return result;
8444 }
8445
8446 /* API to switch between external subtitles */
8447 int
8448 _mmplayer_set_external_subtitle_path(MMHandleType hplayer, const char *filepath)
8449 {
8450         int result = MM_ERROR_NONE;
8451         mmplayer_t *player = (mmplayer_t *)hplayer;
8452         char *path = NULL;
8453
8454         MMPLAYER_FENTER();
8455
8456         /* check player handle */
8457         MMPLAYER_RETURN_VAL_IF_FAIL(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
8458
8459         /* filepath can be null in idle state */
8460         if (filepath) {
8461                 /* check file path */
8462                 if ((path = strstr(filepath, "file://")))
8463                         result = _mmplayer_exist_file_path(path + 7);
8464                 else
8465                         result = _mmplayer_exist_file_path(filepath);
8466
8467                 if (result != MM_ERROR_NONE) {
8468                         LOGE("invalid subtitle path 0x%X", result);
8469                         return result; /* file not found or permission denied */
8470                 }
8471         }
8472
8473         if (!player->pipeline) {
8474                 /* IDLE state */
8475                 if (mm_player_set_attribute(hplayer, NULL, "subtitle_uri", filepath,
8476                                 (filepath)?(strlen(filepath)):(0), NULL) != MM_ERROR_NONE) {
8477                         LOGE("failed to set attribute");
8478                         return MM_ERROR_PLAYER_INTERNAL;
8479                 }
8480         } else {
8481                 /* cur state <> IDLE(READY, PAUSE, PLAYING..) */
8482                 /* check filepath */
8483                 MMPLAYER_RETURN_VAL_IF_FAIL(filepath, MM_ERROR_COMMON_INVALID_ARGUMENT);
8484
8485                 if (!__mmplayer_check_subtitle(player)) {
8486                         if (mm_player_set_attribute(hplayer, NULL, "subtitle_uri",
8487                                         filepath, strlen(filepath), NULL) != MM_ERROR_NONE) {
8488                                 LOGE("failed to set attribute");
8489                                 return MM_ERROR_PLAYER_INTERNAL;
8490                         }
8491
8492                         if (__mmplayer_gst_create_text_pipeline(player) != MM_ERROR_NONE) {
8493                                 LOGE("fail to create text pipeline");
8494                                 return MM_ERROR_PLAYER_INTERNAL;
8495                         }
8496
8497                         result = _mmplayer_sync_subtitle_pipeline(player);
8498                 } else {
8499                         result = __mmplayer_change_external_subtitle_language(player, filepath);
8500                 }
8501
8502                 MMPLAYER_BUS_MSG_THREAD_SIGNAL(player);
8503                 player->is_external_subtitle_added_now = TRUE;
8504
8505                 MMPLAYER_SUBTITLE_INFO_LOCK(player);
8506                 if (!player->subtitle_language_list) {
8507                         gint64 timeout = g_get_monotonic_time() + G_TIME_SPAN_SECOND; /* wait 1 sec */
8508                         if (!MMPLAYER_SUBTITLE_INFO_WAIT_UNTIL(player, timeout))
8509                                 LOGW("subtitle language list is not updated yet");
8510                 }
8511                 MMPLAYER_SUBTITLE_INFO_UNLOCK(player);
8512         }
8513
8514         MMPLAYER_FLEAVE();
8515         return result;
8516 }
8517
8518 static int
8519 __mmplayer_switch_stream(mmplayer_t *player, mmplayer_track_type_e type, int index)
8520 {
8521         guint active_idx = 0;
8522         GstStream *stream = NULL;
8523         GList *streams = NULL;
8524         GstCaps *caps = NULL;
8525
8526         MMPLAYER_FENTER();
8527         LOGD("Switching Streams... type: %d, index: %d", type, index);
8528
8529         player->track[type].active_track_index = index;
8530
8531         for (int i = 0; i < MM_PLAYER_TRACK_TYPE_MAX; i++) {
8532                 /* FIXME: need to consider the non display type or audio only in case of MM_PLAYER_TRACK_TYPE_VIDEO */
8533                 LOGD("track type:%d, total: %d, active: %d", i,
8534                                 player->track[i].total_track_num, player->track[i].active_track_index);
8535                 if (player->track[i].total_track_num > 0 &&
8536                         player->track[i].active_track_index > INVALID_TRACK_INDEX) {
8537                         active_idx = player->track[i].active_track_index;
8538                         stream = g_ptr_array_index(player->track[i].streams, active_idx);
8539                         streams = g_list_append (streams, (gchar *)gst_stream_get_stream_id(stream));
8540                         LOGD("Selecting %d type stream : %s\n", i, gst_stream_get_stream_id(stream));
8541
8542                         if (i == MM_PLAYER_TRACK_TYPE_AUDIO) {
8543                                 caps = gst_stream_get_caps(stream);
8544                                 if (caps) {
8545                                         _mmplayer_set_audio_attrs(player, caps);
8546                                         gst_caps_unref(caps);
8547                                 }
8548                         }
8549                 }
8550         }
8551
8552         if (streams) {
8553                 LOGD("send select stream event");
8554                 gst_element_send_event(player->pipeline->mainbin[MMPLAYER_M_AUTOPLUG].gst,
8555                                 gst_event_new_select_streams(streams));
8556                 g_list_free(streams);
8557         }
8558
8559         /* in paused state, seek to current pos to flush mq buffer and release waiting task */
8560         if (MMPLAYER_CURRENT_STATE(player) == MM_PLAYER_STATE_PAUSED) {
8561                 gint64 pos_nsec = GST_CLOCK_TIME_NONE;
8562
8563                 if (!gst_element_query_position(player->pipeline->mainbin[MMPLAYER_M_PIPE].gst, GST_FORMAT_TIME, &pos_nsec))
8564                         pos_nsec = player->last_position;
8565
8566                 LOGD("current pos %" GST_TIME_FORMAT ", rate = %f", GST_TIME_ARGS(pos_nsec), player->playback_rate);
8567
8568                 if (!_mmplayer_gst_seek(player, player->pipeline->mainbin[MMPLAYER_M_PIPE].gst,
8569                                         player->playback_rate, GST_FORMAT_TIME,
8570                                         (GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_ACCURATE),
8571                                         GST_SEEK_TYPE_SET, pos_nsec, GST_SEEK_TYPE_SET, GST_CLOCK_TIME_NONE)) {
8572                         LOGW("failed to seek");
8573                         return MM_ERROR_PLAYER_INTERNAL;
8574                 }
8575         }
8576
8577         MMPLAYER_FLEAVE();
8578         return MM_ERROR_NONE;
8579 }
8580
8581 static int
8582 __mmplayer_change_selector_pad(mmplayer_t *player, mmplayer_track_type_e type, int index)
8583 {
8584         int result = MM_ERROR_NONE;
8585         gchar *change_pad_name = NULL;
8586         GstPad *sinkpad = NULL;
8587         mmplayer_gst_element_t *mainbin = NULL;
8588         main_element_id_e elem_idx = MMPLAYER_M_NUM;
8589         GstCaps *caps = NULL;
8590         gint total_track_num = 0;
8591
8592         MMPLAYER_FENTER();
8593
8594         MMPLAYER_RETURN_VAL_IF_FAIL(player && player->pipeline && player->pipeline->mainbin,
8595                                                                                                         MM_ERROR_PLAYER_NOT_INITIALIZED);
8596
8597         LOGD("Change Track(%d) to %d", type, index);
8598
8599         mainbin = player->pipeline->mainbin;
8600
8601         if (type == MM_PLAYER_TRACK_TYPE_AUDIO) {
8602                 elem_idx = MMPLAYER_M_A_INPUT_SELECTOR;
8603         } else if (type == MM_PLAYER_TRACK_TYPE_TEXT) {
8604                 elem_idx = MMPLAYER_M_T_INPUT_SELECTOR;
8605         } else {
8606                 /* Changing Video Track is not supported. */
8607                 LOGE("Track Type Error");
8608                 goto EXIT;
8609         }
8610
8611         if (mainbin[elem_idx].gst == NULL) {
8612                 result = MM_ERROR_PLAYER_NO_OP;
8613                 LOGD("Req track doesn't exist");
8614                 goto EXIT;
8615         }
8616
8617         total_track_num = player->track[type].total_track_num;
8618         if (total_track_num <= 0) {
8619                 result = MM_ERROR_PLAYER_NO_OP;
8620                 LOGD("Language list is not available");
8621                 goto EXIT;
8622         }
8623
8624         if ((index < 0) || (index >= total_track_num)) {
8625                 result = MM_ERROR_INVALID_ARGUMENT;
8626                 LOGD("Not a proper index : %d", index);
8627                 goto EXIT;
8628         }
8629
8630         /*To get the new pad from the selector*/
8631         change_pad_name = g_strdup_printf("sink_%u", index);
8632         if (change_pad_name == NULL) {
8633                 result = MM_ERROR_PLAYER_INTERNAL;
8634                 LOGD("Pad does not exists");
8635                 goto EXIT;
8636         }
8637
8638         LOGD("new active pad name: %s", change_pad_name);
8639
8640         sinkpad = gst_element_get_static_pad(mainbin[elem_idx].gst, change_pad_name);
8641         if (sinkpad == NULL) {
8642                 LOGD("sinkpad is NULL");
8643                 result = MM_ERROR_PLAYER_INTERNAL;
8644                 goto EXIT;
8645         }
8646
8647         LOGD("Set Active Pad - %s:%s", GST_DEBUG_PAD_NAME(sinkpad));
8648         g_object_set(mainbin[elem_idx].gst, "active-pad", sinkpad, NULL);
8649
8650         caps = gst_pad_get_current_caps(sinkpad);
8651         MMPLAYER_LOG_GST_CAPS_TYPE(caps);
8652
8653         if (sinkpad)
8654                 gst_object_unref(sinkpad);
8655
8656         if (type == MM_PLAYER_TRACK_TYPE_AUDIO)
8657                 _mmplayer_set_audio_attrs(player, caps);
8658
8659         if (caps)
8660                 gst_caps_unref(caps);
8661
8662 EXIT:
8663         MMPLAYER_FREEIF(change_pad_name);
8664         return result;
8665 }
8666
8667 int
8668 _mmplayer_change_track_language(MMHandleType hplayer, mmplayer_track_type_e type, int index)
8669 {
8670         int result = MM_ERROR_NONE;
8671         mmplayer_t *player = NULL;
8672         mmplayer_gst_element_t *mainbin = NULL;
8673
8674         gint current_active_index = 0;
8675
8676         GstState current_state = GST_STATE_VOID_PENDING;
8677         gint64 time = 0;
8678
8679         MMPLAYER_FENTER();
8680
8681         player = (mmplayer_t *)hplayer;
8682         MMPLAYER_RETURN_VAL_IF_FAIL(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
8683
8684         if (!player->pipeline) {
8685                 LOGE("Track %d pre setting -> %d", type, index);
8686
8687                 player->track[type].active_track_index = index;
8688                 goto EXIT;
8689         }
8690
8691         mainbin = player->pipeline->mainbin;
8692
8693         current_active_index = player->track[type].active_track_index;
8694
8695         /*If index is same as running index no need to change the pad*/
8696         if (current_active_index == index)
8697                 goto EXIT;
8698
8699         if (!gst_element_query_position(mainbin[MMPLAYER_M_PIPE].gst, GST_FORMAT_TIME, &time)) {
8700                 result = MM_ERROR_PLAYER_INVALID_STATE;
8701                 goto EXIT;
8702         }
8703
8704         current_state = GST_STATE(mainbin[MMPLAYER_M_PIPE].gst);
8705         if (current_state < GST_STATE_PAUSED) {
8706                 result = MM_ERROR_PLAYER_INVALID_STATE;
8707                 LOGW("Pipeline not in proper state");
8708                 goto EXIT;
8709         }
8710
8711         if (MMPLAYER_USE_DECODEBIN(player))
8712                 result = __mmplayer_change_selector_pad(player, type, index);
8713         else
8714                 result = __mmplayer_switch_stream(player, type, index);
8715
8716         if (result != MM_ERROR_NONE) {
8717                 LOGE("failed to change track");
8718                 goto EXIT;
8719         }
8720
8721         player->track[type].active_track_index = index;
8722
8723         if (MMPLAYER_USE_DECODEBIN(player)) {
8724                 GstEvent *event = NULL;
8725                 if (current_state == GST_STATE_PLAYING) {
8726                         event = gst_event_new_seek(player->playback_rate, GST_FORMAT_TIME,
8727                                 (GstSeekFlags)(GST_SEEK_FLAG_SEGMENT | GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_SKIP),
8728                                 GST_SEEK_TYPE_SET, time, GST_SEEK_TYPE_NONE, -1);
8729                         if (event) {
8730                                 _mmplayer_gst_send_event_to_sink(player, event);
8731                         } else {
8732                                 result = MM_ERROR_PLAYER_INTERNAL;
8733                                 goto EXIT;
8734                         }
8735                 }
8736         }
8737
8738 EXIT:
8739         return result;
8740 }
8741
8742 int
8743 _mmplayer_get_subtitle_silent(MMHandleType hplayer, int *silent)
8744 {
8745         mmplayer_t *player = (mmplayer_t *)hplayer;
8746
8747         MMPLAYER_FENTER();
8748
8749         /* check player handle */
8750         MMPLAYER_RETURN_VAL_IF_FAIL(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
8751
8752         *silent = player->set_mode.subtitle_off;
8753
8754         LOGD("subtitle is %s.", silent ? "ON" : "OFF");
8755
8756         MMPLAYER_FLEAVE();
8757
8758         return MM_ERROR_NONE;
8759 }
8760
8761 static gboolean
8762 __mmplayer_add_dump_buffer_probe(mmplayer_t *player, GstElement *element)
8763 {
8764         MMPLAYER_RETURN_VAL_IF_FAIL(player, FALSE);
8765         MMPLAYER_RETURN_VAL_IF_FAIL(element, FALSE);
8766
8767         gchar *factory_name = GST_OBJECT_NAME(gst_element_get_factory(element));
8768         gchar dump_file_name[PLAYER_INI_MAX_STRLEN*2];
8769
8770         int idx = 0;
8771
8772         for (idx = 0; player->ini.dump_element_keyword[idx][0] != '\0'; idx++) {
8773                 if (g_strrstr(factory_name, player->ini.dump_element_keyword[idx])) {
8774                         LOGD("dump [%s] sink pad", player->ini.dump_element_keyword[idx]);
8775                         mmplayer_dump_t *dump_s;
8776                         dump_s = g_try_malloc(sizeof(mmplayer_dump_t));
8777                         if (dump_s == NULL) {
8778                                 LOGE("malloc fail");
8779                                 return FALSE;
8780                         }
8781
8782                         dump_s->dump_element_file = NULL;
8783                         dump_s->dump_pad = NULL;
8784                         dump_s->dump_pad = gst_element_get_static_pad(element, "sink");
8785
8786                         if (dump_s->dump_pad) {
8787                                 memset(dump_file_name, 0x00, PLAYER_INI_MAX_STRLEN * 2);
8788                                 snprintf(dump_file_name, PLAYER_INI_MAX_STRLEN * 2, "%s/%s_sink_pad.dump", player->ini.dump_element_path, player->ini.dump_element_keyword[idx]);
8789                                 dump_s->dump_element_file = fopen(dump_file_name, "w+");
8790                                 dump_s->probe_handle_id = gst_pad_add_probe(dump_s->dump_pad, GST_PAD_PROBE_TYPE_BUFFER, __mmplayer_dump_buffer_probe_cb, dump_s->dump_element_file, NULL);
8791                                 /* add list for removed buffer probe and close FILE */
8792                                 player->dump_list = g_list_append(player->dump_list, dump_s);
8793                                 LOGD("%s sink pad added buffer probe for dump", factory_name);
8794                                 return TRUE;
8795                         } else {
8796                                 MMPLAYER_FREEIF(dump_s);
8797                                 LOGE("failed to get %s sink pad added", factory_name);
8798                         }
8799                 }
8800         }
8801         return FALSE;
8802 }
8803
8804 static GstPadProbeReturn
8805 __mmplayer_dump_buffer_probe_cb(GstPad *pad,  GstPadProbeInfo *info, gpointer u_data)
8806 {
8807         FILE *dump_data = (FILE *)u_data;
8808 //      int written = 0;
8809         GstBuffer *buffer = gst_pad_probe_info_get_buffer(info);
8810         GstMapInfo probe_info = GST_MAP_INFO_INIT;
8811
8812         MMPLAYER_RETURN_VAL_IF_FAIL(dump_data, GST_PAD_PROBE_PASS);
8813
8814         gst_buffer_map(buffer, &probe_info, GST_MAP_READ);
8815 #ifdef __DEBUG__
8816         LOGD("buffer timestamp = %" GST_TIME_FORMAT, GST_TIME_ARGS(GST_BUFFER_TIMESTAMP(buffer)));
8817 #endif
8818         fwrite(probe_info.data, 1, probe_info.size , dump_data);
8819
8820         gst_buffer_unmap(buffer, &probe_info);
8821
8822         return GST_PAD_PROBE_OK;
8823 }
8824
8825 static void
8826 __mmplayer_release_dump_list(GList *dump_list)
8827 {
8828         GList *d_list = dump_list;
8829
8830         if (!d_list)
8831                 return;
8832
8833         for (; d_list; d_list = g_list_next(d_list)) {
8834                 mmplayer_dump_t *dump_s = d_list->data;
8835                 if (dump_s->dump_pad) {
8836                         if (dump_s->probe_handle_id)
8837                                 gst_pad_remove_probe(dump_s->dump_pad, dump_s->probe_handle_id);
8838                         gst_object_unref(GST_OBJECT(dump_s->dump_pad));
8839                 }
8840                 if (dump_s->dump_element_file) {
8841                         fclose(dump_s->dump_element_file);
8842                         dump_s->dump_element_file = NULL;
8843                 }
8844                 MMPLAYER_FREEIF(dump_s);
8845         }
8846         g_list_free(dump_list);
8847         dump_list = NULL;
8848 }
8849
8850 int
8851 _mmplayer_has_closed_caption(MMHandleType hplayer, bool *exist)
8852 {
8853         mmplayer_t *player = (mmplayer_t *)hplayer;
8854
8855         MMPLAYER_FENTER();
8856
8857         MMPLAYER_RETURN_VAL_IF_FAIL(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
8858         MMPLAYER_RETURN_VAL_IF_FAIL(exist, MM_ERROR_INVALID_ARGUMENT);
8859
8860         *exist = (bool)player->has_closed_caption;
8861
8862         MMPLAYER_FLEAVE();
8863
8864         return MM_ERROR_NONE;
8865 }
8866
8867 void
8868 _mm_player_video_stream_internal_buffer_unref(void *buffer)
8869 {
8870         MMPLAYER_FENTER();
8871         if (buffer) {
8872 #ifdef __DEBUG__
8873                 LOGD("unref internal gst buffer %p", buffer);
8874 #endif
8875                 gst_buffer_unref((GstBuffer *)buffer);
8876                 buffer = NULL;
8877         }
8878         MMPLAYER_FLEAVE();
8879 }
8880
8881 int
8882 _mmplayer_get_timeout(MMHandleType hplayer, int *timeout)
8883 {
8884         mmplayer_t *player = (mmplayer_t *)hplayer;
8885
8886         MMPLAYER_FENTER();
8887
8888         MMPLAYER_RETURN_VAL_IF_FAIL(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
8889         MMPLAYER_RETURN_VAL_IF_FAIL(timeout, MM_ERROR_COMMON_INVALID_ARGUMENT);
8890
8891         if (MMPLAYER_IS_STREAMING(player))
8892                 *timeout = (int)player->ini.live_state_change_timeout;
8893         else
8894                 *timeout = (int)player->ini.localplayback_state_change_timeout;
8895
8896         LOGD("timeout = %d", *timeout);
8897
8898         MMPLAYER_FLEAVE();
8899         return MM_ERROR_NONE;
8900 }
8901
8902 static void
8903 __mmplayer_initialize_storage_info(mmplayer_t *player, mmplayer_path_type_e path_type)
8904 {
8905         int i = 0;
8906         MMPLAYER_FENTER();
8907         MMPLAYER_RETURN_IF_FAIL(player);
8908
8909         for (i = 0; i < MMPLAYER_PATH_MAX; i++) {
8910
8911                 if (path_type == MMPLAYER_PATH_MAX || path_type == i) {
8912                         player->storage_info[i].type = STORAGE_TYPE_INTERNAL;
8913                         player->storage_info[i].state = STORAGE_STATE_UNMOUNTABLE;
8914                         player->storage_info[i].id = -1;
8915                         memset(player->storage_info[i].path, 0x00, MM_MAX_URL_LEN);
8916
8917                         if (path_type != MMPLAYER_PATH_MAX)
8918                                 break;
8919                 }
8920         }
8921
8922         MMPLAYER_FLEAVE();
8923 }
8924
8925 int
8926 _mmplayer_manage_external_storage_state(MMHandleType hplayer, int id, int state)
8927 {
8928         int ret = MM_ERROR_NONE;
8929         mmplayer_t *player = (mmplayer_t *)hplayer;
8930         MMMessageParamType msg_param = {0, };
8931
8932         MMPLAYER_FENTER();
8933         MMPLAYER_RETURN_VAL_IF_FAIL(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
8934
8935         LOGW("state changed storage %d:%d", id, state);
8936
8937         if (state != STORAGE_STATE_UNMOUNTABLE && state != STORAGE_STATE_REMOVED)
8938                 return MM_ERROR_NONE;
8939
8940         /* FIXME: text path should be handled separately. */
8941         if (((player->storage_info[MMPLAYER_PATH_VOD].type == STORAGE_TYPE_EXTERNAL)
8942                 && (player->storage_info[MMPLAYER_PATH_VOD].id == id)) ||
8943                 ((player->storage_info[MMPLAYER_PATH_TEXT].type == STORAGE_TYPE_EXTERNAL)
8944                 && (player->storage_info[MMPLAYER_PATH_TEXT].id == id))) {
8945                 LOGW("external storage is removed");
8946
8947                 if (player->msg_posted == FALSE) {
8948                         memset(&msg_param, 0, sizeof(MMMessageParamType));
8949                         msg_param.code = MM_ERROR_PLAYER_INVALID_URI;
8950                         MMPLAYER_POST_MSG(player, MM_MESSAGE_ERROR, &msg_param);
8951                         player->msg_posted = TRUE;
8952                 }
8953
8954                 /* unrealize the player */
8955                 ret = _mmplayer_unrealize(hplayer);
8956                 if (ret != MM_ERROR_NONE)
8957                         LOGE("failed to unrealize");
8958         }
8959
8960         MMPLAYER_FLEAVE();
8961         return ret;
8962 }
8963
8964 int
8965 _mmplayer_get_adaptive_variant_info(MMHandleType hplayer, int *num, char **var_info)
8966 {
8967         int ret = MM_ERROR_NONE;
8968         mmplayer_t *player = (mmplayer_t *)hplayer;
8969         int idx = 0, total = 0;
8970         gchar *result = NULL, *tmp = NULL;
8971
8972         MMPLAYER_FENTER();
8973         MMPLAYER_RETURN_VAL_IF_FAIL(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
8974         MMPLAYER_RETURN_VAL_IF_FAIL(num && var_info, MM_ERROR_COMMON_INVALID_ARGUMENT);
8975
8976         total = *num = g_list_length(player->adaptive_info.var_list);
8977         if (total <= 0) {
8978                 LOGW("There is no stream variant info.");
8979                 return ret;
8980         }
8981
8982         result = g_strdup("");
8983         for (idx = 0 ; idx < total ; idx++) {
8984                 stream_variant_t *v_data = NULL;
8985                 v_data = g_list_nth_data(player->adaptive_info.var_list, idx);
8986
8987                 if (v_data) {
8988                         gchar data[64] = {0};
8989                         snprintf(data, sizeof(data), "%d,%d,%d,", v_data->bandwidth, v_data->width, v_data->height);
8990
8991                         tmp = g_strconcat(result, data, NULL);
8992                         g_free(result);
8993                         result = tmp;
8994                 } else {
8995                         LOGW("There is no variant data in %d", idx);
8996                         (*num)--;
8997                 }
8998         }
8999
9000         *var_info = (char *)result;
9001
9002         LOGD("variant info %d:%s", *num, *var_info);
9003         MMPLAYER_FLEAVE();
9004         return ret;
9005 }
9006
9007 int
9008 _mmplayer_set_max_adaptive_variant_limit(MMHandleType hplayer, int bandwidth, int width, int height)
9009 {
9010         int ret = MM_ERROR_NONE;
9011         mmplayer_t *player = (mmplayer_t *)hplayer;
9012
9013         MMPLAYER_FENTER();
9014         MMPLAYER_RETURN_VAL_IF_FAIL(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
9015
9016         LOGD("set limit to [b]%d, [w]%d, [h]%d", bandwidth, width, height);
9017
9018         player->adaptive_info.limit.bandwidth = (bandwidth >= ADAPTIVE_VARIANT_DEFAULT_VALUE) ? (bandwidth) : (ADAPTIVE_VARIANT_DEFAULT_VALUE);
9019         player->adaptive_info.limit.width = (width >= ADAPTIVE_VARIANT_DEFAULT_VALUE) ? (width) : (ADAPTIVE_VARIANT_DEFAULT_VALUE);
9020         player->adaptive_info.limit.height = (height >= ADAPTIVE_VARIANT_DEFAULT_VALUE) ? (height) : (ADAPTIVE_VARIANT_DEFAULT_VALUE);
9021
9022         if (player->pipeline && player->pipeline->mainbin && player->pipeline->mainbin[MMPLAYER_M_ADAPTIVE_DEMUX].gst) {
9023                 LOGD("update max limit of %s", GST_ELEMENT_NAME(player->pipeline->mainbin[MMPLAYER_M_ADAPTIVE_DEMUX].gst));
9024                 g_object_set(player->pipeline->mainbin[MMPLAYER_M_ADAPTIVE_DEMUX].gst,
9025                                                 "max-bandwidth", bandwidth, "max-video-width", width, "max-video-height", height, NULL);
9026
9027                 /* FIXME: seek to current position for applying new variant limitation */
9028         }
9029
9030         MMPLAYER_FLEAVE();
9031         return ret;
9032
9033 }
9034
9035 int
9036 _mmplayer_get_max_adaptive_variant_limit(MMHandleType hplayer, int *bandwidth, int *width, int *height)
9037 {
9038         int ret = MM_ERROR_NONE;
9039         mmplayer_t *player = (mmplayer_t *)hplayer;
9040
9041         MMPLAYER_FENTER();
9042         MMPLAYER_RETURN_VAL_IF_FAIL(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
9043         MMPLAYER_RETURN_VAL_IF_FAIL(bandwidth && width && height, MM_ERROR_COMMON_INVALID_ARGUMENT);
9044
9045         *bandwidth = player->adaptive_info.limit.bandwidth;
9046         *width = player->adaptive_info.limit.width;
9047         *height = player->adaptive_info.limit.height;
9048
9049         LOGD("get limit to [b]%d, [w]%d, [h]%d", *bandwidth, *width, *height);
9050
9051         MMPLAYER_FLEAVE();
9052         return ret;
9053 }
9054
9055 int
9056 _mmplayer_get_streaming_buffering_time(MMHandleType hplayer, int *prebuffer_ms, int *rebuffer_ms)
9057 {
9058         int ret = MM_ERROR_NONE;
9059         mmplayer_t *player = (mmplayer_t *)hplayer;
9060
9061         MMPLAYER_FENTER();
9062         MMPLAYER_RETURN_VAL_IF_FAIL(player && player->streamer, MM_ERROR_PLAYER_NOT_INITIALIZED);
9063         MMPLAYER_RETURN_VAL_IF_FAIL(prebuffer_ms && rebuffer_ms, MM_ERROR_COMMON_INVALID_ARGUMENT);
9064         MMPLAYER_RETURN_VAL_IF_FAIL(MMPLAYER_IS_STREAMING(player), MM_ERROR_PLAYER_NO_OP);
9065
9066         *prebuffer_ms = player->streamer->buffering_req.prebuffer_time;
9067
9068         if (player->streamer->buffering_req.rebuffer_time > MIN_BUFFERING_TIME)
9069                 *rebuffer_ms = player->streamer->buffering_req.rebuffer_time;
9070         else /* live case */
9071                 *rebuffer_ms = DEFAULT_LIVE_REBUFFER_TIME;
9072
9073         LOGD("buffering time %d ms / %d ms", *prebuffer_ms, *rebuffer_ms);
9074
9075         MMPLAYER_FLEAVE();
9076         return ret;
9077 }
9078
9079 int
9080 _mmplayer_set_codec_type(MMHandleType hplayer, mmplayer_stream_type_e stream_type, mmplayer_codec_type_e codec_type)
9081 {
9082 #define IDX_FIRST_SW_CODEC 0
9083         mmplayer_t *player = (mmplayer_t *)hplayer;
9084         int default_codec_type = MM_PLAYER_CODEC_TYPE_DEFAULT;
9085         const char *attr_name = NULL;
9086         const char *default_type = NULL;
9087         const char *element_hw = NULL;
9088         const char *element_sw = NULL;
9089
9090         MMPLAYER_FENTER();
9091         MMPLAYER_RETURN_VAL_IF_FAIL(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
9092
9093         LOGD("stream type: %d, codec_type: %d", stream_type, codec_type);
9094
9095         /* FIXME: player need to know whether the decoder exist or not about required codec type since 6.0*/
9096         switch (stream_type) {
9097         case MM_PLAYER_STREAM_TYPE_AUDIO:
9098                 attr_name = MM_PLAYER_AUDIO_CODEC_TYPE;
9099                 default_type = player->ini.audiocodec_default_type;
9100                 element_hw = player->ini.audiocodec_element_hw;
9101                 element_sw = player->ini.audiocodec_element_sw[IDX_FIRST_SW_CODEC];
9102                 break;
9103         case MM_PLAYER_STREAM_TYPE_VIDEO:
9104                 attr_name = MM_PLAYER_VIDEO_CODEC_TYPE;
9105                 default_type = player->ini.videocodec_default_type;
9106                 element_hw = player->ini.videocodec_element_hw;
9107                 element_sw = player->ini.videocodec_element_sw[IDX_FIRST_SW_CODEC];
9108                 break;
9109         default:
9110                 LOGE("Invalid stream type %s", MMPLAYER_STREAM_TYPE_GET_NAME(stream_type));
9111                 return MM_ERROR_COMMON_INVALID_ARGUMENT;
9112                 break;
9113         }
9114
9115         LOGD("default setting: [%s][%s][h:%s][s:%s]", attr_name, default_type, element_hw, element_sw);
9116
9117         if (!strcmp(default_type, "sw"))
9118                 default_codec_type = MM_PLAYER_CODEC_TYPE_SW;
9119         else
9120                 default_codec_type = MM_PLAYER_CODEC_TYPE_HW;
9121
9122         if (codec_type == MM_PLAYER_CODEC_TYPE_DEFAULT)
9123                 codec_type = default_codec_type;
9124
9125         /* to support codec selection, codec info have to be added in ini file.
9126            in case of hw codec is selected, filter elements should be applied
9127            depending on the hw capabilities. */
9128         if (codec_type != default_codec_type) {
9129                 if (((codec_type == MM_PLAYER_CODEC_TYPE_HW) && (!strcmp(element_hw, ""))) ||
9130                         ((codec_type == MM_PLAYER_CODEC_TYPE_SW) && (!strcmp(element_sw, "")))) {
9131                         LOGE("There is no codec for type %d", codec_type);
9132                         return MM_ERROR_PLAYER_NO_OP;
9133                 }
9134
9135                 LOGD("sorting is required");
9136                 if (stream_type == MM_PLAYER_STREAM_TYPE_AUDIO)
9137                         player->need_audio_dec_sorting = TRUE;
9138                 else
9139                         player->need_video_dec_sorting = TRUE;
9140         }
9141
9142         LOGD("update %s codec_type to %d", attr_name, codec_type);
9143         mm_player_set_attribute(hplayer, NULL, attr_name, codec_type, NULL);
9144
9145         MMPLAYER_FLEAVE();
9146         return MM_ERROR_NONE;
9147 }
9148
9149 int
9150 _mmplayer_set_replaygain_enabled(MMHandleType hplayer, bool enabled)
9151 {
9152         mmplayer_t *player = (mmplayer_t *)hplayer;
9153         GstElement *rg_vol_element = NULL;
9154
9155         MMPLAYER_FENTER();
9156
9157         MMPLAYER_RETURN_VAL_IF_FAIL(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
9158
9159         player->sound.rg_enable = enabled;
9160
9161         /* just hold rgvolume enable value if pipeline is not ready */
9162         if (!player->pipeline || !player->pipeline->audiobin) {
9163                 LOGD("pipeline is not ready. holding rgvolume enable value");
9164                 return MM_ERROR_NONE;
9165         }
9166
9167         rg_vol_element = player->pipeline->audiobin[MMPLAYER_A_RGVOL].gst;
9168
9169         if (!rg_vol_element) {
9170                 LOGD("rgvolume element is not created");
9171                 return MM_ERROR_PLAYER_INTERNAL;
9172         }
9173
9174         if (enabled)
9175                 g_object_set(rg_vol_element, "enable-rgvolume", TRUE, NULL);
9176         else
9177                 g_object_set(rg_vol_element, "enable-rgvolume", FALSE, NULL);
9178
9179         MMPLAYER_FLEAVE();
9180
9181         return MM_ERROR_NONE;
9182 }
9183
9184 int
9185 _mmplayer_is_replaygain_enabled(MMHandleType hplayer, bool *enabled)
9186 {
9187         mmplayer_t *player = (mmplayer_t *)hplayer;
9188         GstElement *rg_vol_element = NULL;
9189         gboolean enable = FALSE;
9190
9191         MMPLAYER_FENTER();
9192
9193         MMPLAYER_RETURN_VAL_IF_FAIL(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
9194         MMPLAYER_RETURN_VAL_IF_FAIL(enabled, MM_ERROR_INVALID_ARGUMENT);
9195
9196         /* just hold enable_rg value if pipeline is not ready */
9197         if (!player->pipeline || !player->pipeline->audiobin) {
9198                 LOGD("pipeline is not ready. holding rgvolume value (%d)", player->sound.rg_enable);
9199                 *enabled = player->sound.rg_enable;
9200                 return MM_ERROR_NONE;
9201         }
9202
9203         rg_vol_element = player->pipeline->audiobin[MMPLAYER_A_RGVOL].gst;
9204
9205         if (!rg_vol_element) {
9206                 LOGD("rgvolume element is not created");
9207                 return MM_ERROR_PLAYER_INTERNAL;
9208         }
9209
9210         g_object_get(rg_vol_element, "enable-rgvolume", &enable, NULL);
9211         *enabled = (bool)enable;
9212
9213         MMPLAYER_FLEAVE();
9214
9215         return MM_ERROR_NONE;
9216 }
9217
9218 int
9219 _mmplayer_set_video_roi_area(MMHandleType hplayer, double scale_x, double scale_y, double scale_width, double scale_height)
9220 {
9221         mmplayer_t *player = (mmplayer_t *)hplayer;
9222         MMHandleType attrs = 0;
9223         int handle = 0;
9224         int ret = MM_ERROR_NONE;
9225
9226         MMPLAYER_FENTER();
9227
9228         MMPLAYER_RETURN_VAL_IF_FAIL(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
9229
9230         attrs = MMPLAYER_GET_ATTRS(player);
9231         MMPLAYER_RETURN_VAL_IF_FAIL(attrs, MM_ERROR_PLAYER_INTERNAL);
9232
9233         mm_attrs_get_int_by_name(attrs, "display_overlay", &handle);
9234         if (!handle) {
9235                 LOGE("Display handle is NULL, after setting window handle, set video roi area");
9236                 return MM_ERROR_PLAYER_INTERNAL;
9237         }
9238
9239         player->video_roi.scale_x = scale_x;
9240         player->video_roi.scale_y = scale_y;
9241         player->video_roi.scale_width = scale_width;
9242         player->video_roi.scale_height = scale_height;
9243
9244         /* check video sinkbin is created */
9245         if (!_mmplayer_is_videosink_ready(player, MM_DISPLAY_SURFACE_NUM))
9246                 return MM_ERROR_NONE;
9247
9248         if (!gst_video_overlay_set_video_roi_area(
9249                 GST_VIDEO_OVERLAY(player->pipeline->videobin[MMPLAYER_V_SINK].gst),
9250                 scale_x, scale_y, scale_width, scale_height))
9251                 ret = MM_ERROR_PLAYER_INTERNAL;
9252         else
9253                 LOGD("set video param : video roi area scale value: x(%f) y(%f) width(%f) height(%f)",
9254                         scale_x, scale_y, scale_width, scale_height);
9255
9256         MMPLAYER_FLEAVE();
9257
9258         return ret;
9259 }
9260
9261 int
9262 _mmplayer_get_video_roi_area(MMHandleType hplayer, double *scale_x, double *scale_y, double *scale_width, double *scale_height)
9263 {
9264         mmplayer_t *player = (mmplayer_t *)hplayer;
9265         int ret = MM_ERROR_NONE;
9266
9267         MMPLAYER_FENTER();
9268
9269         MMPLAYER_RETURN_VAL_IF_FAIL(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
9270         MMPLAYER_RETURN_VAL_IF_FAIL(scale_x && scale_y && scale_width && scale_height, MM_ERROR_INVALID_ARGUMENT);
9271
9272         *scale_x = player->video_roi.scale_x;
9273         *scale_y = player->video_roi.scale_y;
9274         *scale_width = player->video_roi.scale_width;
9275         *scale_height = player->video_roi.scale_height;
9276
9277         LOGD("get video param : video roi area scale value: x(%f) y(%f) width(%f) height(%f)",
9278                 *scale_x, *scale_y, *scale_width, *scale_height);
9279
9280         return ret;
9281 }
9282
9283 int
9284 _mmplayer_set_client_pid(MMHandleType hplayer, int pid)
9285 {
9286         mmplayer_t *player = (mmplayer_t *)hplayer;
9287
9288         MMPLAYER_FENTER();
9289
9290         MMPLAYER_RETURN_VAL_IF_FAIL(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
9291
9292         player->client_pid = pid;
9293
9294         LOGD("client pid[%d] %p", pid, player);
9295
9296         MMPLAYER_FLEAVE();
9297
9298         return MM_ERROR_NONE;
9299 }
9300
9301 int
9302 _mmplayer_is_audio_control_available(MMHandleType hplayer, mmplayer_audio_control_opt_e opt, bool *available)
9303 {
9304         mmplayer_t *player = (mmplayer_t *)hplayer;
9305         mmplayer_codec_type_e codec_type = MM_PLAYER_CODEC_TYPE_DEFAULT;
9306         enum audio_element_id elem_id = MMPLAYER_A_NUM;
9307
9308         MMPLAYER_FENTER();
9309
9310         MMPLAYER_RETURN_VAL_IF_FAIL(player, MM_ERROR_PLAYER_NOT_INITIALIZED);
9311         MMPLAYER_RETURN_VAL_IF_FAIL(available, MM_ERROR_INVALID_ARGUMENT);
9312
9313         *available = true;
9314         mm_attrs_get_int_by_name(player->attrs, MM_PLAYER_AUDIO_CODEC_TYPE, (int *)&codec_type);
9315
9316         LOGD("current state %d, codec_type %d", MMPLAYER_CURRENT_STATE(player), codec_type);
9317
9318         if (codec_type == MM_PLAYER_CODEC_TYPE_SW)
9319                 return MM_ERROR_NONE;
9320
9321         /* in case of audio codec default type is HW */
9322         switch(opt) {
9323                 case MM_PLAYER_AUDIO_CONTROL_OPT_EFFECT:
9324                         if (player->ini.support_audio_effect)
9325                                 return MM_ERROR_NONE;
9326                         elem_id = MMPLAYER_A_FILTER;
9327                 break;
9328                 case MM_PLAYER_AUDIO_CONTROL_OPT_REPLAYGAIN:
9329                         if (player->ini.support_replaygain_control)
9330                                 return MM_ERROR_NONE;
9331                         elem_id = MMPLAYER_A_RGVOL;
9332                 break;
9333                 case MM_PLAYER_AUDIO_CONTROL_OPT_PITCH:
9334                         if (player->ini.support_pitch_control)
9335                                 return MM_ERROR_NONE;
9336                         elem_id = MMPLAYER_A_PITCH;
9337                 break;
9338                 case MM_PLAYER_AUDIO_CONTROL_OPT_PCM_EXPORTING:
9339                         if (player->ini.support_audio_effect)
9340                                 return MM_ERROR_NONE;
9341                 break;
9342                 /* default case handling is not required */
9343         }
9344
9345         if (MMPLAYER_CURRENT_STATE(player) < MM_PLAYER_STATE_READY) {
9346                 LOGW("audio control option [%d] is not available", opt);
9347                 *available = false;
9348         } else {
9349                 /* setting pcm exporting option is allowed before READY state */
9350                 if (opt == MM_PLAYER_AUDIO_CONTROL_OPT_PCM_EXPORTING)
9351                         return MM_ERROR_PLAYER_INVALID_STATE;
9352
9353                 /* check whether the audio filter exist or not after READY state,
9354                    because the sw codec could be added during auto-plugging in some cases */
9355                 if (!player->pipeline ||
9356                         !player->pipeline->audiobin ||
9357                         !player->pipeline->audiobin[elem_id].gst) {
9358                         LOGW("there is no audio elem [%d]", elem_id);
9359                         *available = false;
9360                 }
9361         }
9362
9363         LOGD("audio control opt %d, available %d", opt, *available);
9364
9365         MMPLAYER_FLEAVE();
9366
9367         return MM_ERROR_NONE;
9368 }
9369
9370 static gboolean
9371 __mmplayer_update_duration_value(mmplayer_t *player)
9372 {
9373         gboolean ret = FALSE;
9374         gint64 dur_nsec = 0;
9375         LOGD("try to update duration");
9376
9377         if (gst_element_query_duration(player->pipeline->mainbin[MMPLAYER_M_PIPE].gst, GST_FORMAT_TIME, &dur_nsec) && (dur_nsec > 0)) {
9378                 player->duration = dur_nsec;
9379                 LOGW("duration : %"G_GINT64_FORMAT" msec", GST_TIME_AS_MSECONDS(dur_nsec));
9380                 ret = TRUE;
9381         }
9382
9383         if (player->duration < 0) {
9384                 LOGW("duration is Non-Initialized !!!");
9385                 player->duration = 0;
9386         }
9387
9388         /* update streaming service type */
9389         player->streaming_type =  _mmplayer_get_stream_service_type(player);
9390
9391         /* check duration is OK */
9392         if (dur_nsec == 0 && !MMPLAYER_IS_LIVE_STREAMING(player))
9393                 /* FIXIT : find another way to get duration here. */
9394                 LOGW("finally it's failed to get duration from pipeline. progressbar will not work correctely!");
9395
9396         return ret;
9397 }
9398
9399 static gboolean
9400 __mmplayer_update_audio_attrs(mmplayer_t *player, MMHandleType attrs)
9401 {
9402         /* update audio params
9403         NOTE : We need original audio params and it can be only obtained from src pad of audio
9404         decoder. Below code only valid when we are not using 'resampler' just before
9405         'audioconverter'. */
9406         GstCaps *caps_a = NULL;
9407         GstPad *pad = NULL;
9408         gint samplerate = 0, channels = 0;
9409         GstStructure *p = NULL;
9410         GstElement *aconv = NULL;
9411
9412         LOGD("try to update audio attrs");
9413
9414         MMPLAYER_RETURN_VAL_IF_FAIL(player->pipeline->audiobin, FALSE);
9415
9416         if (player->pipeline->audiobin[MMPLAYER_A_CONV].gst) {
9417                 aconv = player->pipeline->audiobin[MMPLAYER_A_CONV].gst;
9418         } else if (player->pipeline->audiobin[MMPLAYER_A_EXTRACT_CONV].gst) {
9419                 aconv = player->pipeline->audiobin[MMPLAYER_A_EXTRACT_CONV].gst;
9420         } else {
9421                 LOGE("there is no audio converter");
9422                 return FALSE;
9423         }
9424
9425         pad = gst_element_get_static_pad(aconv, "sink");
9426
9427         if (!pad) {
9428                 LOGW("failed to get pad from audio converter");
9429                 return FALSE;
9430         }
9431
9432         caps_a = gst_pad_get_current_caps(pad);
9433         if (!caps_a) {
9434                 LOGW("not ready to get audio caps");
9435                 gst_object_unref(pad);
9436                 return FALSE;
9437         }
9438
9439         p = gst_caps_get_structure(caps_a, 0);
9440         gst_structure_get_int(p, "rate", &samplerate);
9441         gst_structure_get_int(p, "channels", &channels);
9442
9443         mm_player_set_attribute((MMHandleType)player, NULL,
9444                         "content_audio_samplerate", samplerate,
9445                         "content_audio_channels", channels, NULL);
9446
9447         SECURE_LOGD("samplerate : %d    channels : %d", samplerate, channels);
9448
9449         gst_caps_unref(caps_a);
9450         gst_object_unref(pad);
9451
9452         return TRUE;
9453 }
9454
9455 static gboolean
9456 __mmplayer_update_video_attrs(mmplayer_t *player, MMHandleType attrs)
9457 {
9458         LOGD("try to update video attrs");
9459
9460         GstCaps *caps_v = NULL;
9461         GstPad *pad = NULL;
9462         gint tmpNu, tmpDe;
9463         gint width, height;
9464         GstStructure *p = NULL;
9465
9466         MMPLAYER_RETURN_VAL_IF_FAIL(player->pipeline->videobin, FALSE);
9467         MMPLAYER_RETURN_VAL_IF_FAIL(player->pipeline->videobin[MMPLAYER_V_SINK].gst, FALSE);
9468
9469         pad = gst_element_get_static_pad(player->pipeline->videobin[MMPLAYER_V_SINK].gst, "sink");
9470         if (!pad) {
9471                 LOGD("no videosink sink pad");
9472                 return FALSE;
9473         }
9474
9475         caps_v = gst_pad_get_current_caps(pad);
9476         /* Use v_stream_caps, if fail to get video_sink sink pad*/
9477         if (!caps_v && player->v_stream_caps) {
9478                 caps_v = player->v_stream_caps;
9479                 gst_caps_ref(caps_v);
9480         }
9481
9482         if (!caps_v) {
9483                 LOGD("no negotiated caps from videosink");
9484                 gst_object_unref(pad);
9485                 return FALSE;
9486         }
9487
9488         p = gst_caps_get_structure(caps_v, 0);
9489         gst_structure_get_int(p, "width", &width);
9490         gst_structure_get_int(p, "height", &height);
9491
9492         mm_player_set_attribute((MMHandleType)player, NULL,
9493                         MM_PLAYER_VIDEO_WIDTH, width, MM_PLAYER_VIDEO_HEIGHT, height, NULL);
9494
9495         gst_structure_get_fraction(p, "framerate", &tmpNu, &tmpDe);
9496
9497         SECURE_LOGD("width : %d     height : %d", width, height);
9498
9499         gst_caps_unref(caps_v);
9500         gst_object_unref(pad);
9501
9502         if (tmpDe > 0) {
9503                 mm_player_set_attribute((MMHandleType)player, NULL,
9504                                 MM_PLAYER_VIDEO_FPS, (tmpNu/tmpDe), NULL);
9505                 SECURE_LOGD("fps : %d", tmpNu / tmpDe);
9506         }
9507
9508         return TRUE;
9509 }
9510
9511 static gboolean
9512 __mmplayer_update_bitrate_attrs(mmplayer_t *player, MMHandleType attrs)
9513 {
9514         gboolean ret = FALSE;
9515         guint64 data_size = 0;
9516         gchar *path = NULL;
9517         struct stat sb;
9518
9519         /* FIXIT : please make it clear the dependency with duration/codec/uritype */
9520         if (!player->duration)
9521                 return FALSE;
9522
9523         if (!MMPLAYER_IS_STREAMING(player) && (player->can_support_codec & FOUND_PLUGIN_VIDEO)) {
9524                 mm_attrs_get_string_by_name(attrs, "profile_uri", &path);
9525                 if (stat(path, &sb) == 0)
9526                         data_size = (guint64)sb.st_size;
9527
9528         } else if (MMPLAYER_IS_HTTP_STREAMING(player)) {
9529                 data_size = player->http_content_size;
9530         }
9531
9532         LOGD("try to update bitrate : data_size = %"G_GUINT64_FORMAT, data_size);
9533
9534         if (data_size) {
9535                 guint64 bitrate = 0;
9536                 guint64 msec_dur = 0;
9537
9538                 msec_dur = GST_TIME_AS_MSECONDS(player->duration);
9539                 if (msec_dur > 0) {
9540                         bitrate = data_size * 8 * 1000 / msec_dur;
9541                         SECURE_LOGD("file size : %"G_GUINT64_FORMAT
9542                                         ", video bitrate = %"G_GUINT64_FORMAT, data_size, bitrate);
9543                         mm_player_set_attribute((MMHandleType)player, NULL,
9544                                         MM_PLAYER_VIDEO_BITRATE, (int)bitrate, NULL);
9545                         ret = TRUE;
9546                 } else {
9547                         LOGD("player duration is less than 0");
9548                 }
9549         }
9550
9551         if (MMPLAYER_IS_RTSP_STREAMING(player)) {
9552                 if (player->total_bitrate) {
9553                         mm_player_set_attribute((MMHandleType)player, NULL,
9554                                         MM_PLAYER_VIDEO_BITRATE, player->total_bitrate, NULL);
9555                         ret = TRUE;
9556                 }
9557         }
9558
9559         return ret;
9560 }
9561
9562 static void
9563 __mmplayer_copy_uri_and_set_type(mmplayer_parse_profile_t *data, const char *uri, int uri_type)
9564 {
9565         strncpy(data->uri, uri, MM_MAX_URL_LEN - 1);
9566         data->uri_type = uri_type;
9567 }
9568
9569 static int
9570 __mmplayer_set_mem_uri(mmplayer_parse_profile_t *data, char *path, void *param)
9571 {
9572         int ret = MM_ERROR_PLAYER_INVALID_URI;
9573         int mem_size = 0;
9574         char *buffer = NULL;
9575         char *seperator = strchr(path, ',');
9576         char ext[100] = {0,}, size[100] = {0,};
9577
9578         if (seperator) {
9579                 if ((buffer = strstr(path, "ext="))) {
9580                         buffer += strlen("ext=");
9581
9582                         if (strlen(buffer)) {
9583                                 strncpy(ext, buffer, 99);
9584
9585                                 if ((seperator = strchr(ext, ','))
9586                                         || (seperator = strchr(ext, ' '))
9587                                         || (seperator = strchr(ext, '\0'))) {
9588                                         seperator[0] = '\0';
9589                                 }
9590                         }
9591                 }
9592
9593                 if ((buffer = strstr(path, "size="))) {
9594                         buffer += strlen("size=");
9595
9596                         if (strlen(buffer) > 0) {
9597                                 strncpy(size, buffer, 99);
9598
9599                                 if ((seperator = strchr(size, ','))
9600                                         || (seperator = strchr(size, ' '))
9601                                         || (seperator = strchr(size, '\0'))) {
9602                                         seperator[0] = '\0';
9603                                 }
9604
9605                                 mem_size = atoi(size);
9606                         }
9607                 }
9608         }
9609
9610         LOGD("ext: %s, mem_size: %d, mmap(param): %p", ext, mem_size, param);
9611
9612         if (mem_size && param) {
9613                 if (data->input_mem.buf)
9614                         free(data->input_mem.buf);
9615                 data->input_mem.buf = malloc(mem_size);
9616
9617                 if (data->input_mem.buf) {
9618                         memcpy(data->input_mem.buf, param, mem_size);
9619                         data->input_mem.len = mem_size;
9620                         ret = MM_ERROR_NONE;
9621                 } else {
9622                         LOGE("failed to alloc mem %d", mem_size);
9623                         ret = MM_ERROR_PLAYER_INTERNAL;
9624                 }
9625
9626                 data->input_mem.offset = 0;
9627                 data->uri_type = MM_PLAYER_URI_TYPE_MEM;
9628         }
9629
9630         return ret;
9631 }
9632
9633 static int
9634 __mmplayer_set_file_uri(mmplayer_parse_profile_t *data, const char *uri)
9635 {
9636         gchar *location = NULL;
9637         GError *err = NULL;
9638         char *path = NULL;
9639         int ret = MM_ERROR_NONE;
9640
9641         if ((path = strstr(uri, "file://"))) {
9642                 location = g_filename_from_uri(uri, NULL, &err);
9643                 if (!location || (err != NULL)) {
9644                         LOGE("Invalid URI '%s' for filesrc: %s", path,
9645                                 (err != NULL) ? err->message : "unknown error");
9646                         if (err)
9647                                 g_error_free(err);
9648
9649                         MMPLAYER_FREEIF(location);
9650
9651                         data->uri_type = MM_PLAYER_URI_TYPE_NONE;
9652                         return MM_ERROR_PLAYER_INVALID_URI;
9653                 }
9654                 LOGD("path from uri: %s", location);
9655         }
9656
9657         path = (location != NULL) ? (location) : ((char *)uri);
9658
9659
9660         ret = _mmplayer_exist_file_path(path);
9661
9662         /* if no protocol prefix exist. check file existence and then give file:// as it's prefix */
9663         if (ret == MM_ERROR_NONE) {
9664                 if (_mmplayer_is_sdp_file(path)) {
9665                         LOGD("uri is actually a file but it's sdp file. giving it to rtspsrc");
9666                         g_snprintf(data->uri,  MM_MAX_URL_LEN, "rtsp-sdp://%s", path);
9667                         data->uri_type = MM_PLAYER_URI_TYPE_URL_RTSP;
9668                 } else {
9669                         g_snprintf(data->uri,  MM_MAX_URL_LEN, "file://%s", path);
9670                         data->uri_type = MM_PLAYER_URI_TYPE_FILE;
9671                 }
9672         } else if (ret == MM_ERROR_PLAYER_PERMISSION_DENIED) {
9673                 data->uri_type = MM_PLAYER_URI_TYPE_NO_PERMISSION;
9674         } else {
9675                 LOGE("invalid uri, could not play..");
9676                 data->uri_type = MM_PLAYER_URI_TYPE_NONE;
9677         }
9678
9679         MMPLAYER_FREEIF(location);
9680
9681         return ret;
9682 }
9683
9684 static mmplayer_video_decoded_data_info_t *
9685 __mmplayer_create_stream_from_pad(GstPad *pad)
9686 {
9687         GstCaps *caps = NULL;
9688         GstStructure *structure = NULL;
9689         unsigned int fourcc = 0;
9690         const gchar *string_format = NULL;
9691         mmplayer_video_decoded_data_info_t *stream = NULL;
9692         gint width, height;
9693         MMPixelFormatType format;
9694         GstVideoInfo info;
9695
9696         caps = gst_pad_get_current_caps(pad);
9697         if (!caps) {
9698                 LOGE("Caps is NULL.");
9699                 return NULL;
9700         }
9701
9702 #ifdef __DEBUG__
9703         MMPLAYER_LOG_GST_CAPS_TYPE(caps);
9704 #endif
9705         structure = gst_caps_get_structure(caps, 0);
9706         gst_structure_get_int(structure, "width", &width);
9707         gst_structure_get_int(structure, "height", &height);
9708         string_format = gst_structure_get_string(structure, "format");
9709
9710         if (string_format)
9711                 fourcc = _mmplayer_convert_fourcc_string_to_value(string_format);
9712         format = _mmplayer_get_pixtype(fourcc);
9713         gst_video_info_from_caps(&info, caps);
9714         gst_caps_unref(caps);
9715
9716         /* moved here */
9717         if (width == 0 || height == 0 || format == MM_PIXEL_FORMAT_INVALID) {
9718                 LOGE("Wrong condition!!");
9719                 return NULL;
9720         }
9721
9722         stream = (mmplayer_video_decoded_data_info_t *)g_try_malloc0(sizeof(mmplayer_video_decoded_data_info_t));
9723         if (!stream) {
9724                 LOGE("failed to alloc mem for video data");
9725                 return NULL;
9726         }
9727
9728         stream->width = width;
9729         stream->height = height;
9730         stream->format = format;
9731         stream->plane_num = GST_VIDEO_INFO_N_PLANES(&info);
9732
9733         return stream;
9734 }
9735
9736 static void
9737 __mmplayer_zerocopy_set_stride_elevation_bo(mmplayer_video_decoded_data_info_t *stream, GstMemory *mem)
9738 {
9739         unsigned int pitch = 0;
9740         unsigned int size = 0;
9741         int index = 0;
9742         tbm_surface_h surface = gst_tizen_memory_get_surface(mem);
9743         tbm_bo bo = NULL;
9744
9745         for (index = 0; index < gst_tizen_memory_get_num_bos(mem); index++) {
9746                 bo = gst_tizen_memory_get_bos(mem, index);
9747                 if (bo)
9748                         stream->bo[index] = tbm_bo_ref(bo);
9749                 else
9750                         LOGE("failed to get bo for index %d", index);
9751         }
9752
9753         for (index = 0; index < stream->plane_num; index++) {
9754                 tbm_surface_internal_get_plane_data(surface, index, &size, NULL, &pitch);
9755                 stream->stride[index] = pitch;
9756                 if (pitch)
9757                         stream->elevation[index] = size / pitch;
9758                 else
9759                         stream->elevation[index] = stream->height;
9760         }
9761 }
9762
9763 static gboolean
9764 __mmplayer_swcodec_set_stride_elevation(mmplayer_video_decoded_data_info_t *stream)
9765 {
9766         if (stream->format == MM_PIXEL_FORMAT_I420) {
9767                 int ret = TBM_SURFACE_ERROR_NONE;
9768                 tbm_surface_h surface;
9769                 tbm_surface_info_s info;
9770
9771                 surface = tbm_surface_create(stream->width, stream->height, TBM_FORMAT_YUV420);
9772
9773                 ret = tbm_surface_get_info(surface, &info);
9774                 if (ret != TBM_SURFACE_ERROR_NONE) {
9775                         tbm_surface_destroy(surface);
9776                         return FALSE;
9777                 }
9778
9779                 tbm_surface_destroy(surface);
9780                 stream->stride[0] = info.planes[0].stride;
9781                 stream->elevation[0] = info.planes[0].size / info.planes[0].stride;
9782                 stream->stride[1] = info.planes[1].stride;
9783                 stream->elevation[1] = info.planes[1].size / info.planes[1].stride;
9784                 stream->stride[2] = info.planes[2].stride;
9785                 stream->elevation[2] = info.planes[2].size / info.planes[2].stride;
9786                 stream->bo_size = info.planes[0].size + info.planes[1].size + info.planes[2].size;
9787         } else if (stream->format == MM_PIXEL_FORMAT_RGBA) {
9788                 stream->stride[0] = stream->width * 4;
9789                 stream->elevation[0] = stream->height;
9790                 stream->bo_size = stream->stride[0] * stream->height;
9791         } else {
9792                 LOGE("Not support format %d", stream->format);
9793                 return FALSE;
9794         }
9795
9796         return TRUE;
9797 }
9798
9799 static gboolean
9800 __mmplayer_swcodec_set_bo(mmplayer_t *player, mmplayer_video_decoded_data_info_t *stream, GstMemory *mem)
9801 {
9802         tbm_bo_handle thandle;
9803         gboolean is_mapped;
9804         int src_stride[MM_PLAYER_IMGB_MPLANE_MAX] = { 0, };
9805         int src_offset[MM_PLAYER_IMGB_MPLANE_MAX] = { 0, };
9806         int dest_offset[MM_PLAYER_IMGB_MPLANE_MAX] = { 0, };
9807         int i = 0;
9808         int j = 0;
9809         int k = 0;
9810         unsigned char *src = NULL;
9811         unsigned char *dest = NULL;
9812         GstMapInfo mapinfo = GST_MAP_INFO_INIT;
9813
9814         is_mapped = gst_memory_map(mem, &mapinfo, GST_MAP_READWRITE);
9815         if (!is_mapped) {
9816                 LOGE("fail to gst_memory_map");
9817                 return FALSE;
9818         }
9819
9820         if (!mapinfo.data) {
9821                 LOGE("data pointer is wrong");
9822                 goto ERROR;
9823         }
9824
9825         stream->bo[0] = __mmplayer_video_stream_get_bo(player, stream->bo_size);
9826         if (!stream->bo[0]) {
9827                 LOGE("Fail to tbm_bo_alloc!!");
9828                 goto ERROR;
9829         }
9830
9831         thandle = tbm_bo_map(stream->bo[0], TBM_DEVICE_CPU, TBM_OPTION_WRITE);
9832         if (!thandle.ptr) {
9833                 LOGE("thandle pointer is wrong");
9834                 goto ERROR;
9835         }
9836
9837         if (stream->format == MM_PIXEL_FORMAT_I420) {
9838                 src_stride[0] = GST_ROUND_UP_4(stream->width);
9839                 src_stride[1] = src_stride[2] = GST_ROUND_UP_4(stream->width >> 1);
9840                 src_offset[1] = src_stride[0] * GST_ROUND_UP_2(stream->height);
9841                 src_offset[2] = src_offset[1] + (src_stride[1] * (GST_ROUND_UP_2(stream->height) >> 1));
9842
9843                 dest_offset[0] = 0;
9844                 dest_offset[1] = stream->stride[0] * stream->elevation[0];
9845                 dest_offset[2] = dest_offset[1] + stream->stride[1] * stream->elevation[1];
9846
9847                 for (i = 0; i < 3; i++) {
9848                         src = mapinfo.data + src_offset[i];
9849                         dest = thandle.ptr + dest_offset[i];
9850
9851                         if (i > 0)
9852                                 k = 1;
9853
9854                         for (j = 0; j < stream->height >> k; j++) {
9855                                 memcpy(dest, src, stream->width>>k);
9856                                 src += src_stride[i];
9857                                 dest += stream->stride[i];
9858                         }
9859                 }
9860         } else if (stream->format == MM_PIXEL_FORMAT_RGBA) {
9861                 memcpy(thandle.ptr, mapinfo.data, stream->bo_size);
9862         } else {
9863                 LOGE("Not support format %d", stream->format);
9864                 goto ERROR;
9865         }
9866
9867         tbm_bo_unmap(stream->bo[0]);
9868         gst_memory_unmap(mem, &mapinfo);
9869
9870         return TRUE;
9871
9872 ERROR:
9873         if (stream->bo[0])
9874                 tbm_bo_unmap(stream->bo[0]);
9875
9876         if (is_mapped)
9877                 gst_memory_unmap(mem, &mapinfo);
9878
9879         return FALSE;
9880 }
9881
9882 static void
9883 __mmplayer_set_pause_state(mmplayer_t *player)
9884 {
9885         if (player->sent_bos)
9886                 return;
9887
9888         /* rtsp case, get content attrs by GstMessage */
9889         if (MMPLAYER_IS_RTSP_STREAMING(player))
9890                 return;
9891
9892         /* it's first time to update all content attrs. */
9893         _mmplayer_update_content_attrs(player, ATTR_ALL);
9894 }
9895
9896 static void
9897 __mmplayer_set_playing_state(mmplayer_t *player)
9898 {
9899         gchar *audio_codec = NULL;
9900
9901         if (player->resumed_by_rewind && player->playback_rate < 0.0) {
9902                 /* initialize because auto resume is done well. */
9903                 player->resumed_by_rewind = FALSE;
9904                 player->playback_rate = 1.0;
9905         }
9906
9907         if (player->sent_bos)
9908                 return;
9909
9910         /* try to get content metadata */
9911
9912         /* NOTE : giving ATTR_MISSING_ONLY may have dependency with
9913          * c-api since c-api doesn't use _start() anymore. It may not work properly with
9914          * legacy mmfw-player api
9915          */
9916         _mmplayer_update_content_attrs(player, ATTR_MISSING_ONLY);
9917
9918         if ((player->cmd == MMPLAYER_COMMAND_START)
9919                 || (player->cmd == MMPLAYER_COMMAND_RESUME)) {
9920                 __mmplayer_handle_missed_plugin(player);
9921         }
9922
9923         /* check audio codec field is set or not
9924          * we can get it from typefinder or codec's caps.
9925          */
9926         mm_attrs_get_string_by_name(player->attrs, "content_audio_codec", &audio_codec);
9927
9928         /* The codec format can't be sent for audio only case like amr, mid etc.
9929          * Because, parser don't make related TAG.
9930          * So, if it's not set yet, fill it with found data.
9931          */
9932         if (!audio_codec) {
9933                 if (g_strrstr(player->type_caps_str, "audio/midi"))
9934                         audio_codec = "MIDI";
9935                 else if (g_strrstr(player->type_caps_str, "audio/x-amr"))
9936                         audio_codec = "AMR";
9937                 else if (g_strrstr(player->type_caps_str, "audio/mpeg")
9938                                 && !g_strrstr(player->type_caps_str, "mpegversion=(int)1"))
9939                         audio_codec = "AAC";
9940                 else
9941                         audio_codec = "unknown";
9942
9943                 if (mm_player_set_attribute((MMHandleType)player, NULL,
9944                                 "content_audio_codec", audio_codec, strlen(audio_codec), NULL) != MM_ERROR_NONE)
9945                         LOGE("failed to set attribute");
9946
9947                 LOGD("set audio codec type with caps");
9948         }
9949
9950         return;
9951 }