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, 2010, 2015, 2016 Igalia S.L
6 * Copyright (C) 2015, 2016 Metrological Group B.V.
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.
24 #ifndef MediaPlayerPrivateGStreamerBase_h
25 #define MediaPlayerPrivateGStreamerBase_h
26 #if ENABLE(VIDEO) && USE(GSTREAMER)
28 #include "GStreamerCommon.h"
29 #include "MainThreadNotifier.h"
30 #include "MediaPlayerPrivate.h"
31 #include "PlatformLayer.h"
34 #include <wtf/Condition.h>
35 #include <wtf/Forward.h>
36 #include <wtf/RunLoop.h>
37 #include <wtf/WeakPtr.h>
39 #if USE(TEXTURE_MAPPER_GL)
40 #include "TextureMapperPlatformLayerProxyProvider.h"
43 typedef struct _GstStreamVolume GstStreamVolume;
44 typedef struct _GstVideoInfo GstVideoInfo;
45 typedef struct _GstGLContext GstGLContext;
46 typedef struct _GstGLDisplay GstGLDisplay;
50 class BitmapTextureGL;
52 class GraphicsContext;
53 class GraphicsContext3D;
56 class VideoTextureCopierGStreamer;
58 #if USE(TEXTURE_MAPPER_GL)
59 class TextureMapperPlatformLayerProxy;
62 void registerWebKitGStreamerElements();
64 class MediaPlayerPrivateGStreamerBase : public MediaPlayerPrivateInterface, public CanMakeWeakPtr<MediaPlayerPrivateGStreamerBase>
65 #if USE(TEXTURE_MAPPER_GL)
66 , public PlatformLayer
71 virtual ~MediaPlayerPrivateGStreamerBase();
73 FloatSize naturalSize() const override;
75 void setVolume(float) override;
76 float volume() const override;
79 bool ensureGstGLContext();
80 GstContext* requestGLContext(const char* contextType);
82 static bool initializeGStreamerAndRegisterWebKitElements();
83 bool supportsMuting() const override { return true; }
84 void setMuted(bool) override;
87 MediaPlayer::NetworkState networkState() const override;
88 MediaPlayer::ReadyState readyState() const override;
90 void setVisible(bool) override { }
91 void setSize(const IntSize&) override;
94 // Prefer MediaTime based methods over float based.
95 float duration() const override { return durationMediaTime().toFloat(); }
96 double durationDouble() const override { return durationMediaTime().toDouble(); }
97 MediaTime durationMediaTime() const override { return MediaTime::zeroTime(); }
98 float currentTime() const override { return currentMediaTime().toFloat(); }
99 double currentTimeDouble() const override { return currentMediaTime().toDouble(); }
100 MediaTime currentMediaTime() const override { return MediaTime::zeroTime(); }
101 void seek(float time) override { seek(MediaTime::createWithFloat(time)); }
102 void seekDouble(double time) override { seek(MediaTime::createWithDouble(time)); }
103 void seek(const MediaTime&) override { }
104 float maxTimeSeekable() const override { return maxMediaTimeSeekable().toFloat(); }
105 MediaTime maxMediaTimeSeekable() const override { return MediaTime::zeroTime(); }
106 double minTimeSeekable() const override { return minMediaTimeSeekable().toFloat(); }
107 MediaTime minMediaTimeSeekable() const override { return MediaTime::zeroTime(); }
109 void paint(GraphicsContext&, const FloatRect&) override;
111 bool hasSingleSecurityOrigin() const override { return true; }
112 virtual MediaTime maxTimeLoaded() const { return MediaTime::zeroTime(); }
114 bool supportsFullscreen() const override;
116 MediaPlayer::MovieLoadType movieLoadType() const override;
117 virtual bool isLiveStream() const = 0;
119 MediaPlayer* mediaPlayer() const { return m_player; }
121 unsigned decodedFrameCount() const override;
122 unsigned droppedFrameCount() const override;
123 unsigned audioDecodedByteCount() const override;
124 unsigned videoDecodedByteCount() const override;
126 void acceleratedRenderingStateChanged() override;
128 #if USE(TEXTURE_MAPPER_GL)
129 PlatformLayer* platformLayer() const override { return const_cast<MediaPlayerPrivateGStreamerBase*>(this); }
130 #if PLATFORM(WIN_CAIRO)
131 // FIXME: Accelerated rendering has not been implemented for WinCairo yet.
132 bool supportsAcceleratedRendering() const override { return false; }
134 bool supportsAcceleratedRendering() const override { return true; }
138 #if ENABLE(ENCRYPTED_MEDIA)
139 void cdmInstanceAttached(CDMInstance&) override;
140 void cdmInstanceDetached(CDMInstance&) override;
141 void dispatchDecryptionKey(GstBuffer*);
142 void handleProtectionEvent(GstEvent*);
143 void attemptToDecryptWithLocalInstance();
144 void attemptToDecryptWithInstance(CDMInstance&) override;
145 void dispatchCDMInstance();
146 void initializationDataEncountered(GstEvent*);
149 static bool supportsKeySystem(const String& keySystem, const String& mimeType);
150 static MediaPlayer::SupportsType extendedSupportsType(const MediaEngineSupportParameters&, MediaPlayer::SupportsType);
152 #if USE(GSTREAMER_GL)
153 bool copyVideoTextureToPlatformTexture(GraphicsContext3D*, Platform3DObject, GC3Denum, GC3Dint, GC3Denum, GC3Denum, GC3Denum, bool, bool) override;
154 NativeImagePtr nativeImageForCurrentTime() override;
157 void setVideoSourceOrientation(const ImageOrientation&);
158 GstElement* pipeline() const { return m_pipeline.get(); }
160 virtual bool handleSyncMessage(GstMessage*);
163 MediaPlayerPrivateGStreamerBase(MediaPlayer*);
164 virtual GstElement* createVideoSink();
166 #if USE(GSTREAMER_GL)
167 static GstFlowReturn newSampleCallback(GstElement*, MediaPlayerPrivateGStreamerBase*);
168 static GstFlowReturn newPrerollCallback(GstElement*, MediaPlayerPrivateGStreamerBase*);
169 void flushCurrentBuffer();
170 GstElement* createGLAppSink();
171 GstElement* createVideoSinkGL();
172 GstGLContext* gstGLContext() const { return m_glContext.get(); }
173 GstGLDisplay* gstGLDisplay() const { return m_glDisplay.get(); }
174 void ensureGLVideoSinkContext();
177 #if USE(TEXTURE_MAPPER_GL)
178 RefPtr<TextureMapperPlatformLayerProxy> proxy() const override;
179 void swapBuffersIfNeeded() override;
180 void pushTextureToCompositor();
183 GstElement* videoSink() const { return m_videoSink.get(); }
185 void setStreamVolumeElement(GstStreamVolume*);
186 virtual GstElement* createAudioSink() { return 0; }
187 virtual GstElement* audioSink() const { return 0; }
189 void setPipeline(GstElement*);
191 void triggerRepaint(GstSample*);
193 void cancelRepaint(bool destroying = false);
195 static void repaintCallback(MediaPlayerPrivateGStreamerBase*, GstSample*);
196 static void repaintCancelledCallback(MediaPlayerPrivateGStreamerBase*);
198 void notifyPlayerOfVolumeChange();
199 void notifyPlayerOfMute();
201 static void volumeChangedCallback(MediaPlayerPrivateGStreamerBase*);
202 static void muteChangedCallback(MediaPlayerPrivateGStreamerBase*);
204 enum MainThreadNotification {
205 VideoChanged = 1 << 0,
206 VideoCapsChanged = 1 << 1,
207 AudioChanged = 1 << 2,
208 VolumeChanged = 1 << 3,
209 MuteChanged = 1 << 4,
210 #if ENABLE(VIDEO_TRACK)
211 TextChanged = 1 << 5,
213 SizeChanged = 1 << 6,
214 #if GST_CHECK_VERSION(1, 10, 0)
215 StreamCollectionChanged = 1 << 7
219 Ref<MainThreadNotifier<MainThreadNotification>> m_notifier;
220 MediaPlayer* m_player;
221 GRefPtr<GstElement> m_pipeline;
222 GRefPtr<GstStreamVolume> m_volumeElement;
223 GRefPtr<GstElement> m_videoSink;
224 GRefPtr<GstElement> m_fpsSink;
225 MediaPlayer::ReadyState m_readyState;
226 mutable MediaPlayer::NetworkState m_networkState;
228 mutable Lock m_sampleMutex;
229 GRefPtr<GstSample> m_sample;
231 mutable FloatSize m_videoSize;
232 bool m_usingFallbackVideoSink { false };
233 bool m_renderingCanBeAccelerated { false };
235 Condition m_drawCondition;
237 bool m_destroying { false };
238 RunLoop::Timer<MediaPlayerPrivateGStreamerBase> m_drawTimer;
240 #if USE(TEXTURE_MAPPER_GL)
241 RefPtr<TextureMapperPlatformLayerProxy> m_platformLayerProxy;
244 #if USE(GSTREAMER_GL)
245 GRefPtr<GstGLContext> m_glContext;
246 GRefPtr<GstGLDisplay> m_glDisplay;
247 GRefPtr<GstContext> m_glDisplayElementContext;
248 GRefPtr<GstContext> m_glAppElementContext;
249 std::unique_ptr<VideoTextureCopierGStreamer> m_videoTextureCopier;
252 ImageOrientation m_videoSourceOrientation;
254 #if ENABLE(ENCRYPTED_MEDIA)
255 Lock m_protectionMutex;
256 Condition m_protectionCondition;
257 RefPtr<const CDMInstance> m_cdmInstance;
258 HashSet<uint32_t> m_handledProtectionEvents;
259 bool m_needToResendCredentials { false };
265 #endif // USE(GSTREAMER)