blob: 0ab70391fafc7d4e174e4f2fccda6ad02650e23f [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
mflodman@webrtc.orgecbeb2b2013-07-23 11:35:00 +000029// Super simple and temporary overuse logic. This will move to the application
30// as soon as the new API allows changing send codec on the fly.
31class ResolutionAdaptor : public webrtc::CpuOveruseObserver {
32 public:
33 ResolutionAdaptor(ViECodec* codec, int channel, size_t width, size_t height)
34 : codec_(codec),
35 channel_(channel),
36 max_width_(width),
37 max_height_(height) {}
38
39 virtual ~ResolutionAdaptor() {}
40
41 virtual void OveruseDetected() OVERRIDE {
42 VideoCodec codec;
43 if (codec_->GetSendCodec(channel_, codec) != 0)
44 return;
45
46 if (codec.width / 2 < min_width || codec.height / 2 < min_height)
47 return;
48
49 codec.width /= 2;
50 codec.height /= 2;
51 codec_->SetSendCodec(channel_, codec);
52 }
53
54 virtual void NormalUsage() OVERRIDE {
55 VideoCodec codec;
56 if (codec_->GetSendCodec(channel_, codec) != 0)
57 return;
58
59 if (codec.width * 2u > max_width_ || codec.height * 2u > max_height_)
60 return;
61
62 codec.width *= 2;
63 codec.height *= 2;
64 codec_->SetSendCodec(channel_, codec);
65 }
66
67 private:
68 // Temporary and arbitrary chosen minimum resolution.
69 static const size_t min_width = 160;
70 static const size_t min_height = 120;
71
72 ViECodec* codec_;
73 const int channel_;
74
75 const size_t max_width_;
76 const size_t max_height_;
77};
78
pbos@webrtc.org12d5ede2013-07-09 08:02:33 +000079VideoSendStream::VideoSendStream(newapi::Transport* transport,
mflodman@webrtc.orgecbeb2b2013-07-23 11:35:00 +000080 bool overuse_detection,
pbos@webrtc.org12d5ede2013-07-09 08:02:33 +000081 webrtc::VideoEngine* video_engine,
mflodman@webrtc.orge4d538a2013-12-13 09:40:45 +000082 const VideoSendStream::Config& config,
83 int base_channel)
pbos@webrtc.org8f2997c2013-11-14 08:58:14 +000084 : transport_adapter_(transport),
sprang@webrtc.org4a9843f2013-11-26 11:41:59 +000085 encoded_frame_proxy_(config.post_encode_callback),
pbos@webrtc.org8f2997c2013-11-14 08:58:14 +000086 codec_lock_(CriticalSectionWrapper::CreateCriticalSection()),
87 config_(config),
mflodman@webrtc.orge4d538a2013-12-13 09:40:45 +000088 external_codec_(NULL),
89 channel_(-1) {
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +000090 video_engine_base_ = ViEBase::GetInterface(video_engine);
mflodman@webrtc.orge4d538a2013-12-13 09:40:45 +000091 video_engine_base_->CreateChannel(channel_, base_channel);
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +000092 assert(channel_ != -1);
93
94 rtp_rtcp_ = ViERTP_RTCP::GetInterface(video_engine);
95 assert(rtp_rtcp_ != NULL);
96
pbos@webrtc.org8f2997c2013-11-14 08:58:14 +000097 assert(config_.rtp.ssrcs.size() > 0);
henrik.lundin@webrtc.orgd7d60c82013-11-21 14:05:40 +000098 if (config_.suspend_below_min_bitrate)
99 config_.pacing = true;
stefan@webrtc.orga0a91d82013-08-22 09:29:56 +0000100 rtp_rtcp_->SetTransmissionSmoothingStatus(channel_, config_.pacing);
pbos@webrtc.org905cebd2013-09-11 10:14:56 +0000101
102 for (size_t i = 0; i < config_.rtp.extensions.size(); ++i) {
103 const std::string& extension = config_.rtp.extensions[i].name;
104 int id = config_.rtp.extensions[i].id;
pbos@webrtc.org60108c22013-11-20 11:48:56 +0000105 if (extension == RtpExtension::kTOffset) {
pbos@webrtc.org905cebd2013-09-11 10:14:56 +0000106 if (rtp_rtcp_->SetSendTimestampOffsetStatus(channel_, true, id) != 0)
107 abort();
pbos@webrtc.org60108c22013-11-20 11:48:56 +0000108 } else if (extension == RtpExtension::kAbsSendTime) {
pbos@webrtc.orge22b7612013-09-11 19:00:39 +0000109 if (rtp_rtcp_->SetSendAbsoluteSendTimeStatus(channel_, true, id) != 0)
110 abort();
pbos@webrtc.org905cebd2013-09-11 10:14:56 +0000111 } else {
112 abort(); // Unsupported extension.
113 }
114 }
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000115
mflodman@webrtc.orgab6ccbc2013-12-13 16:36:28 +0000116 rtp_rtcp_->SetRembStatus(channel_, true, false);
117
pbos@webrtc.orgaa693dd2013-09-20 11:56:26 +0000118 // Enable NACK, FEC or both.
119 if (config_.rtp.fec.red_payload_type != -1) {
120 assert(config_.rtp.fec.ulpfec_payload_type != -1);
121 if (config_.rtp.nack.rtp_history_ms > 0) {
122 rtp_rtcp_->SetHybridNACKFECStatus(
123 channel_,
124 true,
125 static_cast<unsigned char>(config_.rtp.fec.red_payload_type),
126 static_cast<unsigned char>(config_.rtp.fec.ulpfec_payload_type));
127 } else {
128 rtp_rtcp_->SetFECStatus(
129 channel_,
130 true,
131 static_cast<unsigned char>(config_.rtp.fec.red_payload_type),
132 static_cast<unsigned char>(config_.rtp.fec.ulpfec_payload_type));
133 }
134 } else {
135 rtp_rtcp_->SetNACKStatus(channel_, config_.rtp.nack.rtp_history_ms > 0);
136 }
137
pbos@webrtc.orgdebc6722013-08-22 09:42:17 +0000138 char rtcp_cname[ViERTP_RTCP::KMaxRTCPCNameLength];
139 assert(config_.rtp.c_name.length() < ViERTP_RTCP::KMaxRTCPCNameLength);
140 strncpy(rtcp_cname, config_.rtp.c_name.c_str(), sizeof(rtcp_cname) - 1);
141 rtcp_cname[sizeof(rtcp_cname) - 1] = '\0';
142
143 rtp_rtcp_->SetRTCPCName(channel_, rtcp_cname);
144
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000145 capture_ = ViECapture::GetInterface(video_engine);
146 capture_->AllocateExternalCaptureDevice(capture_id_, external_capture_);
147 capture_->ConnectCaptureDevice(capture_id_, channel_);
148
149 network_ = ViENetwork::GetInterface(video_engine);
150 assert(network_ != NULL);
151
pbos@webrtc.org26d75f32013-09-18 11:52:42 +0000152 network_->RegisterSendTransport(channel_, transport_adapter_);
sprang@webrtc.org6133dd52013-10-16 13:29:14 +0000153 // 28 to match packet overhead in ModuleRtpRtcpImpl.
pbos@webrtc.orgb581c902013-10-28 16:32:01 +0000154 network_->SetMTU(channel_,
155 static_cast<unsigned int>(config_.rtp.max_packet_size + 28));
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000156
stefan@webrtc.orga0a91d82013-08-22 09:29:56 +0000157 if (config.encoder) {
158 external_codec_ = ViEExternalCodec::GetInterface(video_engine);
159 if (external_codec_->RegisterExternalSendCodec(
160 channel_, config.codec.plType, config.encoder,
161 config.internal_source) != 0) {
162 abort();
163 }
164 }
165
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000166 codec_ = ViECodec::GetInterface(video_engine);
pbos@webrtc.org8f2997c2013-11-14 08:58:14 +0000167 if (!SetCodec(config_.codec))
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000168 abort();
mflodman@webrtc.orgecbeb2b2013-07-23 11:35:00 +0000169
170 if (overuse_detection) {
171 overuse_observer_.reset(
172 new ResolutionAdaptor(codec_, channel_, config_.codec.width,
173 config_.codec.height));
174 video_engine_base_->RegisterCpuOveruseObserver(channel_,
pbos@webrtc.org905cebd2013-09-11 10:14:56 +0000175 overuse_observer_.get());
mflodman@webrtc.orgecbeb2b2013-07-23 11:35:00 +0000176 }
pbos@webrtc.org3ba57eb2013-10-21 10:34:43 +0000177
178 image_process_ = ViEImageProcess::GetInterface(video_engine);
179 image_process_->RegisterPreEncodeCallback(channel_,
180 config_.pre_encode_callback);
sprang@webrtc.org4a9843f2013-11-26 11:41:59 +0000181 if (config_.post_encode_callback) {
182 image_process_->RegisterPostEncodeImageCallback(channel_,
183 &encoded_frame_proxy_);
184 }
henrik.lundin@webrtc.orgce21c822013-10-23 11:04:57 +0000185
henrik.lundin@webrtc.org8fdf1912013-11-18 12:18:43 +0000186 if (config.suspend_below_min_bitrate) {
187 codec_->SuspendBelowMinBitrate(channel_);
henrik.lundin@webrtc.orgce21c822013-10-23 11:04:57 +0000188 }
sprang@webrtc.orgca723002014-01-07 09:54:34 +0000189
190 stats_proxy_.reset(
191 new SendStatisticsProxy(config, this));
192
193 rtp_rtcp_->RegisterSendChannelRtcpStatisticsCallback(channel_,
194 stats_proxy_.get());
195 rtp_rtcp_->RegisterSendChannelRtpStatisticsCallback(channel_,
196 stats_proxy_.get());
197 rtp_rtcp_->RegisterSendBitrateObserver(channel_, stats_proxy_.get());
198 rtp_rtcp_->RegisterSendFrameCountObserver(channel_, stats_proxy_.get());
199
200 codec_->RegisterEncoderObserver(channel_, *stats_proxy_);
201 capture_->RegisterObserver(capture_id_, *stats_proxy_);
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000202}
203
204VideoSendStream::~VideoSendStream() {
sprang@webrtc.orgca723002014-01-07 09:54:34 +0000205 capture_->DeregisterObserver(capture_id_);
206 codec_->DeregisterEncoderObserver(channel_);
207
208 rtp_rtcp_->DeregisterSendFrameCountObserver(channel_, stats_proxy_.get());
209 rtp_rtcp_->DeregisterSendBitrateObserver(channel_, stats_proxy_.get());
210 rtp_rtcp_->DeregisterSendChannelRtpStatisticsCallback(channel_,
211 stats_proxy_.get());
212 rtp_rtcp_->DeregisterSendChannelRtcpStatisticsCallback(channel_,
213 stats_proxy_.get());
214
pbos@webrtc.org3ba57eb2013-10-21 10:34:43 +0000215 image_process_->DeRegisterPreEncodeCallback(channel_);
216
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000217 network_->DeregisterSendTransport(channel_);
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000218
219 capture_->DisconnectCaptureDevice(channel_);
220 capture_->ReleaseCaptureDevice(capture_id_);
221
stefan@webrtc.orga0a91d82013-08-22 09:29:56 +0000222 if (external_codec_) {
223 external_codec_->DeRegisterExternalSendCodec(channel_,
224 config_.codec.plType);
225 }
226
pbos@webrtc.org3ba57eb2013-10-21 10:34:43 +0000227 video_engine_base_->DeleteChannel(channel_);
228
229 image_process_->Release();
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000230 video_engine_base_->Release();
231 capture_->Release();
232 codec_->Release();
stefan@webrtc.orga0a91d82013-08-22 09:29:56 +0000233 if (external_codec_)
234 external_codec_->Release();
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000235 network_->Release();
236 rtp_rtcp_->Release();
237}
238
pbos@webrtc.org7123a802013-12-11 16:26:16 +0000239void VideoSendStream::PutFrame(const I420VideoFrame& frame) {
240 input_frame_.CopyFrame(frame);
241 SwapFrame(&input_frame_);
242}
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000243
pbos@webrtc.org7123a802013-12-11 16:26:16 +0000244void VideoSendStream::SwapFrame(I420VideoFrame* frame) {
245 // TODO(pbos): Warn if frame is "too far" into the future, or too old. This
246 // would help detect if frame's being used without NTP.
247 // TO REVIEWER: Is there any good check for this? Should it be
248 // skipped?
249 if (frame != &input_frame_)
250 input_frame_.SwapFrame(frame);
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000251
pbos@webrtc.org7123a802013-12-11 16:26:16 +0000252 // TODO(pbos): Local rendering should not be done on the capture thread.
253 if (config_.local_renderer != NULL)
254 config_.local_renderer->RenderFrame(input_frame_, 0);
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000255
pbos@webrtc.org7123a802013-12-11 16:26:16 +0000256 external_capture_->SwapFrame(&input_frame_);
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000257}
258
pbos@webrtc.orgd8e92c92013-08-23 09:19:30 +0000259VideoSendStreamInput* VideoSendStream::Input() { return this; }
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000260
pbos@webrtc.org48cc9dc2013-11-20 11:36:47 +0000261void VideoSendStream::StartSending() {
pbos@webrtc.orgd9f91852013-05-23 12:37:11 +0000262 if (video_engine_base_->StartSend(channel_) != 0)
263 abort();
pbos@webrtc.orgbf9bc322013-08-05 12:01:36 +0000264 if (video_engine_base_->StartReceive(channel_) != 0)
265 abort();
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000266}
267
pbos@webrtc.org48cc9dc2013-11-20 11:36:47 +0000268void VideoSendStream::StopSending() {
pbos@webrtc.orgd9f91852013-05-23 12:37:11 +0000269 if (video_engine_base_->StopSend(channel_) != 0)
270 abort();
pbos@webrtc.orgbf9bc322013-08-05 12:01:36 +0000271 if (video_engine_base_->StopReceive(channel_) != 0)
272 abort();
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000273}
274
pbos@webrtc.org8f2997c2013-11-14 08:58:14 +0000275bool VideoSendStream::SetCodec(const VideoCodec& codec) {
pbos@webrtc.org9105cbd2013-11-28 11:59:31 +0000276 assert(config_.rtp.ssrcs.size() >= codec.numberOfSimulcastStreams);
pbos@webrtc.org8f2997c2013-11-14 08:58:14 +0000277
278 CriticalSectionScoped crit(codec_lock_.get());
279 if (codec_->SetSendCodec(channel_, codec) != 0)
280 return false;
281
pbos@webrtc.org9105cbd2013-11-28 11:59:31 +0000282 for (size_t i = 0; i < config_.rtp.ssrcs.size(); ++i) {
283 rtp_rtcp_->SetLocalSSRC(channel_,
284 config_.rtp.ssrcs[i],
285 kViEStreamTypeNormal,
286 static_cast<unsigned char>(i));
287 }
288
pbos@webrtc.org8f2997c2013-11-14 08:58:14 +0000289 config_.codec = codec;
pbos@webrtc.org9105cbd2013-11-28 11:59:31 +0000290 if (config_.rtp.rtx.ssrcs.empty())
291 return true;
292
293 // Set up RTX.
294 assert(config_.rtp.rtx.ssrcs.size() == config_.rtp.ssrcs.size());
295 for (size_t i = 0; i < config_.rtp.ssrcs.size(); ++i) {
296 rtp_rtcp_->SetLocalSSRC(channel_,
297 config_.rtp.rtx.ssrcs[i],
298 kViEStreamTypeRtx,
299 static_cast<unsigned char>(i));
300 }
301
302 if (config_.rtp.rtx.rtx_payload_type != 0) {
303 rtp_rtcp_->SetRtxSendPayloadType(channel_,
304 config_.rtp.rtx.rtx_payload_type);
305 }
306
pbos@webrtc.org8f2997c2013-11-14 08:58:14 +0000307 return true;
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000308}
309
pbos@webrtc.org8f2997c2013-11-14 08:58:14 +0000310VideoCodec VideoSendStream::GetCodec() {
311 CriticalSectionScoped crit(codec_lock_.get());
312 return config_.codec;
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000313}
314
pbos@webrtc.orgbf9bc322013-08-05 12:01:36 +0000315bool VideoSendStream::DeliverRtcp(const uint8_t* packet, size_t length) {
316 return network_->ReceivedRTCPPacket(
pbos@webrtc.org30c741a2013-08-05 13:25:51 +0000317 channel_, packet, static_cast<int>(length)) == 0;
pbos@webrtc.orgbf9bc322013-08-05 12:01:36 +0000318}
sprang@webrtc.orgca723002014-01-07 09:54:34 +0000319
320VideoSendStream::Stats VideoSendStream::GetStats() const {
321 return stats_proxy_->GetStats();
322}
323
324bool VideoSendStream::GetSendSideDelay(VideoSendStream::Stats* stats) {
325 return codec_->GetSendSideDelay(
326 channel_, &stats->avg_delay_ms, &stats->max_delay_ms);
327}
328
329std::string VideoSendStream::GetCName() {
330 char rtcp_cname[ViERTP_RTCP::KMaxRTCPCNameLength];
331 rtp_rtcp_->GetRTCPCName(channel_, rtcp_cname);
332 return rtcp_cname;
333}
334
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000335} // namespace internal
336} // namespace webrtc