blob: 7d9e6803204d6651ea1677c61112fb37cfe66a88 [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"
19#include "webrtc/video_engine/include/vie_network.h"
20#include "webrtc/video_engine/include/vie_rtp_rtcp.h"
21#include "webrtc/video_engine/new_include/video_send_stream.h"
22
23namespace webrtc {
24namespace internal {
25
mflodman@webrtc.orgecbeb2b2013-07-23 11:35:00 +000026// Super simple and temporary overuse logic. This will move to the application
27// as soon as the new API allows changing send codec on the fly.
28class ResolutionAdaptor : public webrtc::CpuOveruseObserver {
29 public:
30 ResolutionAdaptor(ViECodec* codec, int channel, size_t width, size_t height)
31 : codec_(codec),
32 channel_(channel),
33 max_width_(width),
34 max_height_(height) {}
35
36 virtual ~ResolutionAdaptor() {}
37
38 virtual void OveruseDetected() OVERRIDE {
39 VideoCodec codec;
40 if (codec_->GetSendCodec(channel_, codec) != 0)
41 return;
42
43 if (codec.width / 2 < min_width || codec.height / 2 < min_height)
44 return;
45
46 codec.width /= 2;
47 codec.height /= 2;
48 codec_->SetSendCodec(channel_, codec);
49 }
50
51 virtual void NormalUsage() OVERRIDE {
52 VideoCodec codec;
53 if (codec_->GetSendCodec(channel_, codec) != 0)
54 return;
55
56 if (codec.width * 2u > max_width_ || codec.height * 2u > max_height_)
57 return;
58
59 codec.width *= 2;
60 codec.height *= 2;
61 codec_->SetSendCodec(channel_, codec);
62 }
63
64 private:
65 // Temporary and arbitrary chosen minimum resolution.
66 static const size_t min_width = 160;
67 static const size_t min_height = 120;
68
69 ViECodec* codec_;
70 const int channel_;
71
72 const size_t max_width_;
73 const size_t max_height_;
74};
75
pbos@webrtc.org12d5ede2013-07-09 08:02:33 +000076VideoSendStream::VideoSendStream(newapi::Transport* transport,
mflodman@webrtc.orgecbeb2b2013-07-23 11:35:00 +000077 bool overuse_detection,
pbos@webrtc.org12d5ede2013-07-09 08:02:33 +000078 webrtc::VideoEngine* video_engine,
79 const newapi::VideoSendStream::Config& config)
pbos@webrtc.org2c343fc2013-06-05 11:33:21 +000080 : transport_(transport), config_(config) {
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +000081
82 if (config_.codec.numberOfSimulcastStreams > 0) {
83 assert(config_.rtp.ssrcs.size() == config_.codec.numberOfSimulcastStreams);
84 } else {
85 assert(config_.rtp.ssrcs.size() == 1);
86 }
87
88 video_engine_base_ = ViEBase::GetInterface(video_engine);
89 video_engine_base_->CreateChannel(channel_);
90 assert(channel_ != -1);
91
92 rtp_rtcp_ = ViERTP_RTCP::GetInterface(video_engine);
93 assert(rtp_rtcp_ != NULL);
94
95 assert(config_.rtp.ssrcs.size() == 1);
96 rtp_rtcp_->SetLocalSSRC(channel_, config_.rtp.ssrcs[0]);
pbos@webrtc.orgbf9bc322013-08-05 12:01:36 +000097 rtp_rtcp_->SetNACKStatus(channel_, config_.rtp.nack.rtp_history_ms > 0);
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +000098
99 capture_ = ViECapture::GetInterface(video_engine);
100 capture_->AllocateExternalCaptureDevice(capture_id_, external_capture_);
101 capture_->ConnectCaptureDevice(capture_id_, channel_);
102
103 network_ = ViENetwork::GetInterface(video_engine);
104 assert(network_ != NULL);
105
106 network_->RegisterSendTransport(channel_, *this);
107
108 codec_ = ViECodec::GetInterface(video_engine);
109 if (codec_->SetSendCodec(channel_, config_.codec) != 0) {
110 abort();
111 }
mflodman@webrtc.orgecbeb2b2013-07-23 11:35:00 +0000112
113 if (overuse_detection) {
114 overuse_observer_.reset(
115 new ResolutionAdaptor(codec_, channel_, config_.codec.width,
116 config_.codec.height));
117 video_engine_base_->RegisterCpuOveruseObserver(channel_,
118 overuse_observer_.get());
119 }
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000120}
121
122VideoSendStream::~VideoSendStream() {
123 network_->DeregisterSendTransport(channel_);
124 video_engine_base_->DeleteChannel(channel_);
125
126 capture_->DisconnectCaptureDevice(channel_);
127 capture_->ReleaseCaptureDevice(capture_id_);
128
129 video_engine_base_->Release();
130 capture_->Release();
131 codec_->Release();
132 network_->Release();
133 rtp_rtcp_->Release();
134}
135
136void VideoSendStream::PutFrame(const I420VideoFrame& frame,
pbos@webrtc.orgd9f91852013-05-23 12:37:11 +0000137 uint32_t time_since_capture_ms) {
138 // TODO(pbos): frame_copy should happen after the VideoProcessingModule has
139 // resized the frame.
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000140 I420VideoFrame frame_copy;
141 frame_copy.CopyFrame(frame);
142
143 if (config_.pre_encode_callback != NULL) {
144 config_.pre_encode_callback->FrameCallback(&frame_copy);
145 }
146
147 ViEVideoFrameI420 vf;
148
149 // TODO(pbos): This represents a memcpy step and is only required because
150 // external_capture_ only takes ViEVideoFrameI420s.
151 vf.y_plane = frame_copy.buffer(kYPlane);
152 vf.u_plane = frame_copy.buffer(kUPlane);
153 vf.v_plane = frame_copy.buffer(kVPlane);
154 vf.y_pitch = frame.stride(kYPlane);
155 vf.u_pitch = frame.stride(kUPlane);
156 vf.v_pitch = frame.stride(kVPlane);
157 vf.width = frame.width();
158 vf.height = frame.height();
159
pbos@webrtc.orgd9f91852013-05-23 12:37:11 +0000160 external_capture_->IncomingFrameI420(vf, frame.render_time_ms());
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000161
162 if (config_.local_renderer != NULL) {
163 config_.local_renderer->RenderFrame(frame, 0);
164 }
165}
166
167newapi::VideoSendStreamInput* VideoSendStream::Input() { return this; }
168
169void VideoSendStream::StartSend() {
pbos@webrtc.orgd9f91852013-05-23 12:37:11 +0000170 if (video_engine_base_->StartSend(channel_) != 0)
171 abort();
pbos@webrtc.orgbf9bc322013-08-05 12:01:36 +0000172 if (video_engine_base_->StartReceive(channel_) != 0)
173 abort();
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000174}
175
176void VideoSendStream::StopSend() {
pbos@webrtc.orgd9f91852013-05-23 12:37:11 +0000177 if (video_engine_base_->StopSend(channel_) != 0)
178 abort();
pbos@webrtc.orgbf9bc322013-08-05 12:01:36 +0000179 if (video_engine_base_->StopReceive(channel_) != 0)
180 abort();
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000181}
182
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000183bool VideoSendStream::SetTargetBitrate(
pbos@webrtc.orgd9f91852013-05-23 12:37:11 +0000184 int min_bitrate,
185 int max_bitrate,
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000186 const std::vector<SimulcastStream>& streams) {
187 return false;
188}
189
190void VideoSendStream::GetSendCodec(VideoCodec* send_codec) {
191 *send_codec = config_.codec;
192}
193
pbos@webrtc.orgd9f91852013-05-23 12:37:11 +0000194int VideoSendStream::SendPacket(int /*channel*/,
195 const void* packet,
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000196 int length) {
197 // TODO(pbos): Lock these methods and the destructor so it can't be processing
198 // a packet when the destructor has been called.
199 assert(length >= 0);
pbos@webrtc.org12d5ede2013-07-09 08:02:33 +0000200 bool success = transport_->SendRTP(static_cast<const uint8_t*>(packet),
201 static_cast<size_t>(length));
202 return success ? 0 : -1;
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000203}
204
pbos@webrtc.orgd9f91852013-05-23 12:37:11 +0000205int VideoSendStream::SendRTCPPacket(int /*channel*/,
206 const void* packet,
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000207 int length) {
208 assert(length >= 0);
pbos@webrtc.org12d5ede2013-07-09 08:02:33 +0000209 bool success = transport_->SendRTCP(static_cast<const uint8_t*>(packet),
210 static_cast<size_t>(length));
211 return success ? 0 : -1;
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000212}
213
pbos@webrtc.orgbf9bc322013-08-05 12:01:36 +0000214bool VideoSendStream::DeliverRtcp(const uint8_t* packet, size_t length) {
215 return network_->ReceivedRTCPPacket(
pbos@webrtc.org30c741a2013-08-05 13:25:51 +0000216 channel_, packet, static_cast<int>(length)) == 0;
pbos@webrtc.orgbf9bc322013-08-05 12:01:36 +0000217}
218
pbos@webrtc.orgdc8c8832013-05-16 12:08:03 +0000219} // namespace internal
220} // namespace webrtc