blob: fa6a7a7c16d247269845d8f1aeccc2764c86af50 [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;
perkj600246e2016-05-04 11:26:51 -070037class IvfFileWriter;
Peter Boströmf16fcbe2015-04-30 12:16:05 +020038class ProcessThread;
Peter Boström723ead82016-02-22 15:14:01 +010039class RtpRtcp;
Peter Boström59d91dc2015-04-27 17:24:33 +020040class ViEEncoder;
Stefan Holmer58c664c2016-02-08 14:31:30 +010041class VieRemb;
pbos@webrtc.org29d58392013-05-16 12:08:03 +000042
Peter Boströmcd5c25c2016-04-21 16:48:08 +020043namespace vcm {
44class VideoSender;
45} // namespace vcm
46
pbos@webrtc.org29d58392013-05-16 12:08:03 +000047namespace internal {
mflodman@webrtc.org6879c8a2013-07-23 11:35:00 +000048
solenberge5269742015-09-08 05:13:22 -070049class VideoSendStream : public webrtc::VideoSendStream,
mflodman86aabb22016-03-11 15:44:32 +010050 public webrtc::CpuOveruseObserver,
Per83d09102016-04-15 14:59:13 +020051 public webrtc::BitrateAllocatorObserver,
perkjbc75d972016-05-02 06:31:25 -070052 public webrtc::VCMProtectionCallback,
53 protected webrtc::EncodedImageCallback {
pbos@webrtc.org29d58392013-05-16 12:08:03 +000054 public:
solenberge5269742015-09-08 05:13:22 -070055 VideoSendStream(int num_cpu_cores,
Peter Boströmf16fcbe2015-04-30 12:16:05 +020056 ProcessThread* module_process_thread,
mflodmane3787022015-10-21 13:24:28 +020057 CallStats* call_stats,
mflodman0c478b32015-10-21 15:52:16 +020058 CongestionController* congestion_controller,
mflodman0e7e2592015-11-12 21:02:42 -080059 BitrateAllocator* bitrate_allocator,
asapersson35151f32016-05-02 23:44:01 -070060 SendDelayStats* send_delay_stats,
mflodman86aabb22016-03-11 15:44:32 +010061 VieRemb* remb,
mflodman@webrtc.orgf3973e82013-12-13 09:40:45 +000062 const VideoSendStream::Config& config,
pbos@webrtc.orgbbe0a852014-09-19 12:30:25 +000063 const VideoEncoderConfig& encoder_config,
Peter Boström45553ae2015-05-08 13:54:38 +020064 const std::map<uint32_t, RtpState>& suspended_ssrcs);
pbos@webrtc.org29d58392013-05-16 12:08:03 +000065
Jelena Marusiccd670222015-07-16 09:30:09 +020066 ~VideoSendStream() override;
pbos@webrtc.org29d58392013-05-16 12:08:03 +000067
pbos1ba8d392016-05-01 20:18:34 -070068 void SignalNetworkState(NetworkState state);
69 bool DeliverRtcp(const uint8_t* packet, size_t length);
pbos@webrtc.org29d58392013-05-16 12:08:03 +000070
Jelena Marusiccd670222015-07-16 09:30:09 +020071 // webrtc::VideoSendStream implementation.
pbos1ba8d392016-05-01 20:18:34 -070072 void Start() override;
73 void Stop() override;
Jelena Marusiccd670222015-07-16 09:30:09 +020074 VideoCaptureInput* Input() override;
Peter Boström905f8e72016-03-02 16:59:56 +010075 void ReconfigureVideoEncoder(const VideoEncoderConfig& config) override;
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000076 Stats GetStats() override;
sprang@webrtc.orgccd42842014-01-07 09:54:34 +000077
solenberge5269742015-09-08 05:13:22 -070078 // webrtc::CpuOveruseObserver implementation.
79 void OveruseDetected() override;
80 void NormalUsage() override;
81
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +000082 typedef std::map<uint32_t, RtpState> RtpStateMap;
83 RtpStateMap GetRtpStates() const;
84
mflodman0e7e2592015-11-12 21:02:42 -080085 int GetPaddingNeededBps() const;
pbos@webrtc.org2b19f062014-12-11 13:26:09 +000086
mflodman86aabb22016-03-11 15:44:32 +010087 // Implements BitrateAllocatorObserver.
88 void OnBitrateUpdated(uint32_t bitrate_bps,
89 uint8_t fraction_loss,
90 int64_t rtt) override;
91
Per83d09102016-04-15 14:59:13 +020092 // Implements webrtc::VCMProtectionCallback.
93 int ProtectionRequest(const FecProtectionParams* delta_params,
94 const FecProtectionParams* key_params,
95 uint32_t* sent_video_rate_bps,
96 uint32_t* sent_nack_rate_bps,
97 uint32_t* sent_fec_rate_bps) override;
98
pbos@webrtc.org29d58392013-05-16 12:08:03 +000099 private:
pbos14fe7082016-04-20 06:35:56 -0700100 struct EncoderSettings {
101 VideoCodec video_codec;
102 int min_transmit_bitrate_bps;
perkjbc75d972016-05-02 06:31:25 -0700103 std::vector<VideoStream> streams;
pbos14fe7082016-04-20 06:35:56 -0700104 };
perkjbc75d972016-05-02 06:31:25 -0700105
106 // Implements EncodedImageCallback. The implementation routes encoded frames
107 // to the |payload_router_| and |config.pre_encode_callback| if set.
108 // Called on an arbitrary encoder callback thread.
109 int32_t Encoded(const EncodedImage& encoded_image,
110 const CodecSpecificInfo* codec_specific_info,
111 const RTPFragmentationHeader* fragmentation) override;
112
Peter Boströma4c76882016-03-03 16:29:02 +0100113 static bool EncoderThreadFunction(void* obj);
114 void EncoderProcess();
115
Per83d09102016-04-15 14:59:13 +0200116 void ConfigureProtection();
pbos@webrtc.orgbe9d2a42014-06-30 13:19:09 +0000117 void ConfigureSsrcs();
Peter Boström373284d2015-11-03 13:53:06 +0100118
119 SendStatisticsProxy stats_proxy_;
sprang@webrtc.org40709352013-11-26 11:41:59 +0000120 EncodedFrameCallbackAdapter encoded_frame_proxy_;
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +0000121 const VideoSendStream::Config config_;
pbos@webrtc.org2bb1bda2014-07-07 13:06:48 +0000122 std::map<uint32_t, RtpState> suspended_ssrcs_;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000123
Peter Boströmf16fcbe2015-04-30 12:16:05 +0200124 ProcessThread* const module_process_thread_;
mflodmane3787022015-10-21 13:24:28 +0200125 CallStats* const call_stats_;
mflodman0c478b32015-10-21 15:52:16 +0200126 CongestionController* const congestion_controller_;
mflodman86aabb22016-03-11 15:44:32 +0100127 BitrateAllocator* const bitrate_allocator_;
Stefan Holmer58c664c2016-02-08 14:31:30 +0100128 VieRemb* const remb_;
Peter Boström59d91dc2015-04-27 17:24:33 +0200129
perkj600246e2016-05-04 11:26:51 -0700130 static const bool kEnableFrameRecording = false;
131 static const int kMaxLayers = 3;
132 std::unique_ptr<IvfFileWriter> file_writers_[kMaxLayers];
133
Peter Boströma4c76882016-03-03 16:29:02 +0100134 rtc::PlatformThread encoder_thread_;
135 rtc::Event encoder_wakeup_event_;
136 volatile int stop_encoder_thread_;
pbos14fe7082016-04-20 06:35:56 -0700137 rtc::CriticalSection encoder_settings_crit_;
138 rtc::Optional<EncoderSettings> pending_encoder_settings_
139 GUARDED_BY(encoder_settings_crit_);
Peter Boströma4c76882016-03-03 16:29:02 +0100140
Peter Boströme4499152016-02-05 11:13:28 +0100141 OveruseFrameDetector overuse_detector_;
Peter Boström579e8322016-02-12 16:30:04 +0100142 ViEEncoder vie_encoder_;
perkj600246e2016-05-04 11:26:51 -0700143 EncoderStateFeedback encoder_feedback_;
Peter Boströmcd5c25c2016-04-21 16:48:08 +0200144 vcm::VideoSender* const video_sender_;
Per83d09102016-04-15 14:59:13 +0200145
146 const std::unique_ptr<RtcpBandwidthObserver> bandwidth_observer_;
147 // RtpRtcp modules, declared here as they use other members on construction.
Peter Boström723ead82016-02-22 15:14:01 +0100148 const std::vector<RtpRtcp*> rtp_rtcp_modules_;
Per83d09102016-04-15 14:59:13 +0200149 PayloadRouter payload_router_;
Peter Boström8c66a002016-02-11 13:51:10 +0100150 VideoCaptureInput input_;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000151};
152} // namespace internal
153} // namespace webrtc
154
mflodman@webrtc.orgf3973e82013-12-13 09:40:45 +0000155#endif // WEBRTC_VIDEO_VIDEO_SEND_STREAM_H_