1 /*
2 
3   This file is a part of JRTPLIB
4   Copyright (c) 1999-2017 Jori Liesenborgs
5 
6   Contact: jori.liesenborgs@gmail.com
7 
8   This library was developed at the Expertise Centre for Digital Media
9   (http://www.edm.uhasselt.be), a research center of the Hasselt University
10   (http://www.uhasselt.be). The library is based upon work done for
11   my thesis at the School for Knowledge Technology (Belgium/The Netherlands).
12 
13   Permission is hereby granted, free of charge, to any person obtaining a
14   copy of this software and associated documentation files (the "Software"),
15   to deal in the Software without restriction, including without limitation
16   the rights to use, copy, modify, merge, publish, distribute, sublicense,
17   and/or sell copies of the Software, and to permit persons to whom the
18   Software is furnished to do so, subject to the following conditions:
19 
20   The above copyright notice and this permission notice shall be included
21   in all copies or substantial portions of the Software.
22 
23   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
24   OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
26   THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28   FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
29   IN THE SOFTWARE.
30 
31 */
32 
33 /**
34  * \file rtcpbyepacket.h
35  */
36 
37 #ifndef RTCPBYEPACKET_H
38 
39 #define RTCPBYEPACKET_H
40 
41 #include "rtpconfig.h"
42 #include "rtcppacket.h"
43 #include "rtpstructs.h"
44 #ifdef RTP_SUPPORT_NETINET_IN
45 	#include <netinet/in.h>
46 #endif // RTP_SUPPORT_NETINET_IN
47 
48 namespace jrtplib
49 {
50 
51 class RTCPCompoundPacket;
52 
53 /** Describes an RTCP BYE packet. */
54 class JRTPLIB_IMPORTEXPORT RTCPBYEPacket : public RTCPPacket
55 {
56 public:
57 	/** Creates an instance based on the data in \c data with length \c datalen.
58 	 *  Creates an instance based on the data in \c data with length \c datalen. Since the \c data pointer
59 	 *  is referenced inside the class (no copy of the data is made) one must make sure that the memory it
60 	 *  points to is valid as long as the class instance exists.
61 	 */
62 	RTCPBYEPacket(uint8_t *data,size_t datalen);
~RTCPBYEPacket()63 	~RTCPBYEPacket()							{ }
64 
65 	/** Returns the number of SSRC identifiers present in this BYE packet. */
66 	int GetSSRCCount() const;
67 
68 	/** Returns the SSRC described by \c index which may have a value from 0 to GetSSRCCount()-1
69 	 *  (note that no check is performed to see if \c index is valid).
70 	 */
71 	uint32_t GetSSRC(int index) const; // note: no check is performed to see if index is valid!
72 
73 	/** Returns true if the BYE packet contains a reason for leaving. */
74 	bool HasReasonForLeaving() const;
75 
76 	/** Returns the length of the string which describes why the source(s) left. */
77 	size_t GetReasonLength() const;
78 
79 	/** Returns the actual reason for leaving data. */
80 	uint8_t *GetReasonData();
81 
82 #ifdef RTPDEBUG
83 	void Dump();
84 #endif // RTPDEBUG
85 private:
86 	size_t reasonoffset;
87 };
88 
GetSSRCCount()89 inline int RTCPBYEPacket::GetSSRCCount() const
90 {
91 	if (!knownformat)
92 		return 0;
93 
94 	RTCPCommonHeader *hdr = (RTCPCommonHeader *)data;
95 	return (int)(hdr->count);
96 }
97 
GetSSRC(int index)98 inline uint32_t RTCPBYEPacket::GetSSRC(int index) const
99 {
100 	if (!knownformat)
101 		return 0;
102 	uint32_t *ssrc = (uint32_t *)(data+sizeof(RTCPCommonHeader)+sizeof(uint32_t)*index);
103 	return ntohl(*ssrc);
104 }
105 
HasReasonForLeaving()106 inline bool RTCPBYEPacket::HasReasonForLeaving() const
107 {
108 	if (!knownformat)
109 		return false;
110 	if (reasonoffset == 0)
111 		return false;
112 	return true;
113 }
114 
GetReasonLength()115 inline size_t RTCPBYEPacket::GetReasonLength() const
116 {
117 	if (!knownformat)
118 		return 0;
119 	if (reasonoffset == 0)
120 		return 0;
121 	uint8_t *reasonlen = (data+reasonoffset);
122 	return (size_t)(*reasonlen);
123 }
124 
GetReasonData()125 inline uint8_t *RTCPBYEPacket::GetReasonData()
126 {
127 	if (!knownformat)
128 		return 0;
129 	if (reasonoffset == 0)
130 		return 0;
131 	uint8_t *reasonlen = (data+reasonoffset);
132 	if ((*reasonlen) == 0)
133 		return 0;
134 	return (data+reasonoffset+1);
135 }
136 
137 } // end namespace
138 
139 #endif // RTCPBYEPACKET_H
140 
141