Fixed whitespace in all files (no tabs)
[senf.git] / Packets / RTPPacket.hh
1 // $Id$
2 //
3 // Copyright (C) 2006
4 // Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
5 // Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
6 //     Stefan Bund <stefan.bund@fokus.fraunhofer.de>
7 //
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.b
12 //
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the
20 // Free Software Foundation, Inc.,
21 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
23 #ifndef HH_RTPPacket_
24 #define HH_RTPPacket_ 1
25
26 // Custom includes
27 #include "Packet.hh"
28 #include "ParseInt.hh"
29 #include "ParseArray.hh"
30 #include "ParseVec.hh"
31 #include "PacketRegistry.hh"
32
33 //#include "RTPPacket.mpp"
34 ///////////////////////////////hh.p////////////////////////////////////////
35
36 namespace senf {
37
38
39     template <class Iterator=nil, class IPacket=nil>
40     struct Parse_RTP : public ParserBase<Iterator,IPacket>
41     {
42         template <class I, class P=nil>
43         struct rebind { typedef Parse_RTP<I,P> parser; };
44         typedef Iterator byte_iterator;
45
46         Parse_RTP() {}
47         Parse_RTP(Iterator const & i) : ParserBase<Iterator,IPacket>(i) {}
48
49         ///////////////////////////////////////////////////////////////////////////
50
51         typedef Parse_UIntField  < 0, 2, Iterator >    Parse_Version;
52         typedef Parse_Flag       < 2, Iterator >       Parse_P;
53         typedef Parse_Flag       < 3, Iterator >       Parse_X;
54         typedef Parse_UIntField  < 4, 8, Iterator >    Parse_CC;
55         typedef Parse_Flag       < 0, Iterator >       Parse_M;
56         typedef Parse_UIntField  < 1, 8, Iterator >    Parse_PT;
57         typedef Parse_UInt16     < Iterator >          Parse_Seq;
58         typedef Parse_UInt32     < Iterator >          Parse_32bit;
59         typedef Parse_Vector     < Parse_32bit, Parse_CC, Iterator > Parse_CCVec;
60
61         Parse_Version  version()      const { return Parse_Version  (this->i()      ); }
62         Parse_P        padding()      const { return Parse_P        (this->i()      ); }
63         Parse_X        extension()    const { return Parse_X        (this->i()      ); }
64         Parse_CC       csrcCount()    const { return Parse_CC       (this->i()      ); }
65         Parse_M        marker()       const { return Parse_M        (this->i() + 1  ); }
66         Parse_PT       payloadType()  const { return Parse_PT       (this->i() + 1  ); }
67         Parse_Seq      seqNumber()    const { return Parse_Seq      (this->i() + 2  ); }
68         Parse_32bit    timestamp()    const { return Parse_32bit    (this->i() + 4  ); }
69         Parse_32bit    ssrc()         const { return Parse_32bit    (this->i() + 8  ); }
70         Parse_CCVec    csrcList()     const { return Parse_CCVec (csrcCount(), this->i() + 12 ); }
71
72
73
74         ///////////////////////////////////////////////////////////////////////////
75
76         unsigned int bytes() const { return 12 + ( 4 * csrcCount()); }
77         static bool check(Iterator const & b, Iterator const & e)
78         { return e-b>= 12 and unsigned(e-b) >= Parse_RTP<Iterator>(b).bytes(); }
79
80     };
81
82     struct RTPTypes {
83         typedef boost::uint16_t key_t;
84     };
85
86     class RTPPacket
87         : public Packet,
88           public Parse_RTP<Packet::iterator, RTPPacket>,
89           public PacketRegistryMixin<RTPTypes,RTPPacket>
90     {
91         using Packet::registerInterpreter;
92         using PacketRegistryMixin<RTPTypes,RTPPacket>::registerInterpreter;
93     public:
94         ///////////////////////////////////////////////////////////////////////////
95         // Types
96
97         typedef ptr_t<RTPPacket>::ptr ptr;
98
99         ///////////////////////////////////////////////////////////////////////////
100
101         typedef Parse_UInt8 < Packet::iterator >  Parse_paddingOctet;
102
103         Parse_paddingOctet paddingOctet() const {
104             return Parse_paddingOctet( end() -1 );
105         }
106
107     private:
108         template <class Arg>
109         RTPPacket(Arg const & arg);
110
111         virtual void v_nextInterpreter() const;
112         virtual void v_finalize();
113         virtual void v_dump(std::ostream & os) const;
114
115         friend class Packet;
116     };
117
118
119
120     template <class Iterator=nil, class IPacket=nil>
121     struct Parse_RTPExtensionBase : public ParserBase<Iterator,IPacket>
122     {
123         template <class I, class P=nil>
124         struct rebind { typedef Parse_RTPExtensionBase<I,P> parser; };
125         typedef Iterator byte_iterator;
126
127         Parse_RTPExtensionBase() {}
128         Parse_RTPExtensionBase(Iterator const & i) : ParserBase<Iterator,IPacket>(i) {}
129
130         ///////////////////////////////////////////////////////////////////////////
131
132         typedef Parse_UInt16    < Iterator >        Parse_16bit;
133
134         Parse_16bit proDef() const { return Parse_16bit(this->i()); };
135         Parse_16bit length() const { return Parse_16bit(this->i()+2); };
136
137         unsigned int bytes() const { return 4 + length(); }
138         static bool check(Iterator const & b, Iterator const & e)
139         { return e-b>=4 && unsigned(e-b) >= Parse_RTPExtensionBase<Iterator>(b).bytes(); }
140
141
142     };
143
144     class RTPExtensionBasePacket
145         : public Packet,
146           public PacketRegistryMixin<RTPTypes, RTPExtensionBasePacket>
147     {
148         using PacketRegistryMixin<RTPTypes,RTPExtensionBasePacket>::registerInterpreter;
149         using Packet::registerInterpreter;
150     public:
151          ///////////////////////////////////////////////////////////////////////////
152          typedef ptr_t<RTPExtensionBasePacket>::ptr ptr;
153
154     protected:
155         template <class Arg>
156         RTPExtensionBasePacket(Arg const & arg);
157
158     private:
159         virtual void v_nextInterpreter() const;
160         virtual void v_finalize() = 0;
161         virtual void v_dump(std::ostream & os) const;
162
163         friend class Packet;
164
165     };
166
167
168     template <class Iterator=nil, class IPacket=nil>
169     struct Parse_RTPUnknownExtension : public Parse_RTPExtensionBase<Iterator,IPacket>
170     {
171         template <class I, class P=nil>
172         struct rebind { typedef Parse_RTPUnknownExtension<I,P> parser; };
173         typedef Iterator byte_iterator;
174
175         Parse_RTPUnknownExtension() {}
176         Parse_RTPUnknownExtension(Iterator const & i) : ParserBase<Iterator,IPacket>(i) {}
177
178         ///////////////////////////////////////////////////////////////////////////
179
180         typedef Parse_UInt16 < Iterator >                          Parse_16bit;
181         typedef Parse_UInt8  < Iterator >                          Parse_8bit;
182         typedef Parse_Vector < Parse_8bit, Parse_16bit, Iterator > Parse_ext;
183
184         Parse_ext ext() const { return Parse_ext (this->length(), this->i() + 4 ); }
185
186     };
187
188     class RTPUnknownExtensionPacket
189         : public RTPExtensionBasePacket,
190           public Parse_RTPUnknownExtension<Packet::iterator, RTPUnknownExtensionPacket>
191     {
192     public:
193         ///////////////////////////////////////////////////////////////////////////
194         // Types
195
196         typedef ptr_t<RTPUnknownExtensionPacket>::ptr ptr;
197
198         ///////////////////////////////////////////////////////////////////////////
199
200     private:
201         template <class Arg>
202         RTPUnknownExtensionPacket(Arg const & arg);
203
204         //virtual void v_nextInterpreter() const;
205         virtual void v_finalize();
206
207         friend class RTPExtensionBasePacket;
208         friend class Packet;
209     };
210
211 }
212
213
214 ///////////////////////////////hh.e////////////////////////////////////////
215 //#include RTPPacket.cci"
216 //#include "RTPPacket.ct"
217 #include "RTPPacket.cti"
218 #endif
219
220 \f
221 // Local Variables:
222 // mode: c++
223 // fill-column: 100
224 // c-file-style: "senf"
225 // indent-tabs-mode: nil
226 // ispell-local-dictionary: "american"
227 // End: