blob: 242f7fa854530d1f91d841b995dad2ea2c36fdcd [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
11#include "webrtc/video_engine/internal/video_send_stream.h"
12
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"
26#include "webrtc/video_engine/new_include/video_send_stream.h"
27
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.org26d75f32013-09-18 11:52:42 +000085 : transport_adapter_(transport), config_(config), external_codec_(NULL) {
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +000086
87 if (config_.codec.numberOfSimulcastStreams > 0) {
88 assert(config_.rtp.ssrcs.size() == config_.codec.numberOfSimulcastStreams);
89 } else {
90 assert(config_.rtp.ssrcs.size() == 1);
91 }
92
93 video_engine_base_ = ViEBase::GetInterface(video_engine);
94 video_engine_base_->CreateChannel(channel_);
95 assert(channel_ != -1);
96
97 rtp_rtcp_ = ViERTP_RTCP::GetInterface(video_engine);
98 assert(rtp_rtcp_ != NULL);
99
stefan@webrtc.orga0a91d82013-08-22 09:29:56 +0000100 if (config_.rtp.ssrcs.size() == 1) {
101 rtp_rtcp_->SetLocalSSRC(channel_, config_.rtp.ssrcs[0]);
102 } else {
103 for (size_t i = 0; i < config_.rtp.ssrcs.size(); ++i) {
104 rtp_rtcp_->SetLocalSSRC(channel_, config_.rtp.ssrcs[i],
105 kViEStreamTypeNormal, i);
106 }
107 }
stefan@webrtc.orga0a91d82013-08-22 09:29:56 +0000108 rtp_rtcp_->SetTransmissionSmoothingStatus(channel_, config_.pacing);
pbos@webrtc.orgf952fce2013-09-16 13:01:47 +0000109 if (!config_.rtp.rtx.ssrcs.empty()) {
110 assert(config_.rtp.rtx.ssrcs.size() == config_.rtp.ssrcs.size());
111 for (size_t i = 0; i < config_.rtp.rtx.ssrcs.size(); ++i) {
112 rtp_rtcp_->SetLocalSSRC(
113 channel_, config_.rtp.rtx.ssrcs[i], kViEStreamTypeRtx, i);
114 }
115
116 if (config_.rtp.rtx.rtx_payload_type != 0) {
117 rtp_rtcp_->SetRtxSendPayloadType(channel_,
118 config_.rtp.rtx.rtx_payload_type);
119 }
120 }
pbos@webrtc.org905cebd2013-09-11 10:14:56 +0000121
122 for (size_t i = 0; i < config_.rtp.extensions.size(); ++i) {
123 const std::string& extension = config_.rtp.extensions[i].name;
124 int id = config_.rtp.extensions[i].id;
125 if (extension == "toffset") {
126 if (rtp_rtcp_->SetSendTimestampOffsetStatus(channel_, true, id) != 0)
127 abort();
pbos@webrtc.orge22b7612013-09-11 19:00:39 +0000128 } else if (extension == "abs-send-time") {
129 if (rtp_rtcp_->SetSendAbsoluteSendTimeStatus(channel_, true, id) != 0)
130 abort();
pbos@webrtc.org905cebd2013-09-11 10:14:56 +0000131 } else {
132 abort(); // Unsupported extension.
133 }
134 }
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000135
pbos@webrtc.orgaa693dd2013-09-20 11:56:26 +0000136 // Enable NACK, FEC or both.
137 if (config_.rtp.fec.red_payload_type != -1) {
138 assert(config_.rtp.fec.ulpfec_payload_type != -1);
139 if (config_.rtp.nack.rtp_history_ms > 0) {
140 rtp_rtcp_->SetHybridNACKFECStatus(
141 channel_,
142 true,
143 static_cast<unsigned char>(config_.rtp.fec.red_payload_type),
144 static_cast<unsigned char>(config_.rtp.fec.ulpfec_payload_type));
145 } else {
146 rtp_rtcp_->SetFECStatus(
147 channel_,
148 true,
149 static_cast<unsigned char>(config_.rtp.fec.red_payload_type),
150 static_cast<unsigned char>(config_.rtp.fec.ulpfec_payload_type));
151 }
152 } else {
153 rtp_rtcp_->SetNACKStatus(channel_, config_.rtp.nack.rtp_history_ms > 0);
154 }
155
pbos@webrtc.orgdebc6722013-08-22 09:42:17 +0000156 char rtcp_cname[ViERTP_RTCP::KMaxRTCPCNameLength];
157 assert(config_.rtp.c_name.length() < ViERTP_RTCP::KMaxRTCPCNameLength);
158 strncpy(rtcp_cname, config_.rtp.c_name.c_str(), sizeof(rtcp_cname) - 1);
159 rtcp_cname[sizeof(rtcp_cname) - 1] = '\0';
160
161 rtp_rtcp_->SetRTCPCName(channel_, rtcp_cname);
162
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000163 capture_ = ViECapture::GetInterface(video_engine);
164 capture_->AllocateExternalCaptureDevice(capture_id_, external_capture_);
165 capture_->ConnectCaptureDevice(capture_id_, channel_);
166
167 network_ = ViENetwork::GetInterface(video_engine);
168 assert(network_ != NULL);
169
pbos@webrtc.org26d75f32013-09-18 11:52:42 +0000170 network_->RegisterSendTransport(channel_, transport_adapter_);
sprang@webrtc.org6133dd52013-10-16 13:29:14 +0000171 // 28 to match packet overhead in ModuleRtpRtcpImpl.
172 network_->SetMTU(channel_, config_.rtp.max_packet_size + 28);
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000173
stefan@webrtc.orga0a91d82013-08-22 09:29:56 +0000174 if (config.encoder) {
175 external_codec_ = ViEExternalCodec::GetInterface(video_engine);
176 if (external_codec_->RegisterExternalSendCodec(
177 channel_, config.codec.plType, config.encoder,
178 config.internal_source) != 0) {
179 abort();
180 }
181 }
182
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000183 codec_ = ViECodec::GetInterface(video_engine);
184 if (codec_->SetSendCodec(channel_, config_.codec) != 0) {
185 abort();
186 }
mflodman@webrtc.orgecbeb2b2013-07-23 11:35:00 +0000187
188 if (overuse_detection) {
189 overuse_observer_.reset(
190 new ResolutionAdaptor(codec_, channel_, config_.codec.width,
191 config_.codec.height));
192 video_engine_base_->RegisterCpuOveruseObserver(channel_,
pbos@webrtc.org905cebd2013-09-11 10:14:56 +0000193 overuse_observer_.get());
mflodman@webrtc.orgecbeb2b2013-07-23 11:35:00 +0000194 }
pbos@webrtc.org3ba57eb2013-10-21 10:34:43 +0000195
196 image_process_ = ViEImageProcess::GetInterface(video_engine);
197 image_process_->RegisterPreEncodeCallback(channel_,
198 config_.pre_encode_callback);
henrik.lundin@webrtc.orgce21c822013-10-23 11:04:57 +0000199
200 if (config.auto_muter.threshold_bps > 0) {
201 assert(config.auto_muter.window_bps >= 0);
202 codec_->EnableAutoMuting(channel_,
203 config.auto_muter.threshold_bps,
204 config.auto_muter.window_bps);
205 }
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000206}
207
208VideoSendStream::~VideoSendStream() {
pbos@webrtc.org3ba57eb2013-10-21 10:34:43 +0000209 image_process_->DeRegisterPreEncodeCallback(channel_);
210
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000211 network_->DeregisterSendTransport(channel_);
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000212
213 capture_->DisconnectCaptureDevice(channel_);
214 capture_->ReleaseCaptureDevice(capture_id_);
215
stefan@webrtc.orga0a91d82013-08-22 09:29:56 +0000216 if (external_codec_) {
217 external_codec_->DeRegisterExternalSendCodec(channel_,
218 config_.codec.plType);
219 }
220
pbos@webrtc.org3ba57eb2013-10-21 10:34:43 +0000221 video_engine_base_->DeleteChannel(channel_);
222
223 image_process_->Release();
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000224 video_engine_base_->Release();
225 capture_->Release();
226 codec_->Release();
stefan@webrtc.orga0a91d82013-08-22 09:29:56 +0000227 if (external_codec_)
228 external_codec_->Release();
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000229 network_->Release();
230 rtp_rtcp_->Release();
231}
232
233void VideoSendStream::PutFrame(const I420VideoFrame& frame,
pbos@webrtc.orgd9f91852013-05-23 12:37:11 +0000234 uint32_t time_since_capture_ms) {
235 // TODO(pbos): frame_copy should happen after the VideoProcessingModule has
236 // resized the frame.
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000237 I420VideoFrame frame_copy;
238 frame_copy.CopyFrame(frame);
239
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000240 ViEVideoFrameI420 vf;
241
242 // TODO(pbos): This represents a memcpy step and is only required because
243 // external_capture_ only takes ViEVideoFrameI420s.
244 vf.y_plane = frame_copy.buffer(kYPlane);
245 vf.u_plane = frame_copy.buffer(kUPlane);
246 vf.v_plane = frame_copy.buffer(kVPlane);
247 vf.y_pitch = frame.stride(kYPlane);
248 vf.u_pitch = frame.stride(kUPlane);
249 vf.v_pitch = frame.stride(kVPlane);
250 vf.width = frame.width();
251 vf.height = frame.height();
252
pbos@webrtc.orgd9f91852013-05-23 12:37:11 +0000253 external_capture_->IncomingFrameI420(vf, frame.render_time_ms());
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000254
255 if (config_.local_renderer != NULL) {
256 config_.local_renderer->RenderFrame(frame, 0);
257 }
258}
259
pbos@webrtc.orgd8e92c92013-08-23 09:19:30 +0000260VideoSendStreamInput* VideoSendStream::Input() { return this; }
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000261
262void VideoSendStream::StartSend() {
pbos@webrtc.orgd9f91852013-05-23 12:37:11 +0000263 if (video_engine_base_->StartSend(channel_) != 0)
264 abort();
pbos@webrtc.orgbf9bc322013-08-05 12:01:36 +0000265 if (video_engine_base_->StartReceive(channel_) != 0)
266 abort();
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000267}
268
269void VideoSendStream::StopSend() {
pbos@webrtc.orgd9f91852013-05-23 12:37:11 +0000270 if (video_engine_base_->StopSend(channel_) != 0)
271 abort();
pbos@webrtc.orgbf9bc322013-08-05 12:01:36 +0000272 if (video_engine_base_->StopReceive(channel_) != 0)
273 abort();
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000274}
275
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000276bool VideoSendStream::SetTargetBitrate(
pbos@webrtc.orgd9f91852013-05-23 12:37:11 +0000277 int min_bitrate,
278 int max_bitrate,
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000279 const std::vector<SimulcastStream>& streams) {
280 return false;
281}
282
283void VideoSendStream::GetSendCodec(VideoCodec* send_codec) {
284 *send_codec = config_.codec;
285}
286
pbos@webrtc.orgbf9bc322013-08-05 12:01:36 +0000287bool VideoSendStream::DeliverRtcp(const uint8_t* packet, size_t length) {
288 return network_->ReceivedRTCPPacket(
pbos@webrtc.org30c741a2013-08-05 13:25:51 +0000289 channel_, packet, static_cast<int>(length)) == 0;
pbos@webrtc.orgbf9bc322013-08-05 12:01:36 +0000290}
291
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000292} // namespace internal
293} // namespace webrtc