blob: 5cacb565b0cd8a13ca48012118c552ff425a98ed [file] [log] [blame]
pbos@webrtc.orgdc8c8832013-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
pbos@webrtc.orgb581c902013-10-28 16:32:01 +000011#include "webrtc/video/video_send_stream.h"
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +000012
henrik.lundin@webrtc.orgce21c822013-10-23 11:04:57 +000013#include <string>
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +000014#include <vector>
15
16#include "webrtc/common_video/libyuv/include/webrtc_libyuv.h"
17#include "webrtc/video_engine/include/vie_base.h"
18#include "webrtc/video_engine/include/vie_capture.h"
19#include "webrtc/video_engine/include/vie_codec.h"
stefan@webrtc.orga0a91d82013-08-22 09:29:56 +000020#include "webrtc/video_engine/include/vie_external_codec.h"
pbos@webrtc.org3ba57eb2013-10-21 10:34:43 +000021#include "webrtc/video_engine/include/vie_image_process.h"
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +000022#include "webrtc/video_engine/include/vie_network.h"
23#include "webrtc/video_engine/include/vie_rtp_rtcp.h"
pbos@webrtc.orgb581c902013-10-28 16:32:01 +000024#include "webrtc/video_send_stream.h"
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +000025
26namespace webrtc {
27namespace internal {
28
pbos@webrtc.org12d5ede2013-07-09 08:02:33 +000029VideoSendStream::VideoSendStream(newapi::Transport* transport,
asapersson@webrtc.org4b1817f2014-01-31 10:05:07 +000030 CpuOveruseObserver* overuse_observer,
pbos@webrtc.org12d5ede2013-07-09 08:02:33 +000031 webrtc::VideoEngine* video_engine,
mflodman@webrtc.orge4d538a2013-12-13 09:40:45 +000032 const VideoSendStream::Config& config,
33 int base_channel)
pbos@webrtc.org8f2997c2013-11-14 08:58:14 +000034 : transport_adapter_(transport),
sprang@webrtc.org4a9843f2013-11-26 11:41:59 +000035 encoded_frame_proxy_(config.post_encode_callback),
pbos@webrtc.org8f2997c2013-11-14 08:58:14 +000036 codec_lock_(CriticalSectionWrapper::CreateCriticalSection()),
37 config_(config),
mflodman@webrtc.orge4d538a2013-12-13 09:40:45 +000038 external_codec_(NULL),
39 channel_(-1) {
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +000040 video_engine_base_ = ViEBase::GetInterface(video_engine);
mflodman@webrtc.orge4d538a2013-12-13 09:40:45 +000041 video_engine_base_->CreateChannel(channel_, base_channel);
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +000042 assert(channel_ != -1);
43
44 rtp_rtcp_ = ViERTP_RTCP::GetInterface(video_engine);
45 assert(rtp_rtcp_ != NULL);
46
pbos@webrtc.org8f2997c2013-11-14 08:58:14 +000047 assert(config_.rtp.ssrcs.size() > 0);
henrik.lundin@webrtc.orgd7d60c82013-11-21 14:05:40 +000048 if (config_.suspend_below_min_bitrate)
49 config_.pacing = true;
stefan@webrtc.orga0a91d82013-08-22 09:29:56 +000050 rtp_rtcp_->SetTransmissionSmoothingStatus(channel_, config_.pacing);
pbos@webrtc.org905cebd2013-09-11 10:14:56 +000051
pbos@webrtc.org9420a1f2014-03-13 12:52:27 +000052 assert(config_.rtp.min_transmit_bitrate_kbps >= 0);
53 rtp_rtcp_->SetMinTransmitBitrate(channel_,
54 config_.rtp.min_transmit_bitrate_kbps);
55
pbos@webrtc.org905cebd2013-09-11 10:14:56 +000056 for (size_t i = 0; i < config_.rtp.extensions.size(); ++i) {
57 const std::string& extension = config_.rtp.extensions[i].name;
58 int id = config_.rtp.extensions[i].id;
pbos@webrtc.org60108c22013-11-20 11:48:56 +000059 if (extension == RtpExtension::kTOffset) {
pbos@webrtc.org905cebd2013-09-11 10:14:56 +000060 if (rtp_rtcp_->SetSendTimestampOffsetStatus(channel_, true, id) != 0)
61 abort();
pbos@webrtc.org60108c22013-11-20 11:48:56 +000062 } else if (extension == RtpExtension::kAbsSendTime) {
pbos@webrtc.orge22b7612013-09-11 19:00:39 +000063 if (rtp_rtcp_->SetSendAbsoluteSendTimeStatus(channel_, true, id) != 0)
64 abort();
pbos@webrtc.org905cebd2013-09-11 10:14:56 +000065 } else {
66 abort(); // Unsupported extension.
67 }
68 }
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +000069
mflodman@webrtc.orgab6ccbc2013-12-13 16:36:28 +000070 rtp_rtcp_->SetRembStatus(channel_, true, false);
71
pbos@webrtc.orgaa693dd2013-09-20 11:56:26 +000072 // Enable NACK, FEC or both.
73 if (config_.rtp.fec.red_payload_type != -1) {
74 assert(config_.rtp.fec.ulpfec_payload_type != -1);
75 if (config_.rtp.nack.rtp_history_ms > 0) {
76 rtp_rtcp_->SetHybridNACKFECStatus(
77 channel_,
78 true,
79 static_cast<unsigned char>(config_.rtp.fec.red_payload_type),
80 static_cast<unsigned char>(config_.rtp.fec.ulpfec_payload_type));
81 } else {
82 rtp_rtcp_->SetFECStatus(
83 channel_,
84 true,
85 static_cast<unsigned char>(config_.rtp.fec.red_payload_type),
86 static_cast<unsigned char>(config_.rtp.fec.ulpfec_payload_type));
87 }
88 } else {
89 rtp_rtcp_->SetNACKStatus(channel_, config_.rtp.nack.rtp_history_ms > 0);
90 }
91
pbos@webrtc.orgdebc6722013-08-22 09:42:17 +000092 char rtcp_cname[ViERTP_RTCP::KMaxRTCPCNameLength];
93 assert(config_.rtp.c_name.length() < ViERTP_RTCP::KMaxRTCPCNameLength);
94 strncpy(rtcp_cname, config_.rtp.c_name.c_str(), sizeof(rtcp_cname) - 1);
95 rtcp_cname[sizeof(rtcp_cname) - 1] = '\0';
96
97 rtp_rtcp_->SetRTCPCName(channel_, rtcp_cname);
98
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +000099 capture_ = ViECapture::GetInterface(video_engine);
100 capture_->AllocateExternalCaptureDevice(capture_id_, external_capture_);
101 capture_->ConnectCaptureDevice(capture_id_, channel_);
102
103 network_ = ViENetwork::GetInterface(video_engine);
104 assert(network_ != NULL);
105
pbos@webrtc.org26d75f32013-09-18 11:52:42 +0000106 network_->RegisterSendTransport(channel_, transport_adapter_);
sprang@webrtc.org6133dd52013-10-16 13:29:14 +0000107 // 28 to match packet overhead in ModuleRtpRtcpImpl.
pbos@webrtc.orgb581c902013-10-28 16:32:01 +0000108 network_->SetMTU(channel_,
109 static_cast<unsigned int>(config_.rtp.max_packet_size + 28));
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000110
stefan@webrtc.orga0a91d82013-08-22 09:29:56 +0000111 if (config.encoder) {
112 external_codec_ = ViEExternalCodec::GetInterface(video_engine);
pbos@webrtc.orgc7667752014-01-24 09:30:53 +0000113 if (external_codec_->RegisterExternalSendCodec(channel_,
114 config.codec.plType,
115 config.encoder,
116 config.internal_source) !=
117 0) {
stefan@webrtc.orga0a91d82013-08-22 09:29:56 +0000118 abort();
119 }
120 }
121
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000122 codec_ = ViECodec::GetInterface(video_engine);
pbos@webrtc.org8f2997c2013-11-14 08:58:14 +0000123 if (!SetCodec(config_.codec))
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000124 abort();
mflodman@webrtc.orgecbeb2b2013-07-23 11:35:00 +0000125
asapersson@webrtc.org4b1817f2014-01-31 10:05:07 +0000126 if (overuse_observer) {
127 video_engine_base_->RegisterCpuOveruseObserver(channel_, overuse_observer);
mflodman@webrtc.orgecbeb2b2013-07-23 11:35:00 +0000128 }
pbos@webrtc.org3ba57eb2013-10-21 10:34:43 +0000129
130 image_process_ = ViEImageProcess::GetInterface(video_engine);
131 image_process_->RegisterPreEncodeCallback(channel_,
132 config_.pre_encode_callback);
sprang@webrtc.org4a9843f2013-11-26 11:41:59 +0000133 if (config_.post_encode_callback) {
134 image_process_->RegisterPostEncodeImageCallback(channel_,
135 &encoded_frame_proxy_);
136 }
henrik.lundin@webrtc.orgce21c822013-10-23 11:04:57 +0000137
henrik.lundin@webrtc.org0bf5a2f2014-03-06 07:19:28 +0000138 if (config_.suspend_below_min_bitrate) {
henrik.lundin@webrtc.org8fdf1912013-11-18 12:18:43 +0000139 codec_->SuspendBelowMinBitrate(channel_);
henrik.lundin@webrtc.orgce21c822013-10-23 11:04:57 +0000140 }
sprang@webrtc.orgca723002014-01-07 09:54:34 +0000141
pbos@webrtc.orgc7667752014-01-24 09:30:53 +0000142 stats_proxy_.reset(new SendStatisticsProxy(config, this));
sprang@webrtc.orgca723002014-01-07 09:54:34 +0000143
144 rtp_rtcp_->RegisterSendChannelRtcpStatisticsCallback(channel_,
145 stats_proxy_.get());
146 rtp_rtcp_->RegisterSendChannelRtpStatisticsCallback(channel_,
147 stats_proxy_.get());
148 rtp_rtcp_->RegisterSendBitrateObserver(channel_, stats_proxy_.get());
149 rtp_rtcp_->RegisterSendFrameCountObserver(channel_, stats_proxy_.get());
150
151 codec_->RegisterEncoderObserver(channel_, *stats_proxy_);
152 capture_->RegisterObserver(capture_id_, *stats_proxy_);
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000153}
154
155VideoSendStream::~VideoSendStream() {
sprang@webrtc.orgca723002014-01-07 09:54:34 +0000156 capture_->DeregisterObserver(capture_id_);
157 codec_->DeregisterEncoderObserver(channel_);
158
159 rtp_rtcp_->DeregisterSendFrameCountObserver(channel_, stats_proxy_.get());
160 rtp_rtcp_->DeregisterSendBitrateObserver(channel_, stats_proxy_.get());
161 rtp_rtcp_->DeregisterSendChannelRtpStatisticsCallback(channel_,
162 stats_proxy_.get());
163 rtp_rtcp_->DeregisterSendChannelRtcpStatisticsCallback(channel_,
164 stats_proxy_.get());
165
pbos@webrtc.org3ba57eb2013-10-21 10:34:43 +0000166 image_process_->DeRegisterPreEncodeCallback(channel_);
167
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000168 network_->DeregisterSendTransport(channel_);
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000169
170 capture_->DisconnectCaptureDevice(channel_);
171 capture_->ReleaseCaptureDevice(capture_id_);
172
stefan@webrtc.orga0a91d82013-08-22 09:29:56 +0000173 if (external_codec_) {
174 external_codec_->DeRegisterExternalSendCodec(channel_,
175 config_.codec.plType);
176 }
177
pbos@webrtc.org3ba57eb2013-10-21 10:34:43 +0000178 video_engine_base_->DeleteChannel(channel_);
179
180 image_process_->Release();
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000181 video_engine_base_->Release();
182 capture_->Release();
183 codec_->Release();
stefan@webrtc.orga0a91d82013-08-22 09:29:56 +0000184 if (external_codec_)
185 external_codec_->Release();
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000186 network_->Release();
187 rtp_rtcp_->Release();
188}
189
pbos@webrtc.org7123a802013-12-11 16:26:16 +0000190void VideoSendStream::PutFrame(const I420VideoFrame& frame) {
191 input_frame_.CopyFrame(frame);
192 SwapFrame(&input_frame_);
193}
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000194
pbos@webrtc.org7123a802013-12-11 16:26:16 +0000195void VideoSendStream::SwapFrame(I420VideoFrame* frame) {
196 // TODO(pbos): Warn if frame is "too far" into the future, or too old. This
197 // would help detect if frame's being used without NTP.
198 // TO REVIEWER: Is there any good check for this? Should it be
199 // skipped?
200 if (frame != &input_frame_)
201 input_frame_.SwapFrame(frame);
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000202
pbos@webrtc.org7123a802013-12-11 16:26:16 +0000203 // TODO(pbos): Local rendering should not be done on the capture thread.
204 if (config_.local_renderer != NULL)
205 config_.local_renderer->RenderFrame(input_frame_, 0);
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000206
pbos@webrtc.org7123a802013-12-11 16:26:16 +0000207 external_capture_->SwapFrame(&input_frame_);
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000208}
209
pbos@webrtc.orgd8e92c92013-08-23 09:19:30 +0000210VideoSendStreamInput* VideoSendStream::Input() { return this; }
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000211
pbos@webrtc.org48cc9dc2013-11-20 11:36:47 +0000212void VideoSendStream::StartSending() {
sprang@webrtc.org48ac0da2014-01-27 13:03:02 +0000213 transport_adapter_.Enable();
pbos@webrtc.orgdf9f0992014-01-10 18:47:32 +0000214 video_engine_base_->StartSend(channel_);
215 video_engine_base_->StartReceive(channel_);
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000216}
217
pbos@webrtc.org48cc9dc2013-11-20 11:36:47 +0000218void VideoSendStream::StopSending() {
pbos@webrtc.orgdf9f0992014-01-10 18:47:32 +0000219 video_engine_base_->StopSend(channel_);
220 video_engine_base_->StopReceive(channel_);
sprang@webrtc.org48ac0da2014-01-27 13:03:02 +0000221 transport_adapter_.Disable();
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000222}
223
pbos@webrtc.org8f2997c2013-11-14 08:58:14 +0000224bool VideoSendStream::SetCodec(const VideoCodec& codec) {
pbos@webrtc.org9105cbd2013-11-28 11:59:31 +0000225 assert(config_.rtp.ssrcs.size() >= codec.numberOfSimulcastStreams);
pbos@webrtc.org8f2997c2013-11-14 08:58:14 +0000226
227 CriticalSectionScoped crit(codec_lock_.get());
228 if (codec_->SetSendCodec(channel_, codec) != 0)
229 return false;
230
pbos@webrtc.org9105cbd2013-11-28 11:59:31 +0000231 for (size_t i = 0; i < config_.rtp.ssrcs.size(); ++i) {
232 rtp_rtcp_->SetLocalSSRC(channel_,
233 config_.rtp.ssrcs[i],
234 kViEStreamTypeNormal,
235 static_cast<unsigned char>(i));
236 }
237
pbos@webrtc.orgc7667752014-01-24 09:30:53 +0000238 if (&config_.codec != &codec)
239 config_.codec = codec;
240
pbos@webrtc.org9105cbd2013-11-28 11:59:31 +0000241 if (config_.rtp.rtx.ssrcs.empty())
242 return true;
243
244 // Set up RTX.
245 assert(config_.rtp.rtx.ssrcs.size() == config_.rtp.ssrcs.size());
246 for (size_t i = 0; i < config_.rtp.ssrcs.size(); ++i) {
247 rtp_rtcp_->SetLocalSSRC(channel_,
248 config_.rtp.rtx.ssrcs[i],
249 kViEStreamTypeRtx,
250 static_cast<unsigned char>(i));
251 }
252
pbos@webrtc.orgc7667752014-01-24 09:30:53 +0000253 if (config_.rtp.rtx.payload_type != 0)
254 rtp_rtcp_->SetRtxSendPayloadType(channel_, config_.rtp.rtx.payload_type);
pbos@webrtc.org9105cbd2013-11-28 11:59:31 +0000255
pbos@webrtc.org8f2997c2013-11-14 08:58:14 +0000256 return true;
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000257}
258
pbos@webrtc.org8f2997c2013-11-14 08:58:14 +0000259VideoCodec VideoSendStream::GetCodec() {
260 CriticalSectionScoped crit(codec_lock_.get());
261 return config_.codec;
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000262}
263
pbos@webrtc.orgbf9bc322013-08-05 12:01:36 +0000264bool VideoSendStream::DeliverRtcp(const uint8_t* packet, size_t length) {
265 return network_->ReceivedRTCPPacket(
pbos@webrtc.org30c741a2013-08-05 13:25:51 +0000266 channel_, packet, static_cast<int>(length)) == 0;
pbos@webrtc.orgbf9bc322013-08-05 12:01:36 +0000267}
sprang@webrtc.orgca723002014-01-07 09:54:34 +0000268
269VideoSendStream::Stats VideoSendStream::GetStats() const {
270 return stats_proxy_->GetStats();
271}
272
273bool VideoSendStream::GetSendSideDelay(VideoSendStream::Stats* stats) {
274 return codec_->GetSendSideDelay(
275 channel_, &stats->avg_delay_ms, &stats->max_delay_ms);
276}
277
278std::string VideoSendStream::GetCName() {
279 char rtcp_cname[ViERTP_RTCP::KMaxRTCPCNameLength];
280 rtp_rtcp_->GetRTCPCName(channel_, rtcp_cname);
281 return rtcp_cname;
282}
283
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000284} // namespace internal
285} // namespace webrtc