blob: 04e4bdd7255bf4af23ab81521e8077265459efe8 [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
27namespace newapi {
28VideoCall* VideoCall::Create(const newapi::VideoCall::Config& config) {
29 webrtc::VideoEngine* video_engine = webrtc::VideoEngine::Create();
30 assert(video_engine != NULL);
31
pbos@webrtc.orgfd39e132013-08-14 13:52:52 +000032 return new internal::VideoCall(video_engine, config);
33}
34} // namespace newapi
35
pbos@webrtc.org29d58392013-05-16 12:08:03 +000036namespace internal {
37
38VideoCall::VideoCall(webrtc::VideoEngine* video_engine,
mflodman@webrtc.org6879c8a2013-07-23 11:35:00 +000039 const newapi::VideoCall::Config& config)
40 : config_(config),
pbos@webrtc.org1819fd72013-06-10 13:48:26 +000041 receive_lock_(RWLockWrapper::CreateRWLock()),
42 send_lock_(RWLockWrapper::CreateRWLock()),
pbos@webrtc.org40523702013-08-05 12:49:22 +000043 rtp_header_parser_(RtpHeaderParser::Create()),
pbos@webrtc.org1819fd72013-06-10 13:48:26 +000044 video_engine_(video_engine) {
pbos@webrtc.org29d58392013-05-16 12:08:03 +000045 assert(video_engine != NULL);
mflodman@webrtc.org6879c8a2013-07-23 11:35:00 +000046 assert(config.send_transport != NULL);
pbos@webrtc.org29d58392013-05-16 12:08:03 +000047
48 rtp_rtcp_ = ViERTP_RTCP::GetInterface(video_engine_);
49 assert(rtp_rtcp_ != NULL);
50
51 codec_ = ViECodec::GetInterface(video_engine_);
52 assert(codec_ != NULL);
53}
54
55VideoCall::~VideoCall() {
pbos@webrtc.org29d58392013-05-16 12:08:03 +000056 codec_->Release();
pbos@webrtc.orgfd39e132013-08-14 13:52:52 +000057 rtp_rtcp_->Release();
58 webrtc::VideoEngine::Delete(video_engine_);
pbos@webrtc.org29d58392013-05-16 12:08:03 +000059}
60
61newapi::PacketReceiver* VideoCall::Receiver() { return this; }
62
63std::vector<VideoCodec> VideoCall::GetVideoCodecs() {
64 std::vector<VideoCodec> codecs;
65
66 VideoCodec codec;
67 for (size_t i = 0; i < static_cast<size_t>(codec_->NumberOfCodecs()); ++i) {
68 if (codec_->GetCodec(i, codec) == 0) {
69 codecs.push_back(codec);
70 }
71 }
72 return codecs;
73}
74
pbos@webrtc.org025f4f12013-06-05 11:33:21 +000075VideoSendStream::Config VideoCall::GetDefaultSendConfig() {
76 VideoSendStream::Config config;
77 codec_->GetCodec(0, config.codec);
78 return config;
pbos@webrtc.org29d58392013-05-16 12:08:03 +000079}
80
81newapi::VideoSendStream* VideoCall::CreateSendStream(
pbos@webrtc.org1819fd72013-06-10 13:48:26 +000082 const newapi::VideoSendStream::Config& config) {
83 assert(config.rtp.ssrcs.size() > 0);
84 assert(config.codec.numberOfSimulcastStreams == 0 ||
85 config.codec.numberOfSimulcastStreams == config.rtp.ssrcs.size());
86
pbos@webrtc.org40523702013-08-05 12:49:22 +000087 VideoSendStream* send_stream = new VideoSendStream(
88 config_.send_transport, config_.overuse_detection, video_engine_, config);
pbos@webrtc.org1819fd72013-06-10 13:48:26 +000089
90 WriteLockScoped write_lock(*send_lock_);
91 for (size_t i = 0; i < config.rtp.ssrcs.size(); ++i) {
92 assert(send_ssrcs_.find(config.rtp.ssrcs[i]) == send_ssrcs_.end());
93 send_ssrcs_[config.rtp.ssrcs[i]] = send_stream;
pbos@webrtc.org29d58392013-05-16 12:08:03 +000094 }
95 return send_stream;
96}
97
98newapi::SendStreamState* VideoCall::DestroySendStream(
99 newapi::VideoSendStream* send_stream) {
100 if (send_stream == NULL) {
101 return NULL;
102 }
103 // TODO(pbos): Remove it properly! Free the SSRCs!
104 delete static_cast<VideoSendStream*>(send_stream);
105
106 // TODO(pbos): Return its previous state
107 return NULL;
108}
109
pbos@webrtc.org025f4f12013-06-05 11:33:21 +0000110VideoReceiveStream::Config VideoCall::GetDefaultReceiveConfig() {
111 return newapi::VideoReceiveStream::Config();
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000112}
113
114newapi::VideoReceiveStream* VideoCall::CreateReceiveStream(
pbos@webrtc.org1819fd72013-06-10 13:48:26 +0000115 const newapi::VideoReceiveStream::Config& config) {
pbos@webrtc.org40523702013-08-05 12:49:22 +0000116 VideoReceiveStream* receive_stream =
117 new VideoReceiveStream(video_engine_, config, config_.send_transport);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000118
pbos@webrtc.org1819fd72013-06-10 13:48:26 +0000119 WriteLockScoped write_lock(*receive_lock_);
120 assert(receive_ssrcs_.find(config.rtp.ssrc) == receive_ssrcs_.end());
121 receive_ssrcs_[config.rtp.ssrc] = receive_stream;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000122 return receive_stream;
123}
124
125void VideoCall::DestroyReceiveStream(
126 newapi::VideoReceiveStream* receive_stream) {
127 if (receive_stream == NULL) {
128 return;
129 }
130 // TODO(pbos): Remove its SSRCs!
131 delete static_cast<VideoReceiveStream*>(receive_stream);
132}
133
134uint32_t VideoCall::SendBitrateEstimate() {
135 // TODO(pbos): Return send-bitrate estimate
136 return 0;
137}
138
139uint32_t VideoCall::ReceiveBitrateEstimate() {
140 // TODO(pbos): Return receive-bitrate estimate
141 return 0;
142}
143
pbos@webrtc.org40523702013-08-05 12:49:22 +0000144bool VideoCall::DeliverRtcp(const uint8_t* packet, size_t length) {
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000145 // TODO(pbos): Figure out what channel needs it actually.
146 // Do NOT broadcast! Also make sure it's a valid packet.
147 bool rtcp_delivered = false;
pbos@webrtc.org40523702013-08-05 12:49:22 +0000148 {
149 ReadLockScoped read_lock(*receive_lock_);
150 for (std::map<uint32_t, VideoReceiveStream*>::iterator it =
151 receive_ssrcs_.begin();
152 it != receive_ssrcs_.end();
153 ++it) {
154 if (it->second->DeliverRtcp(static_cast<const uint8_t*>(packet),
155 length)) {
156 rtcp_delivered = true;
157 }
pbos@webrtc.orgbbb07e62013-08-05 12:01:36 +0000158 }
159 }
160
pbos@webrtc.org40523702013-08-05 12:49:22 +0000161 {
162 ReadLockScoped read_lock(*send_lock_);
163 for (std::map<uint32_t, VideoSendStream*>::iterator it =
164 send_ssrcs_.begin();
165 it != send_ssrcs_.end();
166 ++it) {
167 if (it->second->DeliverRtcp(static_cast<const uint8_t*>(packet),
168 length)) {
169 rtcp_delivered = true;
170 }
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000171 }
172 }
173 return rtcp_delivered;
174}
175
pbos@webrtc.org40523702013-08-05 12:49:22 +0000176bool VideoCall::DeliverRtp(const RTPHeader& header,
177 const uint8_t* packet,
178 size_t length) {
179 VideoReceiveStream* receiver;
180 {
181 ReadLockScoped read_lock(*receive_lock_);
182 std::map<uint32_t, VideoReceiveStream*>::iterator it =
183 receive_ssrcs_.find(header.ssrc);
184 if (it == receive_ssrcs_.end()) {
185 // TODO(pbos): Log some warning, SSRC without receiver.
186 return false;
187 }
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000188
pbos@webrtc.org40523702013-08-05 12:49:22 +0000189 receiver = it->second;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000190 }
pbos@webrtc.orgbbb07e62013-08-05 12:01:36 +0000191 return receiver->DeliverRtp(static_cast<const uint8_t*>(packet), length);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000192}
193
pbos@webrtc.org40523702013-08-05 12:49:22 +0000194bool VideoCall::DeliverPacket(const uint8_t* packet, size_t length) {
195 // TODO(pbos): ExtensionMap if there are extensions.
196 if (RtpHeaderParser::IsRtcp(packet, static_cast<int>(length)))
197 return DeliverRtcp(packet, length);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000198
pbos@webrtc.org40523702013-08-05 12:49:22 +0000199 RTPHeader rtp_header;
200 if (!rtp_header_parser_->Parse(packet, static_cast<int>(length), &rtp_header))
201 return false;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000202
pbos@webrtc.org40523702013-08-05 12:49:22 +0000203 return DeliverRtp(rtp_header, packet, length);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000204}
205
206} // namespace internal
207} // namespace webrtc