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 | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 98 | if (send_stream == NULL) { |
| 99 | return NULL; |
| 100 | } |
| 101 | // TODO(pbos): Remove it properly! Free the SSRCs! |
| 102 | delete static_cast<VideoSendStream*>(send_stream); |
| 103 | |
| 104 | // TODO(pbos): Return its previous state |
| 105 | return NULL; |
| 106 | } |
| 107 | |
pbos@webrtc.org | 025f4f1 | 2013-06-05 11:33:21 +0000 | [diff] [blame] | 108 | VideoReceiveStream::Config VideoCall::GetDefaultReceiveConfig() { |
pbos@webrtc.org | 74fa489 | 2013-08-23 09:19:30 +0000 | [diff] [blame] | 109 | return VideoReceiveStream::Config(); |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 110 | } |
| 111 | |
pbos@webrtc.org | 74fa489 | 2013-08-23 09:19:30 +0000 | [diff] [blame] | 112 | VideoReceiveStream* VideoCall::CreateReceiveStream( |
| 113 | const VideoReceiveStream::Config& config) { |
pbos@webrtc.org | 4052370 | 2013-08-05 12:49:22 +0000 | [diff] [blame] | 114 | VideoReceiveStream* receive_stream = |
| 115 | new VideoReceiveStream(video_engine_, config, config_.send_transport); |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 116 | |
pbos@webrtc.org | 1819fd7 | 2013-06-10 13:48:26 +0000 | [diff] [blame] | 117 | WriteLockScoped write_lock(*receive_lock_); |
| 118 | assert(receive_ssrcs_.find(config.rtp.ssrc) == receive_ssrcs_.end()); |
| 119 | receive_ssrcs_[config.rtp.ssrc] = receive_stream; |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 120 | return receive_stream; |
| 121 | } |
| 122 | |
| 123 | void VideoCall::DestroyReceiveStream( |
pbos@webrtc.org | 74fa489 | 2013-08-23 09:19:30 +0000 | [diff] [blame] | 124 | webrtc::VideoReceiveStream* receive_stream) { |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 125 | if (receive_stream == NULL) { |
| 126 | return; |
| 127 | } |
| 128 | // TODO(pbos): Remove its SSRCs! |
| 129 | delete static_cast<VideoReceiveStream*>(receive_stream); |
| 130 | } |
| 131 | |
| 132 | uint32_t VideoCall::SendBitrateEstimate() { |
| 133 | // TODO(pbos): Return send-bitrate estimate |
| 134 | return 0; |
| 135 | } |
| 136 | |
| 137 | uint32_t VideoCall::ReceiveBitrateEstimate() { |
| 138 | // TODO(pbos): Return receive-bitrate estimate |
| 139 | return 0; |
| 140 | } |
| 141 | |
pbos@webrtc.org | 4052370 | 2013-08-05 12:49:22 +0000 | [diff] [blame] | 142 | bool VideoCall::DeliverRtcp(const uint8_t* packet, size_t length) { |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 143 | // TODO(pbos): Figure out what channel needs it actually. |
| 144 | // Do NOT broadcast! Also make sure it's a valid packet. |
| 145 | bool rtcp_delivered = false; |
pbos@webrtc.org | 4052370 | 2013-08-05 12:49:22 +0000 | [diff] [blame] | 146 | { |
| 147 | ReadLockScoped read_lock(*receive_lock_); |
| 148 | for (std::map<uint32_t, VideoReceiveStream*>::iterator it = |
| 149 | receive_ssrcs_.begin(); |
| 150 | it != receive_ssrcs_.end(); |
| 151 | ++it) { |
| 152 | if (it->second->DeliverRtcp(static_cast<const uint8_t*>(packet), |
| 153 | length)) { |
| 154 | rtcp_delivered = true; |
| 155 | } |
pbos@webrtc.org | bbb07e6 | 2013-08-05 12:01:36 +0000 | [diff] [blame] | 156 | } |
| 157 | } |
| 158 | |
pbos@webrtc.org | 4052370 | 2013-08-05 12:49:22 +0000 | [diff] [blame] | 159 | { |
| 160 | ReadLockScoped read_lock(*send_lock_); |
| 161 | for (std::map<uint32_t, VideoSendStream*>::iterator it = |
| 162 | send_ssrcs_.begin(); |
| 163 | it != send_ssrcs_.end(); |
| 164 | ++it) { |
| 165 | if (it->second->DeliverRtcp(static_cast<const uint8_t*>(packet), |
| 166 | length)) { |
| 167 | rtcp_delivered = true; |
| 168 | } |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 169 | } |
| 170 | } |
| 171 | return rtcp_delivered; |
| 172 | } |
| 173 | |
pbos@webrtc.org | 4052370 | 2013-08-05 12:49:22 +0000 | [diff] [blame] | 174 | bool VideoCall::DeliverRtp(const RTPHeader& header, |
| 175 | const uint8_t* packet, |
| 176 | size_t length) { |
| 177 | VideoReceiveStream* receiver; |
| 178 | { |
| 179 | ReadLockScoped read_lock(*receive_lock_); |
| 180 | std::map<uint32_t, VideoReceiveStream*>::iterator it = |
| 181 | receive_ssrcs_.find(header.ssrc); |
| 182 | if (it == receive_ssrcs_.end()) { |
| 183 | // TODO(pbos): Log some warning, SSRC without receiver. |
| 184 | return false; |
| 185 | } |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 186 | |
pbos@webrtc.org | 4052370 | 2013-08-05 12:49:22 +0000 | [diff] [blame] | 187 | receiver = it->second; |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 188 | } |
pbos@webrtc.org | bbb07e6 | 2013-08-05 12:01:36 +0000 | [diff] [blame] | 189 | return receiver->DeliverRtp(static_cast<const uint8_t*>(packet), length); |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 190 | } |
| 191 | |
pbos@webrtc.org | 4052370 | 2013-08-05 12:49:22 +0000 | [diff] [blame] | 192 | bool VideoCall::DeliverPacket(const uint8_t* packet, size_t length) { |
| 193 | // TODO(pbos): ExtensionMap if there are extensions. |
| 194 | if (RtpHeaderParser::IsRtcp(packet, static_cast<int>(length))) |
| 195 | return DeliverRtcp(packet, length); |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 196 | |
pbos@webrtc.org | 4052370 | 2013-08-05 12:49:22 +0000 | [diff] [blame] | 197 | RTPHeader rtp_header; |
| 198 | if (!rtp_header_parser_->Parse(packet, static_cast<int>(length), &rtp_header)) |
| 199 | return false; |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 200 | |
pbos@webrtc.org | 4052370 | 2013-08-05 12:49:22 +0000 | [diff] [blame] | 201 | return DeliverRtp(rtp_header, packet, length); |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | } // namespace internal |
| 205 | } // namespace webrtc |