1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
|
/*
* Copyright (C) 2020 Igalia S.L
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* aint with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#pragma once
#if USE(GSTREAMER) && ENABLE(VIDEO)
#include "GStreamerCommon.h"
#include "ImageDecoder.h"
#include "MIMETypeRegistry.h"
#include "SampleMap.h"
#include "SharedBuffer.h"
#include <wtf/Condition.h>
#include <wtf/Forward.h>
#include <wtf/Lock.h>
#include <wtf/RunLoop.h>
#include <wtf/WeakPtr.h>
namespace WebCore {
class ContentType;
class ImageDecoderGStreamerSample;
class ImageDecoderGStreamer final : public ImageDecoder {
WTF_MAKE_FAST_ALLOCATED;
WTF_MAKE_NONCOPYABLE(ImageDecoderGStreamer);
public:
static RefPtr<ImageDecoderGStreamer> create(FragmentedSharedBuffer&, const String& mimeType, AlphaOption, GammaAndColorProfileOption);
ImageDecoderGStreamer(FragmentedSharedBuffer&, const String& mimeType, AlphaOption, GammaAndColorProfileOption);
virtual ~ImageDecoderGStreamer() = default;
static bool supportsMediaType(MediaType type) { return type == MediaType::Video; }
static bool supportsContainerType(const String&);
size_t bytesDecodedToDetermineProperties() const override { return 0; }
static bool canDecodeType(const String& mimeType);
void setEncodedDataStatusChangeCallback(Function<void(EncodedDataStatus)>&& callback) final { m_encodedDataStatusChangedCallback = WTFMove(callback); }
EncodedDataStatus encodedDataStatus() const final;
IntSize size() const final;
size_t frameCount() const final { return m_sampleData.size(); }
RepetitionCount repetitionCount() const final;
String uti() const final;
String filenameExtension() const final { return MIMETypeRegistry::preferredExtensionForMIMEType(m_mimeType); }
std::optional<IntPoint> hotSpot() const final { return std::nullopt; }
IntSize frameSizeAtIndex(size_t, SubsamplingLevel = SubsamplingLevel::Default) const final { return size(); }
bool frameIsCompleteAtIndex(size_t index) const final { return sampleAtIndex(index); }
ImageDecoder::FrameMetadata frameMetadataAtIndex(size_t) const final;
Seconds frameDurationAtIndex(size_t) const final;
bool frameHasAlphaAtIndex(size_t) const final;
bool frameAllowSubsamplingAtIndex(size_t index) const final { return index <= m_sampleData.size(); }
unsigned frameBytesAtIndex(size_t, SubsamplingLevel = SubsamplingLevel::Default) const final;
PlatformImagePtr createFrameImageAtIndex(size_t, SubsamplingLevel = SubsamplingLevel::Default, const DecodingOptions& = DecodingOptions(DecodingMode::Synchronous)) final;
void setExpectedContentSize(long long) final { }
void setData(const FragmentedSharedBuffer&, bool allDataReceived) final;
bool isAllDataReceived() const final { return m_eos; }
void clearFrameBufferCache(size_t) final;
void setHasEOS();
void notifySample(GRefPtr<GstSample>&&);
private:
class InnerDecoder : public ThreadSafeRefCounted<InnerDecoder>, public CanMakeWeakPtr<InnerDecoder> {
WTF_MAKE_FAST_ALLOCATED;
WTF_MAKE_NONCOPYABLE(InnerDecoder);
public:
static RefPtr<InnerDecoder> create(ImageDecoderGStreamer& decoder, const uint8_t* data, gssize size)
{
return adoptRef(*new InnerDecoder(decoder, data, size));
}
InnerDecoder(ImageDecoderGStreamer& decoder, const uint8_t* data, gssize size)
: m_decoder(decoder)
, m_runLoop(RunLoop::current())
{
m_memoryStream = adoptGRef(g_memory_input_stream_new_from_data(data, size, nullptr));
}
~InnerDecoder();
void run();
EncodedDataStatus encodedDataStatus() const;
private :
static void decodebinPadAddedCallback(ImageDecoderGStreamer::InnerDecoder*, GstPad*);
void handleMessage(GstMessage*);
void preparePipeline();
void connectDecoderPad(GstPad*);
ImageDecoderGStreamer& m_decoder;
GRefPtr<GstElement> m_pipeline;
GRefPtr<GInputStream> m_memoryStream;
GRefPtr<GstElement> m_decodebin;
RunLoop& m_runLoop;
Condition m_messageCondition;
Lock m_messageLock;
bool m_messageDispatched WTF_GUARDED_BY_LOCK(m_messageLock) { false };
};
void handleSample(GRefPtr<GstSample>&&);
void pushEncodedData(const FragmentedSharedBuffer&);
const ImageDecoderGStreamerSample* sampleAtIndex(size_t) const;
Function<void(EncodedDataStatus)> m_encodedDataStatusChangedCallback;
SampleMap m_sampleData;
DecodeOrderSampleMap::iterator m_cursor;
Lock m_sampleGeneratorLock;
bool m_eos { false };
std::optional<IntSize> m_size;
String m_mimeType;
RefPtr<ImageDecoderGStreamer::InnerDecoder> m_innerDecoder;
Condition m_sampleCondition;
Lock m_sampleLock;
GRefPtr<GstSample> m_sample WTF_GUARDED_BY_LOCK(m_sampleLock);
Condition m_handlerCondition;
Lock m_handlerLock;
};
}
#endif
|