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