blob: da014f4b9eb2aa15f4b07e74be897aefeca3d557 [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
13#include <cassert>
14#include <cstring>
15#include <map>
16#include <vector>
17
pbos@webrtc.org29d58392013-05-16 12:08:03 +000018#include "webrtc/video_engine/include/vie_base.h"
19#include "webrtc/video_engine/include/vie_codec.h"
20#include "webrtc/video_engine/include/vie_rtp_rtcp.h"
pbos@webrtc.org29d58392013-05-16 12:08:03 +000021#include "webrtc/video_engine/internal/video_receive_stream.h"
22#include "webrtc/video_engine/internal/video_send_stream.h"
pbos@webrtc.orgf5d4cb12013-05-17 13:44:48 +000023#include "webrtc/video_engine/new_include/video_engine.h"
pbos@webrtc.org29d58392013-05-16 12:08:03 +000024
25namespace webrtc {
26namespace internal {
27
28VideoCall::VideoCall(webrtc::VideoEngine* video_engine,
mflodman@webrtc.org6879c8a2013-07-23 11:35:00 +000029 const newapi::VideoCall::Config& config)
30 : config_(config),
pbos@webrtc.org1819fd72013-06-10 13:48:26 +000031 receive_lock_(RWLockWrapper::CreateRWLock()),
32 send_lock_(RWLockWrapper::CreateRWLock()),
pbos@webrtc.org40523702013-08-05 12:49:22 +000033 rtp_header_parser_(RtpHeaderParser::Create()),
pbos@webrtc.org1819fd72013-06-10 13:48:26 +000034 video_engine_(video_engine) {
pbos@webrtc.org29d58392013-05-16 12:08:03 +000035 assert(video_engine != NULL);
mflodman@webrtc.org6879c8a2013-07-23 11:35:00 +000036 assert(config.send_transport != NULL);
pbos@webrtc.org29d58392013-05-16 12:08:03 +000037
38 rtp_rtcp_ = ViERTP_RTCP::GetInterface(video_engine_);
39 assert(rtp_rtcp_ != NULL);
40
41 codec_ = ViECodec::GetInterface(video_engine_);
42 assert(codec_ != NULL);
43}
44
45VideoCall::~VideoCall() {
46 rtp_rtcp_->Release();
47 codec_->Release();
48}
49
50newapi::PacketReceiver* VideoCall::Receiver() { return this; }
51
52std::vector<VideoCodec> VideoCall::GetVideoCodecs() {
53 std::vector<VideoCodec> codecs;
54
55 VideoCodec codec;
56 for (size_t i = 0; i < static_cast<size_t>(codec_->NumberOfCodecs()); ++i) {
57 if (codec_->GetCodec(i, codec) == 0) {
58 codecs.push_back(codec);
59 }
60 }
61 return codecs;
62}
63
pbos@webrtc.org025f4f12013-06-05 11:33:21 +000064VideoSendStream::Config VideoCall::GetDefaultSendConfig() {
65 VideoSendStream::Config config;
66 codec_->GetCodec(0, config.codec);
67 return config;
pbos@webrtc.org29d58392013-05-16 12:08:03 +000068}
69
70newapi::VideoSendStream* VideoCall::CreateSendStream(
pbos@webrtc.org1819fd72013-06-10 13:48:26 +000071 const newapi::VideoSendStream::Config& config) {
72 assert(config.rtp.ssrcs.size() > 0);
73 assert(config.codec.numberOfSimulcastStreams == 0 ||
74 config.codec.numberOfSimulcastStreams == config.rtp.ssrcs.size());
75
pbos@webrtc.org40523702013-08-05 12:49:22 +000076 VideoSendStream* send_stream = new VideoSendStream(
77 config_.send_transport, config_.overuse_detection, video_engine_, config);
pbos@webrtc.org1819fd72013-06-10 13:48:26 +000078
79 WriteLockScoped write_lock(*send_lock_);
80 for (size_t i = 0; i < config.rtp.ssrcs.size(); ++i) {
81 assert(send_ssrcs_.find(config.rtp.ssrcs[i]) == send_ssrcs_.end());
82 send_ssrcs_[config.rtp.ssrcs[i]] = send_stream;
pbos@webrtc.org29d58392013-05-16 12:08:03 +000083 }
84 return send_stream;
85}
86
87newapi::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
pbos@webrtc.org025f4f12013-06-05 11:33:21 +000099VideoReceiveStream::Config VideoCall::GetDefaultReceiveConfig() {
100 return newapi::VideoReceiveStream::Config();
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000101}
102
103newapi::VideoReceiveStream* VideoCall::CreateReceiveStream(
pbos@webrtc.org1819fd72013-06-10 13:48:26 +0000104 const newapi::VideoReceiveStream::Config& config) {
pbos@webrtc.org40523702013-08-05 12:49:22 +0000105 VideoReceiveStream* receive_stream =
106 new VideoReceiveStream(video_engine_, config, config_.send_transport);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000107
pbos@webrtc.org1819fd72013-06-10 13:48:26 +0000108 WriteLockScoped write_lock(*receive_lock_);
109 assert(receive_ssrcs_.find(config.rtp.ssrc) == receive_ssrcs_.end());
110 receive_ssrcs_[config.rtp.ssrc] = receive_stream;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000111 return receive_stream;
112}
113
114void VideoCall::DestroyReceiveStream(
115 newapi::VideoReceiveStream* receive_stream) {
116 if (receive_stream == NULL) {
117 return;
118 }
119 // TODO(pbos): Remove its SSRCs!
120 delete static_cast<VideoReceiveStream*>(receive_stream);
121}
122
123uint32_t VideoCall::SendBitrateEstimate() {
124 // TODO(pbos): Return send-bitrate estimate
125 return 0;
126}
127
128uint32_t VideoCall::ReceiveBitrateEstimate() {
129 // TODO(pbos): Return receive-bitrate estimate
130 return 0;
131}
132
pbos@webrtc.org40523702013-08-05 12:49:22 +0000133bool VideoCall::DeliverRtcp(const uint8_t* packet, size_t length) {
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000134 // TODO(pbos): Figure out what channel needs it actually.
135 // Do NOT broadcast! Also make sure it's a valid packet.
136 bool rtcp_delivered = false;
pbos@webrtc.org40523702013-08-05 12:49:22 +0000137 {
138 ReadLockScoped read_lock(*receive_lock_);
139 for (std::map<uint32_t, VideoReceiveStream*>::iterator it =
140 receive_ssrcs_.begin();
141 it != receive_ssrcs_.end();
142 ++it) {
143 if (it->second->DeliverRtcp(static_cast<const uint8_t*>(packet),
144 length)) {
145 rtcp_delivered = true;
146 }
pbos@webrtc.orgbbb07e62013-08-05 12:01:36 +0000147 }
148 }
149
pbos@webrtc.org40523702013-08-05 12:49:22 +0000150 {
151 ReadLockScoped read_lock(*send_lock_);
152 for (std::map<uint32_t, VideoSendStream*>::iterator it =
153 send_ssrcs_.begin();
154 it != send_ssrcs_.end();
155 ++it) {
156 if (it->second->DeliverRtcp(static_cast<const uint8_t*>(packet),
157 length)) {
158 rtcp_delivered = true;
159 }
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000160 }
161 }
162 return rtcp_delivered;
163}
164
pbos@webrtc.org40523702013-08-05 12:49:22 +0000165bool VideoCall::DeliverRtp(const RTPHeader& header,
166 const uint8_t* packet,
167 size_t length) {
168 VideoReceiveStream* receiver;
169 {
170 ReadLockScoped read_lock(*receive_lock_);
171 std::map<uint32_t, VideoReceiveStream*>::iterator it =
172 receive_ssrcs_.find(header.ssrc);
173 if (it == receive_ssrcs_.end()) {
174 // TODO(pbos): Log some warning, SSRC without receiver.
175 return false;
176 }
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000177
pbos@webrtc.org40523702013-08-05 12:49:22 +0000178 receiver = it->second;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000179 }
pbos@webrtc.orgbbb07e62013-08-05 12:01:36 +0000180 return receiver->DeliverRtp(static_cast<const uint8_t*>(packet), length);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000181}
182
pbos@webrtc.org40523702013-08-05 12:49:22 +0000183bool VideoCall::DeliverPacket(const uint8_t* packet, size_t length) {
184 // TODO(pbos): ExtensionMap if there are extensions.
185 if (RtpHeaderParser::IsRtcp(packet, static_cast<int>(length)))
186 return DeliverRtcp(packet, length);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000187
pbos@webrtc.org40523702013-08-05 12:49:22 +0000188 RTPHeader rtp_header;
189 if (!rtp_header_parser_->Parse(packet, static_cast<int>(length), &rtp_header))
190 return false;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000191
pbos@webrtc.org40523702013-08-05 12:49:22 +0000192 return DeliverRtp(rtp_header, packet, length);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000193}
194
195} // namespace internal
196} // namespace webrtc