blob: 6e50395c08a609ce9e455972f174473d72af07e0 [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
11#include "webrtc/video_engine/internal/video_call.h"
12
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.org74fa4892013-08-23 09:19:30 +000027VideoCall* VideoCall::Create(const VideoCall::Config& config) {
pbos@webrtc.orgfd39e132013-08-14 13:52:52 +000028 webrtc::VideoEngine* video_engine = webrtc::VideoEngine::Create();
29 assert(video_engine != NULL);
30
pbos@webrtc.orgfd39e132013-08-14 13:52:52 +000031 return new internal::VideoCall(video_engine, config);
32}
pbos@webrtc.orgfd39e132013-08-14 13:52:52 +000033
pbos@webrtc.org29d58392013-05-16 12:08:03 +000034namespace internal {
35
36VideoCall::VideoCall(webrtc::VideoEngine* video_engine,
pbos@webrtc.org74fa4892013-08-23 09:19:30 +000037 const VideoCall::Config& config)
mflodman@webrtc.org6879c8a2013-07-23 11:35:00 +000038 : config_(config),
pbos@webrtc.org1819fd72013-06-10 13:48:26 +000039 receive_lock_(RWLockWrapper::CreateRWLock()),
40 send_lock_(RWLockWrapper::CreateRWLock()),
pbos@webrtc.org40523702013-08-05 12:49:22 +000041 rtp_header_parser_(RtpHeaderParser::Create()),
pbos@webrtc.org1819fd72013-06-10 13:48:26 +000042 video_engine_(video_engine) {
pbos@webrtc.org29d58392013-05-16 12:08:03 +000043 assert(video_engine != NULL);
mflodman@webrtc.org6879c8a2013-07-23 11:35:00 +000044 assert(config.send_transport != NULL);
pbos@webrtc.org29d58392013-05-16 12:08:03 +000045
46 rtp_rtcp_ = ViERTP_RTCP::GetInterface(video_engine_);
47 assert(rtp_rtcp_ != NULL);
48
49 codec_ = ViECodec::GetInterface(video_engine_);
50 assert(codec_ != NULL);
51}
52
53VideoCall::~VideoCall() {
pbos@webrtc.org29d58392013-05-16 12:08:03 +000054 codec_->Release();
pbos@webrtc.orgfd39e132013-08-14 13:52:52 +000055 rtp_rtcp_->Release();
56 webrtc::VideoEngine::Delete(video_engine_);
pbos@webrtc.org29d58392013-05-16 12:08:03 +000057}
58
pbos@webrtc.org74fa4892013-08-23 09:19:30 +000059PacketReceiver* VideoCall::Receiver() { return this; }
pbos@webrtc.org29d58392013-05-16 12:08:03 +000060
61std::vector<VideoCodec> VideoCall::GetVideoCodecs() {
62 std::vector<VideoCodec> codecs;
63
64 VideoCodec codec;
65 for (size_t i = 0; i < static_cast<size_t>(codec_->NumberOfCodecs()); ++i) {
66 if (codec_->GetCodec(i, codec) == 0) {
67 codecs.push_back(codec);
68 }
69 }
70 return codecs;
71}
72
pbos@webrtc.org025f4f12013-06-05 11:33:21 +000073VideoSendStream::Config VideoCall::GetDefaultSendConfig() {
74 VideoSendStream::Config config;
75 codec_->GetCodec(0, config.codec);
76 return config;
pbos@webrtc.org29d58392013-05-16 12:08:03 +000077}
78
pbos@webrtc.org74fa4892013-08-23 09:19:30 +000079VideoSendStream* VideoCall::CreateSendStream(
80 const VideoSendStream::Config& config) {
pbos@webrtc.org1819fd72013-06-10 13:48:26 +000081 assert(config.rtp.ssrcs.size() > 0);
82 assert(config.codec.numberOfSimulcastStreams == 0 ||
83 config.codec.numberOfSimulcastStreams == config.rtp.ssrcs.size());
84
pbos@webrtc.org40523702013-08-05 12:49:22 +000085 VideoSendStream* send_stream = new VideoSendStream(
86 config_.send_transport, config_.overuse_detection, video_engine_, config);
pbos@webrtc.org1819fd72013-06-10 13:48:26 +000087
88 WriteLockScoped write_lock(*send_lock_);
89 for (size_t i = 0; i < config.rtp.ssrcs.size(); ++i) {
90 assert(send_ssrcs_.find(config.rtp.ssrcs[i]) == send_ssrcs_.end());
91 send_ssrcs_[config.rtp.ssrcs[i]] = send_stream;
pbos@webrtc.org29d58392013-05-16 12:08:03 +000092 }
93 return send_stream;
94}
95
pbos@webrtc.org74fa4892013-08-23 09:19:30 +000096SendStreamState* VideoCall::DestroySendStream(
97 webrtc::VideoSendStream* send_stream) {
pbos@webrtc.org95e51f52013-09-05 12:38:54 +000098 assert(send_stream != NULL);
99
100 VideoSendStream* send_stream_impl = NULL;
101 {
102 WriteLockScoped write_lock(*send_lock_);
103 for (std::map<uint32_t, VideoSendStream*>::iterator it =
104 send_ssrcs_.begin();
105 it != send_ssrcs_.end();
106 ++it) {
107 if (it->second == static_cast<VideoSendStream*>(send_stream)) {
108 send_stream_impl = it->second;
109 send_ssrcs_.erase(it);
110 break;
111 }
112 }
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000113 }
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000114
115 assert(send_stream_impl != NULL);
116 delete send_stream_impl;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000117
118 // TODO(pbos): Return its previous state
119 return NULL;
120}
121
pbos@webrtc.org025f4f12013-06-05 11:33:21 +0000122VideoReceiveStream::Config VideoCall::GetDefaultReceiveConfig() {
pbos@webrtc.org74fa4892013-08-23 09:19:30 +0000123 return VideoReceiveStream::Config();
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000124}
125
pbos@webrtc.org74fa4892013-08-23 09:19:30 +0000126VideoReceiveStream* VideoCall::CreateReceiveStream(
127 const VideoReceiveStream::Config& config) {
pbos@webrtc.org40523702013-08-05 12:49:22 +0000128 VideoReceiveStream* receive_stream =
129 new VideoReceiveStream(video_engine_, config, config_.send_transport);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000130
pbos@webrtc.org1819fd72013-06-10 13:48:26 +0000131 WriteLockScoped write_lock(*receive_lock_);
132 assert(receive_ssrcs_.find(config.rtp.ssrc) == receive_ssrcs_.end());
133 receive_ssrcs_[config.rtp.ssrc] = receive_stream;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000134 return receive_stream;
135}
136
137void VideoCall::DestroyReceiveStream(
pbos@webrtc.org74fa4892013-08-23 09:19:30 +0000138 webrtc::VideoReceiveStream* receive_stream) {
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000139 assert(receive_stream != NULL);
140
141 VideoReceiveStream* receive_stream_impl = NULL;
142 {
143 WriteLockScoped write_lock(*receive_lock_);
144 for (std::map<uint32_t, VideoReceiveStream*>::iterator it =
145 receive_ssrcs_.begin();
146 it != receive_ssrcs_.end();
147 ++it) {
148 if (it->second == static_cast<VideoReceiveStream*>(receive_stream)) {
149 receive_stream_impl = it->second;
150 receive_ssrcs_.erase(it);
151 break;
152 }
153 }
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000154 }
pbos@webrtc.org95e51f52013-09-05 12:38:54 +0000155
156 assert(receive_stream_impl != NULL);
157 delete receive_stream_impl;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000158}
159
160uint32_t VideoCall::SendBitrateEstimate() {
161 // TODO(pbos): Return send-bitrate estimate
162 return 0;
163}
164
165uint32_t VideoCall::ReceiveBitrateEstimate() {
166 // TODO(pbos): Return receive-bitrate estimate
167 return 0;
168}
169
pbos@webrtc.org40523702013-08-05 12:49:22 +0000170bool VideoCall::DeliverRtcp(const uint8_t* packet, size_t length) {
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000171 // TODO(pbos): Figure out what channel needs it actually.
172 // Do NOT broadcast! Also make sure it's a valid packet.
173 bool rtcp_delivered = false;
pbos@webrtc.org40523702013-08-05 12:49:22 +0000174 {
175 ReadLockScoped read_lock(*receive_lock_);
176 for (std::map<uint32_t, VideoReceiveStream*>::iterator it =
177 receive_ssrcs_.begin();
178 it != receive_ssrcs_.end();
179 ++it) {
180 if (it->second->DeliverRtcp(static_cast<const uint8_t*>(packet),
181 length)) {
182 rtcp_delivered = true;
183 }
pbos@webrtc.orgbbb07e62013-08-05 12:01:36 +0000184 }
185 }
186
pbos@webrtc.org40523702013-08-05 12:49:22 +0000187 {
188 ReadLockScoped read_lock(*send_lock_);
189 for (std::map<uint32_t, VideoSendStream*>::iterator it =
190 send_ssrcs_.begin();
191 it != send_ssrcs_.end();
192 ++it) {
193 if (it->second->DeliverRtcp(static_cast<const uint8_t*>(packet),
194 length)) {
195 rtcp_delivered = true;
196 }
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000197 }
198 }
199 return rtcp_delivered;
200}
201
pbos@webrtc.org40523702013-08-05 12:49:22 +0000202bool VideoCall::DeliverRtp(const RTPHeader& header,
203 const uint8_t* packet,
204 size_t length) {
205 VideoReceiveStream* receiver;
206 {
207 ReadLockScoped read_lock(*receive_lock_);
208 std::map<uint32_t, VideoReceiveStream*>::iterator it =
209 receive_ssrcs_.find(header.ssrc);
210 if (it == receive_ssrcs_.end()) {
211 // TODO(pbos): Log some warning, SSRC without receiver.
212 return false;
213 }
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000214
pbos@webrtc.org40523702013-08-05 12:49:22 +0000215 receiver = it->second;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000216 }
pbos@webrtc.orgbbb07e62013-08-05 12:01:36 +0000217 return receiver->DeliverRtp(static_cast<const uint8_t*>(packet), length);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000218}
219
pbos@webrtc.org40523702013-08-05 12:49:22 +0000220bool VideoCall::DeliverPacket(const uint8_t* packet, size_t length) {
221 // TODO(pbos): ExtensionMap if there are extensions.
222 if (RtpHeaderParser::IsRtcp(packet, static_cast<int>(length)))
223 return DeliverRtcp(packet, length);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000224
pbos@webrtc.org40523702013-08-05 12:49:22 +0000225 RTPHeader rtp_header;
226 if (!rtp_header_parser_->Parse(packet, static_cast<int>(length), &rtp_header))
227 return false;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000228
pbos@webrtc.org40523702013-08-05 12:49:22 +0000229 return DeliverRtp(rtp_header, packet, length);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000230}
231
232} // namespace internal
233} // namespace webrtc