rtmppkt.h 6.8 KB

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