Duration doesn't change even if it is updated on EOS.
[framework/web/webkit-efl.git] / Source / WebCore / platform / graphics / gstreamer / MediaPlayerPrivateGStreamer.cpp
1 /*
2  * Copyright (C) 2007, 2009 Apple Inc.  All rights reserved.
3  * Copyright (C) 2007 Collabora Ltd.  All rights reserved.
4  * Copyright (C) 2007 Alp Toker <alp@atoker.com>
5  * Copyright (C) 2009 Gustavo Noronha Silva <gns@gnome.org>
6  * Copyright (C) 2009, 2010 Igalia S.L
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public License
19  * aint with this library; see the file COPYING.LIB.  If not, write to
20  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21  * Boston, MA 02110-1301, USA.
22  */
23
24 #include "config.h"
25 #include "MediaPlayerPrivateGStreamer.h"
26
27 #if ENABLE(VIDEO) && USE(GSTREAMER)
28
29 #include "ColorSpace.h"
30 #if ENABLE(TIZEN_GSTREAMER_VIDEO)
31 #include "CookieJar.h"
32 #include <power.h>
33 #endif
34 #include "Document.h"
35 #include "Frame.h"
36 #include "FrameView.h"
37 #include "GStreamerGWorld.h"
38 #include "GStreamerUtilities.h"
39 #include "GStreamerVersioning.h"
40 #include "GraphicsContext.h"
41 #include "GraphicsTypes.h"
42 #if ENABLE(TIZEN_MEDIA_STREAM)
43 #include "HTMLMediaElement.h"
44 #endif
45 #include "ImageGStreamer.h"
46 #include "ImageOrientation.h"
47 #include "IntRect.h"
48 #include "KURL.h"
49 #include "MIMETypeRegistry.h"
50 #include "MediaPlayer.h"
51 #include "NotImplemented.h"
52 #include "SecurityOrigin.h"
53 #include "TimeRanges.h"
54 #include "VideoSinkGStreamer.h"
55 #include "WebKitWebSourceGStreamer.h"
56 #include <gst/gst.h>
57 #include <gst/video/video.h>
58 #include <limits>
59 #include <math.h>
60 #include <wtf/gobject/GOwnPtr.h>
61 #include <wtf/text/CString.h>
62
63 #ifdef GST_API_VERSION_1
64 #include <gst/audio/streamvolume.h>
65 #else
66 #include <gst/interfaces/streamvolume.h>
67 #endif
68
69 #if ENABLE(TIZEN_ACCELERATED_COMPOSITING) && USE(TIZEN_TEXTURE_MAPPER)
70 #include "Page.h"
71 #include "Settings.h"
72 #include "VideoLayerTizen.h"
73 #endif // ENABLE(TIZEN_ACCELERATED_COMPOSITING) && USE(TIZEN_TEXTURE_MAPPER)
74
75 #if ENABLE(TIZEN_MEDIA_STREAM) || ENABLE(TIZEN_FILE_SYSTEM)
76 #include "URIUtils.h"
77 #endif // ENABLE(TIZEN_MEDIA_STREAM) || ENABLE(TIZEN_FILE_SYSTEM)
78
79 #if ENABLE(TIZEN_MEDIA_STREAM)
80 #include "WebKitCameraSourceGStreamerTizen.h"
81 #endif // ENABLE(TIZEN_MEDIA_STREAM)
82
83 // GstPlayFlags flags from playbin2. It is the policy of GStreamer to
84 // not publicly expose element-specific enums. That's why this
85 // GstPlayFlags enum has been copied here.
86 typedef enum {
87     GST_PLAY_FLAG_VIDEO         = 0x00000001,
88     GST_PLAY_FLAG_AUDIO         = 0x00000002,
89     GST_PLAY_FLAG_TEXT          = 0x00000004,
90     GST_PLAY_FLAG_VIS           = 0x00000008,
91     GST_PLAY_FLAG_SOFT_VOLUME   = 0x00000010,
92     GST_PLAY_FLAG_NATIVE_AUDIO  = 0x00000020,
93     GST_PLAY_FLAG_NATIVE_VIDEO  = 0x00000040,
94     GST_PLAY_FLAG_DOWNLOAD      = 0x00000080,
95     GST_PLAY_FLAG_BUFFERING     = 0x000000100
96 } GstPlayFlags;
97
98 #ifdef GST_API_VERSION_1
99 static const char* gPlaybinName = "playbin";
100 #else
101 static const char* gPlaybinName = "playbin2";
102 #endif
103
104 GST_DEBUG_CATEGORY_STATIC(webkit_media_player_debug);
105 #define GST_CAT_DEFAULT webkit_media_player_debug
106
107 using namespace std;
108
109 namespace WebCore {
110
111 static int greatestCommonDivisor(int a, int b)
112 {
113     while (b) {
114         int temp = a;
115         a = b;
116         b = temp % b;
117     }
118
119     return ABS(a);
120 }
121
122 static gboolean mediaPlayerPrivateMessageCallback(GstBus*, GstMessage* message, MediaPlayerPrivateGStreamer* player)
123 {
124     return player->handleMessage(message);
125 }
126
127 static void mediaPlayerPrivateSourceChangedCallback(GObject*, GParamSpec*, MediaPlayerPrivateGStreamer* player)
128 {
129     player->sourceChanged();
130 }
131
132 static void mediaPlayerPrivateVolumeChangedCallback(GObject*, GParamSpec*, MediaPlayerPrivateGStreamer* player)
133 {
134     // This is called when playbin receives the notify::volume signal.
135     player->volumeChanged();
136 }
137
138 static gboolean mediaPlayerPrivateVolumeChangeTimeoutCallback(MediaPlayerPrivateGStreamer* player)
139 {
140     // This is the callback of the timeout source created in ::volumeChanged.
141     player->notifyPlayerOfVolumeChange();
142     return FALSE;
143 }
144
145 static void mediaPlayerPrivateMuteChangedCallback(GObject*, GParamSpec*, MediaPlayerPrivateGStreamer* player)
146 {
147     // This is called when playbin receives the notify::mute signal.
148     player->muteChanged();
149 }
150
151 static gboolean mediaPlayerPrivateMuteChangeTimeoutCallback(MediaPlayerPrivateGStreamer* player)
152 {
153     // This is the callback of the timeout source created in ::muteChanged.
154     player->notifyPlayerOfMute();
155     return FALSE;
156 }
157
158 static void mediaPlayerPrivateVideoSinkCapsChangedCallback(GObject*, GParamSpec*, MediaPlayerPrivateGStreamer* player)
159 {
160     player->videoChanged();
161 }
162
163 static void mediaPlayerPrivateVideoChangedCallback(GObject*, MediaPlayerPrivateGStreamer* player)
164 {
165     player->videoChanged();
166 }
167
168 static void mediaPlayerPrivateAudioChangedCallback(GObject*, MediaPlayerPrivateGStreamer* player)
169 {
170     player->audioChanged();
171 }
172
173 #if ENABLE(TIZEN_GSTREAMER_VIDEO)
174 static void mediaPlayerPrivateSourceSetupCallback(GObject*, GstElement* source, MediaPlayerPrivateGStreamer* player)
175 {
176     HTMLMediaElement* element = static_cast<HTMLMediaElement*>(player->player()->mediaPlayerClient());
177
178     // Grab the current document
179     Document* document = element->document();
180     if (!document)
181         document = element->ownerDocument();
182
183     // Grab the frame and network manager
184     Frame* frame = document ? document->frame() : 0;
185     FrameLoader* frameLoader = frame ? frame->loader() : 0;
186
187     String cookie = cookies(document, player->url());
188     if (!cookie.length())
189         cookie = cookieRequestHeaderFieldValue(document, player->url());
190
191     GstStructure* headers = gst_structure_new("extra-headers", "Cookie", G_TYPE_STRING, cookie.utf8().data(), NULL);
192     g_object_set(source, "extra-headers", headers, NULL);
193     gst_structure_free(headers);
194
195 #if ENABLE(TIZEN_WEBKIT2_PROXY)
196     player->setProxy(source);
197 #endif
198
199     if (frameLoader)
200         g_object_set(source, "user-agent", frameLoader->userAgent(player->url()).utf8().data(), NULL);
201 }
202 #endif
203
204 static gboolean mediaPlayerPrivateAudioChangeTimeoutCallback(MediaPlayerPrivateGStreamer* player)
205 {
206     // This is the callback of the timeout source created in ::audioChanged.
207     player->notifyPlayerOfAudio();
208     return FALSE;
209 }
210
211 static gboolean mediaPlayerPrivateVideoChangeTimeoutCallback(MediaPlayerPrivateGStreamer* player)
212 {
213     // This is the callback of the timeout source created in ::videoChanged.
214     player->notifyPlayerOfVideo();
215     return FALSE;
216 }
217
218 #if ENABLE(TIZEN_GSTREAMER_VIDEO) && (!ENABLE(TIZEN_ACCELERATED_COMPOSITING) || !USE(TIZEN_TEXTURE_MAPPER) || !ENABLE(TIZEN_WEBKIT2_TILED_AC_SHARED_PLATFORM_SURFACE) || CPU(X86) || CPU(X86_64))
219 static void mediaPlayerPrivateRepaintCallback(WebKitVideoSink*, GstBuffer *buffer, MediaPlayerPrivateGStreamer* playerPrivate)
220 {
221     playerPrivate->triggerRepaint(buffer);
222 }
223 #endif
224
225 #if ENABLE(TIZEN_ACCELERATED_COMPOSITING) && USE(TIZEN_TEXTURE_MAPPER) && ENABLE(TIZEN_WEBKIT2_TILED_AC_SHARED_PLATFORM_SURFACE)
226 static GstBusSyncReply mediaPlayerPrivateSyncHandler(GstBus* bus, GstMessage* message, MediaPlayerPrivateGStreamer* playerPrivate)
227 {
228     // ignore anything but 'prepare-xid' message
229     if (GST_MESSAGE_TYPE(message) != GST_MESSAGE_ELEMENT)
230         return GST_BUS_PASS;
231     if (!gst_structure_has_name (message->structure, "prepare-xid"))
232         return GST_BUS_PASS;
233
234     playerPrivate->xWindowIdPrepared(message);
235     return GST_BUS_DROP;
236 }
237 #endif
238
239 #if ENABLE(TIZEN_GSTREAMER_AUDIO)
240 static ASM_cb_result_t MediaPlayerAudioSessionEventSourcePause(ASM_event_sources_t eventSource, void* callbackData)
241 {
242     MediaPlayer* player = static_cast<MediaPlayer*>(callbackData);
243     HTMLMediaElement* element = static_cast<HTMLMediaElement*>(player->mediaPlayerClient());
244
245     if (!player)
246         return ASM_CB_RES_IGNORE;
247
248     switch (eventSource) {
249     case ASM_EVENT_SOURCE_CALL_START:
250     case ASM_EVENT_SOURCE_ALARM_START:
251     case ASM_EVENT_SOURCE_MEDIA:
252     case ASM_EVENT_SOURCE_EMERGENCY_START:
253     case ASM_EVENT_SOURCE_OTHER_PLAYER_APP:
254     case ASM_EVENT_SOURCE_RESOURCE_CONFLICT:
255     case ASM_EVENT_SOURCE_EARJACK_UNPLUG:
256         if (!player->url().string().contains("camera://")) {
257             element->pause();
258             return ASM_CB_RES_PAUSE;
259         }
260     default:
261         return ASM_CB_RES_NONE;
262     }
263 }
264
265 static ASM_cb_result_t MediaPlayerAudioSessionEventSourcePlay(ASM_event_sources_t eventSource, void* callbackData)
266 {
267     MediaPlayer* player = static_cast<MediaPlayer*>(callbackData);
268     HTMLMediaElement* element = static_cast<HTMLMediaElement*>(player->mediaPlayerClient());
269
270     if (!player)
271         return ASM_CB_RES_IGNORE;
272
273     switch (eventSource) {
274     case ASM_EVENT_SOURCE_ALARM_END:
275         if (!element->isVideo() && !player->url().string().contains("camera://")) {
276             element->play();
277             return ASM_CB_RES_PLAYING;
278         }
279         return ASM_CB_RES_NONE;
280     default:
281         return ASM_CB_RES_NONE;
282     }
283 }
284
285 static ASM_cb_result_t mediaPlayerPrivateAudioSessionNotifyCallback(int, ASM_event_sources_t eventSource, ASM_sound_commands_t command, unsigned int, void* callbackData)
286 {
287     if (command == ASM_COMMAND_STOP || command == ASM_COMMAND_PAUSE)
288         return MediaPlayerAudioSessionEventSourcePause(eventSource, callbackData);
289     if (command == ASM_COMMAND_PLAY || command == ASM_COMMAND_RESUME)
290         return MediaPlayerAudioSessionEventSourcePlay(eventSource, callbackData);
291
292     return ASM_CB_RES_NONE;
293 }
294 #endif
295
296 PassOwnPtr<MediaPlayerPrivateInterface> MediaPlayerPrivateGStreamer::create(MediaPlayer* player)
297 {
298     return adoptPtr(new MediaPlayerPrivateGStreamer(player));
299 }
300
301 void MediaPlayerPrivateGStreamer::registerMediaEngine(MediaEngineRegistrar registrar)
302 {
303     if (isAvailable())
304         registrar(create, getSupportedTypes, supportsType, 0, 0, 0);
305 }
306
307 bool initializeGStreamerAndRegisterWebKitElements()
308 {
309     if (!initializeGStreamer())
310         return false;
311
312     GRefPtr<GstElementFactory> srcFactory = gst_element_factory_find("webkitwebsrc");
313     if (!srcFactory)
314 #if ENABLE(TIZEN_MEDIA_STREAM)
315         return gst_element_register(0, "webkitcamerasrc", GST_RANK_PRIMARY + 200, WEBKIT_TYPE_CAMERA_SRC);
316 #else
317         GST_DEBUG_CATEGORY_INIT(webkit_media_player_debug, "webkitmediaplayer", 0, "WebKit media player");
318         return gst_element_register(0, "webkitwebsrc", GST_RANK_PRIMARY + 100, WEBKIT_TYPE_WEB_SRC);
319 #endif // ENABLE(TIZEN_MEDIA_STREAM)
320
321     return true;
322 }
323
324 bool MediaPlayerPrivateGStreamer::isAvailable()
325 {
326     if (!initializeGStreamerAndRegisterWebKitElements())
327         return false;
328
329     GRefPtr<GstElementFactory> factory = gst_element_factory_find(gPlaybinName);
330     return factory;
331 }
332
333 MediaPlayerPrivateGStreamer::MediaPlayerPrivateGStreamer(MediaPlayer* player)
334     : m_player(player)
335     , m_webkitVideoSink(0)
336 #if ENABLE(TIZEN_GSTREAMER_VIDEO)
337     , m_videoSinkBin(0)
338 #endif
339     , m_fpsSink(0)
340     , m_source(0)
341     , m_seekTime(0)
342     , m_changingRate(false)
343     , m_endTime(numeric_limits<float>::infinity())
344     , m_isEndReached(false)
345     , m_networkState(MediaPlayer::Empty)
346     , m_readyState(MediaPlayer::HaveNothing)
347     , m_isStreaming(false)
348     , m_size(IntSize())
349     , m_buffer(0)
350     , m_mediaLocations(0)
351     , m_mediaLocationCurrentIndex(0)
352     , m_resetPipeline(false)
353     , m_paused(true)
354     , m_seeking(false)
355     , m_buffering(false)
356     , m_playbackRate(1)
357     , m_errorOccured(false)
358     , m_mediaDuration(0)
359     , m_startedBuffering(false)
360     , m_fillTimer(this, &MediaPlayerPrivateGStreamer::fillTimerFired)
361     , m_maxTimeLoaded(0)
362     , m_bufferingPercentage(0)
363     , m_preload(MediaPlayer::Auto)
364     , m_delayingLoad(false)
365     , m_mediaDurationKnown(true)
366     , m_maxTimeLoadedAtLastDidLoadingProgress(0)
367     , m_volumeTimerHandler(0)
368     , m_muteTimerHandler(0)
369     , m_hasVideo(false)
370     , m_hasAudio(false)
371     , m_audioTimerHandler(0)
372     , m_videoTimerHandler(0)
373     , m_webkitAudioSink(0)
374     , m_totalBytes(-1)
375 #if ENABLE(TIZEN_ACCELERATED_COMPOSITING) && USE(TIZEN_TEXTURE_MAPPER)
376     , m_videoLayer(VideoLayerTizen::create(static_cast<HTMLMediaElement*>(player->mediaPlayerClient())))
377 #endif // ENABLE(TIZEN_ACCELERATED_COMPOSITING) && USE(TIZEN_TEXTURE_MAPPER)
378 #if ENABLE(TIZEN_GSTREAMER_AUDIO)
379     , m_audioSessionManager(AudioSessionManagerGStreamerTizen::createAudioSessionManager())
380 #endif
381     , m_originalPreloadWasAutoAndWasOverridden(false)
382     , m_preservesPitch(false)
383 #if ENABLE(TIZEN_GSTREAMER_VIDEO)
384     , m_suspendTime(0)
385 #endif
386 {
387 }
388
389 MediaPlayerPrivateGStreamer::~MediaPlayerPrivateGStreamer()
390 {
391     if (m_fillTimer.isActive())
392         m_fillTimer.stop();
393
394     if (m_buffer)
395         gst_buffer_unref(m_buffer);
396     m_buffer = 0;
397
398     if (m_mediaLocations) {
399         gst_structure_free(m_mediaLocations);
400         m_mediaLocations = 0;
401     }
402
403 #ifndef GST_API_VERSION_1
404     if (m_videoSinkBin) {
405         gst_object_unref(m_videoSinkBin);
406         m_videoSinkBin = 0;
407     }
408 #endif
409
410     if (m_playBin) {
411         GRefPtr<GstBus> bus = webkitGstPipelineGetBus(GST_PIPELINE(m_playBin.get()));
412         ASSERT(bus);
413         g_signal_handlers_disconnect_by_func(bus.get(), reinterpret_cast<gpointer>(mediaPlayerPrivateMessageCallback), this);
414         gst_bus_remove_signal_watch(bus.get());
415
416         gst_element_set_state(m_playBin.get(), GST_STATE_NULL);
417         m_playBin = 0;
418     }
419
420     m_player = 0;
421
422 #if ENABLE(TIZEN_GSTREAMER_AUDIO)
423     if (m_audioSessionManager)
424         m_audioSessionManager->setSoundState(ASM_STATE_STOP);
425 #endif
426
427     if (m_muteTimerHandler)
428         g_source_remove(m_muteTimerHandler);
429
430     if (m_volumeTimerHandler)
431         g_source_remove(m_volumeTimerHandler);
432
433     if (m_videoTimerHandler)
434         g_source_remove(m_videoTimerHandler);
435
436     if (m_audioTimerHandler)
437         g_source_remove(m_audioTimerHandler);
438 }
439
440 void MediaPlayerPrivateGStreamer::load(const String& url)
441 {
442     if (!initializeGStreamerAndRegisterWebKitElements())
443         return;
444
445 #if ENABLE(TIZEN_MEDIA_STREAM) || ENABLE(TIZEN_FILE_SYSTEM)
446     crackURI(url);
447 #endif // ENABLE(TIZEN_MEDIA_STREAM) || ENABLE(TIZEN_FILE_SYSTEM)
448     KURL kurl(KURL(), url);
449     String cleanUrl(url);
450
451     // Clean out everything after file:// url path.
452     if (kurl.isLocalFile())
453         cleanUrl = cleanUrl.substring(0, kurl.pathEnd());
454
455     if (!m_playBin) {
456         createGSTPlayBin();
457         setDownloadBuffering();
458     }
459
460     ASSERT(m_playBin);
461
462     m_url = KURL(KURL(), cleanUrl);
463     g_object_set(m_playBin.get(), "uri", cleanUrl.utf8().data(), NULL);
464
465     LOG_MEDIA_MESSAGE("Load %s", cleanUrl.utf8().data());
466
467 #if ENABLE(TIZEN_MEDIA_STREAM)
468     // Workaround modification for getUserMedia.
469     // When 'playing' event is fired videoWidth and videoHeight are not available.
470     // Set m_videoSize as frame size which comes from webkitCameraSource.
471     // This size is fixed.
472     if (isLocalMediaStream())
473         m_videoSize = IntSize(480, 640);
474 #endif
475
476     if (m_preload == MediaPlayer::None) {
477         LOG_MEDIA_MESSAGE("Delaying load.");
478         m_delayingLoad = true;
479     }
480
481     // Reset network and ready states. Those will be set properly once
482     // the pipeline pre-rolled.
483 #if ENABLE(TIZEN_GSTREAMER_VIDEO)
484     if (!m_delayingLoad)
485         m_networkState = MediaPlayer::Loading;
486     else
487         m_networkState = MediaPlayer::Idle;
488 #else
489     m_networkState = MediaPlayer::Loading;
490 #endif
491     m_player->networkStateChanged();
492     m_readyState = MediaPlayer::HaveNothing;
493     m_player->readyStateChanged();
494     m_volumeAndMuteInitialized = false;
495
496     // GStreamer needs to have the pipeline set to a paused state to
497     // start providing anything useful.
498 #if ENABLE(TIZEN_GSTREAMER_VIDEO)
499     if (!m_delayingLoad)
500 #endif
501     gst_element_set_state(m_playBin.get(), GST_STATE_PAUSED);
502
503 #if ENABLE(TIZEN_GSTREAMER_AUDIO)
504     if (m_audioSessionManager && !m_delayingLoad)
505         m_audioSessionManager->setSoundState(ASM_STATE_PAUSE);
506 #endif
507
508     if (!m_delayingLoad)
509         commitLoad();
510 }
511
512 void MediaPlayerPrivateGStreamer::commitLoad()
513 {
514     ASSERT(!m_delayingLoad);
515     LOG_MEDIA_MESSAGE("Committing load.");
516     updateStates();
517 }
518
519 float MediaPlayerPrivateGStreamer::playbackPosition() const
520 {
521     if (m_isEndReached) {
522         // Position queries on a null pipeline return 0. If we're at
523         // the end of the stream the pipeline is null but we want to
524         // report either the seek time or the duration because this is
525         // what the Media element spec expects us to do.
526         if (m_seeking)
527             return m_seekTime;
528         if (m_mediaDuration)
529             return m_mediaDuration;
530     }
531
532     float ret = 0.0f;
533
534     GstQuery* query = gst_query_new_position(GST_FORMAT_TIME);
535     if (!gst_element_query(m_playBin.get(), query)) {
536         LOG_MEDIA_MESSAGE("Position query failed...");
537         gst_query_unref(query);
538         return ret;
539     }
540
541     gint64 position;
542     gst_query_parse_position(query, 0, &position);
543
544     // Position is available only if the pipeline is not in GST_STATE_NULL or
545     // GST_STATE_READY state.
546     if (position != static_cast<gint64>(GST_CLOCK_TIME_NONE))
547         ret = static_cast<double>(position) / GST_SECOND;
548
549     LOG_MEDIA_MESSAGE("Position %" GST_TIME_FORMAT, GST_TIME_ARGS(position));
550
551     gst_query_unref(query);
552
553     return ret;
554 }
555
556 bool MediaPlayerPrivateGStreamer::changePipelineState(GstState newState)
557 {
558     ASSERT(newState == GST_STATE_PLAYING || newState == GST_STATE_PAUSED);
559
560     GstState currentState;
561     GstState pending;
562
563     gst_element_get_state(m_playBin.get(), &currentState, &pending, 0);
564     LOG_MEDIA_MESSAGE("Current state: %s, pending: %s", gst_element_state_get_name(currentState), gst_element_state_get_name(pending));
565 #if ENABLE(TIZEN_DLOG_SUPPORT)
566     TIZEN_LOGI("Current state: %s, New state: %s", gst_element_state_get_name(currentState), gst_element_state_get_name(newState));
567 #endif
568     if (currentState == newState || pending == newState)
569         return true;
570
571     GstStateChangeReturn setStateResult = gst_element_set_state(m_playBin.get(), newState);
572     GstState pausedOrPlaying = newState == GST_STATE_PLAYING ? GST_STATE_PAUSED : GST_STATE_PLAYING;
573     if (currentState != pausedOrPlaying && setStateResult == GST_STATE_CHANGE_FAILURE) {
574         loadingFailed(MediaPlayer::Empty);
575         return false;
576     }
577     return true;
578 }
579
580 void MediaPlayerPrivateGStreamer::prepareToPlay()
581 {
582     m_isEndReached = false;
583     m_seeking = false;
584
585     if (m_delayingLoad) {
586         m_delayingLoad = false;
587         commitLoad();
588     }
589 }
590
591 void MediaPlayerPrivateGStreamer::play()
592 {
593 #if ENABLE(TIZEN_GSTREAMER_AUDIO)
594     if (!m_audioSessionManager->setSoundState(ASM_STATE_PLAYING) && !isLocalMediaStream())
595         return;
596 #endif
597     if (changePipelineState(GST_STATE_PLAYING)) {
598         m_isEndReached = false;
599         LOG_MEDIA_MESSAGE("Play");
600     }
601 }
602
603 void MediaPlayerPrivateGStreamer::pause()
604 {
605 #if ENABLE(TIZEN_GSTREAMER_AUDIO)
606     if (!m_audioSessionManager->setSoundState(ASM_STATE_PAUSE) && !isLocalMediaStream())
607         return;
608 #endif
609     if (m_isEndReached)
610         return;
611
612     if (changePipelineState(GST_STATE_PAUSED))
613         LOG_MEDIA_MESSAGE("Pause");
614 }
615
616 float MediaPlayerPrivateGStreamer::duration() const
617 {
618     if (!m_playBin)
619         return 0.0f;
620
621     if (m_errorOccured)
622         return 0.0f;
623
624     // Media duration query failed already, don't attempt new useless queries.
625     if (!m_mediaDurationKnown)
626         return numeric_limits<float>::infinity();
627
628     if (m_mediaDuration)
629         return m_mediaDuration;
630
631     GstFormat timeFormat = GST_FORMAT_TIME;
632     gint64 timeLength = 0;
633
634 #ifdef GST_API_VERSION_1
635     bool failure = !gst_element_query_duration(m_playBin.get(), timeFormat, &timeLength) || static_cast<guint64>(timeLength) == GST_CLOCK_TIME_NONE;
636 #else
637     bool failure = !gst_element_query_duration(m_playBin.get(), &timeFormat, &timeLength) || timeFormat != GST_FORMAT_TIME || static_cast<guint64>(timeLength) == GST_CLOCK_TIME_NONE;
638 #endif
639     if (failure) {
640         LOG_MEDIA_MESSAGE("Time duration query failed for %s", m_url.string().utf8().data());
641         return numeric_limits<float>::infinity();
642     }
643
644     LOG_MEDIA_MESSAGE("Duration: %" GST_TIME_FORMAT, GST_TIME_ARGS(timeLength));
645
646     return static_cast<double>(timeLength) / GST_SECOND;
647     // FIXME: handle 3.14.9.5 properly
648 }
649
650 float MediaPlayerPrivateGStreamer::currentTime() const
651 {
652     if (!m_playBin)
653         return 0.0f;
654
655     if (m_errorOccured)
656         return 0.0f;
657
658     if (m_seeking)
659         return m_seekTime;
660
661     // Workaround for
662     // https://bugzilla.gnome.org/show_bug.cgi?id=639941 In GStreamer
663     // 0.10.35 basesink reports wrong duration in case of EOS and
664     // negative playback rate. There's no upstream accepted patch for
665     // this bug yet, hence this temporary workaround.
666     if (m_isEndReached && m_playbackRate < 0)
667         return 0.0f;
668
669     return playbackPosition();
670
671 }
672
673 void MediaPlayerPrivateGStreamer::seek(float time)
674 {
675     if (!m_playBin)
676         return;
677
678     if (m_errorOccured)
679         return;
680
681     LOG_MEDIA_MESSAGE("Seek attempt to %f secs", time);
682 #if ENABLE(TIZEN_DLOG_SUPPORT)
683     TIZEN_LOGI("Seek attempt to %f secs", time);
684 #endif
685
686     // Avoid useless seeking.
687     if (time == currentTime())
688         return;
689
690     // Extract the integer part of the time (seconds) and the
691     // fractional part (microseconds). Attempt to round the
692     // microseconds so no floating point precision is lost and we can
693     // perform an accurate seek.
694     float seconds;
695     float microSeconds = modf(time, &seconds) * 1000000;
696     GTimeVal timeValue;
697     timeValue.tv_sec = static_cast<glong>(seconds);
698     timeValue.tv_usec = static_cast<glong>(roundf(microSeconds / 10000) * 10000);
699
700     GstClockTime clockTime = GST_TIMEVAL_TO_TIME(timeValue);
701     LOG_MEDIA_MESSAGE("Seek: %" GST_TIME_FORMAT, GST_TIME_ARGS(clockTime));
702
703     if (!gst_element_seek(m_playBin.get(), m_player->rate(),
704             GST_FORMAT_TIME,
705             (GstSeekFlags)(GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_ACCURATE),
706             GST_SEEK_TYPE_SET, clockTime,
707             GST_SEEK_TYPE_NONE, GST_CLOCK_TIME_NONE)) {
708         LOG_MEDIA_MESSAGE("Seek to %f failed", time);
709 #if ENABLE(TIZEN_DLOG_SUPPORT)
710         TIZEN_LOGE("Seek to %f failed", time);
711 #endif
712     } else {
713         m_seeking = true;
714         m_seekTime = time;
715 #if ENABLE(TIZEN_GSTREAMER_VIDEO)
716         if (m_seekTime != m_mediaDuration)
717             m_isEndReached = false;
718 #endif
719     }
720 }
721
722 bool MediaPlayerPrivateGStreamer::paused() const
723 {
724     if (m_isEndReached) {
725         LOG_MEDIA_MESSAGE("Ignoring pause at EOS");
726         return true;
727     }
728
729     GstState state;
730     gst_element_get_state(m_playBin.get(), &state, 0, 0);
731     return state == GST_STATE_PAUSED;
732 }
733
734 bool MediaPlayerPrivateGStreamer::seeking() const
735 {
736     return m_seeking;
737 }
738
739 // Returns the size of the video
740 IntSize MediaPlayerPrivateGStreamer::naturalSize() const
741 {
742     if (!hasVideo())
743         return IntSize();
744
745     if (!m_videoSize.isEmpty())
746         return m_videoSize;
747
748     GRefPtr<GstCaps> caps = webkitGstGetPadCaps(m_videoSinkPad.get());
749     if (!caps)
750         return IntSize();
751
752
753     // TODO: handle possible clean aperture data. See
754     // https://bugzilla.gnome.org/show_bug.cgi?id=596571
755     // TODO: handle possible transformation matrix. See
756     // https://bugzilla.gnome.org/show_bug.cgi?id=596326
757
758     // Get the video PAR and original size, if this fails the
759     // video-sink has likely not yet negotiated its caps.
760     int pixelAspectRatioNumerator, pixelAspectRatioDenominator, stride;
761     IntSize originalSize;
762     GstVideoFormat format;
763     if (!getVideoSizeAndFormatFromCaps(caps.get(), originalSize, format, pixelAspectRatioNumerator, pixelAspectRatioDenominator, stride))
764         return IntSize();
765
766     LOG_MEDIA_MESSAGE("Original video size: %dx%d", originalSize.width(), originalSize.height());
767     LOG_MEDIA_MESSAGE("Pixel aspect ratio: %d/%d", pixelAspectRatioNumerator, pixelAspectRatioDenominator);
768
769     // Calculate DAR based on PAR and video size.
770     int displayWidth = originalSize.width() * pixelAspectRatioNumerator;
771     int displayHeight = originalSize.height() * pixelAspectRatioDenominator;
772
773     // Divide display width and height by their GCD to avoid possible overflows.
774     int displayAspectRatioGCD = greatestCommonDivisor(displayWidth, displayHeight);
775     displayWidth /= displayAspectRatioGCD;
776     displayHeight /= displayAspectRatioGCD;
777
778     // Apply DAR to original video size. This is the same behavior as in xvimagesink's setcaps function.
779     guint64 width = 0, height = 0;
780     if (!(originalSize.height() % displayHeight)) {
781         LOG_MEDIA_MESSAGE("Keeping video original height");
782         width = gst_util_uint64_scale_int(originalSize.height(), displayWidth, displayHeight);
783         height = static_cast<guint64>(originalSize.height());
784     } else if (!(originalSize.width() % displayWidth)) {
785         LOG_MEDIA_MESSAGE("Keeping video original width");
786         height = gst_util_uint64_scale_int(originalSize.width(), displayHeight, displayWidth);
787         width = static_cast<guint64>(originalSize.width());
788     } else {
789         LOG_MEDIA_MESSAGE("Approximating while keeping original video height");
790         width = gst_util_uint64_scale_int(originalSize.height(), displayWidth, displayHeight);
791         height = static_cast<guint64>(originalSize.height());
792     }
793
794     LOG_MEDIA_MESSAGE("Natural size: %" G_GUINT64_FORMAT "x%" G_GUINT64_FORMAT, width, height);
795 #if ENABLE(TIZEN_ACCELERATED_COMPOSITING) && USE(TIZEN_TEXTURE_MAPPER) && ENABLE(TIZEN_WEBKIT2_TILED_AC_SHARED_PLATFORM_SURFACE) && USE(ACCELERATED_VIDEO_VAAPI)
796     if (!m_displaySize.isEmpty())
797         m_videoSize = scaleHDVideoToDisplaySize(static_cast<int>(width), static_cast<int>(height), m_displaySize.width(), m_displaySize.height());
798     else
799 #endif
800         m_videoSize = IntSize(static_cast<int>(width), static_cast<int>(height));
801     return m_videoSize;
802 }
803
804 void MediaPlayerPrivateGStreamer::videoChanged()
805 {
806     if (m_videoTimerHandler)
807         g_source_remove(m_videoTimerHandler);
808     m_videoTimerHandler = g_timeout_add(0, reinterpret_cast<GSourceFunc>(mediaPlayerPrivateVideoChangeTimeoutCallback), this);
809 }
810
811 void MediaPlayerPrivateGStreamer::notifyPlayerOfVideo()
812 {
813     m_videoTimerHandler = 0;
814
815     gint videoTracks = 0;
816     if (m_playBin)
817         g_object_get(m_playBin.get(), "n-video", &videoTracks, NULL);
818
819     m_hasVideo = videoTracks > 0;
820
821 #if ENABLE(TIZEN_GSTREAMER_VIDEO)
822     int width, height;
823     if (gst_video_get_size(m_videoSinkPad.get(), &width, &height)) {
824         IntSize size(width, height);
825         if (m_videoSize != size) {
826             m_videoSize = IntSize();
827 #if ENABLE(TIZEN_ACCELERATED_COMPOSITING) && USE(TIZEN_TEXTURE_MAPPER) && ENABLE(TIZEN_WEBKIT2_TILED_AC_SHARED_PLATFORM_SURFACE)
828             m_videoLayer->setOverlay(naturalSize());
829 #endif
830         }
831     }
832
833     m_player->mediaPlayerClient()->mediaPlayerEngineUpdated(m_player);
834 #else // ENABLE(TIZEN_GSTREAMER_VIDEO)
835     m_videoSize = IntSize();
836
837     m_player->mediaPlayerClient()->mediaPlayerEngineUpdated(m_player);
838 #endif
839 }
840
841 void MediaPlayerPrivateGStreamer::audioChanged()
842 {
843     if (m_audioTimerHandler)
844         g_source_remove(m_audioTimerHandler);
845     m_audioTimerHandler = g_timeout_add(0, reinterpret_cast<GSourceFunc>(mediaPlayerPrivateAudioChangeTimeoutCallback), this);
846 }
847
848 void MediaPlayerPrivateGStreamer::notifyPlayerOfAudio()
849 {
850     m_audioTimerHandler = 0;
851
852     gint audioTracks = 0;
853     if (m_playBin)
854         g_object_get(m_playBin.get(), "n-audio", &audioTracks, NULL);
855     m_hasAudio = audioTracks > 0;
856     m_player->mediaPlayerClient()->mediaPlayerEngineUpdated(m_player);
857 }
858
859 void MediaPlayerPrivateGStreamer::setVolume(float volume)
860 {
861     if (!m_playBin)
862         return;
863
864     gst_stream_volume_set_volume(GST_STREAM_VOLUME(m_playBin.get()), GST_STREAM_VOLUME_FORMAT_CUBIC,
865                                  static_cast<double>(volume));
866 }
867
868 void MediaPlayerPrivateGStreamer::notifyPlayerOfVolumeChange()
869 {
870     m_volumeTimerHandler = 0;
871
872     if (!m_player || !m_playBin)
873         return;
874     double volume;
875     volume = gst_stream_volume_get_volume(GST_STREAM_VOLUME(m_playBin.get()), GST_STREAM_VOLUME_FORMAT_CUBIC);
876     // get_volume() can return values superior to 1.0 if the user
877     // applies software user gain via third party application (GNOME
878     // volume control for instance).
879     volume = CLAMP(volume, 0.0, 1.0);
880     m_player->volumeChanged(static_cast<float>(volume));
881 }
882
883 void MediaPlayerPrivateGStreamer::volumeChanged()
884 {
885     if (m_volumeTimerHandler)
886         g_source_remove(m_volumeTimerHandler);
887     m_volumeTimerHandler = g_timeout_add(0, reinterpret_cast<GSourceFunc>(mediaPlayerPrivateVolumeChangeTimeoutCallback), this);
888 }
889
890 void MediaPlayerPrivateGStreamer::setRate(float rate)
891 {
892     // Avoid useless playback rate update.
893     if (m_playbackRate == rate)
894         return;
895
896     GstState state;
897     GstState pending;
898
899     gst_element_get_state(m_playBin.get(), &state, &pending, 0);
900     if ((state != GST_STATE_PLAYING && state != GST_STATE_PAUSED)
901         || (pending == GST_STATE_PAUSED))
902         return;
903
904     if (isLiveStream())
905         return;
906
907     m_playbackRate = rate;
908     m_changingRate = true;
909
910     if (!rate) {
911         gst_element_set_state(m_playBin.get(), GST_STATE_PAUSED);
912 #if ENABLE(TIZEN_GSTREAMER_AUDIO)
913         if (m_audioSessionManager)
914             m_audioSessionManager->setSoundState(ASM_STATE_PAUSE);
915 #endif
916         return;
917     }
918
919     float currentPosition = static_cast<float>(playbackPosition() * GST_SECOND);
920     GstSeekFlags flags = (GstSeekFlags)(GST_SEEK_FLAG_FLUSH);
921     gint64 start, end;
922     bool mute = false;
923
924     LOG_MEDIA_MESSAGE("Set Rate to %f", rate);
925     if (rate > 0) {
926         // Mute the sound if the playback rate is too extreme and
927         // audio pitch is not adjusted.
928         mute = (!m_preservesPitch && (rate < 0.8 || rate > 2));
929         start = currentPosition;
930         end = GST_CLOCK_TIME_NONE;
931     } else {
932         start = 0;
933         mute = true;
934
935         // If we are at beginning of media, start from the end to
936         // avoid immediate EOS.
937         if (currentPosition <= 0)
938             end = static_cast<gint64>(duration() * GST_SECOND);
939         else
940             end = currentPosition;
941     }
942
943     LOG_MEDIA_MESSAGE("Need to mute audio: %d", (int) mute);
944
945     if (!gst_element_seek(m_playBin.get(), rate, GST_FORMAT_TIME, flags,
946                           GST_SEEK_TYPE_SET, start,
947                           GST_SEEK_TYPE_SET, end))
948         LOG_MEDIA_MESSAGE("Set rate to %f failed", rate);
949     else
950         g_object_set(m_playBin.get(), "mute", mute, NULL);
951 }
952
953 void MediaPlayerPrivateGStreamer::setPreservesPitch(bool preservesPitch)
954 {
955     m_preservesPitch = preservesPitch;
956 }
957
958 MediaPlayer::NetworkState MediaPlayerPrivateGStreamer::networkState() const
959 {
960     return m_networkState;
961 }
962
963 MediaPlayer::ReadyState MediaPlayerPrivateGStreamer::readyState() const
964 {
965     return m_readyState;
966 }
967
968 PassRefPtr<TimeRanges> MediaPlayerPrivateGStreamer::buffered() const
969 {
970     RefPtr<TimeRanges> timeRanges = TimeRanges::create();
971     if (m_errorOccured || isLiveStream())
972         return timeRanges.release();
973
974 #if GST_CHECK_VERSION(0, 10, 31)
975     float mediaDuration(duration());
976     if (!mediaDuration || isinf(mediaDuration))
977         return timeRanges.release();
978
979     GstQuery* query = gst_query_new_buffering(GST_FORMAT_PERCENT);
980
981     if (!gst_element_query(m_playBin.get(), query)) {
982         gst_query_unref(query);
983         return timeRanges.release();
984     }
985
986     gint64 rangeStart = 0, rangeStop = 0;
987     for (guint index = 0; index < gst_query_get_n_buffering_ranges(query); index++) {
988         if (gst_query_parse_nth_buffering_range(query, index, &rangeStart, &rangeStop))
989             timeRanges->add(static_cast<float>((rangeStart * mediaDuration) / 100),
990                             static_cast<float>((rangeStop * mediaDuration) / 100));
991     }
992
993     // Fallback to the more general maxTimeLoaded() if no range has
994     // been found.
995     if (!timeRanges->length())
996         if (float loaded = maxTimeLoaded())
997             timeRanges->add(0, loaded);
998
999     gst_query_unref(query);
1000 #else
1001     float loaded = maxTimeLoaded();
1002     if (!m_errorOccured && !isLiveStream() && loaded > 0)
1003         timeRanges->add(0, loaded);
1004 #endif
1005     return timeRanges.release();
1006 }
1007
1008 gboolean MediaPlayerPrivateGStreamer::handleMessage(GstMessage* message)
1009 {
1010     GOwnPtr<GError> err;
1011     GOwnPtr<gchar> debug;
1012     MediaPlayer::NetworkState error;
1013     bool issueError = true;
1014     bool attemptNextLocation = false;
1015     const GstStructure* structure = gst_message_get_structure(message);
1016
1017     if (structure) {
1018         const gchar* messageTypeName = gst_structure_get_name(structure);
1019
1020         // Redirect messages are sent from elements, like qtdemux, to
1021         // notify of the new location(s) of the media.
1022         if (!g_strcmp0(messageTypeName, "redirect")) {
1023             mediaLocationChanged(message);
1024             return TRUE;
1025         }
1026     }
1027
1028     LOG_MEDIA_MESSAGE("Message received from element %s", GST_MESSAGE_SRC_NAME(message));
1029     switch (GST_MESSAGE_TYPE(message)) {
1030     case GST_MESSAGE_ERROR:
1031         if (m_resetPipeline)
1032             break;
1033         gst_message_parse_error(message, &err.outPtr(), &debug.outPtr());
1034         LOG_MEDIA_MESSAGE("Error %d: %s (url=%s)", err->code, err->message, m_url.string().utf8().data());
1035
1036 #if ENABLE(TIZEN_DLOG_SUPPORT)
1037         TIZEN_LOGE("GST_MESSAGE_ERROR %d: %s (url=%s)", err->code, err->message, m_url.string().utf8().data());
1038 #endif
1039
1040         GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS(GST_BIN(m_playBin.get()), GST_DEBUG_GRAPH_SHOW_ALL, "webkit-video.error");
1041
1042         error = MediaPlayer::Empty;
1043         if (err->code == GST_STREAM_ERROR_CODEC_NOT_FOUND
1044             || err->code == GST_STREAM_ERROR_WRONG_TYPE
1045             || err->code == GST_STREAM_ERROR_FAILED
1046             || err->code == GST_CORE_ERROR_MISSING_PLUGIN
1047             || err->code == GST_RESOURCE_ERROR_NOT_FOUND)
1048             error = MediaPlayer::FormatError;
1049         else if (err->domain == GST_STREAM_ERROR) {
1050             // Let the mediaPlayerClient handle the stream error, in
1051             // this case the HTMLMediaElement will emit a stalled
1052             // event.
1053 #if !ENABLE(TIZEN_MEDIA_STREAM)
1054             if (err->code == GST_STREAM_ERROR_TYPE_NOT_FOUND) {
1055                 LOG_MEDIA_MESSAGE("Decode error, let the Media element emit a stalled event.");
1056                 break;
1057             }
1058 #endif
1059             error = MediaPlayer::DecodeError;
1060             attemptNextLocation = true;
1061         } else if (err->domain == GST_RESOURCE_ERROR)
1062             error = MediaPlayer::NetworkError;
1063
1064         if (attemptNextLocation)
1065             issueError = !loadNextLocation();
1066         if (issueError)
1067             loadingFailed(error);
1068         break;
1069     case GST_MESSAGE_EOS:
1070         LOG_MEDIA_MESSAGE("End of Stream");
1071         didEnd();
1072         break;
1073     case GST_MESSAGE_STATE_CHANGED:
1074         // Ignore state changes if load is delayed (preload=none). The
1075         // player state will be updated once commitLoad() is called.
1076         if (m_delayingLoad) {
1077             LOG_MEDIA_MESSAGE("Media load has been delayed. Ignoring state changes for now");
1078             break;
1079         }
1080
1081         // Ignore state changes from internal elements. They are
1082         // forwarded to playbin2 anyway.
1083         if (GST_MESSAGE_SRC(message) == reinterpret_cast<GstObject*>(m_playBin.get())) {
1084             updateStates();
1085
1086             // Construct a filename for the graphviz dot file output.
1087             GstState oldState, newState;
1088             gst_message_parse_state_changed(message, &oldState, &newState, 0);
1089
1090             CString dotFileName = String::format("webkit-video.%s_%s",
1091                                                  gst_element_state_get_name(oldState),
1092                                                  gst_element_state_get_name(newState)).utf8();
1093
1094             GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS(GST_BIN(m_playBin.get()), GST_DEBUG_GRAPH_SHOW_ALL, dotFileName.data());
1095
1096 #if ENABLE(TIZEN_GSTREAMER_VIDEO)
1097             if (m_suspendTime > 0 && newState == GST_STATE_PAUSED) {
1098                 if (!isLocalMediaStream())
1099                     seek(m_suspendTime);
1100                 m_suspendTime = 0;
1101                 m_player->mediaPlayerClient()->setSuspended(false);
1102             }
1103 #endif
1104         }
1105         break;
1106     case GST_MESSAGE_BUFFERING:
1107         processBufferingStats(message);
1108         break;
1109     case GST_MESSAGE_DURATION:
1110         LOG_MEDIA_MESSAGE("Duration changed");
1111         durationChanged();
1112         break;
1113     default:
1114         LOG_MEDIA_MESSAGE("Unhandled GStreamer message type: %s",
1115                     GST_MESSAGE_TYPE_NAME(message));
1116         break;
1117     }
1118     return TRUE;
1119 }
1120
1121 void MediaPlayerPrivateGStreamer::processBufferingStats(GstMessage* message)
1122 {
1123     // This is the immediate buffering that needs to happen so we have
1124     // enough to play right now.
1125     m_buffering = true;
1126     const GstStructure *structure = gst_message_get_structure(message);
1127     gst_structure_get_int(structure, "buffer-percent", &m_bufferingPercentage);
1128
1129     LOG_MEDIA_MESSAGE("[Buffering] Buffering: %d%%.", m_bufferingPercentage);
1130
1131     GstBufferingMode mode;
1132     gst_message_parse_buffering_stats(message, &mode, 0, 0, 0);
1133     if (mode != GST_BUFFERING_DOWNLOAD) {
1134         updateStates();
1135         return;
1136     }
1137
1138     // This is on-disk buffering, that allows us to download much more
1139     // than needed for right now.
1140     if (!m_startedBuffering) {
1141         LOG_MEDIA_MESSAGE("[Buffering] Starting on-disk buffering.");
1142
1143         m_startedBuffering = true;
1144
1145         if (m_fillTimer.isActive())
1146             m_fillTimer.stop();
1147
1148         m_fillTimer.startRepeating(0.2);
1149     }
1150 }
1151
1152 void MediaPlayerPrivateGStreamer::fillTimerFired(Timer<MediaPlayerPrivateGStreamer>*)
1153 {
1154     GstQuery* query = gst_query_new_buffering(GST_FORMAT_PERCENT);
1155
1156     if (!gst_element_query(m_playBin.get(), query)) {
1157         gst_query_unref(query);
1158         return;
1159     }
1160
1161     gint64 start, stop;
1162     gdouble fillStatus = 100.0;
1163
1164     gst_query_parse_buffering_range(query, 0, &start, &stop, 0);
1165     gst_query_unref(query);
1166
1167     if (stop != -1)
1168         fillStatus = 100.0 * stop / GST_FORMAT_PERCENT_MAX;
1169
1170     LOG_MEDIA_MESSAGE("[Buffering] Download buffer filled up to %f%%", fillStatus);
1171
1172     if (!m_mediaDuration)
1173         durationChanged();
1174
1175     // Update maxTimeLoaded only if the media duration is
1176     // available. Otherwise we can't compute it.
1177     if (m_mediaDuration) {
1178         if (fillStatus == 100.0)
1179             m_maxTimeLoaded = m_mediaDuration;
1180         else
1181             m_maxTimeLoaded = static_cast<float>((fillStatus * m_mediaDuration) / 100.0);
1182         LOG_MEDIA_MESSAGE("[Buffering] Updated maxTimeLoaded: %f", m_maxTimeLoaded);
1183     }
1184
1185     if (fillStatus != 100.0) {
1186         updateStates();
1187         return;
1188     }
1189
1190     // Media is now fully loaded. It will play even if network
1191     // connection is cut. Buffering is done, remove the fill source
1192     // from the main loop.
1193     m_fillTimer.stop();
1194     m_startedBuffering = false;
1195     updateStates();
1196 }
1197
1198 float MediaPlayerPrivateGStreamer::maxTimeSeekable() const
1199 {
1200     if (m_errorOccured)
1201         return 0.0f;
1202
1203     LOG_MEDIA_MESSAGE("maxTimeSeekable");
1204     // infinite duration means live stream
1205     if (isinf(duration()))
1206         return 0.0f;
1207
1208     return duration();
1209 }
1210
1211 float MediaPlayerPrivateGStreamer::maxTimeLoaded() const
1212 {
1213     if (m_errorOccured)
1214         return 0.0f;
1215
1216     float loaded = m_maxTimeLoaded;
1217     if (!loaded && !m_fillTimer.isActive())
1218         loaded = duration();
1219     LOG_MEDIA_MESSAGE("maxTimeLoaded: %f", loaded);
1220     return loaded;
1221 }
1222
1223 bool MediaPlayerPrivateGStreamer::didLoadingProgress() const
1224 {
1225     if (!m_playBin || !m_mediaDuration || !totalBytes())
1226         return false;
1227     float currentMaxTimeLoaded = maxTimeLoaded();
1228     bool didLoadingProgress = currentMaxTimeLoaded != m_maxTimeLoadedAtLastDidLoadingProgress;
1229     m_maxTimeLoadedAtLastDidLoadingProgress = currentMaxTimeLoaded;
1230     LOG_MEDIA_MESSAGE("didLoadingProgress: %d", didLoadingProgress);
1231     return didLoadingProgress;
1232 }
1233
1234 unsigned MediaPlayerPrivateGStreamer::totalBytes() const
1235 {
1236     if (m_errorOccured)
1237         return 0;
1238
1239     if (m_totalBytes != -1)
1240         return m_totalBytes;
1241
1242     if (!m_source)
1243         return 0;
1244
1245     GstFormat fmt = GST_FORMAT_BYTES;
1246     gint64 length = 0;
1247 #ifdef GST_API_VERSION_1
1248     if (gst_element_query_duration(m_source.get(), fmt, &length)) {
1249 #else
1250     if (gst_element_query_duration(m_source.get(), &fmt, &length)) {
1251 #endif
1252         LOG_MEDIA_MESSAGE("totalBytes %" G_GINT64_FORMAT, length);
1253         m_totalBytes = static_cast<unsigned>(length);
1254         m_isStreaming = !length;
1255         return m_totalBytes;
1256     }
1257
1258     // Fall back to querying the source pads manually.
1259     // See also https://bugzilla.gnome.org/show_bug.cgi?id=638749
1260     GstIterator* iter = gst_element_iterate_src_pads(m_source.get());
1261     bool done = false;
1262     while (!done) {
1263 #ifdef GST_API_VERSION_1
1264         GValue item = G_VALUE_INIT;
1265         switch (gst_iterator_next(iter, &item)) {
1266         case GST_ITERATOR_OK: {
1267             GstPad* pad = static_cast<GstPad*>(g_value_get_object(&item));
1268             gint64 padLength = 0;
1269             if (gst_pad_query_duration(pad, fmt, &padLength) && padLength > length)
1270                 length = padLength;
1271             break;
1272         }
1273 #else
1274         gpointer data;
1275
1276         switch (gst_iterator_next(iter, &data)) {
1277         case GST_ITERATOR_OK: {
1278             GRefPtr<GstPad> pad = adoptGRef(GST_PAD_CAST(data));
1279             gint64 padLength = 0;
1280             if (gst_pad_query_duration(pad.get(), &fmt, &padLength) && padLength > length)
1281                 length = padLength;
1282             break;
1283         }
1284 #endif
1285         case GST_ITERATOR_RESYNC:
1286             gst_iterator_resync(iter);
1287             break;
1288         case GST_ITERATOR_ERROR:
1289             // Fall through.
1290         case GST_ITERATOR_DONE:
1291             done = true;
1292             break;
1293         }
1294
1295 #ifdef GST_API_VERSION_1
1296         g_value_unset(&item);
1297 #endif
1298     }
1299
1300     gst_iterator_free(iter);
1301
1302     LOG_MEDIA_MESSAGE("totalBytes %" G_GINT64_FORMAT, length);
1303     m_totalBytes = static_cast<unsigned>(length);
1304     m_isStreaming = !length;
1305     return m_totalBytes;
1306 }
1307
1308 unsigned MediaPlayerPrivateGStreamer::decodedFrameCount() const
1309 {
1310     guint64 decodedFrames = 0;
1311     if (m_fpsSink)
1312         g_object_get(m_fpsSink, "frames-rendered", &decodedFrames, NULL);
1313     return static_cast<unsigned>(decodedFrames);
1314 }
1315
1316 unsigned MediaPlayerPrivateGStreamer::droppedFrameCount() const
1317 {
1318     guint64 framesDropped = 0;
1319     if (m_fpsSink)
1320         g_object_get(m_fpsSink, "frames-dropped", &framesDropped, NULL);
1321     return static_cast<unsigned>(framesDropped);
1322 }
1323
1324 unsigned MediaPlayerPrivateGStreamer::audioDecodedByteCount() const
1325 {
1326     GstQuery* query = gst_query_new_position(GST_FORMAT_BYTES);
1327     gint64 position = 0;
1328
1329     if (m_webkitAudioSink && gst_element_query(m_webkitAudioSink.get(), query))
1330         gst_query_parse_position(query, 0, &position);
1331
1332     gst_query_unref(query);
1333     return static_cast<unsigned>(position);
1334 }
1335
1336 unsigned MediaPlayerPrivateGStreamer::videoDecodedByteCount() const
1337 {
1338     GstQuery* query = gst_query_new_position(GST_FORMAT_BYTES);
1339     gint64 position = 0;
1340
1341     if (gst_element_query(m_webkitVideoSink, query))
1342         gst_query_parse_position(query, 0, &position);
1343
1344     gst_query_unref(query);
1345     return static_cast<unsigned>(position);
1346 }
1347
1348 void MediaPlayerPrivateGStreamer::updateAudioSink()
1349 {
1350     if (!m_playBin)
1351         return;
1352
1353     GstElement* sinkPtr = 0;
1354
1355     g_object_get(m_playBin.get(), "audio-sink", &sinkPtr, NULL);
1356     m_webkitAudioSink = adoptGRef(sinkPtr);
1357
1358 }
1359
1360
1361 void MediaPlayerPrivateGStreamer::sourceChanged()
1362 {
1363     GstElement* srcPtr = 0;
1364
1365     g_object_get(m_playBin.get(), "source", &srcPtr, NULL);
1366     m_source = adoptGRef(srcPtr);
1367
1368     if (WEBKIT_IS_WEB_SRC(m_source.get()))
1369         webKitWebSrcSetMediaPlayer(WEBKIT_WEB_SRC(m_source.get()), m_player);
1370 }
1371
1372 void MediaPlayerPrivateGStreamer::cancelLoad()
1373 {
1374     if (m_networkState < MediaPlayer::Loading || m_networkState == MediaPlayer::Loaded)
1375         return;
1376
1377     if (m_playBin)
1378         gst_element_set_state(m_playBin.get(), GST_STATE_NULL);
1379 #if ENABLE(TIZEN_GSTREAMER_AUDIO)
1380     if (m_audioSessionManager)
1381         m_audioSessionManager->setSoundState(ASM_STATE_STOP);
1382 #endif
1383 }
1384
1385 void MediaPlayerPrivateGStreamer::updateStates()
1386 {
1387     if (!m_playBin)
1388         return;
1389
1390     if (m_errorOccured)
1391         return;
1392
1393     MediaPlayer::NetworkState oldNetworkState = m_networkState;
1394     MediaPlayer::ReadyState oldReadyState = m_readyState;
1395     GstState state;
1396     GstState pending;
1397
1398     GstStateChangeReturn ret = gst_element_get_state(m_playBin.get(),
1399         &state, &pending, 250 * GST_NSECOND);
1400
1401     bool shouldUpdateAfterSeek = false;
1402     switch (ret) {
1403     case GST_STATE_CHANGE_SUCCESS:
1404         LOG_MEDIA_MESSAGE("State: %s, pending: %s",
1405             gst_element_state_get_name(state),
1406             gst_element_state_get_name(pending));
1407
1408         m_resetPipeline = state <= GST_STATE_READY;
1409
1410         // Try to figure out ready and network states.
1411         if (state == GST_STATE_READY) {
1412             m_readyState = MediaPlayer::HaveMetadata;
1413             m_networkState = MediaPlayer::Empty;
1414             // Cache the duration without emiting the durationchange
1415             // event because it's taken care of by the media element
1416             // in this precise case.
1417             if (!m_isEndReached)
1418                 cacheDuration();
1419         } else if ((state == GST_STATE_NULL) || (maxTimeLoaded() == duration())) {
1420             m_networkState = MediaPlayer::Loaded;
1421             m_readyState = MediaPlayer::HaveEnoughData;
1422         } else {
1423             m_readyState = currentTime() < maxTimeLoaded() ? MediaPlayer::HaveFutureData : MediaPlayer::HaveCurrentData;
1424             m_networkState = MediaPlayer::Loading;
1425         }
1426
1427         if (m_buffering && state != GST_STATE_READY) {
1428             m_readyState = MediaPlayer::HaveCurrentData;
1429             m_networkState = MediaPlayer::Loading;
1430         }
1431
1432         // Now let's try to get the states in more detail using
1433         // information from GStreamer, while we sync states where
1434         // needed.
1435         if (state == GST_STATE_PAUSED) {
1436             if (!m_webkitAudioSink)
1437                 updateAudioSink();
1438
1439             if (!m_volumeAndMuteInitialized) {
1440                 notifyPlayerOfVolumeChange();
1441                 notifyPlayerOfMute();
1442                 m_volumeAndMuteInitialized = true;
1443             }
1444
1445             if (m_buffering && m_bufferingPercentage == 100) {
1446                 m_buffering = false;
1447                 m_bufferingPercentage = 0;
1448                 m_readyState = MediaPlayer::HaveEnoughData;
1449
1450                 LOG_MEDIA_MESSAGE("[Buffering] Complete.");
1451
1452                 if (!m_paused) {
1453                     LOG_MEDIA_MESSAGE("[Buffering] Restarting playback.");
1454                     gst_element_set_state(m_playBin.get(), GST_STATE_PLAYING);
1455 #if ENABLE(TIZEN_GSTREAMER_AUDIO)
1456                     if (m_audioSessionManager)
1457                         m_audioSessionManager->setSoundState(ASM_STATE_PLAYING);
1458 #endif
1459                 }
1460             } else if (!m_buffering && (currentTime() < duration())) {
1461                 m_paused = true;
1462             }
1463         } else if (state == GST_STATE_PLAYING) {
1464             m_readyState = MediaPlayer::HaveEnoughData;
1465             m_paused = false;
1466
1467             if (m_buffering && !isLiveStream()) {
1468                 m_readyState = MediaPlayer::HaveCurrentData;
1469                 m_networkState = MediaPlayer::Loading;
1470
1471                 LOG_MEDIA_MESSAGE("[Buffering] Pausing stream for buffering.");
1472
1473                 gst_element_set_state(m_playBin.get(), GST_STATE_PAUSED);
1474 #if ENABLE(TIZEN_GSTREAMER_AUDIO)
1475                 if (m_audioSessionManager)
1476                     m_audioSessionManager->setSoundState(ASM_STATE_PAUSE);
1477 #endif
1478             }
1479         } else
1480             m_paused = true;
1481
1482         // Is on-disk buffering in progress?
1483         if (m_fillTimer.isActive())
1484             m_networkState = MediaPlayer::Loading;
1485
1486         if (m_changingRate) {
1487             m_player->rateChanged();
1488             m_changingRate = false;
1489         }
1490
1491         if (m_seeking) {
1492             shouldUpdateAfterSeek = true;
1493             m_seeking = false;
1494         }
1495
1496         break;
1497     case GST_STATE_CHANGE_ASYNC:
1498         LOG_MEDIA_MESSAGE("Async: State: %s, pending: %s",
1499             gst_element_state_get_name(state),
1500             gst_element_state_get_name(pending));
1501         // Change in progress
1502
1503         // On-disk buffering was attempted but the media is live. This
1504         // can't work so disable on-disk buffering and reset the
1505         // pipeline.
1506
1507 #if ENABLE(TIZEN_MEDIA_STREAM)
1508         if (state == GST_STATE_READY && isLiveStream() && m_preload == MediaPlayer::Auto && !isLocalMediaStream()) {
1509 #else
1510         if (state == GST_STATE_READY && isLiveStream() && m_preload == MediaPlayer::Auto) {
1511 #endif
1512             setPreload(MediaPlayer::None);
1513             gst_element_set_state(m_playBin.get(), GST_STATE_NULL);
1514             gst_element_set_state(m_playBin.get(), GST_STATE_PAUSED);
1515 #if ENABLE(TIZEN_GSTREAMER_AUDIO)
1516             if (m_audioSessionManager)
1517                 m_audioSessionManager->setSoundState(ASM_STATE_PAUSE);
1518 #endif
1519         }
1520
1521         // A live stream was paused, reset the pipeline.
1522 #if ENABLE(TIZEN_MEDIA_STREAM)
1523         if (state == GST_STATE_PAUSED && pending == GST_STATE_PLAYING && isLiveStream() && !isLocalMediaStream()) {
1524 #else
1525         if (state == GST_STATE_PAUSED && pending == GST_STATE_PLAYING && isLiveStream()) {
1526 #endif
1527             gst_element_set_state(m_playBin.get(), GST_STATE_NULL);
1528             gst_element_set_state(m_playBin.get(), GST_STATE_PLAYING);
1529 #if ENABLE(TIZEN_GSTREAMER_AUDIO)
1530             if (m_audioSessionManager)
1531                 m_audioSessionManager->setSoundState(ASM_STATE_PLAYING);
1532 #endif
1533         }
1534
1535         if (!isLiveStream() && !m_buffering)
1536             return;
1537
1538         if (m_seeking) {
1539             shouldUpdateAfterSeek = true;
1540             m_seeking = false;
1541         }
1542         break;
1543     case GST_STATE_CHANGE_FAILURE:
1544         LOG_MEDIA_MESSAGE("Failure: State: %s, pending: %s",
1545             gst_element_state_get_name(state),
1546             gst_element_state_get_name(pending));
1547         // Change failed
1548         return;
1549     case GST_STATE_CHANGE_NO_PREROLL:
1550         LOG_MEDIA_MESSAGE("No preroll: State: %s, pending: %s",
1551             gst_element_state_get_name(state),
1552             gst_element_state_get_name(pending));
1553
1554         if (state == GST_STATE_READY)
1555             m_readyState = MediaPlayer::HaveNothing;
1556         else if (state == GST_STATE_PAUSED) {
1557             m_readyState = MediaPlayer::HaveEnoughData;
1558             m_paused = true;
1559             // Live pipelines go in PAUSED without prerolling.
1560             m_isStreaming = true;
1561         } else if (state == GST_STATE_PLAYING)
1562             m_paused = false;
1563
1564         if (m_seeking) {
1565             shouldUpdateAfterSeek = true;
1566             m_seeking = false;
1567             if (!m_paused) {
1568                 gst_element_set_state(m_playBin.get(), GST_STATE_PLAYING);
1569 #if ENABLE(TIZEN_GSTREAMER_AUDIO)
1570                 if (m_audioSessionManager)
1571                     m_audioSessionManager->setSoundState(ASM_STATE_PLAYING);
1572 #endif
1573             }
1574         } else if (!m_paused) {
1575             gst_element_set_state(m_playBin.get(), GST_STATE_PLAYING);
1576 #if ENABLE(TIZEN_GSTREAMER_AUDIO)
1577             if (m_audioSessionManager)
1578                 m_audioSessionManager->setSoundState(ASM_STATE_PLAYING);
1579 #endif
1580         }
1581
1582         m_networkState = MediaPlayer::Loading;
1583         break;
1584     default:
1585         LOG_MEDIA_MESSAGE("Else : %d", ret);
1586         break;
1587     }
1588
1589     if (seeking())
1590         m_readyState = MediaPlayer::HaveNothing;
1591
1592     if (shouldUpdateAfterSeek)
1593         timeChanged();
1594
1595     if (m_networkState != oldNetworkState) {
1596         LOG_MEDIA_MESSAGE("Network State Changed from %u to %u",
1597             oldNetworkState, m_networkState);
1598         m_player->networkStateChanged();
1599     }
1600     if (m_readyState != oldReadyState) {
1601         LOG_MEDIA_MESSAGE("Ready State Changed from %u to %u",
1602             oldReadyState, m_readyState);
1603         m_player->readyStateChanged();
1604 #if ENABLE(TIZEN_ACCELERATED_COMPOSITING) && USE(TIZEN_TEXTURE_MAPPER)
1605         if (state == GST_STATE_PLAYING)
1606             m_player->mediaPlayerClient()->mediaPlayerRenderingModeChanged(m_player);
1607 #endif
1608     }
1609 }
1610
1611 void MediaPlayerPrivateGStreamer::mediaLocationChanged(GstMessage* message)
1612 {
1613     if (m_mediaLocations)
1614         gst_structure_free(m_mediaLocations);
1615
1616     const GstStructure* structure = gst_message_get_structure(message);
1617     if (structure) {
1618         // This structure can contain:
1619         // - both a new-location string and embedded locations structure
1620         // - or only a new-location string.
1621         m_mediaLocations = gst_structure_copy(structure);
1622         const GValue* locations = gst_structure_get_value(m_mediaLocations, "locations");
1623
1624         if (locations)
1625             m_mediaLocationCurrentIndex = static_cast<int>(gst_value_list_get_size(locations)) -1;
1626
1627         loadNextLocation();
1628     }
1629 }
1630
1631 bool MediaPlayerPrivateGStreamer::loadNextLocation()
1632 {
1633     if (!m_mediaLocations)
1634         return false;
1635
1636     const GValue* locations = gst_structure_get_value(m_mediaLocations, "locations");
1637     const gchar* newLocation = 0;
1638
1639     if (!locations) {
1640         // Fallback on new-location string.
1641         newLocation = gst_structure_get_string(m_mediaLocations, "new-location");
1642         if (!newLocation)
1643             return false;
1644     }
1645
1646     if (!newLocation) {
1647         if (m_mediaLocationCurrentIndex < 0) {
1648             m_mediaLocations = 0;
1649             return false;
1650         }
1651
1652         const GValue* location = gst_value_list_get_value(locations,
1653                                                           m_mediaLocationCurrentIndex);
1654         const GstStructure* structure = gst_value_get_structure(location);
1655
1656         if (!structure) {
1657             m_mediaLocationCurrentIndex--;
1658             return false;
1659         }
1660
1661         newLocation = gst_structure_get_string(structure, "new-location");
1662     }
1663
1664     if (newLocation) {
1665         // Found a candidate. new-location is not always an absolute url
1666         // though. We need to take the base of the current url and
1667         // append the value of new-location to it.
1668
1669         gchar* currentLocation = 0;
1670         g_object_get(m_playBin.get(), "uri", &currentLocation, NULL);
1671
1672         KURL currentUrl(KURL(), currentLocation);
1673         g_free(currentLocation);
1674
1675         KURL newUrl;
1676
1677         if (gst_uri_is_valid(newLocation))
1678             newUrl = KURL(KURL(), newLocation);
1679         else
1680             newUrl = KURL(KURL(), currentUrl.baseAsString() + newLocation);
1681
1682         RefPtr<SecurityOrigin> securityOrigin = SecurityOrigin::create(currentUrl);
1683         if (securityOrigin->canRequest(newUrl)) {
1684             LOG_MEDIA_MESSAGE("New media url: %s", newUrl.string().utf8().data());
1685
1686             // Reset player states.
1687             m_networkState = MediaPlayer::Loading;
1688             m_player->networkStateChanged();
1689             m_readyState = MediaPlayer::HaveNothing;
1690             m_player->readyStateChanged();
1691
1692             // Reset pipeline state.
1693             m_resetPipeline = true;
1694             gst_element_set_state(m_playBin.get(), GST_STATE_READY);
1695 #if ENABLE(TIZEN_GSTREAMER_AUDIO)
1696             if (m_audioSessionManager)
1697                 m_audioSessionManager->setSoundState(ASM_STATE_NONE);
1698 #endif
1699             GstState state;
1700             gst_element_get_state(m_playBin.get(), &state, 0, 0);
1701             if (state <= GST_STATE_READY) {
1702                 // Set the new uri and start playing.
1703                 g_object_set(m_playBin.get(), "uri", newUrl.string().utf8().data(), NULL);
1704                 gst_element_set_state(m_playBin.get(), GST_STATE_PLAYING);
1705 #if ENABLE(TIZEN_GSTREAMER_AUDIO)
1706                 if (m_audioSessionManager)
1707                     m_audioSessionManager->setSoundState(ASM_STATE_PLAYING);
1708 #endif
1709                 return true;
1710             }
1711         }
1712     }
1713     m_mediaLocationCurrentIndex--;
1714     return false;
1715
1716 }
1717
1718 void MediaPlayerPrivateGStreamer::loadStateChanged()
1719 {
1720     updateStates();
1721 }
1722
1723 void MediaPlayerPrivateGStreamer::sizeChanged()
1724 {
1725     notImplemented();
1726 }
1727
1728 void MediaPlayerPrivateGStreamer::timeChanged()
1729 {
1730     updateStates();
1731     m_player->timeChanged();
1732 }
1733
1734 void MediaPlayerPrivateGStreamer::didEnd()
1735 {
1736     // Synchronize position and duration values to not confuse the
1737     // HTMLMediaElement. In some cases like reverse playback the
1738     // position is not always reported as 0 for instance.
1739     float now = currentTime();
1740 #if ENABLE(TIZEN_GSTREAMER_VIDEO)
1741     if (now > 0 && now >= duration() && m_mediaDuration != now) {
1742 #else
1743     if (now > 0 && now <= duration() && m_mediaDuration != now) {
1744 #endif
1745         m_mediaDurationKnown = true;
1746         m_mediaDuration = now;
1747         m_player->durationChanged();
1748     }
1749
1750     m_isEndReached = true;
1751     timeChanged();
1752
1753     if (!m_player->mediaPlayerClient()->mediaPlayerIsLooping()) {
1754         m_paused = true;
1755 #if ENABLE(TIZEN_GSTREAMER_AUDIO)
1756         gst_element_set_state(m_playBin.get(), GST_STATE_PAUSED);
1757         if (m_audioSessionManager)
1758             m_audioSessionManager->setSoundState(ASM_STATE_PAUSE);
1759 #else
1760         gst_element_set_state(m_playBin.get(), GST_STATE_NULL);
1761 #endif
1762
1763 #if ENABLE(TIZEN_GSTREAMER_VIDEO)
1764         HTMLMediaElement* element = static_cast<HTMLMediaElement*>(player()->mediaPlayerClient());
1765         if (element->isVideo())
1766             power_unlock_state(POWER_STATE_NORMAL);
1767 #endif
1768     }
1769 }
1770
1771 void MediaPlayerPrivateGStreamer::cacheDuration()
1772 {
1773 #if ENABLE(TIZEN_GSTREAMER_VIDEO)
1774     float previousDuration = m_mediaDuration;
1775 #endif
1776     // Reset cached media duration
1777     m_mediaDuration = 0;
1778
1779     // And re-cache it if possible.
1780     GstState state;
1781     gst_element_get_state(m_playBin.get(), &state, 0, 0);
1782     float newDuration = duration();
1783
1784 #if ENABLE(TIZEN_GSTREAMER_VIDEO)
1785     if (state > GST_STATE_READY) {
1786         // Don't set m_mediaDurationKnown yet if the pipeline is not
1787         // paused. This allows duration() query to fail at least once
1788         // before playback starts and duration becomes known.
1789         m_mediaDurationKnown = !isinf(newDuration);
1790     }
1791
1792     if (!isinf(newDuration))
1793         m_mediaDuration = newDuration;
1794     else
1795         m_mediaDuration = previousDuration;
1796 #else
1797     if (state <= GST_STATE_READY) {
1798         // Don't set m_mediaDurationKnown yet if the pipeline is not
1799         // paused. This allows duration() query to fail at least once
1800         // before playback starts and duration becomes known.
1801         if (!isinf(newDuration))
1802             m_mediaDuration = newDuration;
1803     } else {
1804         m_mediaDurationKnown = !isinf(newDuration);
1805         if (m_mediaDurationKnown)
1806             m_mediaDuration = newDuration;
1807     }
1808
1809     if (!isinf(newDuration))
1810         m_mediaDuration = newDuration;
1811 #endif
1812 }
1813
1814 void MediaPlayerPrivateGStreamer::durationChanged()
1815 {
1816     float previousDuration = m_mediaDuration;
1817
1818     cacheDuration();
1819     // Avoid emiting durationchanged in the case where the previous
1820     // duration was 0 because that case is already handled by the
1821     // HTMLMediaElement.
1822     if (previousDuration && m_mediaDuration != previousDuration)
1823         m_player->durationChanged();
1824
1825     if (m_preload == MediaPlayer::None && m_originalPreloadWasAutoAndWasOverridden) {
1826         m_totalBytes = -1;
1827         if (totalBytes() && !isLiveStream()) {
1828             setPreload(MediaPlayer::Auto);
1829             gst_element_set_state(m_playBin.get(), GST_STATE_NULL);
1830             gst_element_set_state(m_playBin.get(), GST_STATE_PAUSED);
1831 #if ENABLE(TIZEN_GSTREAMER_AUDIO)
1832             if (m_audioSessionManager)
1833                 m_audioSessionManager->setSoundState(ASM_STATE_PAUSE);
1834 #endif
1835         }
1836     }
1837 }
1838
1839 bool MediaPlayerPrivateGStreamer::supportsMuting() const
1840 {
1841     return true;
1842 }
1843
1844 void MediaPlayerPrivateGStreamer::setMuted(bool muted)
1845 {
1846     if (!m_playBin)
1847         return;
1848
1849     g_object_set(m_playBin.get(), "mute", muted, NULL);
1850 }
1851
1852 void MediaPlayerPrivateGStreamer::notifyPlayerOfMute()
1853 {
1854     m_muteTimerHandler = 0;
1855
1856     if (!m_player || !m_playBin)
1857         return;
1858
1859     gboolean muted;
1860     g_object_get(m_playBin.get(), "mute", &muted, NULL);
1861     m_player->muteChanged(static_cast<bool>(muted));
1862 }
1863
1864 void MediaPlayerPrivateGStreamer::muteChanged()
1865 {
1866     if (m_muteTimerHandler)
1867         g_source_remove(m_muteTimerHandler);
1868     m_muteTimerHandler = g_timeout_add(0, reinterpret_cast<GSourceFunc>(mediaPlayerPrivateMuteChangeTimeoutCallback), this);
1869 }
1870
1871 void MediaPlayerPrivateGStreamer::loadingFailed(MediaPlayer::NetworkState error)
1872 {
1873 #if ENABLE(TIZEN_DLOG_SUPPORT)
1874     TIZEN_LOGE("NetworkState : %d", error);
1875 #endif
1876
1877     m_errorOccured = true;
1878     if (m_networkState != error) {
1879         m_networkState = error;
1880         m_player->networkStateChanged();
1881     }
1882     if (m_readyState != MediaPlayer::HaveNothing) {
1883         m_readyState = MediaPlayer::HaveNothing;
1884         m_player->readyStateChanged();
1885     }
1886 }
1887
1888 void MediaPlayerPrivateGStreamer::setSize(const IntSize& size)
1889 {
1890     m_size = size;
1891 }
1892
1893 void MediaPlayerPrivateGStreamer::setVisible(bool visible)
1894 {
1895 }
1896
1897 void MediaPlayerPrivateGStreamer::triggerRepaint(GstBuffer* buffer)
1898 {
1899     g_return_if_fail(GST_IS_BUFFER(buffer));
1900     gst_buffer_replace(&m_buffer, buffer);
1901 #if ENABLE(TIZEN_ACCELERATED_COMPOSITING) && USE(TIZEN_TEXTURE_MAPPER)
1902 #if !ENABLE(TIZEN_WEBKIT2_TILED_AC_SHARED_PLATFORM_SURFACE)
1903     if (m_videoSize.isEmpty())
1904         m_videoSize =  naturalSize();
1905     m_videoLayer->paintVideoLayer(m_videoSize);
1906     return;
1907 #endif
1908 #endif
1909     m_player->repaint();
1910 }
1911
1912 void MediaPlayerPrivateGStreamer::paint(GraphicsContext* context, const IntRect& rect)
1913 {
1914     if (context->paintingDisabled())
1915         return;
1916
1917     if (!m_player->visible())
1918         return;
1919
1920     if (!m_buffer)
1921         return;
1922
1923     GRefPtr<GstCaps> caps = webkitGstGetPadCaps(m_videoSinkPad.get());
1924     if (!caps)
1925         return;
1926
1927     RefPtr<ImageGStreamer> gstImage = ImageGStreamer::createImage(m_buffer, caps.get());
1928     if (!gstImage)
1929         return;
1930
1931     context->drawImage(reinterpret_cast<Image*>(gstImage->image().get()), ColorSpaceSRGB,
1932                        rect, gstImage->rect(), CompositeCopy, DoNotRespectImageOrientation, false);
1933 }
1934
1935 static HashSet<String> mimeTypeCache()
1936 {
1937     initializeGStreamerAndRegisterWebKitElements();
1938
1939     DEFINE_STATIC_LOCAL(HashSet<String>, cache, ());
1940     static bool typeListInitialized = false;
1941
1942     if (typeListInitialized)
1943         return cache;
1944     const char* mimeTypes[] = {
1945         "application/ogg",
1946         "application/vnd.apple.mpegurl",
1947         "application/vnd.rn-realmedia",
1948         "application/x-3gp",
1949 #if ENABLE(TIZEN_GSTREAMER_VIDEO)
1950         "application/x-mpegurl",
1951         "application/x-mpegURL",
1952 #endif
1953         "application/x-pn-realaudio",
1954         "audio/3gpp",
1955         "audio/aac",
1956         "audio/flac",
1957         "audio/iLBC-sh",
1958         "audio/midi",
1959         "audio/mobile-xmf",
1960         "audio/mp1",
1961         "audio/mp2",
1962         "audio/mp3",
1963         "audio/mp4",
1964         "audio/mpeg",
1965         "audio/ogg",
1966         "audio/opus",
1967         "audio/qcelp",
1968         "audio/riff-midi",
1969         "audio/wav",
1970         "audio/webm",
1971         "audio/x-ac3",
1972         "audio/x-aiff",
1973         "audio/x-amr-nb-sh",
1974         "audio/x-amr-wb-sh",
1975         "audio/x-au",
1976         "audio/x-ay",
1977         "audio/x-celt",
1978         "audio/x-dts",
1979         "audio/x-flac",
1980         "audio/x-gbs",
1981         "audio/x-gsm",
1982         "audio/x-gym",
1983         "audio/x-imelody",
1984         "audio/x-ircam",
1985         "audio/x-kss",
1986         "audio/x-m4a",
1987         "audio/x-mod",
1988         "audio/x-mp3",
1989         "audio/x-mpeg",
1990         "audio/x-musepack",
1991         "audio/x-nist",
1992         "audio/x-nsf",
1993         "audio/x-paris",
1994         "audio/x-sap",
1995         "audio/x-sbc",
1996         "audio/x-sds",
1997         "audio/x-shorten",
1998         "audio/x-sid",
1999         "audio/x-spc",
2000         "audio/x-speex",
2001         "audio/x-svx",
2002         "audio/x-ttafile",
2003         "audio/x-vgm",
2004         "audio/x-voc",
2005         "audio/x-vorbis+ogg",
2006         "audio/x-w64",
2007         "audio/x-wav",
2008         "audio/x-wavpack",
2009         "audio/x-wavpack-correction",
2010         "video/3gpp",
2011         "video/mj2",
2012         "video/mp4",
2013         "video/mpeg",
2014         "video/mpegts",
2015         "video/ogg",
2016         "video/quicktime",
2017         "video/vivo",
2018 #if !ENABLE(TIZEN_GSTREAMER_VIDEO)
2019         "video/webm",
2020 #endif
2021         "video/x-cdxa",
2022         "video/x-dirac",
2023         "video/x-dv",
2024         "video/x-fli",
2025         "video/x-flv",
2026         "video/x-h263",
2027         "video/x-ivf",
2028         "video/x-m4v",
2029         "video/x-matroska",
2030         "video/x-mng",
2031         "video/x-ms-asf",
2032         "video/x-msvideo",
2033         "video/x-mve",
2034         "video/x-nuv",
2035         "video/x-vcd"
2036     };
2037
2038     for (unsigned i = 0; i < (sizeof(mimeTypes) / sizeof(*mimeTypes)); ++i)
2039         cache.add(String(mimeTypes[i]));
2040
2041     typeListInitialized = true;
2042     return cache;
2043 }
2044
2045 void MediaPlayerPrivateGStreamer::getSupportedTypes(HashSet<String>& types)
2046 {
2047     types = mimeTypeCache();
2048 }
2049
2050 MediaPlayer::SupportsType MediaPlayerPrivateGStreamer::supportsType(const String& type, const String& codecs, const KURL&)
2051 {
2052     if (type.isNull() || type.isEmpty())
2053         return MediaPlayer::IsNotSupported;
2054
2055     // spec says we should not return "probably" if the codecs string is empty
2056     if (mimeTypeCache().contains(type))
2057         return codecs.isEmpty() ? MediaPlayer::MayBeSupported : MediaPlayer::IsSupported;
2058     return MediaPlayer::IsNotSupported;
2059 }
2060
2061 bool MediaPlayerPrivateGStreamer::hasSingleSecurityOrigin() const
2062 {
2063     return true;
2064 }
2065
2066 bool MediaPlayerPrivateGStreamer::supportsFullscreen() const
2067 {
2068 #if PLATFORM(MAC) && !PLATFORM(IOS) && __MAC_OS_X_VERSION_MIN_REQUIRED == 1050
2069     // See <rdar://problem/7389945>
2070     return false;
2071 #else
2072     return true;
2073 #endif
2074 }
2075
2076 PlatformMedia MediaPlayerPrivateGStreamer::platformMedia() const
2077 {
2078     PlatformMedia p;
2079 #ifndef GST_API_VERSION_1
2080     p.type = PlatformMedia::GStreamerGWorldType;
2081     p.media.gstreamerGWorld = m_gstGWorld.get();
2082 #endif
2083     return p;
2084 }
2085
2086 MediaPlayer::MovieLoadType MediaPlayerPrivateGStreamer::movieLoadType() const
2087 {
2088     if (m_readyState == MediaPlayer::HaveNothing)
2089         return MediaPlayer::Unknown;
2090
2091     if (isLiveStream())
2092         return MediaPlayer::LiveStream;
2093
2094     return MediaPlayer::Download;
2095 }
2096
2097 void MediaPlayerPrivateGStreamer::setDownloadBuffering()
2098 {
2099     if (!m_playBin)
2100         return;
2101
2102     GstPlayFlags flags;
2103
2104     g_object_get(m_playBin.get(), "flags", &flags, NULL);
2105 #if ENABLE(TIZEN_ACCELERATED_COMPOSITING) && USE(TIZEN_TEXTURE_MAPPER) && ENABLE(TIZEN_WEBKIT2_TILED_AC_SHARED_PLATFORM_SURFACE)
2106     flags = (GstPlayFlags)(flags | GST_PLAY_FLAG_NATIVE_VIDEO);
2107 #endif
2108     if (m_preload == MediaPlayer::Auto) {
2109         LOG_MEDIA_MESSAGE("Enabling on-disk buffering");
2110         g_object_set(m_playBin.get(), "flags", flags | GST_PLAY_FLAG_DOWNLOAD, NULL);
2111     } else {
2112         LOG_MEDIA_MESSAGE("Disabling on-disk buffering");
2113         g_object_set(m_playBin.get(), "flags", flags & ~GST_PLAY_FLAG_DOWNLOAD, NULL);
2114     }
2115 }
2116
2117 void MediaPlayerPrivateGStreamer::setPreload(MediaPlayer::Preload preload)
2118 {
2119     m_originalPreloadWasAutoAndWasOverridden = m_preload != preload && m_preload == MediaPlayer::Auto;
2120
2121     m_preload = preload;
2122
2123     setDownloadBuffering();
2124
2125     if (m_delayingLoad && m_preload != MediaPlayer::None) {
2126         m_delayingLoad = false;
2127         commitLoad();
2128     }
2129 }
2130
2131 void MediaPlayerPrivateGStreamer::createAudioSink()
2132 {
2133     // Construct audio sink if pitch preserving is enabled.
2134     if (!m_preservesPitch)
2135         return;
2136
2137     if (!m_playBin)
2138         return;
2139
2140     GstElement* scale = gst_element_factory_make("scaletempo", 0);
2141     if (!scale) {
2142         GST_WARNING("Failed to create scaletempo");
2143         return;
2144     }
2145
2146     GstElement* convert = gst_element_factory_make("audioconvert", 0);
2147     GstElement* resample = gst_element_factory_make("audioresample", 0);
2148     GstElement* sink = gst_element_factory_make("autoaudiosink", 0);
2149
2150     GstElement* audioSink = gst_bin_new("audio-sink");
2151     gst_bin_add_many(GST_BIN(audioSink), scale, convert, resample, sink, NULL);
2152
2153     if (!gst_element_link_many(scale, convert, resample, sink, NULL)) {
2154         GST_WARNING("Failed to link audio sink elements");
2155         gst_object_unref(audioSink);
2156         return;
2157     }
2158
2159     GRefPtr<GstPad> pad = adoptGRef(gst_element_get_static_pad(scale, "sink"));
2160     gst_element_add_pad(audioSink, gst_ghost_pad_new("sink", pad.get()));
2161
2162     g_object_set(m_playBin.get(), "audio-sink", audioSink, NULL);
2163 }
2164
2165 #if ENABLE(TIZEN_GSTREAMER_VIDEO)
2166 GstElement* MediaPlayerPrivateGStreamer::createVideoSink()
2167 {
2168 #if ENABLE(TIZEN_ACCELERATED_COMPOSITING) && USE(TIZEN_TEXTURE_MAPPER) && ENABLE(TIZEN_WEBKIT2_TILED_AC_SHARED_PLATFORM_SURFACE)
2169     m_webkitVideoSink = m_videoLayer->createVideoSink();
2170     m_videoSinkPad = adoptGRef(gst_element_get_static_pad(m_webkitVideoSink, "sink"));
2171
2172     // Primary video should be OFF and Secondary video should be FULL SCREEN mode
2173     g_object_set(m_webkitVideoSink, "display-mode", 2, NULL);
2174 #else // ENABLE(TIZEN_ACCELERATED_COMPOSITING) && USE(TIZEN_TEXTURE_MAPPER) && ENABLE(TIZEN_WEBKIT2_TILED_AC_SHARED_PLATFORM_SURFACE)
2175 #ifndef GST_API_VERSION_1
2176     m_webkitVideoSink = webkitVideoSinkNew(m_gstGWorld.get());
2177 #else
2178     m_webkitVideoSink = webkitVideoSinkNew();
2179 #endif
2180     m_videoSinkPad = adoptGRef(gst_element_get_static_pad(m_webkitVideoSink, "sink"));
2181     g_signal_connect(m_webkitVideoSink, "repaint-requested", G_CALLBACK(mediaPlayerPrivateRepaintCallback), this);
2182 #endif // ENABLE(TIZEN_ACCELERATED_COMPOSITING) && USE(TIZEN_TEXTURE_MAPPER) && ENABLE(TIZEN_WEBKIT2_TILED_AC_SHARED_PLATFORM_SURFACE)
2183
2184 #ifndef GST_API_VERSION_1
2185     m_videoSinkBin = gst_bin_new("video-sink");
2186     GstElement* videoTee = gst_element_factory_make("tee", "videoTee");
2187 #if !ENABLE(TIZEN_GSTREAMER_VIDEO)
2188     GstElement* queue = gst_element_factory_make("queue", 0);
2189 #endif
2190     // Take ownership.
2191     gst_object_ref_sink(m_videoSinkBin);
2192
2193     // Build a new video sink consisting of a bin containing a tee
2194     // (meant to distribute data to multiple video sinks) and our
2195     // internal video sink. For fullscreen we create an autovideosink
2196     // and initially block the data flow towards it and configure it
2197 #if !ENABLE(TIZEN_GSTREAMER_VIDEO)
2198     gst_bin_add_many(GST_BIN(m_videoSinkBin), videoTee, queue, NULL);
2199 #else
2200     gst_bin_add_many(GST_BIN(m_videoSinkBin), videoTee, NULL);
2201 #endif
2202
2203 #if !ENABLE(TIZEN_GSTREAMER_VIDEO)
2204     // Link a new src pad from tee to queue1.
2205     gst_element_link_pads_full(videoTee, 0, queue, "sink", GST_PAD_LINK_CHECK_NOTHING);
2206 #endif
2207 #endif
2208
2209     GstElement* actualVideoSink = 0;
2210     m_fpsSink = gst_element_factory_make("fpsdisplaysink", "sink");
2211     if (m_fpsSink) {
2212         // The verbose property has been added in -bad 0.10.22. Making
2213         // this whole code depend on it because we don't want
2214         // fpsdiplaysink to spit data on stdout.
2215         GstElementFactory* factory = GST_ELEMENT_FACTORY(GST_ELEMENT_GET_CLASS(m_fpsSink)->elementfactory);
2216         if (gst_plugin_feature_check_version(GST_PLUGIN_FEATURE(factory), 0, 10, 22)) {
2217             g_object_set(m_fpsSink, "silent", TRUE , NULL);
2218
2219             // Turn off text overlay unless logging is enabled.
2220 #if LOG_DISABLED
2221             g_object_set(m_fpsSink, "text-overlay", FALSE , NULL);
2222 #else
2223             WTFLogChannel* channel = getChannelFromName("Media");
2224             if (channel->state != WTFLogChannelOn)
2225                 g_object_set(m_fpsSink, "text-overlay", FALSE , NULL);
2226 #endif // LOG_DISABLED
2227
2228             if (g_object_class_find_property(G_OBJECT_GET_CLASS(m_fpsSink), "video-sink")) {
2229                 g_object_set(m_fpsSink, "video-sink", m_webkitVideoSink, NULL);
2230 #ifndef GST_API_VERSION_1
2231                 gst_bin_add(GST_BIN(m_videoSinkBin), m_fpsSink);
2232 #endif
2233                 actualVideoSink = m_fpsSink;
2234             } else
2235                 m_fpsSink = 0;
2236         } else
2237             m_fpsSink = 0;
2238     }
2239
2240     if (!m_fpsSink) {
2241 #ifndef GST_API_VERSION_1
2242         gst_bin_add(GST_BIN(m_videoSinkBin), m_webkitVideoSink);
2243 #endif
2244         actualVideoSink = m_webkitVideoSink;
2245     }
2246
2247     ASSERT(actualVideoSink);
2248
2249 #ifndef GST_API_VERSION_1
2250 #if ENABLE(TIZEN_GSTREAMER_VIDEO)
2251     // Link a new src pad from tee to queue1.
2252     GRefPtr<GstPad> srcPad = adoptGRef(gst_element_get_request_pad(videoTee, "src%d"));
2253     GRefPtr<GstPad> sinkPad = adoptGRef(gst_element_get_static_pad(actualVideoSink, "sink"));
2254     gst_pad_link(srcPad.get(), sinkPad.get());
2255 #else
2256     // Faster elements linking.
2257     gst_element_link_pads_full(queue, "src", actualVideoSink, "sink", GST_PAD_LINK_CHECK_NOTHING);
2258 #endif
2259     // Add a ghostpad to the bin so it can proxy to tee.
2260     GRefPtr<GstPad> pad = adoptGRef(gst_element_get_static_pad(videoTee, "sink"));
2261     gst_element_add_pad(m_videoSinkBin, gst_ghost_pad_new("sink", pad.get()));
2262
2263     // Set the bin as video sink of playbin.
2264     return m_videoSinkBin;
2265 #else
2266     return actualVideoSink;
2267 #endif
2268 }
2269 #endif
2270
2271 void MediaPlayerPrivateGStreamer::createGSTPlayBin()
2272 {
2273     ASSERT(!m_playBin);
2274
2275     // gst_element_factory_make() returns a floating reference so
2276     // we should not adopt.
2277     m_playBin = gst_element_factory_make(gPlaybinName, "play");
2278
2279 #ifndef GST_API_VERSION_1
2280     m_gstGWorld = GStreamerGWorld::createGWorld(m_playBin.get());
2281 #endif
2282
2283     GRefPtr<GstBus> bus = webkitGstPipelineGetBus(GST_PIPELINE(m_playBin.get()));
2284     gst_bus_add_signal_watch(bus.get());
2285     g_signal_connect(bus.get(), "message", G_CALLBACK(mediaPlayerPrivateMessageCallback), this);
2286 #if ENABLE(TIZEN_ACCELERATED_COMPOSITING) && USE(TIZEN_TEXTURE_MAPPER) && ENABLE(TIZEN_WEBKIT2_TILED_AC_SHARED_PLATFORM_SURFACE)
2287     gst_bus_set_sync_handler(bus.get(), GstBusSyncHandler(mediaPlayerPrivateSyncHandler), this);
2288 #endif
2289     gst_object_unref(bus.get());
2290
2291     g_object_set(m_playBin.get(), "mute", m_player->muted(), NULL);
2292
2293     g_signal_connect(m_playBin.get(), "notify::volume", G_CALLBACK(mediaPlayerPrivateVolumeChangedCallback), this);
2294     g_signal_connect(m_playBin.get(), "notify::source", G_CALLBACK(mediaPlayerPrivateSourceChangedCallback), this);
2295     g_signal_connect(m_playBin.get(), "notify::mute", G_CALLBACK(mediaPlayerPrivateMuteChangedCallback), this);
2296     g_signal_connect(m_playBin.get(), "video-changed", G_CALLBACK(mediaPlayerPrivateVideoChangedCallback), this);
2297     g_signal_connect(m_playBin.get(), "audio-changed", G_CALLBACK(mediaPlayerPrivateAudioChangedCallback), this);
2298 #if ENABLE(TIZEN_GSTREAMER_VIDEO)
2299     g_signal_connect(m_playBin.get(), "source-setup", G_CALLBACK(mediaPlayerPrivateSourceSetupCallback), this);
2300 #endif
2301
2302 #if ENABLE(TIZEN_GSTREAMER_VIDEO)
2303     HTMLMediaElement* element = static_cast<HTMLMediaElement*>(player()->mediaPlayerClient());
2304     if (element->isVideo()) {
2305         GstElement* videoElement = createVideoSink();
2306         g_object_set(m_playBin.get(), "video-sink", videoElement, NULL);
2307
2308         GRefPtr<GstPad> videoSinkPad = adoptGRef(gst_element_get_static_pad(m_webkitVideoSink, "sink"));
2309         if (videoSinkPad)
2310             g_signal_connect(videoSinkPad.get(), "notify::caps", G_CALLBACK(mediaPlayerPrivateVideoSinkCapsChangedCallback), this);
2311     } else {
2312         GstElement* fakeSink = gst_element_factory_make("fakesink", 0);
2313         g_object_set(fakeSink, "sync", true, NULL);
2314         g_object_set(m_playBin.get(), "video-sink", fakeSink, NULL);
2315     }
2316 #endif
2317 #if ENABLE(TIZEN_GSTREAMER_AUDIO)
2318     GstElement* realSink = gst_element_factory_make("avsysaudiosink", 0);
2319     g_object_set(realSink, "close-handle-on-prepare", 1, NULL);
2320     g_object_set(m_playBin.get(), "audio-sink", realSink, NULL);
2321
2322     if (m_audioSessionManager)
2323         m_audioSessionManager->registerAudioSessionManager(MM_SESSION_TYPE_SHARE, mediaPlayerPrivateAudioSessionNotifyCallback, player());
2324 #else
2325     createAudioSink();
2326 #endif
2327 }
2328
2329 #if ENABLE(TIZEN_ACCELERATED_COMPOSITING) && USE(TIZEN_TEXTURE_MAPPER)
2330 bool MediaPlayerPrivateGStreamer::supportsAcceleratedRendering() const
2331 {
2332     bool isEmbedVideo = false;
2333     if (m_player->mediaPlayerClient()) {
2334         Document* document = m_player->mediaPlayerClient()->mediaPlayerOwningDocument();
2335         if (document && document->settings()) {
2336             isEmbedVideo = document->settings()->acceleratedCompositingForVideoEnabled()
2337                                 && document->settings()->acceleratedCompositingEnabled();
2338             LOG_MEDIA_MESSAGE("isEmbedVideo: %d", isEmbedVideo);
2339         }
2340     }
2341
2342     return isEmbedVideo;
2343 }
2344
2345 PlatformLayer* MediaPlayerPrivateGStreamer::platformLayer() const
2346 {
2347     return m_videoLayer->platformLayer();
2348 }
2349
2350 #if ENABLE(TIZEN_WEBKIT2_TILED_AC_SHARED_PLATFORM_SURFACE)
2351 void MediaPlayerPrivateGStreamer::paintCurrentFrameInContext(GraphicsContext* context, const IntRect& rect)
2352 {
2353     m_videoLayer->paintCurrentFrameInContext(context, rect);
2354 }
2355
2356 #if USE(ACCELERATED_VIDEO_VAAPI)
2357 // Other device doesn't prefer such optimization, so enclose it in USE(ACCELERATED_VIDEO_VAAPI) macro
2358 IntSize MediaPlayerPrivateGStreamer::scaleHDVideoToDisplaySize(int videoWidth, int videoHeight, int displayWidth, int displayHeight) const
2359 {
2360     int fitWidth, fitHeight;
2361
2362 #if ENABLE(TIZEN_DLOG_SUPPORT)
2363     TIZEN_LOGI("videoWidth: %d, videoHeight: %d, displayWidth: %d, displayHeight: %d ", videoWidth, videoHeight, displayWidth, displayHeight);
2364 #endif
2365     fitWidth = videoWidth;
2366     fitHeight = videoHeight;
2367     if (displayWidth > 0 && displayHeight > 0) {
2368         // In case video is rotated, we always use a 'better'
2369         // orientation (landscape or portrait) to calculate fitWidth/fitHeight.
2370         // It means we use a bigger size which doesn't lose quality
2371         // in either landscape or portrait orientation
2372         if ((videoWidth > videoHeight && displayWidth < displayHeight)
2373             || (videoWidth < videoHeight && displayWidth > displayHeight)) {
2374             int tmp;
2375             tmp = displayWidth;
2376             displayWidth = displayHeight;
2377             displayHeight = tmp;
2378         }
2379
2380         if (videoWidth > displayWidth || videoHeight > displayHeight) {
2381             if (videoWidth * displayHeight > videoHeight * displayWidth) {
2382                 fitWidth = displayWidth;
2383                 fitHeight = videoHeight * displayWidth / videoWidth;
2384                 ASSERT(fitHeight <= displayHeight);
2385             } else {
2386                 fitWidth = videoWidth * displayHeight / videoHeight;
2387                 fitHeight = displayHeight;
2388                 ASSERT(fitWidth <= displayWidth);
2389             }
2390         }
2391     }
2392
2393     return IntSize(fitWidth, fitHeight);
2394 }
2395 #endif
2396 void MediaPlayerPrivateGStreamer::xWindowIdPrepared(GstMessage* message)
2397 {
2398     // It is called in streaming thread.
2399     // It should be used only to set window handle to video sink.
2400
2401     int videoWidth, videoHeight;
2402     gst_structure_get_int(message->structure, "video-width", &videoWidth);
2403     gst_structure_get_int(message->structure, "video-height", &videoHeight);
2404
2405 #if ENABLE(TIZEN_DLOG_SUPPORT)
2406     TIZEN_LOGI("videoWidth: %d, videoHeight: %d", videoWidth, videoHeight);
2407 #endif
2408
2409 #if USE(ACCELERATED_VIDEO_VAAPI)
2410     int displayWidth, displayHeight;
2411     gst_structure_get_int(message->structure, "display-width", &displayWidth);
2412     gst_structure_get_int(message->structure, "display-height", &displayHeight);
2413
2414     m_displaySize = IntSize(displayWidth, displayHeight);
2415     m_videoSize = scaleHDVideoToDisplaySize(videoWidth, videoHeight, displayWidth, displayHeight);
2416 #else
2417     m_videoSize = IntSize(videoWidth, videoHeight);
2418 #endif
2419     m_videoLayer->setOverlay(m_videoSize);
2420 }
2421
2422 #endif // ENABLE(TIZEN_WEBKIT2_TILED_AC_SHARED_PLATFORM_SURFACE)
2423 #endif // ENABLE(TIZEN_ACCELERATED_COMPOSITING) && USE(TIZEN_TEXTURE_MAPPER)
2424
2425 #if ENABLE(TIZEN_WEBKIT2_PROXY)
2426 void MediaPlayerPrivateGStreamer::setProxy(GstElement* source)
2427 {
2428     SoupSession* session = WebCore::ResourceHandle::defaultSession();
2429     if (!session)
2430         return;
2431
2432     SoupURI* proxyUri = 0;
2433     g_object_get(session, SOUP_SESSION_PROXY_URI, &proxyUri, NULL);
2434     if (!proxyUri)
2435         return;
2436
2437     char* proxy = soup_uri_to_string(proxyUri, false);
2438     g_object_set(source, "proxy", proxy, NULL);
2439
2440     soup_uri_free(proxyUri);
2441     g_free(proxy);
2442 }
2443 #endif
2444
2445 #if ENABLE(TIZEN_GSTREAMER_VIDEO)
2446 void MediaPlayerPrivateGStreamer::suspend()
2447 {
2448     m_suspendTime = currentTime();
2449     gst_element_set_state(m_playBin.get(), GST_STATE_NULL);
2450 #if ENABLE(TIZEN_GSTREAMER_AUDIO)
2451     if (m_audioSessionManager)
2452         m_audioSessionManager->setSoundState(ASM_STATE_STOP);
2453 #endif
2454 }
2455
2456 void MediaPlayerPrivateGStreamer::resume()
2457 {
2458     if (isLocalMediaStream()) {
2459         gst_element_set_state(m_playBin.get(), GST_STATE_PLAYING);
2460 #if ENABLE(TIZEN_GSTREAMER_AUDIO)
2461         if (m_audioSessionManager)
2462             m_audioSessionManager->setSoundState(ASM_STATE_PLAYING);
2463 #endif
2464     } else {
2465         gst_element_set_state(m_playBin.get(), GST_STATE_PAUSED);
2466 #if ENABLE(TIZEN_GSTREAMER_AUDIO)
2467         if (m_audioSessionManager)
2468             m_audioSessionManager->setSoundState(ASM_STATE_PAUSE);
2469 #endif
2470     }
2471 }
2472
2473 bool MediaPlayerPrivateGStreamer::isLocalMediaStream()
2474 {
2475     if (m_url.string().contains("camera://"))
2476         return true;
2477     else
2478         return false;
2479 }
2480 #endif // ENABLE(TIZEN_GSTREAMER_VIDEO)
2481
2482 }
2483
2484 #endif // USE(GSTREAMER)