blob: a7eebef73e56af874beaf00044a1e2ea1485f3c4 [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.orgf39df522014-03-19 08:43:57 +000024#include "webrtc/video_engine/vie_defines.h"
pbos@webrtc.orgb581c902013-10-28 16:32:01 +000025#include "webrtc/video_send_stream.h"
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +000026
27namespace webrtc {
28namespace internal {
29
pbos@webrtc.org12d5ede2013-07-09 08:02:33 +000030VideoSendStream::VideoSendStream(newapi::Transport* transport,
asapersson@webrtc.org4b1817f2014-01-31 10:05:07 +000031 CpuOveruseObserver* overuse_observer,
pbos@webrtc.org12d5ede2013-07-09 08:02:33 +000032 webrtc::VideoEngine* video_engine,
mflodman@webrtc.orge4d538a2013-12-13 09:40:45 +000033 const VideoSendStream::Config& config,
34 int base_channel)
pbos@webrtc.org8f2997c2013-11-14 08:58:14 +000035 : transport_adapter_(transport),
sprang@webrtc.org4a9843f2013-11-26 11:41:59 +000036 encoded_frame_proxy_(config.post_encode_callback),
pbos@webrtc.org8f2997c2013-11-14 08:58:14 +000037 codec_lock_(CriticalSectionWrapper::CreateCriticalSection()),
38 config_(config),
mflodman@webrtc.orge4d538a2013-12-13 09:40:45 +000039 external_codec_(NULL),
40 channel_(-1) {
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +000041 video_engine_base_ = ViEBase::GetInterface(video_engine);
mflodman@webrtc.orge4d538a2013-12-13 09:40:45 +000042 video_engine_base_->CreateChannel(channel_, base_channel);
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +000043 assert(channel_ != -1);
44
45 rtp_rtcp_ = ViERTP_RTCP::GetInterface(video_engine);
46 assert(rtp_rtcp_ != NULL);
47
pbos@webrtc.org8f2997c2013-11-14 08:58:14 +000048 assert(config_.rtp.ssrcs.size() > 0);
henrik.lundin@webrtc.orgd7d60c82013-11-21 14:05:40 +000049 if (config_.suspend_below_min_bitrate)
50 config_.pacing = true;
stefan@webrtc.orga0a91d82013-08-22 09:29:56 +000051 rtp_rtcp_->SetTransmissionSmoothingStatus(channel_, config_.pacing);
pbos@webrtc.org905cebd2013-09-11 10:14:56 +000052
pbos@webrtc.orgbef6e622014-03-19 10:59:52 +000053 assert(config_.rtp.min_transmit_bitrate_bps >= 0);
pbos@webrtc.org9420a1f2014-03-13 12:52:27 +000054 rtp_rtcp_->SetMinTransmitBitrate(channel_,
pbos@webrtc.orgbef6e622014-03-19 10:59:52 +000055 config_.rtp.min_transmit_bitrate_bps / 1000);
pbos@webrtc.org9420a1f2014-03-13 12:52:27 +000056
pbos@webrtc.org905cebd2013-09-11 10:14:56 +000057 for (size_t i = 0; i < config_.rtp.extensions.size(); ++i) {
58 const std::string& extension = config_.rtp.extensions[i].name;
59 int id = config_.rtp.extensions[i].id;
pbos@webrtc.org60108c22013-11-20 11:48:56 +000060 if (extension == RtpExtension::kTOffset) {
pbos@webrtc.org905cebd2013-09-11 10:14:56 +000061 if (rtp_rtcp_->SetSendTimestampOffsetStatus(channel_, true, id) != 0)
62 abort();
pbos@webrtc.org60108c22013-11-20 11:48:56 +000063 } else if (extension == RtpExtension::kAbsSendTime) {
pbos@webrtc.orge22b7612013-09-11 19:00:39 +000064 if (rtp_rtcp_->SetSendAbsoluteSendTimeStatus(channel_, true, id) != 0)
65 abort();
pbos@webrtc.org905cebd2013-09-11 10:14:56 +000066 } else {
67 abort(); // Unsupported extension.
68 }
69 }
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +000070
mflodman@webrtc.orgab6ccbc2013-12-13 16:36:28 +000071 rtp_rtcp_->SetRembStatus(channel_, true, false);
72
pbos@webrtc.orgaa693dd2013-09-20 11:56:26 +000073 // Enable NACK, FEC or both.
74 if (config_.rtp.fec.red_payload_type != -1) {
75 assert(config_.rtp.fec.ulpfec_payload_type != -1);
76 if (config_.rtp.nack.rtp_history_ms > 0) {
77 rtp_rtcp_->SetHybridNACKFECStatus(
78 channel_,
79 true,
80 static_cast<unsigned char>(config_.rtp.fec.red_payload_type),
81 static_cast<unsigned char>(config_.rtp.fec.ulpfec_payload_type));
82 } else {
83 rtp_rtcp_->SetFECStatus(
84 channel_,
85 true,
86 static_cast<unsigned char>(config_.rtp.fec.red_payload_type),
87 static_cast<unsigned char>(config_.rtp.fec.ulpfec_payload_type));
88 }
89 } else {
90 rtp_rtcp_->SetNACKStatus(channel_, config_.rtp.nack.rtp_history_ms > 0);
91 }
92
pbos@webrtc.orgdebc6722013-08-22 09:42:17 +000093 char rtcp_cname[ViERTP_RTCP::KMaxRTCPCNameLength];
94 assert(config_.rtp.c_name.length() < ViERTP_RTCP::KMaxRTCPCNameLength);
95 strncpy(rtcp_cname, config_.rtp.c_name.c_str(), sizeof(rtcp_cname) - 1);
96 rtcp_cname[sizeof(rtcp_cname) - 1] = '\0';
97
98 rtp_rtcp_->SetRTCPCName(channel_, rtcp_cname);
99
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000100 capture_ = ViECapture::GetInterface(video_engine);
101 capture_->AllocateExternalCaptureDevice(capture_id_, external_capture_);
102 capture_->ConnectCaptureDevice(capture_id_, channel_);
103
104 network_ = ViENetwork::GetInterface(video_engine);
105 assert(network_ != NULL);
106
pbos@webrtc.org26d75f32013-09-18 11:52:42 +0000107 network_->RegisterSendTransport(channel_, transport_adapter_);
sprang@webrtc.org6133dd52013-10-16 13:29:14 +0000108 // 28 to match packet overhead in ModuleRtpRtcpImpl.
pbos@webrtc.orgb581c902013-10-28 16:32:01 +0000109 network_->SetMTU(channel_,
110 static_cast<unsigned int>(config_.rtp.max_packet_size + 28));
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000111
pbos@webrtc.orgf39df522014-03-19 08:43:57 +0000112 assert(config.encoder_settings.encoder != NULL);
113 assert(config.encoder_settings.payload_type >= 0);
114 assert(config.encoder_settings.payload_type <= 127);
115 external_codec_ = ViEExternalCodec::GetInterface(video_engine);
116 if (external_codec_->RegisterExternalSendCodec(
117 channel_,
118 config.encoder_settings.payload_type,
119 config.encoder_settings.encoder,
120 false) != 0) {
121 abort();
stefan@webrtc.orga0a91d82013-08-22 09:29:56 +0000122 }
123
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000124 codec_ = ViECodec::GetInterface(video_engine);
pbos@webrtc.orgf39df522014-03-19 08:43:57 +0000125 if (!ReconfigureVideoEncoder(config_.encoder_settings.streams,
126 config_.encoder_settings.encoder_settings)) {
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000127 abort();
mflodman@webrtc.orgecbeb2b2013-07-23 11:35:00 +0000128 }
pbos@webrtc.org3ba57eb2013-10-21 10:34:43 +0000129
pbos@webrtc.orgf39df522014-03-19 08:43:57 +0000130 if (overuse_observer)
131 video_engine_base_->RegisterCpuOveruseObserver(channel_, overuse_observer);
132
pbos@webrtc.org3ba57eb2013-10-21 10:34:43 +0000133 image_process_ = ViEImageProcess::GetInterface(video_engine);
134 image_process_->RegisterPreEncodeCallback(channel_,
135 config_.pre_encode_callback);
sprang@webrtc.org4a9843f2013-11-26 11:41:59 +0000136 if (config_.post_encode_callback) {
137 image_process_->RegisterPostEncodeImageCallback(channel_,
138 &encoded_frame_proxy_);
139 }
henrik.lundin@webrtc.orgce21c822013-10-23 11:04:57 +0000140
henrik.lundin@webrtc.org0bf5a2f2014-03-06 07:19:28 +0000141 if (config_.suspend_below_min_bitrate) {
henrik.lundin@webrtc.org8fdf1912013-11-18 12:18:43 +0000142 codec_->SuspendBelowMinBitrate(channel_);
henrik.lundin@webrtc.orgce21c822013-10-23 11:04:57 +0000143 }
sprang@webrtc.orgca723002014-01-07 09:54:34 +0000144
pbos@webrtc.orgc7667752014-01-24 09:30:53 +0000145 stats_proxy_.reset(new SendStatisticsProxy(config, this));
sprang@webrtc.orgca723002014-01-07 09:54:34 +0000146
147 rtp_rtcp_->RegisterSendChannelRtcpStatisticsCallback(channel_,
148 stats_proxy_.get());
149 rtp_rtcp_->RegisterSendChannelRtpStatisticsCallback(channel_,
150 stats_proxy_.get());
151 rtp_rtcp_->RegisterSendBitrateObserver(channel_, stats_proxy_.get());
152 rtp_rtcp_->RegisterSendFrameCountObserver(channel_, stats_proxy_.get());
153
154 codec_->RegisterEncoderObserver(channel_, *stats_proxy_);
155 capture_->RegisterObserver(capture_id_, *stats_proxy_);
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000156}
157
158VideoSendStream::~VideoSendStream() {
sprang@webrtc.orgca723002014-01-07 09:54:34 +0000159 capture_->DeregisterObserver(capture_id_);
160 codec_->DeregisterEncoderObserver(channel_);
161
162 rtp_rtcp_->DeregisterSendFrameCountObserver(channel_, stats_proxy_.get());
163 rtp_rtcp_->DeregisterSendBitrateObserver(channel_, stats_proxy_.get());
164 rtp_rtcp_->DeregisterSendChannelRtpStatisticsCallback(channel_,
165 stats_proxy_.get());
166 rtp_rtcp_->DeregisterSendChannelRtcpStatisticsCallback(channel_,
167 stats_proxy_.get());
168
pbos@webrtc.org3ba57eb2013-10-21 10:34:43 +0000169 image_process_->DeRegisterPreEncodeCallback(channel_);
170
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000171 network_->DeregisterSendTransport(channel_);
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000172
173 capture_->DisconnectCaptureDevice(channel_);
174 capture_->ReleaseCaptureDevice(capture_id_);
175
pbos@webrtc.orgf39df522014-03-19 08:43:57 +0000176 external_codec_->DeRegisterExternalSendCodec(
177 channel_, config_.encoder_settings.payload_type);
stefan@webrtc.orga0a91d82013-08-22 09:29:56 +0000178
pbos@webrtc.org3ba57eb2013-10-21 10:34:43 +0000179 video_engine_base_->DeleteChannel(channel_);
180
181 image_process_->Release();
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000182 video_engine_base_->Release();
183 capture_->Release();
184 codec_->Release();
stefan@webrtc.orga0a91d82013-08-22 09:29:56 +0000185 if (external_codec_)
186 external_codec_->Release();
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000187 network_->Release();
188 rtp_rtcp_->Release();
189}
190
pbos@webrtc.org7123a802013-12-11 16:26:16 +0000191void VideoSendStream::PutFrame(const I420VideoFrame& frame) {
192 input_frame_.CopyFrame(frame);
193 SwapFrame(&input_frame_);
194}
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000195
pbos@webrtc.org7123a802013-12-11 16:26:16 +0000196void VideoSendStream::SwapFrame(I420VideoFrame* frame) {
197 // TODO(pbos): Warn if frame is "too far" into the future, or too old. This
198 // would help detect if frame's being used without NTP.
199 // TO REVIEWER: Is there any good check for this? Should it be
200 // skipped?
201 if (frame != &input_frame_)
202 input_frame_.SwapFrame(frame);
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000203
pbos@webrtc.org7123a802013-12-11 16:26:16 +0000204 // TODO(pbos): Local rendering should not be done on the capture thread.
205 if (config_.local_renderer != NULL)
206 config_.local_renderer->RenderFrame(input_frame_, 0);
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000207
pbos@webrtc.org7123a802013-12-11 16:26:16 +0000208 external_capture_->SwapFrame(&input_frame_);
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000209}
210
pbos@webrtc.orgd8e92c92013-08-23 09:19:30 +0000211VideoSendStreamInput* VideoSendStream::Input() { return this; }
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000212
pbos@webrtc.org48cc9dc2013-11-20 11:36:47 +0000213void VideoSendStream::StartSending() {
sprang@webrtc.org48ac0da2014-01-27 13:03:02 +0000214 transport_adapter_.Enable();
pbos@webrtc.orgdf9f0992014-01-10 18:47:32 +0000215 video_engine_base_->StartSend(channel_);
216 video_engine_base_->StartReceive(channel_);
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000217}
218
pbos@webrtc.org48cc9dc2013-11-20 11:36:47 +0000219void VideoSendStream::StopSending() {
pbos@webrtc.orgdf9f0992014-01-10 18:47:32 +0000220 video_engine_base_->StopSend(channel_);
221 video_engine_base_->StopReceive(channel_);
sprang@webrtc.org48ac0da2014-01-27 13:03:02 +0000222 transport_adapter_.Disable();
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000223}
224
pbos@webrtc.orgf39df522014-03-19 08:43:57 +0000225bool VideoSendStream::ReconfigureVideoEncoder(
226 const std::vector<VideoStream>& streams,
227 void* encoder_settings) {
228 assert(!streams.empty());
229 assert(config_.rtp.ssrcs.size() >= streams.size());
230 // TODO(pbos): Wire encoder_settings.
231 assert(encoder_settings == NULL);
pbos@webrtc.org8f2997c2013-11-14 08:58:14 +0000232
pbos@webrtc.orgf39df522014-03-19 08:43:57 +0000233 // VideoStreams in config_.encoder_settings need to be locked.
pbos@webrtc.org8f2997c2013-11-14 08:58:14 +0000234 CriticalSectionScoped crit(codec_lock_.get());
pbos@webrtc.orgf39df522014-03-19 08:43:57 +0000235
236 VideoCodec video_codec;
237 memset(&video_codec, 0, sizeof(video_codec));
238 video_codec.codecType =
239 (config_.encoder_settings.payload_name == "VP8" ? kVideoCodecVP8
240 : kVideoCodecGeneric);
241
242 if (video_codec.codecType == kVideoCodecVP8) {
243 video_codec.codecSpecific.VP8.resilience = kResilientStream;
244 video_codec.codecSpecific.VP8.numberOfTemporalLayers = 1;
245 video_codec.codecSpecific.VP8.denoisingOn = true;
246 video_codec.codecSpecific.VP8.errorConcealmentOn = false;
247 video_codec.codecSpecific.VP8.automaticResizeOn = false;
248 video_codec.codecSpecific.VP8.frameDroppingOn = true;
249 video_codec.codecSpecific.VP8.keyFrameInterval = 3000;
250 }
251
252 strncpy(video_codec.plName,
253 config_.encoder_settings.payload_name.c_str(),
254 kPayloadNameSize - 1);
255 video_codec.plName[kPayloadNameSize - 1] = '\0';
256 video_codec.plType = config_.encoder_settings.payload_type;
257 video_codec.numberOfSimulcastStreams =
258 static_cast<unsigned char>(streams.size());
259 video_codec.minBitrate = streams[0].min_bitrate_bps / 1000;
260 assert(streams.size() <= kMaxSimulcastStreams);
261 for (size_t i = 0; i < streams.size(); ++i) {
262 SimulcastStream* sim_stream = &video_codec.simulcastStream[i];
263 assert(streams[i].width > 0);
264 assert(streams[i].height > 0);
265 assert(streams[i].max_framerate > 0);
266 // Different framerates not supported per stream at the moment.
267 assert(streams[i].max_framerate == streams[0].max_framerate);
268 assert(streams[i].min_bitrate_bps >= 0);
269 assert(streams[i].target_bitrate_bps >= streams[i].min_bitrate_bps);
270 assert(streams[i].max_bitrate_bps >= streams[i].target_bitrate_bps);
271 assert(streams[i].max_qp >= 0);
272
273 sim_stream->width = static_cast<unsigned short>(streams[i].width);
274 sim_stream->height = static_cast<unsigned short>(streams[i].height);
275 sim_stream->minBitrate = streams[i].min_bitrate_bps / 1000;
276 sim_stream->targetBitrate = streams[i].target_bitrate_bps / 1000;
277 sim_stream->maxBitrate = streams[i].max_bitrate_bps / 1000;
278 sim_stream->qpMax = streams[i].max_qp;
279 // TODO(pbos): Implement mapping for temporal layers.
280 assert(streams[i].temporal_layers.empty());
281
282 video_codec.width = std::max(video_codec.width,
283 static_cast<unsigned short>(streams[i].width));
284 video_codec.height = std::max(
285 video_codec.height, static_cast<unsigned short>(streams[i].height));
286 video_codec.minBitrate =
287 std::min(video_codec.minBitrate,
288 static_cast<unsigned int>(streams[i].min_bitrate_bps / 1000));
289 video_codec.maxBitrate += streams[i].max_bitrate_bps / 1000;
290 video_codec.qpMax = std::max(video_codec.qpMax,
291 static_cast<unsigned int>(streams[i].max_qp));
292 }
293
294 if (video_codec.minBitrate < kViEMinCodecBitrate)
295 video_codec.minBitrate = kViEMinCodecBitrate;
296 if (video_codec.maxBitrate < kViEMinCodecBitrate)
297 video_codec.maxBitrate = kViEMinCodecBitrate;
298
299 video_codec.startBitrate = 300;
300
301 if (video_codec.startBitrate < video_codec.minBitrate)
302 video_codec.startBitrate = video_codec.minBitrate;
303 if (video_codec.startBitrate > video_codec.maxBitrate)
304 video_codec.startBitrate = video_codec.maxBitrate;
305
306 assert(config_.encoder_settings.streams[0].max_framerate > 0);
307 video_codec.maxFramerate = config_.encoder_settings.streams[0].max_framerate;
308
309 if (codec_->SetSendCodec(channel_, video_codec) != 0)
pbos@webrtc.org8f2997c2013-11-14 08:58:14 +0000310 return false;
311
pbos@webrtc.org9105cbd2013-11-28 11:59:31 +0000312 for (size_t i = 0; i < config_.rtp.ssrcs.size(); ++i) {
313 rtp_rtcp_->SetLocalSSRC(channel_,
314 config_.rtp.ssrcs[i],
315 kViEStreamTypeNormal,
316 static_cast<unsigned char>(i));
317 }
318
pbos@webrtc.orgf39df522014-03-19 08:43:57 +0000319 config_.encoder_settings.streams = streams;
320 config_.encoder_settings.encoder_settings = encoder_settings;
pbos@webrtc.orgc7667752014-01-24 09:30:53 +0000321
pbos@webrtc.org9105cbd2013-11-28 11:59:31 +0000322 if (config_.rtp.rtx.ssrcs.empty())
323 return true;
324
325 // Set up RTX.
326 assert(config_.rtp.rtx.ssrcs.size() == config_.rtp.ssrcs.size());
327 for (size_t i = 0; i < config_.rtp.ssrcs.size(); ++i) {
328 rtp_rtcp_->SetLocalSSRC(channel_,
329 config_.rtp.rtx.ssrcs[i],
330 kViEStreamTypeRtx,
331 static_cast<unsigned char>(i));
332 }
333
pbos@webrtc.orgc7667752014-01-24 09:30:53 +0000334 if (config_.rtp.rtx.payload_type != 0)
335 rtp_rtcp_->SetRtxSendPayloadType(channel_, config_.rtp.rtx.payload_type);
pbos@webrtc.org9105cbd2013-11-28 11:59:31 +0000336
pbos@webrtc.org8f2997c2013-11-14 08:58:14 +0000337 return true;
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000338}
339
pbos@webrtc.orgbf9bc322013-08-05 12:01:36 +0000340bool VideoSendStream::DeliverRtcp(const uint8_t* packet, size_t length) {
341 return network_->ReceivedRTCPPacket(
pbos@webrtc.org30c741a2013-08-05 13:25:51 +0000342 channel_, packet, static_cast<int>(length)) == 0;
pbos@webrtc.orgbf9bc322013-08-05 12:01:36 +0000343}
sprang@webrtc.orgca723002014-01-07 09:54:34 +0000344
345VideoSendStream::Stats VideoSendStream::GetStats() const {
346 return stats_proxy_->GetStats();
347}
348
349bool VideoSendStream::GetSendSideDelay(VideoSendStream::Stats* stats) {
350 return codec_->GetSendSideDelay(
351 channel_, &stats->avg_delay_ms, &stats->max_delay_ms);
352}
353
354std::string VideoSendStream::GetCName() {
355 char rtcp_cname[ViERTP_RTCP::KMaxRTCPCNameLength];
356 rtp_rtcp_->GetRTCPCName(channel_, rtcp_cname);
357 return rtcp_cname;
358}
359
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000360} // namespace internal
361} // namespace webrtc