pbos@webrtc.org | dc8c883 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
| 23 | namespace webrtc { |
| 24 | namespace internal { |
| 25 | |
mflodman@webrtc.org | ecbeb2b | 2013-07-23 11:35:00 +0000 | [diff] [blame] | 26 | // 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. |
| 28 | class 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.org | 12d5ede | 2013-07-09 08:02:33 +0000 | [diff] [blame] | 76 | VideoSendStream::VideoSendStream(newapi::Transport* transport, |
mflodman@webrtc.org | ecbeb2b | 2013-07-23 11:35:00 +0000 | [diff] [blame] | 77 | bool overuse_detection, |
pbos@webrtc.org | 12d5ede | 2013-07-09 08:02:33 +0000 | [diff] [blame] | 78 | webrtc::VideoEngine* video_engine, |
| 79 | const newapi::VideoSendStream::Config& config) |
pbos@webrtc.org | 2c343fc | 2013-06-05 11:33:21 +0000 | [diff] [blame] | 80 | : transport_(transport), config_(config) { |
pbos@webrtc.org | dc8c883 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 81 | |
| 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.org | bf9bc32 | 2013-08-05 12:01:36 +0000 | [diff] [blame^] | 97 | rtp_rtcp_->SetNACKStatus(channel_, config_.rtp.nack.rtp_history_ms > 0); |
pbos@webrtc.org | dc8c883 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 98 | |
| 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.org | ecbeb2b | 2013-07-23 11:35:00 +0000 | [diff] [blame] | 112 | |
| 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.org | dc8c883 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | VideoSendStream::~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 | |
| 136 | void VideoSendStream::PutFrame(const I420VideoFrame& frame, |
pbos@webrtc.org | d9f9185 | 2013-05-23 12:37:11 +0000 | [diff] [blame] | 137 | uint32_t time_since_capture_ms) { |
| 138 | // TODO(pbos): frame_copy should happen after the VideoProcessingModule has |
| 139 | // resized the frame. |
pbos@webrtc.org | dc8c883 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 140 | 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.org | d9f9185 | 2013-05-23 12:37:11 +0000 | [diff] [blame] | 160 | external_capture_->IncomingFrameI420(vf, frame.render_time_ms()); |
pbos@webrtc.org | dc8c883 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 161 | |
| 162 | if (config_.local_renderer != NULL) { |
| 163 | config_.local_renderer->RenderFrame(frame, 0); |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | newapi::VideoSendStreamInput* VideoSendStream::Input() { return this; } |
| 168 | |
| 169 | void VideoSendStream::StartSend() { |
pbos@webrtc.org | d9f9185 | 2013-05-23 12:37:11 +0000 | [diff] [blame] | 170 | if (video_engine_base_->StartSend(channel_) != 0) |
| 171 | abort(); |
pbos@webrtc.org | bf9bc32 | 2013-08-05 12:01:36 +0000 | [diff] [blame^] | 172 | if (video_engine_base_->StartReceive(channel_) != 0) |
| 173 | abort(); |
pbos@webrtc.org | dc8c883 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | void VideoSendStream::StopSend() { |
pbos@webrtc.org | d9f9185 | 2013-05-23 12:37:11 +0000 | [diff] [blame] | 177 | if (video_engine_base_->StopSend(channel_) != 0) |
| 178 | abort(); |
pbos@webrtc.org | bf9bc32 | 2013-08-05 12:01:36 +0000 | [diff] [blame^] | 179 | if (video_engine_base_->StopReceive(channel_) != 0) |
| 180 | abort(); |
pbos@webrtc.org | dc8c883 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 181 | } |
| 182 | |
pbos@webrtc.org | dc8c883 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 183 | bool VideoSendStream::SetTargetBitrate( |
pbos@webrtc.org | d9f9185 | 2013-05-23 12:37:11 +0000 | [diff] [blame] | 184 | int min_bitrate, |
| 185 | int max_bitrate, |
pbos@webrtc.org | dc8c883 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 186 | const std::vector<SimulcastStream>& streams) { |
| 187 | return false; |
| 188 | } |
| 189 | |
| 190 | void VideoSendStream::GetSendCodec(VideoCodec* send_codec) { |
| 191 | *send_codec = config_.codec; |
| 192 | } |
| 193 | |
pbos@webrtc.org | d9f9185 | 2013-05-23 12:37:11 +0000 | [diff] [blame] | 194 | int VideoSendStream::SendPacket(int /*channel*/, |
| 195 | const void* packet, |
pbos@webrtc.org | dc8c883 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 196 | 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.org | 12d5ede | 2013-07-09 08:02:33 +0000 | [diff] [blame] | 200 | bool success = transport_->SendRTP(static_cast<const uint8_t*>(packet), |
| 201 | static_cast<size_t>(length)); |
| 202 | return success ? 0 : -1; |
pbos@webrtc.org | dc8c883 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 203 | } |
| 204 | |
pbos@webrtc.org | d9f9185 | 2013-05-23 12:37:11 +0000 | [diff] [blame] | 205 | int VideoSendStream::SendRTCPPacket(int /*channel*/, |
| 206 | const void* packet, |
pbos@webrtc.org | dc8c883 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 207 | int length) { |
| 208 | assert(length >= 0); |
pbos@webrtc.org | 12d5ede | 2013-07-09 08:02:33 +0000 | [diff] [blame] | 209 | bool success = transport_->SendRTCP(static_cast<const uint8_t*>(packet), |
| 210 | static_cast<size_t>(length)); |
| 211 | return success ? 0 : -1; |
pbos@webrtc.org | dc8c883 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 212 | } |
| 213 | |
pbos@webrtc.org | bf9bc32 | 2013-08-05 12:01:36 +0000 | [diff] [blame^] | 214 | bool VideoSendStream::DeliverRtcp(const uint8_t* packet, size_t length) { |
| 215 | return network_->ReceivedRTCPPacket( |
| 216 | channel_, packet, static_cast<int>(length)); |
| 217 | } |
| 218 | |
pbos@webrtc.org | dc8c883 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 219 | } // namespace internal |
| 220 | } // namespace webrtc |