blob: df03b13bdf6021a922739d6d8156ff03cd351e91 [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
pbos@webrtc.orgdebc6722013-08-22 09:42:17 +000013#include <string.h>
14
henrik.lundin@webrtc.orgce21c822013-10-23 11:04:57 +000015#include <string>
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +000016#include <vector>
17
18#include "webrtc/common_video/libyuv/include/webrtc_libyuv.h"
19#include "webrtc/video_engine/include/vie_base.h"
20#include "webrtc/video_engine/include/vie_capture.h"
21#include "webrtc/video_engine/include/vie_codec.h"
stefan@webrtc.orga0a91d82013-08-22 09:29:56 +000022#include "webrtc/video_engine/include/vie_external_codec.h"
pbos@webrtc.org3ba57eb2013-10-21 10:34:43 +000023#include "webrtc/video_engine/include/vie_image_process.h"
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +000024#include "webrtc/video_engine/include/vie_network.h"
25#include "webrtc/video_engine/include/vie_rtp_rtcp.h"
pbos@webrtc.orgb581c902013-10-28 16:32:01 +000026#include "webrtc/video_send_stream.h"
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +000027
28namespace webrtc {
29namespace internal {
30
mflodman@webrtc.orgecbeb2b2013-07-23 11:35:00 +000031// Super simple and temporary overuse logic. This will move to the application
32// as soon as the new API allows changing send codec on the fly.
33class ResolutionAdaptor : public webrtc::CpuOveruseObserver {
34 public:
35 ResolutionAdaptor(ViECodec* codec, int channel, size_t width, size_t height)
36 : codec_(codec),
37 channel_(channel),
38 max_width_(width),
39 max_height_(height) {}
40
41 virtual ~ResolutionAdaptor() {}
42
43 virtual void OveruseDetected() OVERRIDE {
44 VideoCodec codec;
45 if (codec_->GetSendCodec(channel_, codec) != 0)
46 return;
47
48 if (codec.width / 2 < min_width || codec.height / 2 < min_height)
49 return;
50
51 codec.width /= 2;
52 codec.height /= 2;
53 codec_->SetSendCodec(channel_, codec);
54 }
55
56 virtual void NormalUsage() OVERRIDE {
57 VideoCodec codec;
58 if (codec_->GetSendCodec(channel_, codec) != 0)
59 return;
60
61 if (codec.width * 2u > max_width_ || codec.height * 2u > max_height_)
62 return;
63
64 codec.width *= 2;
65 codec.height *= 2;
66 codec_->SetSendCodec(channel_, codec);
67 }
68
69 private:
70 // Temporary and arbitrary chosen minimum resolution.
71 static const size_t min_width = 160;
72 static const size_t min_height = 120;
73
74 ViECodec* codec_;
75 const int channel_;
76
77 const size_t max_width_;
78 const size_t max_height_;
79};
80
pbos@webrtc.org12d5ede2013-07-09 08:02:33 +000081VideoSendStream::VideoSendStream(newapi::Transport* transport,
mflodman@webrtc.orgecbeb2b2013-07-23 11:35:00 +000082 bool overuse_detection,
pbos@webrtc.org12d5ede2013-07-09 08:02:33 +000083 webrtc::VideoEngine* video_engine,
mflodman@webrtc.orge4d538a2013-12-13 09:40:45 +000084 const VideoSendStream::Config& config,
85 int base_channel)
pbos@webrtc.org8f2997c2013-11-14 08:58:14 +000086 : transport_adapter_(transport),
sprang@webrtc.org4a9843f2013-11-26 11:41:59 +000087 encoded_frame_proxy_(config.post_encode_callback),
pbos@webrtc.org8f2997c2013-11-14 08:58:14 +000088 codec_lock_(CriticalSectionWrapper::CreateCriticalSection()),
89 config_(config),
mflodman@webrtc.orge4d538a2013-12-13 09:40:45 +000090 external_codec_(NULL),
91 channel_(-1) {
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +000092 video_engine_base_ = ViEBase::GetInterface(video_engine);
mflodman@webrtc.orge4d538a2013-12-13 09:40:45 +000093 video_engine_base_->CreateChannel(channel_, base_channel);
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +000094 assert(channel_ != -1);
95
96 rtp_rtcp_ = ViERTP_RTCP::GetInterface(video_engine);
97 assert(rtp_rtcp_ != NULL);
98
pbos@webrtc.org8f2997c2013-11-14 08:58:14 +000099 assert(config_.rtp.ssrcs.size() > 0);
henrik.lundin@webrtc.orgd7d60c82013-11-21 14:05:40 +0000100 if (config_.suspend_below_min_bitrate)
101 config_.pacing = true;
stefan@webrtc.orga0a91d82013-08-22 09:29:56 +0000102 rtp_rtcp_->SetTransmissionSmoothingStatus(channel_, config_.pacing);
pbos@webrtc.org905cebd2013-09-11 10:14:56 +0000103
104 for (size_t i = 0; i < config_.rtp.extensions.size(); ++i) {
105 const std::string& extension = config_.rtp.extensions[i].name;
106 int id = config_.rtp.extensions[i].id;
pbos@webrtc.org60108c22013-11-20 11:48:56 +0000107 if (extension == RtpExtension::kTOffset) {
pbos@webrtc.org905cebd2013-09-11 10:14:56 +0000108 if (rtp_rtcp_->SetSendTimestampOffsetStatus(channel_, true, id) != 0)
109 abort();
pbos@webrtc.org60108c22013-11-20 11:48:56 +0000110 } else if (extension == RtpExtension::kAbsSendTime) {
pbos@webrtc.orge22b7612013-09-11 19:00:39 +0000111 if (rtp_rtcp_->SetSendAbsoluteSendTimeStatus(channel_, true, id) != 0)
112 abort();
pbos@webrtc.org905cebd2013-09-11 10:14:56 +0000113 } else {
114 abort(); // Unsupported extension.
115 }
116 }
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000117
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 }
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000189}
190
191VideoSendStream::~VideoSendStream() {
pbos@webrtc.org3ba57eb2013-10-21 10:34:43 +0000192 image_process_->DeRegisterPreEncodeCallback(channel_);
193
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000194 network_->DeregisterSendTransport(channel_);
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000195
196 capture_->DisconnectCaptureDevice(channel_);
197 capture_->ReleaseCaptureDevice(capture_id_);
198
stefan@webrtc.orga0a91d82013-08-22 09:29:56 +0000199 if (external_codec_) {
200 external_codec_->DeRegisterExternalSendCodec(channel_,
201 config_.codec.plType);
202 }
203
pbos@webrtc.org3ba57eb2013-10-21 10:34:43 +0000204 video_engine_base_->DeleteChannel(channel_);
205
206 image_process_->Release();
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000207 video_engine_base_->Release();
208 capture_->Release();
209 codec_->Release();
stefan@webrtc.orga0a91d82013-08-22 09:29:56 +0000210 if (external_codec_)
211 external_codec_->Release();
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000212 network_->Release();
213 rtp_rtcp_->Release();
214}
215
pbos@webrtc.org7123a802013-12-11 16:26:16 +0000216void VideoSendStream::PutFrame(const I420VideoFrame& frame) {
217 input_frame_.CopyFrame(frame);
218 SwapFrame(&input_frame_);
219}
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000220
pbos@webrtc.org7123a802013-12-11 16:26:16 +0000221void VideoSendStream::SwapFrame(I420VideoFrame* frame) {
222 // TODO(pbos): Warn if frame is "too far" into the future, or too old. This
223 // would help detect if frame's being used without NTP.
224 // TO REVIEWER: Is there any good check for this? Should it be
225 // skipped?
226 if (frame != &input_frame_)
227 input_frame_.SwapFrame(frame);
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000228
pbos@webrtc.org7123a802013-12-11 16:26:16 +0000229 // TODO(pbos): Local rendering should not be done on the capture thread.
230 if (config_.local_renderer != NULL)
231 config_.local_renderer->RenderFrame(input_frame_, 0);
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000232
pbos@webrtc.org7123a802013-12-11 16:26:16 +0000233 external_capture_->SwapFrame(&input_frame_);
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000234}
235
pbos@webrtc.orgd8e92c92013-08-23 09:19:30 +0000236VideoSendStreamInput* VideoSendStream::Input() { return this; }
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000237
pbos@webrtc.org48cc9dc2013-11-20 11:36:47 +0000238void VideoSendStream::StartSending() {
pbos@webrtc.orgd9f91852013-05-23 12:37:11 +0000239 if (video_engine_base_->StartSend(channel_) != 0)
240 abort();
pbos@webrtc.orgbf9bc322013-08-05 12:01:36 +0000241 if (video_engine_base_->StartReceive(channel_) != 0)
242 abort();
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000243}
244
pbos@webrtc.org48cc9dc2013-11-20 11:36:47 +0000245void VideoSendStream::StopSending() {
pbos@webrtc.orgd9f91852013-05-23 12:37:11 +0000246 if (video_engine_base_->StopSend(channel_) != 0)
247 abort();
pbos@webrtc.orgbf9bc322013-08-05 12:01:36 +0000248 if (video_engine_base_->StopReceive(channel_) != 0)
249 abort();
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000250}
251
pbos@webrtc.org8f2997c2013-11-14 08:58:14 +0000252bool VideoSendStream::SetCodec(const VideoCodec& codec) {
pbos@webrtc.org9105cbd2013-11-28 11:59:31 +0000253 assert(config_.rtp.ssrcs.size() >= codec.numberOfSimulcastStreams);
pbos@webrtc.org8f2997c2013-11-14 08:58:14 +0000254
255 CriticalSectionScoped crit(codec_lock_.get());
256 if (codec_->SetSendCodec(channel_, codec) != 0)
257 return false;
258
pbos@webrtc.org9105cbd2013-11-28 11:59:31 +0000259 for (size_t i = 0; i < config_.rtp.ssrcs.size(); ++i) {
260 rtp_rtcp_->SetLocalSSRC(channel_,
261 config_.rtp.ssrcs[i],
262 kViEStreamTypeNormal,
263 static_cast<unsigned char>(i));
264 }
265
pbos@webrtc.org8f2997c2013-11-14 08:58:14 +0000266 config_.codec = codec;
pbos@webrtc.org9105cbd2013-11-28 11:59:31 +0000267 if (config_.rtp.rtx.ssrcs.empty())
268 return true;
269
270 // Set up RTX.
271 assert(config_.rtp.rtx.ssrcs.size() == config_.rtp.ssrcs.size());
272 for (size_t i = 0; i < config_.rtp.ssrcs.size(); ++i) {
273 rtp_rtcp_->SetLocalSSRC(channel_,
274 config_.rtp.rtx.ssrcs[i],
275 kViEStreamTypeRtx,
276 static_cast<unsigned char>(i));
277 }
278
279 if (config_.rtp.rtx.rtx_payload_type != 0) {
280 rtp_rtcp_->SetRtxSendPayloadType(channel_,
281 config_.rtp.rtx.rtx_payload_type);
282 }
283
pbos@webrtc.org8f2997c2013-11-14 08:58:14 +0000284 return true;
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000285}
286
pbos@webrtc.org8f2997c2013-11-14 08:58:14 +0000287VideoCodec VideoSendStream::GetCodec() {
288 CriticalSectionScoped crit(codec_lock_.get());
289 return config_.codec;
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000290}
291
pbos@webrtc.orgbf9bc322013-08-05 12:01:36 +0000292bool VideoSendStream::DeliverRtcp(const uint8_t* packet, size_t length) {
293 return network_->ReceivedRTCPPacket(
pbos@webrtc.org30c741a2013-08-05 13:25:51 +0000294 channel_, packet, static_cast<int>(length)) == 0;
pbos@webrtc.orgbf9bc322013-08-05 12:01:36 +0000295}
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000296} // namespace internal
297} // namespace webrtc