blob: da72877f5f6d3b325b936d18074e3a9066568eaf [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
13#include <vector>
14
15#include "webrtc/common_video/libyuv/include/webrtc_libyuv.h"
16#include "webrtc/video_engine/include/vie_base.h"
17#include "webrtc/video_engine/include/vie_capture.h"
18#include "webrtc/video_engine/include/vie_codec.h"
stefan@webrtc.orga0a91d82013-08-22 09:29:56 +000019#include "webrtc/video_engine/include/vie_external_codec.h"
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +000020#include "webrtc/video_engine/include/vie_network.h"
21#include "webrtc/video_engine/include/vie_rtp_rtcp.h"
22#include "webrtc/video_engine/new_include/video_send_stream.h"
23
24namespace webrtc {
25namespace internal {
26
mflodman@webrtc.orgecbeb2b2013-07-23 11:35:00 +000027// Super simple and temporary overuse logic. This will move to the application
28// as soon as the new API allows changing send codec on the fly.
29class ResolutionAdaptor : public webrtc::CpuOveruseObserver {
30 public:
31 ResolutionAdaptor(ViECodec* codec, int channel, size_t width, size_t height)
32 : codec_(codec),
33 channel_(channel),
34 max_width_(width),
35 max_height_(height) {}
36
37 virtual ~ResolutionAdaptor() {}
38
39 virtual void OveruseDetected() OVERRIDE {
40 VideoCodec codec;
41 if (codec_->GetSendCodec(channel_, codec) != 0)
42 return;
43
44 if (codec.width / 2 < min_width || codec.height / 2 < min_height)
45 return;
46
47 codec.width /= 2;
48 codec.height /= 2;
49 codec_->SetSendCodec(channel_, codec);
50 }
51
52 virtual void NormalUsage() OVERRIDE {
53 VideoCodec codec;
54 if (codec_->GetSendCodec(channel_, codec) != 0)
55 return;
56
57 if (codec.width * 2u > max_width_ || codec.height * 2u > max_height_)
58 return;
59
60 codec.width *= 2;
61 codec.height *= 2;
62 codec_->SetSendCodec(channel_, codec);
63 }
64
65 private:
66 // Temporary and arbitrary chosen minimum resolution.
67 static const size_t min_width = 160;
68 static const size_t min_height = 120;
69
70 ViECodec* codec_;
71 const int channel_;
72
73 const size_t max_width_;
74 const size_t max_height_;
75};
76
pbos@webrtc.org12d5ede2013-07-09 08:02:33 +000077VideoSendStream::VideoSendStream(newapi::Transport* transport,
mflodman@webrtc.orgecbeb2b2013-07-23 11:35:00 +000078 bool overuse_detection,
pbos@webrtc.org12d5ede2013-07-09 08:02:33 +000079 webrtc::VideoEngine* video_engine,
80 const newapi::VideoSendStream::Config& config)
stefan@webrtc.orga0a91d82013-08-22 09:29:56 +000081 : transport_(transport), config_(config), external_codec_(NULL) {
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +000082
83 if (config_.codec.numberOfSimulcastStreams > 0) {
84 assert(config_.rtp.ssrcs.size() == config_.codec.numberOfSimulcastStreams);
85 } else {
86 assert(config_.rtp.ssrcs.size() == 1);
87 }
88
89 video_engine_base_ = ViEBase::GetInterface(video_engine);
90 video_engine_base_->CreateChannel(channel_);
91 assert(channel_ != -1);
92
93 rtp_rtcp_ = ViERTP_RTCP::GetInterface(video_engine);
94 assert(rtp_rtcp_ != NULL);
95
stefan@webrtc.orga0a91d82013-08-22 09:29:56 +000096 if (config_.rtp.ssrcs.size() == 1) {
97 rtp_rtcp_->SetLocalSSRC(channel_, config_.rtp.ssrcs[0]);
98 } else {
99 for (size_t i = 0; i < config_.rtp.ssrcs.size(); ++i) {
100 rtp_rtcp_->SetLocalSSRC(channel_, config_.rtp.ssrcs[i],
101 kViEStreamTypeNormal, i);
102 }
103 }
pbos@webrtc.orgbf9bc322013-08-05 12:01:36 +0000104 rtp_rtcp_->SetNACKStatus(channel_, config_.rtp.nack.rtp_history_ms > 0);
stefan@webrtc.orga0a91d82013-08-22 09:29:56 +0000105 rtp_rtcp_->SetTransmissionSmoothingStatus(channel_, config_.pacing);
106 rtp_rtcp_->SetSendTimestampOffsetStatus(channel_, true, 1);
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000107
108 capture_ = ViECapture::GetInterface(video_engine);
109 capture_->AllocateExternalCaptureDevice(capture_id_, external_capture_);
110 capture_->ConnectCaptureDevice(capture_id_, channel_);
111
112 network_ = ViENetwork::GetInterface(video_engine);
113 assert(network_ != NULL);
114
115 network_->RegisterSendTransport(channel_, *this);
116
stefan@webrtc.orga0a91d82013-08-22 09:29:56 +0000117 if (config.encoder) {
118 external_codec_ = ViEExternalCodec::GetInterface(video_engine);
119 if (external_codec_->RegisterExternalSendCodec(
120 channel_, config.codec.plType, config.encoder,
121 config.internal_source) != 0) {
122 abort();
123 }
124 }
125
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000126 codec_ = ViECodec::GetInterface(video_engine);
127 if (codec_->SetSendCodec(channel_, config_.codec) != 0) {
128 abort();
129 }
mflodman@webrtc.orgecbeb2b2013-07-23 11:35:00 +0000130
131 if (overuse_detection) {
132 overuse_observer_.reset(
133 new ResolutionAdaptor(codec_, channel_, config_.codec.width,
134 config_.codec.height));
135 video_engine_base_->RegisterCpuOveruseObserver(channel_,
136 overuse_observer_.get());
137 }
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000138}
139
140VideoSendStream::~VideoSendStream() {
141 network_->DeregisterSendTransport(channel_);
142 video_engine_base_->DeleteChannel(channel_);
143
144 capture_->DisconnectCaptureDevice(channel_);
145 capture_->ReleaseCaptureDevice(capture_id_);
146
stefan@webrtc.orga0a91d82013-08-22 09:29:56 +0000147 if (external_codec_) {
148 external_codec_->DeRegisterExternalSendCodec(channel_,
149 config_.codec.plType);
150 }
151
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000152 video_engine_base_->Release();
153 capture_->Release();
154 codec_->Release();
stefan@webrtc.orga0a91d82013-08-22 09:29:56 +0000155 if (external_codec_)
156 external_codec_->Release();
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000157 network_->Release();
158 rtp_rtcp_->Release();
159}
160
161void VideoSendStream::PutFrame(const I420VideoFrame& frame,
pbos@webrtc.orgd9f91852013-05-23 12:37:11 +0000162 uint32_t time_since_capture_ms) {
163 // TODO(pbos): frame_copy should happen after the VideoProcessingModule has
164 // resized the frame.
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000165 I420VideoFrame frame_copy;
166 frame_copy.CopyFrame(frame);
167
168 if (config_.pre_encode_callback != NULL) {
169 config_.pre_encode_callback->FrameCallback(&frame_copy);
170 }
171
172 ViEVideoFrameI420 vf;
173
174 // TODO(pbos): This represents a memcpy step and is only required because
175 // external_capture_ only takes ViEVideoFrameI420s.
176 vf.y_plane = frame_copy.buffer(kYPlane);
177 vf.u_plane = frame_copy.buffer(kUPlane);
178 vf.v_plane = frame_copy.buffer(kVPlane);
179 vf.y_pitch = frame.stride(kYPlane);
180 vf.u_pitch = frame.stride(kUPlane);
181 vf.v_pitch = frame.stride(kVPlane);
182 vf.width = frame.width();
183 vf.height = frame.height();
184
pbos@webrtc.orgd9f91852013-05-23 12:37:11 +0000185 external_capture_->IncomingFrameI420(vf, frame.render_time_ms());
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000186
187 if (config_.local_renderer != NULL) {
188 config_.local_renderer->RenderFrame(frame, 0);
189 }
190}
191
192newapi::VideoSendStreamInput* VideoSendStream::Input() { return this; }
193
194void VideoSendStream::StartSend() {
pbos@webrtc.orgd9f91852013-05-23 12:37:11 +0000195 if (video_engine_base_->StartSend(channel_) != 0)
196 abort();
pbos@webrtc.orgbf9bc322013-08-05 12:01:36 +0000197 if (video_engine_base_->StartReceive(channel_) != 0)
198 abort();
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000199}
200
201void VideoSendStream::StopSend() {
pbos@webrtc.orgd9f91852013-05-23 12:37:11 +0000202 if (video_engine_base_->StopSend(channel_) != 0)
203 abort();
pbos@webrtc.orgbf9bc322013-08-05 12:01:36 +0000204 if (video_engine_base_->StopReceive(channel_) != 0)
205 abort();
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000206}
207
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000208bool VideoSendStream::SetTargetBitrate(
pbos@webrtc.orgd9f91852013-05-23 12:37:11 +0000209 int min_bitrate,
210 int max_bitrate,
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000211 const std::vector<SimulcastStream>& streams) {
212 return false;
213}
214
215void VideoSendStream::GetSendCodec(VideoCodec* send_codec) {
216 *send_codec = config_.codec;
217}
218
pbos@webrtc.orgd9f91852013-05-23 12:37:11 +0000219int VideoSendStream::SendPacket(int /*channel*/,
220 const void* packet,
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000221 int length) {
222 // TODO(pbos): Lock these methods and the destructor so it can't be processing
223 // a packet when the destructor has been called.
224 assert(length >= 0);
pbos@webrtc.org12d5ede2013-07-09 08:02:33 +0000225 bool success = transport_->SendRTP(static_cast<const uint8_t*>(packet),
226 static_cast<size_t>(length));
stefan@webrtc.orga0a91d82013-08-22 09:29:56 +0000227 return success ? length : -1;
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000228}
229
pbos@webrtc.orgd9f91852013-05-23 12:37:11 +0000230int VideoSendStream::SendRTCPPacket(int /*channel*/,
231 const void* packet,
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000232 int length) {
233 assert(length >= 0);
pbos@webrtc.org12d5ede2013-07-09 08:02:33 +0000234 bool success = transport_->SendRTCP(static_cast<const uint8_t*>(packet),
235 static_cast<size_t>(length));
stefan@webrtc.orga0a91d82013-08-22 09:29:56 +0000236 return success ? length : -1;
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000237}
238
pbos@webrtc.orgbf9bc322013-08-05 12:01:36 +0000239bool VideoSendStream::DeliverRtcp(const uint8_t* packet, size_t length) {
240 return network_->ReceivedRTCPPacket(
pbos@webrtc.org30c741a2013-08-05 13:25:51 +0000241 channel_, packet, static_cast<int>(length)) == 0;
pbos@webrtc.orgbf9bc322013-08-05 12:01:36 +0000242}
243
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000244} // namespace internal
245} // namespace webrtc