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