blob: b67e9e5328b35d93f2df59997071bfb7369ed08d [file] [log] [blame]
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001/*
2 * Copyright (c) 2013 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
mflodman@webrtc.orgf3973e82013-12-13 09:40:45 +000011#ifndef WEBRTC_VIDEO_VIDEO_SEND_STREAM_H_
12#define WEBRTC_VIDEO_VIDEO_SEND_STREAM_H_
pbos@webrtc.org29d58392013-05-16 12:08:03 +000013
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +000014#include <map>
kwibergbfefb032016-05-01 14:53:46 -070015#include <memory>
pbos@webrtc.orgdde16f12014-08-05 23:35:43 +000016#include <vector>
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +000017
mflodman86aabb22016-03-11 15:44:32 +010018#include "webrtc/call/bitrate_allocator.h"
pbos14fe7082016-04-20 06:35:56 -070019#include "webrtc/base/criticalsection.h"
pbos@webrtc.org26c0c412014-09-03 16:17:12 +000020#include "webrtc/call.h"
pbos@webrtc.org29d58392013-05-16 12:08:03 +000021#include "webrtc/common_video/libyuv/include/webrtc_libyuv.h"
sprang@webrtc.org40709352013-11-26 11:41:59 +000022#include "webrtc/video/encoded_frame_callback_adapter.h"
Peter Boström8c66a002016-02-11 13:51:10 +010023#include "webrtc/video/encoder_state_feedback.h"
Peter Boström1d04ac62016-02-05 11:25:46 +010024#include "webrtc/video/payload_router.h"
asapersson35151f32016-05-02 23:44:01 -070025#include "webrtc/video/send_delay_stats.h"
sprang@webrtc.orgccd42842014-01-07 09:54:34 +000026#include "webrtc/video/send_statistics_proxy.h"
Peter Boström4b91bd02015-06-26 06:58:16 +020027#include "webrtc/video/video_capture_input.h"
Peter Boström8c66a002016-02-11 13:51:10 +010028#include "webrtc/video/vie_encoder.h"
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000029#include "webrtc/video_receive_stream.h"
30#include "webrtc/video_send_stream.h"
pbos@webrtc.org29d58392013-05-16 12:08:03 +000031
32namespace webrtc {
33
mflodman0e7e2592015-11-12 21:02:42 -080034class BitrateAllocator;
mflodmane3787022015-10-21 13:24:28 +020035class CallStats;
mflodman0c478b32015-10-21 15:52:16 +020036class CongestionController;
Peter Boströmf16fcbe2015-04-30 12:16:05 +020037class ProcessThread;
Peter Boström723ead82016-02-22 15:14:01 +010038class RtpRtcp;
Peter Boström59d91dc2015-04-27 17:24:33 +020039class ViEEncoder;
Stefan Holmer58c664c2016-02-08 14:31:30 +010040class VieRemb;
pbos@webrtc.org29d58392013-05-16 12:08:03 +000041
Peter Boströmcd5c25c2016-04-21 16:48:08 +020042namespace vcm {
43class VideoSender;
44} // namespace vcm
45
pbos@webrtc.org29d58392013-05-16 12:08:03 +000046namespace internal {
mflodman@webrtc.org6879c8a2013-07-23 11:35:00 +000047
solenberge5269742015-09-08 05:13:22 -070048class VideoSendStream : public webrtc::VideoSendStream,
mflodman86aabb22016-03-11 15:44:32 +010049 public webrtc::CpuOveruseObserver,
Per83d09102016-04-15 14:59:13 +020050 public webrtc::BitrateAllocatorObserver,
perkjbc75d972016-05-02 06:31:25 -070051 public webrtc::VCMProtectionCallback,
52 protected webrtc::EncodedImageCallback {
pbos@webrtc.org29d58392013-05-16 12:08:03 +000053 public:
solenberge5269742015-09-08 05:13:22 -070054 VideoSendStream(int num_cpu_cores,
Peter Boströmf16fcbe2015-04-30 12:16:05 +020055 ProcessThread* module_process_thread,
mflodmane3787022015-10-21 13:24:28 +020056 CallStats* call_stats,
mflodman0c478b32015-10-21 15:52:16 +020057 CongestionController* congestion_controller,
mflodman0e7e2592015-11-12 21:02:42 -080058 BitrateAllocator* bitrate_allocator,
asapersson35151f32016-05-02 23:44:01 -070059 SendDelayStats* send_delay_stats,
mflodman86aabb22016-03-11 15:44:32 +010060 VieRemb* remb,
mflodman@webrtc.orgf3973e82013-12-13 09:40:45 +000061 const VideoSendStream::Config& config,
pbos@webrtc.orgbbe0a852014-09-19 12:30:25 +000062 const VideoEncoderConfig& encoder_config,
Peter Boström45553ae2015-05-08 13:54:38 +020063 const std::map<uint32_t, RtpState>& suspended_ssrcs);
pbos@webrtc.org29d58392013-05-16 12:08:03 +000064
Jelena Marusiccd670222015-07-16 09:30:09 +020065 ~VideoSendStream() override;
pbos@webrtc.org29d58392013-05-16 12:08:03 +000066
pbos1ba8d392016-05-01 20:18:34 -070067 void SignalNetworkState(NetworkState state);
68 bool DeliverRtcp(const uint8_t* packet, size_t length);
pbos@webrtc.org29d58392013-05-16 12:08:03 +000069
Jelena Marusiccd670222015-07-16 09:30:09 +020070 // webrtc::VideoSendStream implementation.
pbos1ba8d392016-05-01 20:18:34 -070071 void Start() override;
72 void Stop() override;
Jelena Marusiccd670222015-07-16 09:30:09 +020073 VideoCaptureInput* Input() override;
Peter Boström905f8e72016-03-02 16:59:56 +010074 void ReconfigureVideoEncoder(const VideoEncoderConfig& config) override;
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000075 Stats GetStats() override;
sprang@webrtc.orgccd42842014-01-07 09:54:34 +000076
solenberge5269742015-09-08 05:13:22 -070077 // webrtc::CpuOveruseObserver implementation.
78 void OveruseDetected() override;
79 void NormalUsage() override;
80
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +000081 typedef std::map<uint32_t, RtpState> RtpStateMap;
82 RtpStateMap GetRtpStates() const;
83
mflodman0e7e2592015-11-12 21:02:42 -080084 int GetPaddingNeededBps() const;
pbos@webrtc.org2b19f062014-12-11 13:26:09 +000085
mflodman86aabb22016-03-11 15:44:32 +010086 // Implements BitrateAllocatorObserver.
87 void OnBitrateUpdated(uint32_t bitrate_bps,
88 uint8_t fraction_loss,
89 int64_t rtt) override;
90
Per83d09102016-04-15 14:59:13 +020091 // Implements webrtc::VCMProtectionCallback.
92 int ProtectionRequest(const FecProtectionParams* delta_params,
93 const FecProtectionParams* key_params,
94 uint32_t* sent_video_rate_bps,
95 uint32_t* sent_nack_rate_bps,
96 uint32_t* sent_fec_rate_bps) override;
97
pbos@webrtc.org29d58392013-05-16 12:08:03 +000098 private:
pbos14fe7082016-04-20 06:35:56 -070099 struct EncoderSettings {
100 VideoCodec video_codec;
101 int min_transmit_bitrate_bps;
perkjbc75d972016-05-02 06:31:25 -0700102 std::vector<VideoStream> streams;
pbos14fe7082016-04-20 06:35:56 -0700103 };
perkjbc75d972016-05-02 06:31:25 -0700104
105 // Implements EncodedImageCallback. The implementation routes encoded frames
106 // to the |payload_router_| and |config.pre_encode_callback| if set.
107 // Called on an arbitrary encoder callback thread.
108 int32_t Encoded(const EncodedImage& encoded_image,
109 const CodecSpecificInfo* codec_specific_info,
110 const RTPFragmentationHeader* fragmentation) override;
111
Peter Boströma4c76882016-03-03 16:29:02 +0100112 static bool EncoderThreadFunction(void* obj);
113 void EncoderProcess();
114
Per83d09102016-04-15 14:59:13 +0200115 void ConfigureProtection();
pbos@webrtc.orgbe9d2a42014-06-30 13:19:09 +0000116 void ConfigureSsrcs();
Peter Boström373284d2015-11-03 13:53:06 +0100117
118 SendStatisticsProxy stats_proxy_;
sprang@webrtc.org40709352013-11-26 11:41:59 +0000119 EncodedFrameCallbackAdapter encoded_frame_proxy_;
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +0000120 const VideoSendStream::Config config_;
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000121 std::map<uint32_t, RtpState> suspended_ssrcs_;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000122
Peter Boströmf16fcbe2015-04-30 12:16:05 +0200123 ProcessThread* const module_process_thread_;
mflodmane3787022015-10-21 13:24:28 +0200124 CallStats* const call_stats_;
mflodman0c478b32015-10-21 15:52:16 +0200125 CongestionController* const congestion_controller_;
mflodman86aabb22016-03-11 15:44:32 +0100126 BitrateAllocator* const bitrate_allocator_;
Stefan Holmer58c664c2016-02-08 14:31:30 +0100127 VieRemb* const remb_;
Peter Boström59d91dc2015-04-27 17:24:33 +0200128
Peter Boströma4c76882016-03-03 16:29:02 +0100129 rtc::PlatformThread encoder_thread_;
130 rtc::Event encoder_wakeup_event_;
131 volatile int stop_encoder_thread_;
pbos14fe7082016-04-20 06:35:56 -0700132 rtc::CriticalSection encoder_settings_crit_;
133 rtc::Optional<EncoderSettings> pending_encoder_settings_
134 GUARDED_BY(encoder_settings_crit_);
Peter Boströma4c76882016-03-03 16:29:02 +0100135
Peter Boströme4499152016-02-05 11:13:28 +0100136 OveruseFrameDetector overuse_detector_;
Peter Boström8c66a002016-02-11 13:51:10 +0100137 EncoderStateFeedback encoder_feedback_;
Peter Boström579e8322016-02-12 16:30:04 +0100138 ViEEncoder vie_encoder_;
Peter Boströmcd5c25c2016-04-21 16:48:08 +0200139 vcm::VideoSender* const video_sender_;
Per83d09102016-04-15 14:59:13 +0200140
141 const std::unique_ptr<RtcpBandwidthObserver> bandwidth_observer_;
142 // RtpRtcp modules, declared here as they use other members on construction.
Peter Boström723ead82016-02-22 15:14:01 +0100143 const std::vector<RtpRtcp*> rtp_rtcp_modules_;
Per83d09102016-04-15 14:59:13 +0200144 PayloadRouter payload_router_;
Peter Boström8c66a002016-02-11 13:51:10 +0100145 VideoCaptureInput input_;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000146};
147} // namespace internal
148} // namespace webrtc
149
mflodman@webrtc.orgf3973e82013-12-13 09:40:45 +0000150#endif // WEBRTC_VIDEO_VIDEO_SEND_STREAM_H_