xref: /aosp_15_r20/external/webrtc/media/engine/webrtc_video_engine.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1*d9f75844SAndroid Build Coastguard Worker /*
2*d9f75844SAndroid Build Coastguard Worker  *  Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
3*d9f75844SAndroid Build Coastguard Worker  *
4*d9f75844SAndroid Build Coastguard Worker  *  Use of this source code is governed by a BSD-style license
5*d9f75844SAndroid Build Coastguard Worker  *  that can be found in the LICENSE file in the root of the source
6*d9f75844SAndroid Build Coastguard Worker  *  tree. An additional intellectual property rights grant can be found
7*d9f75844SAndroid Build Coastguard Worker  *  in the file PATENTS.  All contributing project authors may
8*d9f75844SAndroid Build Coastguard Worker  *  be found in the AUTHORS file in the root of the source tree.
9*d9f75844SAndroid Build Coastguard Worker  */
10*d9f75844SAndroid Build Coastguard Worker 
11*d9f75844SAndroid Build Coastguard Worker #ifndef MEDIA_ENGINE_WEBRTC_VIDEO_ENGINE_H_
12*d9f75844SAndroid Build Coastguard Worker #define MEDIA_ENGINE_WEBRTC_VIDEO_ENGINE_H_
13*d9f75844SAndroid Build Coastguard Worker 
14*d9f75844SAndroid Build Coastguard Worker #include <map>
15*d9f75844SAndroid Build Coastguard Worker #include <memory>
16*d9f75844SAndroid Build Coastguard Worker #include <set>
17*d9f75844SAndroid Build Coastguard Worker #include <string>
18*d9f75844SAndroid Build Coastguard Worker #include <vector>
19*d9f75844SAndroid Build Coastguard Worker 
20*d9f75844SAndroid Build Coastguard Worker #include "absl/types/optional.h"
21*d9f75844SAndroid Build Coastguard Worker #include "api/call/transport.h"
22*d9f75844SAndroid Build Coastguard Worker #include "api/sequence_checker.h"
23*d9f75844SAndroid Build Coastguard Worker #include "api/task_queue/pending_task_safety_flag.h"
24*d9f75844SAndroid Build Coastguard Worker #include "api/transport/field_trial_based_config.h"
25*d9f75844SAndroid Build Coastguard Worker #include "api/video/video_bitrate_allocator_factory.h"
26*d9f75844SAndroid Build Coastguard Worker #include "api/video/video_frame.h"
27*d9f75844SAndroid Build Coastguard Worker #include "api/video/video_sink_interface.h"
28*d9f75844SAndroid Build Coastguard Worker #include "api/video/video_source_interface.h"
29*d9f75844SAndroid Build Coastguard Worker #include "api/video_codecs/sdp_video_format.h"
30*d9f75844SAndroid Build Coastguard Worker #include "call/call.h"
31*d9f75844SAndroid Build Coastguard Worker #include "call/flexfec_receive_stream.h"
32*d9f75844SAndroid Build Coastguard Worker #include "call/video_receive_stream.h"
33*d9f75844SAndroid Build Coastguard Worker #include "call/video_send_stream.h"
34*d9f75844SAndroid Build Coastguard Worker #include "media/base/media_engine.h"
35*d9f75844SAndroid Build Coastguard Worker #include "media/engine/unhandled_packets_buffer.h"
36*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/network_route.h"
37*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/synchronization/mutex.h"
38*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/system/no_unique_address.h"
39*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/thread_annotations.h"
40*d9f75844SAndroid Build Coastguard Worker 
41*d9f75844SAndroid Build Coastguard Worker namespace webrtc {
42*d9f75844SAndroid Build Coastguard Worker class VideoDecoderFactory;
43*d9f75844SAndroid Build Coastguard Worker class VideoEncoderFactory;
44*d9f75844SAndroid Build Coastguard Worker }  // namespace webrtc
45*d9f75844SAndroid Build Coastguard Worker 
46*d9f75844SAndroid Build Coastguard Worker namespace cricket {
47*d9f75844SAndroid Build Coastguard Worker 
48*d9f75844SAndroid Build Coastguard Worker class WebRtcVideoChannel;
49*d9f75844SAndroid Build Coastguard Worker 
50*d9f75844SAndroid Build Coastguard Worker // Public for testing.
51*d9f75844SAndroid Build Coastguard Worker // Inputs StreamStats for all types of substreams (kMedia, kRtx, kFlexfec) and
52*d9f75844SAndroid Build Coastguard Worker // merges any non-kMedia substream stats object into its referenced kMedia-type
53*d9f75844SAndroid Build Coastguard Worker // substream. The resulting substreams are all kMedia. This means, for example,
54*d9f75844SAndroid Build Coastguard Worker // that packet and byte counters of RTX and FlexFEC streams are accounted for in
55*d9f75844SAndroid Build Coastguard Worker // the relevant RTP media stream's stats. This makes the resulting StreamStats
56*d9f75844SAndroid Build Coastguard Worker // objects ready to be turned into "outbound-rtp" stats objects for GetStats()
57*d9f75844SAndroid Build Coastguard Worker // which does not create separate stream stats objects for complementary
58*d9f75844SAndroid Build Coastguard Worker // streams.
59*d9f75844SAndroid Build Coastguard Worker std::map<uint32_t, webrtc::VideoSendStream::StreamStats>
60*d9f75844SAndroid Build Coastguard Worker MergeInfoAboutOutboundRtpSubstreamsForTesting(
61*d9f75844SAndroid Build Coastguard Worker     const std::map<uint32_t, webrtc::VideoSendStream::StreamStats>& substreams);
62*d9f75844SAndroid Build Coastguard Worker 
63*d9f75844SAndroid Build Coastguard Worker class UnsignalledSsrcHandler {
64*d9f75844SAndroid Build Coastguard Worker  public:
65*d9f75844SAndroid Build Coastguard Worker   enum Action {
66*d9f75844SAndroid Build Coastguard Worker     kDropPacket,
67*d9f75844SAndroid Build Coastguard Worker     kDeliverPacket,
68*d9f75844SAndroid Build Coastguard Worker   };
69*d9f75844SAndroid Build Coastguard Worker   virtual Action OnUnsignalledSsrc(WebRtcVideoChannel* channel,
70*d9f75844SAndroid Build Coastguard Worker                                    uint32_t ssrc,
71*d9f75844SAndroid Build Coastguard Worker                                    absl::optional<uint32_t> rtx_ssrc) = 0;
72*d9f75844SAndroid Build Coastguard Worker   virtual ~UnsignalledSsrcHandler() = default;
73*d9f75844SAndroid Build Coastguard Worker };
74*d9f75844SAndroid Build Coastguard Worker 
75*d9f75844SAndroid Build Coastguard Worker // TODO(pbos): Remove, use external handlers only.
76*d9f75844SAndroid Build Coastguard Worker class DefaultUnsignalledSsrcHandler : public UnsignalledSsrcHandler {
77*d9f75844SAndroid Build Coastguard Worker  public:
78*d9f75844SAndroid Build Coastguard Worker   DefaultUnsignalledSsrcHandler();
79*d9f75844SAndroid Build Coastguard Worker   Action OnUnsignalledSsrc(WebRtcVideoChannel* channel,
80*d9f75844SAndroid Build Coastguard Worker                            uint32_t ssrc,
81*d9f75844SAndroid Build Coastguard Worker                            absl::optional<uint32_t> rtx_ssrc) override;
82*d9f75844SAndroid Build Coastguard Worker 
83*d9f75844SAndroid Build Coastguard Worker   rtc::VideoSinkInterface<webrtc::VideoFrame>* GetDefaultSink() const;
84*d9f75844SAndroid Build Coastguard Worker   void SetDefaultSink(WebRtcVideoChannel* channel,
85*d9f75844SAndroid Build Coastguard Worker                       rtc::VideoSinkInterface<webrtc::VideoFrame>* sink);
86*d9f75844SAndroid Build Coastguard Worker 
87*d9f75844SAndroid Build Coastguard Worker   virtual ~DefaultUnsignalledSsrcHandler() = default;
88*d9f75844SAndroid Build Coastguard Worker 
89*d9f75844SAndroid Build Coastguard Worker  private:
90*d9f75844SAndroid Build Coastguard Worker   rtc::VideoSinkInterface<webrtc::VideoFrame>* default_sink_;
91*d9f75844SAndroid Build Coastguard Worker };
92*d9f75844SAndroid Build Coastguard Worker 
93*d9f75844SAndroid Build Coastguard Worker // WebRtcVideoEngine is used for the new native WebRTC Video API (webrtc:1667).
94*d9f75844SAndroid Build Coastguard Worker class WebRtcVideoEngine : public VideoEngineInterface {
95*d9f75844SAndroid Build Coastguard Worker  public:
96*d9f75844SAndroid Build Coastguard Worker   // These video codec factories represents all video codecs, i.e. both software
97*d9f75844SAndroid Build Coastguard Worker   // and external hardware codecs.
98*d9f75844SAndroid Build Coastguard Worker   WebRtcVideoEngine(
99*d9f75844SAndroid Build Coastguard Worker       std::unique_ptr<webrtc::VideoEncoderFactory> video_encoder_factory,
100*d9f75844SAndroid Build Coastguard Worker       std::unique_ptr<webrtc::VideoDecoderFactory> video_decoder_factory,
101*d9f75844SAndroid Build Coastguard Worker       const webrtc::FieldTrialsView& trials);
102*d9f75844SAndroid Build Coastguard Worker 
103*d9f75844SAndroid Build Coastguard Worker   ~WebRtcVideoEngine() override;
104*d9f75844SAndroid Build Coastguard Worker 
105*d9f75844SAndroid Build Coastguard Worker   VideoMediaChannel* CreateMediaChannel(
106*d9f75844SAndroid Build Coastguard Worker       webrtc::Call* call,
107*d9f75844SAndroid Build Coastguard Worker       const MediaConfig& config,
108*d9f75844SAndroid Build Coastguard Worker       const VideoOptions& options,
109*d9f75844SAndroid Build Coastguard Worker       const webrtc::CryptoOptions& crypto_options,
110*d9f75844SAndroid Build Coastguard Worker       webrtc::VideoBitrateAllocatorFactory* video_bitrate_allocator_factory)
111*d9f75844SAndroid Build Coastguard Worker       override;
112*d9f75844SAndroid Build Coastguard Worker 
send_codecs()113*d9f75844SAndroid Build Coastguard Worker   std::vector<VideoCodec> send_codecs() const override {
114*d9f75844SAndroid Build Coastguard Worker     return send_codecs(true);
115*d9f75844SAndroid Build Coastguard Worker   }
recv_codecs()116*d9f75844SAndroid Build Coastguard Worker   std::vector<VideoCodec> recv_codecs() const override {
117*d9f75844SAndroid Build Coastguard Worker     return recv_codecs(true);
118*d9f75844SAndroid Build Coastguard Worker   }
119*d9f75844SAndroid Build Coastguard Worker   std::vector<VideoCodec> send_codecs(bool include_rtx) const override;
120*d9f75844SAndroid Build Coastguard Worker   std::vector<VideoCodec> recv_codecs(bool include_rtx) const override;
121*d9f75844SAndroid Build Coastguard Worker   std::vector<webrtc::RtpHeaderExtensionCapability> GetRtpHeaderExtensions()
122*d9f75844SAndroid Build Coastguard Worker       const override;
123*d9f75844SAndroid Build Coastguard Worker 
124*d9f75844SAndroid Build Coastguard Worker  private:
125*d9f75844SAndroid Build Coastguard Worker   const std::unique_ptr<webrtc::VideoDecoderFactory> decoder_factory_;
126*d9f75844SAndroid Build Coastguard Worker   const std::unique_ptr<webrtc::VideoEncoderFactory> encoder_factory_;
127*d9f75844SAndroid Build Coastguard Worker   const std::unique_ptr<webrtc::VideoBitrateAllocatorFactory>
128*d9f75844SAndroid Build Coastguard Worker       bitrate_allocator_factory_;
129*d9f75844SAndroid Build Coastguard Worker   const webrtc::FieldTrialsView& trials_;
130*d9f75844SAndroid Build Coastguard Worker };
131*d9f75844SAndroid Build Coastguard Worker 
132*d9f75844SAndroid Build Coastguard Worker class WebRtcVideoChannel : public VideoMediaChannel,
133*d9f75844SAndroid Build Coastguard Worker                            public webrtc::Transport,
134*d9f75844SAndroid Build Coastguard Worker                            public webrtc::EncoderSwitchRequestCallback {
135*d9f75844SAndroid Build Coastguard Worker  public:
136*d9f75844SAndroid Build Coastguard Worker   WebRtcVideoChannel(
137*d9f75844SAndroid Build Coastguard Worker       webrtc::Call* call,
138*d9f75844SAndroid Build Coastguard Worker       const MediaConfig& config,
139*d9f75844SAndroid Build Coastguard Worker       const VideoOptions& options,
140*d9f75844SAndroid Build Coastguard Worker       const webrtc::CryptoOptions& crypto_options,
141*d9f75844SAndroid Build Coastguard Worker       webrtc::VideoEncoderFactory* encoder_factory,
142*d9f75844SAndroid Build Coastguard Worker       webrtc::VideoDecoderFactory* decoder_factory,
143*d9f75844SAndroid Build Coastguard Worker       webrtc::VideoBitrateAllocatorFactory* bitrate_allocator_factory);
144*d9f75844SAndroid Build Coastguard Worker   ~WebRtcVideoChannel() override;
145*d9f75844SAndroid Build Coastguard Worker 
146*d9f75844SAndroid Build Coastguard Worker   // VideoMediaChannel implementation
147*d9f75844SAndroid Build Coastguard Worker   bool SetSendParameters(const VideoSendParameters& params) override;
148*d9f75844SAndroid Build Coastguard Worker   bool SetRecvParameters(const VideoRecvParameters& params) override;
149*d9f75844SAndroid Build Coastguard Worker   webrtc::RtpParameters GetRtpSendParameters(uint32_t ssrc) const override;
150*d9f75844SAndroid Build Coastguard Worker   webrtc::RTCError SetRtpSendParameters(
151*d9f75844SAndroid Build Coastguard Worker       uint32_t ssrc,
152*d9f75844SAndroid Build Coastguard Worker       const webrtc::RtpParameters& parameters,
153*d9f75844SAndroid Build Coastguard Worker       webrtc::SetParametersCallback callback) override;
154*d9f75844SAndroid Build Coastguard Worker   webrtc::RtpParameters GetRtpReceiveParameters(uint32_t ssrc) const override;
155*d9f75844SAndroid Build Coastguard Worker   webrtc::RtpParameters GetDefaultRtpReceiveParameters() const override;
156*d9f75844SAndroid Build Coastguard Worker   bool GetSendCodec(VideoCodec* send_codec) override;
157*d9f75844SAndroid Build Coastguard Worker   bool SetSend(bool send) override;
158*d9f75844SAndroid Build Coastguard Worker   bool SetVideoSend(
159*d9f75844SAndroid Build Coastguard Worker       uint32_t ssrc,
160*d9f75844SAndroid Build Coastguard Worker       const VideoOptions* options,
161*d9f75844SAndroid Build Coastguard Worker       rtc::VideoSourceInterface<webrtc::VideoFrame>* source) override;
162*d9f75844SAndroid Build Coastguard Worker   bool AddSendStream(const StreamParams& sp) override;
163*d9f75844SAndroid Build Coastguard Worker   bool RemoveSendStream(uint32_t ssrc) override;
164*d9f75844SAndroid Build Coastguard Worker   bool AddRecvStream(const StreamParams& sp) override;
165*d9f75844SAndroid Build Coastguard Worker   bool AddRecvStream(const StreamParams& sp, bool default_stream);
166*d9f75844SAndroid Build Coastguard Worker   bool RemoveRecvStream(uint32_t ssrc) override;
167*d9f75844SAndroid Build Coastguard Worker   void ResetUnsignaledRecvStream() override;
168*d9f75844SAndroid Build Coastguard Worker   void OnDemuxerCriteriaUpdatePending() override;
169*d9f75844SAndroid Build Coastguard Worker   void OnDemuxerCriteriaUpdateComplete() override;
170*d9f75844SAndroid Build Coastguard Worker   bool SetSink(uint32_t ssrc,
171*d9f75844SAndroid Build Coastguard Worker                rtc::VideoSinkInterface<webrtc::VideoFrame>* sink) override;
172*d9f75844SAndroid Build Coastguard Worker   void SetDefaultSink(
173*d9f75844SAndroid Build Coastguard Worker       rtc::VideoSinkInterface<webrtc::VideoFrame>* sink) override;
174*d9f75844SAndroid Build Coastguard Worker   void FillBitrateInfo(BandwidthEstimationInfo* bwe_info) override;
175*d9f75844SAndroid Build Coastguard Worker   bool GetStats(VideoMediaInfo* info) override;
176*d9f75844SAndroid Build Coastguard Worker 
177*d9f75844SAndroid Build Coastguard Worker   void OnPacketReceived(rtc::CopyOnWriteBuffer packet,
178*d9f75844SAndroid Build Coastguard Worker                         int64_t packet_time_us) override;
179*d9f75844SAndroid Build Coastguard Worker   void OnPacketSent(const rtc::SentPacket& sent_packet) override;
180*d9f75844SAndroid Build Coastguard Worker   void OnReadyToSend(bool ready) override;
181*d9f75844SAndroid Build Coastguard Worker   void OnNetworkRouteChanged(absl::string_view transport_name,
182*d9f75844SAndroid Build Coastguard Worker                              const rtc::NetworkRoute& network_route) override;
183*d9f75844SAndroid Build Coastguard Worker   void SetInterface(NetworkInterface* iface) override;
184*d9f75844SAndroid Build Coastguard Worker 
185*d9f75844SAndroid Build Coastguard Worker   // E2E Encrypted Video Frame API
186*d9f75844SAndroid Build Coastguard Worker   // Set a frame decryptor to a particular ssrc that will intercept all
187*d9f75844SAndroid Build Coastguard Worker   // incoming video frames and attempt to decrypt them before forwarding the
188*d9f75844SAndroid Build Coastguard Worker   // result.
189*d9f75844SAndroid Build Coastguard Worker   void SetFrameDecryptor(uint32_t ssrc,
190*d9f75844SAndroid Build Coastguard Worker                          rtc::scoped_refptr<webrtc::FrameDecryptorInterface>
191*d9f75844SAndroid Build Coastguard Worker                              frame_decryptor) override;
192*d9f75844SAndroid Build Coastguard Worker   // Set a frame encryptor to a particular ssrc that will intercept all
193*d9f75844SAndroid Build Coastguard Worker   // outgoing video frames and attempt to encrypt them and forward the result
194*d9f75844SAndroid Build Coastguard Worker   // to the packetizer.
195*d9f75844SAndroid Build Coastguard Worker   void SetFrameEncryptor(uint32_t ssrc,
196*d9f75844SAndroid Build Coastguard Worker                          rtc::scoped_refptr<webrtc::FrameEncryptorInterface>
197*d9f75844SAndroid Build Coastguard Worker                              frame_encryptor) override;
198*d9f75844SAndroid Build Coastguard Worker 
199*d9f75844SAndroid Build Coastguard Worker   // note: The encoder_selector object must remain valid for the lifetime of the
200*d9f75844SAndroid Build Coastguard Worker   // MediaChannel, unless replaced.
201*d9f75844SAndroid Build Coastguard Worker   void SetEncoderSelector(uint32_t ssrc,
202*d9f75844SAndroid Build Coastguard Worker                           webrtc::VideoEncoderFactory::EncoderSelectorInterface*
203*d9f75844SAndroid Build Coastguard Worker                               encoder_selector) override;
204*d9f75844SAndroid Build Coastguard Worker 
205*d9f75844SAndroid Build Coastguard Worker   void SetVideoCodecSwitchingEnabled(bool enabled) override;
206*d9f75844SAndroid Build Coastguard Worker 
207*d9f75844SAndroid Build Coastguard Worker   bool SetBaseMinimumPlayoutDelayMs(uint32_t ssrc, int delay_ms) override;
208*d9f75844SAndroid Build Coastguard Worker 
209*d9f75844SAndroid Build Coastguard Worker   absl::optional<int> GetBaseMinimumPlayoutDelayMs(
210*d9f75844SAndroid Build Coastguard Worker       uint32_t ssrc) const override;
211*d9f75844SAndroid Build Coastguard Worker 
212*d9f75844SAndroid Build Coastguard Worker   // Implemented for VideoMediaChannelTest.
sending()213*d9f75844SAndroid Build Coastguard Worker   bool sending() const {
214*d9f75844SAndroid Build Coastguard Worker     RTC_DCHECK_RUN_ON(&thread_checker_);
215*d9f75844SAndroid Build Coastguard Worker     return sending_;
216*d9f75844SAndroid Build Coastguard Worker   }
217*d9f75844SAndroid Build Coastguard Worker 
218*d9f75844SAndroid Build Coastguard Worker   absl::optional<uint32_t> GetDefaultReceiveStreamSsrc();
219*d9f75844SAndroid Build Coastguard Worker 
unsignaled_stream_params()220*d9f75844SAndroid Build Coastguard Worker   StreamParams unsignaled_stream_params() {
221*d9f75844SAndroid Build Coastguard Worker     RTC_DCHECK_RUN_ON(&thread_checker_);
222*d9f75844SAndroid Build Coastguard Worker     return unsignaled_stream_params_;
223*d9f75844SAndroid Build Coastguard Worker   }
224*d9f75844SAndroid Build Coastguard Worker 
225*d9f75844SAndroid Build Coastguard Worker   // AdaptReason is used for expressing why a WebRtcVideoSendStream request
226*d9f75844SAndroid Build Coastguard Worker   // a lower input frame size than the currently configured camera input frame
227*d9f75844SAndroid Build Coastguard Worker   // size. There can be more than one reason OR:ed together.
228*d9f75844SAndroid Build Coastguard Worker   enum AdaptReason {
229*d9f75844SAndroid Build Coastguard Worker     ADAPTREASON_NONE = 0,
230*d9f75844SAndroid Build Coastguard Worker     ADAPTREASON_CPU = 1,
231*d9f75844SAndroid Build Coastguard Worker     ADAPTREASON_BANDWIDTH = 2,
232*d9f75844SAndroid Build Coastguard Worker   };
233*d9f75844SAndroid Build Coastguard Worker 
234*d9f75844SAndroid Build Coastguard Worker   static constexpr int kDefaultQpMax = 56;
235*d9f75844SAndroid Build Coastguard Worker 
236*d9f75844SAndroid Build Coastguard Worker   std::vector<webrtc::RtpSource> GetSources(uint32_t ssrc) const override;
237*d9f75844SAndroid Build Coastguard Worker 
238*d9f75844SAndroid Build Coastguard Worker   // Take the buffered packets for `ssrcs` and feed them into DeliverPacket.
239*d9f75844SAndroid Build Coastguard Worker   // This method does nothing unless unknown_ssrc_packet_buffer_ is configured.
240*d9f75844SAndroid Build Coastguard Worker   void BackfillBufferedPackets(rtc::ArrayView<const uint32_t> ssrcs);
241*d9f75844SAndroid Build Coastguard Worker 
242*d9f75844SAndroid Build Coastguard Worker   // Implements webrtc::EncoderSwitchRequestCallback.
243*d9f75844SAndroid Build Coastguard Worker   void RequestEncoderFallback() override;
244*d9f75844SAndroid Build Coastguard Worker   void RequestEncoderSwitch(const webrtc::SdpVideoFormat& format,
245*d9f75844SAndroid Build Coastguard Worker                             bool allow_default_fallback) override;
246*d9f75844SAndroid Build Coastguard Worker 
247*d9f75844SAndroid Build Coastguard Worker   void SetRecordableEncodedFrameCallback(
248*d9f75844SAndroid Build Coastguard Worker       uint32_t ssrc,
249*d9f75844SAndroid Build Coastguard Worker       std::function<void(const webrtc::RecordableEncodedFrame&)> callback)
250*d9f75844SAndroid Build Coastguard Worker       override;
251*d9f75844SAndroid Build Coastguard Worker   void ClearRecordableEncodedFrameCallback(uint32_t ssrc) override;
252*d9f75844SAndroid Build Coastguard Worker   void RequestRecvKeyFrame(uint32_t ssrc) override;
253*d9f75844SAndroid Build Coastguard Worker   void GenerateSendKeyFrame(uint32_t ssrc,
254*d9f75844SAndroid Build Coastguard Worker                             const std::vector<std::string>& rids) override;
255*d9f75844SAndroid Build Coastguard Worker 
256*d9f75844SAndroid Build Coastguard Worker   void SetEncoderToPacketizerFrameTransformer(
257*d9f75844SAndroid Build Coastguard Worker       uint32_t ssrc,
258*d9f75844SAndroid Build Coastguard Worker       rtc::scoped_refptr<webrtc::FrameTransformerInterface> frame_transformer)
259*d9f75844SAndroid Build Coastguard Worker       override;
260*d9f75844SAndroid Build Coastguard Worker   void SetDepacketizerToDecoderFrameTransformer(
261*d9f75844SAndroid Build Coastguard Worker       uint32_t ssrc,
262*d9f75844SAndroid Build Coastguard Worker       rtc::scoped_refptr<webrtc::FrameTransformerInterface> frame_transformer)
263*d9f75844SAndroid Build Coastguard Worker       override;
264*d9f75844SAndroid Build Coastguard Worker 
265*d9f75844SAndroid Build Coastguard Worker  private:
266*d9f75844SAndroid Build Coastguard Worker   class WebRtcVideoReceiveStream;
267*d9f75844SAndroid Build Coastguard Worker 
268*d9f75844SAndroid Build Coastguard Worker   // Finds VideoReceiveStreamInterface corresponding to ssrc. Aware of
269*d9f75844SAndroid Build Coastguard Worker   // unsignalled ssrc handling.
270*d9f75844SAndroid Build Coastguard Worker   WebRtcVideoReceiveStream* FindReceiveStream(uint32_t ssrc)
271*d9f75844SAndroid Build Coastguard Worker       RTC_EXCLUSIVE_LOCKS_REQUIRED(thread_checker_);
272*d9f75844SAndroid Build Coastguard Worker 
273*d9f75844SAndroid Build Coastguard Worker   struct VideoCodecSettings {
274*d9f75844SAndroid Build Coastguard Worker     VideoCodecSettings();
275*d9f75844SAndroid Build Coastguard Worker 
276*d9f75844SAndroid Build Coastguard Worker     // Checks if all members of |*this| are equal to the corresponding members
277*d9f75844SAndroid Build Coastguard Worker     // of `other`.
278*d9f75844SAndroid Build Coastguard Worker     bool operator==(const VideoCodecSettings& other) const;
279*d9f75844SAndroid Build Coastguard Worker     bool operator!=(const VideoCodecSettings& other) const;
280*d9f75844SAndroid Build Coastguard Worker 
281*d9f75844SAndroid Build Coastguard Worker     // Checks if all members of `a`, except `flexfec_payload_type`, are equal
282*d9f75844SAndroid Build Coastguard Worker     // to the corresponding members of `b`.
283*d9f75844SAndroid Build Coastguard Worker     static bool EqualsDisregardingFlexfec(const VideoCodecSettings& a,
284*d9f75844SAndroid Build Coastguard Worker                                           const VideoCodecSettings& b);
285*d9f75844SAndroid Build Coastguard Worker 
286*d9f75844SAndroid Build Coastguard Worker     VideoCodec codec;
287*d9f75844SAndroid Build Coastguard Worker     webrtc::UlpfecConfig ulpfec;
288*d9f75844SAndroid Build Coastguard Worker     int flexfec_payload_type;  // -1 if absent.
289*d9f75844SAndroid Build Coastguard Worker     int rtx_payload_type;      // -1 if absent.
290*d9f75844SAndroid Build Coastguard Worker     int rtx_time;              // -1 if absent.
291*d9f75844SAndroid Build Coastguard Worker   };
292*d9f75844SAndroid Build Coastguard Worker 
293*d9f75844SAndroid Build Coastguard Worker   struct ChangedSendParameters {
294*d9f75844SAndroid Build Coastguard Worker     // These optionals are unset if not changed.
295*d9f75844SAndroid Build Coastguard Worker     absl::optional<VideoCodecSettings> send_codec;
296*d9f75844SAndroid Build Coastguard Worker     absl::optional<std::vector<VideoCodecSettings>> negotiated_codecs;
297*d9f75844SAndroid Build Coastguard Worker     absl::optional<std::vector<webrtc::RtpExtension>> rtp_header_extensions;
298*d9f75844SAndroid Build Coastguard Worker     absl::optional<std::string> mid;
299*d9f75844SAndroid Build Coastguard Worker     absl::optional<bool> extmap_allow_mixed;
300*d9f75844SAndroid Build Coastguard Worker     absl::optional<int> max_bandwidth_bps;
301*d9f75844SAndroid Build Coastguard Worker     absl::optional<bool> conference_mode;
302*d9f75844SAndroid Build Coastguard Worker     absl::optional<webrtc::RtcpMode> rtcp_mode;
303*d9f75844SAndroid Build Coastguard Worker   };
304*d9f75844SAndroid Build Coastguard Worker 
305*d9f75844SAndroid Build Coastguard Worker   struct ChangedRecvParameters {
306*d9f75844SAndroid Build Coastguard Worker     // These optionals are unset if not changed.
307*d9f75844SAndroid Build Coastguard Worker     absl::optional<std::vector<VideoCodecSettings>> codec_settings;
308*d9f75844SAndroid Build Coastguard Worker     absl::optional<std::vector<webrtc::RtpExtension>> rtp_header_extensions;
309*d9f75844SAndroid Build Coastguard Worker     // Keep track of the FlexFEC payload type separately from `codec_settings`.
310*d9f75844SAndroid Build Coastguard Worker     // This allows us to recreate the FlexfecReceiveStream separately from the
311*d9f75844SAndroid Build Coastguard Worker     // VideoReceiveStreamInterface when the FlexFEC payload type is changed.
312*d9f75844SAndroid Build Coastguard Worker     absl::optional<int> flexfec_payload_type;
313*d9f75844SAndroid Build Coastguard Worker   };
314*d9f75844SAndroid Build Coastguard Worker 
315*d9f75844SAndroid Build Coastguard Worker   bool GetChangedSendParameters(const VideoSendParameters& params,
316*d9f75844SAndroid Build Coastguard Worker                                 ChangedSendParameters* changed_params) const
317*d9f75844SAndroid Build Coastguard Worker       RTC_EXCLUSIVE_LOCKS_REQUIRED(thread_checker_);
318*d9f75844SAndroid Build Coastguard Worker   bool ApplyChangedParams(const ChangedSendParameters& changed_params);
319*d9f75844SAndroid Build Coastguard Worker   bool GetChangedRecvParameters(const VideoRecvParameters& params,
320*d9f75844SAndroid Build Coastguard Worker                                 ChangedRecvParameters* changed_params) const
321*d9f75844SAndroid Build Coastguard Worker       RTC_EXCLUSIVE_LOCKS_REQUIRED(thread_checker_);
322*d9f75844SAndroid Build Coastguard Worker 
323*d9f75844SAndroid Build Coastguard Worker   void ConfigureReceiverRtp(
324*d9f75844SAndroid Build Coastguard Worker       webrtc::VideoReceiveStreamInterface::Config* config,
325*d9f75844SAndroid Build Coastguard Worker       webrtc::FlexfecReceiveStream::Config* flexfec_config,
326*d9f75844SAndroid Build Coastguard Worker       const StreamParams& sp) const
327*d9f75844SAndroid Build Coastguard Worker       RTC_EXCLUSIVE_LOCKS_REQUIRED(thread_checker_);
328*d9f75844SAndroid Build Coastguard Worker   bool ValidateSendSsrcAvailability(const StreamParams& sp) const
329*d9f75844SAndroid Build Coastguard Worker       RTC_EXCLUSIVE_LOCKS_REQUIRED(thread_checker_);
330*d9f75844SAndroid Build Coastguard Worker   bool ValidateReceiveSsrcAvailability(const StreamParams& sp) const
331*d9f75844SAndroid Build Coastguard Worker       RTC_EXCLUSIVE_LOCKS_REQUIRED(thread_checker_);
332*d9f75844SAndroid Build Coastguard Worker   void DeleteReceiveStream(WebRtcVideoReceiveStream* stream)
333*d9f75844SAndroid Build Coastguard Worker       RTC_EXCLUSIVE_LOCKS_REQUIRED(thread_checker_);
334*d9f75844SAndroid Build Coastguard Worker 
335*d9f75844SAndroid Build Coastguard Worker   static std::string CodecSettingsVectorToString(
336*d9f75844SAndroid Build Coastguard Worker       const std::vector<VideoCodecSettings>& codecs);
337*d9f75844SAndroid Build Coastguard Worker 
338*d9f75844SAndroid Build Coastguard Worker   // Populates `rtx_associated_payload_types`, `raw_payload_types` and
339*d9f75844SAndroid Build Coastguard Worker   // `decoders` based on codec settings provided by `recv_codecs`.
340*d9f75844SAndroid Build Coastguard Worker   // `recv_codecs` must be non-empty and all other parameters must be empty.
341*d9f75844SAndroid Build Coastguard Worker   static void ExtractCodecInformation(
342*d9f75844SAndroid Build Coastguard Worker       rtc::ArrayView<const VideoCodecSettings> recv_codecs,
343*d9f75844SAndroid Build Coastguard Worker       std::map<int, int>& rtx_associated_payload_types,
344*d9f75844SAndroid Build Coastguard Worker       std::set<int>& raw_payload_types,
345*d9f75844SAndroid Build Coastguard Worker       std::vector<webrtc::VideoReceiveStreamInterface::Decoder>& decoders);
346*d9f75844SAndroid Build Coastguard Worker 
347*d9f75844SAndroid Build Coastguard Worker   // Called when the local ssrc changes. Sets `rtcp_receiver_report_ssrc_` and
348*d9f75844SAndroid Build Coastguard Worker   // updates the receive streams.
349*d9f75844SAndroid Build Coastguard Worker   void SetReceiverReportSsrc(uint32_t ssrc) RTC_RUN_ON(&thread_checker_);
350*d9f75844SAndroid Build Coastguard Worker 
351*d9f75844SAndroid Build Coastguard Worker   // Wrapper for the sender part.
352*d9f75844SAndroid Build Coastguard Worker   class WebRtcVideoSendStream {
353*d9f75844SAndroid Build Coastguard Worker    public:
354*d9f75844SAndroid Build Coastguard Worker     WebRtcVideoSendStream(
355*d9f75844SAndroid Build Coastguard Worker         webrtc::Call* call,
356*d9f75844SAndroid Build Coastguard Worker         const StreamParams& sp,
357*d9f75844SAndroid Build Coastguard Worker         webrtc::VideoSendStream::Config config,
358*d9f75844SAndroid Build Coastguard Worker         const VideoOptions& options,
359*d9f75844SAndroid Build Coastguard Worker         bool enable_cpu_overuse_detection,
360*d9f75844SAndroid Build Coastguard Worker         int max_bitrate_bps,
361*d9f75844SAndroid Build Coastguard Worker         const absl::optional<VideoCodecSettings>& codec_settings,
362*d9f75844SAndroid Build Coastguard Worker         const absl::optional<std::vector<webrtc::RtpExtension>>& rtp_extensions,
363*d9f75844SAndroid Build Coastguard Worker         const VideoSendParameters& send_params);
364*d9f75844SAndroid Build Coastguard Worker     ~WebRtcVideoSendStream();
365*d9f75844SAndroid Build Coastguard Worker 
366*d9f75844SAndroid Build Coastguard Worker     void SetSendParameters(const ChangedSendParameters& send_params);
367*d9f75844SAndroid Build Coastguard Worker     webrtc::RTCError SetRtpParameters(const webrtc::RtpParameters& parameters,
368*d9f75844SAndroid Build Coastguard Worker                                       webrtc::SetParametersCallback callback);
369*d9f75844SAndroid Build Coastguard Worker     webrtc::RtpParameters GetRtpParameters() const;
370*d9f75844SAndroid Build Coastguard Worker 
371*d9f75844SAndroid Build Coastguard Worker     void SetFrameEncryptor(
372*d9f75844SAndroid Build Coastguard Worker         rtc::scoped_refptr<webrtc::FrameEncryptorInterface> frame_encryptor);
373*d9f75844SAndroid Build Coastguard Worker 
374*d9f75844SAndroid Build Coastguard Worker     bool SetVideoSend(const VideoOptions* options,
375*d9f75844SAndroid Build Coastguard Worker                       rtc::VideoSourceInterface<webrtc::VideoFrame>* source);
376*d9f75844SAndroid Build Coastguard Worker 
377*d9f75844SAndroid Build Coastguard Worker     // note: The encoder_selector object must remain valid for the lifetime of
378*d9f75844SAndroid Build Coastguard Worker     // the MediaChannel, unless replaced.
379*d9f75844SAndroid Build Coastguard Worker     void SetEncoderSelector(
380*d9f75844SAndroid Build Coastguard Worker         webrtc::VideoEncoderFactory::EncoderSelectorInterface*
381*d9f75844SAndroid Build Coastguard Worker             encoder_selector);
382*d9f75844SAndroid Build Coastguard Worker 
383*d9f75844SAndroid Build Coastguard Worker     void SetSend(bool send);
384*d9f75844SAndroid Build Coastguard Worker 
385*d9f75844SAndroid Build Coastguard Worker     const std::vector<uint32_t>& GetSsrcs() const;
386*d9f75844SAndroid Build Coastguard Worker     // Returns per ssrc VideoSenderInfos. Useful for simulcast scenario.
387*d9f75844SAndroid Build Coastguard Worker     std::vector<VideoSenderInfo> GetPerLayerVideoSenderInfos(bool log_stats);
388*d9f75844SAndroid Build Coastguard Worker     // Aggregates per ssrc VideoSenderInfos to single VideoSenderInfo for
389*d9f75844SAndroid Build Coastguard Worker     // legacy reasons. Used in old GetStats API and track stats.
390*d9f75844SAndroid Build Coastguard Worker     VideoSenderInfo GetAggregatedVideoSenderInfo(
391*d9f75844SAndroid Build Coastguard Worker         const std::vector<VideoSenderInfo>& infos) const;
392*d9f75844SAndroid Build Coastguard Worker     void FillBitrateInfo(BandwidthEstimationInfo* bwe_info);
393*d9f75844SAndroid Build Coastguard Worker 
394*d9f75844SAndroid Build Coastguard Worker     void SetEncoderToPacketizerFrameTransformer(
395*d9f75844SAndroid Build Coastguard Worker         rtc::scoped_refptr<webrtc::FrameTransformerInterface>
396*d9f75844SAndroid Build Coastguard Worker             frame_transformer);
397*d9f75844SAndroid Build Coastguard Worker     void GenerateKeyFrame(const std::vector<std::string>& rids);
398*d9f75844SAndroid Build Coastguard Worker 
399*d9f75844SAndroid Build Coastguard Worker    private:
400*d9f75844SAndroid Build Coastguard Worker     // Parameters needed to reconstruct the underlying stream.
401*d9f75844SAndroid Build Coastguard Worker     // webrtc::VideoSendStream doesn't support setting a lot of options on the
402*d9f75844SAndroid Build Coastguard Worker     // fly, so when those need to be changed we tear down and reconstruct with
403*d9f75844SAndroid Build Coastguard Worker     // similar parameters depending on which options changed etc.
404*d9f75844SAndroid Build Coastguard Worker     struct VideoSendStreamParameters {
405*d9f75844SAndroid Build Coastguard Worker       VideoSendStreamParameters(
406*d9f75844SAndroid Build Coastguard Worker           webrtc::VideoSendStream::Config config,
407*d9f75844SAndroid Build Coastguard Worker           const VideoOptions& options,
408*d9f75844SAndroid Build Coastguard Worker           int max_bitrate_bps,
409*d9f75844SAndroid Build Coastguard Worker           const absl::optional<VideoCodecSettings>& codec_settings);
410*d9f75844SAndroid Build Coastguard Worker       webrtc::VideoSendStream::Config config;
411*d9f75844SAndroid Build Coastguard Worker       VideoOptions options;
412*d9f75844SAndroid Build Coastguard Worker       int max_bitrate_bps;
413*d9f75844SAndroid Build Coastguard Worker       bool conference_mode;
414*d9f75844SAndroid Build Coastguard Worker       absl::optional<VideoCodecSettings> codec_settings;
415*d9f75844SAndroid Build Coastguard Worker       // Sent resolutions + bitrates etc. by the underlying VideoSendStream,
416*d9f75844SAndroid Build Coastguard Worker       // typically changes when setting a new resolution or reconfiguring
417*d9f75844SAndroid Build Coastguard Worker       // bitrates.
418*d9f75844SAndroid Build Coastguard Worker       webrtc::VideoEncoderConfig encoder_config;
419*d9f75844SAndroid Build Coastguard Worker     };
420*d9f75844SAndroid Build Coastguard Worker 
421*d9f75844SAndroid Build Coastguard Worker     rtc::scoped_refptr<webrtc::VideoEncoderConfig::EncoderSpecificSettings>
422*d9f75844SAndroid Build Coastguard Worker     ConfigureVideoEncoderSettings(const VideoCodec& codec);
423*d9f75844SAndroid Build Coastguard Worker     void SetCodec(const VideoCodecSettings& codec);
424*d9f75844SAndroid Build Coastguard Worker     void RecreateWebRtcStream();
425*d9f75844SAndroid Build Coastguard Worker     webrtc::VideoEncoderConfig CreateVideoEncoderConfig(
426*d9f75844SAndroid Build Coastguard Worker         const VideoCodec& codec) const;
427*d9f75844SAndroid Build Coastguard Worker     void ReconfigureEncoder(webrtc::SetParametersCallback callback);
428*d9f75844SAndroid Build Coastguard Worker 
429*d9f75844SAndroid Build Coastguard Worker     // Calls Start or Stop according to whether or not `sending_` is true,
430*d9f75844SAndroid Build Coastguard Worker     // and whether or not the encoding in `rtp_parameters_` is active.
431*d9f75844SAndroid Build Coastguard Worker     void UpdateSendState();
432*d9f75844SAndroid Build Coastguard Worker 
433*d9f75844SAndroid Build Coastguard Worker     webrtc::DegradationPreference GetDegradationPreference() const
434*d9f75844SAndroid Build Coastguard Worker         RTC_EXCLUSIVE_LOCKS_REQUIRED(&thread_checker_);
435*d9f75844SAndroid Build Coastguard Worker 
436*d9f75844SAndroid Build Coastguard Worker     RTC_NO_UNIQUE_ADDRESS webrtc::SequenceChecker thread_checker_;
437*d9f75844SAndroid Build Coastguard Worker     webrtc::TaskQueueBase* const worker_thread_;
438*d9f75844SAndroid Build Coastguard Worker     const std::vector<uint32_t> ssrcs_ RTC_GUARDED_BY(&thread_checker_);
439*d9f75844SAndroid Build Coastguard Worker     const std::vector<SsrcGroup> ssrc_groups_ RTC_GUARDED_BY(&thread_checker_);
440*d9f75844SAndroid Build Coastguard Worker     webrtc::Call* const call_;
441*d9f75844SAndroid Build Coastguard Worker     const bool enable_cpu_overuse_detection_;
442*d9f75844SAndroid Build Coastguard Worker     rtc::VideoSourceInterface<webrtc::VideoFrame>* source_
443*d9f75844SAndroid Build Coastguard Worker         RTC_GUARDED_BY(&thread_checker_);
444*d9f75844SAndroid Build Coastguard Worker 
445*d9f75844SAndroid Build Coastguard Worker     webrtc::VideoSendStream* stream_ RTC_GUARDED_BY(&thread_checker_);
446*d9f75844SAndroid Build Coastguard Worker 
447*d9f75844SAndroid Build Coastguard Worker     // Contains settings that are the same for all streams in the MediaChannel,
448*d9f75844SAndroid Build Coastguard Worker     // such as codecs, header extensions, and the global bitrate limit for the
449*d9f75844SAndroid Build Coastguard Worker     // entire channel.
450*d9f75844SAndroid Build Coastguard Worker     VideoSendStreamParameters parameters_ RTC_GUARDED_BY(&thread_checker_);
451*d9f75844SAndroid Build Coastguard Worker     // Contains settings that are unique for each stream, such as max_bitrate.
452*d9f75844SAndroid Build Coastguard Worker     // Does *not* contain codecs, however.
453*d9f75844SAndroid Build Coastguard Worker     // TODO(skvlad): Move ssrcs_ and ssrc_groups_ into rtp_parameters_.
454*d9f75844SAndroid Build Coastguard Worker     // TODO(skvlad): Combine parameters_ and rtp_parameters_ once we have only
455*d9f75844SAndroid Build Coastguard Worker     // one stream per MediaChannel.
456*d9f75844SAndroid Build Coastguard Worker     webrtc::RtpParameters rtp_parameters_ RTC_GUARDED_BY(&thread_checker_);
457*d9f75844SAndroid Build Coastguard Worker 
458*d9f75844SAndroid Build Coastguard Worker     bool sending_ RTC_GUARDED_BY(&thread_checker_);
459*d9f75844SAndroid Build Coastguard Worker 
460*d9f75844SAndroid Build Coastguard Worker     // TODO(asapersson): investigate why setting
461*d9f75844SAndroid Build Coastguard Worker     // DegrationPreferences::MAINTAIN_RESOLUTION isn't sufficient to disable
462*d9f75844SAndroid Build Coastguard Worker     // downscaling everywhere in the pipeline.
463*d9f75844SAndroid Build Coastguard Worker     const bool disable_automatic_resize_;
464*d9f75844SAndroid Build Coastguard Worker   };
465*d9f75844SAndroid Build Coastguard Worker 
466*d9f75844SAndroid Build Coastguard Worker   // Wrapper for the receiver part, contains configs etc. that are needed to
467*d9f75844SAndroid Build Coastguard Worker   // reconstruct the underlying VideoReceiveStreamInterface.
468*d9f75844SAndroid Build Coastguard Worker   class WebRtcVideoReceiveStream
469*d9f75844SAndroid Build Coastguard Worker       : public rtc::VideoSinkInterface<webrtc::VideoFrame> {
470*d9f75844SAndroid Build Coastguard Worker    public:
471*d9f75844SAndroid Build Coastguard Worker     WebRtcVideoReceiveStream(
472*d9f75844SAndroid Build Coastguard Worker         WebRtcVideoChannel* channel,
473*d9f75844SAndroid Build Coastguard Worker         webrtc::Call* call,
474*d9f75844SAndroid Build Coastguard Worker         const StreamParams& sp,
475*d9f75844SAndroid Build Coastguard Worker         webrtc::VideoReceiveStreamInterface::Config config,
476*d9f75844SAndroid Build Coastguard Worker         bool default_stream,
477*d9f75844SAndroid Build Coastguard Worker         const std::vector<VideoCodecSettings>& recv_codecs,
478*d9f75844SAndroid Build Coastguard Worker         const webrtc::FlexfecReceiveStream::Config& flexfec_config);
479*d9f75844SAndroid Build Coastguard Worker     ~WebRtcVideoReceiveStream();
480*d9f75844SAndroid Build Coastguard Worker 
481*d9f75844SAndroid Build Coastguard Worker     webrtc::VideoReceiveStreamInterface& stream();
482*d9f75844SAndroid Build Coastguard Worker     // Return value may be nullptr.
483*d9f75844SAndroid Build Coastguard Worker     webrtc::FlexfecReceiveStream* flexfec_stream();
484*d9f75844SAndroid Build Coastguard Worker 
485*d9f75844SAndroid Build Coastguard Worker     const std::vector<uint32_t>& GetSsrcs() const;
486*d9f75844SAndroid Build Coastguard Worker 
487*d9f75844SAndroid Build Coastguard Worker     std::vector<webrtc::RtpSource> GetSources();
488*d9f75844SAndroid Build Coastguard Worker 
489*d9f75844SAndroid Build Coastguard Worker     // Does not return codecs, they are filled by the owning WebRtcVideoChannel.
490*d9f75844SAndroid Build Coastguard Worker     webrtc::RtpParameters GetRtpParameters() const;
491*d9f75844SAndroid Build Coastguard Worker 
492*d9f75844SAndroid Build Coastguard Worker     // TODO(deadbeef): Move these feedback parameters into the recv parameters.
493*d9f75844SAndroid Build Coastguard Worker     void SetFeedbackParameters(bool lntf_enabled,
494*d9f75844SAndroid Build Coastguard Worker                                bool nack_enabled,
495*d9f75844SAndroid Build Coastguard Worker                                bool transport_cc_enabled,
496*d9f75844SAndroid Build Coastguard Worker                                webrtc::RtcpMode rtcp_mode,
497*d9f75844SAndroid Build Coastguard Worker                                int rtx_time);
498*d9f75844SAndroid Build Coastguard Worker     void SetRecvParameters(const ChangedRecvParameters& recv_params);
499*d9f75844SAndroid Build Coastguard Worker 
500*d9f75844SAndroid Build Coastguard Worker     void OnFrame(const webrtc::VideoFrame& frame) override;
501*d9f75844SAndroid Build Coastguard Worker     bool IsDefaultStream() const;
502*d9f75844SAndroid Build Coastguard Worker 
503*d9f75844SAndroid Build Coastguard Worker     void SetFrameDecryptor(
504*d9f75844SAndroid Build Coastguard Worker         rtc::scoped_refptr<webrtc::FrameDecryptorInterface> frame_decryptor);
505*d9f75844SAndroid Build Coastguard Worker 
506*d9f75844SAndroid Build Coastguard Worker     bool SetBaseMinimumPlayoutDelayMs(int delay_ms);
507*d9f75844SAndroid Build Coastguard Worker 
508*d9f75844SAndroid Build Coastguard Worker     int GetBaseMinimumPlayoutDelayMs() const;
509*d9f75844SAndroid Build Coastguard Worker 
510*d9f75844SAndroid Build Coastguard Worker     void SetSink(rtc::VideoSinkInterface<webrtc::VideoFrame>* sink);
511*d9f75844SAndroid Build Coastguard Worker 
512*d9f75844SAndroid Build Coastguard Worker     VideoReceiverInfo GetVideoReceiverInfo(bool log_stats);
513*d9f75844SAndroid Build Coastguard Worker 
514*d9f75844SAndroid Build Coastguard Worker     void SetRecordableEncodedFrameCallback(
515*d9f75844SAndroid Build Coastguard Worker         std::function<void(const webrtc::RecordableEncodedFrame&)> callback);
516*d9f75844SAndroid Build Coastguard Worker     void ClearRecordableEncodedFrameCallback();
517*d9f75844SAndroid Build Coastguard Worker     void GenerateKeyFrame();
518*d9f75844SAndroid Build Coastguard Worker 
519*d9f75844SAndroid Build Coastguard Worker     void SetDepacketizerToDecoderFrameTransformer(
520*d9f75844SAndroid Build Coastguard Worker         rtc::scoped_refptr<webrtc::FrameTransformerInterface>
521*d9f75844SAndroid Build Coastguard Worker             frame_transformer);
522*d9f75844SAndroid Build Coastguard Worker 
523*d9f75844SAndroid Build Coastguard Worker     void SetLocalSsrc(uint32_t local_ssrc);
524*d9f75844SAndroid Build Coastguard Worker 
525*d9f75844SAndroid Build Coastguard Worker    private:
526*d9f75844SAndroid Build Coastguard Worker     // Attempts to reconfigure an already existing `flexfec_stream_`, create
527*d9f75844SAndroid Build Coastguard Worker     // one if the configuration is now complete or remove a flexfec stream
528*d9f75844SAndroid Build Coastguard Worker     // when disabled.
529*d9f75844SAndroid Build Coastguard Worker     void SetFlexFecPayload(int payload_type);
530*d9f75844SAndroid Build Coastguard Worker 
531*d9f75844SAndroid Build Coastguard Worker     void RecreateReceiveStream();
532*d9f75844SAndroid Build Coastguard Worker     void CreateReceiveStream();
533*d9f75844SAndroid Build Coastguard Worker     void StartReceiveStream();
534*d9f75844SAndroid Build Coastguard Worker 
535*d9f75844SAndroid Build Coastguard Worker     // Applies a new receive codecs configration to `config_`. Returns true
536*d9f75844SAndroid Build Coastguard Worker     // if the internal stream needs to be reconstructed, or false if no changes
537*d9f75844SAndroid Build Coastguard Worker     // were applied.
538*d9f75844SAndroid Build Coastguard Worker     bool ReconfigureCodecs(const std::vector<VideoCodecSettings>& recv_codecs);
539*d9f75844SAndroid Build Coastguard Worker 
540*d9f75844SAndroid Build Coastguard Worker     WebRtcVideoChannel* const channel_;
541*d9f75844SAndroid Build Coastguard Worker     webrtc::Call* const call_;
542*d9f75844SAndroid Build Coastguard Worker     const StreamParams stream_params_;
543*d9f75844SAndroid Build Coastguard Worker 
544*d9f75844SAndroid Build Coastguard Worker     // Both `stream_` and `flexfec_stream_` are managed by `this`. They are
545*d9f75844SAndroid Build Coastguard Worker     // destroyed by calling call_->DestroyVideoReceiveStream and
546*d9f75844SAndroid Build Coastguard Worker     // call_->DestroyFlexfecReceiveStream, respectively.
547*d9f75844SAndroid Build Coastguard Worker     webrtc::VideoReceiveStreamInterface* stream_;
548*d9f75844SAndroid Build Coastguard Worker     const bool default_stream_;
549*d9f75844SAndroid Build Coastguard Worker     webrtc::VideoReceiveStreamInterface::Config config_;
550*d9f75844SAndroid Build Coastguard Worker     webrtc::FlexfecReceiveStream::Config flexfec_config_;
551*d9f75844SAndroid Build Coastguard Worker     webrtc::FlexfecReceiveStream* flexfec_stream_;
552*d9f75844SAndroid Build Coastguard Worker 
553*d9f75844SAndroid Build Coastguard Worker     webrtc::Mutex sink_lock_;
554*d9f75844SAndroid Build Coastguard Worker     rtc::VideoSinkInterface<webrtc::VideoFrame>* sink_
555*d9f75844SAndroid Build Coastguard Worker         RTC_GUARDED_BY(sink_lock_);
556*d9f75844SAndroid Build Coastguard Worker     // Expands remote RTP timestamps to int64_t to be able to estimate how long
557*d9f75844SAndroid Build Coastguard Worker     // the stream has been running.
558*d9f75844SAndroid Build Coastguard Worker     rtc::TimestampWrapAroundHandler timestamp_wraparound_handler_
559*d9f75844SAndroid Build Coastguard Worker         RTC_GUARDED_BY(sink_lock_);
560*d9f75844SAndroid Build Coastguard Worker     int64_t first_frame_timestamp_ RTC_GUARDED_BY(sink_lock_);
561*d9f75844SAndroid Build Coastguard Worker     // Start NTP time is estimated as current remote NTP time (estimated from
562*d9f75844SAndroid Build Coastguard Worker     // RTCP) minus the elapsed time, as soon as remote NTP time is available.
563*d9f75844SAndroid Build Coastguard Worker     int64_t estimated_remote_start_ntp_time_ms_ RTC_GUARDED_BY(sink_lock_);
564*d9f75844SAndroid Build Coastguard Worker   };
565*d9f75844SAndroid Build Coastguard Worker 
566*d9f75844SAndroid Build Coastguard Worker   void Construct(webrtc::Call* call, WebRtcVideoEngine* engine);
567*d9f75844SAndroid Build Coastguard Worker 
568*d9f75844SAndroid Build Coastguard Worker   bool SendRtp(const uint8_t* data,
569*d9f75844SAndroid Build Coastguard Worker                size_t len,
570*d9f75844SAndroid Build Coastguard Worker                const webrtc::PacketOptions& options) override;
571*d9f75844SAndroid Build Coastguard Worker   bool SendRtcp(const uint8_t* data, size_t len) override;
572*d9f75844SAndroid Build Coastguard Worker 
573*d9f75844SAndroid Build Coastguard Worker   // Generate the list of codec parameters to pass down based on the negotiated
574*d9f75844SAndroid Build Coastguard Worker   // "codecs". Note that VideoCodecSettings correspond to concrete codecs like
575*d9f75844SAndroid Build Coastguard Worker   // VP8, VP9, H264 while VideoCodecs correspond also to "virtual" codecs like
576*d9f75844SAndroid Build Coastguard Worker   // RTX, ULPFEC, FLEXFEC.
577*d9f75844SAndroid Build Coastguard Worker   static std::vector<VideoCodecSettings> MapCodecs(
578*d9f75844SAndroid Build Coastguard Worker       const std::vector<VideoCodec>& codecs);
579*d9f75844SAndroid Build Coastguard Worker   // Get all codecs that are compatible with the receiver.
580*d9f75844SAndroid Build Coastguard Worker   std::vector<VideoCodecSettings> SelectSendVideoCodecs(
581*d9f75844SAndroid Build Coastguard Worker       const std::vector<VideoCodecSettings>& remote_mapped_codecs) const
582*d9f75844SAndroid Build Coastguard Worker       RTC_EXCLUSIVE_LOCKS_REQUIRED(thread_checker_);
583*d9f75844SAndroid Build Coastguard Worker 
584*d9f75844SAndroid Build Coastguard Worker   static bool NonFlexfecReceiveCodecsHaveChanged(
585*d9f75844SAndroid Build Coastguard Worker       std::vector<VideoCodecSettings> before,
586*d9f75844SAndroid Build Coastguard Worker       std::vector<VideoCodecSettings> after);
587*d9f75844SAndroid Build Coastguard Worker 
588*d9f75844SAndroid Build Coastguard Worker   void FillSenderStats(VideoMediaInfo* info, bool log_stats)
589*d9f75844SAndroid Build Coastguard Worker       RTC_EXCLUSIVE_LOCKS_REQUIRED(thread_checker_);
590*d9f75844SAndroid Build Coastguard Worker   void FillReceiverStats(VideoMediaInfo* info, bool log_stats)
591*d9f75844SAndroid Build Coastguard Worker       RTC_EXCLUSIVE_LOCKS_REQUIRED(thread_checker_);
592*d9f75844SAndroid Build Coastguard Worker   void FillBandwidthEstimationStats(const webrtc::Call::Stats& stats,
593*d9f75844SAndroid Build Coastguard Worker                                     VideoMediaInfo* info)
594*d9f75844SAndroid Build Coastguard Worker       RTC_EXCLUSIVE_LOCKS_REQUIRED(thread_checker_);
595*d9f75844SAndroid Build Coastguard Worker   void FillSendAndReceiveCodecStats(VideoMediaInfo* video_media_info)
596*d9f75844SAndroid Build Coastguard Worker       RTC_EXCLUSIVE_LOCKS_REQUIRED(thread_checker_);
597*d9f75844SAndroid Build Coastguard Worker 
598*d9f75844SAndroid Build Coastguard Worker   webrtc::TaskQueueBase* const worker_thread_;
599*d9f75844SAndroid Build Coastguard Worker   webrtc::ScopedTaskSafety task_safety_;
600*d9f75844SAndroid Build Coastguard Worker   RTC_NO_UNIQUE_ADDRESS webrtc::SequenceChecker network_thread_checker_;
601*d9f75844SAndroid Build Coastguard Worker   RTC_NO_UNIQUE_ADDRESS webrtc::SequenceChecker thread_checker_;
602*d9f75844SAndroid Build Coastguard Worker 
603*d9f75844SAndroid Build Coastguard Worker   uint32_t rtcp_receiver_report_ssrc_ RTC_GUARDED_BY(thread_checker_);
604*d9f75844SAndroid Build Coastguard Worker   bool sending_ RTC_GUARDED_BY(thread_checker_);
605*d9f75844SAndroid Build Coastguard Worker   webrtc::Call* const call_;
606*d9f75844SAndroid Build Coastguard Worker 
607*d9f75844SAndroid Build Coastguard Worker   DefaultUnsignalledSsrcHandler default_unsignalled_ssrc_handler_
608*d9f75844SAndroid Build Coastguard Worker       RTC_GUARDED_BY(thread_checker_);
609*d9f75844SAndroid Build Coastguard Worker   UnsignalledSsrcHandler* const unsignalled_ssrc_handler_
610*d9f75844SAndroid Build Coastguard Worker       RTC_GUARDED_BY(thread_checker_);
611*d9f75844SAndroid Build Coastguard Worker 
612*d9f75844SAndroid Build Coastguard Worker   // Delay for unsignaled streams, which may be set before the stream exists.
613*d9f75844SAndroid Build Coastguard Worker   int default_recv_base_minimum_delay_ms_ RTC_GUARDED_BY(thread_checker_) = 0;
614*d9f75844SAndroid Build Coastguard Worker 
615*d9f75844SAndroid Build Coastguard Worker   const MediaConfig::Video video_config_ RTC_GUARDED_BY(thread_checker_);
616*d9f75844SAndroid Build Coastguard Worker 
617*d9f75844SAndroid Build Coastguard Worker   // Using primary-ssrc (first ssrc) as key.
618*d9f75844SAndroid Build Coastguard Worker   std::map<uint32_t, WebRtcVideoSendStream*> send_streams_
619*d9f75844SAndroid Build Coastguard Worker       RTC_GUARDED_BY(thread_checker_);
620*d9f75844SAndroid Build Coastguard Worker   std::map<uint32_t, WebRtcVideoReceiveStream*> receive_streams_
621*d9f75844SAndroid Build Coastguard Worker       RTC_GUARDED_BY(thread_checker_);
622*d9f75844SAndroid Build Coastguard Worker   // When the channel and demuxer get reconfigured, there is a window of time
623*d9f75844SAndroid Build Coastguard Worker   // where we have to be prepared for packets arriving based on the old demuxer
624*d9f75844SAndroid Build Coastguard Worker   // criteria because the streams live on the worker thread and the demuxer
625*d9f75844SAndroid Build Coastguard Worker   // lives on the network thread. Because packets are posted from the network
626*d9f75844SAndroid Build Coastguard Worker   // thread to the worker thread, they can still be in-flight when streams are
627*d9f75844SAndroid Build Coastguard Worker   // reconfgured. This can happen when `demuxer_criteria_id_` and
628*d9f75844SAndroid Build Coastguard Worker   // `demuxer_criteria_completed_id_` don't match. During this time, we do not
629*d9f75844SAndroid Build Coastguard Worker   // want to create unsignalled receive streams and should instead drop the
630*d9f75844SAndroid Build Coastguard Worker   // packets. E.g:
631*d9f75844SAndroid Build Coastguard Worker   // * If RemoveRecvStream(old_ssrc) was recently called, there may be packets
632*d9f75844SAndroid Build Coastguard Worker   //   in-flight for that ssrc. This happens when a receiver becomes inactive.
633*d9f75844SAndroid Build Coastguard Worker   // * If we go from one to many m= sections, the demuxer may change from
634*d9f75844SAndroid Build Coastguard Worker   //   forwarding all packets to only forwarding the configured ssrcs, so there
635*d9f75844SAndroid Build Coastguard Worker   //   is a risk of receiving ssrcs for other, recently added m= sections.
636*d9f75844SAndroid Build Coastguard Worker   uint32_t demuxer_criteria_id_ RTC_GUARDED_BY(thread_checker_) = 0;
637*d9f75844SAndroid Build Coastguard Worker   uint32_t demuxer_criteria_completed_id_ RTC_GUARDED_BY(thread_checker_) = 0;
638*d9f75844SAndroid Build Coastguard Worker   absl::optional<int64_t> last_unsignalled_ssrc_creation_time_ms_
639*d9f75844SAndroid Build Coastguard Worker       RTC_GUARDED_BY(thread_checker_);
640*d9f75844SAndroid Build Coastguard Worker   std::set<uint32_t> send_ssrcs_ RTC_GUARDED_BY(thread_checker_);
641*d9f75844SAndroid Build Coastguard Worker   std::set<uint32_t> receive_ssrcs_ RTC_GUARDED_BY(thread_checker_);
642*d9f75844SAndroid Build Coastguard Worker 
643*d9f75844SAndroid Build Coastguard Worker   absl::optional<VideoCodecSettings> send_codec_
644*d9f75844SAndroid Build Coastguard Worker       RTC_GUARDED_BY(thread_checker_);
645*d9f75844SAndroid Build Coastguard Worker   std::vector<VideoCodecSettings> negotiated_codecs_
646*d9f75844SAndroid Build Coastguard Worker       RTC_GUARDED_BY(thread_checker_);
647*d9f75844SAndroid Build Coastguard Worker 
648*d9f75844SAndroid Build Coastguard Worker   std::vector<webrtc::RtpExtension> send_rtp_extensions_
649*d9f75844SAndroid Build Coastguard Worker       RTC_GUARDED_BY(thread_checker_);
650*d9f75844SAndroid Build Coastguard Worker 
651*d9f75844SAndroid Build Coastguard Worker   webrtc::VideoEncoderFactory* const encoder_factory_
652*d9f75844SAndroid Build Coastguard Worker       RTC_GUARDED_BY(thread_checker_);
653*d9f75844SAndroid Build Coastguard Worker   webrtc::VideoDecoderFactory* const decoder_factory_
654*d9f75844SAndroid Build Coastguard Worker       RTC_GUARDED_BY(thread_checker_);
655*d9f75844SAndroid Build Coastguard Worker   webrtc::VideoBitrateAllocatorFactory* const bitrate_allocator_factory_
656*d9f75844SAndroid Build Coastguard Worker       RTC_GUARDED_BY(thread_checker_);
657*d9f75844SAndroid Build Coastguard Worker   std::vector<VideoCodecSettings> recv_codecs_ RTC_GUARDED_BY(thread_checker_);
658*d9f75844SAndroid Build Coastguard Worker   std::vector<webrtc::RtpExtension> recv_rtp_extensions_
659*d9f75844SAndroid Build Coastguard Worker       RTC_GUARDED_BY(thread_checker_);
660*d9f75844SAndroid Build Coastguard Worker   // See reason for keeping track of the FlexFEC payload type separately in
661*d9f75844SAndroid Build Coastguard Worker   // comment in WebRtcVideoChannel::ChangedRecvParameters.
662*d9f75844SAndroid Build Coastguard Worker   int recv_flexfec_payload_type_ RTC_GUARDED_BY(thread_checker_);
663*d9f75844SAndroid Build Coastguard Worker   webrtc::BitrateConstraints bitrate_config_ RTC_GUARDED_BY(thread_checker_);
664*d9f75844SAndroid Build Coastguard Worker   // TODO(deadbeef): Don't duplicate information between
665*d9f75844SAndroid Build Coastguard Worker   // send_params/recv_params, rtp_extensions, options, etc.
666*d9f75844SAndroid Build Coastguard Worker   VideoSendParameters send_params_ RTC_GUARDED_BY(thread_checker_);
667*d9f75844SAndroid Build Coastguard Worker   VideoOptions default_send_options_ RTC_GUARDED_BY(thread_checker_);
668*d9f75844SAndroid Build Coastguard Worker   VideoRecvParameters recv_params_ RTC_GUARDED_BY(thread_checker_);
669*d9f75844SAndroid Build Coastguard Worker   int64_t last_stats_log_ms_ RTC_GUARDED_BY(thread_checker_);
670*d9f75844SAndroid Build Coastguard Worker   const bool discard_unknown_ssrc_packets_ RTC_GUARDED_BY(thread_checker_);
671*d9f75844SAndroid Build Coastguard Worker   // This is a stream param that comes from the remote description, but wasn't
672*d9f75844SAndroid Build Coastguard Worker   // signaled with any a=ssrc lines. It holds information that was signaled
673*d9f75844SAndroid Build Coastguard Worker   // before the unsignaled receive stream is created when the first packet is
674*d9f75844SAndroid Build Coastguard Worker   // received.
675*d9f75844SAndroid Build Coastguard Worker   StreamParams unsignaled_stream_params_ RTC_GUARDED_BY(thread_checker_);
676*d9f75844SAndroid Build Coastguard Worker   // Per peer connection crypto options that last for the lifetime of the peer
677*d9f75844SAndroid Build Coastguard Worker   // connection.
678*d9f75844SAndroid Build Coastguard Worker   const webrtc::CryptoOptions crypto_options_ RTC_GUARDED_BY(thread_checker_);
679*d9f75844SAndroid Build Coastguard Worker 
680*d9f75844SAndroid Build Coastguard Worker   // Optional frame transformer set on unsignaled streams.
681*d9f75844SAndroid Build Coastguard Worker   rtc::scoped_refptr<webrtc::FrameTransformerInterface>
682*d9f75844SAndroid Build Coastguard Worker       unsignaled_frame_transformer_ RTC_GUARDED_BY(thread_checker_);
683*d9f75844SAndroid Build Coastguard Worker 
684*d9f75844SAndroid Build Coastguard Worker   // Buffer for unhandled packets.
685*d9f75844SAndroid Build Coastguard Worker   std::unique_ptr<UnhandledPacketsBuffer> unknown_ssrc_packet_buffer_
686*d9f75844SAndroid Build Coastguard Worker       RTC_GUARDED_BY(thread_checker_);
687*d9f75844SAndroid Build Coastguard Worker 
688*d9f75844SAndroid Build Coastguard Worker   // TODO(bugs.webrtc.org/11341): Remove this and relevant PC API. Presence
689*d9f75844SAndroid Build Coastguard Worker   // of multiple negotiated codecs allows generic encoder fallback on failures.
690*d9f75844SAndroid Build Coastguard Worker   // Presence of EncoderSelector allows switching to specific encoders.
691*d9f75844SAndroid Build Coastguard Worker   bool allow_codec_switching_ = false;
692*d9f75844SAndroid Build Coastguard Worker };
693*d9f75844SAndroid Build Coastguard Worker 
694*d9f75844SAndroid Build Coastguard Worker }  // namespace cricket
695*d9f75844SAndroid Build Coastguard Worker 
696*d9f75844SAndroid Build Coastguard Worker #endif  // MEDIA_ENGINE_WEBRTC_VIDEO_ENGINE_H_
697