tor-browser

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

media_stream_interface.h (13636B)


      1 /*
      2 *  Copyright 2012 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 // This file contains interfaces for MediaStream, MediaTrack and MediaSource.
     12 // These interfaces are used for implementing MediaStream and MediaTrack as
     13 // defined in http://dev.w3.org/2011/webrtc/editor/webrtc.html#stream-api. These
     14 // interfaces must be used only with PeerConnection.
     15 
     16 #ifndef API_MEDIA_STREAM_INTERFACE_H_
     17 #define API_MEDIA_STREAM_INTERFACE_H_
     18 
     19 #include <stddef.h>
     20 #include <stdint.h>
     21 
     22 #include <optional>
     23 #include <string>
     24 #include <vector>
     25 
     26 #include "api/audio/audio_processing_statistics.h"
     27 #include "api/audio_options.h"
     28 #include "api/ref_count.h"
     29 #include "api/scoped_refptr.h"
     30 #include "api/video/recordable_encoded_frame.h"
     31 #include "api/video/video_frame.h"
     32 #include "api/video/video_sink_interface.h"
     33 #include "api/video/video_source_interface.h"
     34 #include "api/video_track_source_constraints.h"
     35 #include "rtc_base/checks.h"
     36 #include "rtc_base/system/rtc_export.h"
     37 
     38 namespace webrtc {
     39 
     40 // Generic observer interface.
     41 class ObserverInterface {
     42 public:
     43  virtual void OnChanged() = 0;
     44 
     45 protected:
     46  virtual ~ObserverInterface() {}
     47 };
     48 
     49 class NotifierInterface {
     50 public:
     51  virtual void RegisterObserver(ObserverInterface* observer) = 0;
     52  virtual void UnregisterObserver(ObserverInterface* observer) = 0;
     53 
     54  virtual ~NotifierInterface() {}
     55 };
     56 
     57 // Base class for sources. A MediaStreamTrack has an underlying source that
     58 // provides media. A source can be shared by multiple tracks.
     59 class RTC_EXPORT MediaSourceInterface : public RefCountInterface,
     60                                        public NotifierInterface {
     61 public:
     62  enum SourceState { kInitializing, kLive, kEnded, kMuted };
     63 
     64  virtual SourceState state() const = 0;
     65 
     66  virtual bool remote() const = 0;
     67 
     68 protected:
     69  ~MediaSourceInterface() override = default;
     70 };
     71 
     72 // C++ version of MediaStreamTrack.
     73 // See: https://www.w3.org/TR/mediacapture-streams/#mediastreamtrack
     74 class RTC_EXPORT MediaStreamTrackInterface : public RefCountInterface,
     75                                             public NotifierInterface {
     76 public:
     77  enum TrackState {
     78    kLive,
     79    kEnded,
     80  };
     81 
     82  static const char* const kAudioKind;
     83  static const char* const kVideoKind;
     84 
     85  // The kind() method must return kAudioKind only if the object is a
     86  // subclass of AudioTrackInterface, and kVideoKind only if the
     87  // object is a subclass of VideoTrackInterface. It is typically used
     88  // to protect a static_cast<> to the corresponding subclass.
     89  virtual std::string kind() const = 0;
     90 
     91  // Track identifier.
     92  virtual std::string id() const = 0;
     93 
     94  // A disabled track will produce silence (if audio) or black frames (if
     95  // video). Can be disabled and re-enabled.
     96  virtual bool enabled() const = 0;
     97  virtual bool set_enabled(bool enable) = 0;
     98 
     99  // Live or ended. A track will never be live again after becoming ended.
    100  virtual TrackState state() const = 0;
    101 
    102 protected:
    103  ~MediaStreamTrackInterface() override = default;
    104 };
    105 
    106 // VideoTrackSourceInterface is a reference counted source used for
    107 // VideoTracks. The same source can be used by multiple VideoTracks.
    108 // VideoTrackSourceInterface is designed to be invoked on the signaling thread
    109 // except for VideoSourceInterface<VideoFrame> methods that will be
    110 // invoked on the worker thread via a VideoTrack. A custom implementation of a
    111 // source can inherit AdaptedVideoTrackSource instead of directly implementing
    112 // this interface.
    113 class VideoTrackSourceInterface : public MediaSourceInterface,
    114                                  public VideoSourceInterface<VideoFrame> {
    115 public:
    116  struct Stats {
    117    // Original size of captured frame, before video adaptation.
    118    int input_width;
    119    int input_height;
    120  };
    121 
    122  // Indicates that parameters suitable for screencasts should be automatically
    123  // applied to RtpSenders.
    124  // TODO(perkj): Remove these once all known applications have moved to
    125  // explicitly setting suitable parameters for screencasts and don't need this
    126  // implicit behavior.
    127  virtual bool is_screencast() const = 0;
    128 
    129  // Indicates that the encoder should denoise video before encoding it.
    130  // If it is not set, the default configuration is used which is different
    131  // depending on video codec.
    132  // TODO(perkj): Remove this once denoising is done by the source, and not by
    133  // the encoder.
    134  virtual std::optional<bool> needs_denoising() const = 0;
    135 
    136  // Returns false if no stats are available, e.g, for a remote source, or a
    137  // source which has not seen its first frame yet.
    138  //
    139  // Implementation should avoid blocking.
    140  virtual bool GetStats(Stats* stats) = 0;
    141 
    142  // Returns true if encoded output can be enabled in the source.
    143  virtual bool SupportsEncodedOutput() const = 0;
    144 
    145  // Reliably cause a key frame to be generated in encoded output.
    146  // TODO(bugs.webrtc.org/11115): find optimal naming.
    147  virtual void GenerateKeyFrame() = 0;
    148 
    149  // Add an encoded video sink to the source and additionally cause
    150  // a key frame to be generated from the source. The sink will be
    151  // invoked from a decoder queue.
    152  virtual void AddEncodedSink(
    153      VideoSinkInterface<RecordableEncodedFrame>* sink) = 0;
    154 
    155  // Removes an encoded video sink from the source.
    156  virtual void RemoveEncodedSink(
    157      VideoSinkInterface<RecordableEncodedFrame>* sink) = 0;
    158 
    159  // Notify about constraints set on the source. The information eventually gets
    160  // routed to attached sinks via VideoSinkInterface<>::OnConstraintsChanged.
    161  // The call is expected to happen on the network thread.
    162  // TODO(crbug/1255737): make pure virtual once downstream project adapts.
    163  virtual void ProcessConstraints(
    164      const VideoTrackSourceConstraints& /* constraints */) {}
    165 
    166 protected:
    167  ~VideoTrackSourceInterface() override = default;
    168 };
    169 
    170 // VideoTrackInterface is designed to be invoked on the signaling thread except
    171 // for VideoSourceInterface<VideoFrame> methods that must be invoked
    172 // on the worker thread.
    173 // PeerConnectionFactory::CreateVideoTrack can be used for creating a VideoTrack
    174 // that ensures thread safety and that all methods are called on the right
    175 // thread.
    176 class RTC_EXPORT VideoTrackInterface : public MediaStreamTrackInterface,
    177                                       public VideoSourceInterface<VideoFrame> {
    178 public:
    179  // Video track content hint, used to override the source is_screencast
    180  // property.
    181  // See https://crbug.com/653531 and https://w3c.github.io/mst-content-hint.
    182  enum class ContentHint { kNone, kFluid, kDetailed, kText };
    183 
    184  // Register a video sink for this track. Used to connect the track to the
    185  // underlying video engine.
    186  void AddOrUpdateSink(VideoSinkInterface<VideoFrame>* /* sink */,
    187                       const VideoSinkWants& /* wants */) override {}
    188  void RemoveSink(VideoSinkInterface<VideoFrame>* /* sink */) override {}
    189 
    190  virtual VideoTrackSourceInterface* GetSource() const = 0;
    191 
    192  virtual ContentHint content_hint() const;
    193  virtual void set_content_hint(ContentHint /* hint */) {}
    194 
    195 protected:
    196  ~VideoTrackInterface() override = default;
    197 };
    198 
    199 // Interface for receiving audio data from a AudioTrack.
    200 class AudioTrackSinkInterface {
    201 public:
    202  virtual void OnData(const void* /* audio_data */,
    203                      int /* bits_per_sample */,
    204                      int /* sample_rate */,
    205                      size_t /* number_of_channels */,
    206                      size_t /* number_of_frames */) {
    207    RTC_DCHECK_NOTREACHED() << "This method must be overridden, or not used.";
    208  }
    209 
    210  // In this method, `absolute_capture_timestamp_ms`, when available, is
    211  // supposed to deliver the timestamp when this audio frame was originally
    212  // captured. This timestamp MUST be based on the same clock as
    213  // TimeMillis().
    214  virtual void OnData(
    215      const void* audio_data,
    216      int bits_per_sample,
    217      int sample_rate,
    218      size_t number_of_channels,
    219      size_t number_of_frames,
    220      std::optional<int64_t> /* absolute_capture_timestamp_ms */) {
    221    // TODO(bugs.webrtc.org/10739): Deprecate the old OnData and make this one
    222    // pure virtual.
    223    return OnData(audio_data, bits_per_sample, sample_rate, number_of_channels,
    224                  number_of_frames);
    225  }
    226 
    227  // Returns the number of channels encoded by the sink. This can be less than
    228  // the number_of_channels if down-mixing occur. A value of -1 means an unknown
    229  // number.
    230  virtual int NumPreferredChannels() const { return -1; }
    231 
    232 protected:
    233  virtual ~AudioTrackSinkInterface() {}
    234 };
    235 
    236 // AudioSourceInterface is a reference counted source used for AudioTracks.
    237 // The same source can be used by multiple AudioTracks.
    238 class RTC_EXPORT AudioSourceInterface : public MediaSourceInterface {
    239 public:
    240  class AudioObserver {
    241   public:
    242    virtual void OnSetVolume(double volume) = 0;
    243 
    244   protected:
    245    virtual ~AudioObserver() {}
    246  };
    247 
    248  // TODO(deadbeef): Makes all the interfaces pure virtual after they're
    249  // implemented in chromium.
    250 
    251  // Sets the volume of the source. `volume` is in  the range of [0, 10].
    252  // TODO(tommi): This method should be on the track and ideally volume should
    253  // be applied in the track in a way that does not affect clones of the track.
    254  virtual void SetVolume(double /* volume */) {}
    255 
    256  // Registers/unregisters observers to the audio source.
    257  virtual void RegisterAudioObserver(AudioObserver* /* observer */) {}
    258  virtual void UnregisterAudioObserver(AudioObserver* /* observer */) {}
    259 
    260  // TODO(tommi): Make pure virtual.
    261  virtual void AddSink(AudioTrackSinkInterface* /* sink */) {}
    262  virtual void RemoveSink(AudioTrackSinkInterface* /* sink */) {}
    263 
    264  // Returns options for the AudioSource.
    265  // (for some of the settings this approach is broken, e.g. setting
    266  // audio network adaptation on the source is the wrong layer of abstraction).
    267  virtual const AudioOptions options() const;
    268 };
    269 
    270 // Interface of the audio processor used by the audio track to collect
    271 // statistics.
    272 class AudioProcessorInterface : public RefCountInterface {
    273 public:
    274  struct AudioProcessorStatistics {
    275    bool typing_noise_detected = false;
    276    AudioProcessingStats apm_statistics;
    277  };
    278 
    279  // Get audio processor statistics. The `has_remote_tracks` argument should be
    280  // set if there are active remote tracks (this would usually be true during
    281  // a call). If there are no remote tracks some of the stats will not be set by
    282  // the AudioProcessor, because they only make sense if there is at least one
    283  // remote track.
    284  virtual AudioProcessorStatistics GetStats(bool has_remote_tracks) = 0;
    285 
    286 protected:
    287  ~AudioProcessorInterface() override = default;
    288 };
    289 
    290 class RTC_EXPORT AudioTrackInterface : public MediaStreamTrackInterface {
    291 public:
    292  // TODO(deadbeef): Figure out if the following interface should be const or
    293  // not.
    294  virtual AudioSourceInterface* GetSource() const = 0;
    295 
    296  // Add/Remove a sink that will receive the audio data from the track.
    297  virtual void AddSink(AudioTrackSinkInterface* sink) = 0;
    298  virtual void RemoveSink(AudioTrackSinkInterface* sink) = 0;
    299 
    300  // Get the signal level from the audio track.
    301  // Return true on success, otherwise false.
    302  // TODO(deadbeef): Change the interface to int GetSignalLevel() and pure
    303  // virtual after it's implemented in chromium.
    304  virtual bool GetSignalLevel(int* level);
    305 
    306  // Get the audio processor used by the audio track. Return null if the track
    307  // does not have any processor.
    308  // TODO(deadbeef): Make the interface pure virtual.
    309  virtual scoped_refptr<AudioProcessorInterface> GetAudioProcessor();
    310 
    311 protected:
    312  ~AudioTrackInterface() override = default;
    313 };
    314 
    315 typedef std::vector<scoped_refptr<AudioTrackInterface> > AudioTrackVector;
    316 typedef std::vector<scoped_refptr<VideoTrackInterface> > VideoTrackVector;
    317 
    318 // C++ version of https://www.w3.org/TR/mediacapture-streams/#mediastream.
    319 //
    320 // A major difference is that remote audio/video tracks (received by a
    321 // PeerConnection/RtpReceiver) are not synchronized simply by adding them to
    322 // the same stream; a session description with the correct "a=msid" attributes
    323 // must be pushed down.
    324 //
    325 // Thus, this interface acts as simply a container for tracks.
    326 class MediaStreamInterface : public RefCountInterface,
    327                             public NotifierInterface {
    328 public:
    329  virtual std::string id() const = 0;
    330 
    331  virtual AudioTrackVector GetAudioTracks() = 0;
    332  virtual VideoTrackVector GetVideoTracks() = 0;
    333  virtual scoped_refptr<AudioTrackInterface> FindAudioTrack(
    334      const std::string& track_id) = 0;
    335  virtual scoped_refptr<VideoTrackInterface> FindVideoTrack(
    336      const std::string& track_id) = 0;
    337 
    338  // Takes ownership of added tracks.
    339  // Note: Default implementations are for avoiding link time errors in
    340  // implementations that mock this API.
    341  // TODO(bugs.webrtc.org/13980): Remove default implementations.
    342  virtual bool AddTrack(scoped_refptr<AudioTrackInterface> /* track */) {
    343    RTC_CHECK_NOTREACHED();
    344  }
    345  virtual bool AddTrack(scoped_refptr<VideoTrackInterface> /* track */) {
    346    RTC_CHECK_NOTREACHED();
    347  }
    348  virtual bool RemoveTrack(scoped_refptr<AudioTrackInterface> /* track */) {
    349    RTC_CHECK_NOTREACHED();
    350  }
    351  virtual bool RemoveTrack(scoped_refptr<VideoTrackInterface> /* track */) {
    352    RTC_CHECK_NOTREACHED();
    353  }
    354 
    355 protected:
    356  ~MediaStreamInterface() override = default;
    357 };
    358 
    359 }  // namespace webrtc
    360 
    361 #endif  // API_MEDIA_STREAM_INTERFACE_H_