[GStreamer] cannot play live streams
[WebKit-https.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, 2011, 2012, 2013 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 "GStreamerUtilities.h"
30 #include "GStreamerVersioning.h"
31 #include "KURL.h"
32 #include "Logging.h"
33 #include "MIMETypeRegistry.h"
34 #include "MediaPlayer.h"
35 #include "NotImplemented.h"
36 #include "SecurityOrigin.h"
37 #include "TimeRanges.h"
38 #include "WebKitWebSourceGStreamer.h"
39 #include <gst/gst.h>
40 #include <gst/pbutils/missing-plugins.h>
41 #include <limits>
42 #include <wtf/gobject/GOwnPtr.h>
43 #include <wtf/text/CString.h>
44
45 #if ENABLE(VIDEO_TRACK) && defined(GST_API_VERSION_1)
46 #include "InbandTextTrackPrivateGStreamer.h"
47 #include "TextCombinerGStreamer.h"
48 #include "TextSinkGStreamer.h"
49 #endif
50
51 #ifdef GST_API_VERSION_1
52 #include <gst/audio/streamvolume.h>
53 #else
54 #include <gst/interfaces/streamvolume.h>
55 #endif
56
57 // GstPlayFlags flags from playbin2. It is the policy of GStreamer to
58 // not publicly expose element-specific enums. That's why this
59 // GstPlayFlags enum has been copied here.
60 typedef enum {
61     GST_PLAY_FLAG_VIDEO         = 0x00000001,
62     GST_PLAY_FLAG_AUDIO         = 0x00000002,
63     GST_PLAY_FLAG_TEXT          = 0x00000004,
64     GST_PLAY_FLAG_VIS           = 0x00000008,
65     GST_PLAY_FLAG_SOFT_VOLUME   = 0x00000010,
66     GST_PLAY_FLAG_NATIVE_AUDIO  = 0x00000020,
67     GST_PLAY_FLAG_NATIVE_VIDEO  = 0x00000040,
68     GST_PLAY_FLAG_DOWNLOAD      = 0x00000080,
69     GST_PLAY_FLAG_BUFFERING     = 0x000000100
70 } GstPlayFlags;
71
72 // gPercentMax is used when parsing buffering ranges with
73 // gst_query_parse_nth_buffering_range as there was a bug in GStreamer
74 // 0.10 that was using 100 instead of GST_FORMAT_PERCENT_MAX. This was
75 // corrected in 1.0. gst_query_parse_buffering_range worked as
76 // expected with GST_FORMAT_PERCENT_MAX in both cases.
77 #ifdef GST_API_VERSION_1
78 static const char* gPlaybinName = "playbin";
79 static const gint64 gPercentMax = GST_FORMAT_PERCENT_MAX;
80 #else
81 static const char* gPlaybinName = "playbin2";
82 static const gint64 gPercentMax = 100;
83 #endif
84 // Max interval in seconds to stay in the READY state on manual
85 // state change requests.
86 static const guint gReadyStateTimerInterval = 60;
87
88 GST_DEBUG_CATEGORY_EXTERN(webkit_media_player_debug);
89 #define GST_CAT_DEFAULT webkit_media_player_debug
90
91 using namespace std;
92
93 namespace WebCore {
94
95 static gboolean mediaPlayerPrivateMessageCallback(GstBus*, GstMessage* message, MediaPlayerPrivateGStreamer* player)
96 {
97     return player->handleMessage(message);
98 }
99
100 static void mediaPlayerPrivateSourceChangedCallback(GObject*, GParamSpec*, MediaPlayerPrivateGStreamer* player)
101 {
102     player->sourceChanged();
103 }
104
105 static void mediaPlayerPrivateVideoSinkCapsChangedCallback(GObject*, GParamSpec*, MediaPlayerPrivateGStreamer* player)
106 {
107     player->videoChanged();
108 }
109
110 static void mediaPlayerPrivateVideoChangedCallback(GObject*, MediaPlayerPrivateGStreamer* player)
111 {
112     player->videoChanged();
113 }
114
115 static void mediaPlayerPrivateAudioChangedCallback(GObject*, MediaPlayerPrivateGStreamer* player)
116 {
117     player->audioChanged();
118 }
119
120 static gboolean mediaPlayerPrivateAudioChangeTimeoutCallback(MediaPlayerPrivateGStreamer* player)
121 {
122     // This is the callback of the timeout source created in ::audioChanged.
123     player->notifyPlayerOfAudio();
124     return FALSE;
125 }
126
127 #ifdef GST_API_VERSION_1
128 static void setAudioStreamPropertiesCallback(GstChildProxy*, GObject* object, gchar*,
129     MediaPlayerPrivateGStreamer* player)
130 #else
131 static void setAudioStreamPropertiesCallback(GstChildProxy*, GObject* object, MediaPlayerPrivateGStreamer* player)
132 #endif
133 {
134     player->setAudioStreamProperties(object);
135 }
136
137 static gboolean mediaPlayerPrivateVideoChangeTimeoutCallback(MediaPlayerPrivateGStreamer* player)
138 {
139     // This is the callback of the timeout source created in ::videoChanged.
140     player->notifyPlayerOfVideo();
141     return FALSE;
142 }
143
144 #if ENABLE(VIDEO_TRACK) && defined(GST_API_VERSION_1)
145 static void mediaPlayerPrivateTextChangedCallback(GObject*, MediaPlayerPrivateGStreamer* player)
146 {
147     player->textChanged();
148 }
149
150 static gboolean mediaPlayerPrivateTextChangeTimeoutCallback(MediaPlayerPrivateGStreamer* player)
151 {
152     // This is the callback of the timeout source created in ::textChanged.
153     player->notifyPlayerOfText();
154     return FALSE;
155 }
156
157 static GstFlowReturn mediaPlayerPrivateNewTextSampleCallback(GObject*, MediaPlayerPrivateGStreamer* player)
158 {
159     player->newTextSample();
160     return GST_FLOW_OK;
161 }
162 #endif
163
164 static gboolean mediaPlayerPrivateReadyStateTimeoutCallback(MediaPlayerPrivateGStreamer* player)
165 {
166     // This is the callback of the timeout source created in ::changePipelineState.
167     // Reset pipeline if we are sitting on READY state when timeout is reached
168     player->changePipelineState(GST_STATE_NULL);
169     return FALSE;
170 }
171
172 static void mediaPlayerPrivatePluginInstallerResultFunction(GstInstallPluginsReturn result, gpointer userData)
173 {
174     MediaPlayerPrivateGStreamer* player = reinterpret_cast<MediaPlayerPrivateGStreamer*>(userData);
175     player->handlePluginInstallerResult(result);
176 }
177
178 static GstClockTime toGstClockTime(float time)
179 {
180     // Extract the integer part of the time (seconds) and the fractional part (microseconds). Attempt to
181     // round the microseconds so no floating point precision is lost and we can perform an accurate seek.
182     float seconds;
183     float microSeconds = modf(time, &seconds) * 1000000;
184     GTimeVal timeValue;
185     timeValue.tv_sec = static_cast<glong>(seconds);
186     timeValue.tv_usec = static_cast<glong>(roundf(microSeconds / 10000) * 10000);
187     return GST_TIMEVAL_TO_TIME(timeValue);
188 }
189
190 void MediaPlayerPrivateGStreamer::setAudioStreamProperties(GObject* object)
191 {
192     if (g_strcmp0(G_OBJECT_TYPE_NAME(object), "GstPulseSink"))
193         return;
194
195     const char* role = m_player->mediaPlayerClient() && m_player->mediaPlayerClient()->mediaPlayerIsVideo()
196         ? "video" : "music";
197     GstStructure* structure = gst_structure_new("stream-properties", "media.role", G_TYPE_STRING, role, NULL);
198     g_object_set(object, "stream-properties", structure, NULL);
199     gst_structure_free(structure);
200     GOwnPtr<gchar> elementName(gst_element_get_name(GST_ELEMENT(object)));
201     LOG_MEDIA_MESSAGE("Set media.role as %s at %s", role, elementName.get());
202 }
203
204 PassOwnPtr<MediaPlayerPrivateInterface> MediaPlayerPrivateGStreamer::create(MediaPlayer* player)
205 {
206     return adoptPtr(new MediaPlayerPrivateGStreamer(player));
207 }
208
209 void MediaPlayerPrivateGStreamer::registerMediaEngine(MediaEngineRegistrar registrar)
210 {
211     if (isAvailable())
212         registrar(create, getSupportedTypes, supportsType, 0, 0, 0);
213 }
214
215 bool initializeGStreamerAndRegisterWebKitElements()
216 {
217     if (!initializeGStreamer())
218         return false;
219
220     GRefPtr<GstElementFactory> srcFactory = gst_element_factory_find("webkitwebsrc");
221     if (!srcFactory) {
222         GST_DEBUG_CATEGORY_INIT(webkit_media_player_debug, "webkitmediaplayer", 0, "WebKit media player");
223         return gst_element_register(0, "webkitwebsrc", GST_RANK_PRIMARY + 100, WEBKIT_TYPE_WEB_SRC);
224     }
225
226     return true;
227 }
228
229 bool MediaPlayerPrivateGStreamer::isAvailable()
230 {
231     if (!initializeGStreamerAndRegisterWebKitElements())
232         return false;
233
234     GRefPtr<GstElementFactory> factory = gst_element_factory_find(gPlaybinName);
235     return factory;
236 }
237
238 MediaPlayerPrivateGStreamer::MediaPlayerPrivateGStreamer(MediaPlayer* player)
239     : MediaPlayerPrivateGStreamerBase(player)
240     , m_source(0)
241     , m_seekTime(0)
242     , m_changingRate(false)
243     , m_endTime(numeric_limits<float>::infinity())
244     , m_isEndReached(false)
245     , m_isStreaming(false)
246     , m_mediaLocations(0)
247     , m_mediaLocationCurrentIndex(0)
248     , m_resetPipeline(false)
249     , m_paused(true)
250     , m_seeking(false)
251     , m_seekIsPending(false)
252     , m_timeOfOverlappingSeek(-1)
253     , m_buffering(false)
254     , m_playbackRate(1)
255     , m_errorOccured(false)
256     , m_mediaDuration(0)
257     , m_downloadFinished(false)
258     , m_fillTimer(this, &MediaPlayerPrivateGStreamer::fillTimerFired)
259     , m_maxTimeLoaded(0)
260     , m_bufferingPercentage(0)
261     , m_preload(player->preload())
262     , m_delayingLoad(false)
263     , m_mediaDurationKnown(true)
264     , m_maxTimeLoadedAtLastDidLoadingProgress(0)
265     , m_volumeAndMuteInitialized(false)
266     , m_hasVideo(false)
267     , m_hasAudio(false)
268     , m_audioTimerHandler(0)
269     , m_textTimerHandler(0)
270     , m_videoTimerHandler(0)
271     , m_readyTimerHandler(0)
272     , m_webkitAudioSink(0)
273     , m_totalBytes(-1)
274     , m_preservesPitch(false)
275     , m_requestedState(GST_STATE_VOID_PENDING)
276     , m_missingPlugins(false)
277 {
278 }
279
280 MediaPlayerPrivateGStreamer::~MediaPlayerPrivateGStreamer()
281 {
282 #if ENABLE(VIDEO_TRACK) && defined(GST_API_VERSION_1)
283     for (size_t i = 0; i < m_textTracks.size(); ++i)
284         m_textTracks[i]->disconnect();
285 #endif
286     if (m_fillTimer.isActive())
287         m_fillTimer.stop();
288
289     if (m_mediaLocations) {
290         gst_structure_free(m_mediaLocations);
291         m_mediaLocations = 0;
292     }
293
294     if (m_autoAudioSink)
295         g_signal_handlers_disconnect_by_func(G_OBJECT(m_autoAudioSink.get()),
296             reinterpret_cast<gpointer>(setAudioStreamPropertiesCallback), this);
297
298     if (m_readyTimerHandler)
299         g_source_remove(m_readyTimerHandler);
300
301     if (m_playBin) {
302         GRefPtr<GstBus> bus = webkitGstPipelineGetBus(GST_PIPELINE(m_playBin.get()));
303         ASSERT(bus);
304         g_signal_handlers_disconnect_by_func(bus.get(), reinterpret_cast<gpointer>(mediaPlayerPrivateMessageCallback), this);
305         gst_bus_remove_signal_watch(bus.get());
306
307         g_signal_handlers_disconnect_by_func(m_playBin.get(), reinterpret_cast<gpointer>(mediaPlayerPrivateSourceChangedCallback), this);
308         g_signal_handlers_disconnect_by_func(m_playBin.get(), reinterpret_cast<gpointer>(mediaPlayerPrivateVideoChangedCallback), this);
309         g_signal_handlers_disconnect_by_func(m_playBin.get(), reinterpret_cast<gpointer>(mediaPlayerPrivateAudioChangedCallback), this);
310 #if ENABLE(VIDEO_TRACK) && defined(GST_API_VERSION_1)
311         g_signal_handlers_disconnect_by_func(m_playBin.get(), reinterpret_cast<gpointer>(mediaPlayerPrivateNewTextSampleCallback), this);
312         g_signal_handlers_disconnect_by_func(m_playBin.get(), reinterpret_cast<gpointer>(mediaPlayerPrivateTextChangedCallback), this);
313 #endif
314
315         gst_element_set_state(m_playBin.get(), GST_STATE_NULL);
316         m_playBin.clear();
317     }
318
319     GRefPtr<GstPad> videoSinkPad = adoptGRef(gst_element_get_static_pad(m_webkitVideoSink.get(), "sink"));
320     g_signal_handlers_disconnect_by_func(videoSinkPad.get(), reinterpret_cast<gpointer>(mediaPlayerPrivateVideoSinkCapsChangedCallback), this);
321
322     if (m_videoTimerHandler)
323         g_source_remove(m_videoTimerHandler);
324
325     if (m_audioTimerHandler)
326         g_source_remove(m_audioTimerHandler);
327
328     if (m_textTimerHandler)
329         g_source_remove(m_textTimerHandler);
330 }
331
332 void MediaPlayerPrivateGStreamer::load(const String& url)
333 {
334     if (!initializeGStreamerAndRegisterWebKitElements())
335         return;
336
337     KURL kurl(KURL(), url);
338     String cleanUrl(url);
339
340     // Clean out everything after file:// url path.
341     if (kurl.isLocalFile())
342         cleanUrl = cleanUrl.substring(0, kurl.pathEnd());
343
344     if (!m_playBin)
345         createGSTPlayBin();
346
347     ASSERT(m_playBin);
348
349     m_url = KURL(KURL(), cleanUrl);
350     g_object_set(m_playBin.get(), "uri", cleanUrl.utf8().data(), NULL);
351
352     INFO_MEDIA_MESSAGE("Load %s", cleanUrl.utf8().data());
353
354     if (m_preload == MediaPlayer::None) {
355         LOG_MEDIA_MESSAGE("Delaying load.");
356         m_delayingLoad = true;
357     }
358
359     // Reset network and ready states. Those will be set properly once
360     // the pipeline pre-rolled.
361     m_networkState = MediaPlayer::Loading;
362     m_player->networkStateChanged();
363     m_readyState = MediaPlayer::HaveNothing;
364     m_player->readyStateChanged();
365     m_volumeAndMuteInitialized = false;
366
367     if (!m_delayingLoad)
368         commitLoad();
369 }
370
371 #if ENABLE(MEDIA_SOURCE)
372 void MediaPlayerPrivateGStreamer::load(const String& url, PassRefPtr<MediaSource>)
373 {
374     notImplemented();
375 }
376 #endif
377
378 void MediaPlayerPrivateGStreamer::commitLoad()
379 {
380     ASSERT(!m_delayingLoad);
381     LOG_MEDIA_MESSAGE("Committing load.");
382
383     // GStreamer needs to have the pipeline set to a paused state to
384     // start providing anything useful.
385     changePipelineState(GST_STATE_PAUSED);
386
387     setDownloadBuffering();
388     updateStates();
389 }
390
391 float MediaPlayerPrivateGStreamer::playbackPosition() const
392 {
393     if (m_isEndReached) {
394         // Position queries on a null pipeline return 0. If we're at
395         // the end of the stream the pipeline is null but we want to
396         // report either the seek time or the duration because this is
397         // what the Media element spec expects us to do.
398         if (m_seeking)
399             return m_seekTime;
400         if (m_mediaDuration)
401             return m_mediaDuration;
402         return 0;
403     }
404
405     // Position is only available if no async state change is going on and the state is either paused or playing.
406     gint64 position = GST_CLOCK_TIME_NONE;
407     GstQuery* query= gst_query_new_position(GST_FORMAT_TIME);
408     if (gst_element_query(m_playBin.get(), query))
409         gst_query_parse_position(query, 0, &position);
410
411     float result = 0.0f;
412     if (static_cast<GstClockTime>(position) != GST_CLOCK_TIME_NONE)
413         result = static_cast<double>(position) / GST_SECOND;
414     else if (m_canFallBackToLastFinishedSeekPositon)
415         result = m_seekTime;
416
417     LOG_MEDIA_MESSAGE("Position %" GST_TIME_FORMAT, GST_TIME_ARGS(position));
418
419     gst_query_unref(query);
420
421     return result;
422 }
423
424 bool MediaPlayerPrivateGStreamer::changePipelineState(GstState newState)
425 {
426     ASSERT(m_playBin);
427
428     GstState currentState;
429     GstState pending;
430
431     gst_element_get_state(m_playBin.get(), &currentState, &pending, 0);
432     if (currentState == newState || pending == newState) {
433         LOG_MEDIA_MESSAGE("Rejected state change to %s from %s with %s pending", gst_element_state_get_name(newState),
434             gst_element_state_get_name(currentState), gst_element_state_get_name(pending));
435         return true;
436     }
437
438     LOG_MEDIA_MESSAGE("Changing state change to %s from %s with %s pending", gst_element_state_get_name(newState),
439         gst_element_state_get_name(currentState), gst_element_state_get_name(pending));
440
441     GstStateChangeReturn setStateResult = gst_element_set_state(m_playBin.get(), newState);
442     GstState pausedOrPlaying = newState == GST_STATE_PLAYING ? GST_STATE_PAUSED : GST_STATE_PLAYING;
443     if (currentState != pausedOrPlaying && setStateResult == GST_STATE_CHANGE_FAILURE) {
444         loadingFailed(MediaPlayer::Empty);
445         return false;
446     }
447
448     // Create a timer when entering the READY state so that we can free resources
449     // if we stay for too long on READY.
450     // Also lets remove the timer if we request a state change for any state other than READY.
451     // See also https://bugs.webkit.org/show_bug.cgi?id=117354
452     if (newState == GST_STATE_READY && !m_readyTimerHandler) {
453         m_readyTimerHandler = g_timeout_add_seconds(gReadyStateTimerInterval, reinterpret_cast<GSourceFunc>(mediaPlayerPrivateReadyStateTimeoutCallback), this);
454     } else if (newState != GST_STATE_READY && m_readyTimerHandler) {
455         g_source_remove(m_readyTimerHandler);
456         m_readyTimerHandler = 0;
457     }
458
459     return true;
460 }
461
462 void MediaPlayerPrivateGStreamer::prepareToPlay()
463 {
464     m_preload = MediaPlayer::Auto;
465     if (m_delayingLoad) {
466         m_delayingLoad = false;
467         commitLoad();
468     }
469 }
470
471 void MediaPlayerPrivateGStreamer::play()
472 {
473     if (changePipelineState(GST_STATE_PLAYING)) {
474         m_isEndReached = false;
475         m_delayingLoad = false;
476         m_preload = MediaPlayer::Auto;
477         setDownloadBuffering();
478         LOG_MEDIA_MESSAGE("Play");
479     }
480 }
481
482 void MediaPlayerPrivateGStreamer::pause()
483 {
484     GstState currentState, pendingState;
485     gst_element_get_state(m_playBin.get(), &currentState, &pendingState, 0);
486     if (currentState < GST_STATE_PAUSED && pendingState <= GST_STATE_PAUSED)
487         return;
488
489     if (changePipelineState(GST_STATE_PAUSED))
490         INFO_MEDIA_MESSAGE("Pause");
491 }
492
493 float MediaPlayerPrivateGStreamer::duration() const
494 {
495     if (!m_playBin)
496         return 0.0f;
497
498     if (m_errorOccured)
499         return 0.0f;
500
501     // Media duration query failed already, don't attempt new useless queries.
502     if (!m_mediaDurationKnown)
503         return numeric_limits<float>::infinity();
504
505     if (m_mediaDuration)
506         return m_mediaDuration;
507
508     GstFormat timeFormat = GST_FORMAT_TIME;
509     gint64 timeLength = 0;
510
511 #ifdef GST_API_VERSION_1
512     bool failure = !gst_element_query_duration(m_playBin.get(), timeFormat, &timeLength) || static_cast<guint64>(timeLength) == GST_CLOCK_TIME_NONE;
513 #else
514     bool failure = !gst_element_query_duration(m_playBin.get(), &timeFormat, &timeLength) || timeFormat != GST_FORMAT_TIME || static_cast<guint64>(timeLength) == GST_CLOCK_TIME_NONE;
515 #endif
516     if (failure) {
517         LOG_MEDIA_MESSAGE("Time duration query failed for %s", m_url.string().utf8().data());
518         return numeric_limits<float>::infinity();
519     }
520
521     LOG_MEDIA_MESSAGE("Duration: %" GST_TIME_FORMAT, GST_TIME_ARGS(timeLength));
522
523     m_mediaDuration = static_cast<double>(timeLength) / GST_SECOND;
524     return m_mediaDuration;
525     // FIXME: handle 3.14.9.5 properly
526 }
527
528 float MediaPlayerPrivateGStreamer::currentTime() const
529 {
530     if (!m_playBin)
531         return 0.0f;
532
533     if (m_errorOccured)
534         return 0.0f;
535
536     if (m_seeking)
537         return m_seekTime;
538
539     // Workaround for
540     // https://bugzilla.gnome.org/show_bug.cgi?id=639941 In GStreamer
541     // 0.10.35 basesink reports wrong duration in case of EOS and
542     // negative playback rate. There's no upstream accepted patch for
543     // this bug yet, hence this temporary workaround.
544     if (m_isEndReached && m_playbackRate < 0)
545         return 0.0f;
546
547     return playbackPosition();
548 }
549
550 void MediaPlayerPrivateGStreamer::seek(float time)
551 {
552     if (!m_playBin)
553         return;
554
555     if (m_errorOccured)
556         return;
557
558     INFO_MEDIA_MESSAGE("[Seek] seek attempt to %f secs", time);
559
560     // Avoid useless seeking.
561     if (time == currentTime())
562         return;
563
564     if (isLiveStream())
565         return;
566
567     GstClockTime clockTime = toGstClockTime(time);
568     INFO_MEDIA_MESSAGE("[Seek] seeking to %" GST_TIME_FORMAT " (%f)", GST_TIME_ARGS(clockTime), time);
569
570     if (m_seeking) {
571         m_timeOfOverlappingSeek = time;
572         if (m_seekIsPending) {
573             m_seekTime = time;
574             return;
575         }
576     }
577
578     GstState state;
579     GstStateChangeReturn getStateResult = gst_element_get_state(m_playBin.get(), &state, 0, 0);
580     if (getStateResult == GST_STATE_CHANGE_FAILURE || getStateResult == GST_STATE_CHANGE_NO_PREROLL) {
581         LOG_MEDIA_MESSAGE("[Seek] cannot seek, current state change is %s", gst_element_state_change_return_get_name(getStateResult));
582         return;
583     }
584     if (getStateResult == GST_STATE_CHANGE_ASYNC || state < GST_STATE_PAUSED || m_isEndReached) {
585         m_seekIsPending = true;
586         if (m_isEndReached) {
587             LOG_MEDIA_MESSAGE("[Seek] reset pipeline");
588             m_resetPipeline = true;
589             changePipelineState(GST_STATE_PAUSED);
590         }
591     } else {
592         // We can seek now.
593         if (!gst_element_seek(m_playBin.get(), m_player->rate(), GST_FORMAT_TIME, static_cast<GstSeekFlags>(GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_ACCURATE),
594             GST_SEEK_TYPE_SET, clockTime, GST_SEEK_TYPE_NONE, GST_CLOCK_TIME_NONE)) {
595             LOG_MEDIA_MESSAGE("[Seek] seeking to %f failed", time);
596             return;
597         }
598     }
599
600     m_seeking = true;
601     m_seekTime = time;
602     m_isEndReached = false;
603 }
604
605 bool MediaPlayerPrivateGStreamer::paused() const
606 {
607     if (m_isEndReached) {
608         LOG_MEDIA_MESSAGE("Ignoring pause at EOS");
609         return true;
610     }
611
612     GstState state;
613     gst_element_get_state(m_playBin.get(), &state, 0, 0);
614     return state == GST_STATE_PAUSED;
615 }
616
617 bool MediaPlayerPrivateGStreamer::seeking() const
618 {
619     return m_seeking;
620 }
621
622 void MediaPlayerPrivateGStreamer::videoChanged()
623 {
624     if (m_videoTimerHandler)
625         g_source_remove(m_videoTimerHandler);
626     m_videoTimerHandler = g_timeout_add(0, reinterpret_cast<GSourceFunc>(mediaPlayerPrivateVideoChangeTimeoutCallback), this);
627 }
628
629 void MediaPlayerPrivateGStreamer::notifyPlayerOfVideo()
630 {
631     m_videoTimerHandler = 0;
632
633     gint videoTracks = 0;
634     if (m_playBin)
635         g_object_get(m_playBin.get(), "n-video", &videoTracks, NULL);
636
637     m_hasVideo = videoTracks > 0;
638
639     m_videoSize = IntSize();
640
641     m_player->mediaPlayerClient()->mediaPlayerEngineUpdated(m_player);
642 }
643
644 void MediaPlayerPrivateGStreamer::audioChanged()
645 {
646     if (m_audioTimerHandler)
647         g_source_remove(m_audioTimerHandler);
648     m_audioTimerHandler = g_timeout_add(0, reinterpret_cast<GSourceFunc>(mediaPlayerPrivateAudioChangeTimeoutCallback), this);
649 }
650
651 void MediaPlayerPrivateGStreamer::notifyPlayerOfAudio()
652 {
653     m_audioTimerHandler = 0;
654
655     gint audioTracks = 0;
656     if (m_playBin)
657         g_object_get(m_playBin.get(), "n-audio", &audioTracks, NULL);
658     m_hasAudio = audioTracks > 0;
659     m_player->mediaPlayerClient()->mediaPlayerEngineUpdated(m_player);
660 }
661
662 #if ENABLE(VIDEO_TRACK) && defined(GST_API_VERSION_1)
663 void MediaPlayerPrivateGStreamer::textChanged()
664 {
665     if (m_textTimerHandler)
666         g_source_remove(m_textTimerHandler);
667     m_textTimerHandler = g_timeout_add(0, reinterpret_cast<GSourceFunc>(mediaPlayerPrivateTextChangeTimeoutCallback), this);
668 }
669
670 void MediaPlayerPrivateGStreamer::notifyPlayerOfText()
671 {
672     m_textTimerHandler = 0;
673
674     gint numTracks = 0;
675     if (m_playBin)
676         g_object_get(m_playBin.get(), "n-text", &numTracks, NULL);
677
678     for (gint i = 0; i < numTracks; ++i) {
679         GstPad* pad;
680         g_signal_emit_by_name(m_playBin.get(), "get-text-pad", i, &pad, NULL);
681         ASSERT(pad);
682
683         if (i < static_cast<gint>(m_textTracks.size())) {
684             RefPtr<InbandTextTrackPrivateGStreamer> existingTrack = m_textTracks[i];
685             existingTrack->setIndex(i);
686             if (existingTrack->pad() == pad) {
687                 gst_object_unref(pad);
688                 continue;
689             }
690         }
691
692         RefPtr<InbandTextTrackPrivateGStreamer> track = InbandTextTrackPrivateGStreamer::create(i, adoptGRef(pad));
693         m_textTracks.insert(i, track);
694         m_player->addTextTrack(track.release());
695     }
696
697     while (static_cast<gint>(m_textTracks.size()) > numTracks) {
698         RefPtr<InbandTextTrackPrivateGStreamer> track = m_textTracks.last();
699         track->disconnect();
700         m_textTracks.removeLast();
701         m_player->removeTextTrack(track.release());
702     }
703 }
704
705 void MediaPlayerPrivateGStreamer::newTextSample()
706 {
707     if (!m_textAppSink)
708         return;
709
710     GRefPtr<GstEvent> streamStartEvent = adoptGRef(
711         gst_pad_get_sticky_event(m_textAppSinkPad.get(), GST_EVENT_STREAM_START, 0));
712
713     GstSample* sample;
714     g_signal_emit_by_name(m_textAppSink.get(), "pull-sample", &sample, NULL);
715     ASSERT(sample);
716
717     if (streamStartEvent) {
718         bool found = FALSE;
719         const gchar* id;
720         gst_event_parse_stream_start(streamStartEvent.get(), &id);
721         for (size_t i = 0; i < m_textTracks.size(); ++i) {
722             RefPtr<InbandTextTrackPrivateGStreamer> track = m_textTracks[i];
723             if (track->streamId() == id) {
724                 track->handleSample(sample);
725                 found = true;
726                 break;
727             }
728         }
729         if (!found)
730             WARN_MEDIA_MESSAGE("Got sample with unknown stream ID.");
731     } else
732         WARN_MEDIA_MESSAGE("Unable to handle sample with no stream start event.");
733     gst_sample_unref(sample);
734 }
735 #endif
736
737 void MediaPlayerPrivateGStreamer::setRate(float rate)
738 {
739     // Avoid useless playback rate update.
740     if (m_playbackRate == rate)
741         return;
742
743     GstState state;
744     GstState pending;
745
746     gst_element_get_state(m_playBin.get(), &state, &pending, 0);
747     if ((state != GST_STATE_PLAYING && state != GST_STATE_PAUSED)
748         || (pending == GST_STATE_PAUSED))
749         return;
750
751     if (isLiveStream())
752         return;
753
754     m_playbackRate = rate;
755     m_changingRate = true;
756
757     if (!rate) {
758         changePipelineState(GST_STATE_PAUSED);
759         return;
760     }
761
762     float currentPosition = static_cast<float>(playbackPosition() * GST_SECOND);
763     GstSeekFlags flags = (GstSeekFlags)(GST_SEEK_FLAG_FLUSH);
764     gint64 start, end;
765     bool mute = false;
766
767     INFO_MEDIA_MESSAGE("Set Rate to %f", rate);
768     if (rate > 0) {
769         // Mute the sound if the playback rate is too extreme and
770         // audio pitch is not adjusted.
771         mute = (!m_preservesPitch && (rate < 0.8 || rate > 2));
772         start = currentPosition;
773         end = GST_CLOCK_TIME_NONE;
774     } else {
775         start = 0;
776         mute = true;
777
778         // If we are at beginning of media, start from the end to
779         // avoid immediate EOS.
780         if (currentPosition <= 0)
781             end = static_cast<gint64>(duration() * GST_SECOND);
782         else
783             end = currentPosition;
784     }
785
786     INFO_MEDIA_MESSAGE("Need to mute audio?: %d", (int) mute);
787
788     if (!gst_element_seek(m_playBin.get(), rate, GST_FORMAT_TIME, flags,
789                           GST_SEEK_TYPE_SET, start,
790                           GST_SEEK_TYPE_SET, end))
791         ERROR_MEDIA_MESSAGE("Set rate to %f failed", rate);
792     else
793         g_object_set(m_playBin.get(), "mute", mute, NULL);
794 }
795
796 void MediaPlayerPrivateGStreamer::setPreservesPitch(bool preservesPitch)
797 {
798     m_preservesPitch = preservesPitch;
799 }
800
801 PassRefPtr<TimeRanges> MediaPlayerPrivateGStreamer::buffered() const
802 {
803     RefPtr<TimeRanges> timeRanges = TimeRanges::create();
804     if (m_errorOccured || isLiveStream())
805         return timeRanges.release();
806
807 #if GST_CHECK_VERSION(0, 10, 31)
808     float mediaDuration(duration());
809     if (!mediaDuration || std::isinf(mediaDuration))
810         return timeRanges.release();
811
812     GstQuery* query = gst_query_new_buffering(GST_FORMAT_PERCENT);
813
814     if (!gst_element_query(m_playBin.get(), query)) {
815         gst_query_unref(query);
816         return timeRanges.release();
817     }
818
819     for (guint index = 0; index < gst_query_get_n_buffering_ranges(query); index++) {
820         gint64 rangeStart = 0, rangeStop = 0;
821         if (gst_query_parse_nth_buffering_range(query, index, &rangeStart, &rangeStop))
822             timeRanges->add(static_cast<float>((rangeStart * mediaDuration) / gPercentMax),
823                 static_cast<float>((rangeStop * mediaDuration) / gPercentMax));
824     }
825
826     // Fallback to the more general maxTimeLoaded() if no range has
827     // been found.
828     if (!timeRanges->length())
829         if (float loaded = maxTimeLoaded())
830             timeRanges->add(0, loaded);
831
832     gst_query_unref(query);
833 #else
834     float loaded = maxTimeLoaded();
835     if (!m_errorOccured && !isLiveStream() && loaded > 0)
836         timeRanges->add(0, loaded);
837 #endif
838     return timeRanges.release();
839 }
840
841 gboolean MediaPlayerPrivateGStreamer::handleMessage(GstMessage* message)
842 {
843     GOwnPtr<GError> err;
844     GOwnPtr<gchar> debug;
845     MediaPlayer::NetworkState error;
846     bool issueError = true;
847     bool attemptNextLocation = false;
848     const GstStructure* structure = gst_message_get_structure(message);
849     GstState requestedState, currentState;
850
851     m_canFallBackToLastFinishedSeekPositon = false;
852
853     if (structure) {
854         const gchar* messageTypeName = gst_structure_get_name(structure);
855
856         // Redirect messages are sent from elements, like qtdemux, to
857         // notify of the new location(s) of the media.
858         if (!g_strcmp0(messageTypeName, "redirect")) {
859             mediaLocationChanged(message);
860             return TRUE;
861         }
862     }
863
864     // We ignore state changes from internal elements. They are forwarded to playbin2 anyway.
865     bool messageSourceIsPlaybin = GST_MESSAGE_SRC(message) == reinterpret_cast<GstObject*>(m_playBin.get());
866
867     LOG_MEDIA_MESSAGE("Message %s received from element %s", GST_MESSAGE_TYPE_NAME(message), GST_MESSAGE_SRC_NAME(message));
868     switch (GST_MESSAGE_TYPE(message)) {
869     case GST_MESSAGE_ERROR:
870         if (m_resetPipeline)
871             break;
872         if (m_missingPlugins)
873             break;
874         gst_message_parse_error(message, &err.outPtr(), &debug.outPtr());
875         ERROR_MEDIA_MESSAGE("Error %d: %s (url=%s)", err->code, err->message, m_url.string().utf8().data());
876
877         GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS(GST_BIN(m_playBin.get()), GST_DEBUG_GRAPH_SHOW_ALL, "webkit-video.error");
878
879         error = MediaPlayer::Empty;
880         if (err->code == GST_STREAM_ERROR_CODEC_NOT_FOUND
881             || err->code == GST_STREAM_ERROR_WRONG_TYPE
882             || err->code == GST_STREAM_ERROR_FAILED
883             || err->code == GST_CORE_ERROR_MISSING_PLUGIN
884             || err->code == GST_RESOURCE_ERROR_NOT_FOUND)
885             error = MediaPlayer::FormatError;
886         else if (err->domain == GST_STREAM_ERROR) {
887             // Let the mediaPlayerClient handle the stream error, in
888             // this case the HTMLMediaElement will emit a stalled
889             // event.
890             if (err->code == GST_STREAM_ERROR_TYPE_NOT_FOUND) {
891                 ERROR_MEDIA_MESSAGE("Decode error, let the Media element emit a stalled event.");
892                 break;
893             }
894             error = MediaPlayer::DecodeError;
895             attemptNextLocation = true;
896         } else if (err->domain == GST_RESOURCE_ERROR)
897             error = MediaPlayer::NetworkError;
898
899         if (attemptNextLocation)
900             issueError = !loadNextLocation();
901         if (issueError)
902             loadingFailed(error);
903         break;
904     case GST_MESSAGE_EOS:
905         didEnd();
906         break;
907     case GST_MESSAGE_ASYNC_DONE:
908         if (!messageSourceIsPlaybin || m_delayingLoad)
909             break;
910         asyncStateChangeDone();
911         break;
912     case GST_MESSAGE_STATE_CHANGED: {
913         if (!messageSourceIsPlaybin || m_delayingLoad)
914             break;
915         updateStates();
916
917         // Construct a filename for the graphviz dot file output.
918         GstState newState;
919         gst_message_parse_state_changed(message, &currentState, &newState, 0);
920         CString dotFileName = String::format("webkit-video.%s_%s", gst_element_state_get_name(currentState), gst_element_state_get_name(newState)).utf8();
921         GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS(GST_BIN(m_playBin.get()), GST_DEBUG_GRAPH_SHOW_ALL, dotFileName.data());
922
923         break;
924     }
925     case GST_MESSAGE_BUFFERING:
926         processBufferingStats(message);
927         break;
928 #ifdef GST_API_VERSION_1
929     case GST_MESSAGE_DURATION_CHANGED:
930 #else
931     case GST_MESSAGE_DURATION:
932 #endif
933         if (messageSourceIsPlaybin)
934             durationChanged();
935         break;
936     case GST_MESSAGE_REQUEST_STATE:
937         gst_message_parse_request_state(message, &requestedState);
938         gst_element_get_state(m_playBin.get(), &currentState, NULL, 250);
939         if (requestedState < currentState) {
940             GOwnPtr<gchar> elementName(gst_element_get_name(GST_ELEMENT(message)));
941             INFO_MEDIA_MESSAGE("Element %s requested state change to %s", elementName.get(),
942                 gst_element_state_get_name(requestedState));
943             m_requestedState = requestedState;
944             changePipelineState(requestedState);
945         }
946         break;
947     case GST_MESSAGE_ELEMENT:
948         if (gst_is_missing_plugin_message(message)) {
949             gchar* detail = gst_missing_plugin_message_get_installer_detail(message);
950             gchar* detailArray[2] = {detail, 0};
951             GstInstallPluginsReturn result = gst_install_plugins_async(detailArray, 0, mediaPlayerPrivatePluginInstallerResultFunction, this);
952             m_missingPlugins = result == GST_INSTALL_PLUGINS_STARTED_OK;
953             g_free(detail);
954         }
955         break;
956     default:
957         LOG_MEDIA_MESSAGE("Unhandled GStreamer message type: %s",
958                     GST_MESSAGE_TYPE_NAME(message));
959         break;
960     }
961     return TRUE;
962 }
963
964 void MediaPlayerPrivateGStreamer::handlePluginInstallerResult(GstInstallPluginsReturn result)
965 {
966     m_missingPlugins = false;
967     if (result == GST_INSTALL_PLUGINS_SUCCESS) {
968         changePipelineState(GST_STATE_READY);
969         changePipelineState(GST_STATE_PAUSED);
970     }
971 }
972
973 void MediaPlayerPrivateGStreamer::processBufferingStats(GstMessage* message)
974 {
975     m_buffering = true;
976     const GstStructure *structure = gst_message_get_structure(message);
977     gst_structure_get_int(structure, "buffer-percent", &m_bufferingPercentage);
978
979     LOG_MEDIA_MESSAGE("[Buffering] Buffering: %d%%.", m_bufferingPercentage);
980
981     updateStates();
982 }
983
984 void MediaPlayerPrivateGStreamer::fillTimerFired(Timer<MediaPlayerPrivateGStreamer>*)
985 {
986     GstQuery* query = gst_query_new_buffering(GST_FORMAT_PERCENT);
987
988     if (!gst_element_query(m_playBin.get(), query)) {
989         gst_query_unref(query);
990         return;
991     }
992
993     gint64 start, stop;
994     gdouble fillStatus = 100.0;
995
996     gst_query_parse_buffering_range(query, 0, &start, &stop, 0);
997     gst_query_unref(query);
998
999     if (stop != -1)
1000         fillStatus = 100.0 * stop / GST_FORMAT_PERCENT_MAX;
1001
1002     LOG_MEDIA_MESSAGE("[Buffering] Download buffer filled up to %f%%", fillStatus);
1003
1004     if (!m_mediaDuration)
1005         durationChanged();
1006
1007     // Update maxTimeLoaded only if the media duration is
1008     // available. Otherwise we can't compute it.
1009     if (m_mediaDuration) {
1010         if (fillStatus == 100.0)
1011             m_maxTimeLoaded = m_mediaDuration;
1012         else
1013             m_maxTimeLoaded = static_cast<float>((fillStatus * m_mediaDuration) / 100.0);
1014         LOG_MEDIA_MESSAGE("[Buffering] Updated maxTimeLoaded: %f", m_maxTimeLoaded);
1015     }
1016
1017     m_downloadFinished = fillStatus == 100.0;
1018     if (!m_downloadFinished) {
1019         updateStates();
1020         return;
1021     }
1022
1023     // Media is now fully loaded. It will play even if network
1024     // connection is cut. Buffering is done, remove the fill source
1025     // from the main loop.
1026     m_fillTimer.stop();
1027     updateStates();
1028 }
1029
1030 float MediaPlayerPrivateGStreamer::maxTimeSeekable() const
1031 {
1032     if (m_errorOccured)
1033         return 0.0f;
1034
1035     LOG_MEDIA_MESSAGE("maxTimeSeekable");
1036     // infinite duration means live stream
1037     if (std::isinf(duration()))
1038         return 0.0f;
1039
1040     return duration();
1041 }
1042
1043 float MediaPlayerPrivateGStreamer::maxTimeLoaded() const
1044 {
1045     if (m_errorOccured)
1046         return 0.0f;
1047
1048     float loaded = m_maxTimeLoaded;
1049     if (m_isEndReached && m_mediaDuration)
1050         loaded = m_mediaDuration;
1051     LOG_MEDIA_MESSAGE("maxTimeLoaded: %f", loaded);
1052     return loaded;
1053 }
1054
1055 bool MediaPlayerPrivateGStreamer::didLoadingProgress() const
1056 {
1057     if (!m_playBin || !m_mediaDuration || !totalBytes())
1058         return false;
1059     float currentMaxTimeLoaded = maxTimeLoaded();
1060     bool didLoadingProgress = currentMaxTimeLoaded != m_maxTimeLoadedAtLastDidLoadingProgress;
1061     m_maxTimeLoadedAtLastDidLoadingProgress = currentMaxTimeLoaded;
1062     LOG_MEDIA_MESSAGE("didLoadingProgress: %d", didLoadingProgress);
1063     return didLoadingProgress;
1064 }
1065
1066 unsigned MediaPlayerPrivateGStreamer::totalBytes() const
1067 {
1068     if (m_errorOccured)
1069         return 0;
1070
1071     if (m_totalBytes != -1)
1072         return m_totalBytes;
1073
1074     if (!m_source)
1075         return 0;
1076
1077     GstFormat fmt = GST_FORMAT_BYTES;
1078     gint64 length = 0;
1079 #ifdef GST_API_VERSION_1
1080     if (gst_element_query_duration(m_source.get(), fmt, &length)) {
1081 #else
1082     if (gst_element_query_duration(m_source.get(), &fmt, &length)) {
1083 #endif
1084         INFO_MEDIA_MESSAGE("totalBytes %" G_GINT64_FORMAT, length);
1085         m_totalBytes = static_cast<unsigned>(length);
1086         m_isStreaming = !length;
1087         return m_totalBytes;
1088     }
1089
1090     // Fall back to querying the source pads manually.
1091     // See also https://bugzilla.gnome.org/show_bug.cgi?id=638749
1092     GstIterator* iter = gst_element_iterate_src_pads(m_source.get());
1093     bool done = false;
1094     while (!done) {
1095 #ifdef GST_API_VERSION_1
1096         GValue item = G_VALUE_INIT;
1097         switch (gst_iterator_next(iter, &item)) {
1098         case GST_ITERATOR_OK: {
1099             GstPad* pad = static_cast<GstPad*>(g_value_get_object(&item));
1100             gint64 padLength = 0;
1101             if (gst_pad_query_duration(pad, fmt, &padLength) && padLength > length)
1102                 length = padLength;
1103             break;
1104         }
1105 #else
1106         gpointer data;
1107
1108         switch (gst_iterator_next(iter, &data)) {
1109         case GST_ITERATOR_OK: {
1110             GRefPtr<GstPad> pad = adoptGRef(GST_PAD_CAST(data));
1111             gint64 padLength = 0;
1112             if (gst_pad_query_duration(pad.get(), &fmt, &padLength) && padLength > length)
1113                 length = padLength;
1114             break;
1115         }
1116 #endif
1117         case GST_ITERATOR_RESYNC:
1118             gst_iterator_resync(iter);
1119             break;
1120         case GST_ITERATOR_ERROR:
1121             // Fall through.
1122         case GST_ITERATOR_DONE:
1123             done = true;
1124             break;
1125         }
1126
1127 #ifdef GST_API_VERSION_1
1128         g_value_unset(&item);
1129 #endif
1130     }
1131
1132     gst_iterator_free(iter);
1133
1134     INFO_MEDIA_MESSAGE("totalBytes %" G_GINT64_FORMAT, length);
1135     m_totalBytes = static_cast<unsigned>(length);
1136     m_isStreaming = !length;
1137     return m_totalBytes;
1138 }
1139
1140 void MediaPlayerPrivateGStreamer::updateAudioSink()
1141 {
1142     if (!m_playBin)
1143         return;
1144
1145     GstElement* sinkPtr = 0;
1146
1147     g_object_get(m_playBin.get(), "audio-sink", &sinkPtr, NULL);
1148     m_webkitAudioSink = adoptGRef(sinkPtr);
1149
1150 }
1151
1152 GstElement* MediaPlayerPrivateGStreamer::audioSink() const
1153 {
1154     return m_webkitAudioSink.get();
1155 }
1156
1157 void MediaPlayerPrivateGStreamer::sourceChanged()
1158 {
1159     GstElement* srcPtr = 0;
1160
1161     g_object_get(m_playBin.get(), "source", &srcPtr, NULL);
1162     m_source = adoptGRef(srcPtr);
1163
1164     if (WEBKIT_IS_WEB_SRC(m_source.get()))
1165         webKitWebSrcSetMediaPlayer(WEBKIT_WEB_SRC(m_source.get()), m_player);
1166 }
1167
1168 void MediaPlayerPrivateGStreamer::cancelLoad()
1169 {
1170     if (m_networkState < MediaPlayer::Loading || m_networkState == MediaPlayer::Loaded)
1171         return;
1172
1173     if (m_playBin)
1174         changePipelineState(GST_STATE_READY);
1175 }
1176
1177 void MediaPlayerPrivateGStreamer::asyncStateChangeDone()
1178 {
1179     if (!m_playBin || m_errorOccured)
1180         return;
1181
1182     if (m_seeking) {
1183         if (m_seekIsPending)
1184             updateStates();
1185         else {
1186             LOG_MEDIA_MESSAGE("[Seek] seeked to %f", m_seekTime);
1187             m_seeking = false;
1188             if (m_timeOfOverlappingSeek != m_seekTime && m_timeOfOverlappingSeek != -1) {
1189                 seek(m_timeOfOverlappingSeek);
1190                 m_timeOfOverlappingSeek = -1;
1191                 return;
1192             }
1193             m_timeOfOverlappingSeek = -1;
1194
1195             // The pipeline can still have a pending state. In this case a position query will fail.
1196             // Right now we can use m_seekTime as a fallback.
1197             m_canFallBackToLastFinishedSeekPositon = true;
1198             timeChanged();
1199         }
1200     } else
1201         updateStates();
1202 }
1203
1204 void MediaPlayerPrivateGStreamer::updateStates()
1205 {
1206     if (!m_playBin)
1207         return;
1208
1209     if (m_errorOccured)
1210         return;
1211
1212     MediaPlayer::NetworkState oldNetworkState = m_networkState;
1213     MediaPlayer::ReadyState oldReadyState = m_readyState;
1214     GstState state;
1215     GstState pending;
1216
1217     GstStateChangeReturn getStateResult = gst_element_get_state(m_playBin.get(), &state, &pending, 250 * GST_NSECOND);
1218
1219     bool shouldUpdatePlaybackState = false;
1220     switch (getStateResult) {
1221     case GST_STATE_CHANGE_SUCCESS: {
1222         LOG_MEDIA_MESSAGE("State: %s, pending: %s", gst_element_state_get_name(state), gst_element_state_get_name(pending));
1223
1224         if (state <= GST_STATE_READY) {
1225             m_resetPipeline = true;
1226             m_mediaDuration = 0;
1227         } else {
1228             m_resetPipeline = false;
1229             cacheDuration();
1230         }
1231
1232         bool didBuffering = m_buffering;
1233
1234         // Update ready and network states.
1235         switch (state) {
1236         case GST_STATE_NULL:
1237             m_readyState = MediaPlayer::HaveNothing;
1238             m_networkState = MediaPlayer::Empty;
1239             break;
1240         case GST_STATE_READY:
1241             // Do not change network/ready states if on EOS and state changed to READY to avoid
1242             // recreating the player on HTMLMediaElement.
1243             if (!m_isEndReached) {
1244                 m_readyState = MediaPlayer::HaveMetadata;
1245                 m_networkState = MediaPlayer::Empty;
1246             }
1247             break;
1248         case GST_STATE_PAUSED:
1249         case GST_STATE_PLAYING:
1250             if (m_buffering) {
1251                 if (m_bufferingPercentage == 100) {
1252                     LOG_MEDIA_MESSAGE("[Buffering] Complete.");
1253                     m_buffering = false;
1254                     m_readyState = MediaPlayer::HaveEnoughData;
1255                     m_networkState = m_downloadFinished ? MediaPlayer::Idle : MediaPlayer::Loading;
1256                 } else {
1257                     m_readyState = MediaPlayer::HaveCurrentData;
1258                     m_networkState = MediaPlayer::Loading;
1259                 }
1260             } else if (m_downloadFinished) {
1261                 m_readyState = MediaPlayer::HaveEnoughData;
1262                 m_networkState = MediaPlayer::Loaded;
1263             } else {
1264                 m_readyState = MediaPlayer::HaveFutureData;
1265                 m_networkState = MediaPlayer::Loading;
1266             }
1267
1268             break;
1269         default:
1270             ASSERT_NOT_REACHED();
1271             break;
1272         }
1273
1274         // Sync states where needed.
1275         if (state == GST_STATE_PAUSED) {
1276             if (!m_webkitAudioSink)
1277                 updateAudioSink();
1278
1279             if (!m_volumeAndMuteInitialized) {
1280                 notifyPlayerOfVolumeChange();
1281                 notifyPlayerOfMute();
1282                 m_volumeAndMuteInitialized = true;
1283             }
1284
1285             if (didBuffering && !m_buffering && !m_paused) {
1286                 LOG_MEDIA_MESSAGE("[Buffering] Restarting playback.");
1287                 changePipelineState(GST_STATE_PLAYING);
1288             }
1289         } else if (state == GST_STATE_PLAYING) {
1290             m_paused = false;
1291
1292             if (m_buffering && !isLiveStream()) {
1293                 LOG_MEDIA_MESSAGE("[Buffering] Pausing stream for buffering.");
1294                 changePipelineState(GST_STATE_PAUSED);
1295             }
1296         } else
1297             m_paused = true;
1298
1299         if (m_changingRate) {
1300             m_player->rateChanged();
1301             m_changingRate = false;
1302         }
1303
1304         if (m_requestedState == GST_STATE_PAUSED && state == GST_STATE_PAUSED) {
1305             shouldUpdatePlaybackState = true;
1306             LOG_MEDIA_MESSAGE("Requested state change to %s was completed", gst_element_state_get_name(state));
1307         }
1308
1309         break;
1310     }
1311     case GST_STATE_CHANGE_ASYNC:
1312         LOG_MEDIA_MESSAGE("Async: State: %s, pending: %s", gst_element_state_get_name(state), gst_element_state_get_name(pending));
1313         // Change in progress.
1314         break;
1315     case GST_STATE_CHANGE_FAILURE:
1316         LOG_MEDIA_MESSAGE("Failure: State: %s, pending: %s", gst_element_state_get_name(state), gst_element_state_get_name(pending));
1317         // Change failed
1318         return;
1319     case GST_STATE_CHANGE_NO_PREROLL:
1320         LOG_MEDIA_MESSAGE("No preroll: State: %s, pending: %s", gst_element_state_get_name(state), gst_element_state_get_name(pending));
1321
1322         // Live pipelines go in PAUSED without prerolling.
1323         m_isStreaming = true;
1324         setDownloadBuffering();
1325
1326         if (state == GST_STATE_READY)
1327             m_readyState = MediaPlayer::HaveNothing;
1328         else if (state == GST_STATE_PAUSED) {
1329             m_readyState = MediaPlayer::HaveEnoughData;
1330             m_paused = true;
1331         } else if (state == GST_STATE_PLAYING)
1332             m_paused = false;
1333
1334         if (!m_paused)
1335             changePipelineState(GST_STATE_PLAYING);
1336
1337         m_networkState = MediaPlayer::Loading;
1338         break;
1339     default:
1340         LOG_MEDIA_MESSAGE("Else : %d", getStateResult);
1341         break;
1342     }
1343
1344     m_requestedState = GST_STATE_VOID_PENDING;
1345
1346     if (shouldUpdatePlaybackState)
1347         m_player->playbackStateChanged();
1348
1349     if (m_networkState != oldNetworkState) {
1350         LOG_MEDIA_MESSAGE("Network State Changed from %u to %u", oldNetworkState, m_networkState);
1351         m_player->networkStateChanged();
1352     }
1353     if (m_readyState != oldReadyState) {
1354         LOG_MEDIA_MESSAGE("Ready State Changed from %u to %u", oldReadyState, m_readyState);
1355         m_player->readyStateChanged();
1356     }
1357
1358     if (m_seekIsPending && getStateResult == GST_STATE_CHANGE_SUCCESS && (state == GST_STATE_PAUSED || state == GST_STATE_PLAYING)) {
1359         LOG_MEDIA_MESSAGE("[Seek] committing pending seek to %f", m_seekTime);
1360         m_seekIsPending = false;
1361         m_seeking = gst_element_seek(m_playBin.get(), m_player->rate(), GST_FORMAT_TIME, static_cast<GstSeekFlags>(GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_ACCURATE),
1362             GST_SEEK_TYPE_SET, toGstClockTime(m_seekTime), GST_SEEK_TYPE_NONE, GST_CLOCK_TIME_NONE);
1363         if (!m_seeking)
1364             LOG_MEDIA_MESSAGE("[Seek] seeking to %f failed", m_seekTime);
1365     }
1366 }
1367
1368 void MediaPlayerPrivateGStreamer::mediaLocationChanged(GstMessage* message)
1369 {
1370     if (m_mediaLocations)
1371         gst_structure_free(m_mediaLocations);
1372
1373     const GstStructure* structure = gst_message_get_structure(message);
1374     if (structure) {
1375         // This structure can contain:
1376         // - both a new-location string and embedded locations structure
1377         // - or only a new-location string.
1378         m_mediaLocations = gst_structure_copy(structure);
1379         const GValue* locations = gst_structure_get_value(m_mediaLocations, "locations");
1380
1381         if (locations)
1382             m_mediaLocationCurrentIndex = static_cast<int>(gst_value_list_get_size(locations)) -1;
1383
1384         loadNextLocation();
1385     }
1386 }
1387
1388 bool MediaPlayerPrivateGStreamer::loadNextLocation()
1389 {
1390     if (!m_mediaLocations)
1391         return false;
1392
1393     const GValue* locations = gst_structure_get_value(m_mediaLocations, "locations");
1394     const gchar* newLocation = 0;
1395
1396     if (!locations) {
1397         // Fallback on new-location string.
1398         newLocation = gst_structure_get_string(m_mediaLocations, "new-location");
1399         if (!newLocation)
1400             return false;
1401     }
1402
1403     if (!newLocation) {
1404         if (m_mediaLocationCurrentIndex < 0) {
1405             m_mediaLocations = 0;
1406             return false;
1407         }
1408
1409         const GValue* location = gst_value_list_get_value(locations,
1410                                                           m_mediaLocationCurrentIndex);
1411         const GstStructure* structure = gst_value_get_structure(location);
1412
1413         if (!structure) {
1414             m_mediaLocationCurrentIndex--;
1415             return false;
1416         }
1417
1418         newLocation = gst_structure_get_string(structure, "new-location");
1419     }
1420
1421     if (newLocation) {
1422         // Found a candidate. new-location is not always an absolute url
1423         // though. We need to take the base of the current url and
1424         // append the value of new-location to it.
1425         KURL baseUrl = gst_uri_is_valid(newLocation) ? KURL() : m_url;
1426         KURL newUrl = KURL(baseUrl, newLocation);
1427
1428         RefPtr<SecurityOrigin> securityOrigin = SecurityOrigin::create(m_url);
1429         if (securityOrigin->canRequest(newUrl)) {
1430             INFO_MEDIA_MESSAGE("New media url: %s", newUrl.string().utf8().data());
1431
1432             // Reset player states.
1433             m_networkState = MediaPlayer::Loading;
1434             m_player->networkStateChanged();
1435             m_readyState = MediaPlayer::HaveNothing;
1436             m_player->readyStateChanged();
1437
1438             // Reset pipeline state.
1439             m_resetPipeline = true;
1440             changePipelineState(GST_STATE_READY);
1441
1442             GstState state;
1443             gst_element_get_state(m_playBin.get(), &state, 0, 0);
1444             if (state <= GST_STATE_READY) {
1445                 // Set the new uri and start playing.
1446                 g_object_set(m_playBin.get(), "uri", newUrl.string().utf8().data(), NULL);
1447                 m_url = newUrl;
1448                 changePipelineState(GST_STATE_PLAYING);
1449                 return true;
1450             }
1451         } else
1452             INFO_MEDIA_MESSAGE("Not allowed to load new media location: %s", newUrl.string().utf8().data());
1453     }
1454     m_mediaLocationCurrentIndex--;
1455     return false;
1456 }
1457
1458 void MediaPlayerPrivateGStreamer::loadStateChanged()
1459 {
1460     updateStates();
1461 }
1462
1463 void MediaPlayerPrivateGStreamer::timeChanged()
1464 {
1465     updateStates();
1466     m_player->timeChanged();
1467 }
1468
1469 void MediaPlayerPrivateGStreamer::didEnd()
1470 {
1471     // Synchronize position and duration values to not confuse the
1472     // HTMLMediaElement. In some cases like reverse playback the
1473     // position is not always reported as 0 for instance.
1474     float now = currentTime();
1475     if (now > 0 && now <= duration() && m_mediaDuration != now) {
1476         m_mediaDurationKnown = true;
1477         m_mediaDuration = now;
1478         m_player->durationChanged();
1479     }
1480
1481     m_isEndReached = true;
1482     timeChanged();
1483
1484     if (!m_player->mediaPlayerClient()->mediaPlayerIsLooping()) {
1485         m_paused = true;
1486         changePipelineState(GST_STATE_READY);
1487         m_downloadFinished = false;
1488     }
1489 }
1490
1491 void MediaPlayerPrivateGStreamer::cacheDuration()
1492 {
1493     if (m_mediaDuration || !m_mediaDurationKnown)
1494         return;
1495
1496     float newDuration = duration();
1497     if (std::isinf(newDuration)) {
1498         // Only pretend that duration is not available if the the query failed in a stable pipeline state.
1499         GstState state;
1500         if (gst_element_get_state(m_playBin.get(), &state, 0, 0) == GST_STATE_CHANGE_SUCCESS && state > GST_STATE_READY)
1501             m_mediaDurationKnown = false;
1502         return;
1503     }
1504
1505     m_mediaDuration = newDuration;
1506 }
1507
1508 void MediaPlayerPrivateGStreamer::durationChanged()
1509 {
1510     float previousDuration = m_mediaDuration;
1511
1512     cacheDuration();
1513     // Avoid emiting durationchanged in the case where the previous
1514     // duration was 0 because that case is already handled by the
1515     // HTMLMediaElement.
1516     if (previousDuration && m_mediaDuration != previousDuration)
1517         m_player->durationChanged();
1518 }
1519
1520 void MediaPlayerPrivateGStreamer::loadingFailed(MediaPlayer::NetworkState error)
1521 {
1522     m_errorOccured = true;
1523     if (m_networkState != error) {
1524         m_networkState = error;
1525         m_player->networkStateChanged();
1526     }
1527     if (m_readyState != MediaPlayer::HaveNothing) {
1528         m_readyState = MediaPlayer::HaveNothing;
1529         m_player->readyStateChanged();
1530     }
1531
1532     // Loading failed, force reset pipeline and remove ready timer.
1533     gst_element_set_state(m_playBin.get(), GST_STATE_NULL);
1534     if (m_readyTimerHandler) {
1535         g_source_remove(m_readyTimerHandler);
1536         m_readyTimerHandler = 0;
1537     }
1538 }
1539
1540 static HashSet<String> mimeTypeCache()
1541 {
1542     initializeGStreamerAndRegisterWebKitElements();
1543
1544     DEFINE_STATIC_LOCAL(HashSet<String>, cache, ());
1545     static bool typeListInitialized = false;
1546
1547     if (typeListInitialized)
1548         return cache;
1549
1550     const char* mimeTypes[] = {
1551         "application/ogg",
1552         "application/vnd.apple.mpegurl",
1553         "application/vnd.rn-realmedia",
1554         "application/x-3gp",
1555         "application/x-pn-realaudio",
1556         "audio/3gpp",
1557         "audio/aac",
1558         "audio/flac",
1559         "audio/iLBC-sh",
1560         "audio/midi",
1561         "audio/mobile-xmf",
1562         "audio/mp1",
1563         "audio/mp2",
1564         "audio/mp3",
1565         "audio/mp4",
1566         "audio/mpeg",
1567         "audio/ogg",
1568         "audio/opus",
1569         "audio/qcelp",
1570         "audio/riff-midi",
1571         "audio/speex",
1572         "audio/wav",
1573         "audio/webm",
1574         "audio/x-ac3",
1575         "audio/x-aiff",
1576         "audio/x-amr-nb-sh",
1577         "audio/x-amr-wb-sh",
1578         "audio/x-au",
1579         "audio/x-ay",
1580         "audio/x-celt",
1581         "audio/x-dts",
1582         "audio/x-flac",
1583         "audio/x-gbs",
1584         "audio/x-gsm",
1585         "audio/x-gym",
1586         "audio/x-imelody",
1587         "audio/x-ircam",
1588         "audio/x-kss",
1589         "audio/x-m4a",
1590         "audio/x-mod",
1591         "audio/x-mp3",
1592         "audio/x-mpeg",
1593         "audio/x-musepack",
1594         "audio/x-nist",
1595         "audio/x-nsf",
1596         "audio/x-paris",
1597         "audio/x-sap",
1598         "audio/x-sbc",
1599         "audio/x-sds",
1600         "audio/x-shorten",
1601         "audio/x-sid",
1602         "audio/x-spc",
1603         "audio/x-speex",
1604         "audio/x-svx",
1605         "audio/x-ttafile",
1606         "audio/x-vgm",
1607         "audio/x-voc",
1608         "audio/x-vorbis+ogg",
1609         "audio/x-w64",
1610         "audio/x-wav",
1611         "audio/x-wavpack",
1612         "audio/x-wavpack-correction",
1613         "video/3gpp",
1614         "video/mj2",
1615         "video/mp4",
1616         "video/mpeg",
1617         "video/mpegts",
1618         "video/ogg",
1619         "video/quicktime",
1620         "video/vivo",
1621         "video/webm",
1622         "video/x-cdxa",
1623         "video/x-dirac",
1624         "video/x-dv",
1625         "video/x-fli",
1626         "video/x-flv",
1627         "video/x-h263",
1628         "video/x-ivf",
1629         "video/x-m4v",
1630         "video/x-matroska",
1631         "video/x-mng",
1632         "video/x-ms-asf",
1633         "video/x-msvideo",
1634         "video/x-mve",
1635         "video/x-nuv",
1636         "video/x-vcd"
1637     };
1638
1639     for (unsigned i = 0; i < (sizeof(mimeTypes) / sizeof(*mimeTypes)); ++i)
1640         cache.add(String(mimeTypes[i]));
1641
1642     typeListInitialized = true;
1643     return cache;
1644 }
1645
1646 void MediaPlayerPrivateGStreamer::getSupportedTypes(HashSet<String>& types)
1647 {
1648     types = mimeTypeCache();
1649 }
1650
1651 MediaPlayer::SupportsType MediaPlayerPrivateGStreamer::supportsType(const String& type, const String& codecs, const KURL&)
1652 {
1653     if (type.isNull() || type.isEmpty())
1654         return MediaPlayer::IsNotSupported;
1655
1656     // spec says we should not return "probably" if the codecs string is empty
1657     if (mimeTypeCache().contains(type))
1658         return codecs.isEmpty() ? MediaPlayer::MayBeSupported : MediaPlayer::IsSupported;
1659     return MediaPlayer::IsNotSupported;
1660 }
1661
1662 void MediaPlayerPrivateGStreamer::setDownloadBuffering()
1663 {
1664     if (!m_playBin)
1665         return;
1666
1667     GstPlayFlags flags;
1668     g_object_get(m_playBin.get(), "flags", &flags, NULL);
1669
1670     // We don't want to stop downloading if we already started it.
1671     if (flags & GST_PLAY_FLAG_DOWNLOAD && m_readyState > MediaPlayer::HaveNothing && !m_resetPipeline)
1672         return;
1673
1674     bool shouldDownload = !isLiveStream() && m_preload == MediaPlayer::Auto;
1675     if (shouldDownload) {
1676         LOG_MEDIA_MESSAGE("Enabling on-disk buffering");
1677         g_object_set(m_playBin.get(), "flags", flags | GST_PLAY_FLAG_DOWNLOAD, NULL);
1678         m_fillTimer.startRepeating(0.2);
1679     } else {
1680         LOG_MEDIA_MESSAGE("Disabling on-disk buffering");
1681         g_object_set(m_playBin.get(), "flags", flags & ~GST_PLAY_FLAG_DOWNLOAD, NULL);
1682         m_fillTimer.stop();
1683     }
1684 }
1685
1686 void MediaPlayerPrivateGStreamer::setPreload(MediaPlayer::Preload preload)
1687 {
1688     if (preload == MediaPlayer::Auto && isLiveStream())
1689         return;
1690
1691     m_preload = preload;
1692     setDownloadBuffering();
1693
1694     if (m_delayingLoad && m_preload != MediaPlayer::None) {
1695         m_delayingLoad = false;
1696         commitLoad();
1697     }
1698 }
1699
1700 void MediaPlayerPrivateGStreamer::createAudioSink()
1701 {
1702     // Construct audio sink if pitch preserving is enabled.
1703     if (!m_preservesPitch)
1704         return;
1705
1706     if (!m_playBin)
1707         return;
1708
1709     GstElement* scale = gst_element_factory_make("scaletempo", 0);
1710     if (!scale) {
1711         GST_WARNING("Failed to create scaletempo");
1712         return;
1713     }
1714
1715     GstElement* convert = gst_element_factory_make("audioconvert", 0);
1716     GstElement* resample = gst_element_factory_make("audioresample", 0);
1717     GstElement* sink = gst_element_factory_make("autoaudiosink", 0);
1718
1719     m_autoAudioSink = sink;
1720
1721     g_signal_connect(sink, "child-added", G_CALLBACK(setAudioStreamPropertiesCallback), this);
1722
1723     GstElement* audioSink = gst_bin_new("audio-sink");
1724     gst_bin_add_many(GST_BIN(audioSink), scale, convert, resample, sink, NULL);
1725
1726     if (!gst_element_link_many(scale, convert, resample, sink, NULL)) {
1727         GST_WARNING("Failed to link audio sink elements");
1728         gst_object_unref(audioSink);
1729         return;
1730     }
1731
1732     GRefPtr<GstPad> pad = adoptGRef(gst_element_get_static_pad(scale, "sink"));
1733     gst_element_add_pad(audioSink, gst_ghost_pad_new("sink", pad.get()));
1734
1735     g_object_set(m_playBin.get(), "audio-sink", audioSink, NULL);
1736 }
1737
1738 void MediaPlayerPrivateGStreamer::createGSTPlayBin()
1739 {
1740     ASSERT(!m_playBin);
1741
1742     // gst_element_factory_make() returns a floating reference so
1743     // we should not adopt.
1744     m_playBin = gst_element_factory_make(gPlaybinName, "play");
1745     setStreamVolumeElement(GST_STREAM_VOLUME(m_playBin.get()));
1746
1747     GRefPtr<GstBus> bus = webkitGstPipelineGetBus(GST_PIPELINE(m_playBin.get()));
1748     gst_bus_add_signal_watch(bus.get());
1749     g_signal_connect(bus.get(), "message", G_CALLBACK(mediaPlayerPrivateMessageCallback), this);
1750
1751     g_object_set(m_playBin.get(), "mute", m_player->muted(), NULL);
1752
1753     g_signal_connect(m_playBin.get(), "notify::source", G_CALLBACK(mediaPlayerPrivateSourceChangedCallback), this);
1754     g_signal_connect(m_playBin.get(), "video-changed", G_CALLBACK(mediaPlayerPrivateVideoChangedCallback), this);
1755     g_signal_connect(m_playBin.get(), "audio-changed", G_CALLBACK(mediaPlayerPrivateAudioChangedCallback), this);
1756 #if ENABLE(VIDEO_TRACK) && defined(GST_API_VERSION_1)
1757     if (webkitGstCheckVersion(1, 1, 2)) {
1758         g_signal_connect(m_playBin.get(), "text-changed", G_CALLBACK(mediaPlayerPrivateTextChangedCallback), this);
1759
1760         GstElement* textCombiner = webkitTextCombinerNew();
1761         ASSERT(textCombiner);
1762         g_object_set(m_playBin.get(), "text-stream-combiner", textCombiner, NULL);
1763
1764         m_textAppSink = webkitTextSinkNew();
1765         ASSERT(m_textAppSink);
1766
1767         m_textAppSinkPad = adoptGRef(gst_element_get_static_pad(m_textAppSink.get(), "sink"));
1768         ASSERT(m_textAppSinkPad);
1769
1770         g_object_set(m_textAppSink.get(), "emit-signals", true, "enable-last-sample", false, "caps", gst_caps_new_empty_simple("text/vtt"), NULL);
1771         g_signal_connect(m_textAppSink.get(), "new-sample", G_CALLBACK(mediaPlayerPrivateNewTextSampleCallback), this);
1772
1773         g_object_set(m_playBin.get(), "text-sink", m_textAppSink.get(), NULL);
1774     }
1775 #endif
1776
1777     GstElement* videoElement = createVideoSink(m_playBin.get());
1778
1779     g_object_set(m_playBin.get(), "video-sink", videoElement, NULL);
1780
1781     GRefPtr<GstPad> videoSinkPad = adoptGRef(gst_element_get_static_pad(m_webkitVideoSink.get(), "sink"));
1782     if (videoSinkPad)
1783         g_signal_connect(videoSinkPad.get(), "notify::caps", G_CALLBACK(mediaPlayerPrivateVideoSinkCapsChangedCallback), this);
1784
1785     createAudioSink();
1786 }
1787
1788 void MediaPlayerPrivateGStreamer::simulateAudioInterruption()
1789 {
1790     GstMessage* message = gst_message_new_request_state(GST_OBJECT(m_playBin.get()), GST_STATE_PAUSED);
1791     gst_element_post_message(m_playBin.get(), message);
1792 }
1793
1794 }
1795
1796 #endif // USE(GSTREAMER)