2012-10-03 00:04:58 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=2 et sw=2 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
// Original author: ekr@rtfm.com
|
|
|
|
|
|
|
|
#ifndef transportlayerloopback_h__
|
|
|
|
#define transportlayerloopback_h__
|
|
|
|
|
|
|
|
#include "nspr.h"
|
|
|
|
#include "prio.h"
|
|
|
|
#include "prlock.h"
|
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
#include <queue>
|
|
|
|
|
|
|
|
#include "nsCOMPtr.h"
|
2017-07-26 21:18:20 +03:00
|
|
|
#include "nsINamed.h"
|
2012-10-03 00:04:58 +04:00
|
|
|
#include "nsITimer.h"
|
|
|
|
|
|
|
|
#include "m_cpp_utils.h"
|
|
|
|
#include "transportflow.h"
|
|
|
|
#include "transportlayer.h"
|
|
|
|
|
|
|
|
// A simple loopback transport layer that is used for testing.
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
class TransportLayerLoopback : public TransportLayer {
|
|
|
|
public:
|
|
|
|
TransportLayerLoopback()
|
|
|
|
: peer_(nullptr),
|
|
|
|
timer_(nullptr),
|
|
|
|
packets_(),
|
|
|
|
packets_lock_(nullptr),
|
2015-11-04 04:21:35 +03:00
|
|
|
deliverer_(nullptr),
|
|
|
|
combinePackets_(false) {}
|
2012-10-03 00:04:58 +04:00
|
|
|
|
|
|
|
~TransportLayerLoopback() {
|
|
|
|
while (!packets_.empty()) {
|
2018-05-10 01:13:35 +03:00
|
|
|
MediaPacket* packet = packets_.front();
|
2012-10-03 00:04:58 +04:00
|
|
|
packets_.pop();
|
|
|
|
delete packet;
|
|
|
|
}
|
|
|
|
if (packets_lock_) {
|
|
|
|
PR_DestroyLock(packets_lock_);
|
|
|
|
}
|
|
|
|
timer_->Cancel();
|
|
|
|
deliverer_->Detach();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Init
|
|
|
|
nsresult Init();
|
|
|
|
|
|
|
|
// Connect to the other side
|
|
|
|
void Connect(TransportLayerLoopback* peer);
|
|
|
|
|
|
|
|
// Disconnect
|
|
|
|
void Disconnect() {
|
|
|
|
TransportLayerLoopback* peer = peer_;
|
|
|
|
|
|
|
|
peer_ = nullptr;
|
|
|
|
if (peer) {
|
|
|
|
peer->Disconnect();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-04 04:21:35 +03:00
|
|
|
void CombinePackets(bool combine) { combinePackets_ = combine; }
|
|
|
|
|
2012-10-03 00:04:58 +04:00
|
|
|
// Overrides for TransportLayer
|
2018-05-10 01:13:35 +03:00
|
|
|
TransportResult SendPacket(MediaPacket& packet) override;
|
2012-10-03 00:04:58 +04:00
|
|
|
|
|
|
|
// Deliver queued packets
|
|
|
|
void DeliverPackets();
|
|
|
|
|
|
|
|
TRANSPORT_LAYER_ID("loopback")
|
|
|
|
|
|
|
|
private:
|
|
|
|
DISALLOW_COPY_ASSIGN(TransportLayerLoopback);
|
|
|
|
|
|
|
|
// A timer to deliver packets if some are available
|
|
|
|
// Fires every 100 ms
|
2017-07-26 21:18:20 +03:00
|
|
|
class Deliverer : public nsITimerCallback, public nsINamed {
|
2012-10-03 00:04:58 +04:00
|
|
|
public:
|
2014-08-16 01:41:29 +04:00
|
|
|
explicit Deliverer(TransportLayerLoopback* layer) : layer_(layer) {}
|
2012-10-03 00:04:58 +04:00
|
|
|
void Detach() { layer_ = nullptr; }
|
|
|
|
|
2013-07-19 06:23:32 +04:00
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
2012-10-03 00:04:58 +04:00
|
|
|
NS_DECL_NSITIMERCALLBACK
|
2017-07-26 21:18:20 +03:00
|
|
|
NS_DECL_NSINAMED
|
2012-10-03 00:04:58 +04:00
|
|
|
|
|
|
|
private:
|
2020-03-06 12:21:47 +03:00
|
|
|
virtual ~Deliverer() = default;
|
2014-06-24 20:36:44 +04:00
|
|
|
|
2012-10-03 00:04:58 +04:00
|
|
|
DISALLOW_COPY_ASSIGN(Deliverer);
|
|
|
|
|
|
|
|
TransportLayerLoopback* layer_;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Queue a packet for delivery
|
2018-05-10 01:13:35 +03:00
|
|
|
nsresult QueuePacket(MediaPacket& packet);
|
2012-10-03 00:04:58 +04:00
|
|
|
|
|
|
|
TransportLayerLoopback* peer_;
|
|
|
|
nsCOMPtr<nsITimer> timer_;
|
2018-05-10 01:13:35 +03:00
|
|
|
std::queue<MediaPacket*> packets_;
|
2012-10-03 00:04:58 +04:00
|
|
|
PRLock* packets_lock_;
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Deliverer> deliverer_;
|
2015-11-04 04:21:35 +03:00
|
|
|
bool combinePackets_;
|
2012-10-03 00:04:58 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace mozilla
|
|
|
|
#endif
|