pbos@webrtc.org | 2a9108f | 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 | |
pbos@webrtc.org | 206c4a5 | 2013-08-22 09:42:17 +0000 | [diff] [blame] | 13 | #include <string.h> |
| 14 | |
pbos@webrtc.org | 2a9108f | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 15 | #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.org | 55afdbe | 2013-08-22 09:29:56 +0000 | [diff] [blame] | 21 | #include "webrtc/video_engine/include/vie_external_codec.h" |
pbos@webrtc.org | 2a9108f | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 22 | #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 | |
| 26 | namespace webrtc { |
| 27 | namespace internal { |
| 28 | |
mflodman@webrtc.org | bf76ae2 | 2013-07-23 11:35:00 +0000 | [diff] [blame] | 29 | // 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. |
| 31 | class 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.org | 2f02da8 | 2013-07-09 08:02:33 +0000 | [diff] [blame] | 79 | VideoSendStream::VideoSendStream(newapi::Transport* transport, |
mflodman@webrtc.org | bf76ae2 | 2013-07-23 11:35:00 +0000 | [diff] [blame] | 80 | bool overuse_detection, |
pbos@webrtc.org | 2f02da8 | 2013-07-09 08:02:33 +0000 | [diff] [blame] | 81 | webrtc::VideoEngine* video_engine, |
pbos@webrtc.org | c179706 | 2013-08-23 09:19:30 +0000 | [diff] [blame^] | 82 | const VideoSendStream::Config& config) |
stefan@webrtc.org | 55afdbe | 2013-08-22 09:29:56 +0000 | [diff] [blame] | 83 | : transport_(transport), config_(config), external_codec_(NULL) { |
pbos@webrtc.org | 2a9108f | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 84 | |
| 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.org | 55afdbe | 2013-08-22 09:29:56 +0000 | [diff] [blame] | 98 | 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.org | ce85109 | 2013-08-05 12:01:36 +0000 | [diff] [blame] | 106 | rtp_rtcp_->SetNACKStatus(channel_, config_.rtp.nack.rtp_history_ms > 0); |
stefan@webrtc.org | 55afdbe | 2013-08-22 09:29:56 +0000 | [diff] [blame] | 107 | rtp_rtcp_->SetTransmissionSmoothingStatus(channel_, config_.pacing); |
| 108 | rtp_rtcp_->SetSendTimestampOffsetStatus(channel_, true, 1); |
pbos@webrtc.org | 2a9108f | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 109 | |
pbos@webrtc.org | 206c4a5 | 2013-08-22 09:42:17 +0000 | [diff] [blame] | 110 | char rtcp_cname[ViERTP_RTCP::KMaxRTCPCNameLength]; |
| 111 | assert(config_.rtp.c_name.length() < ViERTP_RTCP::KMaxRTCPCNameLength); |
| 112 | strncpy(rtcp_cname, config_.rtp.c_name.c_str(), sizeof(rtcp_cname) - 1); |
| 113 | rtcp_cname[sizeof(rtcp_cname) - 1] = '\0'; |
| 114 | |
| 115 | rtp_rtcp_->SetRTCPCName(channel_, rtcp_cname); |
| 116 | |
pbos@webrtc.org | 2a9108f | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 117 | capture_ = ViECapture::GetInterface(video_engine); |
| 118 | capture_->AllocateExternalCaptureDevice(capture_id_, external_capture_); |
| 119 | capture_->ConnectCaptureDevice(capture_id_, channel_); |
| 120 | |
| 121 | network_ = ViENetwork::GetInterface(video_engine); |
| 122 | assert(network_ != NULL); |
| 123 | |
| 124 | network_->RegisterSendTransport(channel_, *this); |
| 125 | |
stefan@webrtc.org | 55afdbe | 2013-08-22 09:29:56 +0000 | [diff] [blame] | 126 | if (config.encoder) { |
| 127 | external_codec_ = ViEExternalCodec::GetInterface(video_engine); |
| 128 | if (external_codec_->RegisterExternalSendCodec( |
| 129 | channel_, config.codec.plType, config.encoder, |
| 130 | config.internal_source) != 0) { |
| 131 | abort(); |
| 132 | } |
| 133 | } |
| 134 | |
pbos@webrtc.org | 2a9108f | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 135 | codec_ = ViECodec::GetInterface(video_engine); |
| 136 | if (codec_->SetSendCodec(channel_, config_.codec) != 0) { |
| 137 | abort(); |
| 138 | } |
mflodman@webrtc.org | bf76ae2 | 2013-07-23 11:35:00 +0000 | [diff] [blame] | 139 | |
| 140 | if (overuse_detection) { |
| 141 | overuse_observer_.reset( |
| 142 | new ResolutionAdaptor(codec_, channel_, config_.codec.width, |
| 143 | config_.codec.height)); |
| 144 | video_engine_base_->RegisterCpuOveruseObserver(channel_, |
| 145 | overuse_observer_.get()); |
| 146 | } |
pbos@webrtc.org | 2a9108f | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | VideoSendStream::~VideoSendStream() { |
| 150 | network_->DeregisterSendTransport(channel_); |
| 151 | video_engine_base_->DeleteChannel(channel_); |
| 152 | |
| 153 | capture_->DisconnectCaptureDevice(channel_); |
| 154 | capture_->ReleaseCaptureDevice(capture_id_); |
| 155 | |
stefan@webrtc.org | 55afdbe | 2013-08-22 09:29:56 +0000 | [diff] [blame] | 156 | if (external_codec_) { |
| 157 | external_codec_->DeRegisterExternalSendCodec(channel_, |
| 158 | config_.codec.plType); |
| 159 | } |
| 160 | |
pbos@webrtc.org | 2a9108f | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 161 | video_engine_base_->Release(); |
| 162 | capture_->Release(); |
| 163 | codec_->Release(); |
stefan@webrtc.org | 55afdbe | 2013-08-22 09:29:56 +0000 | [diff] [blame] | 164 | if (external_codec_) |
| 165 | external_codec_->Release(); |
pbos@webrtc.org | 2a9108f | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 166 | network_->Release(); |
| 167 | rtp_rtcp_->Release(); |
| 168 | } |
| 169 | |
| 170 | void VideoSendStream::PutFrame(const I420VideoFrame& frame, |
pbos@webrtc.org | 08f3ca9 | 2013-05-23 12:37:11 +0000 | [diff] [blame] | 171 | uint32_t time_since_capture_ms) { |
| 172 | // TODO(pbos): frame_copy should happen after the VideoProcessingModule has |
| 173 | // resized the frame. |
pbos@webrtc.org | 2a9108f | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 174 | I420VideoFrame frame_copy; |
| 175 | frame_copy.CopyFrame(frame); |
| 176 | |
| 177 | if (config_.pre_encode_callback != NULL) { |
| 178 | config_.pre_encode_callback->FrameCallback(&frame_copy); |
| 179 | } |
| 180 | |
| 181 | ViEVideoFrameI420 vf; |
| 182 | |
| 183 | // TODO(pbos): This represents a memcpy step and is only required because |
| 184 | // external_capture_ only takes ViEVideoFrameI420s. |
| 185 | vf.y_plane = frame_copy.buffer(kYPlane); |
| 186 | vf.u_plane = frame_copy.buffer(kUPlane); |
| 187 | vf.v_plane = frame_copy.buffer(kVPlane); |
| 188 | vf.y_pitch = frame.stride(kYPlane); |
| 189 | vf.u_pitch = frame.stride(kUPlane); |
| 190 | vf.v_pitch = frame.stride(kVPlane); |
| 191 | vf.width = frame.width(); |
| 192 | vf.height = frame.height(); |
| 193 | |
pbos@webrtc.org | 08f3ca9 | 2013-05-23 12:37:11 +0000 | [diff] [blame] | 194 | external_capture_->IncomingFrameI420(vf, frame.render_time_ms()); |
pbos@webrtc.org | 2a9108f | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 195 | |
| 196 | if (config_.local_renderer != NULL) { |
| 197 | config_.local_renderer->RenderFrame(frame, 0); |
| 198 | } |
| 199 | } |
| 200 | |
pbos@webrtc.org | c179706 | 2013-08-23 09:19:30 +0000 | [diff] [blame^] | 201 | VideoSendStreamInput* VideoSendStream::Input() { return this; } |
pbos@webrtc.org | 2a9108f | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 202 | |
| 203 | void VideoSendStream::StartSend() { |
pbos@webrtc.org | 08f3ca9 | 2013-05-23 12:37:11 +0000 | [diff] [blame] | 204 | if (video_engine_base_->StartSend(channel_) != 0) |
| 205 | abort(); |
pbos@webrtc.org | ce85109 | 2013-08-05 12:01:36 +0000 | [diff] [blame] | 206 | if (video_engine_base_->StartReceive(channel_) != 0) |
| 207 | abort(); |
pbos@webrtc.org | 2a9108f | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | void VideoSendStream::StopSend() { |
pbos@webrtc.org | 08f3ca9 | 2013-05-23 12:37:11 +0000 | [diff] [blame] | 211 | if (video_engine_base_->StopSend(channel_) != 0) |
| 212 | abort(); |
pbos@webrtc.org | ce85109 | 2013-08-05 12:01:36 +0000 | [diff] [blame] | 213 | if (video_engine_base_->StopReceive(channel_) != 0) |
| 214 | abort(); |
pbos@webrtc.org | 2a9108f | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 215 | } |
| 216 | |
pbos@webrtc.org | 2a9108f | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 217 | bool VideoSendStream::SetTargetBitrate( |
pbos@webrtc.org | 08f3ca9 | 2013-05-23 12:37:11 +0000 | [diff] [blame] | 218 | int min_bitrate, |
| 219 | int max_bitrate, |
pbos@webrtc.org | 2a9108f | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 220 | const std::vector<SimulcastStream>& streams) { |
| 221 | return false; |
| 222 | } |
| 223 | |
| 224 | void VideoSendStream::GetSendCodec(VideoCodec* send_codec) { |
| 225 | *send_codec = config_.codec; |
| 226 | } |
| 227 | |
pbos@webrtc.org | 08f3ca9 | 2013-05-23 12:37:11 +0000 | [diff] [blame] | 228 | int VideoSendStream::SendPacket(int /*channel*/, |
| 229 | const void* packet, |
pbos@webrtc.org | 2a9108f | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 230 | int length) { |
| 231 | // TODO(pbos): Lock these methods and the destructor so it can't be processing |
| 232 | // a packet when the destructor has been called. |
| 233 | assert(length >= 0); |
pbos@webrtc.org | 2f02da8 | 2013-07-09 08:02:33 +0000 | [diff] [blame] | 234 | bool success = transport_->SendRTP(static_cast<const uint8_t*>(packet), |
| 235 | static_cast<size_t>(length)); |
stefan@webrtc.org | 55afdbe | 2013-08-22 09:29:56 +0000 | [diff] [blame] | 236 | return success ? length : -1; |
pbos@webrtc.org | 2a9108f | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 237 | } |
| 238 | |
pbos@webrtc.org | 08f3ca9 | 2013-05-23 12:37:11 +0000 | [diff] [blame] | 239 | int VideoSendStream::SendRTCPPacket(int /*channel*/, |
| 240 | const void* packet, |
pbos@webrtc.org | 2a9108f | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 241 | int length) { |
| 242 | assert(length >= 0); |
pbos@webrtc.org | 2f02da8 | 2013-07-09 08:02:33 +0000 | [diff] [blame] | 243 | bool success = transport_->SendRTCP(static_cast<const uint8_t*>(packet), |
| 244 | static_cast<size_t>(length)); |
stefan@webrtc.org | 55afdbe | 2013-08-22 09:29:56 +0000 | [diff] [blame] | 245 | return success ? length : -1; |
pbos@webrtc.org | 2a9108f | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 246 | } |
| 247 | |
pbos@webrtc.org | ce85109 | 2013-08-05 12:01:36 +0000 | [diff] [blame] | 248 | bool VideoSendStream::DeliverRtcp(const uint8_t* packet, size_t length) { |
| 249 | return network_->ReceivedRTCPPacket( |
pbos@webrtc.org | 043f6a8 | 2013-08-05 13:25:51 +0000 | [diff] [blame] | 250 | channel_, packet, static_cast<int>(length)) == 0; |
pbos@webrtc.org | ce85109 | 2013-08-05 12:01:36 +0000 | [diff] [blame] | 251 | } |
| 252 | |
pbos@webrtc.org | 2a9108f | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 253 | } // namespace internal |
| 254 | } // namespace webrtc |