DecoderAgent.h (4155B)
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 DOM_MEDIA_WEBCODECS_DECODERAGENT_H 8 #define DOM_MEDIA_WEBCODECS_DECODERAGENT_H 9 10 #include "MediaResult.h" 11 #include "PlatformDecoderModule.h" 12 #include "mozilla/DefineEnum.h" 13 #include "mozilla/RefPtr.h" 14 #include "mozilla/TaskQueue.h" 15 #include "mozilla/UniquePtr.h" 16 17 class nsISerialEventTarget; 18 19 namespace mozilla { 20 21 class PDMFactory; 22 class TrackInfo; 23 24 namespace layers { 25 class ImageContainer; 26 } // namespace layers 27 28 // DecoderAgent is a wrapper that contains a MediaDataDecoder. It adapts the 29 // MediaDataDecoder APIs for use in WebCodecs. 30 // 31 // If Configure() is called, Shutdown() must be called to release the resources 32 // gracefully. Except Shutdown(), all the methods can't be called concurrently, 33 // meaning a method can only be called when the previous API call has completed. 34 // The responsability of arranging the method calls is on the caller. 35 // 36 // When Shutdown() is called, all the operations in flight are canceled and the 37 // MediaDataDecoder is shut down. On the other hand, errors are final. A new 38 // DecoderAgent must be created when an error is encountered. 39 // 40 // All the methods need to be called on the DecoderAgent's owner thread. In 41 // WebCodecs, it's either on the main thread or worker thread. 42 class DecoderAgent final { 43 public: 44 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(DecoderAgent); 45 46 using Id = uint32_t; 47 DecoderAgent(Id aId, UniquePtr<TrackInfo>&& aInfo); 48 49 // The following APIs are owner thread only. 50 51 using ConfigurePromise = MozPromise<bool, MediaResult, true /* exclusive */>; 52 RefPtr<ConfigurePromise> Configure(bool aPreferSoftwareDecoder, 53 bool aLowLatency); 54 RefPtr<ShutdownPromise> Shutdown(); 55 using DecodePromise = MediaDataDecoder::DecodePromise; 56 RefPtr<DecodePromise> Decode(MediaRawData* aSample); 57 // WebCodecs's flush() flushes out all the pending decoded data in the 58 // MediaDataDecoder so it is a combination of Drain and Flush. To distinguish 59 // the term from MediaDataDecoder's one, we call it DrainAndFlush() here. 60 RefPtr<DecodePromise> DrainAndFlush(); 61 62 const Id mId; // A unique id. 63 const UniquePtr<TrackInfo> mInfo; 64 65 private: 66 ~DecoderAgent(); 67 68 // Push out all the data in the MediaDataDecoder's pipeline. 69 // TODO: MediaDataDecoder should implement this, instead of asking call site 70 // to run `Drain` multiple times. 71 RefPtr<DecodePromise> Dry(); 72 void DrainUntilDry(); 73 74 MOZ_DEFINE_ENUM_CLASS_WITH_TOSTRING_AT_CLASS_SCOPE( 75 State, (Unconfigured, Configuring, Configured, Decoding, Flushing, 76 ShuttingDown, Error)); 77 void SetState(State aState); 78 79 const RefPtr<nsISerialEventTarget> mOwnerThread; 80 const RefPtr<PDMFactory> mPDMFactory; 81 const RefPtr<layers::ImageContainer> mImageContainer; 82 RefPtr<MediaDataDecoder> mDecoder; 83 State mState; 84 85 // Configure 86 MozPromiseHolder<ConfigurePromise> mConfigurePromise; 87 using CreateDecoderPromise = PlatformDecoderModule::CreateDecoderPromise; 88 MozPromiseRequestHolder<CreateDecoderPromise> mCreateRequest; 89 using InitPromise = MediaDataDecoder::InitPromise; 90 MozPromiseRequestHolder<InitPromise> mInitRequest; 91 92 // Shutdown 93 MozPromiseHolder<ShutdownPromise> mShutdownWhileCreationPromise; 94 95 // Decode 96 MozPromiseHolder<DecodePromise> mDecodePromise; 97 MozPromiseRequestHolder<DecodePromise> mDecodeRequest; 98 99 // DrainAndFlush 100 MozPromiseHolder<DecodePromise> mDrainAndFlushPromise; 101 MediaDataDecoder::DecodedData mDrainAndFlushData; 102 MozPromiseRequestHolder<DecodePromise> mDryRequest; 103 MozPromiseHolder<DecodePromise> mDryPromise; 104 MediaDataDecoder::DecodedData mDryData; 105 MozPromiseRequestHolder<DecodePromise> mDrainRequest; 106 using FlushPromise = MediaDataDecoder::FlushPromise; 107 MozPromiseRequestHolder<FlushPromise> mFlushRequest; 108 }; 109 110 } // namespace mozilla 111 112 #endif // DOM_MEDIA_WEBCODECS_DECODERAGENT_H