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" |
| 23 | #include "webrtc/video_engine/new_include/common.h" |
| 24 | #include "webrtc/video_engine/new_include/video_engine.h" |
| 25 | #include "webrtc/video_engine/internal/video_receive_stream.h" |
| 26 | #include "webrtc/video_engine/internal/video_send_stream.h" |
| 27 | |
| 28 | namespace webrtc { |
| 29 | namespace internal { |
| 30 | |
| 31 | VideoCall::VideoCall(webrtc::VideoEngine* video_engine, |
| 32 | newapi::Transport* send_transport) |
| 33 | : send_transport(send_transport), video_engine_(video_engine) { |
| 34 | assert(video_engine != NULL); |
| 35 | assert(send_transport != NULL); |
| 36 | |
| 37 | rtp_rtcp_ = ViERTP_RTCP::GetInterface(video_engine_); |
| 38 | assert(rtp_rtcp_ != NULL); |
| 39 | |
| 40 | codec_ = ViECodec::GetInterface(video_engine_); |
| 41 | assert(codec_ != NULL); |
| 42 | } |
| 43 | |
| 44 | VideoCall::~VideoCall() { |
| 45 | rtp_rtcp_->Release(); |
| 46 | codec_->Release(); |
| 47 | } |
| 48 | |
| 49 | newapi::PacketReceiver* VideoCall::Receiver() { return this; } |
| 50 | |
| 51 | std::vector<VideoCodec> VideoCall::GetVideoCodecs() { |
| 52 | std::vector<VideoCodec> codecs; |
| 53 | |
| 54 | VideoCodec codec; |
| 55 | for (size_t i = 0; i < static_cast<size_t>(codec_->NumberOfCodecs()); ++i) { |
| 56 | if (codec_->GetCodec(i, codec) == 0) { |
| 57 | codecs.push_back(codec); |
| 58 | } |
| 59 | } |
| 60 | return codecs; |
| 61 | } |
| 62 | |
| 63 | void VideoCall::GetDefaultSendConfig( |
| 64 | newapi::VideoSendStreamConfig* send_stream_config) { |
| 65 | *send_stream_config = newapi::VideoSendStreamConfig(); |
| 66 | codec_->GetCodec(0, send_stream_config->codec); |
| 67 | } |
| 68 | |
| 69 | newapi::VideoSendStream* VideoCall::CreateSendStream( |
| 70 | const newapi::VideoSendStreamConfig& send_stream_config) { |
| 71 | assert(send_stream_config.rtp.ssrcs.size() > 0); |
| 72 | assert(send_stream_config.codec.numberOfSimulcastStreams == 0 || |
| 73 | send_stream_config.codec.numberOfSimulcastStreams == |
| 74 | send_stream_config.rtp.ssrcs.size()); |
| 75 | VideoSendStream* send_stream = |
| 76 | new VideoSendStream(send_transport, video_engine_, send_stream_config); |
| 77 | for (size_t i = 0; i < send_stream_config.rtp.ssrcs.size(); ++i) { |
| 78 | uint32_t ssrc = send_stream_config.rtp.ssrcs[i]; |
| 79 | // SSRC must be previously unused! |
| 80 | assert(send_ssrcs_[ssrc] == NULL && |
| 81 | receive_ssrcs_.find(ssrc) == receive_ssrcs_.end()); |
| 82 | send_ssrcs_[ssrc] = send_stream; |
| 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 | |
| 99 | void VideoCall::GetDefaultReceiveConfig( |
| 100 | newapi::VideoReceiveStreamConfig* receive_stream_config) { |
| 101 | // TODO(pbos): This is not the default config. |
| 102 | *receive_stream_config = newapi::VideoReceiveStreamConfig(); |
| 103 | } |
| 104 | |
| 105 | newapi::VideoReceiveStream* VideoCall::CreateReceiveStream( |
| 106 | const newapi::VideoReceiveStreamConfig& receive_stream_config) { |
| 107 | assert(receive_ssrcs_[receive_stream_config.rtp.ssrc] == NULL); |
| 108 | |
| 109 | VideoReceiveStream* receive_stream = new VideoReceiveStream( |
| 110 | video_engine_, receive_stream_config, send_transport); |
| 111 | |
| 112 | receive_ssrcs_[receive_stream_config.rtp.ssrc] = receive_stream; |
| 113 | |
| 114 | return receive_stream; |
| 115 | } |
| 116 | |
| 117 | void VideoCall::DestroyReceiveStream( |
| 118 | newapi::VideoReceiveStream* receive_stream) { |
| 119 | if (receive_stream == NULL) { |
| 120 | return; |
| 121 | } |
| 122 | // TODO(pbos): Remove its SSRCs! |
| 123 | delete static_cast<VideoReceiveStream*>(receive_stream); |
| 124 | } |
| 125 | |
| 126 | uint32_t VideoCall::SendBitrateEstimate() { |
| 127 | // TODO(pbos): Return send-bitrate estimate |
| 128 | return 0; |
| 129 | } |
| 130 | |
| 131 | uint32_t VideoCall::ReceiveBitrateEstimate() { |
| 132 | // TODO(pbos): Return receive-bitrate estimate |
| 133 | return 0; |
| 134 | } |
| 135 | |
| 136 | bool VideoCall::DeliverRtcp(ModuleRTPUtility::RTPHeaderParser* rtp_parser, |
| 137 | const void* packet, size_t length) { |
| 138 | // TODO(pbos): Figure out what channel needs it actually. |
| 139 | // Do NOT broadcast! Also make sure it's a valid packet. |
| 140 | bool rtcp_delivered = false; |
| 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) |
| 145 | ->DeliverRtcp(packet, length)) { |
| 146 | rtcp_delivered = true; |
| 147 | } |
| 148 | } |
| 149 | return rtcp_delivered; |
| 150 | } |
| 151 | |
| 152 | bool VideoCall::DeliverRtp(ModuleRTPUtility::RTPHeaderParser* rtp_parser, |
| 153 | const void* packet, size_t length) { |
| 154 | WebRtcRTPHeader rtp_header; |
| 155 | |
| 156 | // TODO(pbos): ExtensionMap if there are extensions |
| 157 | if (!rtp_parser->Parse(rtp_header)) { |
| 158 | // TODO(pbos): Should this error be reported and trigger something? |
| 159 | return false; |
| 160 | } |
| 161 | |
| 162 | uint32_t ssrc = rtp_header.header.ssrc; |
| 163 | if (receive_ssrcs_.find(ssrc) == receive_ssrcs_.end()) { |
| 164 | // TODO(pbos): Log some warning, SSRC without receiver. |
| 165 | return false; |
| 166 | } |
| 167 | |
| 168 | VideoReceiveStream* receiver = |
| 169 | static_cast<VideoReceiveStream*>(receive_ssrcs_[ssrc]); |
| 170 | return receiver->DeliverRtp(packet, length); |
| 171 | } |
| 172 | |
| 173 | bool VideoCall::DeliverPacket(const void* packet, size_t length) { |
| 174 | // TODO(pbos): Respect the constness of packet. |
| 175 | ModuleRTPUtility::RTPHeaderParser rtp_parser( |
| 176 | const_cast<uint8_t*>(static_cast<const uint8_t*>(packet)), length); |
| 177 | |
| 178 | if (rtp_parser.RTCP()) { |
| 179 | return DeliverRtcp(&rtp_parser, packet, length); |
| 180 | } |
| 181 | |
| 182 | return DeliverRtp(&rtp_parser, packet, length); |
| 183 | } |
| 184 | |
| 185 | } // namespace internal |
| 186 | } // namespace webrtc |