blob: d238b95773961e4d91d5f8080b1a6a4239152b0f [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
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +000015#include <vector>
16
17#include "webrtc/common_video/libyuv/include/webrtc_libyuv.h"
18#include "webrtc/video_engine/include/vie_base.h"
19#include "webrtc/video_engine/include/vie_capture.h"
20#include "webrtc/video_engine/include/vie_codec.h"
stefan@webrtc.orga0a91d82013-08-22 09:29:56 +000021#include "webrtc/video_engine/include/vie_external_codec.h"
pbos@webrtc.org3ba57eb2013-10-21 10:34:43 +000022#include "webrtc/video_engine/include/vie_image_process.h"
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +000023#include "webrtc/video_engine/include/vie_network.h"
24#include "webrtc/video_engine/include/vie_rtp_rtcp.h"
25#include "webrtc/video_engine/new_include/video_send_stream.h"
26
27namespace webrtc {
28namespace internal {
29
mflodman@webrtc.orgecbeb2b2013-07-23 11:35:00 +000030// Super simple and temporary overuse logic. This will move to the application
31// as soon as the new API allows changing send codec on the fly.
32class ResolutionAdaptor : public webrtc::CpuOveruseObserver {
33 public:
34 ResolutionAdaptor(ViECodec* codec, int channel, size_t width, size_t height)
35 : codec_(codec),
36 channel_(channel),
37 max_width_(width),
38 max_height_(height) {}
39
40 virtual ~ResolutionAdaptor() {}
41
42 virtual void OveruseDetected() OVERRIDE {
43 VideoCodec codec;
44 if (codec_->GetSendCodec(channel_, codec) != 0)
45 return;
46
47 if (codec.width / 2 < min_width || codec.height / 2 < min_height)
48 return;
49
50 codec.width /= 2;
51 codec.height /= 2;
52 codec_->SetSendCodec(channel_, codec);
53 }
54
55 virtual void NormalUsage() OVERRIDE {
56 VideoCodec codec;
57 if (codec_->GetSendCodec(channel_, codec) != 0)
58 return;
59
60 if (codec.width * 2u > max_width_ || codec.height * 2u > max_height_)
61 return;
62
63 codec.width *= 2;
64 codec.height *= 2;
65 codec_->SetSendCodec(channel_, codec);
66 }
67
68 private:
69 // Temporary and arbitrary chosen minimum resolution.
70 static const size_t min_width = 160;
71 static const size_t min_height = 120;
72
73 ViECodec* codec_;
74 const int channel_;
75
76 const size_t max_width_;
77 const size_t max_height_;
78};
79
pbos@webrtc.org12d5ede2013-07-09 08:02:33 +000080VideoSendStream::VideoSendStream(newapi::Transport* transport,
mflodman@webrtc.orgecbeb2b2013-07-23 11:35:00 +000081 bool overuse_detection,
pbos@webrtc.org12d5ede2013-07-09 08:02:33 +000082 webrtc::VideoEngine* video_engine,
pbos@webrtc.orgd8e92c92013-08-23 09:19:30 +000083 const VideoSendStream::Config& config)
pbos@webrtc.org26d75f32013-09-18 11:52:42 +000084 : transport_adapter_(transport), config_(config), external_codec_(NULL) {
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +000085
86 if (config_.codec.numberOfSimulcastStreams > 0) {
87 assert(config_.rtp.ssrcs.size() == config_.codec.numberOfSimulcastStreams);
88 } else {
89 assert(config_.rtp.ssrcs.size() == 1);
90 }
91
92 video_engine_base_ = ViEBase::GetInterface(video_engine);
93 video_engine_base_->CreateChannel(channel_);
94 assert(channel_ != -1);
95
96 rtp_rtcp_ = ViERTP_RTCP::GetInterface(video_engine);
97 assert(rtp_rtcp_ != NULL);
98
stefan@webrtc.orga0a91d82013-08-22 09:29:56 +000099 if (config_.rtp.ssrcs.size() == 1) {
100 rtp_rtcp_->SetLocalSSRC(channel_, config_.rtp.ssrcs[0]);
101 } else {
102 for (size_t i = 0; i < config_.rtp.ssrcs.size(); ++i) {
103 rtp_rtcp_->SetLocalSSRC(channel_, config_.rtp.ssrcs[i],
104 kViEStreamTypeNormal, i);
105 }
106 }
stefan@webrtc.orga0a91d82013-08-22 09:29:56 +0000107 rtp_rtcp_->SetTransmissionSmoothingStatus(channel_, config_.pacing);
pbos@webrtc.orgf952fce2013-09-16 13:01:47 +0000108 if (!config_.rtp.rtx.ssrcs.empty()) {
109 assert(config_.rtp.rtx.ssrcs.size() == config_.rtp.ssrcs.size());
110 for (size_t i = 0; i < config_.rtp.rtx.ssrcs.size(); ++i) {
111 rtp_rtcp_->SetLocalSSRC(
112 channel_, config_.rtp.rtx.ssrcs[i], kViEStreamTypeRtx, i);
113 }
114
115 if (config_.rtp.rtx.rtx_payload_type != 0) {
116 rtp_rtcp_->SetRtxSendPayloadType(channel_,
117 config_.rtp.rtx.rtx_payload_type);
118 }
119 }
pbos@webrtc.org905cebd2013-09-11 10:14:56 +0000120
121 for (size_t i = 0; i < config_.rtp.extensions.size(); ++i) {
122 const std::string& extension = config_.rtp.extensions[i].name;
123 int id = config_.rtp.extensions[i].id;
124 if (extension == "toffset") {
125 if (rtp_rtcp_->SetSendTimestampOffsetStatus(channel_, true, id) != 0)
126 abort();
pbos@webrtc.orge22b7612013-09-11 19:00:39 +0000127 } else if (extension == "abs-send-time") {
128 if (rtp_rtcp_->SetSendAbsoluteSendTimeStatus(channel_, true, id) != 0)
129 abort();
pbos@webrtc.org905cebd2013-09-11 10:14:56 +0000130 } else {
131 abort(); // Unsupported extension.
132 }
133 }
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000134
pbos@webrtc.orgaa693dd2013-09-20 11:56:26 +0000135 // Enable NACK, FEC or both.
136 if (config_.rtp.fec.red_payload_type != -1) {
137 assert(config_.rtp.fec.ulpfec_payload_type != -1);
138 if (config_.rtp.nack.rtp_history_ms > 0) {
139 rtp_rtcp_->SetHybridNACKFECStatus(
140 channel_,
141 true,
142 static_cast<unsigned char>(config_.rtp.fec.red_payload_type),
143 static_cast<unsigned char>(config_.rtp.fec.ulpfec_payload_type));
144 } else {
145 rtp_rtcp_->SetFECStatus(
146 channel_,
147 true,
148 static_cast<unsigned char>(config_.rtp.fec.red_payload_type),
149 static_cast<unsigned char>(config_.rtp.fec.ulpfec_payload_type));
150 }
151 } else {
152 rtp_rtcp_->SetNACKStatus(channel_, config_.rtp.nack.rtp_history_ms > 0);
153 }
154
pbos@webrtc.orgdebc6722013-08-22 09:42:17 +0000155 char rtcp_cname[ViERTP_RTCP::KMaxRTCPCNameLength];
156 assert(config_.rtp.c_name.length() < ViERTP_RTCP::KMaxRTCPCNameLength);
157 strncpy(rtcp_cname, config_.rtp.c_name.c_str(), sizeof(rtcp_cname) - 1);
158 rtcp_cname[sizeof(rtcp_cname) - 1] = '\0';
159
160 rtp_rtcp_->SetRTCPCName(channel_, rtcp_cname);
161
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000162 capture_ = ViECapture::GetInterface(video_engine);
163 capture_->AllocateExternalCaptureDevice(capture_id_, external_capture_);
164 capture_->ConnectCaptureDevice(capture_id_, channel_);
165
166 network_ = ViENetwork::GetInterface(video_engine);
167 assert(network_ != NULL);
168
pbos@webrtc.org26d75f32013-09-18 11:52:42 +0000169 network_->RegisterSendTransport(channel_, transport_adapter_);
sprang@webrtc.org6133dd52013-10-16 13:29:14 +0000170 // 28 to match packet overhead in ModuleRtpRtcpImpl.
171 network_->SetMTU(channel_, config_.rtp.max_packet_size + 28);
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000172
stefan@webrtc.orga0a91d82013-08-22 09:29:56 +0000173 if (config.encoder) {
174 external_codec_ = ViEExternalCodec::GetInterface(video_engine);
175 if (external_codec_->RegisterExternalSendCodec(
176 channel_, config.codec.plType, config.encoder,
177 config.internal_source) != 0) {
178 abort();
179 }
180 }
181
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000182 codec_ = ViECodec::GetInterface(video_engine);
183 if (codec_->SetSendCodec(channel_, config_.codec) != 0) {
184 abort();
185 }
mflodman@webrtc.orgecbeb2b2013-07-23 11:35:00 +0000186
187 if (overuse_detection) {
188 overuse_observer_.reset(
189 new ResolutionAdaptor(codec_, channel_, config_.codec.width,
190 config_.codec.height));
191 video_engine_base_->RegisterCpuOveruseObserver(channel_,
pbos@webrtc.org905cebd2013-09-11 10:14:56 +0000192 overuse_observer_.get());
mflodman@webrtc.orgecbeb2b2013-07-23 11:35:00 +0000193 }
pbos@webrtc.org3ba57eb2013-10-21 10:34:43 +0000194
195 image_process_ = ViEImageProcess::GetInterface(video_engine);
196 image_process_->RegisterPreEncodeCallback(channel_,
197 config_.pre_encode_callback);
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000198}
199
200VideoSendStream::~VideoSendStream() {
pbos@webrtc.org3ba57eb2013-10-21 10:34:43 +0000201 image_process_->DeRegisterPreEncodeCallback(channel_);
202
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000203 network_->DeregisterSendTransport(channel_);
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000204
205 capture_->DisconnectCaptureDevice(channel_);
206 capture_->ReleaseCaptureDevice(capture_id_);
207
stefan@webrtc.orga0a91d82013-08-22 09:29:56 +0000208 if (external_codec_) {
209 external_codec_->DeRegisterExternalSendCodec(channel_,
210 config_.codec.plType);
211 }
212
pbos@webrtc.org3ba57eb2013-10-21 10:34:43 +0000213 video_engine_base_->DeleteChannel(channel_);
214
215 image_process_->Release();
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000216 video_engine_base_->Release();
217 capture_->Release();
218 codec_->Release();
stefan@webrtc.orga0a91d82013-08-22 09:29:56 +0000219 if (external_codec_)
220 external_codec_->Release();
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000221 network_->Release();
222 rtp_rtcp_->Release();
223}
224
225void VideoSendStream::PutFrame(const I420VideoFrame& frame,
pbos@webrtc.orgd9f91852013-05-23 12:37:11 +0000226 uint32_t time_since_capture_ms) {
227 // TODO(pbos): frame_copy should happen after the VideoProcessingModule has
228 // resized the frame.
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000229 I420VideoFrame frame_copy;
230 frame_copy.CopyFrame(frame);
231
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000232 ViEVideoFrameI420 vf;
233
234 // TODO(pbos): This represents a memcpy step and is only required because
235 // external_capture_ only takes ViEVideoFrameI420s.
236 vf.y_plane = frame_copy.buffer(kYPlane);
237 vf.u_plane = frame_copy.buffer(kUPlane);
238 vf.v_plane = frame_copy.buffer(kVPlane);
239 vf.y_pitch = frame.stride(kYPlane);
240 vf.u_pitch = frame.stride(kUPlane);
241 vf.v_pitch = frame.stride(kVPlane);
242 vf.width = frame.width();
243 vf.height = frame.height();
244
pbos@webrtc.orgd9f91852013-05-23 12:37:11 +0000245 external_capture_->IncomingFrameI420(vf, frame.render_time_ms());
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000246
247 if (config_.local_renderer != NULL) {
248 config_.local_renderer->RenderFrame(frame, 0);
249 }
250}
251
pbos@webrtc.orgd8e92c92013-08-23 09:19:30 +0000252VideoSendStreamInput* VideoSendStream::Input() { return this; }
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000253
254void VideoSendStream::StartSend() {
pbos@webrtc.orgd9f91852013-05-23 12:37:11 +0000255 if (video_engine_base_->StartSend(channel_) != 0)
256 abort();
pbos@webrtc.orgbf9bc322013-08-05 12:01:36 +0000257 if (video_engine_base_->StartReceive(channel_) != 0)
258 abort();
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000259}
260
261void VideoSendStream::StopSend() {
pbos@webrtc.orgd9f91852013-05-23 12:37:11 +0000262 if (video_engine_base_->StopSend(channel_) != 0)
263 abort();
pbos@webrtc.orgbf9bc322013-08-05 12:01:36 +0000264 if (video_engine_base_->StopReceive(channel_) != 0)
265 abort();
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000266}
267
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000268bool VideoSendStream::SetTargetBitrate(
pbos@webrtc.orgd9f91852013-05-23 12:37:11 +0000269 int min_bitrate,
270 int max_bitrate,
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000271 const std::vector<SimulcastStream>& streams) {
272 return false;
273}
274
275void VideoSendStream::GetSendCodec(VideoCodec* send_codec) {
276 *send_codec = config_.codec;
277}
278
pbos@webrtc.orgbf9bc322013-08-05 12:01:36 +0000279bool VideoSendStream::DeliverRtcp(const uint8_t* packet, size_t length) {
280 return network_->ReceivedRTCPPacket(
pbos@webrtc.org30c741a2013-08-05 13:25:51 +0000281 channel_, packet, static_cast<int>(length)) == 0;
pbos@webrtc.orgbf9bc322013-08-05 12:01:36 +0000282}
283
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000284} // namespace internal
285} // namespace webrtc