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