/src/mozilla-central/media/mtransport/transportlayerloopback.h
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
2 | | /* vim: set ts=2 et sw=2 tw=80: */ |
3 | | /* This Source Code Form is subject to the terms of the Mozilla Public |
4 | | * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
5 | | * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
6 | | |
7 | | // Original author: ekr@rtfm.com |
8 | | |
9 | | #ifndef transportlayerloopback_h__ |
10 | | #define transportlayerloopback_h__ |
11 | | |
12 | | #include "nspr.h" |
13 | | #include "prio.h" |
14 | | #include "prlock.h" |
15 | | |
16 | | #include <memory> |
17 | | #include <queue> |
18 | | |
19 | | |
20 | | #include "nsAutoPtr.h" |
21 | | #include "nsCOMPtr.h" |
22 | | #include "nsINamed.h" |
23 | | #include "nsITimer.h" |
24 | | |
25 | | |
26 | | #include "m_cpp_utils.h" |
27 | | #include "transportflow.h" |
28 | | #include "transportlayer.h" |
29 | | |
30 | | // A simple loopback transport layer that is used for testing. |
31 | | namespace mozilla { |
32 | | |
33 | | class TransportLayerLoopback : public TransportLayer { |
34 | | public: |
35 | | TransportLayerLoopback() : |
36 | | peer_(nullptr), |
37 | | timer_(nullptr), |
38 | | packets_(), |
39 | | packets_lock_(nullptr), |
40 | | deliverer_(nullptr), |
41 | 0 | combinePackets_(false) {} |
42 | | |
43 | 0 | ~TransportLayerLoopback() { |
44 | 0 | while (!packets_.empty()) { |
45 | 0 | MediaPacket *packet = packets_.front(); |
46 | 0 | packets_.pop(); |
47 | 0 | delete packet; |
48 | 0 | } |
49 | 0 | if (packets_lock_) { |
50 | 0 | PR_DestroyLock(packets_lock_); |
51 | 0 | } |
52 | 0 | timer_->Cancel(); |
53 | 0 | deliverer_->Detach(); |
54 | 0 | } |
55 | | |
56 | | // Init |
57 | | nsresult Init(); |
58 | | |
59 | | // Connect to the other side |
60 | | void Connect(TransportLayerLoopback* peer); |
61 | | |
62 | | // Disconnect |
63 | 0 | void Disconnect() { |
64 | 0 | TransportLayerLoopback *peer = peer_; |
65 | 0 |
|
66 | 0 | peer_ = nullptr; |
67 | 0 | if (peer) { |
68 | 0 | peer->Disconnect(); |
69 | 0 | } |
70 | 0 | } |
71 | | |
72 | 0 | void CombinePackets(bool combine) { combinePackets_ = combine; } |
73 | | |
74 | | // Overrides for TransportLayer |
75 | | TransportResult SendPacket(MediaPacket& packet) override; |
76 | | |
77 | | // Deliver queued packets |
78 | | void DeliverPackets(); |
79 | | |
80 | | TRANSPORT_LAYER_ID("loopback") |
81 | | |
82 | | private: |
83 | | DISALLOW_COPY_ASSIGN(TransportLayerLoopback); |
84 | | |
85 | | // A timer to deliver packets if some are available |
86 | | // Fires every 100 ms |
87 | | class Deliverer : public nsITimerCallback |
88 | | , public nsINamed { |
89 | | public: |
90 | | explicit Deliverer(TransportLayerLoopback *layer) : |
91 | 0 | layer_(layer) {} |
92 | 0 | void Detach() { |
93 | 0 | layer_ = nullptr; |
94 | 0 | } |
95 | | |
96 | | NS_DECL_THREADSAFE_ISUPPORTS |
97 | | NS_DECL_NSITIMERCALLBACK |
98 | | NS_DECL_NSINAMED |
99 | | |
100 | | private: |
101 | 0 | virtual ~Deliverer() { |
102 | 0 | } |
103 | | |
104 | | DISALLOW_COPY_ASSIGN(Deliverer); |
105 | | |
106 | | TransportLayerLoopback *layer_; |
107 | | }; |
108 | | |
109 | | // Queue a packet for delivery |
110 | | nsresult QueuePacket(MediaPacket& packet); |
111 | | |
112 | | TransportLayerLoopback* peer_; |
113 | | nsCOMPtr<nsITimer> timer_; |
114 | | std::queue<MediaPacket *> packets_; |
115 | | PRLock *packets_lock_; |
116 | | RefPtr<Deliverer> deliverer_; |
117 | | bool combinePackets_; |
118 | | }; |
119 | | |
120 | | } // close namespace |
121 | | #endif |