FFmpeg
rtmppkt.h
Go to the documentation of this file.
1 /*
2  * RTMP packet utilities
3  * Copyright (c) 2009 Konstantin Shishkov
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 #ifndef AVFORMAT_RTMPPKT_H
23 #define AVFORMAT_RTMPPKT_H
24 
25 #include "libavcodec/bytestream.h"
26 #include "avformat.h"
27 #include "url.h"
28 
29 /** maximum possible number of different RTMP channels */
30 #define RTMP_CHANNELS 65599
31 
32 /**
33  * channels used to for RTMP packets with different purposes (i.e. data, network
34  * control, remote procedure calls, etc.)
35  */
37  RTMP_NETWORK_CHANNEL = 2, ///< channel for network-related messages (bandwidth report, ping, etc)
38  RTMP_SYSTEM_CHANNEL, ///< channel for sending server control messages
39  RTMP_AUDIO_CHANNEL, ///< channel for audio data
40  RTMP_VIDEO_CHANNEL = 6, ///< channel for video data
41  RTMP_SOURCE_CHANNEL = 8, ///< channel for a/v invokes
42 };
43 
44 /**
45  * known RTMP packet types
46  */
47 typedef enum RTMPPacketType {
48  RTMP_PT_CHUNK_SIZE = 1, ///< chunk size change
49  RTMP_PT_BYTES_READ = 3, ///< number of bytes read
50  RTMP_PT_USER_CONTROL, ///< user control
51  RTMP_PT_WINDOW_ACK_SIZE, ///< window acknowledgement size
52  RTMP_PT_SET_PEER_BW, ///< peer bandwidth
53  RTMP_PT_AUDIO = 8, ///< audio packet
54  RTMP_PT_VIDEO, ///< video packet
55  RTMP_PT_FLEX_STREAM = 15, ///< Flex shared stream
56  RTMP_PT_FLEX_OBJECT, ///< Flex shared object
57  RTMP_PT_FLEX_MESSAGE, ///< Flex shared message
58  RTMP_PT_NOTIFY, ///< some notification
59  RTMP_PT_SHARED_OBJ, ///< shared object
60  RTMP_PT_INVOKE, ///< invoke some stream action
61  RTMP_PT_METADATA = 22, ///< FLV metadata
63 
64 /**
65  * possible RTMP packet header sizes
66  */
68  RTMP_PS_TWELVEBYTES = 0, ///< packet has 12-byte header
69  RTMP_PS_EIGHTBYTES, ///< packet has 8-byte header
70  RTMP_PS_FOURBYTES, ///< packet has 4-byte header
71  RTMP_PS_ONEBYTE ///< packet is really a next chunk of a packet
72 };
73 
74 /**
75  * structure for holding RTMP packets
76  */
77 typedef struct RTMPPacket {
78  int channel_id; ///< RTMP channel ID (nothing to do with audio/video channels though)
79  RTMPPacketType type; ///< packet payload type
80  uint32_t timestamp; ///< packet full timestamp
81  uint32_t ts_field; ///< 24-bit timestamp or increment to the previous one, in milliseconds (latter only for media packets). Clipped to a maximum of 0xFFFFFF, indicating an extended timestamp field.
82  uint32_t extra; ///< probably an additional channel ID used during streaming data
83  uint8_t *data; ///< packet payload
84  int size; ///< packet payload size
85  int offset; ///< amount of data read so far
86  int read; ///< amount read, including headers
87 } RTMPPacket;
88 
89 /**
90  * Create new RTMP packet with given attributes.
91  *
92  * @param pkt packet
93  * @param channel_id packet channel ID
94  * @param type packet type
95  * @param timestamp packet timestamp
96  * @param size packet size
97  * @return zero on success, negative value otherwise
98  */
100  int timestamp, int size);
101 
102 /**
103  * Free RTMP packet.
104  *
105  * @param pkt packet
106  */
108 
109 /**
110  * Read RTMP packet sent by the server.
111  *
112  * @param h reader context
113  * @param p packet
114  * @param chunk_size current chunk size
115  * @param prev_pkt previously read packet headers for all channels
116  * (may be needed for restoring incomplete packet header)
117  * @param nb_prev_pkt number of allocated elements in prev_pkt
118  * @return number of bytes read on success, negative value otherwise
119  */
121  int chunk_size, RTMPPacket **prev_pkt,
122  int *nb_prev_pkt);
123 /**
124  * Read internal RTMP packet sent by the server.
125  *
126  * @param h reader context
127  * @param p packet
128  * @param chunk_size current chunk size
129  * @param prev_pkt previously read packet headers for all channels
130  * (may be needed for restoring incomplete packet header)
131  * @param nb_prev_pkt number of allocated elements in prev_pkt
132  * @param c the first byte already read
133  * @return number of bytes read on success, negative value otherwise
134  */
135 int ff_rtmp_packet_read_internal(URLContext *h, RTMPPacket *p, int chunk_size,
136  RTMPPacket **prev_pkt, int *nb_prev_pkt,
137  uint8_t c);
138 
139 /**
140  * Send RTMP packet to the server.
141  *
142  * @param h reader context
143  * @param p packet to send
144  * @param chunk_size current chunk size
145  * @param prev_pkt previously sent packet headers for all channels
146  * (may be used for packet header compressing)
147  * @param nb_prev_pkt number of allocated elements in prev_pkt
148  * @return number of bytes written on success, negative value otherwise
149  */
151  int chunk_size, RTMPPacket **prev_pkt,
152  int *nb_prev_pkt);
153 
154 /**
155  * Print information and contents of RTMP packet.
156  *
157  * @param ctx output context
158  * @param p packet to dump
159  */
160 void ff_rtmp_packet_dump(void *ctx, RTMPPacket *p);
161 
162 /**
163  * Enlarge the prev_pkt array to fit the given channel
164  *
165  * @param prev_pkt array with previously sent packet headers
166  * @param nb_prev_pkt number of allocated elements in prev_pkt
167  * @param channel the channel number that needs to be allocated
168  */
169 int ff_rtmp_check_alloc_array(RTMPPacket **prev_pkt, int *nb_prev_pkt,
170  int channel);
171 
172 /**
173  * @name Functions used to work with the AMF format (which is also used in .flv)
174  * @see amf_* funcs in libavformat/flvdec.c
175  * @{
176  */
177 
178 /**
179  * Calculate number of bytes taken by first AMF entry in data.
180  *
181  * @param data input data
182  * @param data_end input buffer end
183  * @return number of bytes used by first AMF entry
184  */
185 int ff_amf_tag_size(const uint8_t *data, const uint8_t *data_end);
186 
187 /**
188  * Retrieve value of given AMF object field in string form.
189  *
190  * @param data AMF object data
191  * @param data_end input buffer end
192  * @param name name of field to retrieve
193  * @param dst buffer for storing result
194  * @param dst_size output buffer size
195  * @return 0 if search and retrieval succeeded, negative value otherwise
196  */
197 int ff_amf_get_field_value(const uint8_t *data, const uint8_t *data_end,
198  const uint8_t *name, uint8_t *dst, int dst_size);
199 
200 /**
201  * Write boolean value in AMF format to buffer.
202  *
203  * @param dst pointer to the input buffer (will be modified)
204  * @param val value to write
205  */
206 void ff_amf_write_bool(uint8_t **dst, int val);
207 
208 /**
209  * Write number in AMF format to buffer.
210  *
211  * @param dst pointer to the input buffer (will be modified)
212  * @param num value to write
213  */
214 void ff_amf_write_number(uint8_t **dst, double num);
215 
216 /**
217  * Write string in AMF format to buffer.
218  *
219  * @param dst pointer to the input buffer (will be modified)
220  * @param str string to write
221  */
222 void ff_amf_write_string(uint8_t **dst, const char *str);
223 
224 /**
225  * Write a string consisting of two parts in AMF format to a buffer.
226  *
227  * @param dst pointer to the input buffer (will be modified)
228  * @param str1 first string to write, may be null
229  * @param str2 second string to write, may be null
230  */
231 void ff_amf_write_string2(uint8_t **dst, const char *str1, const char *str2);
232 
233 /**
234  * Write AMF NULL value to buffer.
235  *
236  * @param dst pointer to the input buffer (will be modified)
237  */
238 void ff_amf_write_null(uint8_t **dst);
239 
240 /**
241  * Write marker for AMF object to buffer.
242  *
243  * @param dst pointer to the input buffer (will be modified)
244  */
245 void ff_amf_write_object_start(uint8_t **dst);
246 
247 /**
248  * Write string used as field name in AMF object to buffer.
249  *
250  * @param dst pointer to the input buffer (will be modified)
251  * @param str string to write
252  */
253 void ff_amf_write_field_name(uint8_t **dst, const char *str);
254 
255 /**
256  * Write marker for end of AMF object to buffer.
257  *
258  * @param dst pointer to the input buffer (will be modified)
259  */
260 void ff_amf_write_object_end(uint8_t **dst);
261 
262 /**
263  * Read AMF number value.
264  *
265  *@param[in,out] gbc GetByteContext initialized with AMF-formatted data
266  *@param[out] val read value
267  *@return 0 on success or an AVERROR code on failure
268 */
269 int ff_amf_read_number(GetByteContext *gbc, double *val);
270 
271 /**
272  * Get AMF string value.
273  *
274  * This function behaves the same as ff_amf_read_string except that
275  * it does not expect the AMF type prepended to the actual data.
276  * Appends a trailing null byte to output string in order to
277  * ease later parsing.
278  *
279  *@param[in,out] gbc GetByteContext initialized with AMF-formatted data
280  *@param[out] str read string
281  *@param[in] strsize buffer size available to store the read string
282  *@param[out] length read string length
283  *@return 0 on success or an AVERROR code on failure
284 */
285 int ff_amf_get_string(GetByteContext *bc, uint8_t *str,
286  int strsize, int *length);
287 
288 /**
289  * Read AMF string value.
290  *
291  * Appends a trailing null byte to output string in order to
292  * ease later parsing.
293  *
294  *@param[in,out] gbc GetByteContext initialized with AMF-formatted data
295  *@param[out] str read string
296  *@param[in] strsize buffer size available to store the read string
297  *@param[out] length read string length
298  *@return 0 on success or an AVERROR code on failure
299 */
300 int ff_amf_read_string(GetByteContext *gbc, uint8_t *str,
301  int strsize, int *length);
302 
303 /**
304  * Read AMF NULL value.
305  *
306  *@param[in,out] gbc GetByteContext initialized with AMF-formatted data
307  *@return 0 on success or an AVERROR code on failure
308 */
310 
311 /**
312  * Match AMF string with a NULL-terminated string.
313  *
314  * @return 0 if the strings do not match.
315  */
316 
317 int ff_amf_match_string(const uint8_t *data, int size, const char *str);
318 
319 /** @} */ // AMF funcs
320 
321 #endif /* AVFORMAT_RTMPPKT_H */
RTMP_PT_METADATA
@ RTMP_PT_METADATA
FLV metadata.
Definition: rtmppkt.h:61
RTMP_PT_FLEX_MESSAGE
@ RTMP_PT_FLEX_MESSAGE
Flex shared message.
Definition: rtmppkt.h:57
RTMP_PT_FLEX_OBJECT
@ RTMP_PT_FLEX_OBJECT
Flex shared object.
Definition: rtmppkt.h:56
name
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf default minimum maximum flags name is the option name
Definition: writing_filters.txt:88
RTMP_PT_SHARED_OBJ
@ RTMP_PT_SHARED_OBJ
shared object
Definition: rtmppkt.h:59
RTMP_NETWORK_CHANNEL
@ RTMP_NETWORK_CHANNEL
channel for network-related messages (bandwidth report, ping, etc)
Definition: rtmppkt.h:37
RTMP_PT_BYTES_READ
@ RTMP_PT_BYTES_READ
number of bytes read
Definition: rtmppkt.h:49
RTMPPacket::type
RTMPPacketType type
packet payload type
Definition: rtmppkt.h:79
ff_amf_write_object_end
void ff_amf_write_object_end(uint8_t **dst)
Write marker for end of AMF object to buffer.
Definition: rtmppkt.c:79
GetByteContext
Definition: bytestream.h:33
ff_amf_write_bool
void ff_amf_write_bool(uint8_t **dst, int val)
Write boolean value in AMF format to buffer.
Definition: rtmppkt.c:31
RTMP_PT_NOTIFY
@ RTMP_PT_NOTIFY
some notification
Definition: rtmppkt.h:58
ff_amf_write_null
void ff_amf_write_null(uint8_t **dst)
Write AMF NULL value to buffer.
Definition: rtmppkt.c:63
data
const char data[16]
Definition: mxf.c:143
ff_amf_get_string
int ff_amf_get_string(GetByteContext *bc, uint8_t *str, int strsize, int *length)
Get AMF string value.
Definition: rtmppkt.c:97
RTMP_PT_WINDOW_ACK_SIZE
@ RTMP_PT_WINDOW_ACK_SIZE
window acknowledgement size
Definition: rtmppkt.h:51
ff_amf_read_string
int ff_amf_read_string(GetByteContext *gbc, uint8_t *str, int strsize, int *length)
Read AMF string value.
Definition: rtmppkt.c:115
RTMPPacket::extra
uint32_t extra
probably an additional channel ID used during streaming data
Definition: rtmppkt.h:82
RTMP_VIDEO_CHANNEL
@ RTMP_VIDEO_CHANNEL
channel for video data
Definition: rtmppkt.h:40
ff_amf_write_number
void ff_amf_write_number(uint8_t **dst, double num)
Write number in AMF format to buffer.
Definition: rtmppkt.c:37
ff_amf_read_number
int ff_amf_read_number(GetByteContext *gbc, double *val)
Read AMF number value.
Definition: rtmppkt.c:87
val
static double val(void *priv, double ch)
Definition: aeval.c:77
type
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf type
Definition: writing_filters.txt:86
pkt
AVPacket * pkt
Definition: movenc.c:59
ff_amf_tag_size
int ff_amf_tag_size(const uint8_t *data, const uint8_t *data_end)
Calculate number of bytes taken by first AMF entry in data.
Definition: rtmppkt.c:489
RTMP_SYSTEM_CHANNEL
@ RTMP_SYSTEM_CHANNEL
channel for sending server control messages
Definition: rtmppkt.h:38
RTMPPacket::read
int read
amount read, including headers
Definition: rtmppkt.h:86
ctx
AVFormatContext * ctx
Definition: movenc.c:48
ff_amf_write_string2
void ff_amf_write_string2(uint8_t **dst, const char *str1, const char *str2)
Write a string consisting of two parts in AMF format to a buffer.
Definition: rtmppkt.c:50
ff_amf_read_null
int ff_amf_read_null(GetByteContext *gbc)
Read AMF NULL value.
Definition: rtmppkt.c:123
ff_amf_write_string
void ff_amf_write_string(uint8_t **dst, const char *str)
Write string in AMF format to buffer.
Definition: rtmppkt.c:43
RTMPPacket
structure for holding RTMP packets
Definition: rtmppkt.h:77
RTMPPacketType
RTMPPacketType
known RTMP packet types
Definition: rtmppkt.h:47
ff_amf_write_field_name
void ff_amf_write_field_name(uint8_t **dst, const char *str)
Write string used as field name in AMF object to buffer.
Definition: rtmppkt.c:73
ff_rtmp_packet_read
int ff_rtmp_packet_read(URLContext *h, RTMPPacket *p, int chunk_size, RTMPPacket **prev_pkt, int *nb_prev_pkt)
Read RTMP packet sent by the server.
Definition: rtmppkt.c:151
RTMP_PT_USER_CONTROL
@ RTMP_PT_USER_CONTROL
user control
Definition: rtmppkt.h:50
RTMPPacket::size
int size
packet payload size
Definition: rtmppkt.h:84
c
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
RTMP_PT_INVOKE
@ RTMP_PT_INVOKE
invoke some stream action
Definition: rtmppkt.h:60
size
int size
Definition: twinvq_data.h:10344
RTMPPacket::ts_field
uint32_t ts_field
24-bit timestamp or increment to the previous one, in milliseconds (latter only for media packets)....
Definition: rtmppkt.h:81
ff_rtmp_packet_write
int ff_rtmp_packet_write(URLContext *h, RTMPPacket *p, int chunk_size, RTMPPacket **prev_pkt, int *nb_prev_pkt)
Send RTMP packet to the server.
Definition: rtmppkt.c:305
ff_rtmp_packet_dump
void ff_rtmp_packet_dump(void *ctx, RTMPPacket *p)
Print information and contents of RTMP packet.
RTMPPacketSize
RTMPPacketSize
possible RTMP packet header sizes
Definition: rtmppkt.h:67
URLContext
Definition: url.h:37
ff_amf_write_object_start
void ff_amf_write_object_start(uint8_t **dst)
Write marker for AMF object to buffer.
Definition: rtmppkt.c:68
RTMPPacket::timestamp
uint32_t timestamp
packet full timestamp
Definition: rtmppkt.h:80
url.h
ff_amf_match_string
int ff_amf_match_string(const uint8_t *data, int size, const char *str)
Match AMF string with a NULL-terminated string.
Definition: rtmppkt.c:689
RTMP_PT_SET_PEER_BW
@ RTMP_PT_SET_PEER_BW
peer bandwidth
Definition: rtmppkt.h:52
RTMPPacket::offset
int offset
amount of data read so far
Definition: rtmppkt.h:85
avformat.h
RTMPPacket::channel_id
int channel_id
RTMP channel ID (nothing to do with audio/video channels though)
Definition: rtmppkt.h:78
ff_amf_get_field_value
int ff_amf_get_field_value(const uint8_t *data, const uint8_t *data_end, const uint8_t *name, uint8_t *dst, int dst_size)
Retrieve value of given AMF object field in string form.
Definition: rtmppkt.c:557
RTMP_PS_EIGHTBYTES
@ RTMP_PS_EIGHTBYTES
packet has 8-byte header
Definition: rtmppkt.h:69
ff_rtmp_check_alloc_array
int ff_rtmp_check_alloc_array(RTMPPacket **prev_pkt, int *nb_prev_pkt, int channel)
Enlarge the prev_pkt array to fit the given channel.
Definition: rtmppkt.c:130
RTMPPacket::data
uint8_t * data
packet payload
Definition: rtmppkt.h:83
ff_rtmp_packet_read_internal
int ff_rtmp_packet_read_internal(URLContext *h, RTMPPacket *p, int chunk_size, RTMPPacket **prev_pkt, int *nb_prev_pkt, uint8_t c)
Read internal RTMP packet sent by the server.
Definition: rtmppkt.c:290
RTMPChannel
RTMPChannel
channels used to for RTMP packets with different purposes (i.e.
Definition: rtmppkt.h:36
RTMP_PS_ONEBYTE
@ RTMP_PS_ONEBYTE
packet is really a next chunk of a packet
Definition: rtmppkt.h:71
bytestream.h
convert_header.str
string str
Definition: convert_header.py:20
h
h
Definition: vp9dsp_template.c:2038
RTMP_AUDIO_CHANNEL
@ RTMP_AUDIO_CHANNEL
channel for audio data
Definition: rtmppkt.h:39
RTMP_PT_AUDIO
@ RTMP_PT_AUDIO
audio packet
Definition: rtmppkt.h:53
RTMP_PS_TWELVEBYTES
@ RTMP_PS_TWELVEBYTES
packet has 12-byte header
Definition: rtmppkt.h:68
RTMP_PT_VIDEO
@ RTMP_PT_VIDEO
video packet
Definition: rtmppkt.h:54
RTMP_SOURCE_CHANNEL
@ RTMP_SOURCE_CHANNEL
channel for a/v invokes
Definition: rtmppkt.h:41
ff_rtmp_packet_destroy
void ff_rtmp_packet_destroy(RTMPPacket *pkt)
Free RTMP packet.
Definition: rtmppkt.c:426
RTMP_PS_FOURBYTES
@ RTMP_PS_FOURBYTES
packet has 4-byte header
Definition: rtmppkt.h:70
RTMP_PT_FLEX_STREAM
@ RTMP_PT_FLEX_STREAM
Flex shared stream.
Definition: rtmppkt.h:55
channel
channel
Definition: ebur128.h:39
ff_rtmp_packet_create
int ff_rtmp_packet_create(RTMPPacket *pkt, int channel_id, RTMPPacketType type, int timestamp, int size)
Create new RTMP packet with given attributes.
Definition: rtmppkt.c:408
RTMP_PT_CHUNK_SIZE
@ RTMP_PT_CHUNK_SIZE
chunk size change
Definition: rtmppkt.h:48