blob: b2401daf9ad0111b68cfc203cee7ef4f688c82d4 [file] [log] [blame]
pbos@webrtc.org29d58392013-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.org6879c8a2013-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.orgaf8d5af2013-07-09 08:02:33 +000076VideoSendStream::VideoSendStream(newapi::Transport* transport,
mflodman@webrtc.org6879c8a2013-07-23 11:35:00 +000077 bool overuse_detection,
pbos@webrtc.orgaf8d5af2013-07-09 08:02:33 +000078 webrtc::VideoEngine* video_engine,
79 const newapi::VideoSendStream::Config& config)
pbos@webrtc.org025f4f12013-06-05 11:33:21 +000080 : transport_(transport), config_(config) {
pbos@webrtc.org29d58392013-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]);
97
98 capture_ = ViECapture::GetInterface(video_engine);
99 capture_->AllocateExternalCaptureDevice(capture_id_, external_capture_);
100 capture_->ConnectCaptureDevice(capture_id_, channel_);
101
102 network_ = ViENetwork::GetInterface(video_engine);
103 assert(network_ != NULL);
104
105 network_->RegisterSendTransport(channel_, *this);
106
107 codec_ = ViECodec::GetInterface(video_engine);
108 if (codec_->SetSendCodec(channel_, config_.codec) != 0) {
109 abort();
110 }
mflodman@webrtc.org6879c8a2013-07-23 11:35:00 +0000111
112 if (overuse_detection) {
113 overuse_observer_.reset(
114 new ResolutionAdaptor(codec_, channel_, config_.codec.width,
115 config_.codec.height));
116 video_engine_base_->RegisterCpuOveruseObserver(channel_,
117 overuse_observer_.get());
118 }
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000119}
120
121VideoSendStream::~VideoSendStream() {
122 network_->DeregisterSendTransport(channel_);
123 video_engine_base_->DeleteChannel(channel_);
124
125 capture_->DisconnectCaptureDevice(channel_);
126 capture_->ReleaseCaptureDevice(capture_id_);
127
128 video_engine_base_->Release();
129 capture_->Release();
130 codec_->Release();
131 network_->Release();
132 rtp_rtcp_->Release();
133}
134
135void VideoSendStream::PutFrame(const I420VideoFrame& frame,
pbos@webrtc.org9b303482013-05-23 12:37:11 +0000136 uint32_t time_since_capture_ms) {
137 // TODO(pbos): frame_copy should happen after the VideoProcessingModule has
138 // resized the frame.
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000139 I420VideoFrame frame_copy;
140 frame_copy.CopyFrame(frame);
141
142 if (config_.pre_encode_callback != NULL) {
143 config_.pre_encode_callback->FrameCallback(&frame_copy);
144 }
145
146 ViEVideoFrameI420 vf;
147
148 // TODO(pbos): This represents a memcpy step and is only required because
149 // external_capture_ only takes ViEVideoFrameI420s.
150 vf.y_plane = frame_copy.buffer(kYPlane);
151 vf.u_plane = frame_copy.buffer(kUPlane);
152 vf.v_plane = frame_copy.buffer(kVPlane);
153 vf.y_pitch = frame.stride(kYPlane);
154 vf.u_pitch = frame.stride(kUPlane);
155 vf.v_pitch = frame.stride(kVPlane);
156 vf.width = frame.width();
157 vf.height = frame.height();
158
pbos@webrtc.org9b303482013-05-23 12:37:11 +0000159 external_capture_->IncomingFrameI420(vf, frame.render_time_ms());
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000160
161 if (config_.local_renderer != NULL) {
162 config_.local_renderer->RenderFrame(frame, 0);
163 }
164}
165
166newapi::VideoSendStreamInput* VideoSendStream::Input() { return this; }
167
168void VideoSendStream::StartSend() {
pbos@webrtc.org9b303482013-05-23 12:37:11 +0000169 if (video_engine_base_->StartSend(channel_) != 0)
170 abort();
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000171}
172
173void VideoSendStream::StopSend() {
pbos@webrtc.org9b303482013-05-23 12:37:11 +0000174 if (video_engine_base_->StopSend(channel_) != 0)
175 abort();
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000176}
177
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000178bool VideoSendStream::SetTargetBitrate(
pbos@webrtc.org9b303482013-05-23 12:37:11 +0000179 int min_bitrate,
180 int max_bitrate,
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000181 const std::vector<SimulcastStream>& streams) {
182 return false;
183}
184
185void VideoSendStream::GetSendCodec(VideoCodec* send_codec) {
186 *send_codec = config_.codec;
187}
188
pbos@webrtc.org9b303482013-05-23 12:37:11 +0000189int VideoSendStream::SendPacket(int /*channel*/,
190 const void* packet,
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000191 int length) {
192 // TODO(pbos): Lock these methods and the destructor so it can't be processing
193 // a packet when the destructor has been called.
194 assert(length >= 0);
pbos@webrtc.orgaf8d5af2013-07-09 08:02:33 +0000195 bool success = transport_->SendRTP(static_cast<const uint8_t*>(packet),
196 static_cast<size_t>(length));
197 return success ? 0 : -1;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000198}
199
pbos@webrtc.org9b303482013-05-23 12:37:11 +0000200int VideoSendStream::SendRTCPPacket(int /*channel*/,
201 const void* packet,
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000202 int length) {
203 assert(length >= 0);
pbos@webrtc.orgaf8d5af2013-07-09 08:02:33 +0000204 bool success = transport_->SendRTCP(static_cast<const uint8_t*>(packet),
205 static_cast<size_t>(length));
206 return success ? 0 : -1;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000207}
208
209} // namespace internal
210} // namespace webrtc