rtsp.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /*
  2. * RTSP definitions
  3. * Copyright (c) 2002 Fabrice Bellard
  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 FFMPEG_RTSP_H
  22. #define FFMPEG_RTSP_H
  23. #include <stdint.h>
  24. #include "avformat.h"
  25. #include "rtspcodes.h"
  26. #include "rtpdec.h"
  27. #include "network.h"
  28. /**
  29. * Network layer over which RTP/etc packet data will be transported.
  30. */
  31. enum RTSPLowerTransport {
  32. RTSP_LOWER_TRANSPORT_UDP = 0, /**< UDP/unicast */
  33. RTSP_LOWER_TRANSPORT_TCP = 1, /**< TCP; interleaved in RTSP */
  34. RTSP_LOWER_TRANSPORT_UDP_MULTICAST = 2, /**< UDP/multicast */
  35. RTSP_LOWER_TRANSPORT_NB
  36. };
  37. /**
  38. * Packet profile of the data that we will be receiving. Real servers
  39. * commonly send RDT (although they can sometimes send RTP as well),
  40. * whereas most others will send RTP.
  41. */
  42. enum RTSPTransport {
  43. RTSP_TRANSPORT_RTP, /**< Standards-compliant RTP */
  44. RTSP_TRANSPORT_RDT, /**< Realmedia Data Transport */
  45. RTSP_TRANSPORT_NB
  46. };
  47. #define RTSP_DEFAULT_PORT 554
  48. #define RTSP_MAX_TRANSPORTS 8
  49. #define RTSP_TCP_MAX_PACKET_SIZE 1472
  50. #define RTSP_DEFAULT_NB_AUDIO_CHANNELS 2
  51. #define RTSP_DEFAULT_AUDIO_SAMPLERATE 44100
  52. #define RTSP_RTP_PORT_MIN 5000
  53. #define RTSP_RTP_PORT_MAX 10000
  54. /**
  55. * This describes a single item in the "Transport:" line of one stream as
  56. * negotiated by the SETUP RTSP command. Multiple transports are comma-
  57. * separated ("Transport: x-read-rdt/tcp;interleaved=0-1,rtp/avp/udp;
  58. * client_port=1000-1001;server_port=1800-1801") and described in separate
  59. * RTSPTransportFields.
  60. */
  61. typedef struct RTSPTransportField {
  62. /** interleave ids, if TCP transport; each TCP/RTSP data packet starts
  63. * with a '$', stream length and stream ID. If the stream ID is within
  64. * the range of this interleaved_min-max, then the packet belongs to
  65. * this stream. */
  66. int interleaved_min, interleaved_max;
  67. /** UDP multicast port range; the ports to which we should connect to
  68. * receive multicast UDP data. */
  69. int port_min, port_max;
  70. /** UDP client ports; these should be the local ports of the UDP RTP
  71. * (and RTCP) sockets over which we receive RTP/RTCP data. */
  72. int client_port_min, client_port_max;
  73. /** UDP unicast server port range; the ports to which we should connect
  74. * to receive unicast UDP RTP/RTCP data. */
  75. int server_port_min, server_port_max;
  76. /** time-to-live value (required for multicast); the amount of HOPs that
  77. * packets will be allowed to make before being discarded. */
  78. int ttl;
  79. uint32_t destination; /**< destination IP address */
  80. /** data/packet transport protocol; e.g. RTP or RDT */
  81. enum RTSPTransport transport;
  82. /** network layer transport protocol; e.g. TCP or UDP uni-/multicast */
  83. enum RTSPLowerTransport lower_transport;
  84. } RTSPTransportField;
  85. /**
  86. * This describes the server response to each RTSP command.
  87. */
  88. typedef struct RTSPMessageHeader {
  89. /** length of the data following this header */
  90. int content_length;
  91. enum RTSPStatusCode status_code; /**< response code from server */
  92. /** number of items in the 'transports' variable below */
  93. int nb_transports;
  94. /** Time range of the streams that the server will stream. In
  95. * AV_TIME_BASE unit, AV_NOPTS_VALUE if not used */
  96. int64_t range_start, range_end;
  97. /** describes the complete "Transport:" line of the server in response
  98. * to a SETUP RTSP command by the client */
  99. RTSPTransportField transports[RTSP_MAX_TRANSPORTS];
  100. int seq; /**< sequence number */
  101. /** the "Session:" field. This value is initially set by the server and
  102. * should be re-transmitted by the client in every RTSP command. */
  103. char session_id[512];
  104. /** the "RealChallenge1:" field from the server */
  105. char real_challenge[64];
  106. /** the "Server: field, which can be used to identify some special-case
  107. * servers that are not 100% standards-compliant. We use this to identify
  108. * Windows Media Server, which has a value "WMServer/v.e.r.sion", where
  109. * version is a sequence of digits (e.g. 9.0.0.3372). Helix/Real servers
  110. * use something like "Helix [..] Server Version v.e.r.sion (platform)
  111. * (RealServer compatible)" or "RealServer Version v.e.r.sion (platform)",
  112. * where platform is the output of $uname -msr | sed 's/ /-/g'. */
  113. char server[64];
  114. } RTSPMessageHeader;
  115. /**
  116. * Client state, i.e. whether we are currently receiving data (PLAYING) or
  117. * setup-but-not-receiving (PAUSED). State can be changed in applications
  118. * by calling av_read_play/pause().
  119. */
  120. enum RTSPClientState {
  121. RTSP_STATE_IDLE, /**< not initialized */
  122. RTSP_STATE_PLAYING, /**< initialized and receiving data */
  123. RTSP_STATE_PAUSED, /**< initialized, but not receiving data */
  124. };
  125. /**
  126. * Identifies particular servers that require special handling, such as
  127. * standards-incompliant "Transport:" lines in the SETUP request.
  128. */
  129. enum RTSPServerType {
  130. RTSP_SERVER_RTP, /**< Standards-compliant RTP-server */
  131. RTSP_SERVER_REAL, /**< Realmedia-style server */
  132. RTSP_SERVER_WMS, /**< Windows Media server */
  133. RTSP_SERVER_NB
  134. };
  135. /**
  136. * Private data for the RTSP demuxer.
  137. */
  138. typedef struct RTSPState {
  139. URLContext *rtsp_hd; /* RTSP TCP connexion handle */
  140. /** number of items in the 'rtsp_streams' variable */
  141. int nb_rtsp_streams;
  142. struct RTSPStream **rtsp_streams; /**< streams in this session */
  143. /** indicator of whether we are currently receiving data from the
  144. * server. Basically this isn't more than a simple cache of the
  145. * last PLAY/PAUSE command sent to the server, to make sure we don't
  146. * send 2x the same unexpectedly or commands in the wrong state. */
  147. enum RTSPClientState state;
  148. /** the seek value requested when calling av_seek_frame(). This value
  149. * is subsequently used as part of the "Range" parameter when emitting
  150. * the RTSP PLAY command. If we are currently playing, this command is
  151. * called instantly. If we are currently paused, this command is called
  152. * whenever we resume playback. Either way, the value is only used once,
  153. * see rtsp_read_play() and rtsp_read_seek(). */
  154. int64_t seek_timestamp;
  155. /* XXX: currently we use unbuffered input */
  156. // ByteIOContext rtsp_gb;
  157. int seq; /**< RTSP command sequence number */
  158. /** copy of RTSPMessageHeader->session_id, i.e. the server-provided session
  159. * identifier that the client should re-transmit in each RTSP command */
  160. char session_id[512];
  161. /** the negotiated data/packet transport protocol; e.g. RTP or RDT */
  162. enum RTSPTransport transport;
  163. /** the negotiated network layer transport protocol; e.g. TCP or UDP
  164. * uni-/multicast */
  165. enum RTSPLowerTransport lower_transport;
  166. /** brand of server that we're talking to; e.g. WMS, REAL or other.
  167. * Detected based on the value of RTSPMessageHeader->server or the presence
  168. * of RTSPMessageHeader->real_challenge */
  169. enum RTSPServerType server_type;
  170. /** The last reply of the server to a RTSP command */
  171. char last_reply[2048]; /* XXX: allocate ? */
  172. /** RTSPStream->transport_priv of the last stream that we read a
  173. * packet from */
  174. void *cur_transport_priv;
  175. /** The following are used for Real stream selection */
  176. //@{
  177. /** whether we need to send a "SET_PARAMETER Subscribe:" command */
  178. int need_subscription;
  179. /** stream setup during the last frame read. This is used to detect if
  180. * we need to subscribe or unsubscribe to any new streams. */
  181. enum AVDiscard real_setup_cache[MAX_STREAMS];
  182. /** the last value of the "SET_PARAMETER Subscribe:" RTSP command.
  183. * this is used to send the same "Unsubscribe:" if stream setup changed,
  184. * before sending a new "Subscribe:" command. */
  185. char last_subscription[1024];
  186. //@}
  187. } RTSPState;
  188. /**
  189. * Describes a single stream, as identified by a single m= line block in the
  190. * SDP content. In the case of RDT, one RTSPStream can represent multiple
  191. * AVStreams. In this case, each AVStream in this set has similar content
  192. * (but different codec/bitrate).
  193. */
  194. typedef struct RTSPStream {
  195. URLContext *rtp_handle; /**< RTP stream handle (if UDP) */
  196. void *transport_priv; /**< RTP/RDT parse context */
  197. /** corresponding stream index, if any. -1 if none (MPEG2TS case) */
  198. int stream_index;
  199. /** interleave IDs; copies of RTSPTransportField->interleaved_min/max
  200. * for the selected transport. Only used for TCP. */
  201. int interleaved_min, interleaved_max;
  202. char control_url[1024]; /**< url for this stream (from SDP) */
  203. /** The following are used only in SDP, not RTSP */
  204. //@{
  205. int sdp_port; /**< port (from SDP content) */
  206. struct in_addr sdp_ip; /**< IP address (from SDP content) */
  207. int sdp_ttl; /**< IP Time-To-Live (from SDP content) */
  208. int sdp_payload_type; /**< payload type */
  209. //@}
  210. /** rtp payload parsing infos from SDP (i.e. mapping between private
  211. * payload IDs and media-types (string), so that we can derive what
  212. * type of payload we're dealing with (and how to parse it). */
  213. RTPPayloadData rtp_payload_data;
  214. /** The following are used for dynamic protocols (rtp_*.c/rdt.c) */
  215. //@{
  216. /** handler structure */
  217. RTPDynamicProtocolHandler *dynamic_handler;
  218. /** private data associated with the dynamic protocol */
  219. PayloadContext *dynamic_protocol_context;
  220. //@}
  221. } RTSPStream;
  222. int rtsp_init(void);
  223. void rtsp_parse_line(RTSPMessageHeader *reply, const char *buf);
  224. #if LIBAVFORMAT_VERSION_INT < (53 << 16)
  225. extern int rtsp_default_protocols;
  226. #endif
  227. extern int rtsp_rtp_port_min;
  228. extern int rtsp_rtp_port_max;
  229. int rtsp_pause(AVFormatContext *s);
  230. int rtsp_resume(AVFormatContext *s);
  231. #endif /* FFMPEG_RTSP_H */