1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
3  * You can obtain one at http://mozilla.org/MPL/2.0/. */
4 
5 #ifndef VIDEO_SESSION_H_
6 #define VIDEO_SESSION_H_
7 
8 #include "mozilla/Atomics.h"
9 #include "mozilla/Attributes.h"
10 #include "mozilla/ReentrantMonitor.h"
11 #include "mozilla/SharedThreadPool.h"
12 #include "mozilla/UniquePtr.h"
13 #include "nsITimer.h"
14 
15 #include "MediaConduitInterface.h"
16 #include "common/MediaEngineWrapper.h"
17 #include "RunningStat.h"
18 #include "RtpPacketQueue.h"
19 #include "transport/runnable_utils.h"
20 
21 // conflicts with #include of scoped_ptr.h
22 #undef FF
23 // Video Engine Includes
24 #include "api/video_codecs/video_encoder_factory.h"
25 #include "webrtc/call/call.h"
26 #include "webrtc/common_types.h"
27 #ifdef FF
28 #  undef FF  // Avoid name collision between scoped_ptr.h and nsCRTGlue.h.
29 #endif
30 #include "webrtc/api/video_codecs/video_decoder.h"
31 #include "webrtc/api/video_codecs/video_encoder.h"
32 #include "webrtc/api/video_codecs/sdp_video_format.h"
33 #include "webrtc/common_video/include/i420_buffer_pool.h"
34 #include "webrtc/media/base/videosinkinterface.h"
35 #include "webrtc/media/base/videoadapter.h"
36 #include "webrtc/media/base/videobroadcaster.h"
37 #include <functional>
38 #include <memory>
39 /** This file hosts several structures identifying different aspects
40  * of a RTP Session.
41  */
42 
43 namespace mozilla {
44 
45 // Convert (SI) kilobits/sec to (SI) bits/sec
46 #define KBPS(kbps) kbps * 1000
47 
48 const int kViEMinCodecBitrate_bps = KBPS(30);
49 const unsigned int kVideoMtu = 1200;
50 const int kQpMax = 56;
51 
52 template <typename T>
53 T MinIgnoreZero(const T& a, const T& b);
54 
55 class VideoStreamFactory;
56 class WebrtcAudioConduit;
57 
58 // Interface of external video encoder for WebRTC.
59 class WebrtcVideoEncoder : public VideoEncoder, public webrtc::VideoEncoder {};
60 
61 // Interface of external video decoder for WebRTC.
62 class WebrtcVideoDecoder : public VideoDecoder, public webrtc::VideoDecoder {};
63 
64 /**
65  * Concrete class for Video session. Hooks up
66  *  - media-source and target to external transport
67  */
68 class WebrtcVideoConduit
69     : public VideoSessionConduit,
70       public webrtc::RtcpEventObserver,
71       public webrtc::RtpPacketSinkInterface,
72       public webrtc::Transport,
73       public webrtc::VideoEncoderFactory,
74       public rtc::VideoSinkInterface<webrtc::VideoFrame>,
75       public rtc::VideoSourceInterface<webrtc::VideoFrame> {
76  public:
77   // VoiceEngine defined constant for Payload Name Size.
78   static const unsigned int CODEC_PLNAME_SIZE;
79 
80   // Returns true when both encoder and decoder are HW accelerated.
81   static bool HasH264Hardware();
82 
83   MediaConduitErrorCode SetLocalRTPExtensions(
84       MediaSessionConduitLocalDirection aDirection,
85       const RtpExtList& aExtensions) override;
86 
87   /**
88    * Function to attach Renderer end-point for the Media-Video conduit.
89    * @param aRenderer : Reference to the concrete mozilla Video renderer
90    * implementation Note: Multiple invocations of this API shall remove an
91    * existing renderer and attaches the new to the Conduit.
92    */
93   MediaConduitErrorCode AttachRenderer(
94       RefPtr<mozilla::VideoRenderer> aVideoRenderer) override;
95   void DetachRenderer() override;
96 
97   /**
98    * APIs used by the registered external transport to this Conduit to
99    * feed in received RTP Frames to the VideoEngine for decoding
100    */
101   MediaConduitErrorCode ReceivedRTPPacket(const void* data, int len,
102                                           webrtc::RTPHeader& header) override;
103 
104   /**
105    * APIs used by the registered external transport to this Conduit to
106    * feed in received RTCP Frames to the VideoEngine for decoding
107    */
108   MediaConduitErrorCode ReceivedRTCPPacket(const void* data, int len) override;
109   Maybe<DOMHighResTimeStamp> LastRtcpReceived() const override;
GetNow()110   DOMHighResTimeStamp GetNow() const override { return mCall->GetNow(); }
111 
112   MediaConduitErrorCode StopTransmitting() override;
113   MediaConduitErrorCode StartTransmitting() override;
114   MediaConduitErrorCode StopReceiving() override;
115   MediaConduitErrorCode StartReceiving() override;
116 
117   MediaConduitErrorCode StopTransmittingLocked();
118   MediaConduitErrorCode StartTransmittingLocked();
119   MediaConduitErrorCode StopReceivingLocked();
120   MediaConduitErrorCode StartReceivingLocked();
121 
122   /**
123    * Function to configure sending codec mode for different content
124    */
125   MediaConduitErrorCode ConfigureCodecMode(webrtc::VideoCodecMode) override;
126 
127   /**
128    * Function to configure send codec for the video session
129    * @param sendSessionConfig: CodecConfiguration
130    * @result: On Success, the video engine is configured with passed in codec
131    *          for send
132    *          On failure, video engine transmit functionality is disabled.
133    * NOTE: This API can be invoked multiple time. Invoking this API may involve
134    * restarting transmission sub-system on the engine.
135    */
136   MediaConduitErrorCode ConfigureSendMediaCodec(
137       const VideoCodecConfig* codecInfo,
138       const RtpRtcpConfig& aRtpRtcpConfig) override;
139 
140   /**
141    * Function to configure list of receive codecs for the video session
142    * @param sendSessionConfig: CodecConfiguration
143    * @result: On Success, the video engine is configured with passed in codec
144    *          for send
145    *          Also the playout is enabled.
146    *          On failure, video engine transmit functionality is disabled.
147    * NOTE: This API can be invoked multiple time. Invoking this API may involve
148    * restarting transmission sub-system on the engine.
149    */
150   MediaConduitErrorCode ConfigureRecvMediaCodecs(
151       const std::vector<UniquePtr<VideoCodecConfig>>& codecConfigList,
152       const RtpRtcpConfig& aRtpRtcpConfig) override;
153 
154   /**
155    * Register Transport for this Conduit. RTP and RTCP frames from the
156    * VideoEngine shall be passed to the registered transport for transporting
157    * externally.
158    */
159   MediaConduitErrorCode SetTransmitterTransport(
160       RefPtr<TransportInterface> aTransport) override;
161 
162   MediaConduitErrorCode SetReceiverTransport(
163       RefPtr<TransportInterface> aTransport) override;
164 
165   /**
166    * Function to select and change the encoding resolution based on incoming
167    * frame size and current available bandwidth.
168    * @param width, height: dimensions of the frame
169    * @param frame: optional frame to submit for encoding after reconfig
170    */
171   void SelectSendResolution(unsigned short width, unsigned short height);
172 
173   /**
174    * Function to deliver a capture video frame for encoding and transport.
175    * If the frame's timestamp is 0, it will be automatically generated.
176    *
177    * NOTE: ConfigureSendMediaCodec() must be called before this function can
178    *       be invoked. This ensures the inserted video-frames can be
179    *       transmitted by the conduit.
180    */
181   MediaConduitErrorCode SendVideoFrame(
182       const webrtc::VideoFrame& frame) override;
183 
184   /**
185    * webrtc::Transport method implementation
186    * ---------------------------------------
187    * Webrtc transport implementation to send and receive RTP packet.
188    * VideoConduit registers itself as ExternalTransport to the VideoStream
189    */
190   bool SendRtp(const uint8_t* packet, size_t length,
191                const webrtc::PacketOptions& options) override;
192 
193   /**
194    * webrtc::Transport method implementation
195    * ---------------------------------------
196    * Webrtc transport implementation to send and receive RTCP packet.
197    * VideoConduit registers itself as ExternalTransport to the VideoEngine
198    */
199   bool SendRtcp(const uint8_t* packet, size_t length) override;
200 
201   /*
202    * webrtc:VideoSinkInterface implementation
203    * -------------------------------
204    */
205   void OnFrame(const webrtc::VideoFrame& frame) override;
206 
207   /*
208    * webrtc:VideoSourceInterface implementation
209    * -------------------------------
210    */
211   void AddOrUpdateSink(rtc::VideoSinkInterface<webrtc::VideoFrame>* sink,
212                        const rtc::VideoSinkWants& wants) override;
213   void AddOrUpdateSinkNotLocked(
214       rtc::VideoSinkInterface<webrtc::VideoFrame>* sink,
215       const rtc::VideoSinkWants& wants);
216 
217   void RemoveSink(rtc::VideoSinkInterface<webrtc::VideoFrame>* sink) override;
218   void RemoveSinkNotLocked(rtc::VideoSinkInterface<webrtc::VideoFrame>* sink);
219 
220   void OnSinkWantsChanged(const rtc::VideoSinkWants& wants);
221 
222   uint64_t CodecPluginID() override;
223 
SetPCHandle(const std::string & aPCHandle)224   void SetPCHandle(const std::string& aPCHandle) override {
225     MOZ_ASSERT(NS_IsMainThread());
226     mPCHandle = aPCHandle;
227   }
228 
229   void DeleteStreams() override;
230 
Denoising()231   bool Denoising() const { return mDenoising; }
232 
SpatialLayers()233   uint8_t SpatialLayers() const { return mSpatialLayers; }
234 
TemporalLayers()235   uint8_t TemporalLayers() const { return mTemporalLayers; }
236 
CodecMode()237   webrtc::VideoCodecMode CodecMode() const {
238     MOZ_ASSERT(NS_IsMainThread());
239     return mCodecMode;
240   }
241 
242   WebrtcVideoConduit(RefPtr<WebRtcCallWrapper> aCall,
243                      nsCOMPtr<nsISerialEventTarget> aStsThread);
244   virtual ~WebrtcVideoConduit();
245 
246   MediaConduitErrorCode InitMain();
247   virtual MediaConduitErrorCode Init();
248 
249   std::vector<uint32_t> GetLocalSSRCs() override;
250   bool SetLocalSSRCs(const std::vector<uint32_t>& ssrcs,
251                      const std::vector<uint32_t>& rtxSsrcs) override;
252   // Can be called from any thread
253   bool GetRemoteSSRC(uint32_t* ssrc) override;
254   bool SetRemoteSSRC(uint32_t ssrc, uint32_t rtxSsrc) override;
255   bool UnsetRemoteSSRC(uint32_t ssrc) override;
256   bool SetLocalCNAME(const char* cname) override;
257   bool SetLocalMID(const std::string& mid) override;
258 
259   void SetSyncGroup(const std::string& group) override;
260 
261   bool SetRemoteSSRCLocked(uint32_t ssrc, uint32_t rtxSsrc);
262 
263   bool GetSendPacketTypeStats(
264       webrtc::RtcpPacketTypeCounter* aPacketCounts) override;
265 
266   bool GetRecvPacketTypeStats(
267       webrtc::RtcpPacketTypeCounter* aPacketCounts) override;
268 
269   void PollStats();
270   void UpdateVideoStatsTimer();
271   bool GetVideoEncoderStats(double* framerateMean, double* framerateStdDev,
272                             double* bitrateMean, double* bitrateStdDev,
273                             uint32_t* droppedFrames, uint32_t* framesEncoded,
274                             Maybe<uint64_t>* qpSum) override;
275   bool GetVideoDecoderStats(double* framerateMean, double* framerateStdDev,
276                             double* bitrateMean, double* bitrateStdDev,
277                             uint32_t* discardedPackets,
278                             uint32_t* framesDecoded) override;
279   bool GetRTPReceiverStats(unsigned int* jitterMs,
280                            unsigned int* cumulativeLost) override;
281   bool GetRTCPReceiverReport(uint32_t* jitterMs, uint32_t* packetsReceived,
282                              uint64_t* bytesReceived, uint32_t* cumulativeLost,
283                              Maybe<double>* aOutRttSec) override;
284   bool GetRTCPSenderReport(unsigned int* packetsSent, uint64_t* bytesSent,
285                            DOMHighResTimeStamp* aRemoteTimestamp) override;
286 
287   Maybe<mozilla::dom::RTCBandwidthEstimationInternal> GetBandwidthEstimation()
288       override;
289 
290   void GetRtpSources(nsTArray<dom::RTCRtpSourceEntry>& outSources) override;
291   bool AddFrameHistory(dom::Sequence<dom::RTCVideoFrameHistoryInternal>*
292                            outHistories) const override;
293 
294   uint64_t MozVideoLatencyAvg();
295 
DisableSsrcChanges()296   void DisableSsrcChanges() override {
297     ASSERT_ON_THREAD(mStsThread);
298     mAllowSsrcChange = false;
299   }
300 
301   /**
302    * Callback from libwebrtc with the parsed packet for synchronization
303    * source tracking. STS thread only.
304    */
305   void OnRtpPacket(const webrtc::RtpPacketReceived& packet) override;
306 
AsVideoSessionConduit()307   Maybe<RefPtr<VideoSessionConduit>> AsVideoSessionConduit() override {
308     return Some(RefPtr<VideoSessionConduit>(this));
309   }
310 
RecordTelemetry()311   void RecordTelemetry() const override {
312     ASSERT_ON_THREAD(mStsThread);
313     mSendStreamStats.RecordTelemetry();
314     mRecvStreamStats.RecordTelemetry();
315   }
316 
317   void OnRtcpBye() override;
318 
319   void OnRtcpTimeout() override;
320 
321   void SetRtcpEventObserver(mozilla::RtcpEventObserver* observer) override;
322 
323  private:
324   // Don't allow copying/assigning.
325   WebrtcVideoConduit(const WebrtcVideoConduit&) = delete;
326   void operator=(const WebrtcVideoConduit&) = delete;
327 
328   /**
329    * Statistics for the Call associated with this VideoConduit.
330    * Single threaded.
331    */
332   class CallStatistics {
333    public:
CallStatistics(nsCOMPtr<nsISerialEventTarget> aStatsThread)334     explicit CallStatistics(nsCOMPtr<nsISerialEventTarget> aStatsThread)
335         : mStatsThread(aStatsThread) {}
336     void Update(const webrtc::Call::Stats& aStats);
337     Maybe<mozilla::dom::RTCBandwidthEstimationInternal> Stats() const;
338     Maybe<DOMHighResTimeStamp> RttSec() const;
339 
340    protected:
341     const nsCOMPtr<nsISerialEventTarget> mStatsThread;
342 
343    private:
344     Maybe<webrtc::Call::Stats> mStats = Nothing();
345     Maybe<DOMHighResTimeStamp> mRttSec = Nothing();
346   };
347 
348   /**
349    * Shared statistics for receive and transmit video streams.
350    * Single threaded.
351    */
352   class StreamStatistics {
353    public:
StreamStatistics(nsCOMPtr<nsISerialEventTarget> aStatsThread)354     explicit StreamStatistics(nsCOMPtr<nsISerialEventTarget> aStatsThread)
355         : mStatsThread(aStatsThread) {}
356     void Update(const double aFrameRate, const double aBitrate,
357                 const webrtc::RtcpPacketTypeCounter& aPacketCounts);
358     /**
359      * Returns gathered stream statistics
360      * @param aOutFrMean: mean framerate
361      * @param aOutFrStdDev: standard deviation of framerate
362      * @param aOutBrMean: mean bitrate
363      * @param aOutBrStdDev: standard deviation of bitrate
364      */
365     bool GetVideoStreamStats(double& aOutFrMean, double& aOutFrStdDev,
366                              double& aOutBrMean, double& aOutBrStdDev) const;
367 
368     /**
369      * Accumulates video quality telemetry
370      */
371     void RecordTelemetry() const;
372     const webrtc::RtcpPacketTypeCounter& PacketCounts() const;
373     bool Active() const;
374     void SetActive(bool aActive);
IsSend()375     virtual bool IsSend() const { return false; };
376 
377    protected:
378     const nsCOMPtr<nsISerialEventTarget> mStatsThread;
379 
380    private:
381     bool mActive = false;
382     RunningStat mFrameRate;
383     RunningStat mBitRate;
384     webrtc::RtcpPacketTypeCounter mPacketCounts;
385   };
386 
387   /**
388    * Statistics for sending streams. Single threaded.
389    */
390   class SendStreamStatistics : public StreamStatistics {
391    public:
SendStreamStatistics(nsCOMPtr<nsISerialEventTarget> aStatsThread)392     explicit SendStreamStatistics(nsCOMPtr<nsISerialEventTarget> aStatsThread)
393         : StreamStatistics(
394               std::forward<nsCOMPtr<nsISerialEventTarget>>(aStatsThread)) {}
395     /**
396      * Returns the calculate number of dropped frames
397      */
398     uint32_t DroppedFrames() const;
399     /**
400      * Returns the number of frames that have been encoded so far
401      */
402     uint32_t FramesEncoded() const;
403     void Update(const webrtc::VideoSendStream::Stats& aStats,
404                 uint32_t aConfiguredSsrc);
405     /**
406      * Call once for every frame delivered for encoding
407      */
408     void FrameDeliveredToEncoder();
409 
410     bool SsrcFound() const;
411     uint32_t JitterMs() const;
412     uint32_t PacketsLost() const;
413     uint64_t BytesReceived() const;
414     uint32_t PacketsReceived() const;
415     Maybe<uint64_t> QpSum() const;
IsSend()416     bool IsSend() const override { return true; };
417 
418    private:
419     uint32_t mDroppedFrames = 0;
420     uint32_t mFramesEncoded = 0;
421     int32_t mFramesDeliveredToEncoder = 0;
422 
423     bool mSsrcFound = false;
424     uint32_t mJitterMs = 0;
425     uint32_t mPacketsLost = 0;
426     uint64_t mBytesReceived = 0;
427     uint32_t mPacketsReceived = 0;
428     Maybe<uint64_t> mQpSum;
429   };
430 
431   /**
432    * Statistics for receiving streams. Single threaded.
433    */
434   class ReceiveStreamStatistics : public StreamStatistics {
435    public:
ReceiveStreamStatistics(nsCOMPtr<nsISerialEventTarget> aStatsThread)436     explicit ReceiveStreamStatistics(
437         nsCOMPtr<nsISerialEventTarget> aStatsThread)
438         : StreamStatistics(
439               std::forward<nsCOMPtr<nsISerialEventTarget>>(aStatsThread)) {}
440     uint32_t BytesSent() const;
441     /**
442      * Returns the number of discarded packets
443      */
444     uint32_t DiscardedPackets() const;
445     /**
446      * Returns the number of frames decoded
447      */
448     uint32_t FramesDecoded() const;
449     uint32_t JitterMs() const;
450     uint32_t PacketsLost() const;
451     uint32_t PacketsSent() const;
452     uint32_t Ssrc() const;
453     DOMHighResTimeStamp RemoteTimestamp() const;
454     void Update(const webrtc::VideoReceiveStream::Stats& aStats);
455 
456    private:
457     uint32_t mBytesSent = 0;
458     uint32_t mDiscardedPackets = 0;
459     uint32_t mFramesDecoded = 0;
460     uint32_t mJitterMs = 0;
461     uint32_t mPacketsLost = 0;
462     uint32_t mPacketsSent = 0;
463     uint32_t mSsrc = 0;
464     DOMHighResTimeStamp mRemoteTimestamp = 0;
465   };
466 
467   // Utility function to dump recv codec database
468   void DumpCodecDB() const;
469 
470   // Video Latency Test averaging filter
471   void VideoLatencyUpdate(uint64_t new_sample);
472 
473   MediaConduitErrorCode CreateSendStream();
474   void DeleteSendStream();
475   MediaConduitErrorCode CreateRecvStream();
476   void DeleteRecvStream();
477 
478   std::unique_ptr<webrtc::VideoDecoder> CreateDecoder(
479       webrtc::VideoCodecType aType);
480   std::unique_ptr<webrtc::VideoEncoder> CreateEncoder(
481       webrtc::VideoCodecType aType);
482 
483   // webrtc::VideoEncoderFactory
484   std::vector<webrtc::SdpVideoFormat> GetSupportedFormats() const override;
485 
486   CodecInfo QueryVideoEncoder(
487       const webrtc::SdpVideoFormat& format) const override;
488 
489   std::unique_ptr<webrtc::VideoEncoder> CreateVideoEncoder(
490       const webrtc::SdpVideoFormat& format) override;
491 
492   MediaConduitErrorCode DeliverPacket(const void* data, int len) override;
493 
494   bool RequiresNewSendStream(const VideoCodecConfig& newConfig) const;
495 
496   mutable mozilla::ReentrantMonitor mTransportMonitor;
497 
498   // Accessed on any thread under mTransportMonitor.
499   RefPtr<TransportInterface> mTransmitterTransport;
500 
501   // Accessed on any thread under mTransportMonitor.
502   RefPtr<TransportInterface> mReceiverTransport;
503 
504   // Accessed on any thread under mTransportMonitor.
505   RefPtr<mozilla::VideoRenderer> mRenderer;
506 
507   // Accessed on any thread under mTransportMonitor.
508   unsigned short mReceivingWidth = 0;
509 
510   // Accessed on any thread under mTransportMonitor.
511   unsigned short mReceivingHeight = 0;
512 
513   // Socket transport service thread that runs stats queries against us. Any
514   // thread.
515   const nsCOMPtr<nsISerialEventTarget> mStsThread;
516 
517   Mutex mMutex;
518 
519   // Adapter handling resolution constraints from signaling and sinks.
520   // Written only on main thread. Guarded by mMutex, except for reads on main.
521   UniquePtr<cricket::VideoAdapter> mVideoAdapter;
522 
523   // Our own record of the sinks added to mVideoBroadcaster so we can support
524   // dispatching updates to sinks from off-main-thread. Main thread only.
525   AutoTArray<rtc::VideoSinkInterface<webrtc::VideoFrame>*, 1> mRegisteredSinks;
526 
527   // Broadcaster that distributes our frames to all registered sinks.
528   // Sinks can only be added, updated and removed on main thread.
529   // Frames can be passed in on any thread.
530   rtc::VideoBroadcaster mVideoBroadcaster;
531 
532   // Buffer pool used for scaling frames.
533   // Accessed on the frame-feeding thread only.
534   webrtc::I420BufferPool mBufferPool;
535 
536   // Engine state we are concerned with. Written on main thread and read
537   // anywhere.
538   mozilla::Atomic<bool>
539       mEngineTransmitting;  // If true ==> Transmit Subsystem is up and running
540   mozilla::Atomic<bool>
541       mEngineReceiving;  // if true ==> Receive Subsystem up and running
542 
543   // Local database of currently applied receive codecs. Main thread only.
544   nsTArray<UniquePtr<VideoCodecConfig>> mRecvCodecList;
545 
546   // Written only on main thread. Guarded by mMutex, except for reads on main.
547   UniquePtr<VideoCodecConfig> mCurSendCodecConfig;
548 
549   bool mUpdateResolution = false;
550   int mSinkWantsPixelCount = std::numeric_limits<int>::max();
551 
552   // Bookkeeping of send stream stats. Sts thread only.
553   SendStreamStatistics mSendStreamStats;
554 
555   // Bookkeeping of send stream stats. Sts thread only.
556   ReceiveStreamStatistics mRecvStreamStats;
557 
558   // Bookkeeping of call stats. Sts thread only.
559   CallStatistics mCallStats;
560 
561   // Must call webrtc::Call::DestroyVideoReceive/SendStream to delete this.
562   // Written only on main thread. Guarded by mMutex, except for reads on main.
563   webrtc::VideoReceiveStream* mRecvStream = nullptr;
564 
565   // Must call webrtc::Call::DestroyVideoReceive/SendStream to delete this.
566   // Written only on main thread. Guarded by mMutex, except for reads on main.
567   webrtc::VideoSendStream* mSendStream = nullptr;
568 
569   // Written on the frame feeding thread.
570   // Guarded by mMutex, except for reads on the frame feeding thread.
571   unsigned short mLastWidth = 0;
572 
573   // Written on the frame feeding thread.
574   // Guarded by mMutex, except for reads on the frame feeding thread.
575   unsigned short mLastHeight = 0;
576 
577   // Accessed under mMutex.
578   unsigned int mSendingFramerate;
579 
580   // Written on main thread at creation,
581   // then written or read on any thread under mTransportMonitor.
582   bool mVideoLatencyTestEnable = false;
583 
584   // Accessed from any thread under mTransportMonitor.
585   uint64_t mVideoLatencyAvg = 0;
586 
587   // All in bps.
588   // All written on main thread and guarded by mMutex, except for reads on main.
589   int mMinBitrate = 0;
590   int mStartBitrate = 0;
591   int mPrefMaxBitrate = 0;
592   int mNegotiatedMaxBitrate = 0;
593   int mMinBitrateEstimate = 0;
594 
595   // Set to true to force denoising on.
596   // Written at creation, then read anywhere.
597   bool mDenoising = false;
598 
599   // Set to true to ignore sink wants (scaling due to bwe and cpu usage).
600   // Written at creation, then read anywhere.
601   bool mLockScaling = false;
602 
603   // Written at creation, then read anywhere.
604   uint8_t mSpatialLayers = 1;
605 
606   // Written at creation, then read anywhere.
607   uint8_t mTemporalLayers = 1;
608 
609   static const unsigned int sAlphaNum = 7;
610   static const unsigned int sAlphaDen = 8;
611   static const unsigned int sRoundingPadding = 1024;
612 
613   // Main thread only.
614   RefPtr<WebrtcAudioConduit> mSyncedTo;
615 
616   // Main thread only.
617   webrtc::VideoCodecMode mActiveCodecMode;
618   webrtc::VideoCodecMode mCodecMode;
619 
620   // WEBRTC.ORG Call API
621   // Const so can be accessed on any thread. Most methods are called on
622   // main thread, though Receiver() is called on STS. This seems fine.
623   const RefPtr<WebRtcCallWrapper> mCall;
624 
625   // Written only on main thread. Guarded by mMutex, except for reads on main.
626   webrtc::VideoSendStream::Config mSendStreamConfig;
627 
628   // Main thread only.
629   webrtc::VideoEncoderConfig mEncoderConfig;
630 
631   // Written only on main thread. Guarded by mMutex, except for reads on main.
632   // Calls can happen on any thread.
633   RefPtr<rtc::RefCountedObject<VideoStreamFactory>> mVideoStreamFactory;
634 
635   // Main thread only.
636   webrtc::VideoReceiveStream::Config mRecvStreamConfig;
637 
638   // Are SSRC changes without signaling allowed or not.
639   // Accessed only on mStsThread.
640   bool mAllowSsrcChange = true;
641 
642   // Accessed only on mStsThread.
643   bool mWaitingForInitialSsrc = true;
644 
645   // Accessed during configuration/signaling (main),
646   // and when receiving packets (sts).
647   Atomic<uint32_t> mRecvSSRC;  // this can change during a stream!
648   // Accessed from both the STS and main thread for a variety of things
649   // Set when receiving packets
650   Atomic<uint32_t> mRemoteSSRC;  // this can change during a stream!
651 
652   // Accessed only on mStsThread.
653   RtpPacketQueue mRtpPacketQueue;
654 
655   // The lifetime of these codecs are maintained by the VideoConduit instance.
656   // They are passed to the webrtc::VideoSendStream or VideoReceiveStream,
657   // on construction.
658   std::unique_ptr<webrtc::VideoEncoder> mEncoder;  // only one encoder for now
659   std::vector<std::unique_ptr<webrtc::VideoDecoder>> mDecoders;
660   // Main thread only
661   uint64_t mSendCodecPluginID = 0;
662   // Main thread only
663   uint64_t mRecvCodecPluginID = 0;
664 
665   // Timer that updates video stats periodically. Main thread only.
666   nsCOMPtr<nsITimer> mVideoStatsTimer;
667   // True if mVideoStatsTimer is running. Main thread only.
668   bool mVideoStatsTimerActive = false;
669 
670   // Main thread only
671   std::string mPCHandle;
672 
673   // Accessed only on mStsThread
674   Maybe<DOMHighResTimeStamp> mLastRtcpReceived;
675 
676   // Accessed only on main thread.
677   mozilla::RtcpEventObserver* mRtcpEventObserver = nullptr;
678 
679   // Accessed from main and mStsThread. Uses locks internally.
680   RefPtr<RtpSourceObserver> mRtpSourceObserver;
681   // Tracking the attributes of received frames over time
682   // Protected by mTransportMonitor
683   dom::RTCVideoFrameHistoryInternal mReceivedFrameHistory;
684 };
685 }  // namespace mozilla
686 
687 #endif
688