blob: ae2e4f46241dec28ab92369a02d982cbc2d5fe05 [file] [log] [blame]
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +02001/*
2 * Copyright 2018 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10#ifndef VIDEO_VIDEO_SEND_STREAM_IMPL_H_
11#define VIDEO_VIDEO_SEND_STREAM_IMPL_H_
12
13#include <map>
14#include <memory>
15#include <unordered_set>
16#include <vector>
17
18#include "call/bitrate_allocator.h"
Stefan Holmera2f15332018-07-11 17:11:31 +020019#include "call/payload_router.h"
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +020020#include "common_types.h" // NOLINT(build/include)
21#include "common_video/include/video_bitrate_allocator.h"
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +020022#include "modules/utility/include/process_thread.h"
23#include "modules/video_coding/utility/ivf_file_writer.h"
24#include "rtc_base/weak_ptr.h"
25#include "video/call_stats.h"
26#include "video/encoder_rtcp_feedback.h"
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +020027#include "video/send_delay_stats.h"
28#include "video/send_statistics_proxy.h"
29#include "video/video_send_stream.h"
30#include "video/video_stream_encoder.h"
31
32namespace webrtc {
33namespace internal {
34
35// VideoSendStreamImpl implements internal::VideoSendStream.
36// It is created and destroyed on |worker_queue|. The intent is to decrease the
37// need for locking and to ensure methods are called in sequence.
38// Public methods except |DeliverRtcp| must be called on |worker_queue|.
39// DeliverRtcp is called on the libjingle worker thread or a network thread.
40// An encoder may deliver frames through the EncodedImageCallback on an
41// arbitrary thread.
42class VideoSendStreamImpl : public webrtc::BitrateAllocatorObserver,
43 public webrtc::OverheadObserver,
44 public webrtc::VCMProtectionCallback,
45 public VideoStreamEncoder::EncoderSink,
46 public VideoBitrateAllocationObserver,
47 public webrtc::PacketFeedbackObserver {
48 public:
49 VideoSendStreamImpl(
50 SendStatisticsProxy* stats_proxy,
51 rtc::TaskQueue* worker_queue,
52 CallStats* call_stats,
53 RtpTransportControllerSendInterface* transport,
Sebastian Jansson652dc912018-04-19 17:09:15 +020054 BitrateAllocatorInterface* bitrate_allocator,
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +020055 SendDelayStats* send_delay_stats,
Sebastian Jansson652dc912018-04-19 17:09:15 +020056 VideoStreamEncoderInterface* video_stream_encoder,
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +020057 RtcEventLog* event_log,
58 const VideoSendStream::Config* config,
59 int initial_encoder_max_bitrate,
60 double initial_encoder_bitrate_priority,
61 std::map<uint32_t, RtpState> suspended_ssrcs,
62 std::map<uint32_t, RtpPayloadState> suspended_payload_states,
63 VideoEncoderConfig::ContentType content_type,
Stefan Holmerdbdb3a02018-07-17 16:03:46 +020064 std::unique_ptr<FecController> fec_controller);
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +020065 ~VideoSendStreamImpl() override;
66
67 // RegisterProcessThread register |module_process_thread| with those objects
68 // that use it. Registration has to happen on the thread were
69 // |module_process_thread| was created (libjingle's worker thread).
70 // TODO(perkj): Replace the use of |module_process_thread| with a TaskQueue,
71 // maybe |worker_queue|.
72 void RegisterProcessThread(ProcessThread* module_process_thread);
73 void DeRegisterProcessThread();
74
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +020075 bool DeliverRtcp(const uint8_t* packet, size_t length);
76 void UpdateActiveSimulcastLayers(const std::vector<bool> active_layers);
77 void Start();
78 void Stop();
79
Stefan Holmerdbdb3a02018-07-17 16:03:46 +020080 // TODO(holmer): Move these to RtpTransportControllerSend.
81 std::map<uint32_t, RtpState> GetRtpStates() const;
82
83 std::map<uint32_t, RtpPayloadState> GetRtpPayloadStates() const;
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +020084
85 void EnableEncodedFrameRecording(const std::vector<rtc::PlatformFile>& files,
86 size_t byte_limit);
87
88 void SetTransportOverhead(size_t transport_overhead_per_packet);
89
Danil Chapovalovb9b146c2018-06-15 12:28:07 +020090 absl::optional<float> configured_pacing_factor_;
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +020091
92 // From PacketFeedbackObserver.
93 void OnPacketAdded(uint32_t ssrc, uint16_t seq_num) override;
94 void OnPacketFeedbackVector(
95 const std::vector<PacketFeedback>& packet_feedback_vector) override;
96
97 private:
98 class CheckEncoderActivityTask;
99
100 // Implements BitrateAllocatorObserver.
101 uint32_t OnBitrateUpdated(uint32_t bitrate_bps,
102 uint8_t fraction_loss,
103 int64_t rtt,
104 int64_t probing_interval_ms) override;
105
106 // Implements webrtc::VCMProtectionCallback.
107 int ProtectionRequest(const FecProtectionParams* delta_params,
108 const FecProtectionParams* key_params,
109 uint32_t* sent_video_rate_bps,
110 uint32_t* sent_nack_rate_bps,
111 uint32_t* sent_fec_rate_bps) override;
112
113 // Implements OverheadObserver.
114 void OnOverheadChanged(size_t overhead_bytes_per_packet) override;
115
116 void OnEncoderConfigurationChanged(std::vector<VideoStream> streams,
117 int min_transmit_bitrate_bps) override;
118
119 // Implements EncodedImageCallback. The implementation routes encoded frames
120 // to the |payload_router_| and |config.pre_encode_callback| if set.
121 // Called on an arbitrary encoder callback thread.
122 EncodedImageCallback::Result OnEncodedImage(
123 const EncodedImage& encoded_image,
124 const CodecSpecificInfo* codec_specific_info,
125 const RTPFragmentationHeader* fragmentation) override;
126
127 // Implements VideoBitrateAllocationObserver.
Erik Språng566124a2018-04-23 12:32:22 +0200128 void OnBitrateAllocationUpdated(
129 const VideoBitrateAllocation& allocation) override;
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200130
131 // Starts monitoring and sends a keyframe.
132 void StartupVideoSendStream();
133 // Removes the bitrate observer, stops monitoring and notifies the video
134 // encoder of the bitrate update.
135 void StopVideoSendStream();
136
137 void ConfigureProtection();
138 void ConfigureSsrcs();
139 void SignalEncoderTimedOut();
140 void SignalEncoderActive();
141
142 const bool send_side_bwe_with_overhead_;
143
144 SendStatisticsProxy* const stats_proxy_;
145 const VideoSendStream::Config* const config_;
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200146
147 std::unique_ptr<FecController> fec_controller_;
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200148 rtc::TaskQueue* const worker_queue_;
149
150 rtc::CriticalSection encoder_activity_crit_sect_;
151 CheckEncoderActivityTask* check_encoder_activity_task_
152 RTC_GUARDED_BY(encoder_activity_crit_sect_);
153
154 CallStats* const call_stats_;
155 RtpTransportControllerSendInterface* const transport_;
Sebastian Jansson652dc912018-04-19 17:09:15 +0200156 BitrateAllocatorInterface* const bitrate_allocator_;
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200157
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200158 rtc::CriticalSection ivf_writers_crit_;
159 std::unique_ptr<IvfFileWriter>
160 file_writers_[kMaxSimulcastStreams] RTC_GUARDED_BY(ivf_writers_crit_);
161
162 int max_padding_bitrate_;
163 int encoder_min_bitrate_bps_;
164 uint32_t encoder_max_bitrate_bps_;
165 uint32_t encoder_target_rate_bps_;
166 double encoder_bitrate_priority_;
167 bool has_packet_feedback_;
168
Sebastian Jansson652dc912018-04-19 17:09:15 +0200169 VideoStreamEncoderInterface* const video_stream_encoder_;
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200170 EncoderRtcpFeedback encoder_feedback_;
171
172 RtcpBandwidthObserver* const bandwidth_observer_;
Stefan Holmerdbdb3a02018-07-17 16:03:46 +0200173 VideoRtpSenderInterface* const payload_router_;
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200174
175 // |weak_ptr_| to our self. This is used since we can not call
176 // |weak_ptr_factory_.GetWeakPtr| from multiple sequences but it is ok to copy
177 // an existing WeakPtr.
178 rtc::WeakPtr<VideoSendStreamImpl> weak_ptr_;
179 // |weak_ptr_factory_| must be declared last to make sure all WeakPtr's are
180 // invalidated before any other members are destroyed.
181 rtc::WeakPtrFactory<VideoSendStreamImpl> weak_ptr_factory_;
182
183 rtc::CriticalSection overhead_bytes_per_packet_crit_;
184 size_t overhead_bytes_per_packet_
185 RTC_GUARDED_BY(overhead_bytes_per_packet_crit_);
186 size_t transport_overhead_bytes_per_packet_;
187
188 std::unordered_set<uint16_t> feedback_packet_seq_num_set_;
189 std::vector<bool> loss_mask_vector_;
190};
191} // namespace internal
192} // namespace webrtc
193#endif // VIDEO_VIDEO_SEND_STREAM_IMPL_H_