Line data Source code
1 : /*
2 : * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
3 : *
4 : * Use of this source code is governed by a BSD-style license
5 : * that can be found in the LICENSE file in the root of the source
6 : * tree. An additional intellectual property rights grant can be found
7 : * in the file PATENTS. All contributing project authors may
8 : * be found in the AUTHORS file in the root of the source tree.
9 : *
10 : */
11 : #ifndef WEBRTC_MODULES_RTP_RTCP_SOURCE_RTCP_PACKET_H_
12 : #define WEBRTC_MODULES_RTP_RTCP_SOURCE_RTCP_PACKET_H_
13 :
14 : #include "webrtc/base/basictypes.h"
15 : #include "webrtc/base/buffer.h"
16 :
17 : namespace webrtc {
18 : namespace rtcp {
19 : // Class for building RTCP packets.
20 : //
21 : // Example:
22 : // ReportBlock report_block;
23 : // report_block.SetMediaSsrc(234);
24 : // report_block.SetFractionLost(10);
25 : //
26 : // ReceiverReport rr;
27 : // rr.SetSenderSsrc(123);
28 : // rr.AddReportBlock(report_block);
29 : //
30 : // Fir fir;
31 : // fir.SetSenderSsrc(123);
32 : // fir.AddRequestTo(234, 56);
33 : //
34 : // size_t length = 0; // Builds an intra frame request
35 : // uint8_t packet[kPacketSize]; // with sequence number 56.
36 : // fir.Build(packet, &length, kPacketSize);
37 : //
38 : // rtc::Buffer packet = fir.Build(); // Returns a RawPacket holding
39 : // // the built rtcp packet.
40 : //
41 : // CompoundPacket compound; // Builds a compound RTCP packet with
42 : // compound.Append(&rr); // a receiver report, report block
43 : // compound.Append(&fir); // and fir message.
44 : // rtc::Buffer packet = compound.Build();
45 :
46 : class RtcpPacket {
47 : public:
48 : // Callback used to signal that an RTCP packet is ready. Note that this may
49 : // not contain all data in this RtcpPacket; if a packet cannot fit in
50 : // max_length bytes, it will be fragmented and multiple calls to this
51 : // callback will be made.
52 : class PacketReadyCallback {
53 : public:
54 : virtual void OnPacketReady(uint8_t* data, size_t length) = 0;
55 :
56 : protected:
57 0 : PacketReadyCallback() {}
58 0 : virtual ~PacketReadyCallback() {}
59 : };
60 :
61 0 : virtual ~RtcpPacket() {}
62 :
63 : // Convenience method mostly used for test. Creates packet without
64 : // fragmentation using BlockLength() to allocate big enough buffer.
65 : rtc::Buffer Build() const;
66 :
67 : // Returns true if call to Create succeeded. Provided buffer reference
68 : // will be used for all calls to callback.
69 : bool BuildExternalBuffer(uint8_t* buffer,
70 : size_t max_length,
71 : PacketReadyCallback* callback) const;
72 :
73 : // Size of this packet in bytes (including headers).
74 : virtual size_t BlockLength() const = 0;
75 :
76 : // Creates packet in the given buffer at the given position.
77 : // Calls PacketReadyCallback::OnPacketReady if remaining buffer is too small
78 : // and assume buffer can be reused after OnPacketReady returns.
79 : virtual bool Create(uint8_t* packet,
80 : size_t* index,
81 : size_t max_length,
82 : PacketReadyCallback* callback) const = 0;
83 :
84 : protected:
85 : // Size of the rtcp common header.
86 : static constexpr size_t kHeaderLength = 4;
87 0 : RtcpPacket() {}
88 :
89 : static void CreateHeader(uint8_t count_or_format,
90 : uint8_t packet_type,
91 : size_t block_length, // Payload size in 32bit words.
92 : uint8_t* buffer,
93 : size_t* pos);
94 :
95 : bool OnBufferFull(uint8_t* packet,
96 : size_t* index,
97 : PacketReadyCallback* callback) const;
98 : // Size of the rtcp packet as written in header.
99 : size_t HeaderLength() const;
100 : };
101 : } // namespace rtcp
102 : } // namespace webrtc
103 : #endif // WEBRTC_MODULES_RTP_RTCP_SOURCE_RTCP_PACKET_H_
|