summaryrefslogtreecommitdiffstats
path: root/dom/media/webrtc/libwebrtcglue/VideoConduit.h
blob: 0528d0650176df8d966cbdb2659ec61b7d34950d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef VIDEO_SESSION_H_
#define VIDEO_SESSION_H_

#include "mozilla/Atomics.h"
#include "mozilla/Attributes.h"
#include "mozilla/ReentrantMonitor.h"
#include "mozilla/SharedThreadPool.h"
#include "mozilla/UniquePtr.h"
#include "nsITimer.h"

#include "MediaConduitInterface.h"
#include "common/MediaEngineWrapper.h"
#include "RunningStat.h"
#include "RtpPacketQueue.h"
#include "transport/runnable_utils.h"

// conflicts with #include of scoped_ptr.h
#undef FF
// Video Engine Includes
#include "api/video_codecs/video_encoder_factory.h"
#include "webrtc/call/call.h"
#include "webrtc/common_types.h"
#ifdef FF
#  undef FF  // Avoid name collision between scoped_ptr.h and nsCRTGlue.h.
#endif
#include "webrtc/api/video_codecs/video_decoder.h"
#include "webrtc/api/video_codecs/video_encoder.h"
#include "webrtc/api/video_codecs/sdp_video_format.h"
#include "webrtc/common_video/include/i420_buffer_pool.h"
#include "webrtc/media/base/videosinkinterface.h"
#include "webrtc/media/base/videoadapter.h"
#include "webrtc/media/base/videobroadcaster.h"
#include <functional>
#include <memory>
/** This file hosts several structures identifying different aspects
 * of a RTP Session.
 */

