blob: 13839152abea436e48935b2541e5b524bd513a72 [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
52 for (size_t i = 0; i < config_.rtp.extensions.size(); ++i) {
53 const std::string& extension = config_.rtp.extensions[i].name;
54 int id = config_.rtp.extensions[i].id;
pbos@webrtc.org60108c22013-11-20 11:48:56 +000055 if (extension == RtpExtension::kTOffset) {
pbos@webrtc.org905cebd2013-09-11 10:14:56 +000056 if (rtp_rtcp_->SetSendTimestampOffsetStatus(channel_, true, id) != 0)
57 abort();
pbos@webrtc.org60108c22013-11-20 11:48:56 +000058 } else if (extension == RtpExtension::kAbsSendTime) {
pbos@webrtc.orge22b7612013-09-11 19:00:39 +000059 if (rtp_rtcp_->SetSendAbsoluteSendTimeStatus(channel_, true, id) != 0)
60 abort();
pbos@webrtc.org905cebd2013-09-11 10:14:56 +000061 } else {
62 abort(); // Unsupported extension.
63 }
64 }
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +000065
mflodman@webrtc.orgab6ccbc2013-12-13 16:36:28 +000066 rtp_rtcp_->SetRembStatus(channel_, true, false);
67
pbos@webrtc.orgaa693dd2013-09-20 11:56:26 +000068 // Enable NACK, FEC or both.
69 if (config_.rtp.fec.red_payload_type != -1) {
70 assert(config_.rtp.fec.ulpfec_payload_type != -1);
71 if (config_.rtp.nack.rtp_history_ms > 0) {
72 rtp_rtcp_->SetHybridNACKFECStatus(
73 channel_,
74 true,
75 static_cast<unsigned char>(config_.rtp.fec.red_payload_type),
76 static_cast<unsigned char>(config_.rtp.fec.ulpfec_payload_type));
77 } else {
78 rtp_rtcp_->SetFECStatus(
79 channel_,
80 true,
81 static_cast<unsigned char>(config_.rtp.fec.red_payload_type),
82 static_cast<unsigned char>(config_.rtp.fec.ulpfec_payload_type));
83 }
84 } else {
85 rtp_rtcp_->SetNACKStatus(channel_, config_.rtp.nack.rtp_history_ms > 0);
86 }
87
pbos@webrtc.orgdebc6722013-08-22 09:42:17 +000088 char rtcp_cname[ViERTP_RTCP::KMaxRTCPCNameLength];
89 assert(config_.rtp.c_name.length() < ViERTP_RTCP::KMaxRTCPCNameLength);
90 strncpy(rtcp_cname, config_.rtp.c_name.c_str(), sizeof(rtcp_cname) - 1);
91 rtcp_cname[sizeof(rtcp_cname) - 1] = '\0';
92
93 rtp_rtcp_->SetRTCPCName(channel_, rtcp_cname);
94
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +000095 capture_ = ViECapture::GetInterface(video_engine);
96 capture_->AllocateExternalCaptureDevice(capture_id_, external_capture_);
97 capture_->ConnectCaptureDevice(capture_id_, channel_);
98
99 network_ = ViENetwork::GetInterface(video_engine);
100 assert(network_ != NULL);
101
pbos@webrtc.org26d75f32013-09-18 11:52:42 +0000102 network_->RegisterSendTransport(channel_, transport_adapter_);
sprang@webrtc.org6133dd52013-10-16 13:29:14 +0000103 // 28 to match packet overhead in ModuleRtpRtcpImpl.
pbos@webrtc.orgb581c902013-10-28 16:32:01 +0000104 network_->SetMTU(channel_,
105 static_cast<unsigned int>(config_.rtp.max_packet_size + 28));
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000106
stefan@webrtc.orga0a91d82013-08-22 09:29:56 +0000107 if (config.encoder) {
108 external_codec_ = ViEExternalCodec::GetInterface(video_engine);
pbos@webrtc.orgc7667752014-01-24 09:30:53 +0000109 if (external_codec_->RegisterExternalSendCodec(channel_,
110 config.codec.plType,
111 config.encoder,
112 config.internal_source) !=
113 0) {
stefan@webrtc.orga0a91d82013-08-22 09:29:56 +0000114 abort();
115 }
116 }
117
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000118 codec_ = ViECodec::GetInterface(video_engine);
pbos@webrtc.org8f2997c2013-11-14 08:58:14 +0000119 if (!SetCodec(config_.codec))
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000120 abort();
mflodman@webrtc.orgecbeb2b2013-07-23 11:35:00 +0000121
asapersson@webrtc.org4b1817f2014-01-31 10:05:07 +0000122 if (overuse_observer) {
123 video_engine_base_->RegisterCpuOveruseObserver(channel_, overuse_observer);
mflodman@webrtc.orgecbeb2b2013-07-23 11:35:00 +0000124 }
pbos@webrtc.org3ba57eb2013-10-21 10:34:43 +0000125
126 image_process_ = ViEImageProcess::GetInterface(video_engine);
127 image_process_->RegisterPreEncodeCallback(channel_,
128 config_.pre_encode_callback);
sprang@webrtc.org4a9843f2013-11-26 11:41:59 +0000129 if (config_.post_encode_callback) {
130 image_process_->RegisterPostEncodeImageCallback(channel_,
131 &encoded_frame_proxy_);
132 }
henrik.lundin@webrtc.orgce21c822013-10-23 11:04:57 +0000133
henrik.lundin@webrtc.org8fdf1912013-11-18 12:18:43 +0000134 if (config.suspend_below_min_bitrate) {
135 codec_->SuspendBelowMinBitrate(channel_);
henrik.lundin@webrtc.orgce21c822013-10-23 11:04:57 +0000136 }
sprang@webrtc.orgca723002014-01-07 09:54:34 +0000137
pbos@webrtc.orgc7667752014-01-24 09:30:53 +0000138 stats_proxy_.reset(new SendStatisticsProxy(config, this));
sprang@webrtc.orgca723002014-01-07 09:54:34 +0000139
140 rtp_rtcp_->RegisterSendChannelRtcpStatisticsCallback(channel_,
141 stats_proxy_.get());
142 rtp_rtcp_->RegisterSendChannelRtpStatisticsCallback(channel_,
143 stats_proxy_.get());
144 rtp_rtcp_->RegisterSendBitrateObserver(channel_, stats_proxy_.get());
145 rtp_rtcp_->RegisterSendFrameCountObserver(channel_, stats_proxy_.get());
146
147 codec_->RegisterEncoderObserver(channel_, *stats_proxy_);
148 capture_->RegisterObserver(capture_id_, *stats_proxy_);
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000149}
150
151VideoSendStream::~VideoSendStream() {
sprang@webrtc.orgca723002014-01-07 09:54:34 +0000152 capture_->DeregisterObserver(capture_id_);
153 codec_->DeregisterEncoderObserver(channel_);
154
155 rtp_rtcp_->DeregisterSendFrameCountObserver(channel_, stats_proxy_.get());
156 rtp_rtcp_->DeregisterSendBitrateObserver(channel_, stats_proxy_.get());
157 rtp_rtcp_->DeregisterSendChannelRtpStatisticsCallback(channel_,
158 stats_proxy_.get());
159 rtp_rtcp_->DeregisterSendChannelRtcpStatisticsCallback(channel_,
160 stats_proxy_.get());
161
pbos@webrtc.org3ba57eb2013-10-21 10:34:43 +0000162 image_process_->DeRegisterPreEncodeCallback(channel_);
163
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000164 network_->DeregisterSendTransport(channel_);
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000165
166 capture_->DisconnectCaptureDevice(channel_);
167 capture_->ReleaseCaptureDevice(capture_id_);
168
stefan@webrtc.orga0a91d82013-08-22 09:29:56 +0000169 if (external_codec_) {
170 external_codec_->DeRegisterExternalSendCodec(channel_,
171 config_.codec.plType);
172 }
173
pbos@webrtc.org3ba57eb2013-10-21 10:34:43 +0000174 video_engine_base_->DeleteChannel(channel_);
175
176 image_process_->Release();
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000177 video_engine_base_->Release();
178 capture_->Release();
179 codec_->Release();
stefan@webrtc.orga0a91d82013-08-22 09:29:56 +0000180 if (external_codec_)
181 external_codec_->Release();
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000182 network_->Release();
183 rtp_rtcp_->Release();
184}
185
pbos@webrtc.org7123a802013-12-11 16:26:16 +0000186void VideoSendStream::PutFrame(const I420VideoFrame& frame) {
187 input_frame_.CopyFrame(frame);
188 SwapFrame(&input_frame_);
189}
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000190
pbos@webrtc.org7123a802013-12-11 16:26:16 +0000191void VideoSendStream::SwapFrame(I420VideoFrame* frame) {
192 // TODO(pbos): Warn if frame is "too far" into the future, or too old. This
193 // would help detect if frame's being used without NTP.
194 // TO REVIEWER: Is there any good check for this? Should it be
195 // skipped?
196 if (frame != &input_frame_)
197 input_frame_.SwapFrame(frame);
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000198
pbos@webrtc.org7123a802013-12-11 16:26:16 +0000199 // TODO(pbos): Local rendering should not be done on the capture thread.
200 if (config_.local_renderer != NULL)
201 config_.local_renderer->RenderFrame(input_frame_, 0);
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000202
pbos@webrtc.org7123a802013-12-11 16:26:16 +0000203 external_capture_->SwapFrame(&input_frame_);
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000204}
205
pbos@webrtc.orgd8e92c92013-08-23 09:19:30 +0000206VideoSendStreamInput* VideoSendStream::Input() { return this; }
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000207
pbos@webrtc.org48cc9dc2013-11-20 11:36:47 +0000208void VideoSendStream::StartSending() {
sprang@webrtc.org48ac0da2014-01-27 13:03:02 +0000209 transport_adapter_.Enable();
pbos@webrtc.orgdf9f0992014-01-10 18:47:32 +0000210 video_engine_base_->StartSend(channel_);
211 video_engine_base_->StartReceive(channel_);
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000212}
213
pbos@webrtc.org48cc9dc2013-11-20 11:36:47 +0000214void VideoSendStream::StopSending() {
pbos@webrtc.orgdf9f0992014-01-10 18:47:32 +0000215 video_engine_base_->StopSend(channel_);
216 video_engine_base_->StopReceive(channel_);
sprang@webrtc.org48ac0da2014-01-27 13:03:02 +0000217 transport_adapter_.Disable();
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000218}
219
pbos@webrtc.org8f2997c2013-11-14 08:58:14 +0000220bool VideoSendStream::SetCodec(const VideoCodec& codec) {
pbos@webrtc.org9105cbd2013-11-28 11:59:31 +0000221 assert(config_.rtp.ssrcs.size() >= codec.numberOfSimulcastStreams);
pbos@webrtc.org8f2997c2013-11-14 08:58:14 +0000222
223 CriticalSectionScoped crit(codec_lock_.get());
224 if (codec_->SetSendCodec(channel_, codec) != 0)
225 return false;
226
pbos@webrtc.org9105cbd2013-11-28 11:59:31 +0000227 for (size_t i = 0; i < config_.rtp.ssrcs.size(); ++i) {
228 rtp_rtcp_->SetLocalSSRC(channel_,
229 config_.rtp.ssrcs[i],
230 kViEStreamTypeNormal,
231 static_cast<unsigned char>(i));
232 }
233
pbos@webrtc.orgc7667752014-01-24 09:30:53 +0000234 if (&config_.codec != &codec)
235 config_.codec = codec;
236
pbos@webrtc.org9105cbd2013-11-28 11:59:31 +0000237 if (config_.rtp.rtx.ssrcs.empty())
238 return true;
239
240 // Set up RTX.
241 assert(config_.rtp.rtx.ssrcs.size() == config_.rtp.ssrcs.size());
242 for (size_t i = 0; i < config_.rtp.ssrcs.size(); ++i) {
243 rtp_rtcp_->SetLocalSSRC(channel_,
244 config_.rtp.rtx.ssrcs[i],
245 kViEStreamTypeRtx,
246 static_cast<unsigned char>(i));
247 }
248
pbos@webrtc.orgc7667752014-01-24 09:30:53 +0000249 if (config_.rtp.rtx.payload_type != 0)
250 rtp_rtcp_->SetRtxSendPayloadType(channel_, config_.rtp.rtx.payload_type);
pbos@webrtc.org9105cbd2013-11-28 11:59:31 +0000251
pbos@webrtc.org8f2997c2013-11-14 08:58:14 +0000252 return true;
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000253}
254
pbos@webrtc.org8f2997c2013-11-14 08:58:14 +0000255VideoCodec VideoSendStream::GetCodec() {
256 CriticalSectionScoped crit(codec_lock_.get());
257 return config_.codec;
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000258}
259
pbos@webrtc.orgbf9bc322013-08-05 12:01:36 +0000260bool VideoSendStream::DeliverRtcp(const uint8_t* packet, size_t length) {
261 return network_->ReceivedRTCPPacket(
pbos@webrtc.org30c741a2013-08-05 13:25:51 +0000262 channel_, packet, static_cast<int>(length)) == 0;
pbos@webrtc.orgbf9bc322013-08-05 12:01:36 +0000263}
sprang@webrtc.orgca723002014-01-07 09:54:34 +0000264
265VideoSendStream::Stats VideoSendStream::GetStats() const {
266 return stats_proxy_->GetStats();
267}
268
269bool VideoSendStream::GetSendSideDelay(VideoSendStream::Stats* stats) {
270 return codec_->GetSendSideDelay(
271 channel_, &stats->avg_delay_ms, &stats->max_delay_ms);
272}
273
274std::string VideoSendStream::GetCName() {
275 char rtcp_cname[ViERTP_RTCP::KMaxRTCPCNameLength];
276 rtp_rtcp_->GetRTCPCName(channel_, rtcp_cname);
277 return rtcp_cname;
278}
279
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000280} // namespace internal
281} // namespace webrtc