blob: b5fec72d9f212be5b7d1b8e4d2b4fe6df168fd99 [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);
stefan@webrtc.orga0a91d82013-08-22 09:29:56 +000098 if (config_.rtp.ssrcs.size() == 1) {
99 rtp_rtcp_->SetLocalSSRC(channel_, config_.rtp.ssrcs[0]);
100 } else {
101 for (size_t i = 0; i < config_.rtp.ssrcs.size(); ++i) {
pbos@webrtc.orgb581c902013-10-28 16:32:01 +0000102 rtp_rtcp_->SetLocalSSRC(channel_,
103 config_.rtp.ssrcs[i],
104 kViEStreamTypeNormal,
105 static_cast<unsigned char>(i));
stefan@webrtc.orga0a91d82013-08-22 09:29:56 +0000106 }
107 }
henrik.lundin@webrtc.orgd7d60c82013-11-21 14:05:40 +0000108 if (config_.suspend_below_min_bitrate)
109 config_.pacing = true;
stefan@webrtc.orga0a91d82013-08-22 09:29:56 +0000110 rtp_rtcp_->SetTransmissionSmoothingStatus(channel_, config_.pacing);
pbos@webrtc.orgf952fce2013-09-16 13:01:47 +0000111 if (!config_.rtp.rtx.ssrcs.empty()) {
112 assert(config_.rtp.rtx.ssrcs.size() == config_.rtp.ssrcs.size());
113 for (size_t i = 0; i < config_.rtp.rtx.ssrcs.size(); ++i) {
pbos@webrtc.orgb581c902013-10-28 16:32:01 +0000114 rtp_rtcp_->SetLocalSSRC(channel_,
115 config_.rtp.rtx.ssrcs[i],
116 kViEStreamTypeRtx,
117 static_cast<unsigned char>(i));
pbos@webrtc.orgf952fce2013-09-16 13:01:47 +0000118 }
119
120 if (config_.rtp.rtx.rtx_payload_type != 0) {
121 rtp_rtcp_->SetRtxSendPayloadType(channel_,
122 config_.rtp.rtx.rtx_payload_type);
123 }
124 }
pbos@webrtc.org905cebd2013-09-11 10:14:56 +0000125
126 for (size_t i = 0; i < config_.rtp.extensions.size(); ++i) {
127 const std::string& extension = config_.rtp.extensions[i].name;
128 int id = config_.rtp.extensions[i].id;
pbos@webrtc.org60108c22013-11-20 11:48:56 +0000129 if (extension == RtpExtension::kTOffset) {
pbos@webrtc.org905cebd2013-09-11 10:14:56 +0000130 if (rtp_rtcp_->SetSendTimestampOffsetStatus(channel_, true, id) != 0)
131 abort();
pbos@webrtc.org60108c22013-11-20 11:48:56 +0000132 } else if (extension == RtpExtension::kAbsSendTime) {
pbos@webrtc.orge22b7612013-09-11 19:00:39 +0000133 if (rtp_rtcp_->SetSendAbsoluteSendTimeStatus(channel_, true, id) != 0)
134 abort();
pbos@webrtc.org905cebd2013-09-11 10:14:56 +0000135 } else {
136 abort(); // Unsupported extension.
137 }
138 }
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000139
pbos@webrtc.orgaa693dd2013-09-20 11:56:26 +0000140 // Enable NACK, FEC or both.
141 if (config_.rtp.fec.red_payload_type != -1) {
142 assert(config_.rtp.fec.ulpfec_payload_type != -1);
143 if (config_.rtp.nack.rtp_history_ms > 0) {
144 rtp_rtcp_->SetHybridNACKFECStatus(
145 channel_,
146 true,
147 static_cast<unsigned char>(config_.rtp.fec.red_payload_type),
148 static_cast<unsigned char>(config_.rtp.fec.ulpfec_payload_type));
149 } else {
150 rtp_rtcp_->SetFECStatus(
151 channel_,
152 true,
153 static_cast<unsigned char>(config_.rtp.fec.red_payload_type),
154 static_cast<unsigned char>(config_.rtp.fec.ulpfec_payload_type));
155 }
156 } else {
157 rtp_rtcp_->SetNACKStatus(channel_, config_.rtp.nack.rtp_history_ms > 0);
158 }
159
pbos@webrtc.orgdebc6722013-08-22 09:42:17 +0000160 char rtcp_cname[ViERTP_RTCP::KMaxRTCPCNameLength];
161 assert(config_.rtp.c_name.length() < ViERTP_RTCP::KMaxRTCPCNameLength);
162 strncpy(rtcp_cname, config_.rtp.c_name.c_str(), sizeof(rtcp_cname) - 1);
163 rtcp_cname[sizeof(rtcp_cname) - 1] = '\0';
164
165 rtp_rtcp_->SetRTCPCName(channel_, rtcp_cname);
166
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000167 capture_ = ViECapture::GetInterface(video_engine);
168 capture_->AllocateExternalCaptureDevice(capture_id_, external_capture_);
169 capture_->ConnectCaptureDevice(capture_id_, channel_);
170
171 network_ = ViENetwork::GetInterface(video_engine);
172 assert(network_ != NULL);
173
pbos@webrtc.org26d75f32013-09-18 11:52:42 +0000174 network_->RegisterSendTransport(channel_, transport_adapter_);
sprang@webrtc.org6133dd52013-10-16 13:29:14 +0000175 // 28 to match packet overhead in ModuleRtpRtcpImpl.
pbos@webrtc.orgb581c902013-10-28 16:32:01 +0000176 network_->SetMTU(channel_,
177 static_cast<unsigned int>(config_.rtp.max_packet_size + 28));
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000178
stefan@webrtc.orga0a91d82013-08-22 09:29:56 +0000179 if (config.encoder) {
180 external_codec_ = ViEExternalCodec::GetInterface(video_engine);
181 if (external_codec_->RegisterExternalSendCodec(
182 channel_, config.codec.plType, config.encoder,
183 config.internal_source) != 0) {
184 abort();
185 }
186 }
187
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000188 codec_ = ViECodec::GetInterface(video_engine);
pbos@webrtc.org8f2997c2013-11-14 08:58:14 +0000189 if (!SetCodec(config_.codec))
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000190 abort();
mflodman@webrtc.orgecbeb2b2013-07-23 11:35:00 +0000191
192 if (overuse_detection) {
193 overuse_observer_.reset(
194 new ResolutionAdaptor(codec_, channel_, config_.codec.width,
195 config_.codec.height));
196 video_engine_base_->RegisterCpuOveruseObserver(channel_,
pbos@webrtc.org905cebd2013-09-11 10:14:56 +0000197 overuse_observer_.get());
mflodman@webrtc.orgecbeb2b2013-07-23 11:35:00 +0000198 }
pbos@webrtc.org3ba57eb2013-10-21 10:34:43 +0000199
200 image_process_ = ViEImageProcess::GetInterface(video_engine);
201 image_process_->RegisterPreEncodeCallback(channel_,
202 config_.pre_encode_callback);
sprang@webrtc.org4a9843f2013-11-26 11:41:59 +0000203 if (config_.post_encode_callback) {
204 image_process_->RegisterPostEncodeImageCallback(channel_,
205 &encoded_frame_proxy_);
206 }
henrik.lundin@webrtc.orgce21c822013-10-23 11:04:57 +0000207
henrik.lundin@webrtc.org8fdf1912013-11-18 12:18:43 +0000208 if (config.suspend_below_min_bitrate) {
209 codec_->SuspendBelowMinBitrate(channel_);
henrik.lundin@webrtc.orgce21c822013-10-23 11:04:57 +0000210 }
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000211}
212
213VideoSendStream::~VideoSendStream() {
pbos@webrtc.org3ba57eb2013-10-21 10:34:43 +0000214 image_process_->DeRegisterPreEncodeCallback(channel_);
215
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000216 network_->DeregisterSendTransport(channel_);
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000217
218 capture_->DisconnectCaptureDevice(channel_);
219 capture_->ReleaseCaptureDevice(capture_id_);
220
stefan@webrtc.orga0a91d82013-08-22 09:29:56 +0000221 if (external_codec_) {
222 external_codec_->DeRegisterExternalSendCodec(channel_,
223 config_.codec.plType);
224 }
225
pbos@webrtc.org3ba57eb2013-10-21 10:34:43 +0000226 video_engine_base_->DeleteChannel(channel_);
227
228 image_process_->Release();
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000229 video_engine_base_->Release();
230 capture_->Release();
231 codec_->Release();
stefan@webrtc.orga0a91d82013-08-22 09:29:56 +0000232 if (external_codec_)
233 external_codec_->Release();
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000234 network_->Release();
235 rtp_rtcp_->Release();
236}
237
238void VideoSendStream::PutFrame(const I420VideoFrame& frame,
pbos@webrtc.orgd9f91852013-05-23 12:37:11 +0000239 uint32_t time_since_capture_ms) {
240 // TODO(pbos): frame_copy should happen after the VideoProcessingModule has
241 // resized the frame.
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000242 I420VideoFrame frame_copy;
243 frame_copy.CopyFrame(frame);
244
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000245 ViEVideoFrameI420 vf;
246
247 // TODO(pbos): This represents a memcpy step and is only required because
248 // external_capture_ only takes ViEVideoFrameI420s.
249 vf.y_plane = frame_copy.buffer(kYPlane);
250 vf.u_plane = frame_copy.buffer(kUPlane);
251 vf.v_plane = frame_copy.buffer(kVPlane);
252 vf.y_pitch = frame.stride(kYPlane);
253 vf.u_pitch = frame.stride(kUPlane);
254 vf.v_pitch = frame.stride(kVPlane);
255 vf.width = frame.width();
256 vf.height = frame.height();
257
pbos@webrtc.orgd9f91852013-05-23 12:37:11 +0000258 external_capture_->IncomingFrameI420(vf, frame.render_time_ms());
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000259
260 if (config_.local_renderer != NULL) {
261 config_.local_renderer->RenderFrame(frame, 0);
262 }
263}
264
pbos@webrtc.orgd8e92c92013-08-23 09:19:30 +0000265VideoSendStreamInput* VideoSendStream::Input() { return this; }
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000266
pbos@webrtc.org48cc9dc2013-11-20 11:36:47 +0000267void VideoSendStream::StartSending() {
pbos@webrtc.orgd9f91852013-05-23 12:37:11 +0000268 if (video_engine_base_->StartSend(channel_) != 0)
269 abort();
pbos@webrtc.orgbf9bc322013-08-05 12:01:36 +0000270 if (video_engine_base_->StartReceive(channel_) != 0)
271 abort();
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000272}
273
pbos@webrtc.org48cc9dc2013-11-20 11:36:47 +0000274void VideoSendStream::StopSending() {
pbos@webrtc.orgd9f91852013-05-23 12:37:11 +0000275 if (video_engine_base_->StopSend(channel_) != 0)
276 abort();
pbos@webrtc.orgbf9bc322013-08-05 12:01:36 +0000277 if (video_engine_base_->StopReceive(channel_) != 0)
278 abort();
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000279}
280
pbos@webrtc.org8f2997c2013-11-14 08:58:14 +0000281bool VideoSendStream::SetCodec(const VideoCodec& codec) {
282 if (codec.numberOfSimulcastStreams > 0)
283 assert(config_.rtp.ssrcs.size() >= codec.numberOfSimulcastStreams);
284
285 CriticalSectionScoped crit(codec_lock_.get());
286 if (codec_->SetSendCodec(channel_, codec) != 0)
287 return false;
288
289 config_.codec = codec;
290 return true;
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000291}
292
pbos@webrtc.org8f2997c2013-11-14 08:58:14 +0000293VideoCodec VideoSendStream::GetCodec() {
294 CriticalSectionScoped crit(codec_lock_.get());
295 return config_.codec;
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000296}
297
pbos@webrtc.orgbf9bc322013-08-05 12:01:36 +0000298bool VideoSendStream::DeliverRtcp(const uint8_t* packet, size_t length) {
299 return network_->ReceivedRTCPPacket(
pbos@webrtc.org30c741a2013-08-05 13:25:51 +0000300 channel_, packet, static_cast<int>(length)) == 0;
pbos@webrtc.orgbf9bc322013-08-05 12:01:36 +0000301}
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000302} // namespace internal
303} // namespace webrtc