pbos@webrtc.org | 29d5839 | 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_call.h" |
| 12 | |
pbos@webrtc.org | 12dc1a3 | 2013-08-05 16:22:53 +0000 | [diff] [blame] | 13 | #include <assert.h> |
| 14 | #include <string.h> |
| 15 | |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 16 | #include <map> |
| 17 | #include <vector> |
| 18 | |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 19 | #include "webrtc/video_engine/include/vie_base.h" |
| 20 | #include "webrtc/video_engine/include/vie_codec.h" |
| 21 | #include "webrtc/video_engine/include/vie_rtp_rtcp.h" |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 22 | #include "webrtc/video_engine/internal/video_receive_stream.h" |
| 23 | #include "webrtc/video_engine/internal/video_send_stream.h" |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 24 | |
| 25 | namespace webrtc { |
pbos@webrtc.org | fd39e13 | 2013-08-14 13:52:52 +0000 | [diff] [blame] | 26 | |
pbos@webrtc.org | 74fa489 | 2013-08-23 09:19:30 +0000 | [diff] [blame] | 27 | VideoCall* VideoCall::Create(const VideoCall::Config& config) { |
pbos@webrtc.org | fd39e13 | 2013-08-14 13:52:52 +0000 | [diff] [blame] | 28 | webrtc::VideoEngine* video_engine = webrtc::VideoEngine::Create(); |
| 29 | assert(video_engine != NULL); |
| 30 | |
pbos@webrtc.org | fd39e13 | 2013-08-14 13:52:52 +0000 | [diff] [blame] | 31 | return new internal::VideoCall(video_engine, config); |
| 32 | } |
pbos@webrtc.org | fd39e13 | 2013-08-14 13:52:52 +0000 | [diff] [blame] | 33 | |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 34 | namespace internal { |
| 35 | |
| 36 | VideoCall::VideoCall(webrtc::VideoEngine* video_engine, |
pbos@webrtc.org | 74fa489 | 2013-08-23 09:19:30 +0000 | [diff] [blame] | 37 | const VideoCall::Config& config) |
mflodman@webrtc.org | 6879c8a | 2013-07-23 11:35:00 +0000 | [diff] [blame] | 38 | : config_(config), |
pbos@webrtc.org | 1819fd7 | 2013-06-10 13:48:26 +0000 | [diff] [blame] | 39 | receive_lock_(RWLockWrapper::CreateRWLock()), |
| 40 | send_lock_(RWLockWrapper::CreateRWLock()), |
pbos@webrtc.org | 4052370 | 2013-08-05 12:49:22 +0000 | [diff] [blame] | 41 | rtp_header_parser_(RtpHeaderParser::Create()), |
pbos@webrtc.org | 1819fd7 | 2013-06-10 13:48:26 +0000 | [diff] [blame] | 42 | video_engine_(video_engine) { |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 43 | assert(video_engine != NULL); |
mflodman@webrtc.org | 6879c8a | 2013-07-23 11:35:00 +0000 | [diff] [blame] | 44 | assert(config.send_transport != NULL); |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 45 | |
| 46 | rtp_rtcp_ = ViERTP_RTCP::GetInterface(video_engine_); |
| 47 | assert(rtp_rtcp_ != NULL); |
| 48 | |
| 49 | codec_ = ViECodec::GetInterface(video_engine_); |
| 50 | assert(codec_ != NULL); |
| 51 | } |
| 52 | |
| 53 | VideoCall::~VideoCall() { |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 54 | codec_->Release(); |
pbos@webrtc.org | fd39e13 | 2013-08-14 13:52:52 +0000 | [diff] [blame] | 55 | rtp_rtcp_->Release(); |
| 56 | webrtc::VideoEngine::Delete(video_engine_); |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 57 | } |
| 58 | |
pbos@webrtc.org | 74fa489 | 2013-08-23 09:19:30 +0000 | [diff] [blame] | 59 | PacketReceiver* VideoCall::Receiver() { return this; } |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 60 | |
| 61 | std::vector<VideoCodec> VideoCall::GetVideoCodecs() { |
| 62 | std::vector<VideoCodec> codecs; |
| 63 | |
| 64 | VideoCodec codec; |
| 65 | for (size_t i = 0; i < static_cast<size_t>(codec_->NumberOfCodecs()); ++i) { |
| 66 | if (codec_->GetCodec(i, codec) == 0) { |
| 67 | codecs.push_back(codec); |
| 68 | } |
| 69 | } |
| 70 | return codecs; |
| 71 | } |
| 72 | |
pbos@webrtc.org | 025f4f1 | 2013-06-05 11:33:21 +0000 | [diff] [blame] | 73 | VideoSendStream::Config VideoCall::GetDefaultSendConfig() { |
| 74 | VideoSendStream::Config config; |
| 75 | codec_->GetCodec(0, config.codec); |
| 76 | return config; |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 77 | } |
| 78 | |
pbos@webrtc.org | 74fa489 | 2013-08-23 09:19:30 +0000 | [diff] [blame] | 79 | VideoSendStream* VideoCall::CreateSendStream( |
| 80 | const VideoSendStream::Config& config) { |
pbos@webrtc.org | 1819fd7 | 2013-06-10 13:48:26 +0000 | [diff] [blame] | 81 | assert(config.rtp.ssrcs.size() > 0); |
| 82 | assert(config.codec.numberOfSimulcastStreams == 0 || |
| 83 | config.codec.numberOfSimulcastStreams == config.rtp.ssrcs.size()); |
| 84 | |
pbos@webrtc.org | 4052370 | 2013-08-05 12:49:22 +0000 | [diff] [blame] | 85 | VideoSendStream* send_stream = new VideoSendStream( |
| 86 | config_.send_transport, config_.overuse_detection, video_engine_, config); |
pbos@webrtc.org | 1819fd7 | 2013-06-10 13:48:26 +0000 | [diff] [blame] | 87 | |
| 88 | WriteLockScoped write_lock(*send_lock_); |
| 89 | for (size_t i = 0; i < config.rtp.ssrcs.size(); ++i) { |
| 90 | assert(send_ssrcs_.find(config.rtp.ssrcs[i]) == send_ssrcs_.end()); |
| 91 | send_ssrcs_[config.rtp.ssrcs[i]] = send_stream; |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 92 | } |
| 93 | return send_stream; |
| 94 | } |
| 95 | |
pbos@webrtc.org | 74fa489 | 2013-08-23 09:19:30 +0000 | [diff] [blame] | 96 | SendStreamState* VideoCall::DestroySendStream( |
| 97 | webrtc::VideoSendStream* send_stream) { |
pbos@webrtc.org | 95e51f5 | 2013-09-05 12:38:54 +0000 | [diff] [blame^] | 98 | assert(send_stream != NULL); |
| 99 | |
| 100 | VideoSendStream* send_stream_impl = NULL; |
| 101 | { |
| 102 | WriteLockScoped write_lock(*send_lock_); |
| 103 | for (std::map<uint32_t, VideoSendStream*>::iterator it = |
| 104 | send_ssrcs_.begin(); |
| 105 | it != send_ssrcs_.end(); |
| 106 | ++it) { |
| 107 | if (it->second == static_cast<VideoSendStream*>(send_stream)) { |
| 108 | send_stream_impl = it->second; |
| 109 | send_ssrcs_.erase(it); |
| 110 | break; |
| 111 | } |
| 112 | } |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 113 | } |
pbos@webrtc.org | 95e51f5 | 2013-09-05 12:38:54 +0000 | [diff] [blame^] | 114 | |
| 115 | assert(send_stream_impl != NULL); |
| 116 | delete send_stream_impl; |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 117 | |
| 118 | // TODO(pbos): Return its previous state |
| 119 | return NULL; |
| 120 | } |
| 121 | |
pbos@webrtc.org | 025f4f1 | 2013-06-05 11:33:21 +0000 | [diff] [blame] | 122 | VideoReceiveStream::Config VideoCall::GetDefaultReceiveConfig() { |
pbos@webrtc.org | 74fa489 | 2013-08-23 09:19:30 +0000 | [diff] [blame] | 123 | return VideoReceiveStream::Config(); |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 124 | } |
| 125 | |
pbos@webrtc.org | 74fa489 | 2013-08-23 09:19:30 +0000 | [diff] [blame] | 126 | VideoReceiveStream* VideoCall::CreateReceiveStream( |
| 127 | const VideoReceiveStream::Config& config) { |
pbos@webrtc.org | 4052370 | 2013-08-05 12:49:22 +0000 | [diff] [blame] | 128 | VideoReceiveStream* receive_stream = |
| 129 | new VideoReceiveStream(video_engine_, config, config_.send_transport); |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 130 | |
pbos@webrtc.org | 1819fd7 | 2013-06-10 13:48:26 +0000 | [diff] [blame] | 131 | WriteLockScoped write_lock(*receive_lock_); |
| 132 | assert(receive_ssrcs_.find(config.rtp.ssrc) == receive_ssrcs_.end()); |
| 133 | receive_ssrcs_[config.rtp.ssrc] = receive_stream; |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 134 | return receive_stream; |
| 135 | } |
| 136 | |
| 137 | void VideoCall::DestroyReceiveStream( |
pbos@webrtc.org | 74fa489 | 2013-08-23 09:19:30 +0000 | [diff] [blame] | 138 | webrtc::VideoReceiveStream* receive_stream) { |
pbos@webrtc.org | 95e51f5 | 2013-09-05 12:38:54 +0000 | [diff] [blame^] | 139 | assert(receive_stream != NULL); |
| 140 | |
| 141 | VideoReceiveStream* receive_stream_impl = NULL; |
| 142 | { |
| 143 | WriteLockScoped write_lock(*receive_lock_); |
| 144 | for (std::map<uint32_t, VideoReceiveStream*>::iterator it = |
| 145 | receive_ssrcs_.begin(); |
| 146 | it != receive_ssrcs_.end(); |
| 147 | ++it) { |
| 148 | if (it->second == static_cast<VideoReceiveStream*>(receive_stream)) { |
| 149 | receive_stream_impl = it->second; |
| 150 | receive_ssrcs_.erase(it); |
| 151 | break; |
| 152 | } |
| 153 | } |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 154 | } |
pbos@webrtc.org | 95e51f5 | 2013-09-05 12:38:54 +0000 | [diff] [blame^] | 155 | |
| 156 | assert(receive_stream_impl != NULL); |
| 157 | delete receive_stream_impl; |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | uint32_t VideoCall::SendBitrateEstimate() { |
| 161 | // TODO(pbos): Return send-bitrate estimate |
| 162 | return 0; |
| 163 | } |
| 164 | |
| 165 | uint32_t VideoCall::ReceiveBitrateEstimate() { |
| 166 | // TODO(pbos): Return receive-bitrate estimate |
| 167 | return 0; |
| 168 | } |
| 169 | |
pbos@webrtc.org | 4052370 | 2013-08-05 12:49:22 +0000 | [diff] [blame] | 170 | bool VideoCall::DeliverRtcp(const uint8_t* packet, size_t length) { |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 171 | // TODO(pbos): Figure out what channel needs it actually. |
| 172 | // Do NOT broadcast! Also make sure it's a valid packet. |
| 173 | bool rtcp_delivered = false; |
pbos@webrtc.org | 4052370 | 2013-08-05 12:49:22 +0000 | [diff] [blame] | 174 | { |
| 175 | ReadLockScoped read_lock(*receive_lock_); |
| 176 | for (std::map<uint32_t, VideoReceiveStream*>::iterator it = |
| 177 | receive_ssrcs_.begin(); |
| 178 | it != receive_ssrcs_.end(); |
| 179 | ++it) { |
| 180 | if (it->second->DeliverRtcp(static_cast<const uint8_t*>(packet), |
| 181 | length)) { |
| 182 | rtcp_delivered = true; |
| 183 | } |
pbos@webrtc.org | bbb07e6 | 2013-08-05 12:01:36 +0000 | [diff] [blame] | 184 | } |
| 185 | } |
| 186 | |
pbos@webrtc.org | 4052370 | 2013-08-05 12:49:22 +0000 | [diff] [blame] | 187 | { |
| 188 | ReadLockScoped read_lock(*send_lock_); |
| 189 | for (std::map<uint32_t, VideoSendStream*>::iterator it = |
| 190 | send_ssrcs_.begin(); |
| 191 | it != send_ssrcs_.end(); |
| 192 | ++it) { |
| 193 | if (it->second->DeliverRtcp(static_cast<const uint8_t*>(packet), |
| 194 | length)) { |
| 195 | rtcp_delivered = true; |
| 196 | } |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 197 | } |
| 198 | } |
| 199 | return rtcp_delivered; |
| 200 | } |
| 201 | |
pbos@webrtc.org | 4052370 | 2013-08-05 12:49:22 +0000 | [diff] [blame] | 202 | bool VideoCall::DeliverRtp(const RTPHeader& header, |
| 203 | const uint8_t* packet, |
| 204 | size_t length) { |
| 205 | VideoReceiveStream* receiver; |
| 206 | { |
| 207 | ReadLockScoped read_lock(*receive_lock_); |
| 208 | std::map<uint32_t, VideoReceiveStream*>::iterator it = |
| 209 | receive_ssrcs_.find(header.ssrc); |
| 210 | if (it == receive_ssrcs_.end()) { |
| 211 | // TODO(pbos): Log some warning, SSRC without receiver. |
| 212 | return false; |
| 213 | } |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 214 | |
pbos@webrtc.org | 4052370 | 2013-08-05 12:49:22 +0000 | [diff] [blame] | 215 | receiver = it->second; |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 216 | } |
pbos@webrtc.org | bbb07e6 | 2013-08-05 12:01:36 +0000 | [diff] [blame] | 217 | return receiver->DeliverRtp(static_cast<const uint8_t*>(packet), length); |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 218 | } |
| 219 | |
pbos@webrtc.org | 4052370 | 2013-08-05 12:49:22 +0000 | [diff] [blame] | 220 | bool VideoCall::DeliverPacket(const uint8_t* packet, size_t length) { |
| 221 | // TODO(pbos): ExtensionMap if there are extensions. |
| 222 | if (RtpHeaderParser::IsRtcp(packet, static_cast<int>(length))) |
| 223 | return DeliverRtcp(packet, length); |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 224 | |
pbos@webrtc.org | 4052370 | 2013-08-05 12:49:22 +0000 | [diff] [blame] | 225 | RTPHeader rtp_header; |
| 226 | if (!rtp_header_parser_->Parse(packet, static_cast<int>(length), &rtp_header)) |
| 227 | return false; |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 228 | |
pbos@webrtc.org | 4052370 | 2013-08-05 12:49:22 +0000 | [diff] [blame] | 229 | return DeliverRtp(rtp_header, packet, length); |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | } // namespace internal |
| 233 | } // namespace webrtc |