blob: 76d8bc3d2c0491bf3bb7d2bb0498154a34f5e8e5 [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.orgdc8c8832013-05-16 12:08:03 +000022#include "webrtc/video_engine/include/vie_network.h"
23#include "webrtc/video_engine/include/vie_rtp_rtcp.h"
24#include "webrtc/video_engine/new_include/video_send_stream.h"
25
26namespace webrtc {
27namespace internal {
28
mflodman@webrtc.orgecbeb2b2013-07-23 11:35:00 +000029// Super simple and temporary overuse logic. This will move to the application
30// as soon as the new API allows changing send codec on the fly.
31class ResolutionAdaptor : public webrtc::CpuOveruseObserver {
32 public:
33 ResolutionAdaptor(ViECodec* codec, int channel, size_t width, size_t height)
34 : codec_(codec),
35 channel_(channel),
36 max_width_(width),
37 max_height_(height) {}
38
39 virtual ~ResolutionAdaptor() {}
40
41 virtual void OveruseDetected() OVERRIDE {
42 VideoCodec codec;
43 if (codec_->GetSendCodec(channel_, codec) != 0)
44 return;
45
46 if (codec.width / 2 < min_width || codec.height / 2 < min_height)
47 return;
48
49 codec.width /= 2;
50 codec.height /= 2;
51 codec_->SetSendCodec(channel_, codec);
52 }
53
54 virtual void NormalUsage() OVERRIDE {
55 VideoCodec codec;
56 if (codec_->GetSendCodec(channel_, codec) != 0)
57 return;
58
59 if (codec.width * 2u > max_width_ || codec.height * 2u > max_height_)
60 return;
61
62 codec.width *= 2;
63 codec.height *= 2;
64 codec_->SetSendCodec(channel_, codec);
65 }
66
67 private:
68 // Temporary and arbitrary chosen minimum resolution.
69 static const size_t min_width = 160;
70 static const size_t min_height = 120;
71
72 ViECodec* codec_;
73 const int channel_;
74
75 const size_t max_width_;
76 const size_t max_height_;
77};
78
pbos@webrtc.org12d5ede2013-07-09 08:02:33 +000079VideoSendStream::VideoSendStream(newapi::Transport* transport,
mflodman@webrtc.orgecbeb2b2013-07-23 11:35:00 +000080 bool overuse_detection,
pbos@webrtc.org12d5ede2013-07-09 08:02:33 +000081 webrtc::VideoEngine* video_engine,
pbos@webrtc.orgd8e92c92013-08-23 09:19:30 +000082 const VideoSendStream::Config& config)
stefan@webrtc.orga0a91d82013-08-22 09:29:56 +000083 : transport_(transport), config_(config), external_codec_(NULL) {
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +000084
85 if (config_.codec.numberOfSimulcastStreams > 0) {
86 assert(config_.rtp.ssrcs.size() == config_.codec.numberOfSimulcastStreams);
87 } else {
88 assert(config_.rtp.ssrcs.size() == 1);
89 }
90
91 video_engine_base_ = ViEBase::GetInterface(video_engine);
92 video_engine_base_->CreateChannel(channel_);
93 assert(channel_ != -1);
94
95 rtp_rtcp_ = ViERTP_RTCP::GetInterface(video_engine);
96 assert(rtp_rtcp_ != NULL);
97
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) {
102 rtp_rtcp_->SetLocalSSRC(channel_, config_.rtp.ssrcs[i],
103 kViEStreamTypeNormal, i);
104 }
105 }
pbos@webrtc.orgbf9bc322013-08-05 12:01:36 +0000106 rtp_rtcp_->SetNACKStatus(channel_, config_.rtp.nack.rtp_history_ms > 0);
stefan@webrtc.orga0a91d82013-08-22 09:29:56 +0000107 rtp_rtcp_->SetTransmissionSmoothingStatus(channel_, config_.pacing);
pbos@webrtc.org905cebd2013-09-11 10:14:56 +0000108
109 for (size_t i = 0; i < config_.rtp.extensions.size(); ++i) {
110 const std::string& extension = config_.rtp.extensions[i].name;
111 int id = config_.rtp.extensions[i].id;
112 if (extension == "toffset") {
113 if (rtp_rtcp_->SetSendTimestampOffsetStatus(channel_, true, id) != 0)
114 abort();
pbos@webrtc.orge22b7612013-09-11 19:00:39 +0000115 } else if (extension == "abs-send-time") {
116 if (rtp_rtcp_->SetSendAbsoluteSendTimeStatus(channel_, true, id) != 0)
117 abort();
pbos@webrtc.org905cebd2013-09-11 10:14:56 +0000118 } else {
119 abort(); // Unsupported extension.
120 }
121 }
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000122
pbos@webrtc.orgdebc6722013-08-22 09:42:17 +0000123 char rtcp_cname[ViERTP_RTCP::KMaxRTCPCNameLength];
124 assert(config_.rtp.c_name.length() < ViERTP_RTCP::KMaxRTCPCNameLength);
125 strncpy(rtcp_cname, config_.rtp.c_name.c_str(), sizeof(rtcp_cname) - 1);
126 rtcp_cname[sizeof(rtcp_cname) - 1] = '\0';
127
128 rtp_rtcp_->SetRTCPCName(channel_, rtcp_cname);
129
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000130 capture_ = ViECapture::GetInterface(video_engine);
131 capture_->AllocateExternalCaptureDevice(capture_id_, external_capture_);
132 capture_->ConnectCaptureDevice(capture_id_, channel_);
133
134 network_ = ViENetwork::GetInterface(video_engine);
135 assert(network_ != NULL);
136
137 network_->RegisterSendTransport(channel_, *this);
138
stefan@webrtc.orga0a91d82013-08-22 09:29:56 +0000139 if (config.encoder) {
140 external_codec_ = ViEExternalCodec::GetInterface(video_engine);
141 if (external_codec_->RegisterExternalSendCodec(
142 channel_, config.codec.plType, config.encoder,
143 config.internal_source) != 0) {
144 abort();
145 }
146 }
147
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000148 codec_ = ViECodec::GetInterface(video_engine);
149 if (codec_->SetSendCodec(channel_, config_.codec) != 0) {
150 abort();
151 }
mflodman@webrtc.orgecbeb2b2013-07-23 11:35:00 +0000152
153 if (overuse_detection) {
154 overuse_observer_.reset(
155 new ResolutionAdaptor(codec_, channel_, config_.codec.width,
156 config_.codec.height));
157 video_engine_base_->RegisterCpuOveruseObserver(channel_,
pbos@webrtc.org905cebd2013-09-11 10:14:56 +0000158 overuse_observer_.get());
mflodman@webrtc.orgecbeb2b2013-07-23 11:35:00 +0000159 }
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000160}
161
162VideoSendStream::~VideoSendStream() {
163 network_->DeregisterSendTransport(channel_);
164 video_engine_base_->DeleteChannel(channel_);
165
166 capture_->DisconnectCaptureDevice(channel_);
167 capture_->ReleaseCaptureDevice(capture_id_);
168
stefan@webrtc.orga0a91d82013-08-22 09:29:56 +0000169 if (external_codec_) {
170 external_codec_->DeRegisterExternalSendCodec(channel_,
171 config_.codec.plType);
172 }
173
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000174 video_engine_base_->Release();
175 capture_->Release();
176 codec_->Release();
stefan@webrtc.orga0a91d82013-08-22 09:29:56 +0000177 if (external_codec_)
178 external_codec_->Release();
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000179 network_->Release();
180 rtp_rtcp_->Release();
181}
182
183void VideoSendStream::PutFrame(const I420VideoFrame& frame,
pbos@webrtc.orgd9f91852013-05-23 12:37:11 +0000184 uint32_t time_since_capture_ms) {
185 // TODO(pbos): frame_copy should happen after the VideoProcessingModule has
186 // resized the frame.
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000187 I420VideoFrame frame_copy;
188 frame_copy.CopyFrame(frame);
189
190 if (config_.pre_encode_callback != NULL) {
191 config_.pre_encode_callback->FrameCallback(&frame_copy);
192 }
193
194 ViEVideoFrameI420 vf;
195
196 // TODO(pbos): This represents a memcpy step and is only required because
197 // external_capture_ only takes ViEVideoFrameI420s.
198 vf.y_plane = frame_copy.buffer(kYPlane);
199 vf.u_plane = frame_copy.buffer(kUPlane);
200 vf.v_plane = frame_copy.buffer(kVPlane);
201 vf.y_pitch = frame.stride(kYPlane);
202 vf.u_pitch = frame.stride(kUPlane);
203 vf.v_pitch = frame.stride(kVPlane);
204 vf.width = frame.width();
205 vf.height = frame.height();
206
pbos@webrtc.orgd9f91852013-05-23 12:37:11 +0000207 external_capture_->IncomingFrameI420(vf, frame.render_time_ms());
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000208
209 if (config_.local_renderer != NULL) {
210 config_.local_renderer->RenderFrame(frame, 0);
211 }
212}
213
pbos@webrtc.orgd8e92c92013-08-23 09:19:30 +0000214VideoSendStreamInput* VideoSendStream::Input() { return this; }
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000215
216void VideoSendStream::StartSend() {
pbos@webrtc.orgd9f91852013-05-23 12:37:11 +0000217 if (video_engine_base_->StartSend(channel_) != 0)
218 abort();
pbos@webrtc.orgbf9bc322013-08-05 12:01:36 +0000219 if (video_engine_base_->StartReceive(channel_) != 0)
220 abort();
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000221}
222
223void VideoSendStream::StopSend() {
pbos@webrtc.orgd9f91852013-05-23 12:37:11 +0000224 if (video_engine_base_->StopSend(channel_) != 0)
225 abort();
pbos@webrtc.orgbf9bc322013-08-05 12:01:36 +0000226 if (video_engine_base_->StopReceive(channel_) != 0)
227 abort();
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000228}
229
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000230bool VideoSendStream::SetTargetBitrate(
pbos@webrtc.orgd9f91852013-05-23 12:37:11 +0000231 int min_bitrate,
232 int max_bitrate,
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000233 const std::vector<SimulcastStream>& streams) {
234 return false;
235}
236
237void VideoSendStream::GetSendCodec(VideoCodec* send_codec) {
238 *send_codec = config_.codec;
239}
240
pbos@webrtc.orgd9f91852013-05-23 12:37:11 +0000241int VideoSendStream::SendPacket(int /*channel*/,
242 const void* packet,
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000243 int length) {
244 // TODO(pbos): Lock these methods and the destructor so it can't be processing
245 // a packet when the destructor has been called.
246 assert(length >= 0);
pbos@webrtc.org12d5ede2013-07-09 08:02:33 +0000247 bool success = transport_->SendRTP(static_cast<const uint8_t*>(packet),
248 static_cast<size_t>(length));
stefan@webrtc.orga0a91d82013-08-22 09:29:56 +0000249 return success ? length : -1;
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000250}
251
pbos@webrtc.orgd9f91852013-05-23 12:37:11 +0000252int VideoSendStream::SendRTCPPacket(int /*channel*/,
253 const void* packet,
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000254 int length) {
255 assert(length >= 0);
pbos@webrtc.org12d5ede2013-07-09 08:02:33 +0000256 bool success = transport_->SendRTCP(static_cast<const uint8_t*>(packet),
257 static_cast<size_t>(length));
stefan@webrtc.orga0a91d82013-08-22 09:29:56 +0000258 return success ? length : -1;
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000259}
260
pbos@webrtc.orgbf9bc322013-08-05 12:01:36 +0000261bool VideoSendStream::DeliverRtcp(const uint8_t* packet, size_t length) {
262 return network_->ReceivedRTCPPacket(
pbos@webrtc.org30c741a2013-08-05 13:25:51 +0000263 channel_, packet, static_cast<int>(length)) == 0;
pbos@webrtc.orgbf9bc322013-08-05 12:01:36 +0000264}
265
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000266} // namespace internal
267} // namespace webrtc