FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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_SOURCE_CHANNEL, ///< channel for sending a/v to server
40  RTMP_VIDEO_CHANNEL = 8, ///< channel for video data
41  RTMP_AUDIO_CHANNEL, ///< channel for audio data
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_PING, ///< ping
51  RTMP_PT_SERVER_BW, ///< server bandwidth
52  RTMP_PT_CLIENT_BW, ///< client 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_delta; ///< timestamp increment to the previous one in milliseconds (latter only for media packets)
82  uint32_t extra; ///< probably an additional channel ID used during streaming data
83  uint8_t *data; ///< packet payload
84  int data_size; ///< packet payload size
85 } RTMPPacket;
86 
87 /**
88  * Create new RTMP packet with given attributes.
89  *
90  * @param pkt packet
91  * @param channel_id packet channel ID
92  * @param type packet type
93  * @param timestamp packet timestamp
94  * @param size packet size
95  * @return zero on success, negative value otherwise
96  */
97 int ff_rtmp_packet_create(RTMPPacket *pkt, int channel_id, RTMPPacketType type,
98  int timestamp, int size);
99 
100 /**
101  * Free RTMP packet.
102  *
103  * @param pkt packet
104  */
106 
107 /**
108  * Read RTMP packet sent by the server.
109  *
110  * @param h reader context
111  * @param p packet
112  * @param chunk_size current chunk size
113  * @param prev_pkt previously read packet headers for all channels
114  * (may be needed for restoring incomplete packet header)
115  * @return number of bytes read on success, negative value otherwise
116  */
118  int chunk_size, RTMPPacket *prev_pkt);
119 /**
120  * Read internal RTMP packet sent by the server.
121  *
122  * @param h reader context
123  * @param p packet
124  * @param chunk_size current chunk size
125  * @param prev_pkt previously read packet headers for all channels
126  * (may be needed for restoring incomplete packet header)
127  * @param c the first byte already read
128  * @return number of bytes read on success, negative value otherwise
129  */
130 int ff_rtmp_packet_read_internal(URLContext *h, RTMPPacket *p, int chunk_size,
131  RTMPPacket *prev_pkt, uint8_t c);
132 
133 /**
134  * Send RTMP packet to the server.
135  *
136  * @param h reader context
137  * @param p packet to send
138  * @param chunk_size current chunk size
139  * @param prev_pkt previously sent packet headers for all channels
140  * (may be used for packet header compressing)
141  * @return number of bytes written on success, negative value otherwise
142  */
144  int chunk_size, RTMPPacket *prev_pkt);
145 
146 /**
147  * Print information and contents of RTMP packet.
148  *
149  * @param ctx output context
150  * @param p packet to dump
151  */
152 void ff_rtmp_packet_dump(void *ctx, RTMPPacket *p);
153 
154 /**
155  * @name Functions used to work with the AMF format (which is also used in .flv)
156  * @see amf_* funcs in libavformat/flvdec.c
157  * @{
158  */
159 
160 /**
161  * Calculate number of bytes taken by first AMF entry in data.
162  *
163  * @param data input data
164  * @param data_end input buffer end
165  * @return number of bytes used by first AMF entry
166  */
167 int ff_amf_tag_size(const uint8_t *data, const uint8_t *data_end);
168 
169 /**
170  * Retrieve value of given AMF object field in string form.
171  *
172  * @param data AMF object data
173  * @param data_end input buffer end
174  * @param name name of field to retrieve
175  * @param dst buffer for storing result
176  * @param dst_size output buffer size
177  * @return 0 if search and retrieval succeeded, negative value otherwise
178  */
179 int ff_amf_get_field_value(const uint8_t *data, const uint8_t *data_end,
180  const uint8_t *name, uint8_t *dst, int dst_size);
181 
182 /**
183  * Write boolean value in AMF format to buffer.
184  *
185  * @param dst pointer to the input buffer (will be modified)
186  * @param val value to write
187  */
188 void ff_amf_write_bool(uint8_t **dst, int val);
189 
190 /**
191  * Write number in AMF format to buffer.
192  *
193  * @param dst pointer to the input buffer (will be modified)
194  * @param num value to write
195  */
196 void ff_amf_write_number(uint8_t **dst, double num);
197 
198 /**
199  * Write string in AMF format to buffer.
200  *
201  * @param dst pointer to the input buffer (will be modified)
202  * @param str string to write
203  */
204 void ff_amf_write_string(uint8_t **dst, const char *str);
205 
206 /**
207  * Write a string consisting of two parts in AMF format to a buffer.
208  *
209  * @param dst pointer to the input buffer (will be modified)
210  * @param str1 first string to write, may be null
211  * @param str2 second string to write, may be null
212  */
213 void ff_amf_write_string2(uint8_t **dst, const char *str1, const char *str2);
214 
215 /**
216  * Write AMF NULL value to buffer.
217  *
218  * @param dst pointer to the input buffer (will be modified)
219  */
220 void ff_amf_write_null(uint8_t **dst);
221 
222 /**
223  * Write marker for AMF object to buffer.
224  *
225  * @param dst pointer to the input buffer (will be modified)
226  */
228 
229 /**
230  * Write string used as field name in AMF object to buffer.
231  *
232  * @param dst pointer to the input buffer (will be modified)
233  * @param str string to write
234  */
235 void ff_amf_write_field_name(uint8_t **dst, const char *str);
236 
237 /**
238  * Write marker for end of AMF object to buffer.
239  *
240  * @param dst pointer to the input buffer (will be modified)
241  */
242 void ff_amf_write_object_end(uint8_t **dst);
243 
244 /**
245  * Read AMF boolean value.
246  *
247  *@param[in,out] gbc GetByteContext initialized with AMF-formatted data
248  *@param[out] val 0 or 1
249  *@return 0 on success or an AVERROR code on failure
250 */
251 int ff_amf_read_bool(GetByteContext *gbc, int *val);
252 
253 /**
254  * Read AMF number value.
255  *
256  *@param[in,out] gbc GetByteContext initialized with AMF-formatted data
257  *@param[out] val read value
258  *@return 0 on success or an AVERROR code on failure
259 */
260 int ff_amf_read_number(GetByteContext *gbc, double *val);
261 
262 /**
263  * Read AMF string value.
264  *
265  * Appends a trailing null byte to output string in order to
266  * ease later parsing.
267  *
268  *@param[in,out] gbc GetByteContext initialized with AMF-formatted data
269  *@param[out] str read string
270  *@param[in] strsize buffer size available to store the read string
271  *@param[out] length read string length
272  *@return 0 on success or an AVERROR code on failure
273 */
275  int strsize, int *length);
276 
277 /**
278  * Read AMF NULL value.
279  *
280  *@param[in,out] gbc GetByteContext initialized with AMF-formatted data
281  *@return 0 on success or an AVERROR code on failure
282 */
284 
285 
286 /** @} */ // AMF funcs
287 
288 #endif /* AVFORMAT_RTMPPKT_H */