2013-07-10 00:45:36 +00:00
|
|
|
/*
|
2016-02-07 20:46:45 -08:00
|
|
|
* Copyright (c) 2004 The WebRTC project authors. All Rights Reserved.
|
2013-07-10 00:45:36 +00:00
|
|
|
*
|
2016-02-07 20:46:45 -08:00
|
|
|
* Use of this source code is governed by a BSD-style license
|
|
|
|
|
* that can be found in the LICENSE file in the root of the source
|
|
|
|
|
* tree. An additional intellectual property rights grant can be found
|
|
|
|
|
* in the file PATENTS. All contributing project authors may
|
|
|
|
|
* be found in the AUTHORS file in the root of the source tree.
|
2013-07-10 00:45:36 +00:00
|
|
|
*/
|
|
|
|
|
|
2019-01-11 09:11:00 -08:00
|
|
|
#ifndef MEDIA_BASE_FAKE_NETWORK_INTERFACE_H_
|
|
|
|
|
#define MEDIA_BASE_FAKE_NETWORK_INTERFACE_H_
|
2013-07-10 00:45:36 +00:00
|
|
|
|
|
|
|
|
#include <map>
|
2017-10-31 09:53:08 -07:00
|
|
|
#include <set>
|
2022-08-17 10:24:02 +02:00
|
|
|
#include <utility>
|
2014-08-13 17:26:08 +00:00
|
|
|
#include <vector>
|
2013-07-10 00:45:36 +00:00
|
|
|
|
2022-08-17 10:24:02 +02:00
|
|
|
#include "api/task_queue/pending_task_safety_flag.h"
|
|
|
|
|
#include "api/task_queue/task_queue_base.h"
|
2019-01-11 09:11:00 -08:00
|
|
|
#include "media/base/media_channel.h"
|
|
|
|
|
#include "media/base/rtp_utils.h"
|
2023-01-10 14:28:25 +01:00
|
|
|
#include "modules/rtp_rtcp/source/rtp_packet_received.h"
|
2021-07-19 13:20:46 +00:00
|
|
|
#include "modules/rtp_rtcp/source/rtp_util.h"
|
2019-01-11 09:11:00 -08:00
|
|
|
#include "rtc_base/byte_order.h"
|
2021-07-09 15:46:14 +00:00
|
|
|
#include "rtc_base/checks.h"
|
2019-01-11 09:11:00 -08:00
|
|
|
#include "rtc_base/copy_on_write_buffer.h"
|
2017-09-15 06:47:31 +02:00
|
|
|
#include "rtc_base/dscp.h"
|
2020-07-07 15:43:11 +02:00
|
|
|
#include "rtc_base/synchronization/mutex.h"
|
2017-09-15 06:47:31 +02:00
|
|
|
#include "rtc_base/thread.h"
|
2023-01-10 14:28:25 +01:00
|
|
|
#include "rtc_base/time_utils.h"
|
2013-07-10 00:45:36 +00:00
|
|
|
|
|
|
|
|
namespace cricket {
|
|
|
|
|
|
|
|
|
|
// Fake NetworkInterface that sends/receives RTP/RTCP packets.
|
2022-12-13 12:57:24 +00:00
|
|
|
class FakeNetworkInterface : public MediaChannelNetworkInterface {
|
2013-07-10 00:45:36 +00:00
|
|
|
public:
|
|
|
|
|
FakeNetworkInterface()
|
2014-07-29 17:36:52 +00:00
|
|
|
: thread_(rtc::Thread::Current()),
|
2013-07-10 00:45:36 +00:00
|
|
|
dest_(NULL),
|
|
|
|
|
conf_(false),
|
|
|
|
|
sendbuf_size_(-1),
|
2013-10-31 15:40:38 +00:00
|
|
|
recvbuf_size_(-1),
|
2018-06-19 15:03:05 +02:00
|
|
|
dscp_(rtc::DSCP_NO_CHANGE) {}
|
2013-07-10 00:45:36 +00:00
|
|
|
|
2023-06-20 18:30:03 +00:00
|
|
|
void SetDestination(MediaReceiveChannelInterface* dest) { dest_ = dest; }
|
2013-07-10 00:45:36 +00:00
|
|
|
|
|
|
|
|
// Conference mode is a mode where instead of simply forwarding the packets,
|
|
|
|
|
// the transport will send multiple copies of the packet with the specified
|
|
|
|
|
// SSRCs. This allows us to simulate receiving media from multiple sources.
|
2020-05-14 14:47:12 +02:00
|
|
|
void SetConferenceMode(bool conf, const std::vector<uint32_t>& ssrcs)
|
2020-07-07 15:43:11 +02:00
|
|
|
RTC_LOCKS_EXCLUDED(mutex_) {
|
|
|
|
|
webrtc::MutexLock lock(&mutex_);
|
2013-07-10 00:45:36 +00:00
|
|
|
conf_ = conf;
|
|
|
|
|
conf_sent_ssrcs_ = ssrcs;
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-07 15:43:11 +02:00
|
|
|
int NumRtpBytes() RTC_LOCKS_EXCLUDED(mutex_) {
|
|
|
|
|
webrtc::MutexLock lock(&mutex_);
|
2013-07-10 00:45:36 +00:00
|
|
|
int bytes = 0;
|
|
|
|
|
for (size_t i = 0; i < rtp_packets_.size(); ++i) {
|
2015-03-24 09:19:06 +00:00
|
|
|
bytes += static_cast<int>(rtp_packets_[i].size());
|
2013-07-10 00:45:36 +00:00
|
|
|
}
|
|
|
|
|
return bytes;
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-07 15:43:11 +02:00
|
|
|
int NumRtpBytes(uint32_t ssrc) RTC_LOCKS_EXCLUDED(mutex_) {
|
|
|
|
|
webrtc::MutexLock lock(&mutex_);
|
2013-07-10 00:45:36 +00:00
|
|
|
int bytes = 0;
|
|
|
|
|
GetNumRtpBytesAndPackets(ssrc, &bytes, NULL);
|
|
|
|
|
return bytes;
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-07 15:43:11 +02:00
|
|
|
int NumRtpPackets() RTC_LOCKS_EXCLUDED(mutex_) {
|
|
|
|
|
webrtc::MutexLock lock(&mutex_);
|
2013-07-22 21:07:49 +00:00
|
|
|
return static_cast<int>(rtp_packets_.size());
|
2013-07-10 00:45:36 +00:00
|
|
|
}
|
|
|
|
|
|
2020-07-07 15:43:11 +02:00
|
|
|
int NumRtpPackets(uint32_t ssrc) RTC_LOCKS_EXCLUDED(mutex_) {
|
|
|
|
|
webrtc::MutexLock lock(&mutex_);
|
2013-07-10 00:45:36 +00:00
|
|
|
int packets = 0;
|
|
|
|
|
GetNumRtpBytesAndPackets(ssrc, NULL, &packets);
|
|
|
|
|
return packets;
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-07 15:43:11 +02:00
|
|
|
int NumSentSsrcs() RTC_LOCKS_EXCLUDED(mutex_) {
|
|
|
|
|
webrtc::MutexLock lock(&mutex_);
|
2013-07-22 21:07:49 +00:00
|
|
|
return static_cast<int>(sent_ssrcs_.size());
|
2013-07-10 00:45:36 +00:00
|
|
|
}
|
|
|
|
|
|
2021-06-03 12:27:12 +02:00
|
|
|
rtc::CopyOnWriteBuffer GetRtpPacket(int index) RTC_LOCKS_EXCLUDED(mutex_) {
|
2020-07-07 15:43:11 +02:00
|
|
|
webrtc::MutexLock lock(&mutex_);
|
2020-05-14 14:47:12 +02:00
|
|
|
if (index >= static_cast<int>(rtp_packets_.size())) {
|
2021-06-03 12:27:12 +02:00
|
|
|
return {};
|
2013-07-10 00:45:36 +00:00
|
|
|
}
|
2021-06-03 12:27:12 +02:00
|
|
|
return rtp_packets_[index];
|
2013-07-10 00:45:36 +00:00
|
|
|
}
|
|
|
|
|
|
2020-07-07 15:43:11 +02:00
|
|
|
int NumRtcpPackets() RTC_LOCKS_EXCLUDED(mutex_) {
|
|
|
|
|
webrtc::MutexLock lock(&mutex_);
|
2013-07-22 21:07:49 +00:00
|
|
|
return static_cast<int>(rtcp_packets_.size());
|
2013-07-10 00:45:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Note: callers are responsible for deleting the returned buffer.
|
2020-05-14 14:47:12 +02:00
|
|
|
const rtc::CopyOnWriteBuffer* GetRtcpPacket(int index)
|
2020-07-07 15:43:11 +02:00
|
|
|
RTC_LOCKS_EXCLUDED(mutex_) {
|
|
|
|
|
webrtc::MutexLock lock(&mutex_);
|
2020-05-14 14:47:12 +02:00
|
|
|
if (index >= static_cast<int>(rtcp_packets_.size())) {
|
2013-07-10 00:45:36 +00:00
|
|
|
return NULL;
|
|
|
|
|
}
|
2016-03-20 06:15:43 -07:00
|
|
|
return new rtc::CopyOnWriteBuffer(rtcp_packets_[index]);
|
2013-07-10 00:45:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int sendbuf_size() const { return sendbuf_size_; }
|
|
|
|
|
int recvbuf_size() const { return recvbuf_size_; }
|
2014-07-29 17:36:52 +00:00
|
|
|
rtc::DiffServCodePoint dscp() const { return dscp_; }
|
2018-09-17 17:06:08 -07:00
|
|
|
rtc::PacketOptions options() const { return options_; }
|
2013-07-10 00:45:36 +00:00
|
|
|
|
|
|
|
|
protected:
|
2016-03-20 06:15:43 -07:00
|
|
|
virtual bool SendPacket(rtc::CopyOnWriteBuffer* packet,
|
2020-05-14 14:47:12 +02:00
|
|
|
const rtc::PacketOptions& options)
|
2020-07-07 15:43:11 +02:00
|
|
|
RTC_LOCKS_EXCLUDED(mutex_) {
|
2021-07-19 13:20:46 +00:00
|
|
|
if (!webrtc::IsRtpPacket(*packet)) {
|
2013-07-10 00:45:36 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
2021-07-19 13:20:46 +00:00
|
|
|
|
|
|
|
|
webrtc::MutexLock lock(&mutex_);
|
|
|
|
|
sent_ssrcs_[webrtc::ParseRtpSsrc(*packet)]++;
|
2018-09-17 17:06:08 -07:00
|
|
|
options_ = options;
|
2013-07-10 00:45:36 +00:00
|
|
|
|
|
|
|
|
rtp_packets_.push_back(*packet);
|
|
|
|
|
if (conf_) {
|
|
|
|
|
for (size_t i = 0; i < conf_sent_ssrcs_.size(); ++i) {
|
2021-07-09 15:46:14 +00:00
|
|
|
SetRtpSsrc(conf_sent_ssrcs_[i], *packet);
|
2022-08-17 10:24:02 +02:00
|
|
|
PostPacket(*packet);
|
2013-07-10 00:45:36 +00:00
|
|
|
}
|
|
|
|
|
} else {
|
2022-08-17 10:24:02 +02:00
|
|
|
PostPacket(*packet);
|
2013-07-10 00:45:36 +00:00
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-20 06:15:43 -07:00
|
|
|
virtual bool SendRtcp(rtc::CopyOnWriteBuffer* packet,
|
2020-05-14 14:47:12 +02:00
|
|
|
const rtc::PacketOptions& options)
|
2020-07-07 15:43:11 +02:00
|
|
|
RTC_LOCKS_EXCLUDED(mutex_) {
|
|
|
|
|
webrtc::MutexLock lock(&mutex_);
|
2013-07-10 00:45:36 +00:00
|
|
|
rtcp_packets_.push_back(*packet);
|
2018-09-17 17:06:08 -07:00
|
|
|
options_ = options;
|
2013-07-10 00:45:36 +00:00
|
|
|
if (!conf_) {
|
|
|
|
|
// don't worry about RTCP in conf mode for now
|
2022-08-17 10:24:02 +02:00
|
|
|
RTC_LOG(LS_VERBOSE) << "Dropping RTCP packet, they are not handled by "
|
|
|
|
|
"MediaChannel anymore.";
|
2013-07-10 00:45:36 +00:00
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-29 14:48:14 +02:00
|
|
|
virtual int SetOption(SocketType /* type */,
|
|
|
|
|
rtc::Socket::Option opt,
|
|
|
|
|
int option) {
|
2014-07-29 17:36:52 +00:00
|
|
|
if (opt == rtc::Socket::OPT_SNDBUF) {
|
2013-07-10 00:45:36 +00:00
|
|
|
sendbuf_size_ = option;
|
2014-07-29 17:36:52 +00:00
|
|
|
} else if (opt == rtc::Socket::OPT_RCVBUF) {
|
2013-07-10 00:45:36 +00:00
|
|
|
recvbuf_size_ = option;
|
2014-07-29 17:36:52 +00:00
|
|
|
} else if (opt == rtc::Socket::OPT_DSCP) {
|
|
|
|
|
dscp_ = static_cast<rtc::DiffServCodePoint>(option);
|
2013-07-10 00:45:36 +00:00
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-17 10:24:02 +02:00
|
|
|
void PostPacket(rtc::CopyOnWriteBuffer packet) {
|
|
|
|
|
thread_->PostTask(
|
|
|
|
|
SafeTask(safety_.flag(), [this, packet = std::move(packet)]() mutable {
|
|
|
|
|
if (dest_) {
|
2023-01-10 14:28:25 +01:00
|
|
|
webrtc::RtpPacketReceived parsed_packet;
|
|
|
|
|
if (parsed_packet.Parse(packet)) {
|
|
|
|
|
parsed_packet.set_arrival_time(
|
|
|
|
|
webrtc::Timestamp::Micros(rtc::TimeMicros()));
|
|
|
|
|
dest_->OnPacketReceived(std::move(parsed_packet));
|
|
|
|
|
} else {
|
|
|
|
|
RTC_DCHECK_NOTREACHED();
|
|
|
|
|
}
|
2022-08-17 10:24:02 +02:00
|
|
|
}
|
|
|
|
|
}));
|
2013-07-10 00:45:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
2021-07-09 15:46:14 +00:00
|
|
|
void SetRtpSsrc(uint32_t ssrc, rtc::CopyOnWriteBuffer& buffer) {
|
|
|
|
|
RTC_CHECK_GE(buffer.size(), 12);
|
|
|
|
|
rtc::SetBE32(buffer.MutableData() + 8, ssrc);
|
|
|
|
|
}
|
|
|
|
|
|
Use suffixed {uint,int}{8,16,32,64}_t types.
Removes the use of uint8, etc. in favor of uint8_t.
BUG=webrtc:5024
R=henrik.lundin@webrtc.org, henrikg@webrtc.org, perkj@webrtc.org, solenberg@webrtc.org, stefan@webrtc.org, tina.legrand@webrtc.org
Review URL: https://codereview.webrtc.org/1362503003 .
Cr-Commit-Position: refs/heads/master@{#10196}
2015-10-07 12:23:21 +02:00
|
|
|
void GetNumRtpBytesAndPackets(uint32_t ssrc, int* bytes, int* packets) {
|
2013-07-10 00:45:36 +00:00
|
|
|
if (bytes) {
|
|
|
|
|
*bytes = 0;
|
|
|
|
|
}
|
|
|
|
|
if (packets) {
|
|
|
|
|
*packets = 0;
|
|
|
|
|
}
|
|
|
|
|
for (size_t i = 0; i < rtp_packets_.size(); ++i) {
|
2021-07-19 13:20:46 +00:00
|
|
|
if (ssrc == webrtc::ParseRtpSsrc(rtp_packets_[i])) {
|
2013-07-10 00:45:36 +00:00
|
|
|
if (bytes) {
|
2015-03-24 09:19:06 +00:00
|
|
|
*bytes += static_cast<int>(rtp_packets_[i].size());
|
2013-07-10 00:45:36 +00:00
|
|
|
}
|
|
|
|
|
if (packets) {
|
|
|
|
|
++(*packets);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-17 10:24:02 +02:00
|
|
|
webrtc::TaskQueueBase* thread_;
|
2023-06-20 18:30:03 +00:00
|
|
|
MediaReceiveChannelInterface* dest_;
|
2013-07-10 00:45:36 +00:00
|
|
|
bool conf_;
|
|
|
|
|
// The ssrcs used in sending out packets in conference mode.
|
Use suffixed {uint,int}{8,16,32,64}_t types.
Removes the use of uint8, etc. in favor of uint8_t.
BUG=webrtc:5024
R=henrik.lundin@webrtc.org, henrikg@webrtc.org, perkj@webrtc.org, solenberg@webrtc.org, stefan@webrtc.org, tina.legrand@webrtc.org
Review URL: https://codereview.webrtc.org/1362503003 .
Cr-Commit-Position: refs/heads/master@{#10196}
2015-10-07 12:23:21 +02:00
|
|
|
std::vector<uint32_t> conf_sent_ssrcs_;
|
2013-07-10 00:45:36 +00:00
|
|
|
// Map to track counts of packets that have been sent per ssrc.
|
|
|
|
|
// This includes packets that are dropped.
|
Use suffixed {uint,int}{8,16,32,64}_t types.
Removes the use of uint8, etc. in favor of uint8_t.
BUG=webrtc:5024
R=henrik.lundin@webrtc.org, henrikg@webrtc.org, perkj@webrtc.org, solenberg@webrtc.org, stefan@webrtc.org, tina.legrand@webrtc.org
Review URL: https://codereview.webrtc.org/1362503003 .
Cr-Commit-Position: refs/heads/master@{#10196}
2015-10-07 12:23:21 +02:00
|
|
|
std::map<uint32_t, uint32_t> sent_ssrcs_;
|
2013-07-10 00:45:36 +00:00
|
|
|
// Map to track packet-number that needs to be dropped per ssrc.
|
Use suffixed {uint,int}{8,16,32,64}_t types.
Removes the use of uint8, etc. in favor of uint8_t.
BUG=webrtc:5024
R=henrik.lundin@webrtc.org, henrikg@webrtc.org, perkj@webrtc.org, solenberg@webrtc.org, stefan@webrtc.org, tina.legrand@webrtc.org
Review URL: https://codereview.webrtc.org/1362503003 .
Cr-Commit-Position: refs/heads/master@{#10196}
2015-10-07 12:23:21 +02:00
|
|
|
std::map<uint32_t, std::set<uint32_t> > drop_map_;
|
2020-07-07 15:43:11 +02:00
|
|
|
webrtc::Mutex mutex_;
|
2016-03-20 06:15:43 -07:00
|
|
|
std::vector<rtc::CopyOnWriteBuffer> rtp_packets_;
|
|
|
|
|
std::vector<rtc::CopyOnWriteBuffer> rtcp_packets_;
|
2013-07-10 00:45:36 +00:00
|
|
|
int sendbuf_size_;
|
|
|
|
|
int recvbuf_size_;
|
2014-07-29 17:36:52 +00:00
|
|
|
rtc::DiffServCodePoint dscp_;
|
2018-09-17 17:06:08 -07:00
|
|
|
// Options of the most recently sent packet.
|
|
|
|
|
rtc::PacketOptions options_;
|
2022-08-17 10:24:02 +02:00
|
|
|
webrtc::ScopedTaskSafety safety_;
|
2013-07-10 00:45:36 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // namespace cricket
|
|
|
|
|
|
2019-01-11 09:11:00 -08:00
|
|
|
#endif // MEDIA_BASE_FAKE_NETWORK_INTERFACE_H_
|