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 #if ENABLE(TIZEN_GSTREAMER_VIDEO) && (!ENABLE(TIZEN_ACCELERATED_COMPOSITING) || !USE(TIZEN_TEXTURE_MAPPER) || !ENABLE(TIZEN_WEBKIT2_TILED_AC_SHARED_PLATFORM_SURFACE) || CPU(X86) || CPU(X86_64))
219 static void mediaPlayerPrivateRepaintCallback(WebKitVideoSink*, GstBuffer *buffer, MediaPlayerPrivateGStreamer* playerPrivate)
221 playerPrivate->triggerRepaint(buffer);
225 #if ENABLE(TIZEN_ACCELERATED_COMPOSITING) && USE(TIZEN_TEXTURE_MAPPER) && ENABLE(TIZEN_WEBKIT2_TILED_AC_SHARED_PLATFORM_SURFACE)
226 static GstBusSyncReply mediaPlayerPrivateSyncHandler(GstBus* bus, GstMessage* message, MediaPlayerPrivateGStreamer* playerPrivate)
228 // ignore anything but 'prepare-xid' message
229 if (GST_MESSAGE_TYPE(message) != GST_MESSAGE_ELEMENT)
231 if (!gst_structure_has_name (message->structure, "prepare-xid"))
234 playerPrivate->xWindowIdPrepared(message);
239 #if ENABLE(TIZEN_GSTREAMER_AUDIO)
240 static ASM_cb_result_t MediaPlayerAudioSessionEventSourcePause(ASM_event_sources_t eventSource, void* callbackData)
242 MediaPlayer* player = static_cast<MediaPlayer*>(callbackData);
243 HTMLMediaElement* element = static_cast<HTMLMediaElement*>(player->mediaPlayerClient());
246 return ASM_CB_RES_IGNORE;
248 switch (eventSource) {
249 case ASM_EVENT_SOURCE_CALL_START:
250 case ASM_EVENT_SOURCE_ALARM_START:
251 case ASM_EVENT_SOURCE_MEDIA:
252 case ASM_EVENT_SOURCE_EMERGENCY_START:
253 case ASM_EVENT_SOURCE_OTHER_PLAYER_APP:
254 case ASM_EVENT_SOURCE_RESOURCE_CONFLICT:
255 case ASM_EVENT_SOURCE_EARJACK_UNPLUG:
256 if (!player->url().string().contains("camera://")) {
258 return ASM_CB_RES_PAUSE;
261 return ASM_CB_RES_NONE;
265 static ASM_cb_result_t MediaPlayerAudioSessionEventSourcePlay(ASM_event_sources_t eventSource, void* callbackData)
267 MediaPlayer* player = static_cast<MediaPlayer*>(callbackData);
268 HTMLMediaElement* element = static_cast<HTMLMediaElement*>(player->mediaPlayerClient());
271 return ASM_CB_RES_IGNORE;
273 switch (eventSource) {
274 case ASM_EVENT_SOURCE_ALARM_END:
275 if (!element->isVideo() && !player->url().string().contains("camera://")) {
277 return ASM_CB_RES_PLAYING;
279 return ASM_CB_RES_NONE;
281 return ASM_CB_RES_NONE;
285 static ASM_cb_result_t mediaPlayerPrivateAudioSessionNotifyCallback(int, ASM_event_sources_t eventSource, ASM_sound_commands_t command, unsigned int, void* callbackData)
287 if (command == ASM_COMMAND_STOP || command == ASM_COMMAND_PAUSE)
288 return MediaPlayerAudioSessionEventSourcePause(eventSource, callbackData);
289 if (command == ASM_COMMAND_PLAY || command == ASM_COMMAND_RESUME)
290 return MediaPlayerAudioSessionEventSourcePlay(eventSource, callbackData);
292 return ASM_CB_RES_NONE;
296 PassOwnPtr<MediaPlayerPrivateInterface> MediaPlayerPrivateGStreamer::create(MediaPlayer* player)
298 return adoptPtr(new MediaPlayerPrivateGStreamer(player));
301 void MediaPlayerPrivateGStreamer::registerMediaEngine(MediaEngineRegistrar registrar)
304 registrar(create, getSupportedTypes, supportsType, 0, 0, 0);
307 bool initializeGStreamerAndRegisterWebKitElements()
309 if (!initializeGStreamer())
312 GRefPtr<GstElementFactory> srcFactory = gst_element_factory_find("webkitwebsrc");
314 #if ENABLE(TIZEN_MEDIA_STREAM)
315 return gst_element_register(0, "webkitcamerasrc", GST_RANK_PRIMARY + 200, WEBKIT_TYPE_CAMERA_SRC);
317 GST_DEBUG_CATEGORY_INIT(webkit_media_player_debug, "webkitmediaplayer", 0, "WebKit media player");
318 return gst_element_register(0, "webkitwebsrc", GST_RANK_PRIMARY + 100, WEBKIT_TYPE_WEB_SRC);
319 #endif // ENABLE(TIZEN_MEDIA_STREAM)
324 bool MediaPlayerPrivateGStreamer::isAvailable()
326 if (!initializeGStreamerAndRegisterWebKitElements())
329 GRefPtr<GstElementFactory> factory = gst_element_factory_find(gPlaybinName);
333 MediaPlayerPrivateGStreamer::MediaPlayerPrivateGStreamer(MediaPlayer* player)
335 , m_webkitVideoSink(0)
336 #if ENABLE(TIZEN_GSTREAMER_VIDEO)
342 , m_changingRate(false)
343 , m_endTime(numeric_limits<float>::infinity())
344 , m_isEndReached(false)
345 , m_networkState(MediaPlayer::Empty)
346 , m_readyState(MediaPlayer::HaveNothing)
347 , m_isStreaming(false)
350 , m_mediaLocations(0)
351 , m_mediaLocationCurrentIndex(0)
352 , m_resetPipeline(false)
357 , m_errorOccured(false)
359 , m_startedBuffering(false)
360 , m_fillTimer(this, &MediaPlayerPrivateGStreamer::fillTimerFired)
362 , m_bufferingPercentage(0)
363 , m_preload(MediaPlayer::Auto)
364 , m_delayingLoad(false)
365 , m_mediaDurationKnown(true)
366 , m_maxTimeLoadedAtLastDidLoadingProgress(0)
367 , m_volumeTimerHandler(0)
368 , m_muteTimerHandler(0)
371 , m_audioTimerHandler(0)
372 , m_videoTimerHandler(0)
373 , m_webkitAudioSink(0)
375 #if ENABLE(TIZEN_ACCELERATED_COMPOSITING) && USE(TIZEN_TEXTURE_MAPPER)
376 , m_videoLayer(VideoLayerTizen::create(static_cast<HTMLMediaElement*>(player->mediaPlayerClient())))
377 #endif // ENABLE(TIZEN_ACCELERATED_COMPOSITING) && USE(TIZEN_TEXTURE_MAPPER)
378 #if ENABLE(TIZEN_GSTREAMER_AUDIO)
379 , m_audioSessionManager(AudioSessionManagerGStreamerTizen::createAudioSessionManager())
381 , m_originalPreloadWasAutoAndWasOverridden(false)
382 , m_preservesPitch(false)
383 #if ENABLE(TIZEN_GSTREAMER_VIDEO)
389 MediaPlayerPrivateGStreamer::~MediaPlayerPrivateGStreamer()
391 if (m_fillTimer.isActive())
395 gst_buffer_unref(m_buffer);
398 if (m_mediaLocations) {
399 gst_structure_free(m_mediaLocations);
400 m_mediaLocations = 0;
403 #ifndef GST_API_VERSION_1
404 if (m_videoSinkBin) {
405 gst_object_unref(m_videoSinkBin);
411 GRefPtr<GstBus> bus = webkitGstPipelineGetBus(GST_PIPELINE(m_playBin.get()));
413 g_signal_handlers_disconnect_by_func(bus.get(), reinterpret_cast<gpointer>(mediaPlayerPrivateMessageCallback), this);
414 gst_bus_remove_signal_watch(bus.get());
416 gst_element_set_state(m_playBin.get(), GST_STATE_NULL);
422 #if ENABLE(TIZEN_GSTREAMER_AUDIO)
423 if (m_audioSessionManager)
424 m_audioSessionManager->setSoundState(ASM_STATE_STOP);
427 if (m_muteTimerHandler)
428 g_source_remove(m_muteTimerHandler);
430 if (m_volumeTimerHandler)
431 g_source_remove(m_volumeTimerHandler);
433 if (m_videoTimerHandler)
434 g_source_remove(m_videoTimerHandler);
436 if (m_audioTimerHandler)
437 g_source_remove(m_audioTimerHandler);
440 void MediaPlayerPrivateGStreamer::load(const String& url)
442 if (!initializeGStreamerAndRegisterWebKitElements())
445 #if ENABLE(TIZEN_MEDIA_STREAM) || ENABLE(TIZEN_FILE_SYSTEM)
447 #endif // ENABLE(TIZEN_MEDIA_STREAM) || ENABLE(TIZEN_FILE_SYSTEM)
448 KURL kurl(KURL(), url);
449 String cleanUrl(url);
451 // Clean out everything after file:// url path.
452 if (kurl.isLocalFile())
453 cleanUrl = cleanUrl.substring(0, kurl.pathEnd());
457 setDownloadBuffering();
462 m_url = KURL(KURL(), cleanUrl);
463 g_object_set(m_playBin.get(), "uri", cleanUrl.utf8().data(), NULL);
465 LOG_MEDIA_MESSAGE("Load %s", cleanUrl.utf8().data());
467 #if ENABLE(TIZEN_MEDIA_STREAM)
468 // Workaround modification for getUserMedia.
469 // When 'playing' event is fired videoWidth and videoHeight are not available.
470 // Set m_videoSize as frame size which comes from webkitCameraSource.
471 // This size is fixed.
472 if (isLocalMediaStream())
473 m_videoSize = IntSize(480, 640);
476 if (m_preload == MediaPlayer::None) {
477 LOG_MEDIA_MESSAGE("Delaying load.");
478 m_delayingLoad = true;
481 // Reset network and ready states. Those will be set properly once
482 // the pipeline pre-rolled.
483 #if ENABLE(TIZEN_GSTREAMER_VIDEO)
485 m_networkState = MediaPlayer::Loading;
487 m_networkState = MediaPlayer::Idle;
489 m_networkState = MediaPlayer::Loading;
491 m_player->networkStateChanged();
492 m_readyState = MediaPlayer::HaveNothing;
493 m_player->readyStateChanged();
494 m_volumeAndMuteInitialized = false;
496 // GStreamer needs to have the pipeline set to a paused state to
497 // start providing anything useful.
498 #if ENABLE(TIZEN_GSTREAMER_VIDEO)
501 gst_element_set_state(m_playBin.get(), GST_STATE_PAUSED);
503 #if ENABLE(TIZEN_GSTREAMER_AUDIO)
504 if (m_audioSessionManager && !m_delayingLoad)
505 m_audioSessionManager->setSoundState(ASM_STATE_PAUSE);
512 void MediaPlayerPrivateGStreamer::commitLoad()
514 ASSERT(!m_delayingLoad);
515 LOG_MEDIA_MESSAGE("Committing load.");
519 float MediaPlayerPrivateGStreamer::playbackPosition() const
521 if (m_isEndReached) {
522 // Position queries on a null pipeline return 0. If we're at
523 // the end of the stream the pipeline is null but we want to
524 // report either the seek time or the duration because this is
525 // what the Media element spec expects us to do.
529 return m_mediaDuration;
534 GstQuery* query = gst_query_new_position(GST_FORMAT_TIME);
535 if (!gst_element_query(m_playBin.get(), query)) {
536 LOG_MEDIA_MESSAGE("Position query failed...");
537 gst_query_unref(query);
542 gst_query_parse_position(query, 0, &position);
544 // Position is available only if the pipeline is not in GST_STATE_NULL or
545 // GST_STATE_READY state.
546 if (position != static_cast<gint64>(GST_CLOCK_TIME_NONE))
547 ret = static_cast<double>(position) / GST_SECOND;
549 LOG_MEDIA_MESSAGE("Position %" GST_TIME_FORMAT, GST_TIME_ARGS(position));
551 gst_query_unref(query);
556 bool MediaPlayerPrivateGStreamer::changePipelineState(GstState newState)
558 ASSERT(newState == GST_STATE_PLAYING || newState == GST_STATE_PAUSED);
560 GstState currentState;
563 gst_element_get_state(m_playBin.get(), ¤tState, &pending, 0);
564 LOG_MEDIA_MESSAGE("Current state: %s, pending: %s", gst_element_state_get_name(currentState), gst_element_state_get_name(pending));
565 #if ENABLE(TIZEN_DLOG_SUPPORT)
566 TIZEN_LOGI("Current state: %s, New state: %s", gst_element_state_get_name(currentState), gst_element_state_get_name(newState));
568 if (currentState == newState || pending == newState)
571 GstStateChangeReturn setStateResult = gst_element_set_state(m_playBin.get(), newState);
572 GstState pausedOrPlaying = newState == GST_STATE_PLAYING ? GST_STATE_PAUSED : GST_STATE_PLAYING;
573 if (currentState != pausedOrPlaying && setStateResult == GST_STATE_CHANGE_FAILURE) {
574 loadingFailed(MediaPlayer::Empty);
580 void MediaPlayerPrivateGStreamer::prepareToPlay()
582 m_isEndReached = false;
585 if (m_delayingLoad) {
586 m_delayingLoad = false;
591 void MediaPlayerPrivateGStreamer::play()
593 #if ENABLE(TIZEN_GSTREAMER_AUDIO)
594 if (!m_audioSessionManager->setSoundState(ASM_STATE_PLAYING) && !isLocalMediaStream())
597 if (changePipelineState(GST_STATE_PLAYING)) {
598 m_isEndReached = false;
599 LOG_MEDIA_MESSAGE("Play");
603 void MediaPlayerPrivateGStreamer::pause()
605 #if ENABLE(TIZEN_GSTREAMER_AUDIO)
606 if (!m_audioSessionManager->setSoundState(ASM_STATE_PAUSE) && !isLocalMediaStream())
612 if (changePipelineState(GST_STATE_PAUSED))
613 LOG_MEDIA_MESSAGE("Pause");
616 float MediaPlayerPrivateGStreamer::duration() const
624 // Media duration query failed already, don't attempt new useless queries.
625 if (!m_mediaDurationKnown)
626 return numeric_limits<float>::infinity();
629 return m_mediaDuration;
631 GstFormat timeFormat = GST_FORMAT_TIME;
632 gint64 timeLength = 0;
634 #ifdef GST_API_VERSION_1
635 bool failure = !gst_element_query_duration(m_playBin.get(), timeFormat, &timeLength) || static_cast<guint64>(timeLength) == GST_CLOCK_TIME_NONE;
637 bool failure = !gst_element_query_duration(m_playBin.get(), &timeFormat, &timeLength) || timeFormat != GST_FORMAT_TIME || static_cast<guint64>(timeLength) == GST_CLOCK_TIME_NONE;
640 LOG_MEDIA_MESSAGE("Time duration query failed for %s", m_url.string().utf8().data());
641 return numeric_limits<float>::infinity();
644 LOG_MEDIA_MESSAGE("Duration: %" GST_TIME_FORMAT, GST_TIME_ARGS(timeLength));
646 return static_cast<double>(timeLength) / GST_SECOND;
647 // FIXME: handle 3.14.9.5 properly
650 float MediaPlayerPrivateGStreamer::currentTime() const
662 // https://bugzilla.gnome.org/show_bug.cgi?id=639941 In GStreamer
663 // 0.10.35 basesink reports wrong duration in case of EOS and
664 // negative playback rate. There's no upstream accepted patch for
665 // this bug yet, hence this temporary workaround.
666 if (m_isEndReached && m_playbackRate < 0)
669 return playbackPosition();
673 void MediaPlayerPrivateGStreamer::seek(float time)
681 LOG_MEDIA_MESSAGE("Seek attempt to %f secs", time);
682 #if ENABLE(TIZEN_DLOG_SUPPORT)
683 TIZEN_LOGI("Seek attempt to %f secs", time);
686 // Avoid useless seeking.
687 if (time == currentTime())
690 // Extract the integer part of the time (seconds) and the
691 // fractional part (microseconds). Attempt to round the
692 // microseconds so no floating point precision is lost and we can
693 // perform an accurate seek.
695 float microSeconds = modf(time, &seconds) * 1000000;
697 timeValue.tv_sec = static_cast<glong>(seconds);
698 timeValue.tv_usec = static_cast<glong>(roundf(microSeconds / 10000) * 10000);
700 GstClockTime clockTime = GST_TIMEVAL_TO_TIME(timeValue);
701 LOG_MEDIA_MESSAGE("Seek: %" GST_TIME_FORMAT, GST_TIME_ARGS(clockTime));
703 if (!gst_element_seek(m_playBin.get(), m_player->rate(),
705 (GstSeekFlags)(GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_ACCURATE),
706 GST_SEEK_TYPE_SET, clockTime,
707 GST_SEEK_TYPE_NONE, GST_CLOCK_TIME_NONE)) {
708 LOG_MEDIA_MESSAGE("Seek to %f failed", time);
709 #if ENABLE(TIZEN_DLOG_SUPPORT)
710 TIZEN_LOGE("Seek to %f failed", time);
715 #if ENABLE(TIZEN_GSTREAMER_VIDEO)
716 if (m_seekTime != m_mediaDuration)
717 m_isEndReached = false;
722 bool MediaPlayerPrivateGStreamer::paused() const
724 if (m_isEndReached) {
725 LOG_MEDIA_MESSAGE("Ignoring pause at EOS");
730 gst_element_get_state(m_playBin.get(), &state, 0, 0);
731 return state == GST_STATE_PAUSED;
734 bool MediaPlayerPrivateGStreamer::seeking() const
739 // Returns the size of the video
740 IntSize MediaPlayerPrivateGStreamer::naturalSize() const
745 if (!m_videoSize.isEmpty())
748 GRefPtr<GstCaps> caps = webkitGstGetPadCaps(m_videoSinkPad.get());
753 // TODO: handle possible clean aperture data. See
754 // https://bugzilla.gnome.org/show_bug.cgi?id=596571
755 // TODO: handle possible transformation matrix. See
756 // https://bugzilla.gnome.org/show_bug.cgi?id=596326
758 // Get the video PAR and original size, if this fails the
759 // video-sink has likely not yet negotiated its caps.
760 int pixelAspectRatioNumerator, pixelAspectRatioDenominator, stride;
761 IntSize originalSize;
762 GstVideoFormat format;
763 if (!getVideoSizeAndFormatFromCaps(caps.get(), originalSize, format, pixelAspectRatioNumerator, pixelAspectRatioDenominator, stride))
766 LOG_MEDIA_MESSAGE("Original video size: %dx%d", originalSize.width(), originalSize.height());
767 LOG_MEDIA_MESSAGE("Pixel aspect ratio: %d/%d", pixelAspectRatioNumerator, pixelAspectRatioDenominator);
769 // Calculate DAR based on PAR and video size.
770 int displayWidth = originalSize.width() * pixelAspectRatioNumerator;
771 int displayHeight = originalSize.height() * pixelAspectRatioDenominator;
773 // Divide display width and height by their GCD to avoid possible overflows.
774 int displayAspectRatioGCD = greatestCommonDivisor(displayWidth, displayHeight);
775 displayWidth /= displayAspectRatioGCD;
776 displayHeight /= displayAspectRatioGCD;
778 // Apply DAR to original video size. This is the same behavior as in xvimagesink's setcaps function.
779 guint64 width = 0, height = 0;
780 if (!(originalSize.height() % displayHeight)) {
781 LOG_MEDIA_MESSAGE("Keeping video original height");
782 width = gst_util_uint64_scale_int(originalSize.height(), displayWidth, displayHeight);
783 height = static_cast<guint64>(originalSize.height());
784 } else if (!(originalSize.width() % displayWidth)) {
785 LOG_MEDIA_MESSAGE("Keeping video original width");
786 height = gst_util_uint64_scale_int(originalSize.width(), displayHeight, displayWidth);
787 width = static_cast<guint64>(originalSize.width());
789 LOG_MEDIA_MESSAGE("Approximating while keeping original video height");
790 width = gst_util_uint64_scale_int(originalSize.height(), displayWidth, displayHeight);
791 height = static_cast<guint64>(originalSize.height());
794 LOG_MEDIA_MESSAGE("Natural size: %" G_GUINT64_FORMAT "x%" G_GUINT64_FORMAT, width, height);
795 #if ENABLE(TIZEN_ACCELERATED_COMPOSITING) && USE(TIZEN_TEXTURE_MAPPER) && ENABLE(TIZEN_WEBKIT2_TILED_AC_SHARED_PLATFORM_SURFACE) && USE(ACCELERATED_VIDEO_VAAPI)
796 if (!m_displaySize.isEmpty())
797 m_videoSize = scaleHDVideoToDisplaySize(static_cast<int>(width), static_cast<int>(height), m_displaySize.width(), m_displaySize.height());
800 m_videoSize = IntSize(static_cast<int>(width), static_cast<int>(height));
804 void MediaPlayerPrivateGStreamer::videoChanged()
806 if (m_videoTimerHandler)
807 g_source_remove(m_videoTimerHandler);
808 m_videoTimerHandler = g_timeout_add(0, reinterpret_cast<GSourceFunc>(mediaPlayerPrivateVideoChangeTimeoutCallback), this);
811 void MediaPlayerPrivateGStreamer::notifyPlayerOfVideo()
813 m_videoTimerHandler = 0;
815 gint videoTracks = 0;
817 g_object_get(m_playBin.get(), "n-video", &videoTracks, NULL);
819 m_hasVideo = videoTracks > 0;
821 #if ENABLE(TIZEN_GSTREAMER_VIDEO)
823 if (gst_video_get_size(m_videoSinkPad.get(), &width, &height)) {
824 IntSize size(width, height);
825 if (m_videoSize != size) {
826 m_videoSize = IntSize();
827 #if ENABLE(TIZEN_ACCELERATED_COMPOSITING) && USE(TIZEN_TEXTURE_MAPPER) && ENABLE(TIZEN_WEBKIT2_TILED_AC_SHARED_PLATFORM_SURFACE)
828 m_videoLayer->setOverlay(naturalSize());
833 m_player->mediaPlayerClient()->mediaPlayerEngineUpdated(m_player);
834 #else // ENABLE(TIZEN_GSTREAMER_VIDEO)
835 m_videoSize = IntSize();
837 m_player->mediaPlayerClient()->mediaPlayerEngineUpdated(m_player);
841 void MediaPlayerPrivateGStreamer::audioChanged()
843 if (m_audioTimerHandler)
844 g_source_remove(m_audioTimerHandler);
845 m_audioTimerHandler = g_timeout_add(0, reinterpret_cast<GSourceFunc>(mediaPlayerPrivateAudioChangeTimeoutCallback), this);
848 void MediaPlayerPrivateGStreamer::notifyPlayerOfAudio()
850 m_audioTimerHandler = 0;
852 gint audioTracks = 0;
854 g_object_get(m_playBin.get(), "n-audio", &audioTracks, NULL);
855 m_hasAudio = audioTracks > 0;
856 m_player->mediaPlayerClient()->mediaPlayerEngineUpdated(m_player);
859 void MediaPlayerPrivateGStreamer::setVolume(float volume)
864 gst_stream_volume_set_volume(GST_STREAM_VOLUME(m_playBin.get()), GST_STREAM_VOLUME_FORMAT_CUBIC,
865 static_cast<double>(volume));
868 void MediaPlayerPrivateGStreamer::notifyPlayerOfVolumeChange()
870 m_volumeTimerHandler = 0;
872 if (!m_player || !m_playBin)
875 volume = gst_stream_volume_get_volume(GST_STREAM_VOLUME(m_playBin.get()), GST_STREAM_VOLUME_FORMAT_CUBIC);
876 // get_volume() can return values superior to 1.0 if the user
877 // applies software user gain via third party application (GNOME
878 // volume control for instance).
879 volume = CLAMP(volume, 0.0, 1.0);
880 m_player->volumeChanged(static_cast<float>(volume));
883 void MediaPlayerPrivateGStreamer::volumeChanged()
885 if (m_volumeTimerHandler)
886 g_source_remove(m_volumeTimerHandler);
887 m_volumeTimerHandler = g_timeout_add(0, reinterpret_cast<GSourceFunc>(mediaPlayerPrivateVolumeChangeTimeoutCallback), this);
890 void MediaPlayerPrivateGStreamer::setRate(float rate)
892 // Avoid useless playback rate update.
893 if (m_playbackRate == rate)
899 gst_element_get_state(m_playBin.get(), &state, &pending, 0);
900 if ((state != GST_STATE_PLAYING && state != GST_STATE_PAUSED)
901 || (pending == GST_STATE_PAUSED))
907 m_playbackRate = rate;
908 m_changingRate = true;
911 gst_element_set_state(m_playBin.get(), GST_STATE_PAUSED);
912 #if ENABLE(TIZEN_GSTREAMER_AUDIO)
913 if (m_audioSessionManager)
914 m_audioSessionManager->setSoundState(ASM_STATE_PAUSE);
919 float currentPosition = static_cast<float>(playbackPosition() * GST_SECOND);
920 GstSeekFlags flags = (GstSeekFlags)(GST_SEEK_FLAG_FLUSH);
924 LOG_MEDIA_MESSAGE("Set Rate to %f", rate);
926 // Mute the sound if the playback rate is too extreme and
927 // audio pitch is not adjusted.
928 mute = (!m_preservesPitch && (rate < 0.8 || rate > 2));
929 start = currentPosition;
930 end = GST_CLOCK_TIME_NONE;
935 // If we are at beginning of media, start from the end to
936 // avoid immediate EOS.
937 if (currentPosition <= 0)
938 end = static_cast<gint64>(duration() * GST_SECOND);
940 end = currentPosition;
943 LOG_MEDIA_MESSAGE("Need to mute audio: %d", (int) mute);
945 if (!gst_element_seek(m_playBin.get(), rate, GST_FORMAT_TIME, flags,
946 GST_SEEK_TYPE_SET, start,
947 GST_SEEK_TYPE_SET, end))
948 LOG_MEDIA_MESSAGE("Set rate to %f failed", rate);
950 g_object_set(m_playBin.get(), "mute", mute, NULL);
953 void MediaPlayerPrivateGStreamer::setPreservesPitch(bool preservesPitch)
955 m_preservesPitch = preservesPitch;
958 MediaPlayer::NetworkState MediaPlayerPrivateGStreamer::networkState() const
960 return m_networkState;
963 MediaPlayer::ReadyState MediaPlayerPrivateGStreamer::readyState() const
968 PassRefPtr<TimeRanges> MediaPlayerPrivateGStreamer::buffered() const
970 RefPtr<TimeRanges> timeRanges = TimeRanges::create();
971 if (m_errorOccured || isLiveStream())
972 return timeRanges.release();
974 #if GST_CHECK_VERSION(0, 10, 31)
975 float mediaDuration(duration());
976 if (!mediaDuration || isinf(mediaDuration))
977 return timeRanges.release();
979 GstQuery* query = gst_query_new_buffering(GST_FORMAT_PERCENT);
981 if (!gst_element_query(m_playBin.get(), query)) {
982 gst_query_unref(query);
983 return timeRanges.release();
986 gint64 rangeStart = 0, rangeStop = 0;
987 for (guint index = 0; index < gst_query_get_n_buffering_ranges(query); index++) {
988 if (gst_query_parse_nth_buffering_range(query, index, &rangeStart, &rangeStop))
989 timeRanges->add(static_cast<float>((rangeStart * mediaDuration) / 100),
990 static_cast<float>((rangeStop * mediaDuration) / 100));
993 // Fallback to the more general maxTimeLoaded() if no range has
995 if (!timeRanges->length())
996 if (float loaded = maxTimeLoaded())
997 timeRanges->add(0, loaded);
999 gst_query_unref(query);
1001 float loaded = maxTimeLoaded();
1002 if (!m_errorOccured && !isLiveStream() && loaded > 0)
1003 timeRanges->add(0, loaded);
1005 return timeRanges.release();
1008 gboolean MediaPlayerPrivateGStreamer::handleMessage(GstMessage* message)
1010 GOwnPtr<GError> err;
1011 GOwnPtr<gchar> debug;
1012 MediaPlayer::NetworkState error;
1013 bool issueError = true;
1014 bool attemptNextLocation = false;
1015 const GstStructure* structure = gst_message_get_structure(message);
1018 const gchar* messageTypeName = gst_structure_get_name(structure);
1020 // Redirect messages are sent from elements, like qtdemux, to
1021 // notify of the new location(s) of the media.
1022 if (!g_strcmp0(messageTypeName, "redirect")) {
1023 mediaLocationChanged(message);
1028 LOG_MEDIA_MESSAGE("Message received from element %s", GST_MESSAGE_SRC_NAME(message));
1029 switch (GST_MESSAGE_TYPE(message)) {
1030 case GST_MESSAGE_ERROR:
1031 if (m_resetPipeline)
1033 gst_message_parse_error(message, &err.outPtr(), &debug.outPtr());
1034 LOG_MEDIA_MESSAGE("Error %d: %s (url=%s)", err->code, err->message, m_url.string().utf8().data());
1036 #if ENABLE(TIZEN_DLOG_SUPPORT)
1037 TIZEN_LOGE("GST_MESSAGE_ERROR %d: %s (url=%s)", err->code, err->message, m_url.string().utf8().data());
1040 GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS(GST_BIN(m_playBin.get()), GST_DEBUG_GRAPH_SHOW_ALL, "webkit-video.error");
1042 error = MediaPlayer::Empty;
1043 if (err->code == GST_STREAM_ERROR_CODEC_NOT_FOUND
1044 || err->code == GST_STREAM_ERROR_WRONG_TYPE
1045 || err->code == GST_STREAM_ERROR_FAILED
1046 || err->code == GST_CORE_ERROR_MISSING_PLUGIN
1047 || err->code == GST_RESOURCE_ERROR_NOT_FOUND)
1048 error = MediaPlayer::FormatError;
1049 else if (err->domain == GST_STREAM_ERROR) {
1050 // Let the mediaPlayerClient handle the stream error, in
1051 // this case the HTMLMediaElement will emit a stalled
1053 #if !ENABLE(TIZEN_MEDIA_STREAM)
1054 if (err->code == GST_STREAM_ERROR_TYPE_NOT_FOUND) {
1055 LOG_MEDIA_MESSAGE("Decode error, let the Media element emit a stalled event.");
1059 error = MediaPlayer::DecodeError;
1060 attemptNextLocation = true;
1061 } else if (err->domain == GST_RESOURCE_ERROR)
1062 error = MediaPlayer::NetworkError;
1064 if (attemptNextLocation)
1065 issueError = !loadNextLocation();
1067 loadingFailed(error);
1069 case GST_MESSAGE_EOS:
1070 LOG_MEDIA_MESSAGE("End of Stream");
1073 case GST_MESSAGE_STATE_CHANGED:
1074 // Ignore state changes if load is delayed (preload=none). The
1075 // player state will be updated once commitLoad() is called.
1076 if (m_delayingLoad) {
1077 LOG_MEDIA_MESSAGE("Media load has been delayed. Ignoring state changes for now");
1081 // Ignore state changes from internal elements. They are
1082 // forwarded to playbin2 anyway.
1083 if (GST_MESSAGE_SRC(message) == reinterpret_cast<GstObject*>(m_playBin.get())) {
1086 // Construct a filename for the graphviz dot file output.
1087 GstState oldState, newState;
1088 gst_message_parse_state_changed(message, &oldState, &newState, 0);
1090 CString dotFileName = String::format("webkit-video.%s_%s",
1091 gst_element_state_get_name(oldState),
1092 gst_element_state_get_name(newState)).utf8();
1094 GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS(GST_BIN(m_playBin.get()), GST_DEBUG_GRAPH_SHOW_ALL, dotFileName.data());
1096 #if ENABLE(TIZEN_GSTREAMER_VIDEO)
1097 if (m_suspendTime > 0 && newState == GST_STATE_PAUSED) {
1098 if (!isLocalMediaStream())
1099 seek(m_suspendTime);
1101 m_player->mediaPlayerClient()->setSuspended(false);
1106 case GST_MESSAGE_BUFFERING:
1107 processBufferingStats(message);
1109 case GST_MESSAGE_DURATION:
1110 LOG_MEDIA_MESSAGE("Duration changed");
1114 LOG_MEDIA_MESSAGE("Unhandled GStreamer message type: %s",
1115 GST_MESSAGE_TYPE_NAME(message));
1121 void MediaPlayerPrivateGStreamer::processBufferingStats(GstMessage* message)
1123 // This is the immediate buffering that needs to happen so we have
1124 // enough to play right now.
1126 const GstStructure *structure = gst_message_get_structure(message);
1127 gst_structure_get_int(structure, "buffer-percent", &m_bufferingPercentage);
1129 LOG_MEDIA_MESSAGE("[Buffering] Buffering: %d%%.", m_bufferingPercentage);
1131 GstBufferingMode mode;
1132 gst_message_parse_buffering_stats(message, &mode, 0, 0, 0);
1133 if (mode != GST_BUFFERING_DOWNLOAD) {
1138 // This is on-disk buffering, that allows us to download much more
1139 // than needed for right now.
1140 if (!m_startedBuffering) {
1141 LOG_MEDIA_MESSAGE("[Buffering] Starting on-disk buffering.");
1143 m_startedBuffering = true;
1145 if (m_fillTimer.isActive())
1148 m_fillTimer.startRepeating(0.2);
1152 void MediaPlayerPrivateGStreamer::fillTimerFired(Timer<MediaPlayerPrivateGStreamer>*)
1154 GstQuery* query = gst_query_new_buffering(GST_FORMAT_PERCENT);
1156 if (!gst_element_query(m_playBin.get(), query)) {
1157 gst_query_unref(query);
1162 gdouble fillStatus = 100.0;
1164 gst_query_parse_buffering_range(query, 0, &start, &stop, 0);
1165 gst_query_unref(query);
1168 fillStatus = 100.0 * stop / GST_FORMAT_PERCENT_MAX;
1170 LOG_MEDIA_MESSAGE("[Buffering] Download buffer filled up to %f%%", fillStatus);
1172 if (!m_mediaDuration)
1175 // Update maxTimeLoaded only if the media duration is
1176 // available. Otherwise we can't compute it.
1177 if (m_mediaDuration) {
1178 if (fillStatus == 100.0)
1179 m_maxTimeLoaded = m_mediaDuration;
1181 m_maxTimeLoaded = static_cast<float>((fillStatus * m_mediaDuration) / 100.0);
1182 LOG_MEDIA_MESSAGE("[Buffering] Updated maxTimeLoaded: %f", m_maxTimeLoaded);
1185 if (fillStatus != 100.0) {
1190 // Media is now fully loaded. It will play even if network
1191 // connection is cut. Buffering is done, remove the fill source
1192 // from the main loop.
1194 m_startedBuffering = false;
1198 float MediaPlayerPrivateGStreamer::maxTimeSeekable() const
1203 LOG_MEDIA_MESSAGE("maxTimeSeekable");
1204 // infinite duration means live stream
1205 if (isinf(duration()))
1211 float MediaPlayerPrivateGStreamer::maxTimeLoaded() const
1216 float loaded = m_maxTimeLoaded;
1217 if (!loaded && !m_fillTimer.isActive())
1218 loaded = duration();
1219 LOG_MEDIA_MESSAGE("maxTimeLoaded: %f", loaded);
1223 bool MediaPlayerPrivateGStreamer::didLoadingProgress() const
1225 if (!m_playBin || !m_mediaDuration || !totalBytes())
1227 float currentMaxTimeLoaded = maxTimeLoaded();
1228 bool didLoadingProgress = currentMaxTimeLoaded != m_maxTimeLoadedAtLastDidLoadingProgress;
1229 m_maxTimeLoadedAtLastDidLoadingProgress = currentMaxTimeLoaded;
1230 LOG_MEDIA_MESSAGE("didLoadingProgress: %d", didLoadingProgress);
1231 return didLoadingProgress;
1234 unsigned MediaPlayerPrivateGStreamer::totalBytes() const
1239 if (m_totalBytes != -1)
1240 return m_totalBytes;
1245 GstFormat fmt = GST_FORMAT_BYTES;
1247 #ifdef GST_API_VERSION_1
1248 if (gst_element_query_duration(m_source.get(), fmt, &length)) {
1250 if (gst_element_query_duration(m_source.get(), &fmt, &length)) {
1252 LOG_MEDIA_MESSAGE("totalBytes %" G_GINT64_FORMAT, length);
1253 m_totalBytes = static_cast<unsigned>(length);
1254 m_isStreaming = !length;
1255 return m_totalBytes;
1258 // Fall back to querying the source pads manually.
1259 // See also https://bugzilla.gnome.org/show_bug.cgi?id=638749
1260 GstIterator* iter = gst_element_iterate_src_pads(m_source.get());
1263 #ifdef GST_API_VERSION_1
1264 GValue item = G_VALUE_INIT;
1265 switch (gst_iterator_next(iter, &item)) {
1266 case GST_ITERATOR_OK: {
1267 GstPad* pad = static_cast<GstPad*>(g_value_get_object(&item));
1268 gint64 padLength = 0;
1269 if (gst_pad_query_duration(pad, fmt, &padLength) && padLength > length)
1276 switch (gst_iterator_next(iter, &data)) {
1277 case GST_ITERATOR_OK: {
1278 GRefPtr<GstPad> pad = adoptGRef(GST_PAD_CAST(data));
1279 gint64 padLength = 0;
1280 if (gst_pad_query_duration(pad.get(), &fmt, &padLength) && padLength > length)
1285 case GST_ITERATOR_RESYNC:
1286 gst_iterator_resync(iter);
1288 case GST_ITERATOR_ERROR:
1290 case GST_ITERATOR_DONE:
1295 #ifdef GST_API_VERSION_1
1296 g_value_unset(&item);
1300 gst_iterator_free(iter);
1302 LOG_MEDIA_MESSAGE("totalBytes %" G_GINT64_FORMAT, length);
1303 m_totalBytes = static_cast<unsigned>(length);
1304 m_isStreaming = !length;
1305 return m_totalBytes;
1308 unsigned MediaPlayerPrivateGStreamer::decodedFrameCount() const
1310 guint64 decodedFrames = 0;
1312 g_object_get(m_fpsSink, "frames-rendered", &decodedFrames, NULL);
1313 return static_cast<unsigned>(decodedFrames);
1316 unsigned MediaPlayerPrivateGStreamer::droppedFrameCount() const
1318 guint64 framesDropped = 0;
1320 g_object_get(m_fpsSink, "frames-dropped", &framesDropped, NULL);
1321 return static_cast<unsigned>(framesDropped);
1324 unsigned MediaPlayerPrivateGStreamer::audioDecodedByteCount() const
1326 GstQuery* query = gst_query_new_position(GST_FORMAT_BYTES);
1327 gint64 position = 0;
1329 if (m_webkitAudioSink && gst_element_query(m_webkitAudioSink.get(), query))
1330 gst_query_parse_position(query, 0, &position);
1332 gst_query_unref(query);
1333 return static_cast<unsigned>(position);
1336 unsigned MediaPlayerPrivateGStreamer::videoDecodedByteCount() const
1338 GstQuery* query = gst_query_new_position(GST_FORMAT_BYTES);
1339 gint64 position = 0;
1341 if (gst_element_query(m_webkitVideoSink, query))
1342 gst_query_parse_position(query, 0, &position);
1344 gst_query_unref(query);
1345 return static_cast<unsigned>(position);
1348 void MediaPlayerPrivateGStreamer::updateAudioSink()
1353 GstElement* sinkPtr = 0;
1355 g_object_get(m_playBin.get(), "audio-sink", &sinkPtr, NULL);
1356 m_webkitAudioSink = adoptGRef(sinkPtr);
1361 void MediaPlayerPrivateGStreamer::sourceChanged()
1363 GstElement* srcPtr = 0;
1365 g_object_get(m_playBin.get(), "source", &srcPtr, NULL);
1366 m_source = adoptGRef(srcPtr);
1368 if (WEBKIT_IS_WEB_SRC(m_source.get()))
1369 webKitWebSrcSetMediaPlayer(WEBKIT_WEB_SRC(m_source.get()), m_player);
1372 void MediaPlayerPrivateGStreamer::cancelLoad()
1374 if (m_networkState < MediaPlayer::Loading || m_networkState == MediaPlayer::Loaded)
1378 gst_element_set_state(m_playBin.get(), GST_STATE_NULL);
1379 #if ENABLE(TIZEN_GSTREAMER_AUDIO)
1380 if (m_audioSessionManager)
1381 m_audioSessionManager->setSoundState(ASM_STATE_STOP);
1385 void MediaPlayerPrivateGStreamer::updateStates()
1393 MediaPlayer::NetworkState oldNetworkState = m_networkState;
1394 MediaPlayer::ReadyState oldReadyState = m_readyState;
1398 GstStateChangeReturn ret = gst_element_get_state(m_playBin.get(),
1399 &state, &pending, 250 * GST_NSECOND);
1401 bool shouldUpdateAfterSeek = false;
1403 case GST_STATE_CHANGE_SUCCESS:
1404 LOG_MEDIA_MESSAGE("State: %s, pending: %s",
1405 gst_element_state_get_name(state),
1406 gst_element_state_get_name(pending));
1408 m_resetPipeline = state <= GST_STATE_READY;
1410 // Try to figure out ready and network states.
1411 if (state == GST_STATE_READY) {
1412 m_readyState = MediaPlayer::HaveMetadata;
1413 m_networkState = MediaPlayer::Empty;
1414 // Cache the duration without emiting the durationchange
1415 // event because it's taken care of by the media element
1416 // in this precise case.
1417 if (!m_isEndReached)
1419 } else if ((state == GST_STATE_NULL) || (maxTimeLoaded() == duration())) {
1420 m_networkState = MediaPlayer::Loaded;
1421 m_readyState = MediaPlayer::HaveEnoughData;
1423 m_readyState = currentTime() < maxTimeLoaded() ? MediaPlayer::HaveFutureData : MediaPlayer::HaveCurrentData;
1424 m_networkState = MediaPlayer::Loading;
1427 if (m_buffering && state != GST_STATE_READY) {
1428 m_readyState = MediaPlayer::HaveCurrentData;
1429 m_networkState = MediaPlayer::Loading;
1432 // Now let's try to get the states in more detail using
1433 // information from GStreamer, while we sync states where
1435 if (state == GST_STATE_PAUSED) {
1436 if (!m_webkitAudioSink)
1439 if (!m_volumeAndMuteInitialized) {
1440 notifyPlayerOfVolumeChange();
1441 notifyPlayerOfMute();
1442 m_volumeAndMuteInitialized = true;
1445 if (m_buffering && m_bufferingPercentage == 100) {
1446 m_buffering = false;
1447 m_bufferingPercentage = 0;
1448 m_readyState = MediaPlayer::HaveEnoughData;
1450 LOG_MEDIA_MESSAGE("[Buffering] Complete.");
1453 LOG_MEDIA_MESSAGE("[Buffering] Restarting playback.");
1454 gst_element_set_state(m_playBin.get(), GST_STATE_PLAYING);
1455 #if ENABLE(TIZEN_GSTREAMER_AUDIO)
1456 if (m_audioSessionManager)
1457 m_audioSessionManager->setSoundState(ASM_STATE_PLAYING);
1460 } else if (!m_buffering && (currentTime() < duration())) {
1463 } else if (state == GST_STATE_PLAYING) {
1464 m_readyState = MediaPlayer::HaveEnoughData;
1467 if (m_buffering && !isLiveStream()) {
1468 m_readyState = MediaPlayer::HaveCurrentData;
1469 m_networkState = MediaPlayer::Loading;
1471 LOG_MEDIA_MESSAGE("[Buffering] Pausing stream for buffering.");
1473 gst_element_set_state(m_playBin.get(), GST_STATE_PAUSED);
1474 #if ENABLE(TIZEN_GSTREAMER_AUDIO)
1475 if (m_audioSessionManager)
1476 m_audioSessionManager->setSoundState(ASM_STATE_PAUSE);
1482 // Is on-disk buffering in progress?
1483 if (m_fillTimer.isActive())
1484 m_networkState = MediaPlayer::Loading;
1486 if (m_changingRate) {
1487 m_player->rateChanged();
1488 m_changingRate = false;
1492 shouldUpdateAfterSeek = true;
1497 case GST_STATE_CHANGE_ASYNC:
1498 LOG_MEDIA_MESSAGE("Async: State: %s, pending: %s",
1499 gst_element_state_get_name(state),
1500 gst_element_state_get_name(pending));
1501 // Change in progress
1503 // On-disk buffering was attempted but the media is live. This
1504 // can't work so disable on-disk buffering and reset the
1507 #if ENABLE(TIZEN_MEDIA_STREAM)
1508 if (state == GST_STATE_READY && isLiveStream() && m_preload == MediaPlayer::Auto && !isLocalMediaStream()) {
1510 if (state == GST_STATE_READY && isLiveStream() && m_preload == MediaPlayer::Auto) {
1512 setPreload(MediaPlayer::None);
1513 gst_element_set_state(m_playBin.get(), GST_STATE_NULL);
1514 gst_element_set_state(m_playBin.get(), GST_STATE_PAUSED);
1515 #if ENABLE(TIZEN_GSTREAMER_AUDIO)
1516 if (m_audioSessionManager)
1517 m_audioSessionManager->setSoundState(ASM_STATE_PAUSE);
1521 // A live stream was paused, reset the pipeline.
1522 #if ENABLE(TIZEN_MEDIA_STREAM)
1523 if (state == GST_STATE_PAUSED && pending == GST_STATE_PLAYING && isLiveStream() && !isLocalMediaStream()) {
1525 if (state == GST_STATE_PAUSED && pending == GST_STATE_PLAYING && isLiveStream()) {
1527 gst_element_set_state(m_playBin.get(), GST_STATE_NULL);
1528 gst_element_set_state(m_playBin.get(), GST_STATE_PLAYING);
1529 #if ENABLE(TIZEN_GSTREAMER_AUDIO)
1530 if (m_audioSessionManager)
1531 m_audioSessionManager->setSoundState(ASM_STATE_PLAYING);
1535 if (!isLiveStream() && !m_buffering)
1539 shouldUpdateAfterSeek = true;
1543 case GST_STATE_CHANGE_FAILURE:
1544 LOG_MEDIA_MESSAGE("Failure: State: %s, pending: %s",
1545 gst_element_state_get_name(state),
1546 gst_element_state_get_name(pending));
1549 case GST_STATE_CHANGE_NO_PREROLL:
1550 LOG_MEDIA_MESSAGE("No preroll: State: %s, pending: %s",
1551 gst_element_state_get_name(state),
1552 gst_element_state_get_name(pending));
1554 if (state == GST_STATE_READY)
1555 m_readyState = MediaPlayer::HaveNothing;
1556 else if (state == GST_STATE_PAUSED) {
1557 m_readyState = MediaPlayer::HaveEnoughData;
1559 // Live pipelines go in PAUSED without prerolling.
1560 m_isStreaming = true;
1561 } else if (state == GST_STATE_PLAYING)
1565 shouldUpdateAfterSeek = true;
1568 gst_element_set_state(m_playBin.get(), GST_STATE_PLAYING);
1569 #if ENABLE(TIZEN_GSTREAMER_AUDIO)
1570 if (m_audioSessionManager)
1571 m_audioSessionManager->setSoundState(ASM_STATE_PLAYING);
1574 } else if (!m_paused) {
1575 gst_element_set_state(m_playBin.get(), GST_STATE_PLAYING);
1576 #if ENABLE(TIZEN_GSTREAMER_AUDIO)
1577 if (m_audioSessionManager)
1578 m_audioSessionManager->setSoundState(ASM_STATE_PLAYING);
1582 m_networkState = MediaPlayer::Loading;
1585 LOG_MEDIA_MESSAGE("Else : %d", ret);
1590 m_readyState = MediaPlayer::HaveNothing;
1592 if (shouldUpdateAfterSeek)
1595 if (m_networkState != oldNetworkState) {
1596 LOG_MEDIA_MESSAGE("Network State Changed from %u to %u",
1597 oldNetworkState, m_networkState);
1598 m_player->networkStateChanged();
1600 if (m_readyState != oldReadyState) {
1601 LOG_MEDIA_MESSAGE("Ready State Changed from %u to %u",
1602 oldReadyState, m_readyState);
1603 m_player->readyStateChanged();
1604 #if ENABLE(TIZEN_ACCELERATED_COMPOSITING) && USE(TIZEN_TEXTURE_MAPPER)
1605 if (state == GST_STATE_PLAYING)
1606 m_player->mediaPlayerClient()->mediaPlayerRenderingModeChanged(m_player);
1611 void MediaPlayerPrivateGStreamer::mediaLocationChanged(GstMessage* message)
1613 if (m_mediaLocations)
1614 gst_structure_free(m_mediaLocations);
1616 const GstStructure* structure = gst_message_get_structure(message);
1618 // This structure can contain:
1619 // - both a new-location string and embedded locations structure
1620 // - or only a new-location string.
1621 m_mediaLocations = gst_structure_copy(structure);
1622 const GValue* locations = gst_structure_get_value(m_mediaLocations, "locations");
1625 m_mediaLocationCurrentIndex = static_cast<int>(gst_value_list_get_size(locations)) -1;
1631 bool MediaPlayerPrivateGStreamer::loadNextLocation()
1633 if (!m_mediaLocations)
1636 const GValue* locations = gst_structure_get_value(m_mediaLocations, "locations");
1637 const gchar* newLocation = 0;
1640 // Fallback on new-location string.
1641 newLocation = gst_structure_get_string(m_mediaLocations, "new-location");
1647 if (m_mediaLocationCurrentIndex < 0) {
1648 m_mediaLocations = 0;
1652 const GValue* location = gst_value_list_get_value(locations,
1653 m_mediaLocationCurrentIndex);
1654 const GstStructure* structure = gst_value_get_structure(location);
1657 m_mediaLocationCurrentIndex--;
1661 newLocation = gst_structure_get_string(structure, "new-location");
1665 // Found a candidate. new-location is not always an absolute url
1666 // though. We need to take the base of the current url and
1667 // append the value of new-location to it.
1669 gchar* currentLocation = 0;
1670 g_object_get(m_playBin.get(), "uri", ¤tLocation, NULL);
1672 KURL currentUrl(KURL(), currentLocation);
1673 g_free(currentLocation);
1677 if (gst_uri_is_valid(newLocation))
1678 newUrl = KURL(KURL(), newLocation);
1680 newUrl = KURL(KURL(), currentUrl.baseAsString() + newLocation);
1682 RefPtr<SecurityOrigin> securityOrigin = SecurityOrigin::create(currentUrl);
1683 if (securityOrigin->canRequest(newUrl)) {
1684 LOG_MEDIA_MESSAGE("New media url: %s", newUrl.string().utf8().data());
1686 // Reset player states.
1687 m_networkState = MediaPlayer::Loading;
1688 m_player->networkStateChanged();
1689 m_readyState = MediaPlayer::HaveNothing;
1690 m_player->readyStateChanged();
1692 // Reset pipeline state.
1693 m_resetPipeline = true;
1694 gst_element_set_state(m_playBin.get(), GST_STATE_READY);
1695 #if ENABLE(TIZEN_GSTREAMER_AUDIO)
1696 if (m_audioSessionManager)
1697 m_audioSessionManager->setSoundState(ASM_STATE_NONE);
1700 gst_element_get_state(m_playBin.get(), &state, 0, 0);
1701 if (state <= GST_STATE_READY) {
1702 // Set the new uri and start playing.
1703 g_object_set(m_playBin.get(), "uri", newUrl.string().utf8().data(), NULL);
1704 gst_element_set_state(m_playBin.get(), GST_STATE_PLAYING);
1705 #if ENABLE(TIZEN_GSTREAMER_AUDIO)
1706 if (m_audioSessionManager)
1707 m_audioSessionManager->setSoundState(ASM_STATE_PLAYING);
1713 m_mediaLocationCurrentIndex--;
1718 void MediaPlayerPrivateGStreamer::loadStateChanged()
1723 void MediaPlayerPrivateGStreamer::sizeChanged()
1728 void MediaPlayerPrivateGStreamer::timeChanged()
1731 m_player->timeChanged();
1734 void MediaPlayerPrivateGStreamer::didEnd()
1736 // Synchronize position and duration values to not confuse the
1737 // HTMLMediaElement. In some cases like reverse playback the
1738 // position is not always reported as 0 for instance.
1739 float now = currentTime();
1740 #if ENABLE(TIZEN_GSTREAMER_VIDEO)
1741 if (now > 0 && now >= duration() && m_mediaDuration != now) {
1743 if (now > 0 && now <= duration() && m_mediaDuration != now) {
1745 m_mediaDurationKnown = true;
1746 m_mediaDuration = now;
1747 m_player->durationChanged();
1750 m_isEndReached = true;
1753 if (!m_player->mediaPlayerClient()->mediaPlayerIsLooping()) {
1755 #if ENABLE(TIZEN_GSTREAMER_AUDIO)
1756 gst_element_set_state(m_playBin.get(), GST_STATE_PAUSED);
1757 if (m_audioSessionManager)
1758 m_audioSessionManager->setSoundState(ASM_STATE_PAUSE);
1760 gst_element_set_state(m_playBin.get(), GST_STATE_NULL);
1763 #if ENABLE(TIZEN_GSTREAMER_VIDEO)
1764 HTMLMediaElement* element = static_cast<HTMLMediaElement*>(player()->mediaPlayerClient());
1765 if (element->isVideo())
1766 power_unlock_state(POWER_STATE_NORMAL);
1771 void MediaPlayerPrivateGStreamer::cacheDuration()
1773 #if ENABLE(TIZEN_GSTREAMER_VIDEO)
1774 float previousDuration = m_mediaDuration;
1776 // Reset cached media duration
1777 m_mediaDuration = 0;
1779 // And re-cache it if possible.
1781 gst_element_get_state(m_playBin.get(), &state, 0, 0);
1782 float newDuration = duration();
1784 #if ENABLE(TIZEN_GSTREAMER_VIDEO)
1785 if (state > GST_STATE_READY) {
1786 // Don't set m_mediaDurationKnown yet if the pipeline is not
1787 // paused. This allows duration() query to fail at least once
1788 // before playback starts and duration becomes known.
1789 m_mediaDurationKnown = !isinf(newDuration);
1792 if (!isinf(newDuration))
1793 m_mediaDuration = newDuration;
1795 m_mediaDuration = previousDuration;
1797 if (state <= GST_STATE_READY) {
1798 // Don't set m_mediaDurationKnown yet if the pipeline is not
1799 // paused. This allows duration() query to fail at least once
1800 // before playback starts and duration becomes known.
1801 if (!isinf(newDuration))
1802 m_mediaDuration = newDuration;
1804 m_mediaDurationKnown = !isinf(newDuration);
1805 if (m_mediaDurationKnown)
1806 m_mediaDuration = newDuration;
1809 if (!isinf(newDuration))
1810 m_mediaDuration = newDuration;
1814 void MediaPlayerPrivateGStreamer::durationChanged()
1816 float previousDuration = m_mediaDuration;
1819 // Avoid emiting durationchanged in the case where the previous
1820 // duration was 0 because that case is already handled by the
1821 // HTMLMediaElement.
1822 if (previousDuration && m_mediaDuration != previousDuration)
1823 m_player->durationChanged();
1825 if (m_preload == MediaPlayer::None && m_originalPreloadWasAutoAndWasOverridden) {
1827 if (totalBytes() && !isLiveStream()) {
1828 setPreload(MediaPlayer::Auto);
1829 gst_element_set_state(m_playBin.get(), GST_STATE_NULL);
1830 gst_element_set_state(m_playBin.get(), GST_STATE_PAUSED);
1831 #if ENABLE(TIZEN_GSTREAMER_AUDIO)
1832 if (m_audioSessionManager)
1833 m_audioSessionManager->setSoundState(ASM_STATE_PAUSE);
1839 bool MediaPlayerPrivateGStreamer::supportsMuting() const
1844 void MediaPlayerPrivateGStreamer::setMuted(bool muted)
1849 g_object_set(m_playBin.get(), "mute", muted, NULL);
1852 void MediaPlayerPrivateGStreamer::notifyPlayerOfMute()
1854 m_muteTimerHandler = 0;
1856 if (!m_player || !m_playBin)
1860 g_object_get(m_playBin.get(), "mute", &muted, NULL);
1861 m_player->muteChanged(static_cast<bool>(muted));
1864 void MediaPlayerPrivateGStreamer::muteChanged()
1866 if (m_muteTimerHandler)
1867 g_source_remove(m_muteTimerHandler);
1868 m_muteTimerHandler = g_timeout_add(0, reinterpret_cast<GSourceFunc>(mediaPlayerPrivateMuteChangeTimeoutCallback), this);
1871 void MediaPlayerPrivateGStreamer::loadingFailed(MediaPlayer::NetworkState error)
1873 #if ENABLE(TIZEN_DLOG_SUPPORT)
1874 TIZEN_LOGE("NetworkState : %d", error);
1877 m_errorOccured = true;
1878 if (m_networkState != error) {
1879 m_networkState = error;
1880 m_player->networkStateChanged();
1882 if (m_readyState != MediaPlayer::HaveNothing) {
1883 m_readyState = MediaPlayer::HaveNothing;
1884 m_player->readyStateChanged();
1888 void MediaPlayerPrivateGStreamer::setSize(const IntSize& size)
1893 void MediaPlayerPrivateGStreamer::setVisible(bool visible)
1897 void MediaPlayerPrivateGStreamer::triggerRepaint(GstBuffer* buffer)
1899 g_return_if_fail(GST_IS_BUFFER(buffer));
1900 gst_buffer_replace(&m_buffer, buffer);
1901 #if ENABLE(TIZEN_ACCELERATED_COMPOSITING) && USE(TIZEN_TEXTURE_MAPPER)
1902 #if !ENABLE(TIZEN_WEBKIT2_TILED_AC_SHARED_PLATFORM_SURFACE)
1903 if (m_videoSize.isEmpty())
1904 m_videoSize = naturalSize();
1905 m_videoLayer->paintVideoLayer(m_videoSize);
1909 m_player->repaint();
1912 void MediaPlayerPrivateGStreamer::paint(GraphicsContext* context, const IntRect& rect)
1914 if (context->paintingDisabled())
1917 if (!m_player->visible())
1923 GRefPtr<GstCaps> caps = webkitGstGetPadCaps(m_videoSinkPad.get());
1927 RefPtr<ImageGStreamer> gstImage = ImageGStreamer::createImage(m_buffer, caps.get());
1931 context->drawImage(reinterpret_cast<Image*>(gstImage->image().get()), ColorSpaceSRGB,
1932 rect, gstImage->rect(), CompositeCopy, DoNotRespectImageOrientation, false);
1935 static HashSet<String> mimeTypeCache()
1937 initializeGStreamerAndRegisterWebKitElements();
1939 DEFINE_STATIC_LOCAL(HashSet<String>, cache, ());
1940 static bool typeListInitialized = false;
1942 if (typeListInitialized)
1944 const char* mimeTypes[] = {
1946 "application/vnd.apple.mpegurl",
1947 "application/vnd.rn-realmedia",
1948 "application/x-3gp",
1949 #if ENABLE(TIZEN_GSTREAMER_VIDEO)
1950 "application/x-mpegurl",
1951 "application/x-mpegURL",
1953 "application/x-pn-realaudio",
1973 "audio/x-amr-nb-sh",
1974 "audio/x-amr-wb-sh",
2005 "audio/x-vorbis+ogg",
2009 "audio/x-wavpack-correction",
2018 #if !ENABLE(TIZEN_GSTREAMER_VIDEO)
2038 for (unsigned i = 0; i < (sizeof(mimeTypes) / sizeof(*mimeTypes)); ++i)
2039 cache.add(String(mimeTypes[i]));
2041 typeListInitialized = true;
2045 void MediaPlayerPrivateGStreamer::getSupportedTypes(HashSet<String>& types)
2047 types = mimeTypeCache();
2050 MediaPlayer::SupportsType MediaPlayerPrivateGStreamer::supportsType(const String& type, const String& codecs, const KURL&)
2052 if (type.isNull() || type.isEmpty())
2053 return MediaPlayer::IsNotSupported;
2055 // spec says we should not return "probably" if the codecs string is empty
2056 if (mimeTypeCache().contains(type))
2057 return codecs.isEmpty() ? MediaPlayer::MayBeSupported : MediaPlayer::IsSupported;
2058 return MediaPlayer::IsNotSupported;
2061 bool MediaPlayerPrivateGStreamer::hasSingleSecurityOrigin() const
2066 bool MediaPlayerPrivateGStreamer::supportsFullscreen() const
2068 #if PLATFORM(MAC) && !PLATFORM(IOS) && __MAC_OS_X_VERSION_MIN_REQUIRED == 1050
2069 // See <rdar://problem/7389945>
2076 PlatformMedia MediaPlayerPrivateGStreamer::platformMedia() const
2079 #ifndef GST_API_VERSION_1
2080 p.type = PlatformMedia::GStreamerGWorldType;
2081 p.media.gstreamerGWorld = m_gstGWorld.get();
2086 MediaPlayer::MovieLoadType MediaPlayerPrivateGStreamer::movieLoadType() const
2088 if (m_readyState == MediaPlayer::HaveNothing)
2089 return MediaPlayer::Unknown;
2092 return MediaPlayer::LiveStream;
2094 return MediaPlayer::Download;
2097 void MediaPlayerPrivateGStreamer::setDownloadBuffering()
2104 g_object_get(m_playBin.get(), "flags", &flags, NULL);
2105 #if ENABLE(TIZEN_ACCELERATED_COMPOSITING) && USE(TIZEN_TEXTURE_MAPPER) && ENABLE(TIZEN_WEBKIT2_TILED_AC_SHARED_PLATFORM_SURFACE)
2106 flags = (GstPlayFlags)(flags | GST_PLAY_FLAG_NATIVE_VIDEO);
2108 if (m_preload == MediaPlayer::Auto) {
2109 LOG_MEDIA_MESSAGE("Enabling on-disk buffering");
2110 g_object_set(m_playBin.get(), "flags", flags | GST_PLAY_FLAG_DOWNLOAD, NULL);
2112 LOG_MEDIA_MESSAGE("Disabling on-disk buffering");
2113 g_object_set(m_playBin.get(), "flags", flags & ~GST_PLAY_FLAG_DOWNLOAD, NULL);
2117 void MediaPlayerPrivateGStreamer::setPreload(MediaPlayer::Preload preload)
2119 m_originalPreloadWasAutoAndWasOverridden = m_preload != preload && m_preload == MediaPlayer::Auto;
2121 m_preload = preload;
2123 setDownloadBuffering();
2125 if (m_delayingLoad && m_preload != MediaPlayer::None) {
2126 m_delayingLoad = false;
2131 void MediaPlayerPrivateGStreamer::createAudioSink()
2133 // Construct audio sink if pitch preserving is enabled.
2134 if (!m_preservesPitch)
2140 GstElement* scale = gst_element_factory_make("scaletempo", 0);
2142 GST_WARNING("Failed to create scaletempo");
2146 GstElement* convert = gst_element_factory_make("audioconvert", 0);
2147 GstElement* resample = gst_element_factory_make("audioresample", 0);
2148 GstElement* sink = gst_element_factory_make("autoaudiosink", 0);
2150 GstElement* audioSink = gst_bin_new("audio-sink");
2151 gst_bin_add_many(GST_BIN(audioSink), scale, convert, resample, sink, NULL);
2153 if (!gst_element_link_many(scale, convert, resample, sink, NULL)) {
2154 GST_WARNING("Failed to link audio sink elements");
2155 gst_object_unref(audioSink);
2159 GRefPtr<GstPad> pad = adoptGRef(gst_element_get_static_pad(scale, "sink"));
2160 gst_element_add_pad(audioSink, gst_ghost_pad_new("sink", pad.get()));
2162 g_object_set(m_playBin.get(), "audio-sink", audioSink, NULL);
2165 #if ENABLE(TIZEN_GSTREAMER_VIDEO)
2166 GstElement* MediaPlayerPrivateGStreamer::createVideoSink()
2168 #if ENABLE(TIZEN_ACCELERATED_COMPOSITING) && USE(TIZEN_TEXTURE_MAPPER) && ENABLE(TIZEN_WEBKIT2_TILED_AC_SHARED_PLATFORM_SURFACE)
2169 m_webkitVideoSink = m_videoLayer->createVideoSink();
2170 m_videoSinkPad = adoptGRef(gst_element_get_static_pad(m_webkitVideoSink, "sink"));
2172 // Primary video should be OFF and Secondary video should be FULL SCREEN mode
2173 g_object_set(m_webkitVideoSink, "display-mode", 2, NULL);
2174 #else // ENABLE(TIZEN_ACCELERATED_COMPOSITING) && USE(TIZEN_TEXTURE_MAPPER) && ENABLE(TIZEN_WEBKIT2_TILED_AC_SHARED_PLATFORM_SURFACE)
2175 #ifndef GST_API_VERSION_1
2176 m_webkitVideoSink = webkitVideoSinkNew(m_gstGWorld.get());
2178 m_webkitVideoSink = webkitVideoSinkNew();
2180 m_videoSinkPad = adoptGRef(gst_element_get_static_pad(m_webkitVideoSink, "sink"));
2181 g_signal_connect(m_webkitVideoSink, "repaint-requested", G_CALLBACK(mediaPlayerPrivateRepaintCallback), this);
2182 #endif // ENABLE(TIZEN_ACCELERATED_COMPOSITING) && USE(TIZEN_TEXTURE_MAPPER) && ENABLE(TIZEN_WEBKIT2_TILED_AC_SHARED_PLATFORM_SURFACE)
2184 #ifndef GST_API_VERSION_1
2185 m_videoSinkBin = gst_bin_new("video-sink");
2186 GstElement* videoTee = gst_element_factory_make("tee", "videoTee");
2187 #if !ENABLE(TIZEN_GSTREAMER_VIDEO)
2188 GstElement* queue = gst_element_factory_make("queue", 0);
2191 gst_object_ref_sink(m_videoSinkBin);
2193 // Build a new video sink consisting of a bin containing a tee
2194 // (meant to distribute data to multiple video sinks) and our
2195 // internal video sink. For fullscreen we create an autovideosink
2196 // and initially block the data flow towards it and configure it
2197 #if !ENABLE(TIZEN_GSTREAMER_VIDEO)
2198 gst_bin_add_many(GST_BIN(m_videoSinkBin), videoTee, queue, NULL);
2200 gst_bin_add_many(GST_BIN(m_videoSinkBin), videoTee, NULL);
2203 #if !ENABLE(TIZEN_GSTREAMER_VIDEO)
2204 // Link a new src pad from tee to queue1.
2205 gst_element_link_pads_full(videoTee, 0, queue, "sink", GST_PAD_LINK_CHECK_NOTHING);
2209 GstElement* actualVideoSink = 0;
2210 m_fpsSink = gst_element_factory_make("fpsdisplaysink", "sink");
2212 // The verbose property has been added in -bad 0.10.22. Making
2213 // this whole code depend on it because we don't want
2214 // fpsdiplaysink to spit data on stdout.
2215 GstElementFactory* factory = GST_ELEMENT_FACTORY(GST_ELEMENT_GET_CLASS(m_fpsSink)->elementfactory);
2216 if (gst_plugin_feature_check_version(GST_PLUGIN_FEATURE(factory), 0, 10, 22)) {
2217 g_object_set(m_fpsSink, "silent", TRUE , NULL);
2219 // Turn off text overlay unless logging is enabled.
2221 g_object_set(m_fpsSink, "text-overlay", FALSE , NULL);
2223 WTFLogChannel* channel = getChannelFromName("Media");
2224 if (channel->state != WTFLogChannelOn)
2225 g_object_set(m_fpsSink, "text-overlay", FALSE , NULL);
2226 #endif // LOG_DISABLED
2228 if (g_object_class_find_property(G_OBJECT_GET_CLASS(m_fpsSink), "video-sink")) {
2229 g_object_set(m_fpsSink, "video-sink", m_webkitVideoSink, NULL);
2230 #ifndef GST_API_VERSION_1
2231 gst_bin_add(GST_BIN(m_videoSinkBin), m_fpsSink);
2233 actualVideoSink = m_fpsSink;
2241 #ifndef GST_API_VERSION_1
2242 gst_bin_add(GST_BIN(m_videoSinkBin), m_webkitVideoSink);
2244 actualVideoSink = m_webkitVideoSink;
2247 ASSERT(actualVideoSink);
2249 #ifndef GST_API_VERSION_1
2250 #if ENABLE(TIZEN_GSTREAMER_VIDEO)
2251 // Link a new src pad from tee to queue1.
2252 GRefPtr<GstPad> srcPad = adoptGRef(gst_element_get_request_pad(videoTee, "src%d"));
2253 GRefPtr<GstPad> sinkPad = adoptGRef(gst_element_get_static_pad(actualVideoSink, "sink"));
2254 gst_pad_link(srcPad.get(), sinkPad.get());
2256 // Faster elements linking.
2257 gst_element_link_pads_full(queue, "src", actualVideoSink, "sink", GST_PAD_LINK_CHECK_NOTHING);
2259 // Add a ghostpad to the bin so it can proxy to tee.
2260 GRefPtr<GstPad> pad = adoptGRef(gst_element_get_static_pad(videoTee, "sink"));
2261 gst_element_add_pad(m_videoSinkBin, gst_ghost_pad_new("sink", pad.get()));
2263 // Set the bin as video sink of playbin.
2264 return m_videoSinkBin;
2266 return actualVideoSink;
2271 void MediaPlayerPrivateGStreamer::createGSTPlayBin()
2275 // gst_element_factory_make() returns a floating reference so
2276 // we should not adopt.
2277 m_playBin = gst_element_factory_make(gPlaybinName, "play");
2279 #ifndef GST_API_VERSION_1
2280 m_gstGWorld = GStreamerGWorld::createGWorld(m_playBin.get());
2283 GRefPtr<GstBus> bus = webkitGstPipelineGetBus(GST_PIPELINE(m_playBin.get()));
2284 gst_bus_add_signal_watch(bus.get());
2285 g_signal_connect(bus.get(), "message", G_CALLBACK(mediaPlayerPrivateMessageCallback), this);
2286 #if ENABLE(TIZEN_ACCELERATED_COMPOSITING) && USE(TIZEN_TEXTURE_MAPPER) && ENABLE(TIZEN_WEBKIT2_TILED_AC_SHARED_PLATFORM_SURFACE)
2287 gst_bus_set_sync_handler(bus.get(), GstBusSyncHandler(mediaPlayerPrivateSyncHandler), this);
2289 gst_object_unref(bus.get());
2291 g_object_set(m_playBin.get(), "mute", m_player->muted(), NULL);
2293 g_signal_connect(m_playBin.get(), "notify::volume", G_CALLBACK(mediaPlayerPrivateVolumeChangedCallback), this);
2294 g_signal_connect(m_playBin.get(), "notify::source", G_CALLBACK(mediaPlayerPrivateSourceChangedCallback), this);
2295 g_signal_connect(m_playBin.get(), "notify::mute", G_CALLBACK(mediaPlayerPrivateMuteChangedCallback), this);
2296 g_signal_connect(m_playBin.get(), "video-changed", G_CALLBACK(mediaPlayerPrivateVideoChangedCallback), this);
2297 g_signal_connect(m_playBin.get(), "audio-changed", G_CALLBACK(mediaPlayerPrivateAudioChangedCallback), this);
2298 #if ENABLE(TIZEN_GSTREAMER_VIDEO)
2299 g_signal_connect(m_playBin.get(), "source-setup", G_CALLBACK(mediaPlayerPrivateSourceSetupCallback), this);
2302 #if ENABLE(TIZEN_GSTREAMER_VIDEO)
2303 HTMLMediaElement* element = static_cast<HTMLMediaElement*>(player()->mediaPlayerClient());
2304 if (element->isVideo()) {
2305 GstElement* videoElement = createVideoSink();
2306 g_object_set(m_playBin.get(), "video-sink", videoElement, NULL);
2308 GRefPtr<GstPad> videoSinkPad = adoptGRef(gst_element_get_static_pad(m_webkitVideoSink, "sink"));
2310 g_signal_connect(videoSinkPad.get(), "notify::caps", G_CALLBACK(mediaPlayerPrivateVideoSinkCapsChangedCallback), this);
2312 GstElement* fakeSink = gst_element_factory_make("fakesink", 0);
2313 g_object_set(fakeSink, "sync", true, NULL);
2314 g_object_set(m_playBin.get(), "video-sink", fakeSink, NULL);
2317 #if ENABLE(TIZEN_GSTREAMER_AUDIO)
2318 GstElement* realSink = gst_element_factory_make("avsysaudiosink", 0);
2319 g_object_set(realSink, "close-handle-on-prepare", 1, NULL);
2320 g_object_set(m_playBin.get(), "audio-sink", realSink, NULL);
2322 if (m_audioSessionManager)
2323 m_audioSessionManager->registerAudioSessionManager(MM_SESSION_TYPE_SHARE, mediaPlayerPrivateAudioSessionNotifyCallback, player());
2329 #if ENABLE(TIZEN_ACCELERATED_COMPOSITING) && USE(TIZEN_TEXTURE_MAPPER)
2330 bool MediaPlayerPrivateGStreamer::supportsAcceleratedRendering() const
2332 bool isEmbedVideo = false;
2333 if (m_player->mediaPlayerClient()) {
2334 Document* document = m_player->mediaPlayerClient()->mediaPlayerOwningDocument();
2335 if (document && document->settings()) {
2336 isEmbedVideo = document->settings()->acceleratedCompositingForVideoEnabled()
2337 && document->settings()->acceleratedCompositingEnabled();
2338 LOG_MEDIA_MESSAGE("isEmbedVideo: %d", isEmbedVideo);
2342 return isEmbedVideo;
2345 PlatformLayer* MediaPlayerPrivateGStreamer::platformLayer() const
2347 return m_videoLayer->platformLayer();
2350 #if ENABLE(TIZEN_WEBKIT2_TILED_AC_SHARED_PLATFORM_SURFACE)
2351 void MediaPlayerPrivateGStreamer::paintCurrentFrameInContext(GraphicsContext* context, const IntRect& rect)
2353 m_videoLayer->paintCurrentFrameInContext(context, rect);
2356 #if USE(ACCELERATED_VIDEO_VAAPI)
2357 // Other device doesn't prefer such optimization, so enclose it in USE(ACCELERATED_VIDEO_VAAPI) macro
2358 IntSize MediaPlayerPrivateGStreamer::scaleHDVideoToDisplaySize(int videoWidth, int videoHeight, int displayWidth, int displayHeight) const
2360 int fitWidth, fitHeight;
2362 #if ENABLE(TIZEN_DLOG_SUPPORT)
2363 TIZEN_LOGI("videoWidth: %d, videoHeight: %d, displayWidth: %d, displayHeight: %d ", videoWidth, videoHeight, displayWidth, displayHeight);
2365 fitWidth = videoWidth;
2366 fitHeight = videoHeight;
2367 if (displayWidth > 0 && displayHeight > 0) {
2368 // In case video is rotated, we always use a 'better'
2369 // orientation (landscape or portrait) to calculate fitWidth/fitHeight.
2370 // It means we use a bigger size which doesn't lose quality
2371 // in either landscape or portrait orientation
2372 if ((videoWidth > videoHeight && displayWidth < displayHeight)
2373 || (videoWidth < videoHeight && displayWidth > displayHeight)) {
2376 displayWidth = displayHeight;
2377 displayHeight = tmp;
2380 if (videoWidth > displayWidth || videoHeight > displayHeight) {
2381 if (videoWidth * displayHeight > videoHeight * displayWidth) {
2382 fitWidth = displayWidth;
2383 fitHeight = videoHeight * displayWidth / videoWidth;
2384 ASSERT(fitHeight <= displayHeight);
2386 fitWidth = videoWidth * displayHeight / videoHeight;
2387 fitHeight = displayHeight;
2388 ASSERT(fitWidth <= displayWidth);
2393 return IntSize(fitWidth, fitHeight);
2396 void MediaPlayerPrivateGStreamer::xWindowIdPrepared(GstMessage* message)
2398 // It is called in streaming thread.
2399 // It should be used only to set window handle to video sink.
2401 int videoWidth, videoHeight;
2402 gst_structure_get_int(message->structure, "video-width", &videoWidth);
2403 gst_structure_get_int(message->structure, "video-height", &videoHeight);
2405 #if ENABLE(TIZEN_DLOG_SUPPORT)
2406 TIZEN_LOGI("videoWidth: %d, videoHeight: %d", videoWidth, videoHeight);
2409 #if USE(ACCELERATED_VIDEO_VAAPI)
2410 int displayWidth, displayHeight;
2411 gst_structure_get_int(message->structure, "display-width", &displayWidth);
2412 gst_structure_get_int(message->structure, "display-height", &displayHeight);
2414 m_displaySize = IntSize(displayWidth, displayHeight);
2415 m_videoSize = scaleHDVideoToDisplaySize(videoWidth, videoHeight, displayWidth, displayHeight);
2417 m_videoSize = IntSize(videoWidth, videoHeight);
2419 m_videoLayer->setOverlay(m_videoSize);
2422 #endif // ENABLE(TIZEN_WEBKIT2_TILED_AC_SHARED_PLATFORM_SURFACE)
2423 #endif // ENABLE(TIZEN_ACCELERATED_COMPOSITING) && USE(TIZEN_TEXTURE_MAPPER)
2425 #if ENABLE(TIZEN_WEBKIT2_PROXY)
2426 void MediaPlayerPrivateGStreamer::setProxy(GstElement* source)
2428 SoupSession* session = WebCore::ResourceHandle::defaultSession();
2432 SoupURI* proxyUri = 0;
2433 g_object_get(session, SOUP_SESSION_PROXY_URI, &proxyUri, NULL);
2437 char* proxy = soup_uri_to_string(proxyUri, false);
2438 g_object_set(source, "proxy", proxy, NULL);
2440 soup_uri_free(proxyUri);
2445 #if ENABLE(TIZEN_GSTREAMER_VIDEO)
2446 void MediaPlayerPrivateGStreamer::suspend()
2448 m_suspendTime = currentTime();
2449 gst_element_set_state(m_playBin.get(), GST_STATE_NULL);
2450 #if ENABLE(TIZEN_GSTREAMER_AUDIO)
2451 if (m_audioSessionManager)
2452 m_audioSessionManager->setSoundState(ASM_STATE_STOP);
2456 void MediaPlayerPrivateGStreamer::resume()
2458 if (isLocalMediaStream()) {
2459 gst_element_set_state(m_playBin.get(), GST_STATE_PLAYING);
2460 #if ENABLE(TIZEN_GSTREAMER_AUDIO)
2461 if (m_audioSessionManager)
2462 m_audioSessionManager->setSoundState(ASM_STATE_PLAYING);
2465 gst_element_set_state(m_playBin.get(), GST_STATE_PAUSED);
2466 #if ENABLE(TIZEN_GSTREAMER_AUDIO)
2467 if (m_audioSessionManager)
2468 m_audioSessionManager->setSoundState(ASM_STATE_PAUSE);
2473 bool MediaPlayerPrivateGStreamer::isLocalMediaStream()
2475 if (m_url.string().contains("camera://"))
2480 #endif // ENABLE(TIZEN_GSTREAMER_VIDEO)
2484 #endif // USE(GSTREAMER)