blob: 99112bff4b15acb56259546c74c7c31d76f9251f [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.orgf5d4cb12013-05-17 13:44:48 +000024#include "webrtc/video_engine/new_include/video_engine.h"
pbos@webrtc.org29d58392013-05-16 12:08:03 +000025
26namespace webrtc {
27namespace internal {
28
29VideoCall::VideoCall(webrtc::VideoEngine* video_engine,
mflodman@webrtc.org6879c8a2013-07-23 11:35:00 +000030 const newapi::VideoCall::Config& config)
31 : config_(config),
pbos@webrtc.org1819fd72013-06-10 13:48:26 +000032 receive_lock_(RWLockWrapper::CreateRWLock()),
33 send_lock_(RWLockWrapper::CreateRWLock()),
pbos@webrtc.org40523702013-08-05 12:49:22 +000034 rtp_header_parser_(RtpHeaderParser::Create()),
pbos@webrtc.org1819fd72013-06-10 13:48:26 +000035 video_engine_(video_engine) {
pbos@webrtc.org29d58392013-05-16 12:08:03 +000036 assert(video_engine != NULL);
mflodman@webrtc.org6879c8a2013-07-23 11:35:00 +000037 assert(config.send_transport != NULL);
pbos@webrtc.org29d58392013-05-16 12:08:03 +000038
39 rtp_rtcp_ = ViERTP_RTCP::GetInterface(video_engine_);
40 assert(rtp_rtcp_ != NULL);
41
42 codec_ = ViECodec::GetInterface(video_engine_);
43 assert(codec_ != NULL);
44}
45
46VideoCall::~VideoCall() {
47 rtp_rtcp_->Release();
48 codec_->Release();
49}
50
51newapi::PacketReceiver* VideoCall::Receiver() { return this; }
52
53std::vector<VideoCodec> VideoCall::GetVideoCodecs() {
54 std::vector<VideoCodec> codecs;
55
56 VideoCodec codec;
57 for (size_t i = 0; i < static_cast<size_t>(codec_->NumberOfCodecs()); ++i) {
58 if (codec_->GetCodec(i, codec) == 0) {
59 codecs.push_back(codec);
60 }
61 }
62 return codecs;
63}
64
pbos@webrtc.org025f4f12013-06-05 11:33:21 +000065VideoSendStream::Config VideoCall::GetDefaultSendConfig() {
66 VideoSendStream::Config config;
67 codec_->GetCodec(0, config.codec);
68 return config;
pbos@webrtc.org29d58392013-05-16 12:08:03 +000069}
70
71newapi::VideoSendStream* VideoCall::CreateSendStream(
pbos@webrtc.org1819fd72013-06-10 13:48:26 +000072 const newapi::VideoSendStream::Config& config) {
73 assert(config.rtp.ssrcs.size() > 0);
74 assert(config.codec.numberOfSimulcastStreams == 0 ||
75 config.codec.numberOfSimulcastStreams == config.rtp.ssrcs.size());
76
pbos@webrtc.org40523702013-08-05 12:49:22 +000077 VideoSendStream* send_stream = new VideoSendStream(
78 config_.send_transport, config_.overuse_detection, video_engine_, config);
pbos@webrtc.org1819fd72013-06-10 13:48:26 +000079
80 WriteLockScoped write_lock(*send_lock_);
81 for (size_t i = 0; i < config.rtp.ssrcs.size(); ++i) {
82 assert(send_ssrcs_.find(config.rtp.ssrcs[i]) == send_ssrcs_.end());
83 send_ssrcs_[config.rtp.ssrcs[i]] = send_stream;
pbos@webrtc.org29d58392013-05-16 12:08:03 +000084 }
85 return send_stream;
86}
87
88newapi::SendStreamState* VideoCall::DestroySendStream(
89 newapi::VideoSendStream* send_stream) {
90 if (send_stream == NULL) {
91 return NULL;
92 }
93 // TODO(pbos): Remove it properly! Free the SSRCs!
94 delete static_cast<VideoSendStream*>(send_stream);
95
96 // TODO(pbos): Return its previous state
97 return NULL;
98}
99
pbos@webrtc.org025f4f12013-06-05 11:33:21 +0000100VideoReceiveStream::Config VideoCall::GetDefaultReceiveConfig() {
101 return newapi::VideoReceiveStream::Config();
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000102}
103
104newapi::VideoReceiveStream* VideoCall::CreateReceiveStream(
pbos@webrtc.org1819fd72013-06-10 13:48:26 +0000105 const newapi::VideoReceiveStream::Config& config) {
pbos@webrtc.org40523702013-08-05 12:49:22 +0000106 VideoReceiveStream* receive_stream =
107 new VideoReceiveStream(video_engine_, config, config_.send_transport);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000108
pbos@webrtc.org1819fd72013-06-10 13:48:26 +0000109 WriteLockScoped write_lock(*receive_lock_);
110 assert(receive_ssrcs_.find(config.rtp.ssrc) == receive_ssrcs_.end());
111 receive_ssrcs_[config.rtp.ssrc] = receive_stream;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000112 return receive_stream;
113}
114
115void VideoCall::DestroyReceiveStream(
116 newapi::VideoReceiveStream* receive_stream) {
117 if (receive_stream == NULL) {
118 return;
119 }
120 // TODO(pbos): Remove its SSRCs!
121 delete static_cast<VideoReceiveStream*>(receive_stream);
122}
123
124uint32_t VideoCall::SendBitrateEstimate() {
125 // TODO(pbos): Return send-bitrate estimate
126 return 0;
127}
128
129uint32_t VideoCall::ReceiveBitrateEstimate() {
130 // TODO(pbos): Return receive-bitrate estimate
131 return 0;
132}
133
pbos@webrtc.org40523702013-08-05 12:49:22 +0000134bool VideoCall::DeliverRtcp(const uint8_t* packet, size_t length) {
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000135 // TODO(pbos): Figure out what channel needs it actually.
136 // Do NOT broadcast! Also make sure it's a valid packet.
137 bool rtcp_delivered = false;
pbos@webrtc.org40523702013-08-05 12:49:22 +0000138 {
139 ReadLockScoped read_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->DeliverRtcp(static_cast<const uint8_t*>(packet),
145 length)) {
146 rtcp_delivered = true;
147 }
pbos@webrtc.orgbbb07e62013-08-05 12:01:36 +0000148 }
149 }
150
pbos@webrtc.org40523702013-08-05 12:49:22 +0000151 {
152 ReadLockScoped read_lock(*send_lock_);
153 for (std::map<uint32_t, VideoSendStream*>::iterator it =
154 send_ssrcs_.begin();
155 it != send_ssrcs_.end();
156 ++it) {
157 if (it->second->DeliverRtcp(static_cast<const uint8_t*>(packet),
158 length)) {
159 rtcp_delivered = true;
160 }
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000161 }
162 }
163 return rtcp_delivered;
164}
165
pbos@webrtc.org40523702013-08-05 12:49:22 +0000166bool VideoCall::DeliverRtp(const RTPHeader& header,
167 const uint8_t* packet,
168 size_t length) {
169 VideoReceiveStream* receiver;
170 {
171 ReadLockScoped read_lock(*receive_lock_);
172 std::map<uint32_t, VideoReceiveStream*>::iterator it =
173 receive_ssrcs_.find(header.ssrc);
174 if (it == receive_ssrcs_.end()) {
175 // TODO(pbos): Log some warning, SSRC without receiver.
176 return false;
177 }
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000178
pbos@webrtc.org40523702013-08-05 12:49:22 +0000179 receiver = it->second;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000180 }
pbos@webrtc.orgbbb07e62013-08-05 12:01:36 +0000181 return receiver->DeliverRtp(static_cast<const uint8_t*>(packet), length);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000182}
183
pbos@webrtc.org40523702013-08-05 12:49:22 +0000184bool VideoCall::DeliverPacket(const uint8_t* packet, size_t length) {
185 // TODO(pbos): ExtensionMap if there are extensions.
186 if (RtpHeaderParser::IsRtcp(packet, static_cast<int>(length)))
187 return DeliverRtcp(packet, length);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000188
pbos@webrtc.org40523702013-08-05 12:49:22 +0000189 RTPHeader rtp_header;
190 if (!rtp_header_parser_->Parse(packet, static_cast<int>(length), &rtp_header))
191 return false;
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000192
pbos@webrtc.org40523702013-08-05 12:49:22 +0000193 return DeliverRtp(rtp_header, packet, length);
pbos@webrtc.org29d58392013-05-16 12:08:03 +0000194}
195
196} // namespace internal
197} // namespace webrtc