namespace mozilla {

// Convert (SI) kilobits/sec to (SI) bits/sec
#define KBPS(kbps) kbps * 1000

const int kViEMinCodecBitrate_bps = KBPS(30);
const unsigned int kVideoMtu = 1200;
const int kQpMax = 56;

template <typename T>
T MinIgnoreZero(const T& a, const T& b);

class VideoStreamFactory;
class WebrtcAudioConduit;

// Interface of external video encoder for WebRTC.
class WebrtcVideoEncoder : public VideoEncoder, public webrtc::VideoEncoder {};

// Interface of external video decoder for WebRTC.
class WebrtcVideoDecoder : public VideoDecoder, public webrtc::VideoDecoder {};

/**
 * Concrete class for Video session. Hooks up
 *  - media-source and target to external transport
 */
class WebrtcVideoConduit
    : public VideoSessionConduit,
      public webrtc::RtcpEventObserver,
      public webrtc::RtpPacketSinkInterface,
      public webrtc::Transport,
      public webrtc::VideoEncoderFactory,
      public rtc::VideoSinkInterface<webrtc::VideoFrame>,
      public rtc::VideoSourceInterface<webrtc::VideoFrame> {
 public:
  // VoiceEngine defined constant for Payload Name Size.
  static const unsigned int CODEC_PLNAME_SIZE;

  // Returns true when both encoder and decoder are HW accelerated.
  static bool HasH264Hardware();

  MediaConduitErrorCode SetLocalRTPExtensions(
      MediaSessionConduitLocalDirection aDirection,
      const RtpExtList& aExtensions) override;

  /**
   * Function to attach Renderer end-point for the Media-Video conduit.
   * @param aRenderer : Reference to the concrete mozilla Video renderer
   * implementation Note: Multiple invocations of this API shall remove an
   * existing renderer and attaches the new to the Conduit.
   */
  MediaConduitErrorCode AttachRenderer(
      RefPtr<mozilla::VideoRenderer> aVideoRenderer) override;
  void DetachRenderer() override;

  /**
   * APIs used by the registered external transport to this Conduit to
   * feed in received RTP Frames to the VideoEngine for decoding
   */
  MediaConduitErrorCode ReceivedRTPPacket(const void* data, int len,
                                          webrtc::RTPHeader& header) override;

  /**
   * APIs used by the registered external transport to this Conduit to
   * feed in received RTCP Frames to the VideoEngine for decoding
   */
  MediaConduitErrorCode ReceivedRTCPPacket(const void* data, int len) override;
  Maybe<DOMHighResTimeStamp> LastRtcpReceived() const override;
  DOMHighResTimeStamp GetNow() const override { return mCall->GetNow(); }

  MediaConduitErrorCode StopTransmitting() override;
  MediaConduitErrorCode StartTransmitting() override;
  MediaConduitErrorCode StopReceiving() override;
  MediaConduitErrorCode StartReceiving() override;

  MediaConduitErrorCode StopTransmittingLocked();
  MediaConduitErrorCode StartTransmittingLocked();
  MediaConduitErrorCode StopReceivingLocked();
  MediaConduitErrorCode StartReceivingLocked();

  /**
   * Function to configure sending codec mode for different content
   */
  MediaConduitErrorCode ConfigureCodecMode(webrtc::VideoCodecMode) override;

  /**
   * Function to configure send codec for the video session
   * @param sendSessionConfig: CodecConfiguration
   * @result: On Success, the video engine is configured with passed in codec
   *          for send
   *          On failure, video engine transmit functionality is disabled.
   * NOTE: This API can be invoked multiple time. Invoking this API may involve
   * restarting transmission sub-system on the engine.
   */
  MediaConduitErrorCode ConfigureSendMediaCodec(
      const VideoCodecConfig* codecInfo,
      const RtpRtcpConfig& aRtpRtcpConfig) override;

  /**
   * Function to configure list of receive codecs for the video session
   * @param sendSessionConfig: CodecConfiguration
   * @result: On Success, the video engine is configured with passed in codec
   *          for send
   *          Also the playout is enabled.
   *          On failure, video engine transmit functionality is disabled.
   * NOTE: This API can be invoked multiple time. Invoking this API may involve
   * restarting transmission sub-system on the engine.
   */
  MediaConduitErrorCode ConfigureRecvMediaCodecs(
      const std::vector<UniquePtr<VideoCodecConfig>>& codecConfigList,
      const RtpRtcpConfig& aRtpRtcpConfig) override;

  /**
   * Register Transport for this Conduit. RTP and RTCP frames from the
   * VideoEngine shall be passed to the registered transport for transporting
   * externally.
   */
  MediaConduitErrorCode SetTransmitterTransport(
      RefPtr<TransportInterface> aTransport) override;

  MediaConduitErrorCode SetReceiverTransport(
      RefPtr<TransportInterface> aTransport) override;

  /**
   * Function to select and change the encoding resolution based on incoming
   * frame size and current available bandwidth.
   * @param width, height: dimensions of the frame
   * @param frame: optional frame to submit for encoding after reconfig
   */
  void SelectSendResolution(unsigned short width, unsigned short height);

  /**
   * Function to deliver a capture video frame for encoding and transport.
   * If the frame's timestamp is 0, it will be automatically generated.
   *
   * NOTE: ConfigureSendMediaCodec() must be called before this function can
   *       be invoked. This ensures the inserted video-frames can be
   *       transmitted by the conduit.
   */
  MediaConduitErrorCode SendVideoFrame(
      const webrtc::VideoFrame& frame) override;

  /**
   * webrtc::Transport method implementation
   * ---------------------------------------
   * Webrtc transport implementation to send and receive RTP packet.
   * VideoConduit registers itself as ExternalTransport to the VideoStream
   */
  bool SendRtp(const uint8_t* packet, size_t length,
               const webrtc::PacketOptions& options) override;

  /**
   * webrtc::Transport method implementation
   * ---------------------------------------
   * Webrtc transport implementation to send and receive RTCP packet.
   * VideoConduit registers itself as ExternalTransport to the VideoEngine
   */
  bool SendRtcp(const uint8_t* packet, size_t length) override;

  /*
   * webrtc:VideoSinkInterface implementation
   * -------------------------------
   */
  void OnFrame(const webrtc::VideoFrame& frame) override;

  /*
   * webrtc:VideoSourceInterface implementation
   * -------------------------------
   */
  void AddOrUpdateSink(rtc::VideoSinkInterface<webrtc::VideoFrame>* sink,
                       const rtc::VideoSinkWants& wants) override;
  void AddOrUpdateSinkNotLocked(
      rtc::VideoSinkInterface<webrtc::VideoFrame>* sink,
      const rtc::VideoSinkWants& wants);

  void RemoveSink(rtc::VideoSinkInterface<webrtc::VideoFrame>* sink) override;
  void RemoveSinkNotLocked(rtc::VideoSinkInterface<webrtc::VideoFrame>* sink);

  void OnSinkWantsChanged(const rtc::VideoSinkWants& wants);

  uint64_t CodecPluginID() override;

  void SetPCHandle(const std::string& aPCHandle) override {
    MOZ_ASSERT(NS_IsMainThread());
    mPCHandle = aPCHandle;
  }

  void DeleteStreams() override;

  bool Denoising() const { return mDenoising; }

  uint8_t SpatialLayers() const { return mSpatialLayers; }

  uint8_t TemporalLayers() const { return mTemporalLayers; }

  webrtc::VideoCodecMode CodecMode() const {
    MOZ_ASSERT(NS_IsMainThread());
    return mCodecMode;
  }

  WebrtcVideoConduit(RefPtr<WebRtcCallWrapper> aCall,
                     nsCOMPtr<nsISerialEventTarget> aStsThread);
  virtual ~WebrtcVideoConduit();

  MediaConduitErrorCode InitMain();
  virtual MediaConduitErrorCode Init();

  std::vector<uint32_t> GetLocalSSRCs() override;
  bool SetLocalSSRCs(const std::vector<uint32_t>& ssrcs,
                     const std::vector<uint32_t>& rtxSsrcs) override;
  // Can be called from any thread
  bool GetRemoteSSRC(uint32_t* ssrc) override;
  bool SetRemoteSSRC(uint32_t ssrc, uint32_t rtxSsrc) override;
  bool UnsetRemoteSSRC(uint32_t ssrc) override;
  bool SetLocalCNAME(const char* cname) override;
  bool SetLocalMID(const std::string& mid) override;

  void SetSyncGroup(const std::string& group) override;

  bool SetRemoteSSRCLocked(uint32_t ssrc, uint32_t rtxSsrc);

  bool GetSendPacketTypeStats(
      webrtc::RtcpPacketTypeCounter* aPacketCounts) override;

  bool GetRecvPacketTypeStats(
      webrtc::RtcpPacketTypeCounter* aPacketCounts) override;

  void PollStats();
  void UpdateVideoStatsTimer();
  bool GetVideoEncoderStats(double* framerateMean, double* framerateStdDev,
                            double* bitrateMean, double* bitrateStdDev,
                            uint32_t* droppedFrames, uint32_t* framesEncoded,
                            Maybe<uint64_t>* qpSum) override;
  bool GetVideoDecoderStats(double* framerateMean, double* framerateStdDev,
                            double* bitrateMean, double* bitrateStdDev,
                            uint32_t* discardedPackets,
                            uint32_t* framesDecoded) override;
  bool GetRTPReceiverStats(unsigned int* jitterMs,
                           unsigned int* cumulativeLost) override;
  bool GetRTCPReceiverReport(uint32_t* jitterMs, uint32_t* packetsReceived,
                             uint64_t* bytesReceived, uint32_t* cumulativeLost,
                             Maybe<double>* aOutRttSec) override;
  bool GetRTCPSenderReport(unsigned int* packetsSent, uint64_t* bytesSent,
                           DOMHighResTimeStamp* aRemoteTimestamp) override;

  Maybe<mozilla::dom::RTCBandwidthEstimationInternal> GetBandwidthEstimation()
      override;

  void GetRtpSources(nsTArray<dom::RTCRtpSourceEntry>& outSources) override;
  bool AddFrameHistory(dom::Sequence<dom::RTCVideoFrameHistoryInternal>*
                           outHistories) const override;

  uint64_t MozVideoLatencyAvg();

  void DisableSsrcChanges() override {
    ASSERT_ON_THREAD(mStsThread);
    mAllowSsrcChange = false;
  }

  /**
   * Callback from libwebrtc with the parsed packet for synchronization
   * source tracking. STS thread only.
   */
  void OnRtpPacket(const webrtc::RtpPacketReceived& packet) override;

  Maybe<RefPtr<VideoSessionConduit>> AsVideoSessionConduit() override {
    return Some(RefPtr<VideoSessionConduit>(this));
  }

  void RecordTelemetry() const override {
    ASSERT_ON_THREAD(mStsThread);
    mSendStreamStats.RecordTelemetry();
    mRecvStreamStats.RecordTelemetry();
  }

  void OnRtcpBye() override;

  void OnRtcpTimeout() override;

  void SetRtcpEventObserver(mozilla::RtcpEventObserver* observer) override;

 private:
  // Don't allow copying/assigning.
  WebrtcVideoConduit(const WebrtcVideoConduit&) = delete;
  void operator=(const WebrtcVideoConduit&) = delete;

  /**
   * Statistics for the Call associated with this VideoConduit.
   * Single threaded.
   */
  class CallStatistics {
   public:
    explicit CallStatistics(nsCOMPtr<nsISerialEventTarget> aStatsThread)
        : mStatsThread(aStatsThread) {}
    void Update(const webrtc::Call::Stats& aStats);
    Maybe<mozilla::dom::RTCBandwidthEstimationInternal> Stats() const;
    Maybe<DOMHighResTimeStamp> RttSec() const;

   protected:
    const nsCOMPtr<nsISerialEventTarget> mStatsThread;

   private:
    Maybe<webrtc::Call::Stats> mStats = Nothing();
    Maybe<DOMHighResTimeStamp> mRttSec = Nothing();
  };

  /**
   * Shared statistics for receive and transmit video streams.
   * Single threaded.
   */
  class StreamStatistics {
   public:
    explicit StreamStatistics(nsCOMPtr<nsISerialEventTarget> aStatsThread)
        : mStatsThread(aStatsThread) {}
    void Update(const double aFrameRate, const double aBitrate,
                const webrtc::RtcpPacketTypeCounter& aPacketCounts);
    /**
     * Returns gathered stream statistics
     * @param aOutFrMean: mean framerate
     * @param aOutFrStdDev: standard deviation of framerate
     * @param aOutBrMean: mean bitrate
     * @param aOutBrStdDev: standard deviation of bitrate
     */
    bool GetVideoStreamStats(double& aOutFrMean, double& aOutFrStdDev,
                             double& aOutBrMean, double& aOutBrStdDev) const;

    /**
     * Accumulates video quality telemetry
     */
    void RecordTelemetry() const;
    const webrtc::RtcpPacketTypeCounter& PacketCounts() const;
    bool Active() const;
    void SetActive(bool aActive);
    virtual bool IsSend() const { return false; };

   protected:
    const nsCOMPtr<nsISerialEventTarget> mStatsThread;

   private:
    bool mActive = false;
    RunningStat mFrameRate;
    RunningStat mBitRate;
    webrtc::RtcpPacketTypeCounter mPacketCounts;
  };

  /**
   * Statistics for sending streams. Single threaded.
   */
  class SendStreamStatistics : public StreamStatistics {
   public:
    explicit SendStreamStatistics(nsCOMPtr<nsISerialEventTarget> aStatsThread)
        : StreamStatistics(
              std::forward<nsCOMPtr<nsISerialEventTarget>>(aStatsThread)) {}
    /**
     * Returns the calculate number of dropped frames
     */
    uint32_t DroppedFrames() const;
    /**
     * Returns the number of frames that have been encoded so far
     */
    uint32_t FramesEncoded() const;
    void Update(const webrtc::VideoSendStream::Stats& aStats,
                uint32_t aConfiguredSsrc);
    /**
     * Call once for every frame delivered for encoding
     */
    void FrameDeliveredToEncoder();

    bool SsrcFound() const;
    uint32_t JitterMs() const;
    uint32_t PacketsLost() const;
    uint64_t BytesReceived() const;
    uint32_t PacketsReceived() const;
    Maybe<uint64_t> QpSum() const;
    bool IsSend() const override { return true; };

   private:
    uint32_t mDroppedFrames = 0;
    uint32_t mFramesEncoded = 0;
    int32_t mFramesDeliveredToEncoder = 0;

    bool mSsrcFound = false;
    uint32_t mJitterMs = 0;
    uint32_t mPacketsLost = 0;
    uint64_t mBytesReceived = 0;
    uint32_t mPacketsReceived = 0;
    Maybe<uint64_t> mQpSum;
  };

  /**
   * Statistics for receiving streams. Single threaded.
   */
  class ReceiveStreamStatistics : public StreamStatistics {
   public:
    explicit ReceiveStreamStatistics(
        nsCOMPtr<nsISerialEventTarget> aStatsThread)
        : StreamStatistics(
              std::forward<nsCOMPtr<nsISerialEventTarget>>(aStatsThread)) {}
    uint32_t BytesSent() const;
    /**
     * Returns the number of discarded packets
     */
    uint32_t DiscardedPackets() const;
    /**
     * Returns the number of frames decoded
     */
    uint32_t FramesDecoded() const;
    uint32_t JitterMs() const;
    uint32_t PacketsLost() const;
    uint32_t PacketsSent() const;
    uint32_t Ssrc() const;
    DOMHighResTimeStamp RemoteTimestamp() const;
    void Update(const webrtc::VideoReceiveStream::Stats& aStats);

   private:
    uint32_t mBytesSent = 0;
    uint32_t mDiscardedPackets = 0;
    uint32_t mFramesDecoded = 0;
    uint32_t mJitterMs = 0;
    uint32_t mPacketsLost = 0;
    uint32_t mPacketsSent = 0;
    uint32_t mSsrc = 0;
    DOMHighResTimeStamp mRemoteTimestamp = 0;
  };

  // Utility function to dump recv codec database
  void DumpCodecDB() const;

  // Video Latency Test averaging filter
  void VideoLatencyUpdate(uint64_t new_sample);

  MediaConduitErrorCode CreateSendStream();
  void DeleteSendStream();
  MediaConduitErrorCode CreateRecvStream();
  void DeleteRecvStream();

  std::unique_ptr<webrtc::VideoDecoder> CreateDecoder(
      webrtc::VideoCodecType aType);
  std::unique_ptr<webrtc::VideoEncoder> CreateEncoder(
      webrtc::VideoCodecType aType);

  // webrtc::VideoEncoderFactory
  std::vector<webrtc::SdpVideoFormat> GetSupportedFormats() const override;

  CodecInfo QueryVideoEncoder(
      const webrtc::SdpVideoFormat& format) const override;

  std::unique_ptr<webrtc::VideoEncoder> CreateVideoEncoder(
      const webrtc::SdpVideoFormat& format) override;

  MediaConduitErrorCode DeliverPacket(const void* data, int len) override;

  bool RequiresNewSendStream(const VideoCodecConfig& newConfig) const;

  mutable mozilla::ReentrantMonitor mTransportMonitor;

  // Accessed on any thread under mTransportMonitor.
  RefPtr<TransportInterface> mTransmitterTransport;

  // Accessed on any thread under mTransportMonitor.
  RefPtr<TransportInterface> mReceiverTransport;

  // Accessed on any thread under mTransportMonitor.
  RefPtr<mozilla::VideoRenderer> mRenderer;

  // Accessed on any thread under mTransportMonitor.
  unsigned short mReceivingWidth = 0;

  // Accessed on any thread under mTransportMonitor.
  unsigned short mReceivingHeight = 0;

  // Socket transport service thread that runs stats queries against us. Any
  // thread.
  const nsCOMPtr<nsISerialEventTarget> mStsThread;

  Mutex mMutex;

  // Adapter handling resolution constraints from signaling and sinks.
  // Written only on main thread. Guarded by mMutex, except for reads on main.
  UniquePtr<cricket::VideoAdapter> mVideoAdapter;

  // Our own record of the sinks added to mVideoBroadcaster so we can support
  // dispatching updates to sinks from off-main-thread. Main thread only.
  AutoTArray<rtc::VideoSinkInterface<webrtc::VideoFrame>*, 1> mRegisteredSinks;

  // Broadcaster that distributes our frames to all registered sinks.
  // Sinks can only be added, updated and removed on main thread.
  // Frames can be passed in on any thread.
  rtc::VideoBroadcaster mVideoBroadcaster;

  // Buffer pool used for scaling frames.
  // Accessed on the frame-feeding thread only.
  webrtc::I420BufferPool mBufferPool;

  // Engine state we are concerned with. Written on main thread and read
  // anywhere.
  mozilla::Atomic<bool>
      mEngineTransmitting;  // If true ==> Transmit Subsystem is up and running
  mozilla::Atomic<bool>
      mEngineReceiving;  // if true ==> Receive Subsystem up and running

  // Local database of currently applied receive codecs. Main thread only.
  nsTArray<UniquePtr<VideoCodecConfig>> mRecvCodecList;

  // Written only on main thread. Guarded by mMutex, except for reads on main.
  UniquePtr<VideoCodecConfig> mCurSendCodecConfig;

  bool mUpdateResolution = false;
  int mSinkWantsPixelCount = std::numeric_limits<int>::max();

  // Bookkeeping of send stream stats. Sts thread only.
  SendStreamStatistics mSendStreamStats;

  // Bookkeeping of send stream stats. Sts thread only.
  ReceiveStreamStatistics mRecvStreamStats;

  // Bookkeeping of call stats. Sts thread only.
  CallStatistics mCallStats;

  // Must call webrtc::Call::DestroyVideoReceive/SendStream to delete this.
  // Written only on main thread. Guarded by mMutex, except for reads on main.
  webrtc::VideoReceiveStream* mRecvStream = nullptr;

  // Must call webrtc::Call::DestroyVideoReceive/SendStream to delete this.
  // Written only on main thread. Guarded by mMutex, except for reads on main.
  webrtc::VideoSendStream* mSendStream = nullptr;

  // Written on the frame feeding thread.
  // Guarded by mMutex, except for reads on the frame feeding thread.
  unsigned short mLastWidth = 0;

  // Written on the frame feeding thread.
  // Guarded by mMutex, except for reads on the frame feeding thread.
  unsigned short mLastHeight = 0;

  // Accessed under mMutex.
  unsigned int mSendingFramerate;

  // Written on main thread at creation,
  // then written or read on any thread under mTransportMonitor.
  bool mVideoLatencyTestEnable = false;

  // Accessed from any thread under mTransportMonitor.
  uint64_t mVideoLatencyAvg = 0;

  // All in bps.
  // All written on main thread and guarded by mMutex, except for reads on main.
  int mMinBitrate = 0;
  int mStartBitrate = 0;
  int mPrefMaxBitrate = 0;
  int mNegotiatedMaxBitrate = 0;
  int mMinBitrateEstimate = 0;

  // Set to true to force denoising on.
  // Written at creation, then read anywhere.
  bool mDenoising = false;

  // Set to true to ignore sink wants (scaling due to bwe and cpu usage).
  // Written at creation, then read anywhere.
  bool mLockScaling = false;

  // Written at creation, then read anywhere.
  uint8_t mSpatialLayers = 1;

  // Written at creation, then read anywhere.
  uint8_t mTemporalLayers = 1;

  static const unsigned int sAlphaNum = 7;
  static const unsigned int sAlphaDen = 8;
  static const unsigned int sRoundingPadding = 1024;

  // Main thread only.
  RefPtr<WebrtcAudioConduit> mSyncedTo;

  // Main thread only.
  webrtc::VideoCodecMode mActiveCodecMode;
  webrtc::VideoCodecMode mCodecMode;

  // WEBRTC.ORG Call API
  // Const so can be accessed on any thread. Most methods are called on
  // main thread, though Receiver() is called on STS. This seems fine.
  const RefPtr<WebRtcCallWrapper> mCall;

  // Written only on main thread. Guarded by mMutex, except for reads on main.
  webrtc::VideoSendStream::Config mSendStreamConfig;

  // Main thread only.
  webrtc::VideoEncoderConfig mEncoderConfig;

  // Written only on main thread. Guarded by mMutex, except for reads on main.
  // Calls can happen on any thread.
  RefPtr<rtc::RefCountedObject<VideoStreamFactory>> mVideoStreamFactory;

  // Main thread only.
  webrtc::VideoReceiveStream::Config mRecvStreamConfig;

  // Are SSRC changes without signaling allowed or not.
  // Accessed only on mStsThread.
  bool mAllowSsrcChange = true;

  // Accessed only on mStsThread.
  bool mWaitingForInitialSsrc = true;

  // Accessed during configuration/signaling (main),
  // and when receiving packets (sts).
  Atomic<uint32_t> mRecvSSRC;  // this can change during a stream!
  // Accessed from both the STS and main thread for a variety of things
  // Set when receiving packets
  Atomic<uint32_t> mRemoteSSRC;  // this can change during a stream!

  // Accessed only on mStsThread.
  RtpPacketQueue mRtpPacketQueue;

  // The lifetime of these codecs are maintained by the VideoConduit instance.
  // They are passed to the webrtc::VideoSendStream or VideoReceiveStream,
  // on construction.
  std::unique_ptr<webrtc::VideoEncoder> mEncoder;  // only one encoder for now
  std::vector<std::unique_ptr<webrtc::VideoDecoder>> mDecoders;
  // Main thread only
  uint64_t mSendCodecPluginID = 0;
  // Main thread only
  uint64_t mRecvCodecPluginID = 0;

  // Timer that updates video stats periodically. Main thread only.
  nsCOMPtr<nsITimer> mVideoStatsTimer;
  // True if mVideoStatsTimer is running. Main thread only.
  bool mVideoStatsTimerActive = false;

  // Main thread only
  std::string mPCHandle;

  // Accessed only on mStsThread
  Maybe<DOMHighResTimeStamp> mLastRtcpReceived;

  // Accessed only on main thread.
  mozilla::RtcpEventObserver* mRtcpEventObserver = nullptr;

  // Accessed from main and mStsThread. Uses locks internally.
  RefPtr<RtpSourceObserver> mRtpSourceObserver;
  // Tracking the attributes of received frames over time
  // Protected by mTransportMonitor
  dom::RTCVideoFrameHistoryInternal mReceivedFrameHistory;
};
}  // namespace mozilla

#endif