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