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