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
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.
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.
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.
25 #include "MediaPlayerPrivateGStreamer.h"
27 #if ENABLE(VIDEO) && USE(GSTREAMER)
29 #include "ColorSpace.h"
30 #if ENABLE(TIZEN_GSTREAMER_VIDEO)
31 #include "CookieJar.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"
45 #include "ImageGStreamer.h"
46 #include "ImageOrientation.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"
57 #include <gst/video/video.h>
60 #include <wtf/gobject/GOwnPtr.h>
61 #include <wtf/text/CString.h>
63 #ifdef GST_API_VERSION_1
64 #include <gst/audio/streamvolume.h>
66 #include <gst/interfaces/streamvolume.h>
69 #if ENABLE(TIZEN_ACCELERATED_COMPOSITING) && USE(TIZEN_TEXTURE_MAPPER)
72 #include "VideoLayerTizen.h"
73 #endif // ENABLE(TIZEN_ACCELERATED_COMPOSITING) && USE(TIZEN_TEXTURE_MAPPER)
75 #if ENABLE(TIZEN_MEDIA_STREAM) || ENABLE(TIZEN_FILE_SYSTEM)
77 #endif // ENABLE(TIZEN_MEDIA_STREAM) || ENABLE(TIZEN_FILE_SYSTEM)
79 #if ENABLE(TIZEN_MEDIA_STREAM)
80 #include "WebKitCameraSourceGStreamerTizen.h"
81 #endif // ENABLE(TIZEN_MEDIA_STREAM)
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.
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
98 #ifdef GST_API_VERSION_1
99 static const char* gPlaybinName = "playbin";
101 static const char* gPlaybinName = "playbin2";
104 GST_DEBUG_CATEGORY_STATIC(webkit_media_player_debug);
105 #define GST_CAT_DEFAULT webkit_media_player_debug
111 static int greatestCommonDivisor(int a, int b)
122 static gboolean mediaPlayerPrivateMessageCallback(GstBus*, GstMessage* message, MediaPlayerPrivateGStreamer* player)
124 return player->handleMessage(message);
127 static void mediaPlayerPrivateSourceChangedCallback(GObject*, GParamSpec*, MediaPlayerPrivateGStreamer* player)
129 player->sourceChanged();
132 static void mediaPlayerPrivateVolumeChangedCallback(GObject*, GParamSpec*, MediaPlayerPrivateGStreamer* player)
134 // This is called when playbin receives the notify::volume signal.
135 player->volumeChanged();
138 static gboolean mediaPlayerPrivateVolumeChangeTimeoutCallback(MediaPlayerPrivateGStreamer* player)
140 // This is the callback of the timeout source created in ::volumeChanged.
141 player->notifyPlayerOfVolumeChange();
145 static void mediaPlayerPrivateMuteChangedCallback(GObject*, GParamSpec*, MediaPlayerPrivateGStreamer* player)
147 // This is called when playbin receives the notify::mute signal.
148 player->muteChanged();
151 static gboolean mediaPlayerPrivateMuteChangeTimeoutCallback(MediaPlayerPrivateGStreamer* player)
153 // This is the callback of the timeout source created in ::muteChanged.
154 player->notifyPlayerOfMute();
158 static void mediaPlayerPrivateVideoSinkCapsChangedCallback(GObject*, GParamSpec*, MediaPlayerPrivateGStreamer* player)
160 player->videoChanged();
163 static void mediaPlayerPrivateVideoChangedCallback(GObject*, MediaPlayerPrivateGStreamer* player)
165 player->videoChanged();
168 static void mediaPlayerPrivateAudioChangedCallback(GObject*, MediaPlayerPrivateGStreamer* player)
170 player->audioChanged();
173 #if ENABLE(TIZEN_GSTREAMER_VIDEO)
174 static void mediaPlayerPrivateSourceSetupCallback(GObject*, GstElement* source, MediaPlayerPrivateGStreamer* player)
176 HTMLMediaElement* element = static_cast<HTMLMediaElement*>(player->player()->mediaPlayerClient());
178 // Grab the current document
179 Document* document = element->document();
181 document = element->ownerDocument();
183 // Grab the frame and network manager
184 Frame* frame = document ? document->frame() : 0;
185 FrameLoader* frameLoader = frame ? frame->loader() : 0;
187 String cookie = cookies(document, player->url());
188 if (!cookie.length())
189 cookie = cookieRequestHeaderFieldValue(document, player->url());
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);
195 #if ENABLE(TIZEN_WEBKIT2_PROXY)
196 player->setProxy(source);
200 g_object_set(source, "user-agent", frameLoader->userAgent(player->url()).utf8().data(), NULL);
204 static gboolean mediaPlayerPrivateAudioChangeTimeoutCallback(MediaPlayerPrivateGStreamer* player)
206 // This is the callback of the timeout source created in ::audioChanged.
207 player->notifyPlayerOfAudio();
211 static gboolean mediaPlayerPrivateVideoChangeTimeoutCallback(MediaPlayerPrivateGStreamer* player)
213 // This is the callback of the timeout source created in ::videoChanged.
214 player->notifyPlayerOfVideo();
218 static void mediaPlayerPrivateRepaintCallback(WebKitVideoSink*, GstBuffer *buffer, MediaPlayerPrivateGStreamer* playerPrivate)
220 playerPrivate->triggerRepaint(buffer);
223 #if ENABLE(TIZEN_ACCELERATED_COMPOSITING) && USE(TIZEN_TEXTURE_MAPPER) && ENABLE(TIZEN_WEBKIT2_TILED_AC_SHARED_PLATFORM_SURFACE)
224 static GstBusSyncReply mediaPlayerPrivateSyncHandler(GstBus* bus, GstMessage* message, MediaPlayerPrivateGStreamer* playerPrivate)
226 // ignore anything but 'prepare-xid' message
227 if (GST_MESSAGE_TYPE(message) != GST_MESSAGE_ELEMENT)
229 if (!gst_structure_has_name (message->structure, "prepare-xid"))
232 playerPrivate->xWindowIdPrepared(message);
237 #if ENABLE(TIZEN_GSTREAMER_AUDIO)
238 static ASM_cb_result_t MediaPlayerAudioSessionEventSourcePause(ASM_event_sources_t eventSource, void* callbackData)
240 MediaPlayer* player = static_cast<MediaPlayer*>(callbackData);
242 return ASM_CB_RES_IGNORE;
244 switch (eventSource) {
245 case ASM_EVENT_SOURCE_CALL_START:
246 case ASM_EVENT_SOURCE_ALARM_START:
247 case ASM_EVENT_SOURCE_EARJACK_UNPLUG:
248 case ASM_EVENT_SOURCE_MEDIA:
249 case ASM_EVENT_SOURCE_EMERGENCY_START:
250 case ASM_EVENT_SOURCE_OTHER_PLAYER_APP:
251 case ASM_EVENT_SOURCE_RESOURCE_CONFLICT:
253 return ASM_CB_RES_PAUSE;
255 return ASM_CB_RES_NONE;
259 static ASM_cb_result_t MediaPlayerAudioSessionEventSourcePlay(ASM_event_sources_t eventSource, void* callbackData)
261 MediaPlayer* player = static_cast<MediaPlayer*>(callbackData);
263 return ASM_CB_RES_IGNORE;
265 switch (eventSource) {
266 case ASM_EVENT_SOURCE_ALARM_END:
267 if (!player->hasVideo()) {
269 return ASM_CB_RES_PLAYING;
271 return ASM_CB_RES_NONE;
273 return ASM_CB_RES_NONE;
277 static ASM_cb_result_t mediaPlayerPrivateAudioSessionNotifyCallback(int, ASM_event_sources_t eventSource, ASM_sound_commands_t command, unsigned int, void* callbackData)
279 if (command == ASM_COMMAND_STOP || command == ASM_COMMAND_PAUSE)
280 return MediaPlayerAudioSessionEventSourcePause(eventSource, callbackData);
281 if (command == ASM_COMMAND_PLAY || command == ASM_COMMAND_RESUME)
282 return MediaPlayerAudioSessionEventSourcePlay(eventSource, callbackData);
284 return ASM_CB_RES_NONE;
288 PassOwnPtr<MediaPlayerPrivateInterface> MediaPlayerPrivateGStreamer::create(MediaPlayer* player)
290 return adoptPtr(new MediaPlayerPrivateGStreamer(player));
293 void MediaPlayerPrivateGStreamer::registerMediaEngine(MediaEngineRegistrar registrar)
296 registrar(create, getSupportedTypes, supportsType, 0, 0, 0);
299 bool initializeGStreamerAndRegisterWebKitElements()
301 if (!initializeGStreamer())
304 GRefPtr<GstElementFactory> srcFactory = gst_element_factory_find("webkitwebsrc");
306 #if ENABLE(TIZEN_MEDIA_STREAM)
307 return gst_element_register(0, "webkitcamerasrc", GST_RANK_PRIMARY + 200, WEBKIT_TYPE_CAMERA_SRC);
309 GST_DEBUG_CATEGORY_INIT(webkit_media_player_debug, "webkitmediaplayer", 0, "WebKit media player");
310 return gst_element_register(0, "webkitwebsrc", GST_RANK_PRIMARY + 100, WEBKIT_TYPE_WEB_SRC);
311 #endif // ENABLE(TIZEN_MEDIA_STREAM)
316 bool MediaPlayerPrivateGStreamer::isAvailable()
318 if (!initializeGStreamerAndRegisterWebKitElements())
321 GRefPtr<GstElementFactory> factory = gst_element_factory_find(gPlaybinName);
325 MediaPlayerPrivateGStreamer::MediaPlayerPrivateGStreamer(MediaPlayer* player)
327 , m_webkitVideoSink(0)
328 #if ENABLE(TIZEN_GSTREAMER_VIDEO)
334 , m_changingRate(false)
335 , m_endTime(numeric_limits<float>::infinity())
336 , m_isEndReached(false)
337 , m_networkState(MediaPlayer::Empty)
338 , m_readyState(MediaPlayer::HaveNothing)
339 , m_isStreaming(false)
342 , m_mediaLocations(0)
343 , m_mediaLocationCurrentIndex(0)
344 , m_resetPipeline(false)
349 , m_errorOccured(false)
351 , m_startedBuffering(false)
352 , m_fillTimer(this, &MediaPlayerPrivateGStreamer::fillTimerFired)
354 , m_bufferingPercentage(0)
355 , m_preload(MediaPlayer::Auto)
356 , m_delayingLoad(false)
357 , m_mediaDurationKnown(true)
358 , m_maxTimeLoadedAtLastDidLoadingProgress(0)
359 , m_volumeTimerHandler(0)
360 , m_muteTimerHandler(0)
363 , m_audioTimerHandler(0)
364 , m_videoTimerHandler(0)
365 , m_webkitAudioSink(0)
367 #if ENABLE(TIZEN_ACCELERATED_COMPOSITING) && USE(TIZEN_TEXTURE_MAPPER)
368 , m_videoLayer(VideoLayerTizen::create(static_cast<HTMLMediaElement*>(player->mediaPlayerClient())))
369 #endif // ENABLE(TIZEN_ACCELERATED_COMPOSITING) && USE(TIZEN_TEXTURE_MAPPER)
370 #if ENABLE(TIZEN_GSTREAMER_AUDIO)
371 , m_audioSessionManager(AudioSessionManagerGStreamerTizen::createAudioSessionManager())
373 , m_originalPreloadWasAutoAndWasOverridden(false)
374 , m_preservesPitch(false)
375 #if ENABLE(TIZEN_GSTREAMER_VIDEO)
381 MediaPlayerPrivateGStreamer::~MediaPlayerPrivateGStreamer()
383 if (m_fillTimer.isActive())
387 gst_buffer_unref(m_buffer);
390 if (m_mediaLocations) {
391 gst_structure_free(m_mediaLocations);
392 m_mediaLocations = 0;
395 #ifndef GST_API_VERSION_1
396 if (m_videoSinkBin) {
397 gst_object_unref(m_videoSinkBin);
403 GRefPtr<GstBus> bus = webkitGstPipelineGetBus(GST_PIPELINE(m_playBin.get()));
405 g_signal_handlers_disconnect_by_func(bus.get(), reinterpret_cast<gpointer>(mediaPlayerPrivateMessageCallback), this);
406 gst_bus_remove_signal_watch(bus.get());
408 gst_element_set_state(m_playBin.get(), GST_STATE_NULL);
412 #if ENABLE(TIZEN_GSTREAMER_AUDIO)
413 if (m_audioSessionManager)
414 m_audioSessionManager->setSoundState(ASM_STATE_STOP);
419 if (m_muteTimerHandler)
420 g_source_remove(m_muteTimerHandler);
422 if (m_volumeTimerHandler)
423 g_source_remove(m_volumeTimerHandler);
425 if (m_videoTimerHandler)
426 g_source_remove(m_videoTimerHandler);
428 if (m_audioTimerHandler)
429 g_source_remove(m_audioTimerHandler);
432 void MediaPlayerPrivateGStreamer::load(const String& url)
434 if (!initializeGStreamerAndRegisterWebKitElements())
437 #if ENABLE(TIZEN_MEDIA_STREAM) || ENABLE(TIZEN_FILE_SYSTEM)
439 #endif // ENABLE(TIZEN_MEDIA_STREAM) || ENABLE(TIZEN_FILE_SYSTEM)
440 KURL kurl(KURL(), url);
441 String cleanUrl(url);
443 // Clean out everything after file:// url path.
444 if (kurl.isLocalFile())
445 cleanUrl = cleanUrl.substring(0, kurl.pathEnd());
449 setDownloadBuffering();
454 m_url = KURL(KURL(), cleanUrl);
455 g_object_set(m_playBin.get(), "uri", cleanUrl.utf8().data(), NULL);
457 LOG_MEDIA_MESSAGE("Load %s", cleanUrl.utf8().data());
459 if (m_preload == MediaPlayer::None) {
460 LOG_MEDIA_MESSAGE("Delaying load.");
461 m_delayingLoad = true;
464 // Reset network and ready states. Those will be set properly once
465 // the pipeline pre-rolled.
466 #if ENABLE(TIZEN_GSTREAMER_VIDEO)
468 m_networkState = MediaPlayer::Loading;
470 m_networkState = MediaPlayer::Idle;
472 m_networkState = MediaPlayer::Loading;
474 m_player->networkStateChanged();
475 m_readyState = MediaPlayer::HaveNothing;
476 m_player->readyStateChanged();
477 m_volumeAndMuteInitialized = false;
479 // GStreamer needs to have the pipeline set to a paused state to
480 // start providing anything useful.
481 #if ENABLE(TIZEN_GSTREAMER_VIDEO)
484 gst_element_set_state(m_playBin.get(), GST_STATE_PAUSED);
486 #if ENABLE(TIZEN_GSTREAMER_AUDIO)
487 if (m_audioSessionManager && !m_delayingLoad)
488 m_audioSessionManager->setSoundState(ASM_STATE_PAUSE);
495 void MediaPlayerPrivateGStreamer::commitLoad()
497 ASSERT(!m_delayingLoad);
498 LOG_MEDIA_MESSAGE("Committing load.");
502 float MediaPlayerPrivateGStreamer::playbackPosition() const
504 if (m_isEndReached) {
505 // Position queries on a null pipeline return 0. If we're at
506 // the end of the stream the pipeline is null but we want to
507 // report either the seek time or the duration because this is
508 // what the Media element spec expects us to do.
512 return m_mediaDuration;
517 GstQuery* query = gst_query_new_position(GST_FORMAT_TIME);
518 if (!gst_element_query(m_playBin.get(), query)) {
519 LOG_MEDIA_MESSAGE("Position query failed...");
520 gst_query_unref(query);
525 gst_query_parse_position(query, 0, &position);
527 // Position is available only if the pipeline is not in GST_STATE_NULL or
528 // GST_STATE_READY state.
529 if (position != static_cast<gint64>(GST_CLOCK_TIME_NONE))
530 ret = static_cast<double>(position) / GST_SECOND;
532 LOG_MEDIA_MESSAGE("Position %" GST_TIME_FORMAT, GST_TIME_ARGS(position));
534 gst_query_unref(query);
539 bool MediaPlayerPrivateGStreamer::changePipelineState(GstState newState)
541 ASSERT(newState == GST_STATE_PLAYING || newState == GST_STATE_PAUSED);
543 GstState currentState;
546 gst_element_get_state(m_playBin.get(), ¤tState, &pending, 0);
547 LOG_MEDIA_MESSAGE("Current state: %s, pending: %s", gst_element_state_get_name(currentState), gst_element_state_get_name(pending));
548 #if ENABLE(TIZEN_DLOG_SUPPORT)
549 TIZEN_LOGI("Current state: %s, New state: %s", gst_element_state_get_name(currentState), gst_element_state_get_name(newState));
551 if (currentState == newState || pending == newState)
554 GstStateChangeReturn setStateResult = gst_element_set_state(m_playBin.get(), newState);
555 GstState pausedOrPlaying = newState == GST_STATE_PLAYING ? GST_STATE_PAUSED : GST_STATE_PLAYING;
556 if (currentState != pausedOrPlaying && setStateResult == GST_STATE_CHANGE_FAILURE) {
557 loadingFailed(MediaPlayer::Empty);
563 void MediaPlayerPrivateGStreamer::prepareToPlay()
565 m_isEndReached = false;
568 if (m_delayingLoad) {
569 m_delayingLoad = false;
574 void MediaPlayerPrivateGStreamer::play()
576 #if ENABLE(TIZEN_GSTREAMER_AUDIO)
577 if (m_audioSessionManager && !m_audioSessionManager->setSoundState(ASM_STATE_PLAYING))
580 if (changePipelineState(GST_STATE_PLAYING)) {
581 m_isEndReached = false;
582 LOG_MEDIA_MESSAGE("Play");
586 void MediaPlayerPrivateGStreamer::pause()
588 #if ENABLE(TIZEN_GSTREAMER_AUDIO)
589 if (m_audioSessionManager && !m_audioSessionManager->setSoundState(ASM_STATE_PAUSE))
595 if (changePipelineState(GST_STATE_PAUSED))
596 LOG_MEDIA_MESSAGE("Pause");
599 float MediaPlayerPrivateGStreamer::duration() const
607 // Media duration query failed already, don't attempt new useless queries.
608 if (!m_mediaDurationKnown)
609 return numeric_limits<float>::infinity();
612 return m_mediaDuration;
614 GstFormat timeFormat = GST_FORMAT_TIME;
615 gint64 timeLength = 0;
617 #ifdef GST_API_VERSION_1
618 bool failure = !gst_element_query_duration(m_playBin.get(), timeFormat, &timeLength) || static_cast<guint64>(timeLength) == GST_CLOCK_TIME_NONE;
620 bool failure = !gst_element_query_duration(m_playBin.get(), &timeFormat, &timeLength) || timeFormat != GST_FORMAT_TIME || static_cast<guint64>(timeLength) == GST_CLOCK_TIME_NONE;
623 LOG_MEDIA_MESSAGE("Time duration query failed for %s", m_url.string().utf8().data());
624 return numeric_limits<float>::infinity();
627 LOG_MEDIA_MESSAGE("Duration: %" GST_TIME_FORMAT, GST_TIME_ARGS(timeLength));
629 return static_cast<double>(timeLength) / GST_SECOND;
630 // FIXME: handle 3.14.9.5 properly
633 float MediaPlayerPrivateGStreamer::currentTime() const
645 // https://bugzilla.gnome.org/show_bug.cgi?id=639941 In GStreamer
646 // 0.10.35 basesink reports wrong duration in case of EOS and
647 // negative playback rate. There's no upstream accepted patch for
648 // this bug yet, hence this temporary workaround.
649 if (m_isEndReached && m_playbackRate < 0)
652 return playbackPosition();
656 void MediaPlayerPrivateGStreamer::seek(float time)
664 LOG_MEDIA_MESSAGE("Seek attempt to %f secs", time);
665 #if ENABLE(TIZEN_DLOG_SUPPORT)
666 TIZEN_LOGI("Seek attempt to %f secs", time);
669 // Avoid useless seeking.
670 if (time == currentTime())
673 // Extract the integer part of the time (seconds) and the
674 // fractional part (microseconds). Attempt to round the
675 // microseconds so no floating point precision is lost and we can
676 // perform an accurate seek.
678 float microSeconds = modf(time, &seconds) * 1000000;
680 timeValue.tv_sec = static_cast<glong>(seconds);
681 timeValue.tv_usec = static_cast<glong>(roundf(microSeconds / 10000) * 10000);
683 GstClockTime clockTime = GST_TIMEVAL_TO_TIME(timeValue);
684 LOG_MEDIA_MESSAGE("Seek: %" GST_TIME_FORMAT, GST_TIME_ARGS(clockTime));
686 if (!gst_element_seek(m_playBin.get(), m_player->rate(),
688 (GstSeekFlags)(GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_ACCURATE),
689 GST_SEEK_TYPE_SET, clockTime,
690 GST_SEEK_TYPE_NONE, GST_CLOCK_TIME_NONE)) {
691 LOG_MEDIA_MESSAGE("Seek to %f failed", time);
692 #if ENABLE(TIZEN_DLOG_SUPPORT)
693 TIZEN_LOGE("Seek to %f failed", time);
701 bool MediaPlayerPrivateGStreamer::paused() const
703 if (m_isEndReached) {
704 LOG_MEDIA_MESSAGE("Ignoring pause at EOS");
709 gst_element_get_state(m_playBin.get(), &state, 0, 0);
710 return state == GST_STATE_PAUSED;
713 bool MediaPlayerPrivateGStreamer::seeking() const
718 // Returns the size of the video
719 IntSize MediaPlayerPrivateGStreamer::naturalSize() const
724 if (!m_videoSize.isEmpty())
727 GRefPtr<GstCaps> caps = webkitGstGetPadCaps(m_videoSinkPad.get());
732 // TODO: handle possible clean aperture data. See
733 // https://bugzilla.gnome.org/show_bug.cgi?id=596571
734 // TODO: handle possible transformation matrix. See
735 // https://bugzilla.gnome.org/show_bug.cgi?id=596326
737 // Get the video PAR and original size, if this fails the
738 // video-sink has likely not yet negotiated its caps.
739 int pixelAspectRatioNumerator, pixelAspectRatioDenominator, stride;
740 IntSize originalSize;
741 GstVideoFormat format;
742 if (!getVideoSizeAndFormatFromCaps(caps.get(), originalSize, format, pixelAspectRatioNumerator, pixelAspectRatioDenominator, stride))
745 LOG_MEDIA_MESSAGE("Original video size: %dx%d", originalSize.width(), originalSize.height());
746 LOG_MEDIA_MESSAGE("Pixel aspect ratio: %d/%d", pixelAspectRatioNumerator, pixelAspectRatioDenominator);
748 // Calculate DAR based on PAR and video size.
749 int displayWidth = originalSize.width() * pixelAspectRatioNumerator;
750 int displayHeight = originalSize.height() * pixelAspectRatioDenominator;
752 // Divide display width and height by their GCD to avoid possible overflows.
753 int displayAspectRatioGCD = greatestCommonDivisor(displayWidth, displayHeight);
754 displayWidth /= displayAspectRatioGCD;
755 displayHeight /= displayAspectRatioGCD;
757 // Apply DAR to original video size. This is the same behavior as in xvimagesink's setcaps function.
758 guint64 width = 0, height = 0;
759 if (!(originalSize.height() % displayHeight)) {
760 LOG_MEDIA_MESSAGE("Keeping video original height");
761 width = gst_util_uint64_scale_int(originalSize.height(), displayWidth, displayHeight);
762 height = static_cast<guint64>(originalSize.height());
763 } else if (!(originalSize.width() % displayWidth)) {
764 LOG_MEDIA_MESSAGE("Keeping video original width");
765 height = gst_util_uint64_scale_int(originalSize.width(), displayHeight, displayWidth);
766 width = static_cast<guint64>(originalSize.width());
768 LOG_MEDIA_MESSAGE("Approximating while keeping original video height");
769 width = gst_util_uint64_scale_int(originalSize.height(), displayWidth, displayHeight);
770 height = static_cast<guint64>(originalSize.height());
773 LOG_MEDIA_MESSAGE("Natural size: %" G_GUINT64_FORMAT "x%" G_GUINT64_FORMAT, width, height);
774 m_videoSize = IntSize(static_cast<int>(width), static_cast<int>(height));
778 void MediaPlayerPrivateGStreamer::videoChanged()
780 if (m_videoTimerHandler)
781 g_source_remove(m_videoTimerHandler);
782 m_videoTimerHandler = g_timeout_add(0, reinterpret_cast<GSourceFunc>(mediaPlayerPrivateVideoChangeTimeoutCallback), this);
785 void MediaPlayerPrivateGStreamer::notifyPlayerOfVideo()
787 m_videoTimerHandler = 0;
789 gint videoTracks = 0;
791 g_object_get(m_playBin.get(), "n-video", &videoTracks, NULL);
793 m_hasVideo = videoTracks > 0;
795 #if ENABLE(TIZEN_ACCELERATED_COMPOSITING) && USE(TIZEN_TEXTURE_MAPPER) && ENABLE(TIZEN_WEBKIT2_TILED_AC_SHARED_PLATFORM_SURFACE)
798 if (gst_video_get_size(m_videoSinkPad.get(), &width, &height)) {
799 IntSize size(width, height);
800 if (m_videoSize != size) {
801 m_videoSize = IntSize();
802 m_videoLayer->setOverlay(naturalSize());
806 m_videoSize = IntSize();
808 m_player->mediaPlayerClient()->mediaPlayerEngineUpdated(m_player);
811 void MediaPlayerPrivateGStreamer::audioChanged()
813 if (m_audioTimerHandler)
814 g_source_remove(m_audioTimerHandler);
815 m_audioTimerHandler = g_timeout_add(0, reinterpret_cast<GSourceFunc>(mediaPlayerPrivateAudioChangeTimeoutCallback), this);
818 void MediaPlayerPrivateGStreamer::notifyPlayerOfAudio()
820 m_audioTimerHandler = 0;
822 gint audioTracks = 0;
824 g_object_get(m_playBin.get(), "n-audio", &audioTracks, NULL);
825 m_hasAudio = audioTracks > 0;
826 m_player->mediaPlayerClient()->mediaPlayerEngineUpdated(m_player);
829 void MediaPlayerPrivateGStreamer::setVolume(float volume)
834 gst_stream_volume_set_volume(GST_STREAM_VOLUME(m_playBin.get()), GST_STREAM_VOLUME_FORMAT_CUBIC,
835 static_cast<double>(volume));
838 void MediaPlayerPrivateGStreamer::notifyPlayerOfVolumeChange()
840 m_volumeTimerHandler = 0;
842 if (!m_player || !m_playBin)
845 volume = gst_stream_volume_get_volume(GST_STREAM_VOLUME(m_playBin.get()), GST_STREAM_VOLUME_FORMAT_CUBIC);
846 // get_volume() can return values superior to 1.0 if the user
847 // applies software user gain via third party application (GNOME
848 // volume control for instance).
849 volume = CLAMP(volume, 0.0, 1.0);
850 m_player->volumeChanged(static_cast<float>(volume));
853 void MediaPlayerPrivateGStreamer::volumeChanged()
855 if (m_volumeTimerHandler)
856 g_source_remove(m_volumeTimerHandler);
857 m_volumeTimerHandler = g_timeout_add(0, reinterpret_cast<GSourceFunc>(mediaPlayerPrivateVolumeChangeTimeoutCallback), this);
860 void MediaPlayerPrivateGStreamer::setRate(float rate)
862 // Avoid useless playback rate update.
863 if (m_playbackRate == rate)
869 gst_element_get_state(m_playBin.get(), &state, &pending, 0);
870 if ((state != GST_STATE_PLAYING && state != GST_STATE_PAUSED)
871 || (pending == GST_STATE_PAUSED))
877 m_playbackRate = rate;
878 m_changingRate = true;
881 gst_element_set_state(m_playBin.get(), GST_STATE_PAUSED);
882 #if ENABLE(TIZEN_GSTREAMER_AUDIO)
883 if (m_audioSessionManager)
884 m_audioSessionManager->setSoundState(ASM_STATE_PAUSE);
889 float currentPosition = static_cast<float>(playbackPosition() * GST_SECOND);
890 GstSeekFlags flags = (GstSeekFlags)(GST_SEEK_FLAG_FLUSH);
894 LOG_MEDIA_MESSAGE("Set Rate to %f", rate);
896 // Mute the sound if the playback rate is too extreme and
897 // audio pitch is not adjusted.
898 mute = (!m_preservesPitch && (rate < 0.8 || rate > 2));
899 start = currentPosition;
900 end = GST_CLOCK_TIME_NONE;
905 // If we are at beginning of media, start from the end to
906 // avoid immediate EOS.
907 if (currentPosition <= 0)
908 end = static_cast<gint64>(duration() * GST_SECOND);
910 end = currentPosition;
913 LOG_MEDIA_MESSAGE("Need to mute audio: %d", (int) mute);
915 if (!gst_element_seek(m_playBin.get(), rate, GST_FORMAT_TIME, flags,
916 GST_SEEK_TYPE_SET, start,
917 GST_SEEK_TYPE_SET, end))
918 LOG_MEDIA_MESSAGE("Set rate to %f failed", rate);
920 g_object_set(m_playBin.get(), "mute", mute, NULL);
923 void MediaPlayerPrivateGStreamer::setPreservesPitch(bool preservesPitch)
925 m_preservesPitch = preservesPitch;
928 MediaPlayer::NetworkState MediaPlayerPrivateGStreamer::networkState() const
930 return m_networkState;
933 MediaPlayer::ReadyState MediaPlayerPrivateGStreamer::readyState() const
938 PassRefPtr<TimeRanges> MediaPlayerPrivateGStreamer::buffered() const
940 RefPtr<TimeRanges> timeRanges = TimeRanges::create();
941 if (m_errorOccured || isLiveStream())
942 return timeRanges.release();
944 #if GST_CHECK_VERSION(0, 10, 31)
945 float mediaDuration(duration());
946 if (!mediaDuration || isinf(mediaDuration))
947 return timeRanges.release();
949 GstQuery* query = gst_query_new_buffering(GST_FORMAT_PERCENT);
951 if (!gst_element_query(m_playBin.get(), query)) {
952 gst_query_unref(query);
953 return timeRanges.release();
956 gint64 rangeStart = 0, rangeStop = 0;
957 for (guint index = 0; index < gst_query_get_n_buffering_ranges(query); index++) {
958 if (gst_query_parse_nth_buffering_range(query, index, &rangeStart, &rangeStop))
959 timeRanges->add(static_cast<float>((rangeStart * mediaDuration) / 100),
960 static_cast<float>((rangeStop * mediaDuration) / 100));
963 // Fallback to the more general maxTimeLoaded() if no range has
965 if (!timeRanges->length())
966 if (float loaded = maxTimeLoaded())
967 timeRanges->add(0, loaded);
969 gst_query_unref(query);
971 float loaded = maxTimeLoaded();
972 if (!m_errorOccured && !isLiveStream() && loaded > 0)
973 timeRanges->add(0, loaded);
975 return timeRanges.release();
978 gboolean MediaPlayerPrivateGStreamer::handleMessage(GstMessage* message)
981 GOwnPtr<gchar> debug;
982 MediaPlayer::NetworkState error;
983 bool issueError = true;
984 bool attemptNextLocation = false;
985 const GstStructure* structure = gst_message_get_structure(message);
988 const gchar* messageTypeName = gst_structure_get_name(structure);
990 // Redirect messages are sent from elements, like qtdemux, to
991 // notify of the new location(s) of the media.
992 if (!g_strcmp0(messageTypeName, "redirect")) {
993 mediaLocationChanged(message);
998 LOG_MEDIA_MESSAGE("Message received from element %s", GST_MESSAGE_SRC_NAME(message));
999 switch (GST_MESSAGE_TYPE(message)) {
1000 case GST_MESSAGE_ERROR:
1001 if (m_resetPipeline)
1003 gst_message_parse_error(message, &err.outPtr(), &debug.outPtr());
1004 LOG_MEDIA_MESSAGE("Error %d: %s (url=%s)", err->code, err->message, m_url.string().utf8().data());
1006 #if ENABLE(TIZEN_DLOG_SUPPORT)
1007 TIZEN_LOGE("GST_MESSAGE_ERROR %d: %s (url=%s)", err->code, err->message, m_url.string().utf8().data());
1010 GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS(GST_BIN(m_playBin.get()), GST_DEBUG_GRAPH_SHOW_ALL, "webkit-video.error");
1012 error = MediaPlayer::Empty;
1013 if (err->code == GST_STREAM_ERROR_CODEC_NOT_FOUND
1014 || err->code == GST_STREAM_ERROR_WRONG_TYPE
1015 || err->code == GST_STREAM_ERROR_FAILED
1016 || err->code == GST_CORE_ERROR_MISSING_PLUGIN
1017 || err->code == GST_RESOURCE_ERROR_NOT_FOUND)
1018 error = MediaPlayer::FormatError;
1019 else if (err->domain == GST_STREAM_ERROR) {
1020 // Let the mediaPlayerClient handle the stream error, in
1021 // this case the HTMLMediaElement will emit a stalled
1023 #if !ENABLE(TIZEN_MEDIA_STREAM)
1024 if (err->code == GST_STREAM_ERROR_TYPE_NOT_FOUND) {
1025 LOG_MEDIA_MESSAGE("Decode error, let the Media element emit a stalled event.");
1029 error = MediaPlayer::DecodeError;
1030 attemptNextLocation = true;
1031 } else if (err->domain == GST_RESOURCE_ERROR)
1032 error = MediaPlayer::NetworkError;
1034 if (attemptNextLocation)
1035 issueError = !loadNextLocation();
1037 loadingFailed(error);
1039 case GST_MESSAGE_EOS:
1040 LOG_MEDIA_MESSAGE("End of Stream");
1043 case GST_MESSAGE_STATE_CHANGED:
1044 // Ignore state changes if load is delayed (preload=none). The
1045 // player state will be updated once commitLoad() is called.
1046 if (m_delayingLoad) {
1047 LOG_MEDIA_MESSAGE("Media load has been delayed. Ignoring state changes for now");
1051 // Ignore state changes from internal elements. They are
1052 // forwarded to playbin2 anyway.
1053 if (GST_MESSAGE_SRC(message) == reinterpret_cast<GstObject*>(m_playBin.get())) {
1056 // Construct a filename for the graphviz dot file output.
1057 GstState oldState, newState;
1058 gst_message_parse_state_changed(message, &oldState, &newState, 0);
1060 CString dotFileName = String::format("webkit-video.%s_%s",
1061 gst_element_state_get_name(oldState),
1062 gst_element_state_get_name(newState)).utf8();
1064 GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS(GST_BIN(m_playBin.get()), GST_DEBUG_GRAPH_SHOW_ALL, dotFileName.data());
1066 #if ENABLE(TIZEN_GSTREAMER_VIDEO)
1067 if (m_suspendTime > 0 && newState == GST_STATE_PAUSED) {
1068 seek(m_suspendTime);
1070 m_player->mediaPlayerClient()->setSuspended(false);
1073 HTMLMediaElement* element = static_cast<HTMLMediaElement*>(player()->mediaPlayerClient());
1074 if (element->isVideo()) {
1075 if (oldState == GST_STATE_PAUSED && newState == GST_STATE_PLAYING)
1076 power_lock_state(POWER_STATE_NORMAL, 0);
1077 else if (oldState == GST_STATE_PLAYING && newState == GST_STATE_PAUSED)
1078 power_unlock_state(POWER_STATE_NORMAL);
1083 case GST_MESSAGE_BUFFERING:
1084 processBufferingStats(message);
1086 case GST_MESSAGE_DURATION:
1087 LOG_MEDIA_MESSAGE("Duration changed");
1091 LOG_MEDIA_MESSAGE("Unhandled GStreamer message type: %s",
1092 GST_MESSAGE_TYPE_NAME(message));
1098 void MediaPlayerPrivateGStreamer::processBufferingStats(GstMessage* message)
1100 // This is the immediate buffering that needs to happen so we have
1101 // enough to play right now.
1103 const GstStructure *structure = gst_message_get_structure(message);
1104 gst_structure_get_int(structure, "buffer-percent", &m_bufferingPercentage);
1106 LOG_MEDIA_MESSAGE("[Buffering] Buffering: %d%%.", m_bufferingPercentage);
1108 GstBufferingMode mode;
1109 gst_message_parse_buffering_stats(message, &mode, 0, 0, 0);
1110 if (mode != GST_BUFFERING_DOWNLOAD) {
1115 // This is on-disk buffering, that allows us to download much more
1116 // than needed for right now.
1117 if (!m_startedBuffering) {
1118 LOG_MEDIA_MESSAGE("[Buffering] Starting on-disk buffering.");
1120 m_startedBuffering = true;
1122 if (m_fillTimer.isActive())
1125 m_fillTimer.startRepeating(0.2);
1129 void MediaPlayerPrivateGStreamer::fillTimerFired(Timer<MediaPlayerPrivateGStreamer>*)
1131 GstQuery* query = gst_query_new_buffering(GST_FORMAT_PERCENT);
1133 if (!gst_element_query(m_playBin.get(), query)) {
1134 gst_query_unref(query);
1139 gdouble fillStatus = 100.0;
1141 gst_query_parse_buffering_range(query, 0, &start, &stop, 0);
1142 gst_query_unref(query);
1145 fillStatus = 100.0 * stop / GST_FORMAT_PERCENT_MAX;
1147 LOG_MEDIA_MESSAGE("[Buffering] Download buffer filled up to %f%%", fillStatus);
1149 if (!m_mediaDuration)
1152 // Update maxTimeLoaded only if the media duration is
1153 // available. Otherwise we can't compute it.
1154 if (m_mediaDuration) {
1155 if (fillStatus == 100.0)
1156 m_maxTimeLoaded = m_mediaDuration;
1158 m_maxTimeLoaded = static_cast<float>((fillStatus * m_mediaDuration) / 100.0);
1159 LOG_MEDIA_MESSAGE("[Buffering] Updated maxTimeLoaded: %f", m_maxTimeLoaded);
1162 if (fillStatus != 100.0) {
1167 // Media is now fully loaded. It will play even if network
1168 // connection is cut. Buffering is done, remove the fill source
1169 // from the main loop.
1171 m_startedBuffering = false;
1175 float MediaPlayerPrivateGStreamer::maxTimeSeekable() const
1180 LOG_MEDIA_MESSAGE("maxTimeSeekable");
1181 // infinite duration means live stream
1182 if (isinf(duration()))
1188 float MediaPlayerPrivateGStreamer::maxTimeLoaded() const
1193 float loaded = m_maxTimeLoaded;
1194 if (!loaded && !m_fillTimer.isActive())
1195 loaded = duration();
1196 LOG_MEDIA_MESSAGE("maxTimeLoaded: %f", loaded);
1200 bool MediaPlayerPrivateGStreamer::didLoadingProgress() const
1202 if (!m_playBin || !m_mediaDuration || !totalBytes())
1204 float currentMaxTimeLoaded = maxTimeLoaded();
1205 bool didLoadingProgress = currentMaxTimeLoaded != m_maxTimeLoadedAtLastDidLoadingProgress;
1206 m_maxTimeLoadedAtLastDidLoadingProgress = currentMaxTimeLoaded;
1207 LOG_MEDIA_MESSAGE("didLoadingProgress: %d", didLoadingProgress);
1208 return didLoadingProgress;
1211 unsigned MediaPlayerPrivateGStreamer::totalBytes() const
1216 if (m_totalBytes != -1)
1217 return m_totalBytes;
1222 GstFormat fmt = GST_FORMAT_BYTES;
1224 #ifdef GST_API_VERSION_1
1225 if (gst_element_query_duration(m_source.get(), fmt, &length)) {
1227 if (gst_element_query_duration(m_source.get(), &fmt, &length)) {
1229 LOG_MEDIA_MESSAGE("totalBytes %" G_GINT64_FORMAT, length);
1230 m_totalBytes = static_cast<unsigned>(length);
1231 m_isStreaming = !length;
1232 return m_totalBytes;
1235 // Fall back to querying the source pads manually.
1236 // See also https://bugzilla.gnome.org/show_bug.cgi?id=638749
1237 GstIterator* iter = gst_element_iterate_src_pads(m_source.get());
1240 #ifdef GST_API_VERSION_1
1241 GValue item = G_VALUE_INIT;
1242 switch (gst_iterator_next(iter, &item)) {
1243 case GST_ITERATOR_OK: {
1244 GstPad* pad = static_cast<GstPad*>(g_value_get_object(&item));
1245 gint64 padLength = 0;
1246 if (gst_pad_query_duration(pad, fmt, &padLength) && padLength > length)
1253 switch (gst_iterator_next(iter, &data)) {
1254 case GST_ITERATOR_OK: {
1255 GRefPtr<GstPad> pad = adoptGRef(GST_PAD_CAST(data));
1256 gint64 padLength = 0;
1257 if (gst_pad_query_duration(pad.get(), &fmt, &padLength) && padLength > length)
1262 case GST_ITERATOR_RESYNC:
1263 gst_iterator_resync(iter);
1265 case GST_ITERATOR_ERROR:
1267 case GST_ITERATOR_DONE:
1272 #ifdef GST_API_VERSION_1
1273 g_value_unset(&item);
1277 gst_iterator_free(iter);
1279 LOG_MEDIA_MESSAGE("totalBytes %" G_GINT64_FORMAT, length);
1280 m_totalBytes = static_cast<unsigned>(length);
1281 m_isStreaming = !length;
1282 return m_totalBytes;
1285 unsigned MediaPlayerPrivateGStreamer::decodedFrameCount() const
1287 guint64 decodedFrames = 0;
1289 g_object_get(m_fpsSink, "frames-rendered", &decodedFrames, NULL);
1290 return static_cast<unsigned>(decodedFrames);
1293 unsigned MediaPlayerPrivateGStreamer::droppedFrameCount() const
1295 guint64 framesDropped = 0;
1297 g_object_get(m_fpsSink, "frames-dropped", &framesDropped, NULL);
1298 return static_cast<unsigned>(framesDropped);
1301 unsigned MediaPlayerPrivateGStreamer::audioDecodedByteCount() const
1303 GstQuery* query = gst_query_new_position(GST_FORMAT_BYTES);
1304 gint64 position = 0;
1306 if (m_webkitAudioSink && gst_element_query(m_webkitAudioSink.get(), query))
1307 gst_query_parse_position(query, 0, &position);
1309 gst_query_unref(query);
1310 return static_cast<unsigned>(position);
1313 unsigned MediaPlayerPrivateGStreamer::videoDecodedByteCount() const
1315 GstQuery* query = gst_query_new_position(GST_FORMAT_BYTES);
1316 gint64 position = 0;
1318 if (gst_element_query(m_webkitVideoSink, query))
1319 gst_query_parse_position(query, 0, &position);
1321 gst_query_unref(query);
1322 return static_cast<unsigned>(position);
1325 void MediaPlayerPrivateGStreamer::updateAudioSink()
1330 GstElement* sinkPtr = 0;
1332 g_object_get(m_playBin.get(), "audio-sink", &sinkPtr, NULL);
1333 m_webkitAudioSink = adoptGRef(sinkPtr);
1338 void MediaPlayerPrivateGStreamer::sourceChanged()
1340 GstElement* srcPtr = 0;
1342 g_object_get(m_playBin.get(), "source", &srcPtr, NULL);
1343 m_source = adoptGRef(srcPtr);
1345 if (WEBKIT_IS_WEB_SRC(m_source.get()))
1346 webKitWebSrcSetMediaPlayer(WEBKIT_WEB_SRC(m_source.get()), m_player);
1349 void MediaPlayerPrivateGStreamer::cancelLoad()
1351 if (m_networkState < MediaPlayer::Loading || m_networkState == MediaPlayer::Loaded)
1355 gst_element_set_state(m_playBin.get(), GST_STATE_NULL);
1356 #if ENABLE(TIZEN_GSTREAMER_AUDIO)
1357 if (m_audioSessionManager)
1358 m_audioSessionManager->setSoundState(ASM_STATE_STOP);
1362 void MediaPlayerPrivateGStreamer::updateStates()
1370 MediaPlayer::NetworkState oldNetworkState = m_networkState;
1371 MediaPlayer::ReadyState oldReadyState = m_readyState;
1375 GstStateChangeReturn ret = gst_element_get_state(m_playBin.get(),
1376 &state, &pending, 250 * GST_NSECOND);
1378 bool shouldUpdateAfterSeek = false;
1380 case GST_STATE_CHANGE_SUCCESS:
1381 LOG_MEDIA_MESSAGE("State: %s, pending: %s",
1382 gst_element_state_get_name(state),
1383 gst_element_state_get_name(pending));
1385 m_resetPipeline = state <= GST_STATE_READY;
1387 // Try to figure out ready and network states.
1388 if (state == GST_STATE_READY) {
1389 m_readyState = MediaPlayer::HaveMetadata;
1390 m_networkState = MediaPlayer::Empty;
1391 // Cache the duration without emiting the durationchange
1392 // event because it's taken care of by the media element
1393 // in this precise case.
1394 if (!m_isEndReached)
1396 } else if ((state == GST_STATE_NULL) || (maxTimeLoaded() == duration())) {
1397 m_networkState = MediaPlayer::Loaded;
1398 m_readyState = MediaPlayer::HaveEnoughData;
1400 m_readyState = currentTime() < maxTimeLoaded() ? MediaPlayer::HaveFutureData : MediaPlayer::HaveCurrentData;
1401 m_networkState = MediaPlayer::Loading;
1404 if (m_buffering && state != GST_STATE_READY) {
1405 m_readyState = MediaPlayer::HaveCurrentData;
1406 m_networkState = MediaPlayer::Loading;
1409 // Now let's try to get the states in more detail using
1410 // information from GStreamer, while we sync states where
1412 if (state == GST_STATE_PAUSED) {
1413 if (!m_webkitAudioSink)
1416 if (!m_volumeAndMuteInitialized) {
1417 notifyPlayerOfVolumeChange();
1418 notifyPlayerOfMute();
1419 m_volumeAndMuteInitialized = true;
1422 if (m_buffering && m_bufferingPercentage == 100) {
1423 m_buffering = false;
1424 m_bufferingPercentage = 0;
1425 m_readyState = MediaPlayer::HaveEnoughData;
1427 LOG_MEDIA_MESSAGE("[Buffering] Complete.");
1430 LOG_MEDIA_MESSAGE("[Buffering] Restarting playback.");
1431 gst_element_set_state(m_playBin.get(), GST_STATE_PLAYING);
1432 #if ENABLE(TIZEN_GSTREAMER_AUDIO)
1433 if (m_audioSessionManager)
1434 m_audioSessionManager->setSoundState(ASM_STATE_PLAYING);
1437 } else if (!m_buffering && (currentTime() < duration())) {
1440 } else if (state == GST_STATE_PLAYING) {
1441 m_readyState = MediaPlayer::HaveEnoughData;
1444 if (m_buffering && !isLiveStream()) {
1445 m_readyState = MediaPlayer::HaveCurrentData;
1446 m_networkState = MediaPlayer::Loading;
1448 LOG_MEDIA_MESSAGE("[Buffering] Pausing stream for buffering.");
1450 gst_element_set_state(m_playBin.get(), GST_STATE_PAUSED);
1451 #if ENABLE(TIZEN_GSTREAMER_AUDIO)
1452 if (m_audioSessionManager)
1453 m_audioSessionManager->setSoundState(ASM_STATE_PAUSE);
1459 // Is on-disk buffering in progress?
1460 if (m_fillTimer.isActive())
1461 m_networkState = MediaPlayer::Loading;
1463 if (m_changingRate) {
1464 m_player->rateChanged();
1465 m_changingRate = false;
1469 shouldUpdateAfterSeek = true;
1474 case GST_STATE_CHANGE_ASYNC:
1475 LOG_MEDIA_MESSAGE("Async: State: %s, pending: %s",
1476 gst_element_state_get_name(state),
1477 gst_element_state_get_name(pending));
1478 // Change in progress
1480 // On-disk buffering was attempted but the media is live. This
1481 // can't work so disable on-disk buffering and reset the
1483 if (state == GST_STATE_READY && isLiveStream() && m_preload == MediaPlayer::Auto) {
1484 setPreload(MediaPlayer::None);
1485 gst_element_set_state(m_playBin.get(), GST_STATE_NULL);
1486 gst_element_set_state(m_playBin.get(), GST_STATE_PAUSED);
1487 #if ENABLE(TIZEN_GSTREAMER_AUDIO)
1488 if (m_audioSessionManager)
1489 m_audioSessionManager->setSoundState(ASM_STATE_PAUSE);
1493 // A live stream was paused, reset the pipeline.
1494 if (state == GST_STATE_PAUSED && pending == GST_STATE_PLAYING && isLiveStream()) {
1495 gst_element_set_state(m_playBin.get(), GST_STATE_NULL);
1496 gst_element_set_state(m_playBin.get(), GST_STATE_PLAYING);
1497 #if ENABLE(TIZEN_GSTREAMER_AUDIO)
1498 if (m_audioSessionManager)
1499 m_audioSessionManager->setSoundState(ASM_STATE_PLAYING);
1503 if (!isLiveStream() && !m_buffering)
1507 shouldUpdateAfterSeek = true;
1511 case GST_STATE_CHANGE_FAILURE:
1512 LOG_MEDIA_MESSAGE("Failure: State: %s, pending: %s",
1513 gst_element_state_get_name(state),
1514 gst_element_state_get_name(pending));
1517 case GST_STATE_CHANGE_NO_PREROLL:
1518 LOG_MEDIA_MESSAGE("No preroll: State: %s, pending: %s",
1519 gst_element_state_get_name(state),
1520 gst_element_state_get_name(pending));
1522 if (state == GST_STATE_READY)
1523 m_readyState = MediaPlayer::HaveNothing;
1524 else if (state == GST_STATE_PAUSED) {
1525 m_readyState = MediaPlayer::HaveEnoughData;
1527 // Live pipelines go in PAUSED without prerolling.
1528 m_isStreaming = true;
1529 } else if (state == GST_STATE_PLAYING)
1533 shouldUpdateAfterSeek = true;
1536 gst_element_set_state(m_playBin.get(), GST_STATE_PLAYING);
1537 #if ENABLE(TIZEN_GSTREAMER_AUDIO)
1538 if (m_audioSessionManager)
1539 m_audioSessionManager->setSoundState(ASM_STATE_PLAYING);
1542 } else if (!m_paused) {
1543 gst_element_set_state(m_playBin.get(), GST_STATE_PLAYING);
1544 #if ENABLE(TIZEN_GSTREAMER_AUDIO)
1545 if (m_audioSessionManager)
1546 m_audioSessionManager->setSoundState(ASM_STATE_PLAYING);
1550 m_networkState = MediaPlayer::Loading;
1553 LOG_MEDIA_MESSAGE("Else : %d", ret);
1558 m_readyState = MediaPlayer::HaveNothing;
1560 if (shouldUpdateAfterSeek)
1563 if (m_networkState != oldNetworkState) {
1564 LOG_MEDIA_MESSAGE("Network State Changed from %u to %u",
1565 oldNetworkState, m_networkState);
1566 m_player->networkStateChanged();
1568 if (m_readyState != oldReadyState) {
1569 LOG_MEDIA_MESSAGE("Ready State Changed from %u to %u",
1570 oldReadyState, m_readyState);
1571 m_player->readyStateChanged();
1572 #if ENABLE(TIZEN_ACCELERATED_COMPOSITING) && USE(TIZEN_TEXTURE_MAPPER)
1573 if (state == GST_STATE_PLAYING)
1574 m_player->mediaPlayerClient()->mediaPlayerRenderingModeChanged(m_player);
1579 void MediaPlayerPrivateGStreamer::mediaLocationChanged(GstMessage* message)
1581 if (m_mediaLocations)
1582 gst_structure_free(m_mediaLocations);
1584 const GstStructure* structure = gst_message_get_structure(message);
1586 // This structure can contain:
1587 // - both a new-location string and embedded locations structure
1588 // - or only a new-location string.
1589 m_mediaLocations = gst_structure_copy(structure);
1590 const GValue* locations = gst_structure_get_value(m_mediaLocations, "locations");
1593 m_mediaLocationCurrentIndex = static_cast<int>(gst_value_list_get_size(locations)) -1;
1599 bool MediaPlayerPrivateGStreamer::loadNextLocation()
1601 if (!m_mediaLocations)
1604 const GValue* locations = gst_structure_get_value(m_mediaLocations, "locations");
1605 const gchar* newLocation = 0;
1608 // Fallback on new-location string.
1609 newLocation = gst_structure_get_string(m_mediaLocations, "new-location");
1615 if (m_mediaLocationCurrentIndex < 0) {
1616 m_mediaLocations = 0;
1620 const GValue* location = gst_value_list_get_value(locations,
1621 m_mediaLocationCurrentIndex);
1622 const GstStructure* structure = gst_value_get_structure(location);
1625 m_mediaLocationCurrentIndex--;
1629 newLocation = gst_structure_get_string(structure, "new-location");
1633 // Found a candidate. new-location is not always an absolute url
1634 // though. We need to take the base of the current url and
1635 // append the value of new-location to it.
1637 gchar* currentLocation = 0;
1638 g_object_get(m_playBin.get(), "uri", ¤tLocation, NULL);
1640 KURL currentUrl(KURL(), currentLocation);
1641 g_free(currentLocation);
1645 if (gst_uri_is_valid(newLocation))
1646 newUrl = KURL(KURL(), newLocation);
1648 newUrl = KURL(KURL(), currentUrl.baseAsString() + newLocation);
1650 RefPtr<SecurityOrigin> securityOrigin = SecurityOrigin::create(currentUrl);
1651 if (securityOrigin->canRequest(newUrl)) {
1652 LOG_MEDIA_MESSAGE("New media url: %s", newUrl.string().utf8().data());
1654 // Reset player states.
1655 m_networkState = MediaPlayer::Loading;
1656 m_player->networkStateChanged();
1657 m_readyState = MediaPlayer::HaveNothing;
1658 m_player->readyStateChanged();
1660 // Reset pipeline state.
1661 m_resetPipeline = true;
1662 gst_element_set_state(m_playBin.get(), GST_STATE_READY);
1663 #if ENABLE(TIZEN_GSTREAMER_AUDIO)
1664 if (m_audioSessionManager)
1665 m_audioSessionManager->setSoundState(ASM_STATE_NONE);
1668 gst_element_get_state(m_playBin.get(), &state, 0, 0);
1669 if (state <= GST_STATE_READY) {
1670 // Set the new uri and start playing.
1671 g_object_set(m_playBin.get(), "uri", newUrl.string().utf8().data(), NULL);
1672 gst_element_set_state(m_playBin.get(), GST_STATE_PLAYING);
1673 #if ENABLE(TIZEN_GSTREAMER_AUDIO)
1674 if (m_audioSessionManager)
1675 m_audioSessionManager->setSoundState(ASM_STATE_PLAYING);
1681 m_mediaLocationCurrentIndex--;
1686 void MediaPlayerPrivateGStreamer::loadStateChanged()
1691 void MediaPlayerPrivateGStreamer::sizeChanged()
1696 void MediaPlayerPrivateGStreamer::timeChanged()
1699 m_player->timeChanged();
1702 void MediaPlayerPrivateGStreamer::didEnd()
1704 // Synchronize position and duration values to not confuse the
1705 // HTMLMediaElement. In some cases like reverse playback the
1706 // position is not always reported as 0 for instance.
1707 float now = currentTime();
1708 if (now > 0 && now <= duration() && m_mediaDuration != now) {
1709 m_mediaDurationKnown = true;
1710 m_mediaDuration = now;
1711 m_player->durationChanged();
1714 m_isEndReached = true;
1717 if (!m_player->mediaPlayerClient()->mediaPlayerIsLooping()) {
1719 #if ENABLE(TIZEN_GSTREAMER_AUDIO)
1720 gst_element_set_state(m_playBin.get(), GST_STATE_PAUSED);
1721 if (m_audioSessionManager)
1722 m_audioSessionManager->setSoundState(ASM_STATE_PAUSE);
1724 gst_element_set_state(m_playBin.get(), GST_STATE_NULL);
1727 #if ENABLE(TIZEN_GSTREAMER_VIDEO)
1728 HTMLMediaElement* element = static_cast<HTMLMediaElement*>(player()->mediaPlayerClient());
1729 if (element->isVideo())
1730 power_unlock_state(POWER_STATE_NORMAL);
1736 void MediaPlayerPrivateGStreamer::cacheDuration()
1738 // Reset cached media duration
1739 m_mediaDuration = 0;
1741 // And re-cache it if possible.
1743 gst_element_get_state(m_playBin.get(), &state, 0, 0);
1744 float newDuration = duration();
1746 if (state <= GST_STATE_READY) {
1747 // Don't set m_mediaDurationKnown yet if the pipeline is not
1748 // paused. This allows duration() query to fail at least once
1749 // before playback starts and duration becomes known.
1750 if (!isinf(newDuration))
1751 m_mediaDuration = newDuration;
1753 m_mediaDurationKnown = !isinf(newDuration);
1754 if (m_mediaDurationKnown)
1755 m_mediaDuration = newDuration;
1758 if (!isinf(newDuration))
1759 m_mediaDuration = newDuration;
1762 void MediaPlayerPrivateGStreamer::durationChanged()
1764 float previousDuration = m_mediaDuration;
1767 // Avoid emiting durationchanged in the case where the previous
1768 // duration was 0 because that case is already handled by the
1769 // HTMLMediaElement.
1770 if (previousDuration && m_mediaDuration != previousDuration)
1771 m_player->durationChanged();
1773 if (m_preload == MediaPlayer::None && m_originalPreloadWasAutoAndWasOverridden) {
1775 if (totalBytes() && !isLiveStream()) {
1776 setPreload(MediaPlayer::Auto);
1777 gst_element_set_state(m_playBin.get(), GST_STATE_NULL);
1778 gst_element_set_state(m_playBin.get(), GST_STATE_PAUSED);
1779 #if ENABLE(TIZEN_GSTREAMER_AUDIO)
1780 if (m_audioSessionManager)
1781 m_audioSessionManager->setSoundState(ASM_STATE_PAUSE);
1787 bool MediaPlayerPrivateGStreamer::supportsMuting() const
1792 void MediaPlayerPrivateGStreamer::setMuted(bool muted)
1797 g_object_set(m_playBin.get(), "mute", muted, NULL);
1800 void MediaPlayerPrivateGStreamer::notifyPlayerOfMute()
1802 m_muteTimerHandler = 0;
1804 if (!m_player || !m_playBin)
1808 g_object_get(m_playBin.get(), "mute", &muted, NULL);
1809 m_player->muteChanged(static_cast<bool>(muted));
1812 void MediaPlayerPrivateGStreamer::muteChanged()
1814 if (m_muteTimerHandler)
1815 g_source_remove(m_muteTimerHandler);
1816 m_muteTimerHandler = g_timeout_add(0, reinterpret_cast<GSourceFunc>(mediaPlayerPrivateMuteChangeTimeoutCallback), this);
1819 void MediaPlayerPrivateGStreamer::loadingFailed(MediaPlayer::NetworkState error)
1821 #if ENABLE(TIZEN_DLOG_SUPPORT)
1822 TIZEN_LOGE("NetworkState : %d", error);
1825 m_errorOccured = true;
1826 if (m_networkState != error) {
1827 m_networkState = error;
1828 m_player->networkStateChanged();
1830 if (m_readyState != MediaPlayer::HaveNothing) {
1831 m_readyState = MediaPlayer::HaveNothing;
1832 m_player->readyStateChanged();
1836 void MediaPlayerPrivateGStreamer::setSize(const IntSize& size)
1841 void MediaPlayerPrivateGStreamer::setVisible(bool visible)
1845 void MediaPlayerPrivateGStreamer::triggerRepaint(GstBuffer* buffer)
1847 g_return_if_fail(GST_IS_BUFFER(buffer));
1848 gst_buffer_replace(&m_buffer, buffer);
1849 #if ENABLE(TIZEN_ACCELERATED_COMPOSITING) && USE(TIZEN_TEXTURE_MAPPER)
1850 #if !ENABLE(TIZEN_WEBKIT2_TILED_AC_SHARED_PLATFORM_SURFACE)
1851 if (m_videoSize.isEmpty())
1852 m_videoSize = naturalSize();
1853 m_videoLayer->paintVideoLayer(m_videoSize);
1857 m_player->repaint();
1860 void MediaPlayerPrivateGStreamer::paint(GraphicsContext* context, const IntRect& rect)
1862 if (context->paintingDisabled())
1865 if (!m_player->visible())
1871 GRefPtr<GstCaps> caps = webkitGstGetPadCaps(m_videoSinkPad.get());
1875 RefPtr<ImageGStreamer> gstImage = ImageGStreamer::createImage(m_buffer, caps.get());
1879 context->drawImage(reinterpret_cast<Image*>(gstImage->image().get()), ColorSpaceSRGB,
1880 rect, gstImage->rect(), CompositeCopy, DoNotRespectImageOrientation, false);
1883 static HashSet<String> mimeTypeCache()
1885 initializeGStreamerAndRegisterWebKitElements();
1887 DEFINE_STATIC_LOCAL(HashSet<String>, cache, ());
1888 static bool typeListInitialized = false;
1890 if (typeListInitialized)
1892 const char* mimeTypes[] = {
1894 "application/vnd.apple.mpegurl",
1895 "application/vnd.rn-realmedia",
1896 "application/x-3gp",
1897 #if ENABLE(TIZEN_GSTREAMER_VIDEO)
1898 "application/x-mpegurl",
1899 "application/x-mpegURL",
1901 "application/x-pn-realaudio",
1921 "audio/x-amr-nb-sh",
1922 "audio/x-amr-wb-sh",
1953 "audio/x-vorbis+ogg",
1957 "audio/x-wavpack-correction",
1984 for (unsigned i = 0; i < (sizeof(mimeTypes) / sizeof(*mimeTypes)); ++i)
1985 cache.add(String(mimeTypes[i]));
1987 typeListInitialized = true;
1991 void MediaPlayerPrivateGStreamer::getSupportedTypes(HashSet<String>& types)
1993 types = mimeTypeCache();
1996 MediaPlayer::SupportsType MediaPlayerPrivateGStreamer::supportsType(const String& type, const String& codecs, const KURL&)
1998 if (type.isNull() || type.isEmpty())
1999 return MediaPlayer::IsNotSupported;
2001 // spec says we should not return "probably" if the codecs string is empty
2002 if (mimeTypeCache().contains(type))
2003 return codecs.isEmpty() ? MediaPlayer::MayBeSupported : MediaPlayer::IsSupported;
2004 return MediaPlayer::IsNotSupported;
2007 bool MediaPlayerPrivateGStreamer::hasSingleSecurityOrigin() const
2012 bool MediaPlayerPrivateGStreamer::supportsFullscreen() const
2014 #if PLATFORM(MAC) && !PLATFORM(IOS) && __MAC_OS_X_VERSION_MIN_REQUIRED == 1050
2015 // See <rdar://problem/7389945>
2022 PlatformMedia MediaPlayerPrivateGStreamer::platformMedia() const
2025 #ifndef GST_API_VERSION_1
2026 p.type = PlatformMedia::GStreamerGWorldType;
2027 p.media.gstreamerGWorld = m_gstGWorld.get();
2032 MediaPlayer::MovieLoadType MediaPlayerPrivateGStreamer::movieLoadType() const
2034 if (m_readyState == MediaPlayer::HaveNothing)
2035 return MediaPlayer::Unknown;
2038 return MediaPlayer::LiveStream;
2040 return MediaPlayer::Download;
2043 void MediaPlayerPrivateGStreamer::setDownloadBuffering()
2050 g_object_get(m_playBin.get(), "flags", &flags, NULL);
2051 #if ENABLE(TIZEN_ACCELERATED_COMPOSITING) && USE(TIZEN_TEXTURE_MAPPER) && ENABLE(TIZEN_WEBKIT2_TILED_AC_SHARED_PLATFORM_SURFACE)
2052 flags = (GstPlayFlags)(flags | GST_PLAY_FLAG_NATIVE_VIDEO);
2054 if (m_preload == MediaPlayer::Auto) {
2055 LOG_MEDIA_MESSAGE("Enabling on-disk buffering");
2056 g_object_set(m_playBin.get(), "flags", flags | GST_PLAY_FLAG_DOWNLOAD, NULL);
2058 LOG_MEDIA_MESSAGE("Disabling on-disk buffering");
2059 g_object_set(m_playBin.get(), "flags", flags & ~GST_PLAY_FLAG_DOWNLOAD, NULL);
2063 void MediaPlayerPrivateGStreamer::setPreload(MediaPlayer::Preload preload)
2065 m_originalPreloadWasAutoAndWasOverridden = m_preload != preload && m_preload == MediaPlayer::Auto;
2067 m_preload = preload;
2069 setDownloadBuffering();
2071 if (m_delayingLoad && m_preload != MediaPlayer::None) {
2072 m_delayingLoad = false;
2077 void MediaPlayerPrivateGStreamer::createAudioSink()
2079 // Construct audio sink if pitch preserving is enabled.
2080 if (!m_preservesPitch)
2086 GstElement* scale = gst_element_factory_make("scaletempo", 0);
2088 GST_WARNING("Failed to create scaletempo");
2092 GstElement* convert = gst_element_factory_make("audioconvert", 0);
2093 GstElement* resample = gst_element_factory_make("audioresample", 0);
2094 GstElement* sink = gst_element_factory_make("autoaudiosink", 0);
2096 GstElement* audioSink = gst_bin_new("audio-sink");
2097 gst_bin_add_many(GST_BIN(audioSink), scale, convert, resample, sink, NULL);
2099 if (!gst_element_link_many(scale, convert, resample, sink, NULL)) {
2100 GST_WARNING("Failed to link audio sink elements");
2101 gst_object_unref(audioSink);
2105 GRefPtr<GstPad> pad = adoptGRef(gst_element_get_static_pad(scale, "sink"));
2106 gst_element_add_pad(audioSink, gst_ghost_pad_new("sink", pad.get()));
2108 g_object_set(m_playBin.get(), "audio-sink", audioSink, NULL);
2111 #if ENABLE(TIZEN_GSTREAMER_VIDEO)
2112 GstElement* MediaPlayerPrivateGStreamer::createVideoSink()
2114 #if ENABLE(TIZEN_ACCELERATED_COMPOSITING) && USE(TIZEN_TEXTURE_MAPPER) && ENABLE(TIZEN_WEBKIT2_TILED_AC_SHARED_PLATFORM_SURFACE)
2115 m_webkitVideoSink = m_videoLayer->createVideoSink();
2116 m_videoSinkPad = adoptGRef(gst_element_get_static_pad(m_webkitVideoSink, "sink"));
2118 // Primary video should be OFF and Secondary video should be FULL SCREEN mode
2119 g_object_set(m_webkitVideoSink, "display-mode", 2, NULL);
2120 #else // ENABLE(TIZEN_ACCELERATED_COMPOSITING) && USE(TIZEN_TEXTURE_MAPPER) && ENABLE(TIZEN_WEBKIT2_TILED_AC_SHARED_PLATFORM_SURFACE)
2121 #ifndef GST_API_VERSION_1
2122 m_webkitVideoSink = webkitVideoSinkNew(m_gstGWorld.get());
2124 m_webkitVideoSink = webkitVideoSinkNew();
2126 m_videoSinkPad = adoptGRef(gst_element_get_static_pad(m_webkitVideoSink, "sink"));
2127 g_signal_connect(m_webkitVideoSink, "repaint-requested", G_CALLBACK(mediaPlayerPrivateRepaintCallback), this);
2128 #endif // ENABLE(TIZEN_ACCELERATED_COMPOSITING) && USE(TIZEN_TEXTURE_MAPPER) && ENABLE(TIZEN_WEBKIT2_TILED_AC_SHARED_PLATFORM_SURFACE)
2130 #ifndef GST_API_VERSION_1
2131 m_videoSinkBin = gst_bin_new("video-sink");
2132 GstElement* videoTee = gst_element_factory_make("tee", "videoTee");
2133 #if !ENABLE(TIZEN_GSTREAMER_VIDEO)
2134 GstElement* queue = gst_element_factory_make("queue", 0);
2137 gst_object_ref_sink(m_videoSinkBin);
2139 // Build a new video sink consisting of a bin containing a tee
2140 // (meant to distribute data to multiple video sinks) and our
2141 // internal video sink. For fullscreen we create an autovideosink
2142 // and initially block the data flow towards it and configure it
2143 #if !ENABLE(TIZEN_GSTREAMER_VIDEO)
2144 gst_bin_add_many(GST_BIN(m_videoSinkBin), videoTee, queue, NULL);
2146 gst_bin_add_many(GST_BIN(m_videoSinkBin), videoTee, NULL);
2149 #if !ENABLE(TIZEN_GSTREAMER_VIDEO)
2150 // Link a new src pad from tee to queue1.
2151 gst_element_link_pads_full(videoTee, 0, queue, "sink", GST_PAD_LINK_CHECK_NOTHING);
2155 GstElement* actualVideoSink = 0;
2156 m_fpsSink = gst_element_factory_make("fpsdisplaysink", "sink");
2158 // The verbose property has been added in -bad 0.10.22. Making
2159 // this whole code depend on it because we don't want
2160 // fpsdiplaysink to spit data on stdout.
2161 GstElementFactory* factory = GST_ELEMENT_FACTORY(GST_ELEMENT_GET_CLASS(m_fpsSink)->elementfactory);
2162 if (gst_plugin_feature_check_version(GST_PLUGIN_FEATURE(factory), 0, 10, 22)) {
2163 g_object_set(m_fpsSink, "silent", TRUE , NULL);
2165 // Turn off text overlay unless logging is enabled.
2167 g_object_set(m_fpsSink, "text-overlay", FALSE , NULL);
2169 WTFLogChannel* channel = getChannelFromName("Media");
2170 if (channel->state != WTFLogChannelOn)
2171 g_object_set(m_fpsSink, "text-overlay", FALSE , NULL);
2172 #endif // LOG_DISABLED
2174 if (g_object_class_find_property(G_OBJECT_GET_CLASS(m_fpsSink), "video-sink")) {
2175 g_object_set(m_fpsSink, "video-sink", m_webkitVideoSink, NULL);
2176 #ifndef GST_API_VERSION_1
2177 gst_bin_add(GST_BIN(m_videoSinkBin), m_fpsSink);
2179 actualVideoSink = m_fpsSink;
2187 #ifndef GST_API_VERSION_1
2188 gst_bin_add(GST_BIN(m_videoSinkBin), m_webkitVideoSink);
2190 actualVideoSink = m_webkitVideoSink;
2193 ASSERT(actualVideoSink);
2195 #ifndef GST_API_VERSION_1
2196 #if ENABLE(TIZEN_GSTREAMER_VIDEO)
2197 // Link a new src pad from tee to queue1.
2198 GRefPtr<GstPad> srcPad = adoptGRef(gst_element_get_request_pad(videoTee, "src%d"));
2199 GRefPtr<GstPad> sinkPad = adoptGRef(gst_element_get_static_pad(actualVideoSink, "sink"));
2200 gst_pad_link(srcPad.get(), sinkPad.get());
2202 // Faster elements linking.
2203 gst_element_link_pads_full(queue, "src", actualVideoSink, "sink", GST_PAD_LINK_CHECK_NOTHING);
2205 // Add a ghostpad to the bin so it can proxy to tee.
2206 GRefPtr<GstPad> pad = adoptGRef(gst_element_get_static_pad(videoTee, "sink"));
2207 gst_element_add_pad(m_videoSinkBin, gst_ghost_pad_new("sink", pad.get()));
2209 // Set the bin as video sink of playbin.
2210 return m_videoSinkBin;
2212 return actualVideoSink;
2217 void MediaPlayerPrivateGStreamer::createGSTPlayBin()
2221 // gst_element_factory_make() returns a floating reference so
2222 // we should not adopt.
2223 m_playBin = gst_element_factory_make(gPlaybinName, "play");
2225 #ifndef GST_API_VERSION_1
2226 m_gstGWorld = GStreamerGWorld::createGWorld(m_playBin.get());
2229 GRefPtr<GstBus> bus = webkitGstPipelineGetBus(GST_PIPELINE(m_playBin.get()));
2230 gst_bus_add_signal_watch(bus.get());
2231 g_signal_connect(bus.get(), "message", G_CALLBACK(mediaPlayerPrivateMessageCallback), this);
2232 #if ENABLE(TIZEN_ACCELERATED_COMPOSITING) && USE(TIZEN_TEXTURE_MAPPER) && ENABLE(TIZEN_WEBKIT2_TILED_AC_SHARED_PLATFORM_SURFACE)
2233 gst_bus_set_sync_handler(bus.get(), GstBusSyncHandler(mediaPlayerPrivateSyncHandler), this);
2235 gst_object_unref(bus.get());
2237 g_object_set(m_playBin.get(), "mute", m_player->muted(), NULL);
2239 g_signal_connect(m_playBin.get(), "notify::volume", G_CALLBACK(mediaPlayerPrivateVolumeChangedCallback), this);
2240 g_signal_connect(m_playBin.get(), "notify::source", G_CALLBACK(mediaPlayerPrivateSourceChangedCallback), this);
2241 g_signal_connect(m_playBin.get(), "notify::mute", G_CALLBACK(mediaPlayerPrivateMuteChangedCallback), this);
2242 g_signal_connect(m_playBin.get(), "video-changed", G_CALLBACK(mediaPlayerPrivateVideoChangedCallback), this);
2243 g_signal_connect(m_playBin.get(), "audio-changed", G_CALLBACK(mediaPlayerPrivateAudioChangedCallback), this);
2244 #if ENABLE(TIZEN_GSTREAMER_VIDEO)
2245 g_signal_connect(m_playBin.get(), "source-setup", G_CALLBACK(mediaPlayerPrivateSourceSetupCallback), this);
2248 #if ENABLE(TIZEN_GSTREAMER_VIDEO)
2249 HTMLMediaElement* element = static_cast<HTMLMediaElement*>(player()->mediaPlayerClient());
2250 if (element->isVideo()) {
2251 GstElement* videoElement = createVideoSink();
2252 g_object_set(m_playBin.get(), "video-sink", videoElement, NULL);
2254 GRefPtr<GstPad> videoSinkPad = adoptGRef(gst_element_get_static_pad(m_webkitVideoSink, "sink"));
2256 g_signal_connect(videoSinkPad.get(), "notify::caps", G_CALLBACK(mediaPlayerPrivateVideoSinkCapsChangedCallback), this);
2258 GstElement* fakeSink = gst_element_factory_make("fakesink", 0);
2259 g_object_set(fakeSink, "sync", true, NULL);
2260 g_object_set(m_playBin.get(), "video-sink", fakeSink, NULL);
2264 #if ENABLE(TIZEN_GSTREAMER_AUDIO)
2265 if (m_audioSessionManager)
2266 m_audioSessionManager->registerAudioSessionManager(MM_SESSION_TYPE_SHARE, mediaPlayerPrivateAudioSessionNotifyCallback, player());
2272 #if ENABLE(TIZEN_ACCELERATED_COMPOSITING) && USE(TIZEN_TEXTURE_MAPPER)
2273 bool MediaPlayerPrivateGStreamer::supportsAcceleratedRendering() const
2275 bool isEmbedVideo = false;
2276 if (m_player->mediaPlayerClient()) {
2277 Document* document = m_player->mediaPlayerClient()->mediaPlayerOwningDocument();
2278 if (document && document->settings()) {
2279 isEmbedVideo = document->settings()->acceleratedCompositingForVideoEnabled()
2280 && document->settings()->acceleratedCompositingEnabled();
2281 LOG_MEDIA_MESSAGE("isEmbedVideo: %d", isEmbedVideo);
2285 return isEmbedVideo;
2288 PlatformLayer* MediaPlayerPrivateGStreamer::platformLayer() const
2290 return m_videoLayer->platformLayer();
2293 #if ENABLE(TIZEN_WEBKIT2_TILED_AC_SHARED_PLATFORM_SURFACE)
2294 void MediaPlayerPrivateGStreamer::paintCurrentFrameInContext(GraphicsContext* context, const IntRect& rect)
2296 m_videoLayer->paintCurrentFrameInContext(context, rect);
2299 void MediaPlayerPrivateGStreamer::xWindowIdPrepared(GstMessage* message)
2303 gst_structure_get_int(message->structure, "video-width", &width);
2304 gst_structure_get_int(message->structure, "video-height", &height);
2306 #if ENABLE(TIZEN_DLOG_SUPPORT)
2307 TIZEN_LOGI("Width: %d, Height: %d", width, height);
2310 IntSize size(width, height);
2312 m_videoLayer->setOverlay(size);
2314 #endif // ENABLE(TIZEN_WEBKIT2_TILED_AC_SHARED_PLATFORM_SURFACE)
2315 #endif // ENABLE(TIZEN_ACCELERATED_COMPOSITING) && USE(TIZEN_TEXTURE_MAPPER)
2317 #if ENABLE(TIZEN_WEBKIT2_PROXY)
2318 void MediaPlayerPrivateGStreamer::setProxy(GstElement* source)
2320 SoupSession* session = WebCore::ResourceHandle::defaultSession();
2324 SoupURI* proxyUri = 0;
2325 g_object_get(session, SOUP_SESSION_PROXY_URI, &proxyUri, NULL);
2329 char* proxy = soup_uri_to_string(proxyUri, false);
2330 g_object_set(source, "proxy", proxy, NULL);
2332 soup_uri_free(proxyUri);
2337 #if ENABLE(TIZEN_GSTREAMER_VIDEO)
2338 void MediaPlayerPrivateGStreamer::suspend()
2340 m_suspendTime = currentTime();
2341 gst_element_set_state(m_playBin.get(), GST_STATE_NULL);
2342 #if ENABLE(TIZEN_GSTREAMER_AUDIO)
2343 if (m_audioSessionManager)
2344 m_audioSessionManager->setSoundState(ASM_STATE_STOP);
2348 void MediaPlayerPrivateGStreamer::resume()
2350 gst_element_set_state(m_playBin.get(), GST_STATE_PAUSED);
2351 #if ENABLE(TIZEN_GSTREAMER_AUDIO)
2352 if (m_audioSessionManager)
2353 m_audioSessionManager->setSoundState(ASM_STATE_PAUSE);
2361 #endif // USE(GSTREAMER)