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, |
| 31 | newapi::Transport* send_transport) |
| 32 | : send_transport(send_transport), video_engine_(video_engine) { |
| 33 | assert(video_engine != NULL); |
| 34 | assert(send_transport != NULL); |
| 35 | |
| 36 | rtp_rtcp_ = ViERTP_RTCP::GetInterface(video_engine_); |
| 37 | assert(rtp_rtcp_ != NULL); |
| 38 | |
| 39 | codec_ = ViECodec::GetInterface(video_engine_); |
| 40 | assert(codec_ != NULL); |
| 41 | } |
| 42 | |
| 43 | VideoCall::~VideoCall() { |
| 44 | rtp_rtcp_->Release(); |
| 45 | codec_->Release(); |
| 46 | } |
| 47 | |
| 48 | newapi::PacketReceiver* VideoCall::Receiver() { return this; } |
| 49 | |
| 50 | std::vector<VideoCodec> VideoCall::GetVideoCodecs() { |
| 51 | std::vector<VideoCodec> codecs; |
| 52 | |
| 53 | VideoCodec codec; |
| 54 | for (size_t i = 0; i < static_cast<size_t>(codec_->NumberOfCodecs()); ++i) { |
| 55 | if (codec_->GetCodec(i, codec) == 0) { |
| 56 | codecs.push_back(codec); |
| 57 | } |
| 58 | } |
| 59 | return codecs; |
| 60 | } |
| 61 | |
| 62 | void VideoCall::GetDefaultSendConfig( |
| 63 | newapi::VideoSendStreamConfig* send_stream_config) { |
| 64 | *send_stream_config = newapi::VideoSendStreamConfig(); |
| 65 | codec_->GetCodec(0, send_stream_config->codec); |
| 66 | } |
| 67 | |
| 68 | newapi::VideoSendStream* VideoCall::CreateSendStream( |
| 69 | const newapi::VideoSendStreamConfig& send_stream_config) { |
| 70 | assert(send_stream_config.rtp.ssrcs.size() > 0); |
| 71 | assert(send_stream_config.codec.numberOfSimulcastStreams == 0 || |
| 72 | send_stream_config.codec.numberOfSimulcastStreams == |
| 73 | send_stream_config.rtp.ssrcs.size()); |
| 74 | VideoSendStream* send_stream = |
| 75 | new VideoSendStream(send_transport, video_engine_, send_stream_config); |
| 76 | for (size_t i = 0; i < send_stream_config.rtp.ssrcs.size(); ++i) { |
| 77 | uint32_t ssrc = send_stream_config.rtp.ssrcs[i]; |
| 78 | // SSRC must be previously unused! |
| 79 | assert(send_ssrcs_[ssrc] == NULL && |
| 80 | receive_ssrcs_.find(ssrc) == receive_ssrcs_.end()); |
| 81 | send_ssrcs_[ssrc] = send_stream; |
| 82 | } |
| 83 | return send_stream; |
| 84 | } |
| 85 | |
| 86 | newapi::SendStreamState* VideoCall::DestroySendStream( |
| 87 | newapi::VideoSendStream* send_stream) { |
| 88 | if (send_stream == NULL) { |
| 89 | return NULL; |
| 90 | } |
| 91 | // TODO(pbos): Remove it properly! Free the SSRCs! |
| 92 | delete static_cast<VideoSendStream*>(send_stream); |
| 93 | |
| 94 | // TODO(pbos): Return its previous state |
| 95 | return NULL; |
| 96 | } |
| 97 | |
| 98 | void VideoCall::GetDefaultReceiveConfig( |
| 99 | newapi::VideoReceiveStreamConfig* receive_stream_config) { |
| 100 | // TODO(pbos): This is not the default config. |
| 101 | *receive_stream_config = newapi::VideoReceiveStreamConfig(); |
| 102 | } |
| 103 | |
| 104 | newapi::VideoReceiveStream* VideoCall::CreateReceiveStream( |
| 105 | const newapi::VideoReceiveStreamConfig& receive_stream_config) { |
| 106 | assert(receive_ssrcs_[receive_stream_config.rtp.ssrc] == NULL); |
| 107 | |
| 108 | VideoReceiveStream* receive_stream = new VideoReceiveStream( |
| 109 | video_engine_, receive_stream_config, send_transport); |
| 110 | |
| 111 | receive_ssrcs_[receive_stream_config.rtp.ssrc] = receive_stream; |
| 112 | |
| 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; |
| 140 | for (std::map<uint32_t, newapi::VideoReceiveStream*>::iterator it = |
| 141 | receive_ssrcs_.begin(); |
| 142 | it != receive_ssrcs_.end(); ++it) { |
| 143 | if (static_cast<VideoReceiveStream*>(it->second) |
| 144 | ->DeliverRtcp(packet, length)) { |
| 145 | rtcp_delivered = true; |
| 146 | } |
| 147 | } |
| 148 | return rtcp_delivered; |
| 149 | } |
| 150 | |
| 151 | bool VideoCall::DeliverRtp(ModuleRTPUtility::RTPHeaderParser* rtp_parser, |
| 152 | const void* packet, size_t length) { |
stefan@webrtc.org | a5cb98c | 2013-05-29 12:12:51 +0000 | [diff] [blame^] | 153 | RTPHeader rtp_header; |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 154 | |
| 155 | // TODO(pbos): ExtensionMap if there are extensions |
| 156 | if (!rtp_parser->Parse(rtp_header)) { |
| 157 | // TODO(pbos): Should this error be reported and trigger something? |
| 158 | return false; |
| 159 | } |
| 160 | |
stefan@webrtc.org | a5cb98c | 2013-05-29 12:12:51 +0000 | [diff] [blame^] | 161 | uint32_t ssrc = rtp_header.ssrc; |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 162 | if (receive_ssrcs_.find(ssrc) == receive_ssrcs_.end()) { |
| 163 | // TODO(pbos): Log some warning, SSRC without receiver. |
| 164 | return false; |
| 165 | } |
| 166 | |
| 167 | VideoReceiveStream* receiver = |
| 168 | static_cast<VideoReceiveStream*>(receive_ssrcs_[ssrc]); |
| 169 | return receiver->DeliverRtp(packet, length); |
| 170 | } |
| 171 | |
| 172 | bool VideoCall::DeliverPacket(const void* packet, size_t length) { |
| 173 | // TODO(pbos): Respect the constness of packet. |
| 174 | ModuleRTPUtility::RTPHeaderParser rtp_parser( |
| 175 | const_cast<uint8_t*>(static_cast<const uint8_t*>(packet)), length); |
| 176 | |
| 177 | if (rtp_parser.RTCP()) { |
| 178 | return DeliverRtcp(&rtp_parser, packet, length); |
| 179 | } |
| 180 | |
| 181 | return DeliverRtp(&rtp_parser, packet, length); |
| 182 | } |
| 183 | |
| 184 | } // namespace internal |
| 185 | } // namespace webrtc |