blob: acb65bf03186b6770bc03dd5619be6df4cefb091 [file] [log] [blame]
pbos@webrtc.org29d58392013-05-16 12:08:03 +00001/*
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
pbos@webrtc.org841c8a42013-09-09 15:04:25 +000011#include "webrtc/video_engine/internal/call.h"
pbos@webrtc.org29d58392013-05-16 12:08:03 +000012
pbos@webrtc.org12dc1a32013-08-05 16:22:53 +000013#include <assert.h>
14#include <string.h>
15
pbos@webrtc.org29d58392013-05-16 12:08:03 +000016#include <map>
17#include <vector>
18
pbos@webrtc.org29d58392013-05-16 12:08:03 +000019#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.org29d58392013-05-16 12:08:03 +000022#include "webrtc/video_engine/internal/video_receive_stream.h"
23#include "webrtc/video_engine/internal/video_send_stream.h"
pbos@webrtc.org29d58392013-05-16 12:08:03 +000024
25namespace webrtc {
pbos@webrtc.orgfd39e132013-08-14 13:52:52 +000026
pbos@webrtc.org841c8a42013-09-09 15:04:25 +000027Call* Call::Create(const Call::Config& config) {
28 VideoEngine* video_engine = VideoEngine::Create();
pbos@webrtc.orgfd39e132013-08-14 13:52:52 +000029 assert(video_engine != NULL);
30
pbos@webrtc.org841c8a42013-09-09 15:04:25 +000031 return new internal::Call(video_engine, config);
pbos@webrtc.orgfd39e132013-08-14 13:52:52 +000032}
pbos@webrtc.orgfd39e132013-08-14 13:52:52 +000033
pbos@webrtc.org29d58392013-05-16 12:08:03 +000034namespace internal {
35
pbos@webrtc.org841c8a42013-09-09 15:04:25 +000036Call::Call(webrtc::VideoEngine* video_engine, const Call::Config& config)
mflodman@webrtc.org6879c8a2013-07-23 11:35:00 +000037 : config_(config),
pbos@webrtc.org1819fd72013-06-10 13:48:26 +000038 receive_lock_(RWLockWrapper::CreateRWLock()),
39 send_lock_(RWLockWrapper::CreateRWLock()),
pbos@webrtc.org40523702013-08-05 12:49:22 +000040 rtp_header_parser_(RtpHeaderParser::Create()),
pbos@webrtc.org1819fd72013-06-10 13:48:26 +000041 video_engine_(video_engine) {
pbos@webrtc.org29d58392013-05-16 12:08:03 +000042 assert(video_engine != NULL);
mflodman@webrtc.org6879c8a2013-07-23 11:35:00 +000043 assert(config.send_transport != NULL);
pbos@webrtc.org29d58392013-05-16 12:08:03 +000044
45 rtp_rtcp_ = ViERTP_RTCP::GetInterface(video_engine_);
46 assert(rtp_rtcp_ != NULL);
47
48 codec_ = ViECodec::GetInterface(video_engine_);
49 assert(codec_ != NULL);
50}
51
pbos@webrtc.org841c8a42013-09-09 15:04:25 +000052Call::~Call() {
pbos@webrtc.org29d58392013-05-16 12:08:03 +000053 codec_->Release();
pbos@webrtc.orgfd39e132013-08-14 13:52:52 +000054 rtp_rtcp_->Release();
55 webrtc::VideoEngine::Delete(video_engine_);
pbos@webrtc.org29d58392013-05-16 12:08:03 +000056}
57
pbos@webrtc.org841c8a42013-09-09 15:04:25 +000058PacketReceiver* Call::Receiver() { return this; }
pbos@webrtc.org29d58392013-05-16 12:08:03 +000059
pbos@webrtc.org841c8a42013-09-09 15:04:25 +000060std::vector<VideoCodec> Call::GetVideoCodecs() {
pbos@webrtc.org29d58392013-05-16 12:08:03 +000061 std::vector<VideoCodec> codecs;
62
63 VideoCodec codec;
64 for (size_t i = 0; i < static_cast<size_t>(codec_->NumberOfCodecs()); ++i) {
65 if (codec_->GetCodec(i, codec) == 0) {
66 codecs.push_back(codec);
67 }
68 }
69 return codecs;
70}
71
pbos@webrtc.org841c8a42013-09-09 15:04:25 +000072VideoSendStream::Config Call::GetDefaultSendConfig() {
pbos@webrtc.org025f4f12013-06-05 11:33:21 +000073 VideoSendStream::Config config;
74 codec_->GetCodec(0, config.codec);
75 return config;
pbos@webrtc.org29d58392013-05-16 12:08:03 +000076}
77
pbos@webrtc.org841c8a42013-09-09 15:04:25 +000078VideoSendStream* Call::CreateSendStream(const VideoSendStream::Config& config) {
pbos@webrtc.org1819fd72013-06-10 13:48:26 +000079 assert(config.rtp.ssrcs.size() > 0);
80 assert(config.codec.numberOfSimulcastStreams == 0 ||
81 config.codec.numberOfSimulcastStreams == config.rtp.ssrcs.size());
82
pbos@webrtc.org40523702013-08-05 12:49:22 +000083 VideoSendStream* send_stream = new VideoSendStream(
84 config_.send_transport, config_.overuse_detection, video_engine_, config);
pbos@webrtc.org1819fd72013-06-10 13:48:26 +000085
86 WriteLockScoped write_lock(*send_lock_);
87 for (size_t i = 0; i < config.rtp.ssrcs.size(); ++i) {
88 assert(send_ssrcs_.find(config.rtp.ssrcs[i]) == send_ssrcs_.end());
89 send_ssrcs_[config.rtp.ssrcs[i]] = send_stream;
pbos@webrtc.org29d58392013-05-16 12:08:03 +000090 }
91 return send_stream;
92}
93
pbos@webrtc.org841c8a42013-09-09 15:04:25 +000094SendStreamState* Call::DestroySendStream(webrtc::VideoSendStream* send_stream) {
pbos@webrtc.org95e51f52013-09-05 12:38:54 +000095 assert(send_stream != NULL);
96
97 VideoSendStream* send_stream_impl = NULL;
98 {
99 WriteLockScoped write_lock(*send_lock_);
100 for (std::map<uint32_t, VideoSendStream*>::iterator it =
101 send_ssrcs_.begin();
102 it != send_ssrcs_.end();
103 ++it) {
104 if (it->second == static_cast<VideoSendStream*>(send_stream)) {
105 send_stream_impl = it->second;
106 send_ssrcs_.erase(it);
107 break;
108 }
109 }
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000110 }
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000111
112 assert(send_stream_impl != NULL);
113 delete send_stream_impl;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000114
115 // TODO(pbos): Return its previous state
116 return NULL;
117}
118
pbos@webrtc.org841c8a42013-09-09 15:04:25 +0000119VideoReceiveStream::Config Call::GetDefaultReceiveConfig() {
pbos@webrtc.org74fa4892013-08-23 09:19:30 +0000120 return VideoReceiveStream::Config();
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000121}
122
pbos@webrtc.org841c8a42013-09-09 15:04:25 +0000123VideoReceiveStream* Call::CreateReceiveStream(
pbos@webrtc.org74fa4892013-08-23 09:19:30 +0000124 const VideoReceiveStream::Config& config) {
pbos@webrtc.org40523702013-08-05 12:49:22 +0000125 VideoReceiveStream* receive_stream =
126 new VideoReceiveStream(video_engine_, config, config_.send_transport);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000127
pbos@webrtc.org1819fd72013-06-10 13:48:26 +0000128 WriteLockScoped write_lock(*receive_lock_);
129 assert(receive_ssrcs_.find(config.rtp.ssrc) == receive_ssrcs_.end());
130 receive_ssrcs_[config.rtp.ssrc] = receive_stream;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000131 return receive_stream;
132}
133
pbos@webrtc.org841c8a42013-09-09 15:04:25 +0000134void Call::DestroyReceiveStream(webrtc::VideoReceiveStream* receive_stream) {
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000135 assert(receive_stream != NULL);
136
137 VideoReceiveStream* receive_stream_impl = NULL;
138 {
139 WriteLockScoped write_lock(*receive_lock_);
140 for (std::map<uint32_t, VideoReceiveStream*>::iterator it =
141 receive_ssrcs_.begin();
142 it != receive_ssrcs_.end();
143 ++it) {
144 if (it->second == static_cast<VideoReceiveStream*>(receive_stream)) {
145 receive_stream_impl = it->second;
146 receive_ssrcs_.erase(it);
147 break;
148 }
149 }
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000150 }
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000151
152 assert(receive_stream_impl != NULL);
153 delete receive_stream_impl;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000154}
155
pbos@webrtc.org841c8a42013-09-09 15:04:25 +0000156uint32_t Call::SendBitrateEstimate() {
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000157 // TODO(pbos): Return send-bitrate estimate
158 return 0;
159}
160
pbos@webrtc.org841c8a42013-09-09 15:04:25 +0000161uint32_t Call::ReceiveBitrateEstimate() {
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000162 // TODO(pbos): Return receive-bitrate estimate
163 return 0;
164}
165
pbos@webrtc.org841c8a42013-09-09 15:04:25 +0000166bool Call::DeliverRtcp(const uint8_t* packet, size_t length) {
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000167 // TODO(pbos): Figure out what channel needs it actually.
168 // Do NOT broadcast! Also make sure it's a valid packet.
169 bool rtcp_delivered = false;
pbos@webrtc.org40523702013-08-05 12:49:22 +0000170 {
171 ReadLockScoped read_lock(*receive_lock_);
172 for (std::map<uint32_t, VideoReceiveStream*>::iterator it =
173 receive_ssrcs_.begin();
174 it != receive_ssrcs_.end();
175 ++it) {
176 if (it->second->DeliverRtcp(static_cast<const uint8_t*>(packet),
177 length)) {
178 rtcp_delivered = true;
179 }
pbos@webrtc.orgbbb07e62013-08-05 12:01:36 +0000180 }
181 }
182
pbos@webrtc.org40523702013-08-05 12:49:22 +0000183 {
184 ReadLockScoped read_lock(*send_lock_);
185 for (std::map<uint32_t, VideoSendStream*>::iterator it =
186 send_ssrcs_.begin();
187 it != send_ssrcs_.end();
188 ++it) {
189 if (it->second->DeliverRtcp(static_cast<const uint8_t*>(packet),
190 length)) {
191 rtcp_delivered = true;
192 }
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000193 }
194 }
195 return rtcp_delivered;
196}
197
pbos@webrtc.org841c8a42013-09-09 15:04:25 +0000198bool Call::DeliverRtp(const RTPHeader& header,
199 const uint8_t* packet,
200 size_t length) {
pbos@webrtc.org40523702013-08-05 12:49:22 +0000201 VideoReceiveStream* receiver;
202 {
203 ReadLockScoped read_lock(*receive_lock_);
204 std::map<uint32_t, VideoReceiveStream*>::iterator it =
205 receive_ssrcs_.find(header.ssrc);
206 if (it == receive_ssrcs_.end()) {
207 // TODO(pbos): Log some warning, SSRC without receiver.
208 return false;
209 }
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000210
pbos@webrtc.org40523702013-08-05 12:49:22 +0000211 receiver = it->second;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000212 }
pbos@webrtc.orgbbb07e62013-08-05 12:01:36 +0000213 return receiver->DeliverRtp(static_cast<const uint8_t*>(packet), length);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000214}
215
pbos@webrtc.org841c8a42013-09-09 15:04:25 +0000216bool Call::DeliverPacket(const uint8_t* packet, size_t length) {
pbos@webrtc.org40523702013-08-05 12:49:22 +0000217 // TODO(pbos): ExtensionMap if there are extensions.
218 if (RtpHeaderParser::IsRtcp(packet, static_cast<int>(length)))
219 return DeliverRtcp(packet, length);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000220
pbos@webrtc.org40523702013-08-05 12:49:22 +0000221 RTPHeader rtp_header;
222 if (!rtp_header_parser_->Parse(packet, static_cast<int>(length), &rtp_header))
223 return false;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000224
pbos@webrtc.org40523702013-08-05 12:49:22 +0000225 return DeliverRtp(rtp_header, packet, length);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000226}
227
228} // namespace internal
229} // namespace webrtc