rtmppkt.h 7.1 KB

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