You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

298 lines
9.5KB

  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_PING, ///< ping
  46. RTMP_PT_SERVER_BW, ///< server bandwidth
  47. RTMP_PT_CLIENT_BW, ///< client 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_delta; ///< timestamp increment to the previous one in milliseconds (latter only for media packets)
  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. * @return number of bytes read on success, negative value otherwise
  108. */
  109. int ff_rtmp_packet_read(URLContext *h, RTMPPacket *p,
  110. int chunk_size, RTMPPacket *prev_pkt);
  111. /**
  112. * Read internal RTMP packet sent by the server.
  113. *
  114. * @param h reader context
  115. * @param p packet
  116. * @param chunk_size current chunk size
  117. * @param prev_pkt previously read packet headers for all channels
  118. * (may be needed for restoring incomplete packet header)
  119. * @param c the first byte already read
  120. * @return number of bytes read on success, negative value otherwise
  121. */
  122. int ff_rtmp_packet_read_internal(URLContext *h, RTMPPacket *p, int chunk_size,
  123. RTMPPacket *prev_pkt, uint8_t c);
  124. /**
  125. * Send RTMP packet to the server.
  126. *
  127. * @param h reader context
  128. * @param p packet to send
  129. * @param chunk_size current chunk size
  130. * @param prev_pkt previously sent packet headers for all channels
  131. * (may be used for packet header compressing)
  132. * @return number of bytes written on success, negative value otherwise
  133. */
  134. int ff_rtmp_packet_write(URLContext *h, RTMPPacket *p,
  135. int chunk_size, RTMPPacket *prev_pkt);
  136. /**
  137. * Print information and contents of RTMP packet.
  138. *
  139. * @param ctx output context
  140. * @param p packet to dump
  141. */
  142. void ff_rtmp_packet_dump(void *ctx, RTMPPacket *p);
  143. /**
  144. * @name Functions used to work with the AMF format (which is also used in .flv)
  145. * @see amf_* funcs in libavformat/flvdec.c
  146. * @{
  147. */
  148. /**
  149. * Calculate number of bytes taken by first AMF entry in data.
  150. *
  151. * @param data input data
  152. * @param data_end input buffer end
  153. * @return number of bytes used by first AMF entry
  154. */
  155. int ff_amf_tag_size(const uint8_t *data, const uint8_t *data_end);
  156. /**
  157. * Retrieve value of given AMF object field in string form.
  158. *
  159. * @param data AMF object data
  160. * @param data_end input buffer end
  161. * @param name name of field to retrieve
  162. * @param dst buffer for storing result
  163. * @param dst_size output buffer size
  164. * @return 0 if search and retrieval succeeded, negative value otherwise
  165. */
  166. int ff_amf_get_field_value(const uint8_t *data, const uint8_t *data_end,
  167. const uint8_t *name, uint8_t *dst, int dst_size);
  168. /**
  169. * Write boolean value in AMF format to buffer.
  170. *
  171. * @param dst pointer to the input buffer (will be modified)
  172. * @param val value to write
  173. */
  174. void ff_amf_write_bool(uint8_t **dst, int val);
  175. /**
  176. * Write number in AMF format to buffer.
  177. *
  178. * @param dst pointer to the input buffer (will be modified)
  179. * @param num value to write
  180. */
  181. void ff_amf_write_number(uint8_t **dst, double num);
  182. /**
  183. * Write string in AMF format to buffer.
  184. *
  185. * @param dst pointer to the input buffer (will be modified)
  186. * @param str string to write
  187. */
  188. void ff_amf_write_string(uint8_t **dst, const char *str);
  189. /**
  190. * Write a string consisting of two parts in AMF format to a buffer.
  191. *
  192. * @param dst pointer to the input buffer (will be modified)
  193. * @param str1 first string to write, may be null
  194. * @param str2 second string to write, may be null
  195. */
  196. void ff_amf_write_string2(uint8_t **dst, const char *str1, const char *str2);
  197. /**
  198. * Write AMF NULL value to buffer.
  199. *
  200. * @param dst pointer to the input buffer (will be modified)
  201. */
  202. void ff_amf_write_null(uint8_t **dst);
  203. /**
  204. * Write marker for AMF object to buffer.
  205. *
  206. * @param dst pointer to the input buffer (will be modified)
  207. */
  208. void ff_amf_write_object_start(uint8_t **dst);
  209. /**
  210. * Write string used as field name in AMF object to buffer.
  211. *
  212. * @param dst pointer to the input buffer (will be modified)
  213. * @param str string to write
  214. */
  215. void ff_amf_write_field_name(uint8_t **dst, const char *str);
  216. /**
  217. * Write marker for end of AMF object to buffer.
  218. *
  219. * @param dst pointer to the input buffer (will be modified)
  220. */
  221. void ff_amf_write_object_end(uint8_t **dst);
  222. /**
  223. * Read AMF boolean value.
  224. *
  225. *@param[in,out] gbc GetByteContext initialized with AMF-formatted data
  226. *@param[out] val 0 or 1
  227. *@return 0 on success or an AVERROR code on failure
  228. */
  229. int ff_amf_read_bool(GetByteContext *gbc, int *val);
  230. /**
  231. * Read AMF number value.
  232. *
  233. *@param[in,out] gbc GetByteContext initialized with AMF-formatted data
  234. *@param[out] val read value
  235. *@return 0 on success or an AVERROR code on failure
  236. */
  237. int ff_amf_read_number(GetByteContext *gbc, double *val);
  238. /**
  239. * Read AMF string value.
  240. *
  241. * Appends a trailing null byte to output string in order to
  242. * ease later parsing.
  243. *
  244. *@param[in,out] gbc GetByteContext initialized with AMF-formatted data
  245. *@param[out] str read string
  246. *@param[in] strsize buffer size available to store the read string
  247. *@param[out] length read string length
  248. *@return 0 on success or an AVERROR code on failure
  249. */
  250. int ff_amf_read_string(GetByteContext *gbc, uint8_t *str,
  251. int strsize, int *length);
  252. /**
  253. * Read AMF NULL value.
  254. *
  255. *@param[in,out] gbc GetByteContext initialized with AMF-formatted data
  256. *@return 0 on success or an AVERROR code on failure
  257. */
  258. int ff_amf_read_null(GetByteContext *gbc);
  259. /**
  260. * Match AMF string with a NULL-terminated string.
  261. *
  262. * @return 0 if the strings do not match.
  263. */
  264. int ff_amf_match_string(const uint8_t *data, int size, const char *str);
  265. /** @} */ // AMF funcs
  266. #endif /* AVFORMAT_RTMPPKT_H */