]> git.sesse.net Git - ffmpeg/blob - libavformat/rtmppkt.h
avformat: Drop pointless "format" from container long names
[ffmpeg] / libavformat / rtmppkt.h
1 /*
2  * RTMP packet utilities
3  * Copyright (c) 2009 Kostya Shishkov
4  *
5  * This file is part of Libav.
6  *
7  * Libav 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  * Libav 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 Libav; 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 "avformat.h"
26 #include "url.h"
27
28 /** maximum possible number of different RTMP channels */
29 #define RTMP_CHANNELS 65599
30
31 /**
32  * channels used to for RTMP packets with different purposes (i.e. data, network
33  * control, remote procedure calls, etc.)
34  */
35 enum RTMPChannel {
36     RTMP_NETWORK_CHANNEL = 2,   ///< channel for network-related messages (bandwidth report, ping, etc)
37     RTMP_SYSTEM_CHANNEL,        ///< channel for sending server control messages
38     RTMP_SOURCE_CHANNEL,        ///< channel for sending a/v to server
39     RTMP_VIDEO_CHANNEL = 8,     ///< channel for video data
40     RTMP_AUDIO_CHANNEL,         ///< channel for audio data
41 };
42
43 /**
44  * known RTMP packet types
45  */
46 typedef enum RTMPPacketType {
47     RTMP_PT_CHUNK_SIZE   =  1,  ///< chunk size change
48     RTMP_PT_BYTES_READ   =  3,  ///< number of bytes read
49     RTMP_PT_PING,               ///< ping
50     RTMP_PT_SERVER_BW,          ///< server bandwidth
51     RTMP_PT_CLIENT_BW,          ///< client bandwidth
52     RTMP_PT_AUDIO        =  8,  ///< audio packet
53     RTMP_PT_VIDEO,              ///< video packet
54     RTMP_PT_FLEX_STREAM  = 15,  ///< Flex shared stream
55     RTMP_PT_FLEX_OBJECT,        ///< Flex shared object
56     RTMP_PT_FLEX_MESSAGE,       ///< Flex shared message
57     RTMP_PT_NOTIFY,             ///< some notification
58     RTMP_PT_SHARED_OBJ,         ///< shared object
59     RTMP_PT_INVOKE,             ///< invoke some stream action
60     RTMP_PT_METADATA     = 22,  ///< FLV metadata
61 } RTMPPacketType;
62
63 /**
64  * possible RTMP packet header sizes
65  */
66 enum RTMPPacketSize {
67     RTMP_PS_TWELVEBYTES = 0, ///< packet has 12-byte header
68     RTMP_PS_EIGHTBYTES,      ///< packet has 8-byte header
69     RTMP_PS_FOURBYTES,       ///< packet has 4-byte header
70     RTMP_PS_ONEBYTE          ///< packet is really a next chunk of a packet
71 };
72
73 /**
74  * structure for holding RTMP packets
75  */
76 typedef struct RTMPPacket {
77     int            channel_id; ///< RTMP channel ID (nothing to do with audio/video channels though)
78     RTMPPacketType type;       ///< packet payload type
79     uint32_t       timestamp;  ///< packet full timestamp
80     uint32_t       ts_delta;   ///< timestamp increment to the previous one in milliseconds (latter only for media packets)
81     uint32_t       extra;      ///< probably an additional channel ID used during streaming data
82     uint8_t        *data;      ///< packet payload
83     int            data_size;  ///< packet payload size
84 } RTMPPacket;
85
86 /**
87  * Create new RTMP packet with given attributes.
88  *
89  * @param pkt        packet
90  * @param channel_id packet channel ID
91  * @param type       packet type
92  * @param timestamp  packet timestamp
93  * @param size       packet size
94  * @return zero on success, negative value otherwise
95  */
96 int ff_rtmp_packet_create(RTMPPacket *pkt, int channel_id, RTMPPacketType type,
97                           int timestamp, int size);
98
99 /**
100  * Free RTMP packet.
101  *
102  * @param pkt packet
103  */
104 void ff_rtmp_packet_destroy(RTMPPacket *pkt);
105
106 /**
107  * Read RTMP packet sent by the server.
108  *
109  * @param h          reader context
110  * @param p          packet
111  * @param chunk_size current chunk size
112  * @param prev_pkt   previously read packet headers for all channels
113  *                   (may be needed for restoring incomplete packet header)
114  * @return number of bytes read on success, negative value otherwise
115  */
116 int ff_rtmp_packet_read(URLContext *h, RTMPPacket *p,
117                         int chunk_size, RTMPPacket *prev_pkt);
118 /**
119  * Read internal RTMP packet sent by the server.
120  *
121  * @param h          reader context
122  * @param p          packet
123  * @param chunk_size current chunk size
124  * @param prev_pkt   previously read packet headers for all channels
125  *                   (may be needed for restoring incomplete packet header)
126  * @param c          the first byte already read
127  * @return number of bytes read on success, negative value otherwise
128  */
129 int ff_rtmp_packet_read_internal(URLContext *h, RTMPPacket *p, int chunk_size,
130                                  RTMPPacket *prev_pkt, uint8_t c);
131
132 /**
133  * Send RTMP packet to the server.
134  *
135  * @param h          reader context
136  * @param p          packet to send
137  * @param chunk_size current chunk size
138  * @param prev_pkt   previously sent packet headers for all channels
139  *                   (may be used for packet header compressing)
140  * @return number of bytes written on success, negative value otherwise
141  */
142 int ff_rtmp_packet_write(URLContext *h, RTMPPacket *p,
143                          int chunk_size, RTMPPacket *prev_pkt);
144
145 /**
146  * Print information and contents of RTMP packet.
147  *
148  * @param ctx        output context
149  * @param p          packet to dump
150  */
151 void ff_rtmp_packet_dump(void *ctx, RTMPPacket *p);
152
153 /**
154  * @name Functions used to work with the AMF format (which is also used in .flv)
155  * @see amf_* funcs in libavformat/flvdec.c
156  * @{
157  */
158
159 /**
160  * Calculate number of bytes taken by first AMF entry in data.
161  *
162  * @param data input data
163  * @param data_end input buffer end
164  * @return number of bytes used by first AMF entry
165  */
166 int ff_amf_tag_size(const uint8_t *data, const uint8_t *data_end);
167
168 /**
169  * Retrieve value of given AMF object field in string form.
170  *
171  * @param data     AMF object data
172  * @param data_end input buffer end
173  * @param name     name of field to retrieve
174  * @param dst      buffer for storing result
175  * @param dst_size output buffer size
176  * @return 0 if search and retrieval succeeded, negative value otherwise
177  */
178 int ff_amf_get_field_value(const uint8_t *data, const uint8_t *data_end,
179                            const uint8_t *name, uint8_t *dst, int dst_size);
180
181 /**
182  * Write boolean value in AMF format to buffer.
183  *
184  * @param dst pointer to the input buffer (will be modified)
185  * @param val value to write
186  */
187 void ff_amf_write_bool(uint8_t **dst, int val);
188
189 /**
190  * Write number in AMF format to buffer.
191  *
192  * @param dst pointer to the input buffer (will be modified)
193  * @param num value to write
194  */
195 void ff_amf_write_number(uint8_t **dst, double num);
196
197 /**
198  * Write string in AMF format to buffer.
199  *
200  * @param dst pointer to the input buffer (will be modified)
201  * @param str string to write
202  */
203 void ff_amf_write_string(uint8_t **dst, const char *str);
204
205 /**
206  * Write AMF NULL value to buffer.
207  *
208  * @param dst pointer to the input buffer (will be modified)
209  */
210 void ff_amf_write_null(uint8_t **dst);
211
212 /**
213  * Write marker for AMF object to buffer.
214  *
215  * @param dst pointer to the input buffer (will be modified)
216  */
217 void ff_amf_write_object_start(uint8_t **dst);
218
219 /**
220  * Write string used as field name in AMF object to buffer.
221  *
222  * @param dst pointer to the input buffer (will be modified)
223  * @param str string to write
224  */
225 void ff_amf_write_field_name(uint8_t **dst, const char *str);
226
227 /**
228  * Write marker for end of AMF object to buffer.
229  *
230  * @param dst pointer to the input buffer (will be modified)
231  */
232 void ff_amf_write_object_end(uint8_t **dst);
233
234 /** @} */ // AMF funcs
235
236 #endif /* AVFORMAT_RTMPPKT_H */