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