1 // Copyright 2019 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef CAST_STREAMING_RECEIVER_PACKET_ROUTER_H_
6 #define CAST_STREAMING_RECEIVER_PACKET_ROUTER_H_
7 
8 #include <stdint.h>
9 
10 #include <utility>
11 #include <vector>
12 
13 #include "absl/types/span.h"
14 #include "cast/streaming/environment.h"
15 #include "cast/streaming/ssrc.h"
16 #include "util/flat_map.h"
17 
18 namespace openscreen {
19 namespace cast {
20 
21 class Receiver;
22 
23 // Handles all network I/O among multiple Receivers meant for synchronized
24 // play-out (e.g., one Receiver for audio, one Receiver for video). Incoming
25 // traffic is dispatched to the appropriate Receiver, based on its corresponding
26 // sender's SSRC. Also, all traffic not coming from the same source is
27 // filtered-out.
28 class ReceiverPacketRouter final : public Environment::PacketConsumer {
29  public:
30   explicit ReceiverPacketRouter(Environment* environment);
31   ~ReceiverPacketRouter() final;
32 
33  protected:
34   friend class Receiver;
35 
36   // Called from a Receiver constructor/destructor to register/deregister a
37   // Receiver instance that processes RTP/RTCP packets from a Sender having the
38   // given SSRC.
39   void OnReceiverCreated(Ssrc sender_ssrc, Receiver* receiver);
40   void OnReceiverDestroyed(Ssrc sender_ssrc);
41 
42   // Called by a Receiver to send a RTCP packet back to the source from which
43   // earlier packets were received, or does nothing if OnReceivedPacket() has
44   // not been called yet.
45   void SendRtcpPacket(absl::Span<const uint8_t> packet);
46 
47  private:
48   // Environment::PacketConsumer implementation.
49   void OnReceivedPacket(const IPEndpoint& source,
50                         Clock::time_point arrival_time,
51                         std::vector<uint8_t> packet) final;
52 
53   Environment* const environment_;
54 
55   FlatMap<Ssrc, Receiver*> receivers_;
56 };
57 
58 }  // namespace cast
59 }  // namespace openscreen
60 
61 #endif  // CAST_STREAMING_RECEIVER_PACKET_ROUTER_H_
62