rtpdec.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. * RTP demuxer definitions
  3. * Copyright (c) 2002 Fabrice Bellard
  4. * Copyright (c) 2006 Ryan Martell <rdm4@martellventures.com>
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #ifndef AVFORMAT_RTPDEC_H
  23. #define AVFORMAT_RTPDEC_H
  24. #include "libavcodec/avcodec.h"
  25. #include "avformat.h"
  26. #include "rtp.h"
  27. #include "url.h"
  28. #include "srtp.h"
  29. typedef struct PayloadContext PayloadContext;
  30. typedef struct RTPDynamicProtocolHandler RTPDynamicProtocolHandler;
  31. #define RTP_MIN_PACKET_LENGTH 12
  32. #define RTP_MAX_PACKET_LENGTH 8192
  33. #define RTP_REORDER_QUEUE_DEFAULT_SIZE 500
  34. #define RTP_NOTS_VALUE ((uint32_t)-1)
  35. typedef struct RTPDemuxContext RTPDemuxContext;
  36. RTPDemuxContext *ff_rtp_parse_open(AVFormatContext *s1, AVStream *st,
  37. int payload_type, int queue_size);
  38. void ff_rtp_parse_set_dynamic_protocol(RTPDemuxContext *s, PayloadContext *ctx,
  39. const RTPDynamicProtocolHandler *handler);
  40. void ff_rtp_parse_set_crypto(RTPDemuxContext *s, const char *suite,
  41. const char *params);
  42. int ff_rtp_parse_packet(RTPDemuxContext *s, AVPacket *pkt,
  43. uint8_t **buf, int len);
  44. void ff_rtp_parse_close(RTPDemuxContext *s);
  45. int64_t ff_rtp_queued_packet_time(RTPDemuxContext *s);
  46. void ff_rtp_reset_packet_queue(RTPDemuxContext *s);
  47. /**
  48. * Send a dummy packet on both port pairs to set up the connection
  49. * state in potential NAT routers, so that we're able to receive
  50. * packets.
  51. *
  52. * Note, this only works if the NAT router doesn't remap ports. This
  53. * isn't a standardized procedure, but it works in many cases in practice.
  54. *
  55. * The same routine is used with RDT too, even if RDT doesn't use normal
  56. * RTP packets otherwise.
  57. */
  58. void ff_rtp_send_punch_packets(URLContext* rtp_handle);
  59. /**
  60. * some rtp servers assume client is dead if they don't hear from them...
  61. * so we send a Receiver Report to the provided URLContext or AVIOContext
  62. * (we don't have access to the rtcp handle from here)
  63. */
  64. int ff_rtp_check_and_send_back_rr(RTPDemuxContext *s, URLContext *fd,
  65. AVIOContext *avio, int count);
  66. int ff_rtp_send_rtcp_feedback(RTPDemuxContext *s, URLContext *fd,
  67. AVIOContext *avio);
  68. // these statistics are used for rtcp receiver reports...
  69. typedef struct RTPStatistics {
  70. uint16_t max_seq; ///< highest sequence number seen
  71. uint32_t cycles; ///< shifted count of sequence number cycles
  72. uint32_t base_seq; ///< base sequence number
  73. uint32_t bad_seq; ///< last bad sequence number + 1
  74. int probation; ///< sequence packets till source is valid
  75. uint32_t received; ///< packets received
  76. uint32_t expected_prior; ///< packets expected in last interval
  77. uint32_t received_prior; ///< packets received in last interval
  78. uint32_t transit; ///< relative transit time for previous packet
  79. uint32_t jitter; ///< estimated jitter.
  80. } RTPStatistics;
  81. #define RTP_FLAG_KEY 0x1 ///< RTP packet contains a keyframe
  82. #define RTP_FLAG_MARKER 0x2 ///< RTP marker bit was set for this packet
  83. /**
  84. * Packet parsing for "private" payloads in the RTP specs.
  85. *
  86. * @param ctx RTSP demuxer context
  87. * @param s stream context
  88. * @param st stream that this packet belongs to
  89. * @param pkt packet in which to write the parsed data
  90. * @param timestamp pointer to the RTP timestamp of the input data, can be
  91. * updated by the function if returning older, buffered data
  92. * @param buf pointer to raw RTP packet data
  93. * @param len length of buf
  94. * @param seq RTP sequence number of the packet
  95. * @param flags flags from the RTP packet header (RTP_FLAG_*)
  96. */
  97. typedef int (*DynamicPayloadPacketHandlerProc)(AVFormatContext *ctx,
  98. PayloadContext *s,
  99. AVStream *st, AVPacket *pkt,
  100. uint32_t *timestamp,
  101. const uint8_t * buf,
  102. int len, uint16_t seq, int flags);
  103. struct RTPDynamicProtocolHandler {
  104. const char *enc_name;
  105. enum AVMediaType codec_type;
  106. enum AVCodecID codec_id;
  107. enum AVStreamParseType need_parsing;
  108. int static_payload_id; /* 0 means no payload id is set. 0 is a valid
  109. * payload ID (PCMU), too, but that format doesn't
  110. * require any custom depacketization code. */
  111. int priv_data_size;
  112. /** Initialize dynamic protocol handler, called after the full rtpmap line is parsed, may be null */
  113. int (*init)(AVFormatContext *s, int st_index, PayloadContext *priv_data);
  114. /** Parse the a= line from the sdp field */
  115. int (*parse_sdp_a_line)(AVFormatContext *s, int st_index,
  116. PayloadContext *priv_data, const char *line);
  117. /** Free any data needed by the rtp parsing for this dynamic data.
  118. * Don't free the protocol_data pointer itself, that is freed by the
  119. * caller. This is called even if the init method failed. */
  120. void (*close)(PayloadContext *protocol_data);
  121. /** Parse handler for this dynamic packet */
  122. DynamicPayloadPacketHandlerProc parse_packet;
  123. int (*need_keyframe)(PayloadContext *context);
  124. struct RTPDynamicProtocolHandler *next;
  125. };
  126. typedef struct RTPPacket {
  127. uint16_t seq;
  128. uint8_t *buf;
  129. int len;
  130. int64_t recvtime;
  131. struct RTPPacket *next;
  132. } RTPPacket;
  133. struct RTPDemuxContext {
  134. AVFormatContext *ic;
  135. AVStream *st;
  136. int payload_type;
  137. uint32_t ssrc;
  138. uint16_t seq;
  139. uint32_t timestamp;
  140. uint32_t base_timestamp;
  141. int64_t unwrapped_timestamp;
  142. int64_t range_start_offset;
  143. int max_payload_size;
  144. /* used to send back RTCP RR */
  145. char hostname[256];
  146. int srtp_enabled;
  147. struct SRTPContext srtp;
  148. /** Statistics for this stream (used by RTCP receiver reports) */
  149. RTPStatistics statistics;
  150. /** Fields for packet reordering @{ */
  151. int prev_ret; ///< The return value of the actual parsing of the previous packet
  152. RTPPacket* queue; ///< A sorted queue of buffered packets not yet returned
  153. int queue_len; ///< The number of packets in queue
  154. int queue_size; ///< The size of queue, or 0 if reordering is disabled
  155. /*@}*/
  156. /* rtcp sender statistics receive */
  157. uint64_t last_rtcp_ntp_time;
  158. int64_t last_rtcp_reception_time;
  159. uint64_t first_rtcp_ntp_time;
  160. uint32_t last_rtcp_timestamp;
  161. int64_t rtcp_ts_offset;
  162. /* rtcp sender statistics */
  163. unsigned int packet_count;
  164. unsigned int octet_count;
  165. unsigned int last_octet_count;
  166. int64_t last_feedback_time;
  167. /* dynamic payload stuff */
  168. const RTPDynamicProtocolHandler *handler;
  169. PayloadContext *dynamic_protocol_context;
  170. };
  171. /**
  172. * Iterate over all registered rtp dynamic protocol handlers.
  173. *
  174. * @param opaque a pointer where libavformat will store the iteration state. Must
  175. * point to NULL to start the iteration.
  176. *
  177. * @return the next registered rtp dynamic protocol handler or NULL when the iteration is
  178. * finished
  179. */
  180. const RTPDynamicProtocolHandler *ff_rtp_handler_iterate(void **opaque);
  181. /**
  182. * Find a registered rtp dynamic protocol handler with the specified name.
  183. *
  184. * @param name name of the requested rtp dynamic protocol handler
  185. * @return A rtp dynamic protocol handler if one was found, NULL otherwise.
  186. */
  187. const RTPDynamicProtocolHandler *ff_rtp_handler_find_by_name(const char *name,
  188. enum AVMediaType codec_type);
  189. /**
  190. * Find a registered rtp dynamic protocol handler with a matching codec ID.
  191. *
  192. * @param id AVCodecID of the requested rtp dynamic protocol handler.
  193. * @return A rtp dynamic protocol handler if one was found, NULL otherwise.
  194. */
  195. const RTPDynamicProtocolHandler *ff_rtp_handler_find_by_id(int id,
  196. enum AVMediaType codec_type);
  197. /* from rtsp.c, but used by rtp dynamic protocol handlers. */
  198. int ff_rtsp_next_attr_and_value(const char **p, char *attr, int attr_size,
  199. char *value, int value_size);
  200. int ff_parse_fmtp(AVFormatContext *s,
  201. AVStream *stream, PayloadContext *data, const char *p,
  202. int (*parse_fmtp)(AVFormatContext *s,
  203. AVStream *stream,
  204. PayloadContext *data,
  205. const char *attr, const char *value));
  206. /**
  207. * Close the dynamic buffer and make a packet from it.
  208. */
  209. int ff_rtp_finalize_packet(AVPacket *pkt, AVIOContext **dyn_buf, int stream_idx);
  210. #endif /* AVFORMAT_RTPDEC_H */