1/*
2 * Copyright (C) 2007, 2009 Apple Inc. All rights reserved.
3 * Copyright (C) 2007 Collabora Ltd. All rights reserved.
4 * Copyright (C) 2007 Alp Toker <alp@atoker.com>
5 * Copyright (C) 2009, 2010, 2015, 2016 Igalia S.L
6 * Copyright (C) 2015, 2016 Metrological Group B.V.
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
17 *
18 * You should have received a copy of the GNU Library General Public License
19 * aint with this library; see the file COPYING.LIB. If not, write to
20 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
22 */
23
24#ifndef MediaPlayerPrivateGStreamerBase_h
25#define MediaPlayerPrivateGStreamerBase_h
26#if ENABLE(VIDEO) && USE(GSTREAMER)
27
28#include "GStreamerCommon.h"
29#include "GStreamerEMEUtilities.h"
30#include "MainThreadNotifier.h"
31#include "MediaPlayerPrivate.h"
32#include "PlatformLayer.h"
33#include <glib.h>
34#include <gst/gst.h>
35#include <wtf/Condition.h>
36#include <wtf/Forward.h>
37#include <wtf/RunLoop.h>
38#include <wtf/WeakPtr.h>
39
40#if USE(TEXTURE_MAPPER_GL)
41#include "TextureMapperGL.h"
42#if USE(NICOSIA)
43#include "NicosiaContentLayerTextureMapperImpl.h"
44#else
45#include "TextureMapperPlatformLayerProxyProvider.h"
46#endif
47#endif
48
49typedef struct _GstStreamVolume GstStreamVolume;
50typedef struct _GstVideoInfo GstVideoInfo;
51typedef struct _GstGLContext GstGLContext;
52typedef struct _GstGLDisplay GstGLDisplay;
53
54namespace WebCore {
55
56class BitmapTextureGL;
57class GLContext;
58class GraphicsContext;
59class GraphicsContext3D;
60class IntSize;
61class IntRect;
62class VideoTextureCopierGStreamer;
63
64#if USE(TEXTURE_MAPPER_GL)
65class TextureMapperPlatformLayerProxy;
66#endif
67
68void registerWebKitGStreamerElements();
69
70class MediaPlayerPrivateGStreamerBase : public MediaPlayerPrivateInterface, public CanMakeWeakPtr<MediaPlayerPrivateGStreamerBase>
71#if USE(TEXTURE_MAPPER_GL)
72#if USE(NICOSIA)
73 , public Nicosia::ContentLayerTextureMapperImpl::Client
74#else
75 , public PlatformLayer
76#endif
77#endif
78{
79
80public:
81 static void initializeDebugCategory();
82
83 virtual ~MediaPlayerPrivateGStreamerBase();
84
85 FloatSize naturalSize() const override;
86
87 void setVolume(float) override;
88 float volume() const override;
89
90#if USE(GSTREAMER_GL)
91 bool ensureGstGLContext();
92 GstContext* requestGLContext(const char* contextType);
93#endif
94 bool supportsMuting() const override { return true; }
95 void setMuted(bool) override;
96 bool muted() const;
97
98 MediaPlayer::NetworkState networkState() const override;
99 MediaPlayer::ReadyState readyState() const override;
100
101 void setVisible(bool) override { }
102 void setSize(const IntSize&) override;
103 void sizeChanged();
104
105 // Prefer MediaTime based methods over float based.
106 float duration() const override { return durationMediaTime().toFloat(); }
107 double durationDouble() const override { return durationMediaTime().toDouble(); }
108 MediaTime durationMediaTime() const override { return MediaTime::zeroTime(); }
109 float currentTime() const override { return currentMediaTime().toFloat(); }
110 double currentTimeDouble() const override { return currentMediaTime().toDouble(); }
111 MediaTime currentMediaTime() const override { return MediaTime::zeroTime(); }
112 void seek(float time) override { seek(MediaTime::createWithFloat(time)); }
113 void seekDouble(double time) override { seek(MediaTime::createWithDouble(time)); }
114 void seek(const MediaTime&) override { }
115 float maxTimeSeekable() const override { return maxMediaTimeSeekable().toFloat(); }
116 MediaTime maxMediaTimeSeekable() const override { return MediaTime::zeroTime(); }
117 double minTimeSeekable() const override { return minMediaTimeSeekable().toFloat(); }
118 MediaTime minMediaTimeSeekable() const override { return MediaTime::zeroTime(); }
119
120 void paint(GraphicsContext&, const FloatRect&) override;
121
122 bool hasSingleSecurityOrigin() const override { return true; }
123 virtual MediaTime maxTimeLoaded() const { return MediaTime::zeroTime(); }
124
125 bool supportsFullscreen() const override;
126
127 MediaPlayer::MovieLoadType movieLoadType() const override;
128 virtual bool isLiveStream() const = 0;
129
130 MediaPlayer* mediaPlayer() const { return m_player; }
131
132 unsigned decodedFrameCount() const override;
133 unsigned droppedFrameCount() const override;
134 unsigned audioDecodedByteCount() const override;
135 unsigned videoDecodedByteCount() const override;
136
137 void acceleratedRenderingStateChanged() override;
138
139#if USE(TEXTURE_MAPPER_GL)
140 PlatformLayer* platformLayer() const override;
141#if PLATFORM(WIN_CAIRO)
142 // FIXME: Accelerated rendering has not been implemented for WinCairo yet.
143 bool supportsAcceleratedRendering() const override { return false; }
144#else
145 bool supportsAcceleratedRendering() const override { return true; }
146#endif
147#endif
148
149#if ENABLE(ENCRYPTED_MEDIA)
150 void cdmInstanceAttached(CDMInstance&) override;
151 void cdmInstanceDetached(CDMInstance&) override;
152 void handleProtectionEvent(GstEvent*);
153 virtual void attemptToDecryptWithLocalInstance();
154 void attemptToDecryptWithInstance(CDMInstance&) final;
155 void initializationDataEncountered(InitData&&);
156 void setWaitingForKey(bool);
157 bool waitingForKey() const override;
158#endif
159
160 static bool supportsKeySystem(const String& keySystem, const String& mimeType);
161 static MediaPlayer::SupportsType extendedSupportsType(const MediaEngineSupportParameters&, MediaPlayer::SupportsType);
162
163#if USE(GSTREAMER_GL)
164 bool copyVideoTextureToPlatformTexture(GraphicsContext3D*, Platform3DObject, GC3Denum, GC3Dint, GC3Denum, GC3Denum, GC3Denum, bool, bool) override;
165 NativeImagePtr nativeImageForCurrentTime() override;
166#endif
167
168 void setVideoSourceOrientation(const ImageOrientation&);
169 GstElement* pipeline() const { return m_pipeline.get(); }
170
171 virtual bool handleSyncMessage(GstMessage*);
172
173protected:
174 MediaPlayerPrivateGStreamerBase(MediaPlayer*);
175 virtual GstElement* createVideoSink();
176
177#if USE(GSTREAMER_HOLEPUNCH)
178 GstElement* createHolePunchVideoSink();
179 void pushNextHolePunchBuffer();
180 bool shouldIgnoreIntrinsicSize() final { return true; }
181#endif
182
183#if USE(GSTREAMER_GL)
184 static GstFlowReturn newSampleCallback(GstElement*, MediaPlayerPrivateGStreamerBase*);
185 static GstFlowReturn newPrerollCallback(GstElement*, MediaPlayerPrivateGStreamerBase*);
186 void flushCurrentBuffer();
187 GstElement* createGLAppSink();
188 GstElement* createVideoSinkGL();
189 GstGLContext* gstGLContext() const { return m_glContext.get(); }
190 GstGLDisplay* gstGLDisplay() const { return m_glDisplay.get(); }
191 void ensureGLVideoSinkContext();
192#endif
193
194#if USE(TEXTURE_MAPPER_GL)
195 void pushTextureToCompositor();
196#if USE(NICOSIA)
197 void swapBuffersIfNeeded() override;
198#else
199 RefPtr<TextureMapperPlatformLayerProxy> proxy() const override;
200 void swapBuffersIfNeeded() override;
201#endif
202#endif
203
204 GstElement* videoSink() const { return m_videoSink.get(); }
205
206 void setStreamVolumeElement(GstStreamVolume*);
207 virtual GstElement* createAudioSink() { return 0; }
208 virtual GstElement* audioSink() const { return 0; }
209
210 void setPipeline(GstElement*);
211
212 void triggerRepaint(GstSample*);
213 void repaint();
214 void cancelRepaint(bool destroying = false);
215
216 static void repaintCallback(MediaPlayerPrivateGStreamerBase*, GstSample*);
217 static void repaintCancelledCallback(MediaPlayerPrivateGStreamerBase*);
218
219 void notifyPlayerOfVolumeChange();
220 void notifyPlayerOfMute();
221
222 static void volumeChangedCallback(MediaPlayerPrivateGStreamerBase*);
223 static void muteChangedCallback(MediaPlayerPrivateGStreamerBase*);
224
225#if USE(TEXTURE_MAPPER_GL)
226 void updateTextureMapperFlags();
227 TextureMapperGL::Flags m_textureMapperFlags;
228#endif
229
230 enum MainThreadNotification {
231 VideoChanged = 1 << 0,
232 VideoCapsChanged = 1 << 1,
233 AudioChanged = 1 << 2,
234 VolumeChanged = 1 << 3,
235 MuteChanged = 1 << 4,
236#if ENABLE(VIDEO_TRACK)
237 TextChanged = 1 << 5,
238#endif
239 SizeChanged = 1 << 6,
240#if GST_CHECK_VERSION(1, 10, 0)
241 StreamCollectionChanged = 1 << 7
242#endif
243 };
244
245 Ref<MainThreadNotifier<MainThreadNotification>> m_notifier;
246 MediaPlayer* m_player;
247 GRefPtr<GstElement> m_pipeline;
248 GRefPtr<GstStreamVolume> m_volumeElement;
249 GRefPtr<GstElement> m_videoSink;
250 GRefPtr<GstElement> m_fpsSink;
251 MediaPlayer::ReadyState m_readyState;
252 mutable MediaPlayer::NetworkState m_networkState;
253 IntSize m_size;
254 mutable Lock m_sampleMutex;
255 GRefPtr<GstSample> m_sample;
256
257 mutable FloatSize m_videoSize;
258 bool m_usingFallbackVideoSink { false };
259 bool m_renderingCanBeAccelerated { false };
260
261 Condition m_drawCondition;
262 Lock m_drawMutex;
263 bool m_destroying { false };
264 RunLoop::Timer<MediaPlayerPrivateGStreamerBase> m_drawTimer;
265
266#if USE(TEXTURE_MAPPER_GL)
267#if USE(NICOSIA)
268 Ref<Nicosia::ContentLayer> m_nicosiaLayer;
269#else
270 RefPtr<TextureMapperPlatformLayerProxy> m_platformLayerProxy;
271#endif
272#endif
273
274#if USE(GSTREAMER_GL)
275 GRefPtr<GstGLContext> m_glContext;
276 GRefPtr<GstGLDisplay> m_glDisplay;
277 GRefPtr<GstContext> m_glDisplayElementContext;
278 GRefPtr<GstContext> m_glAppElementContext;
279 std::unique_ptr<VideoTextureCopierGStreamer> m_videoTextureCopier;
280#endif
281
282 ImageOrientation m_videoSourceOrientation;
283
284#if ENABLE(ENCRYPTED_MEDIA)
285 Lock m_protectionMutex;
286 Condition m_protectionCondition;
287 RefPtr<const CDMInstance> m_cdmInstance;
288 HashSet<uint32_t> m_handledProtectionEvents;
289 bool m_waitingForKey { false };
290#endif
291
292 enum class WebKitGstVideoDecoderPlatform { ImxVPU, Video4Linux };
293 Optional<WebKitGstVideoDecoderPlatform> m_videoDecoderPlatform;
294};
295
296}
297
298#endif // USE(GSTREAMER)
299#endif
300