tor-browser

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

video_encoder_factory.h (5066B)


      1 /*
      2 *  Copyright (c) 2017 The WebRTC project authors. All Rights Reserved.
      3 *
      4 *  Use of this source code is governed by a BSD-style license
      5 *  that can be found in the LICENSE file in the root of the source
      6 *  tree. An additional intellectual property rights grant can be found
      7 *  in the file PATENTS.  All contributing project authors may
      8 *  be found in the AUTHORS file in the root of the source tree.
      9 */
     10 
     11 #ifndef API_VIDEO_CODECS_VIDEO_ENCODER_FACTORY_H_
     12 #define API_VIDEO_CODECS_VIDEO_ENCODER_FACTORY_H_
     13 
     14 #include <memory>
     15 #include <optional>
     16 #include <string>
     17 #include <vector>
     18 
     19 #include "api/environment/environment.h"
     20 #include "api/units/data_rate.h"
     21 #include "api/video/render_resolution.h"
     22 #include "api/video_codecs/sdp_video_format.h"
     23 #include "api/video_codecs/video_encoder.h"
     24 
     25 namespace webrtc {
     26 
     27 // A factory that creates VideoEncoders.
     28 // NOTE: This class is still under development and may change without notice.
     29 class VideoEncoderFactory {
     30 public:
     31  struct CodecSupport {
     32    bool is_supported = false;
     33    bool is_power_efficient = false;
     34  };
     35 
     36  // An injectable class that is continuously updated with encoding conditions
     37  // and selects the best encoder given those conditions. An implementation is
     38  // typically stateful to avoid toggling between different encoders, which is
     39  // costly due to recreation of objects, a new codec will always start with a
     40  // key-frame.
     41  class EncoderSelectorInterface {
     42   public:
     43    virtual ~EncoderSelectorInterface() {}
     44 
     45    // Informs the encoder selector about which encoder that is currently being
     46    // used.
     47    virtual void OnCurrentEncoder(const SdpVideoFormat& format) = 0;
     48 
     49    // Called every time the available bitrate is updated. Should return a
     50    // non-empty if an encoder switch should be performed.
     51    virtual std::optional<SdpVideoFormat> OnAvailableBitrate(
     52        const DataRate& rate) = 0;
     53 
     54    // Called every time the encoder input resolution change. Should return a
     55    // non-empty if an encoder switch should be performed.
     56    virtual std::optional<SdpVideoFormat> OnResolutionChange(
     57        const RenderResolution& /* resolution */) {
     58      return std::nullopt;
     59    }
     60 
     61    // Called if the currently used encoder reports itself as broken. Should
     62    // return a non-empty if an encoder switch should be performed.
     63    virtual std::optional<SdpVideoFormat> OnEncoderBroken() = 0;
     64  };
     65 
     66  // Returns a list of supported video formats in order of preference, to use
     67  // for signaling etc.
     68  virtual std::vector<SdpVideoFormat> GetSupportedFormats() const = 0;
     69 
     70  // Returns a list of supported video formats in order of preference, that can
     71  // also be tagged with additional information to allow the VideoEncoderFactory
     72  // to separate between different implementations when CreateVideoEncoder is
     73  // called.
     74  virtual std::vector<SdpVideoFormat> GetImplementations() const {
     75    return GetSupportedFormats();
     76  }
     77 
     78  // Query whether the specifed format is supported or not and if it will be
     79  // power efficient, which is currently interpreted as if there is support for
     80  // hardware acceleration.
     81  // See https://w3c.github.io/webrtc-svc/#scalabilitymodes* for a specification
     82  // of valid values for `scalability_mode`.
     83  // NOTE: QueryCodecSupport is currently an experimental feature that is
     84  // subject to change without notice.
     85  virtual CodecSupport QueryCodecSupport(
     86      const SdpVideoFormat& format,
     87      std::optional<std::string> scalability_mode) const {
     88    // Default implementation, query for supported formats and check if the
     89    // specified format is supported. Returns false if scalability_mode is
     90    // specified.
     91    CodecSupport codec_support;
     92    if (!scalability_mode) {
     93      codec_support.is_supported = format.IsCodecInList(GetSupportedFormats());
     94    }
     95    return codec_support;
     96  }
     97 
     98  // Creates a VideoEncoder for the specified format.
     99  virtual std::unique_ptr<VideoEncoder> Create(
    100      const Environment& env,
    101      const SdpVideoFormat& format) = 0;
    102 
    103  // This method creates a EncoderSelector to use for a VideoSendStream.
    104  // (and hence should probably been called CreateEncoderSelector()).
    105  //
    106  // Note: This method is unsuitable if encoding several streams that
    107  // are using same VideoEncoderFactory (either by several streams in one
    108  // PeerConnection or streams with different PeerConnection but same
    109  // PeerConnectionFactory). This is due to the fact that the method is not
    110  // given any stream identifier, nor is the EncoderSelectorInterface given any
    111  // stream identifiers, i.e one does not know which stream is being encoded
    112  // with help of the selector.
    113  //
    114  // In such scenario, the `RtpSenderInterface::SetEncoderSelector` is
    115  // recommended.
    116  //
    117  // TODO(bugs.webrtc.org:14122): Deprecate and remove in favor of
    118  // `RtpSenderInterface::SetEncoderSelector`.
    119  virtual std::unique_ptr<EncoderSelectorInterface> GetEncoderSelector() const {
    120    return nullptr;
    121  }
    122 
    123  virtual ~VideoEncoderFactory() {}
    124 };
    125 
    126 }  // namespace webrtc
    127 
    128 #endif  // API_VIDEO_CODECS_VIDEO_ENCODER_FACTORY_H_