blob: 97f606a17220883816e80993c45d48e8ce77acbd [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.org29d58392013-05-16 12:08:03 +000098 if (send_stream == NULL) {
99 return NULL;
100 }
101 // TODO(pbos): Remove it properly! Free the SSRCs!
102 delete static_cast<VideoSendStream*>(send_stream);
103
104 // TODO(pbos): Return its previous state
105 return NULL;
106}
107
pbos@webrtc.org025f4f12013-06-05 11:33:21 +0000108VideoReceiveStream::Config VideoCall::GetDefaultReceiveConfig() {
pbos@webrtc.org74fa4892013-08-23 09:19:30 +0000109 return VideoReceiveStream::Config();
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000110}
111
pbos@webrtc.org74fa4892013-08-23 09:19:30 +0000112VideoReceiveStream* VideoCall::CreateReceiveStream(
113 const VideoReceiveStream::Config& config) {
pbos@webrtc.org40523702013-08-05 12:49:22 +0000114 VideoReceiveStream* receive_stream =
115 new VideoReceiveStream(video_engine_, config, config_.send_transport);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000116
pbos@webrtc.org1819fd72013-06-10 13:48:26 +0000117 WriteLockScoped write_lock(*receive_lock_);
118 assert(receive_ssrcs_.find(config.rtp.ssrc) == receive_ssrcs_.end());
119 receive_ssrcs_[config.rtp.ssrc] = receive_stream;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000120 return receive_stream;
121}
122
123void VideoCall::DestroyReceiveStream(
pbos@webrtc.org74fa4892013-08-23 09:19:30 +0000124 webrtc::VideoReceiveStream* receive_stream) {
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000125 if (receive_stream == NULL) {
126 return;
127 }
128 // TODO(pbos): Remove its SSRCs!
129 delete static_cast<VideoReceiveStream*>(receive_stream);
130}
131
132uint32_t VideoCall::SendBitrateEstimate() {
133 // TODO(pbos): Return send-bitrate estimate
134 return 0;
135}
136
137uint32_t VideoCall::ReceiveBitrateEstimate() {
138 // TODO(pbos): Return receive-bitrate estimate
139 return 0;
140}
141
pbos@webrtc.org40523702013-08-05 12:49:22 +0000142bool VideoCall::DeliverRtcp(const uint8_t* packet, size_t length) {
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000143 // TODO(pbos): Figure out what channel needs it actually.
144 // Do NOT broadcast! Also make sure it's a valid packet.
145 bool rtcp_delivered = false;
pbos@webrtc.org40523702013-08-05 12:49:22 +0000146 {
147 ReadLockScoped read_lock(*receive_lock_);
148 for (std::map<uint32_t, VideoReceiveStream*>::iterator it =
149 receive_ssrcs_.begin();
150 it != receive_ssrcs_.end();
151 ++it) {
152 if (it->second->DeliverRtcp(static_cast<const uint8_t*>(packet),
153 length)) {
154 rtcp_delivered = true;
155 }
pbos@webrtc.orgbbb07e62013-08-05 12:01:36 +0000156 }
157 }
158
pbos@webrtc.org40523702013-08-05 12:49:22 +0000159 {
160 ReadLockScoped read_lock(*send_lock_);
161 for (std::map<uint32_t, VideoSendStream*>::iterator it =
162 send_ssrcs_.begin();
163 it != send_ssrcs_.end();
164 ++it) {
165 if (it->second->DeliverRtcp(static_cast<const uint8_t*>(packet),
166 length)) {
167 rtcp_delivered = true;
168 }
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000169 }
170 }
171 return rtcp_delivered;
172}
173
pbos@webrtc.org40523702013-08-05 12:49:22 +0000174bool VideoCall::DeliverRtp(const RTPHeader& header,
175 const uint8_t* packet,
176 size_t length) {
177 VideoReceiveStream* receiver;
178 {
179 ReadLockScoped read_lock(*receive_lock_);
180 std::map<uint32_t, VideoReceiveStream*>::iterator it =
181 receive_ssrcs_.find(header.ssrc);
182 if (it == receive_ssrcs_.end()) {
183 // TODO(pbos): Log some warning, SSRC without receiver.
184 return false;
185 }
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000186
pbos@webrtc.org40523702013-08-05 12:49:22 +0000187 receiver = it->second;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000188 }
pbos@webrtc.orgbbb07e62013-08-05 12:01:36 +0000189 return receiver->DeliverRtp(static_cast<const uint8_t*>(packet), length);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000190}
191
pbos@webrtc.org40523702013-08-05 12:49:22 +0000192bool VideoCall::DeliverPacket(const uint8_t* packet, size_t length) {
193 // TODO(pbos): ExtensionMap if there are extensions.
194 if (RtpHeaderParser::IsRtcp(packet, static_cast<int>(length)))
195 return DeliverRtcp(packet, length);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000196
pbos@webrtc.org40523702013-08-05 12:49:22 +0000197 RTPHeader rtp_header;
198 if (!rtp_header_parser_->Parse(packet, static_cast<int>(length), &rtp_header))
199 return false;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000200
pbos@webrtc.org40523702013-08-05 12:49:22 +0000201 return DeliverRtp(rtp_header, packet, length);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000202}
203
204} // namespace internal
205} // namespace webrtc