rtmppkt.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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. #ifndef AVFORMAT_RTMPPKT_H
  22. #define AVFORMAT_RTMPPKT_H
  23. #include "libavcodec/bytestream.h"
  24. #include "avformat.h"
  25. #include "url.h"
  26. /** maximum possible number of different RTMP channels */
  27. #define RTMP_CHANNELS 65599
  28. /**
  29. * channels used to for RTMP packets with different purposes (i.e. data, network
  30. * control, remote procedure calls, etc.)
  31. */
  32. enum RTMPChannel {
  33. RTMP_NETWORK_CHANNEL = 2, ///< channel for network-related messages (bandwidth report, ping, etc)
  34. RTMP_SYSTEM_CHANNEL, ///< channel for sending server control messages
  35. RTMP_AUDIO_CHANNEL, ///< channel for audio data
  36. RTMP_VIDEO_CHANNEL = 6, ///< channel for video data
  37. RTMP_SOURCE_CHANNEL = 8, ///< channel for a/v invokes
  38. };
  39. /**
  40. * known RTMP packet types
  41. */
  42. typedef enum RTMPPacketType {
  43. RTMP_PT_CHUNK_SIZE = 1, ///< chunk size change
  44. RTMP_PT_BYTES_READ = 3, ///< number of bytes read
  45. RTMP_PT_USER_CONTROL, ///< user control
  46. RTMP_PT_WINDOW_ACK_SIZE, ///< window acknowledgement size
  47. RTMP_PT_SET_PEER_BW, ///< peer bandwidth
  48. RTMP_PT_AUDIO = 8, ///< audio packet
  49. RTMP_PT_VIDEO, ///< video packet
  50. RTMP_PT_FLEX_STREAM = 15, ///< Flex shared stream
  51. RTMP_PT_FLEX_OBJECT, ///< Flex shared object
  52. RTMP_PT_FLEX_MESSAGE, ///< Flex shared message
  53. RTMP_PT_NOTIFY, ///< some notification
  54. RTMP_PT_SHARED_OBJ, ///< shared object
  55. RTMP_PT_INVOKE, ///< invoke some stream action
  56. RTMP_PT_METADATA = 22, ///< FLV metadata
  57. } RTMPPacketType;
  58. /**
  59. * possible RTMP packet header sizes
  60. */
  61. enum RTMPPacketSize {
  62. RTMP_PS_TWELVEBYTES = 0, ///< packet has 12-byte header
  63. RTMP_PS_EIGHTBYTES, ///< packet has 8-byte header
  64. RTMP_PS_FOURBYTES, ///< packet has 4-byte header
  65. RTMP_PS_ONEBYTE ///< packet is really a next chunk of a packet
  66. };
  67. /**
  68. * structure for holding RTMP packets
  69. */
  70. typedef struct RTMPPacket {
  71. int channel_id; ///< RTMP channel ID (nothing to do with audio/video channels though)
  72. RTMPPacketType type; ///< packet payload type
  73. uint32_t timestamp; ///< packet full timestamp
  74. 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.
  75. uint32_t extra; ///< probably an additional channel ID used during streaming data
  76. uint8_t *data; ///< packet payload
  77. int size; ///< packet payload size
  78. int offset; ///< amount of data read so far
  79. int read; ///< amount read, including headers
  80. } RTMPPacket;
  81. /**
  82. * Create new RTMP packet with given attributes.
  83. *
  84. * @param pkt packet
  85. * @param channel_id packet channel ID
  86. * @param type packet type
  87. * @param timestamp packet timestamp
  88. * @param size packet size
  89. * @return zero on success, negative value otherwise
  90. */
  91. int ff_rtmp_packet_create(RTMPPacket *pkt, int channel_id, RTMPPacketType type,
  92. int timestamp, int size);
  93. /**
  94. * Free RTMP packet.
  95. *
  96. * @param pkt packet
  97. */
  98. void ff_rtmp_packet_destroy(RTMPPacket *pkt);
  99. /**
  100. * Read RTMP packet sent by the server.
  101. *
  102. * @param h reader context
  103. * @param p packet
  104. * @param chunk_size current chunk size
  105. * @param prev_pkt previously read packet headers for all channels
  106. * (may be needed for restoring incomplete packet header)
  107. * @param nb_prev_pkt number of allocated elements in prev_pkt
  108. * @return number of bytes read on success, negative value otherwise
  109. */
  110. int ff_rtmp_packet_read(URLContext *h, RTMPPacket *p,
  111. int chunk_size, RTMPPacket **prev_pkt,
  112. int *nb_prev_pkt);
  113. /**
  114. * Read internal RTMP packet sent by the server.
  115. *
  116. * @param h reader context
  117. * @param p packet
  118. * @param chunk_size current chunk size
  119. * @param prev_pkt previously read packet headers for all channels
  120. * (may be needed for restoring incomplete packet header)
  121. * @param nb_prev_pkt number of allocated elements in prev_pkt
  122. * @param c the first byte already read
  123. * @return number of bytes read on success, negative value otherwise
  124. */
  125. int ff_rtmp_packet_read_internal(URLContext *h, RTMPPacket *p, int chunk_size,
  126. RTMPPacket **prev_pkt, int *nb_prev_pkt,
  127. uint8_t c);
  128. /**
  129. * Send RTMP packet to the server.
  130. *
  131. * @param h reader context
  132. * @param p packet to send
  133. * @param chunk_size current chunk size
  134. * @param prev_pkt previously sent packet headers for all channels
  135. * (may be used for packet header compressing)
  136. * @param nb_prev_pkt number of allocated elements in prev_pkt
  137. * @return number of bytes written on success, negative value otherwise
  138. */
  139. int ff_rtmp_packet_write(URLContext *h, RTMPPacket *p,
  140. int chunk_size, RTMPPacket **prev_pkt,
  141. int *nb_prev_pkt);
  142. /**
  143. * Print information and contents of RTMP packet.
  144. *
  145. * @param ctx output context
  146. * @param p packet to dump
  147. */
  148. void ff_rtmp_packet_dump(void *ctx, RTMPPacket *p);
  149. /**
  150. * Enlarge the prev_pkt array to fit the given channel
  151. *
  152. * @param prev_pkt array with previously sent packet headers
  153. * @param nb_prev_pkt number of allocated elements in prev_pkt
  154. * @param channel the channel number that needs to be allocated
  155. */
  156. int ff_rtmp_check_alloc_array(RTMPPacket **prev_pkt, int *nb_prev_pkt,
  157. int channel);
  158. /**
  159. * @name Functions used to work with the AMF format (which is also used in .flv)
  160. * @see amf_* funcs in libavformat/flvdec.c
  161. * @{
  162. */
  163. /**
  164. * Calculate number of bytes taken by first AMF entry in data.
  165. *
  166. * @param data input data
  167. * @param data_end input buffer end
  168. * @return number of bytes used by first AMF entry
  169. */
  170. int ff_amf_tag_size(const uint8_t *data, const uint8_t *data_end);
  171. /**
  172. * Retrieve value of given AMF object field in string form.
  173. *
  174. * @param data AMF object data
  175. * @param data_end input buffer end
  176. * @param name name of field to retrieve
  177. * @param dst buffer for storing result
  178. * @param dst_size output buffer size
  179. * @return 0 if search and retrieval succeeded, negative value otherwise
  180. */
  181. int ff_amf_get_field_value(const uint8_t *data, const uint8_t *data_end,
  182. const uint8_t *name, uint8_t *dst, int dst_size);
  183. /**
  184. * Write boolean value in AMF format to buffer.
  185. *
  186. * @param dst pointer to the input buffer (will be modified)
  187. * @param val value to write
  188. */
  189. void ff_amf_write_bool(uint8_t **dst, int val);
  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. * 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. * Write a string consisting of two parts in AMF format to a buffer.
  206. *
  207. * @param dst pointer to the input buffer (will be modified)
  208. * @param str1 first string to write, may be null
  209. * @param str2 second string to write, may be null
  210. */
  211. void ff_amf_write_string2(uint8_t **dst, const char *str1, const char *str2);
  212. /**
  213. * Write AMF NULL value to buffer.
  214. *
  215. * @param dst pointer to the input buffer (will be modified)
  216. */
  217. void ff_amf_write_null(uint8_t **dst);
  218. /**
  219. * Write marker for AMF object to buffer.
  220. *
  221. * @param dst pointer to the input buffer (will be modified)
  222. */
  223. void ff_amf_write_object_start(uint8_t **dst);
  224. /**
  225. * Write string used as field name in AMF object to buffer.
  226. *
  227. * @param dst pointer to the input buffer (will be modified)
  228. * @param str string to write
  229. */
  230. void ff_amf_write_field_name(uint8_t **dst, const char *str);
  231. /**
  232. * Write marker for end of AMF object to buffer.
  233. *
  234. * @param dst pointer to the input buffer (will be modified)
  235. */
  236. void ff_amf_write_object_end(uint8_t **dst);
  237. /**
  238. * Read AMF boolean value.
  239. *
  240. *@param[in,out] gbc GetByteContext initialized with AMF-formatted data
  241. *@param[out] val 0 or 1
  242. *@return 0 on success or an AVERROR code on failure
  243. */
  244. int ff_amf_read_bool(GetByteContext *gbc, int *val);
  245. /**
  246. * Read AMF number value.
  247. *
  248. *@param[in,out] gbc GetByteContext initialized with AMF-formatted data
  249. *@param[out] val read value
  250. *@return 0 on success or an AVERROR code on failure
  251. */
  252. int ff_amf_read_number(GetByteContext *gbc, double *val);
  253. /**
  254. * Get AMF string value.
  255. *
  256. * This function behaves the same as ff_amf_read_string except that
  257. * it does not expect the AMF type prepended to the actual data.
  258. * Appends a trailing null byte to output string in order to
  259. * ease later parsing.
  260. *
  261. *@param[in,out] gbc GetByteContext initialized with AMF-formatted data
  262. *@param[out] str read string
  263. *@param[in] strsize buffer size available to store the read string
  264. *@param[out] length read string length
  265. *@return 0 on success or an AVERROR code on failure
  266. */
  267. int ff_amf_get_string(GetByteContext *bc, uint8_t *str,
  268. int strsize, int *length);
  269. /**
  270. * Read AMF string value.
  271. *
  272. * Appends a trailing null byte to output string in order to
  273. * ease later parsing.
  274. *
  275. *@param[in,out] gbc GetByteContext initialized with AMF-formatted data
  276. *@param[out] str read string
  277. *@param[in] strsize buffer size available to store the read string
  278. *@param[out] length read string length
  279. *@return 0 on success or an AVERROR code on failure
  280. */
  281. int ff_amf_read_string(GetByteContext *gbc, uint8_t *str,
  282. int strsize, int *length);
  283. /**
  284. * Read AMF NULL value.
  285. *
  286. *@param[in,out] gbc GetByteContext initialized with AMF-formatted data
  287. *@return 0 on success or an AVERROR code on failure
  288. */
  289. int ff_amf_read_null(GetByteContext *gbc);
  290. /**
  291. * Match AMF string with a NULL-terminated string.
  292. *
  293. * @return 0 if the strings do not match.
  294. */
  295. int ff_amf_match_string(const uint8_t *data, int size, const char *str);
  296. /** @} */ // AMF funcs
  297. #endif /* AVFORMAT_RTMPPKT_H */