| 1 | /* |
| 2 | * Copyright (C) 2012, 2015, 2016 Igalia S.L |
| 3 | * Copyright (C) 2015, 2016 Metrological Group B.V. |
| 4 | * |
| 5 | * This library is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU Lesser General Public |
| 7 | * License as published by the Free Software Foundation; either |
| 8 | * version 2 of the License, or (at your option) any later version. |
| 9 | * |
| 10 | * This library is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | * Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU Lesser General Public |
| 16 | * License along with this library; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | */ |
| 19 | |
| 20 | #pragma once |
| 21 | |
| 22 | #if USE(GSTREAMER) |
| 23 | #include "FloatSize.h" |
| 24 | #include "GRefPtrGStreamer.h" |
| 25 | #include "GUniquePtrGStreamer.h" |
| 26 | #include <gst/gst.h> |
| 27 | #include <gst/video/video-format.h> |
| 28 | #include <gst/video/video-info.h> |
| 29 | #include <wtf/MediaTime.h> |
| 30 | #include <wtf/ThreadSafeRefCounted.h> |
| 31 | |
| 32 | namespace WebCore { |
| 33 | |
| 34 | class IntSize; |
| 35 | class SharedBuffer; |
| 36 | |
| 37 | inline bool webkitGstCheckVersion(guint major, guint minor, guint micro) |
| 38 | { |
| 39 | guint currentMajor, currentMinor, currentMicro, currentNano; |
| 40 | gst_version(¤tMajor, ¤tMinor, ¤tMicro, ¤tNano); |
| 41 | |
| 42 | if (currentMajor < major) |
| 43 | return false; |
| 44 | if (currentMajor > major) |
| 45 | return true; |
| 46 | |
| 47 | if (currentMinor < minor) |
| 48 | return false; |
| 49 | if (currentMinor > minor) |
| 50 | return true; |
| 51 | |
| 52 | if (currentMicro < micro) |
| 53 | return false; |
| 54 | |
| 55 | return true; |
| 56 | } |
| 57 | |
| 58 | #define GST_VIDEO_CAPS_TYPE_PREFIX "video/" |
| 59 | #define GST_AUDIO_CAPS_TYPE_PREFIX "audio/" |
| 60 | #define GST_TEXT_CAPS_TYPE_PREFIX "text/" |
| 61 | |
| 62 | GstPad* webkitGstGhostPadFromStaticTemplate(GstStaticPadTemplate*, const gchar* name, GstPad* target); |
| 63 | #if ENABLE(VIDEO) |
| 64 | bool getVideoSizeAndFormatFromCaps(GstCaps*, WebCore::IntSize&, GstVideoFormat&, int& pixelAspectRatioNumerator, int& pixelAspectRatioDenominator, int& stride); |
| 65 | Optional<FloatSize> getVideoResolutionFromCaps(const GstCaps*); |
| 66 | bool getSampleVideoInfo(GstSample*, GstVideoInfo&); |
| 67 | #endif |
| 68 | const char* capsMediaType(const GstCaps*); |
| 69 | bool doCapsHaveType(const GstCaps*, const char*); |
| 70 | bool areEncryptedCaps(const GstCaps*); |
| 71 | Vector<String> extractGStreamerOptionsFromCommandLine(); |
| 72 | bool initializeGStreamer(Optional<Vector<String>>&& = WTF::nullopt); |
| 73 | bool initializeGStreamerAndRegisterWebKitElements(); |
| 74 | unsigned getGstPlayFlag(const char* nick); |
| 75 | uint64_t toGstUnsigned64Time(const MediaTime&); |
| 76 | |
| 77 | inline GstClockTime toGstClockTime(const MediaTime &mediaTime) |
| 78 | { |
| 79 | return static_cast<GstClockTime>(toGstUnsigned64Time(mediaTime)); |
| 80 | } |
| 81 | |
| 82 | class GstMappedBuffer : public ThreadSafeRefCounted<GstMappedBuffer> { |
| 83 | public: |
| 84 | static RefPtr<GstMappedBuffer> create(GstBuffer* buffer, GstMapFlags flags) |
| 85 | { |
| 86 | GstMapInfo info; |
| 87 | if (!gst_buffer_map(buffer, &info, flags)) |
| 88 | return nullptr; |
| 89 | return adoptRef(new GstMappedBuffer(buffer, WTFMove(info))); |
| 90 | } |
| 91 | |
| 92 | // Unfortunately, GST_MAP_READWRITE is defined out of line from the MapFlags |
| 93 | // enum as an int, and C++ is careful to not implicity convert it to an enum. |
| 94 | static RefPtr<GstMappedBuffer> create(GstBuffer* buffer, int flags) |
| 95 | { |
| 96 | return GstMappedBuffer::create(buffer, static_cast<GstMapFlags>(flags)); |
| 97 | } |
| 98 | |
| 99 | ~GstMappedBuffer() |
| 100 | { |
| 101 | gst_buffer_unmap(m_buffer, &m_info); |
| 102 | } |
| 103 | |
| 104 | uint8_t* data() { return static_cast<uint8_t*>(m_info.data); } |
| 105 | const uint8_t* data() const { return static_cast<uint8_t*>(m_info.data); } |
| 106 | size_t size() const { return static_cast<size_t>(m_info.size); } |
| 107 | bool isSharable() const { return !(m_info.flags & GST_MAP_WRITE); } |
| 108 | Ref<SharedBuffer> createSharedBuffer(); |
| 109 | |
| 110 | private: |
| 111 | GstMappedBuffer(GstBuffer* buffer, GstMapInfo&& info) |
| 112 | : m_buffer(buffer) |
| 113 | , m_info(WTFMove(info)) |
| 114 | { |
| 115 | } |
| 116 | |
| 117 | friend bool operator==(const GstMappedBuffer&, const GstMappedBuffer&); |
| 118 | friend bool operator==(const GstMappedBuffer&, const GstBuffer*); |
| 119 | friend bool operator==(const GstBuffer* a, const GstMappedBuffer& b) { return operator==(b, a); } |
| 120 | |
| 121 | GstBuffer* m_buffer { nullptr }; |
| 122 | GstMapInfo m_info; |
| 123 | }; |
| 124 | |
| 125 | inline bool operator==(const GstMappedBuffer& a, const GstMappedBuffer& b) |
| 126 | { |
| 127 | return a.size() == b.size() && !gst_buffer_memcmp(a.m_buffer, 0, b.data(), b.size()); |
| 128 | } |
| 129 | |
| 130 | inline bool operator==(const GstMappedBuffer& a, const GstBuffer* b) |
| 131 | { |
| 132 | GstBuffer* nonConstB = const_cast<GstBuffer*>(b); |
| 133 | return a.size() == gst_buffer_get_size(nonConstB) && !gst_buffer_memcmp(nonConstB, 0, a.data(), a.size()); |
| 134 | } |
| 135 | |
| 136 | class GstMappedFrame { |
| 137 | WTF_MAKE_NONCOPYABLE(GstMappedFrame); |
| 138 | public: |
| 139 | |
| 140 | GstMappedFrame(GstBuffer* buffer, GstVideoInfo info, GstMapFlags flags) |
| 141 | { |
| 142 | m_isValid = gst_video_frame_map(&m_frame, &info, buffer, flags); |
| 143 | } |
| 144 | |
| 145 | GstMappedFrame(GRefPtr<GstSample> sample, GstMapFlags flags) |
| 146 | { |
| 147 | GstVideoInfo info; |
| 148 | |
| 149 | if (!gst_video_info_from_caps(&info, gst_sample_get_caps(sample.get()))) { |
| 150 | m_isValid = false; |
| 151 | return; |
| 152 | } |
| 153 | |
| 154 | m_isValid = gst_video_frame_map(&m_frame, &info, gst_sample_get_buffer(sample.get()), flags); |
| 155 | } |
| 156 | |
| 157 | GstVideoFrame* get() |
| 158 | { |
| 159 | if (!m_isValid) { |
| 160 | GST_INFO("Invalid frame, returning NULL" ); |
| 161 | |
| 162 | return nullptr; |
| 163 | } |
| 164 | |
| 165 | return &m_frame; |
| 166 | } |
| 167 | |
| 168 | uint8_t* ComponentData(int comp) |
| 169 | { |
| 170 | return GST_VIDEO_FRAME_COMP_DATA(&m_frame, comp); |
| 171 | } |
| 172 | |
| 173 | int ComponentStride(int stride) |
| 174 | { |
| 175 | return GST_VIDEO_FRAME_COMP_STRIDE(&m_frame, stride); |
| 176 | } |
| 177 | |
| 178 | GstVideoInfo* info() |
| 179 | { |
| 180 | if (!m_isValid) { |
| 181 | GST_INFO("Invalid frame, returning NULL" ); |
| 182 | |
| 183 | return nullptr; |
| 184 | } |
| 185 | |
| 186 | return &m_frame.info; |
| 187 | } |
| 188 | |
| 189 | int width() |
| 190 | { |
| 191 | return m_isValid ? GST_VIDEO_FRAME_WIDTH(&m_frame) : -1; |
| 192 | } |
| 193 | |
| 194 | int height() |
| 195 | { |
| 196 | return m_isValid ? GST_VIDEO_FRAME_HEIGHT(&m_frame) : -1; |
| 197 | } |
| 198 | |
| 199 | int format() |
| 200 | { |
| 201 | return m_isValid ? GST_VIDEO_FRAME_FORMAT(&m_frame) : GST_VIDEO_FORMAT_UNKNOWN; |
| 202 | } |
| 203 | |
| 204 | ~GstMappedFrame() |
| 205 | { |
| 206 | if (m_isValid) |
| 207 | gst_video_frame_unmap(&m_frame); |
| 208 | m_isValid = false; |
| 209 | } |
| 210 | |
| 211 | explicit operator bool() const { return m_isValid; } |
| 212 | |
| 213 | private: |
| 214 | GstVideoFrame m_frame; |
| 215 | bool m_isValid { false }; |
| 216 | }; |
| 217 | |
| 218 | |
| 219 | void connectSimpleBusMessageCallback(GstElement* pipeline); |
| 220 | void disconnectSimpleBusMessageCallback(GstElement* pipeline); |
| 221 | |
| 222 | } |
| 223 | |
| 224 | #ifndef GST_BUFFER_DTS_OR_PTS |
| 225 | #define GST_BUFFER_DTS_OR_PTS(buffer) (GST_BUFFER_DTS_IS_VALID(buffer) ? GST_BUFFER_DTS(buffer) : GST_BUFFER_PTS(buffer)) |
| 226 | #endif |
| 227 | #endif // USE(GSTREAMER) |
| 228 | |