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.

417 lines
16KB

  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 AVFORMAT_RTSP_H
  22. #define AVFORMAT_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 "Location:" field. This value is used to handle redirection.
  105. */
  106. char location[4096];
  107. /** the "RealChallenge1:" field from the server */
  108. char real_challenge[64];
  109. /** the "Server: field, which can be used to identify some special-case
  110. * servers that are not 100% standards-compliant. We use this to identify
  111. * Windows Media Server, which has a value "WMServer/v.e.r.sion", where
  112. * version is a sequence of digits (e.g. 9.0.0.3372). Helix/Real servers
  113. * use something like "Helix [..] Server Version v.e.r.sion (platform)
  114. * (RealServer compatible)" or "RealServer Version v.e.r.sion (platform)",
  115. * where platform is the output of $uname -msr | sed 's/ /-/g'. */
  116. char server[64];
  117. /** The "timeout" comes as part of the server response to the "SETUP"
  118. * command, in the "Session: <xyz>[;timeout=<value>]" line. It is the
  119. * time, in seconds, that the server will go without traffic over the
  120. * RTSP/TCP connection before it closes the connection. To prevent
  121. * this, sent dummy requests (e.g. OPTIONS) with intervals smaller
  122. * than this value. */
  123. int timeout;
  124. /** The "Notice" or "X-Notice" field value. See
  125. * http://tools.ietf.org/html/draft-stiemerling-rtsp-announce-00
  126. * for a complete list of supported values. */
  127. int notice;
  128. } RTSPMessageHeader;
  129. /**
  130. * Client state, i.e. whether we are currently receiving data (PLAYING) or
  131. * setup-but-not-receiving (PAUSED). State can be changed in applications
  132. * by calling av_read_play/pause().
  133. */
  134. enum RTSPClientState {
  135. RTSP_STATE_IDLE, /**< not initialized */
  136. RTSP_STATE_STREAMING, /**< initialized and sending/receiving data */
  137. RTSP_STATE_PAUSED, /**< initialized, but not receiving data */
  138. RTSP_STATE_SEEKING, /**< initialized, requesting a seek */
  139. };
  140. /**
  141. * Identifies particular servers that require special handling, such as
  142. * standards-incompliant "Transport:" lines in the SETUP request.
  143. */
  144. enum RTSPServerType {
  145. RTSP_SERVER_RTP, /**< Standards-compliant RTP-server */
  146. RTSP_SERVER_REAL, /**< Realmedia-style server */
  147. RTSP_SERVER_WMS, /**< Windows Media server */
  148. RTSP_SERVER_NB
  149. };
  150. /**
  151. * Private data for the RTSP demuxer.
  152. *
  153. * @todo Use ByteIOContext instead of URLContext
  154. */
  155. typedef struct RTSPState {
  156. URLContext *rtsp_hd; /* RTSP TCP connexion handle */
  157. /** number of items in the 'rtsp_streams' variable */
  158. int nb_rtsp_streams;
  159. struct RTSPStream **rtsp_streams; /**< streams in this session */
  160. /** indicator of whether we are currently receiving data from the
  161. * server. Basically this isn't more than a simple cache of the
  162. * last PLAY/PAUSE command sent to the server, to make sure we don't
  163. * send 2x the same unexpectedly or commands in the wrong state. */
  164. enum RTSPClientState state;
  165. /** the seek value requested when calling av_seek_frame(). This value
  166. * is subsequently used as part of the "Range" parameter when emitting
  167. * the RTSP PLAY command. If we are currently playing, this command is
  168. * called instantly. If we are currently paused, this command is called
  169. * whenever we resume playback. Either way, the value is only used once,
  170. * see rtsp_read_play() and rtsp_read_seek(). */
  171. int64_t seek_timestamp;
  172. /* XXX: currently we use unbuffered input */
  173. // ByteIOContext rtsp_gb;
  174. int seq; /**< RTSP command sequence number */
  175. /** copy of RTSPMessageHeader->session_id, i.e. the server-provided session
  176. * identifier that the client should re-transmit in each RTSP command */
  177. char session_id[512];
  178. /** copy of RTSPMessageHeader->timeout, i.e. the time (in seconds) that
  179. * the server will go without traffic on the RTSP/TCP line before it
  180. * closes the connection. */
  181. int timeout;
  182. /** timestamp of the last RTSP command that we sent to the RTSP server.
  183. * This is used to calculate when to send dummy commands to keep the
  184. * connection alive, in conjunction with timeout. */
  185. int64_t last_cmd_time;
  186. /** the negotiated data/packet transport protocol; e.g. RTP or RDT */
  187. enum RTSPTransport transport;
  188. /** the negotiated network layer transport protocol; e.g. TCP or UDP
  189. * uni-/multicast */
  190. enum RTSPLowerTransport lower_transport;
  191. /** brand of server that we're talking to; e.g. WMS, REAL or other.
  192. * Detected based on the value of RTSPMessageHeader->server or the presence
  193. * of RTSPMessageHeader->real_challenge */
  194. enum RTSPServerType server_type;
  195. /** base64-encoded authorization lines (username:password) */
  196. char *auth_b64;
  197. /** The last reply of the server to a RTSP command */
  198. char last_reply[2048]; /* XXX: allocate ? */
  199. /** RTSPStream->transport_priv of the last stream that we read a
  200. * packet from */
  201. void *cur_transport_priv;
  202. /** The following are used for Real stream selection */
  203. //@{
  204. /** whether we need to send a "SET_PARAMETER Subscribe:" command */
  205. int need_subscription;
  206. /** stream setup during the last frame read. This is used to detect if
  207. * we need to subscribe or unsubscribe to any new streams. */
  208. enum AVDiscard real_setup_cache[MAX_STREAMS];
  209. /** the last value of the "SET_PARAMETER Subscribe:" RTSP command.
  210. * this is used to send the same "Unsubscribe:" if stream setup changed,
  211. * before sending a new "Subscribe:" command. */
  212. char last_subscription[1024];
  213. //@}
  214. /** The following are used for RTP/ASF streams */
  215. //@{
  216. /** ASF demuxer context for the embedded ASF stream from WMS servers */
  217. AVFormatContext *asf_ctx;
  218. /** cache for position of the asf demuxer, since we load a new
  219. * data packet in the bytecontext for each incoming RTSP packet. */
  220. uint64_t asf_pb_pos;
  221. //@}
  222. /** some MS RTSP streams contain a URL in the SDP that we need to use
  223. * for all subsequent RTSP requests, rather than the input URI; in
  224. * other cases, this is a copy of AVFormatContext->filename. */
  225. char control_uri[1024];
  226. } RTSPState;
  227. /**
  228. * Describes a single stream, as identified by a single m= line block in the
  229. * SDP content. In the case of RDT, one RTSPStream can represent multiple
  230. * AVStreams. In this case, each AVStream in this set has similar content
  231. * (but different codec/bitrate).
  232. */
  233. typedef struct RTSPStream {
  234. URLContext *rtp_handle; /**< RTP stream handle (if UDP) */
  235. void *transport_priv; /**< RTP/RDT parse context if input, RTP AVFormatContext if output */
  236. /** corresponding stream index, if any. -1 if none (MPEG2TS case) */
  237. int stream_index;
  238. /** interleave IDs; copies of RTSPTransportField->interleaved_min/max
  239. * for the selected transport. Only used for TCP. */
  240. int interleaved_min, interleaved_max;
  241. char control_url[1024]; /**< url for this stream (from SDP) */
  242. /** The following are used only in SDP, not RTSP */
  243. //@{
  244. int sdp_port; /**< port (from SDP content) */
  245. struct in_addr sdp_ip; /**< IP address (from SDP content) */
  246. int sdp_ttl; /**< IP Time-To-Live (from SDP content) */
  247. int sdp_payload_type; /**< payload type */
  248. //@}
  249. /** rtp payload parsing infos from SDP (i.e. mapping between private
  250. * payload IDs and media-types (string), so that we can derive what
  251. * type of payload we're dealing with (and how to parse it). */
  252. RTPPayloadData rtp_payload_data;
  253. /** The following are used for dynamic protocols (rtp_*.c/rdt.c) */
  254. //@{
  255. /** handler structure */
  256. RTPDynamicProtocolHandler *dynamic_handler;
  257. /** private data associated with the dynamic protocol */
  258. PayloadContext *dynamic_protocol_context;
  259. //@}
  260. } RTSPStream;
  261. void ff_rtsp_parse_line(RTSPMessageHeader *reply, const char *buf);
  262. #if LIBAVFORMAT_VERSION_INT < (53 << 16)
  263. extern int rtsp_default_protocols;
  264. #endif
  265. extern int rtsp_rtp_port_min;
  266. extern int rtsp_rtp_port_max;
  267. /**
  268. * Send a command to the RTSP server without waiting for the reply.
  269. *
  270. * @param s RTSP (de)muxer context
  271. * @param cmd the full first line of the request
  272. * @param send_content if non-null, the data to send as request body content
  273. * @param send_content_length the length of the send_content data, or 0 if
  274. * send_content is null
  275. */
  276. void ff_rtsp_send_cmd_with_content_async(AVFormatContext *s,
  277. const char *cmd,
  278. const unsigned char *send_content,
  279. int send_content_length);
  280. /**
  281. * Send a command to the RTSP server without waiting for the reply.
  282. *
  283. * @see rtsp_send_cmd_with_content_async
  284. */
  285. void ff_rtsp_send_cmd_async(AVFormatContext *s, const char *cmd);
  286. /**
  287. * Send a command to the RTSP server and wait for the reply.
  288. *
  289. * @param s RTSP (de)muxer context
  290. * @param cmd the full first line of the request
  291. * @param reply pointer where the RTSP message header will be stored
  292. * @param content_ptr pointer where the RTSP message body, if any, will
  293. * be stored (length is in reply)
  294. * @param send_content if non-null, the data to send as request body content
  295. * @param send_content_length the length of the send_content data, or 0 if
  296. * send_content is null
  297. */
  298. void ff_rtsp_send_cmd_with_content(AVFormatContext *s,
  299. const char *cmd,
  300. RTSPMessageHeader *reply,
  301. unsigned char **content_ptr,
  302. const unsigned char *send_content,
  303. int send_content_length);
  304. /**
  305. * Send a command to the RTSP server and wait for the reply.
  306. *
  307. * @see rtsp_send_cmd_with_content
  308. */
  309. void ff_rtsp_send_cmd(AVFormatContext *s, const char *cmd,
  310. RTSPMessageHeader *reply, unsigned char **content_ptr);
  311. /**
  312. * Read a RTSP message from the server, or prepare to read data
  313. * packets if we're reading data interleaved over the TCP/RTSP
  314. * connection as well.
  315. *
  316. * @param s RTSP (de)muxer context
  317. * @param reply pointer where the RTSP message header will be stored
  318. * @param content_ptr pointer where the RTSP message body, if any, will
  319. * be stored (length is in reply)
  320. * @param return_on_interleaved_data whether the function may return if we
  321. * encounter a data marker ('$'), which precedes data
  322. * packets over interleaved TCP/RTSP connections. If this
  323. * is set, this function will return 1 after encountering
  324. * a '$'. If it is not set, the function will skip any
  325. * data packets (if they are encountered), until a reply
  326. * has been fully parsed. If no more data is available
  327. * without parsing a reply, it will return an error.
  328. *
  329. * @returns 1 if a data packets is ready to be received, -1 on error,
  330. * and 0 on success.
  331. */
  332. int ff_rtsp_read_reply(AVFormatContext *s, RTSPMessageHeader *reply,
  333. unsigned char **content_ptr,
  334. int return_on_interleaved_data);
  335. /**
  336. * Connect to the RTSP server and set up the individual media streams.
  337. * This can be used for both muxers and demuxers.
  338. *
  339. * @param s RTSP (de)muxer context
  340. *
  341. * @returns 0 on success, < 0 on error. Cleans up all allocations done
  342. * within the function on error.
  343. */
  344. int ff_rtsp_connect(AVFormatContext *s);
  345. /**
  346. * Close and free all streams within the RTSP (de)muxer
  347. *
  348. * @param s RTSP (de)muxer context
  349. */
  350. void ff_rtsp_close_streams(AVFormatContext *s);
  351. #endif /* AVFORMAT_RTSP_H */