• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_RTP_PACKETIZER_H_
6 #define CAST_STREAMING_RTP_PACKETIZER_H_
7 
8 #include <stdint.h>
9 
10 #include "absl/types/span.h"
11 #include "cast/streaming/frame_crypto.h"
12 #include "cast/streaming/rtp_defines.h"
13 #include "cast/streaming/ssrc.h"
14 
15 namespace openscreen {
16 namespace cast {
17 
18 // Transforms a logical sequence of EncryptedFrames into RTP packets for
19 // transmission. A single instance of RtpPacketizer should be used for all the
20 // frames in a Cast RTP stream having the same SSRC.
21 class RtpPacketizer {
22  public:
23   // |payload_type| describes the type of the media content for the RTP stream
24   // from the sender having the given |sender_ssrc|.
25   //
26   // The |max_packet_size| argument depends on the optimal over-the-wire size of
27   // packets for the network medium being used. See discussion in rtp_defines.h
28   // for further info.
29   RtpPacketizer(RtpPayloadType payload_type,
30                 Ssrc sender_ssrc,
31                 int max_packet_size);
32 
33   ~RtpPacketizer();
34 
35   // Wire-format one of the RTP packets for the given frame, which must only be
36   // transmitted once. This method should be called in the same sequence that
37   // packets will be transmitted. This also means that, if a packet needs to be
38   // re-transmitted, this method should be called to generate it again. Returns
39   // the subspan of |buffer| that contains the packet. |buffer| must be at least
40   // as large as the |max_packet_size| passed to the constructor.
41   absl::Span<uint8_t> GeneratePacket(const EncryptedFrame& frame,
42                                      FramePacketId packet_id,
43                                      absl::Span<uint8_t> buffer);
44 
45   // Given |frame|, compute the total number of packets over which the whole
46   // frame will be split-up. Returns -1 if the frame is too large and cannot be
47   // packetized.
48   int ComputeNumberOfPackets(const EncryptedFrame& frame) const;
49 
50   // See rtp_defines.h for wire-format diagram.
51   static constexpr int kBaseRtpHeaderSize =
52       // Plus one byte, because this implementation always includes the 8-bit
53       // Reference Frame ID field.
54       kRtpPacketMinValidSize + 1;
55   static constexpr int kAdaptiveLatencyHeaderSize = 4;
56   static constexpr int kMaxRtpHeaderSize =
57       kBaseRtpHeaderSize + kAdaptiveLatencyHeaderSize;
58 
59  private:
max_payload_size()60   int max_payload_size() const {
61     // Start with the configured max packet size, then subtract reserved space
62     // for packet header fields. The rest can be allocated to the payload.
63     return max_packet_size_ - kMaxRtpHeaderSize;
64   }
65 
66   // The validated ctor RtpPayloadType arg, in wire-format form.
67   const uint8_t payload_type_7bits_;
68 
69   const Ssrc sender_ssrc_;
70   const int max_packet_size_;
71 
72   // Incremented each time GeneratePacket() is called. Every packet, even those
73   // re-transmitted, must have different sequence numbers (within wrap-around
74   // concerns) per the RTP spec.
75   uint16_t sequence_number_;
76 };
77 
78 }  // namespace cast
79 }  // namespace openscreen
80 
81 #endif  // CAST_STREAMING_RTP_PACKETIZER_H_
82