tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

EncoderAgent.h (3989B)


      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_EncoderAgent_H
      8 #define DOM_MEDIA_WEBCODECS_EncoderAgent_H
      9 
     10 #include "MediaResult.h"
     11 #include "PEMFactory.h"
     12 #include "PlatformEncoderModule.h"
     13 #include "WebCodecsUtils.h"
     14 #include "mozilla/DefineEnum.h"
     15 #include "mozilla/RefPtr.h"
     16 #include "mozilla/TaskQueue.h"
     17 
     18 class nsISerialEventTarget;
     19 
     20 namespace mozilla {
     21 
     22 class PDMFactory;
     23 class TrackInfo;
     24 
     25 namespace layers {
     26 class ImageContainer;
     27 }  // namespace layers
     28 
     29 // EncoderAgent is a wrapper that contains a MediaDataEncoder. It adapts the
     30 // MediaDataEncoder APIs for use in WebCodecs.
     31 //
     32 // If Configure() is called, Shutdown() must be called to release the resources
     33 // gracefully. Except Shutdown(), all the methods can't be called concurrently,
     34 // meaning a method can only be called when the previous API call has completed.
     35 // The responsability of arranging the method calls is on the caller.
     36 //
     37 // When Shutdown() is called, all the operations in flight are canceled and the
     38 // MediaDataEncoder is shut down. On the other hand, errors are final. A new
     39 // EncoderAgent must be created when an error is encountered.
     40 //
     41 // All the methods need to be called on the EncoderAgent's owner thread. In
     42 // WebCodecs, it's either on the main thread or worker thread.
     43 class EncoderAgent final {
     44 public:
     45  NS_INLINE_DECL_THREADSAFE_REFCOUNTING(EncoderAgent);
     46 
     47  explicit EncoderAgent(WebCodecsId aId);
     48 
     49  // The following APIs are owner thread only.
     50 
     51  using ConfigurePromise = MozPromise<bool, MediaResult, true /* exclusive */>;
     52  using ReconfigurationPromise = MediaDataEncoder::ReconfigurationPromise;
     53  RefPtr<ConfigurePromise> Configure(const EncoderConfig& aConfig);
     54  RefPtr<ReconfigurationPromise> Reconfigure(
     55      const RefPtr<const EncoderConfigurationChangeList>& aConfigChange);
     56  RefPtr<ShutdownPromise> Shutdown();
     57  using EncodePromise = MediaDataEncoder::EncodePromise;
     58  RefPtr<EncodePromise> Encode(nsTArray<RefPtr<MediaData>>&& aInputs);
     59  // WebCodecs's flush() flushes out all the pending encoded data in the
     60  // encoder. It's called Drain internally.
     61  RefPtr<EncodePromise> Drain();
     62 
     63  const WebCodecsId mId;
     64 
     65 private:
     66  ~EncoderAgent();
     67 
     68  // Push out all the data in the MediaDataEncoder's pipeline.
     69  // TODO: MediaDataEncoder should implement this, instead of asking call site
     70  // to run `Drain` multiple times.
     71  void Dry(MediaDataEncoder::EncodedData&& aPendingOutputs);
     72 
     73  MOZ_DEFINE_ENUM_CLASS_WITH_TOSTRING_AT_CLASS_SCOPE(
     74      State, (Unconfigured, Configuring, Configured, Encoding, Draining,
     75              ShuttingDown, Error));
     76  void SetState(State aState);
     77 
     78  const RefPtr<nsISerialEventTarget> mOwnerThread;
     79  const RefPtr<PEMFactory> mPEMFactory;
     80  RefPtr<MediaDataEncoder> mEncoder;
     81  State mState;
     82 
     83  // Configure
     84  MozPromiseHolder<ConfigurePromise> mConfigurePromise;
     85  using CreateEncoderPromise = PlatformEncoderModule::CreateEncoderPromise;
     86  MozPromiseRequestHolder<CreateEncoderPromise> mCreateRequest;
     87  using InitPromise = MediaDataEncoder::InitPromise;
     88  MozPromiseRequestHolder<InitPromise> mInitRequest;
     89 
     90  // Reconfigure
     91  MozPromiseHolder<ReconfigurationPromise> mReconfigurationPromise;
     92  using ReconfigureEncoderRequest = ReconfigurationPromise;
     93  MozPromiseRequestHolder<ReconfigureEncoderRequest> mReconfigurationRequest;
     94 
     95  // Shutdown
     96  MozPromiseHolder<ShutdownPromise> mShutdownWhileCreationPromise;
     97 
     98  // Encoding
     99  MozPromiseHolder<EncodePromise> mEncodePromise;
    100  MozPromiseRequestHolder<EncodePromise> mEncodeRequest;
    101 
    102  // Drain
    103  MozPromiseRequestHolder<EncodePromise> mDrainRequest;
    104  MozPromiseHolder<EncodePromise> mDrainPromise;
    105 };
    106 
    107 }  // namespace mozilla
    108 
    109 #endif  // DOM_MEDIA_WEBCODECS_EncoderAgent_H