DecoderTypes.h (6225B)
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim:set ts=2 sw=2 sts=2 et cindent: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #ifndef mozilla_dom_DecoderTypes_h 8 #define mozilla_dom_DecoderTypes_h 9 10 #include "MediaData.h" 11 #include "mozilla/Maybe.h" 12 #include "mozilla/dom/AudioData.h" 13 #include "mozilla/dom/AudioDecoderBinding.h" 14 #include "mozilla/dom/EncodedAudioChunk.h" 15 #include "mozilla/dom/EncodedVideoChunk.h" 16 #include "mozilla/dom/VideoColorSpaceBinding.h" 17 #include "mozilla/dom/VideoDecoderBinding.h" 18 #include "mozilla/dom/VideoFrame.h" 19 #include "nsStringFwd.h" 20 #include "nsTLiteralString.h" 21 22 namespace mozilla { 23 24 class TrackInfo; 25 class MediaByteBuffer; 26 27 namespace dom { 28 29 class VideoDecoderConfigInternal { 30 public: 31 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(VideoDecoderConfigInternal); 32 33 static RefPtr<VideoDecoderConfigInternal> Create( 34 const VideoDecoderConfig& aConfig); 35 36 VideoDecoderConfigInternal(const nsAString& aCodec, 37 Maybe<uint32_t>&& aCodedHeight, 38 Maybe<uint32_t>&& aCodedWidth, 39 Maybe<VideoColorSpaceInternal>&& aColorSpace, 40 already_AddRefed<MediaByteBuffer> aDescription, 41 Maybe<uint32_t>&& aDisplayAspectHeight, 42 Maybe<uint32_t>&& aDisplayAspectWidth, 43 const HardwareAcceleration& aHardwareAcceleration, 44 Maybe<bool>&& aOptimizeForLatency); 45 46 nsCString ToString() const; 47 48 bool Equals(const VideoDecoderConfigInternal& aOther) const { 49 if (mDescription != aOther.mDescription) { 50 return false; 51 } 52 if (mDescription && aOther.mDescription) { 53 auto lhs = mDescription; 54 auto rhs = aOther.mDescription; 55 if (lhs->Length() != rhs->Length()) { 56 return false; 57 } 58 if (!ArrayEqual(lhs->Elements(), rhs->Elements(), lhs->Length())) { 59 return false; 60 } 61 } 62 return mCodec.Equals(aOther.mCodec) && 63 mCodedHeight == aOther.mCodedHeight && 64 mCodedWidth == aOther.mCodedWidth && 65 mColorSpace == aOther.mColorSpace && 66 mDisplayAspectHeight == aOther.mDisplayAspectWidth && 67 mHardwareAcceleration == aOther.mHardwareAcceleration && 68 mOptimizeForLatency == aOther.mOptimizeForLatency; 69 } 70 71 nsString mCodec; 72 Maybe<uint32_t> mCodedHeight; 73 Maybe<uint32_t> mCodedWidth; 74 Maybe<VideoColorSpaceInternal> mColorSpace; 75 RefPtr<MediaByteBuffer> mDescription; 76 Maybe<uint32_t> mDisplayAspectHeight; 77 Maybe<uint32_t> mDisplayAspectWidth; 78 HardwareAcceleration mHardwareAcceleration; 79 Maybe<bool> mOptimizeForLatency; 80 81 private: 82 ~VideoDecoderConfigInternal() = default; 83 }; 84 85 class VideoDecoderTraits { 86 public: 87 static constexpr nsLiteralCString Name = "VideoDecoder"_ns; 88 using ConfigType = VideoDecoderConfig; 89 using ConfigTypeInternal = VideoDecoderConfigInternal; 90 using InputType = EncodedVideoChunk; 91 using InputTypeInternal = EncodedVideoChunkData; 92 using OutputType = VideoFrame; 93 using OutputCallbackType = VideoFrameOutputCallback; 94 95 static bool IsSupported(const ConfigTypeInternal& aConfig); 96 static Result<UniquePtr<TrackInfo>, nsresult> CreateTrackInfo( 97 const ConfigTypeInternal& aConfig); 98 static bool Validate(const ConfigType& aConfig, nsCString& aErrorMessage); 99 static RefPtr<ConfigTypeInternal> CreateConfigInternal( 100 const ConfigType& aConfig); 101 static bool IsKeyChunk(const InputType& aInput); 102 static UniquePtr<InputTypeInternal> CreateInputInternal( 103 const InputType& aInput); 104 }; 105 106 class AudioDecoderConfigInternal { 107 public: 108 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(AudioDecoderConfigInternal); 109 110 static RefPtr<AudioDecoderConfigInternal> Create( 111 const AudioDecoderConfig& aConfig); 112 113 AudioDecoderConfigInternal(const nsAString& aCodec, uint32_t aSampleRate, 114 uint32_t aNumberOfChannels, 115 already_AddRefed<MediaByteBuffer> aDescription); 116 117 bool Equals(const AudioDecoderConfigInternal& aOther) const { 118 if (mDescription != aOther.mDescription) { 119 return false; 120 } 121 if (mDescription && aOther.mDescription) { 122 auto lhs = mDescription; 123 auto rhs = aOther.mDescription; 124 if (lhs->Length() != rhs->Length()) { 125 return false; 126 } 127 if (!ArrayEqual(lhs->Elements(), rhs->Elements(), lhs->Length())) { 128 return false; 129 } 130 } 131 return mCodec.Equals(aOther.mCodec) && mSampleRate == aOther.mSampleRate && 132 mNumberOfChannels == aOther.mNumberOfChannels && 133 mOptimizeForLatency == aOther.mOptimizeForLatency; 134 } 135 nsCString ToString() const; 136 137 nsString mCodec; 138 uint32_t mSampleRate; 139 uint32_t mNumberOfChannels; 140 RefPtr<MediaByteBuffer> mDescription; 141 // Compilation fix, should be abstracted by DecoderAgent since those are not 142 // supported 143 HardwareAcceleration mHardwareAcceleration = 144 HardwareAcceleration::No_preference; 145 Maybe<bool> mOptimizeForLatency; 146 147 private: 148 ~AudioDecoderConfigInternal() = default; 149 }; 150 151 class AudioDecoderTraits { 152 public: 153 static constexpr nsLiteralCString Name = "AudioDecoder"_ns; 154 using ConfigType = AudioDecoderConfig; 155 using ConfigTypeInternal = AudioDecoderConfigInternal; 156 using InputType = EncodedAudioChunk; 157 using InputTypeInternal = EncodedAudioChunkData; 158 using OutputType = AudioData; 159 using OutputCallbackType = AudioDataOutputCallback; 160 161 static bool IsSupported(const ConfigTypeInternal& aConfig); 162 static Result<UniquePtr<TrackInfo>, nsresult> CreateTrackInfo( 163 const ConfigTypeInternal& aConfig); 164 static bool Validate(const ConfigType& aConfig, nsCString& aErrorMessage); 165 static RefPtr<ConfigTypeInternal> CreateConfigInternal( 166 const ConfigType& aConfig); 167 static bool IsKeyChunk(const InputType& aInput); 168 static UniquePtr<InputTypeInternal> CreateInputInternal( 169 const InputType& aInput); 170 }; 171 172 } // namespace dom 173 } // namespace mozilla 174 175 #endif // mozilla_dom_DecoderTypes_h