blob: 2b83a48c306efaf49ea5b11d235f844c522fd229 [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,
pbos@webrtc.orgd8e92c92013-08-23 09:19:30 +000084 const VideoSendStream::Config& config)
pbos@webrtc.org8f2997c2013-11-14 08:58:14 +000085 : transport_adapter_(transport),
sprang@webrtc.org4a9843f2013-11-26 11:41:59 +000086 encoded_frame_proxy_(config.post_encode_callback),
pbos@webrtc.org8f2997c2013-11-14 08:58:14 +000087 codec_lock_(CriticalSectionWrapper::CreateCriticalSection()),
88 config_(config),
89 external_codec_(NULL) {
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +000090 video_engine_base_ = ViEBase::GetInterface(video_engine);
91 video_engine_base_->CreateChannel(channel_);
92 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
pbos@webrtc.orgaa693dd2013-09-20 11:56:26 +0000116 // Enable NACK, FEC or both.
117 if (config_.rtp.fec.red_payload_type != -1) {
118 assert(config_.rtp.fec.ulpfec_payload_type != -1);
119 if (config_.rtp.nack.rtp_history_ms > 0) {
120 rtp_rtcp_->SetHybridNACKFECStatus(
121 channel_,
122 true,
123 static_cast<unsigned char>(config_.rtp.fec.red_payload_type),
124 static_cast<unsigned char>(config_.rtp.fec.ulpfec_payload_type));
125 } else {
126 rtp_rtcp_->SetFECStatus(
127 channel_,
128 true,
129 static_cast<unsigned char>(config_.rtp.fec.red_payload_type),
130 static_cast<unsigned char>(config_.rtp.fec.ulpfec_payload_type));
131 }
132 } else {
133 rtp_rtcp_->SetNACKStatus(channel_, config_.rtp.nack.rtp_history_ms > 0);
134 }
135
pbos@webrtc.orgdebc6722013-08-22 09:42:17 +0000136 char rtcp_cname[ViERTP_RTCP::KMaxRTCPCNameLength];
137 assert(config_.rtp.c_name.length() < ViERTP_RTCP::KMaxRTCPCNameLength);
138 strncpy(rtcp_cname, config_.rtp.c_name.c_str(), sizeof(rtcp_cname) - 1);
139 rtcp_cname[sizeof(rtcp_cname) - 1] = '\0';
140
141 rtp_rtcp_->SetRTCPCName(channel_, rtcp_cname);
142
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000143 capture_ = ViECapture::GetInterface(video_engine);
144 capture_->AllocateExternalCaptureDevice(capture_id_, external_capture_);
145 capture_->ConnectCaptureDevice(capture_id_, channel_);
146
147 network_ = ViENetwork::GetInterface(video_engine);
148 assert(network_ != NULL);
149
pbos@webrtc.org26d75f32013-09-18 11:52:42 +0000150 network_->RegisterSendTransport(channel_, transport_adapter_);
sprang@webrtc.org6133dd52013-10-16 13:29:14 +0000151 // 28 to match packet overhead in ModuleRtpRtcpImpl.
pbos@webrtc.orgb581c902013-10-28 16:32:01 +0000152 network_->SetMTU(channel_,
153 static_cast<unsigned int>(config_.rtp.max_packet_size + 28));
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000154
stefan@webrtc.orga0a91d82013-08-22 09:29:56 +0000155 if (config.encoder) {
156 external_codec_ = ViEExternalCodec::GetInterface(video_engine);
157 if (external_codec_->RegisterExternalSendCodec(
158 channel_, config.codec.plType, config.encoder,
159 config.internal_source) != 0) {
160 abort();
161 }
162 }
163
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000164 codec_ = ViECodec::GetInterface(video_engine);
pbos@webrtc.org8f2997c2013-11-14 08:58:14 +0000165 if (!SetCodec(config_.codec))
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000166 abort();
mflodman@webrtc.orgecbeb2b2013-07-23 11:35:00 +0000167
168 if (overuse_detection) {
169 overuse_observer_.reset(
170 new ResolutionAdaptor(codec_, channel_, config_.codec.width,
171 config_.codec.height));
172 video_engine_base_->RegisterCpuOveruseObserver(channel_,
pbos@webrtc.org905cebd2013-09-11 10:14:56 +0000173 overuse_observer_.get());
mflodman@webrtc.orgecbeb2b2013-07-23 11:35:00 +0000174 }
pbos@webrtc.org3ba57eb2013-10-21 10:34:43 +0000175
176 image_process_ = ViEImageProcess::GetInterface(video_engine);
177 image_process_->RegisterPreEncodeCallback(channel_,
178 config_.pre_encode_callback);
sprang@webrtc.org4a9843f2013-11-26 11:41:59 +0000179 if (config_.post_encode_callback) {
180 image_process_->RegisterPostEncodeImageCallback(channel_,
181 &encoded_frame_proxy_);
182 }
henrik.lundin@webrtc.orgce21c822013-10-23 11:04:57 +0000183
henrik.lundin@webrtc.org8fdf1912013-11-18 12:18:43 +0000184 if (config.suspend_below_min_bitrate) {
185 codec_->SuspendBelowMinBitrate(channel_);
henrik.lundin@webrtc.orgce21c822013-10-23 11:04:57 +0000186 }
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000187}
188
189VideoSendStream::~VideoSendStream() {
pbos@webrtc.org3ba57eb2013-10-21 10:34:43 +0000190 image_process_->DeRegisterPreEncodeCallback(channel_);
191
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000192 network_->DeregisterSendTransport(channel_);
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000193
194 capture_->DisconnectCaptureDevice(channel_);
195 capture_->ReleaseCaptureDevice(capture_id_);
196
stefan@webrtc.orga0a91d82013-08-22 09:29:56 +0000197 if (external_codec_) {
198 external_codec_->DeRegisterExternalSendCodec(channel_,
199 config_.codec.plType);
200 }
201
pbos@webrtc.org3ba57eb2013-10-21 10:34:43 +0000202 video_engine_base_->DeleteChannel(channel_);
203
204 image_process_->Release();
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000205 video_engine_base_->Release();
206 capture_->Release();
207 codec_->Release();
stefan@webrtc.orga0a91d82013-08-22 09:29:56 +0000208 if (external_codec_)
209 external_codec_->Release();
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000210 network_->Release();
211 rtp_rtcp_->Release();
212}
213
pbos@webrtc.org7123a802013-12-11 16:26:16 +0000214void VideoSendStream::PutFrame(const I420VideoFrame& frame) {
215 input_frame_.CopyFrame(frame);
216 SwapFrame(&input_frame_);
217}
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000218
pbos@webrtc.org7123a802013-12-11 16:26:16 +0000219void VideoSendStream::SwapFrame(I420VideoFrame* frame) {
220 // TODO(pbos): Warn if frame is "too far" into the future, or too old. This
221 // would help detect if frame's being used without NTP.
222 // TO REVIEWER: Is there any good check for this? Should it be
223 // skipped?
224 if (frame != &input_frame_)
225 input_frame_.SwapFrame(frame);
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000226
pbos@webrtc.org7123a802013-12-11 16:26:16 +0000227 // TODO(pbos): Local rendering should not be done on the capture thread.
228 if (config_.local_renderer != NULL)
229 config_.local_renderer->RenderFrame(input_frame_, 0);
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000230
pbos@webrtc.org7123a802013-12-11 16:26:16 +0000231 external_capture_->SwapFrame(&input_frame_);
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000232}
233
pbos@webrtc.orgd8e92c92013-08-23 09:19:30 +0000234VideoSendStreamInput* VideoSendStream::Input() { return this; }
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000235
pbos@webrtc.org48cc9dc2013-11-20 11:36:47 +0000236void VideoSendStream::StartSending() {
pbos@webrtc.orgd9f91852013-05-23 12:37:11 +0000237 if (video_engine_base_->StartSend(channel_) != 0)
238 abort();
pbos@webrtc.orgbf9bc322013-08-05 12:01:36 +0000239 if (video_engine_base_->StartReceive(channel_) != 0)
240 abort();
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000241}
242
pbos@webrtc.org48cc9dc2013-11-20 11:36:47 +0000243void VideoSendStream::StopSending() {
pbos@webrtc.orgd9f91852013-05-23 12:37:11 +0000244 if (video_engine_base_->StopSend(channel_) != 0)
245 abort();
pbos@webrtc.orgbf9bc322013-08-05 12:01:36 +0000246 if (video_engine_base_->StopReceive(channel_) != 0)
247 abort();
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000248}
249
pbos@webrtc.org8f2997c2013-11-14 08:58:14 +0000250bool VideoSendStream::SetCodec(const VideoCodec& codec) {
pbos@webrtc.org9105cbd2013-11-28 11:59:31 +0000251 assert(config_.rtp.ssrcs.size() >= codec.numberOfSimulcastStreams);
pbos@webrtc.org8f2997c2013-11-14 08:58:14 +0000252
253 CriticalSectionScoped crit(codec_lock_.get());
254 if (codec_->SetSendCodec(channel_, codec) != 0)
255 return false;
256
pbos@webrtc.org9105cbd2013-11-28 11:59:31 +0000257 for (size_t i = 0; i < config_.rtp.ssrcs.size(); ++i) {
258 rtp_rtcp_->SetLocalSSRC(channel_,
259 config_.rtp.ssrcs[i],
260 kViEStreamTypeNormal,
261 static_cast<unsigned char>(i));
262 }
263
pbos@webrtc.org8f2997c2013-11-14 08:58:14 +0000264 config_.codec = codec;
pbos@webrtc.org9105cbd2013-11-28 11:59:31 +0000265 if (config_.rtp.rtx.ssrcs.empty())
266 return true;
267
268 // Set up RTX.
269 assert(config_.rtp.rtx.ssrcs.size() == config_.rtp.ssrcs.size());
270 for (size_t i = 0; i < config_.rtp.ssrcs.size(); ++i) {
271 rtp_rtcp_->SetLocalSSRC(channel_,
272 config_.rtp.rtx.ssrcs[i],
273 kViEStreamTypeRtx,
274 static_cast<unsigned char>(i));
275 }
276
277 if (config_.rtp.rtx.rtx_payload_type != 0) {
278 rtp_rtcp_->SetRtxSendPayloadType(channel_,
279 config_.rtp.rtx.rtx_payload_type);
280 }
281
pbos@webrtc.org8f2997c2013-11-14 08:58:14 +0000282 return true;
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000283}
284
pbos@webrtc.org8f2997c2013-11-14 08:58:14 +0000285VideoCodec VideoSendStream::GetCodec() {
286 CriticalSectionScoped crit(codec_lock_.get());
287 return config_.codec;
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000288}
289
pbos@webrtc.orgbf9bc322013-08-05 12:01:36 +0000290bool VideoSendStream::DeliverRtcp(const uint8_t* packet, size_t length) {
291 return network_->ReceivedRTCPPacket(
pbos@webrtc.org30c741a2013-08-05 13:25:51 +0000292 channel_, packet, static_cast<int>(length)) == 0;
pbos@webrtc.orgbf9bc322013-08-05 12:01:36 +0000293}
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000294} // namespace internal
295} // namespace webrtc