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.

438 lines
17KB

  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. #include "httpauth.h"
  29. /**
  30. * Network layer over which RTP/etc packet data will be transported.
  31. */
  32. enum RTSPLowerTransport {
  33. RTSP_LOWER_TRANSPORT_UDP = 0, /**< UDP/unicast */
  34. RTSP_LOWER_TRANSPORT_TCP = 1, /**< TCP; interleaved in RTSP */
  35. RTSP_LOWER_TRANSPORT_UDP_MULTICAST = 2, /**< UDP/multicast */
  36. RTSP_LOWER_TRANSPORT_NB
  37. };
  38. /**
  39. * Packet profile of the data that we will be receiving. Real servers
  40. * commonly send RDT (although they can sometimes send RTP as well),
  41. * whereas most others will send RTP.
  42. */
  43. enum RTSPTransport {
  44. RTSP_TRANSPORT_RTP, /**< Standards-compliant RTP */
  45. RTSP_TRANSPORT_RDT, /**< Realmedia Data Transport */
  46. RTSP_TRANSPORT_NB
  47. };
  48. #define RTSP_DEFAULT_PORT 554
  49. #define RTSP_MAX_TRANSPORTS 8
  50. #define RTSP_TCP_MAX_PACKET_SIZE 1472
  51. #define RTSP_DEFAULT_NB_AUDIO_CHANNELS 2
  52. #define RTSP_DEFAULT_AUDIO_SAMPLERATE 44100
  53. #define RTSP_RTP_PORT_MIN 5000
  54. #define RTSP_RTP_PORT_MAX 10000
  55. /**
  56. * This describes a single item in the "Transport:" line of one stream as
  57. * negotiated by the SETUP RTSP command. Multiple transports are comma-
  58. * separated ("Transport: x-read-rdt/tcp;interleaved=0-1,rtp/avp/udp;
  59. * client_port=1000-1001;server_port=1800-1801") and described in separate
  60. * RTSPTransportFields.
  61. */
  62. typedef struct RTSPTransportField {
  63. /** interleave ids, if TCP transport; each TCP/RTSP data packet starts
  64. * with a '$', stream length and stream ID. If the stream ID is within
  65. * the range of this interleaved_min-max, then the packet belongs to
  66. * this stream. */
  67. int interleaved_min, interleaved_max;
  68. /** UDP multicast port range; the ports to which we should connect to
  69. * receive multicast UDP data. */
  70. int port_min, port_max;
  71. /** UDP client ports; these should be the local ports of the UDP RTP
  72. * (and RTCP) sockets over which we receive RTP/RTCP data. */
  73. int client_port_min, client_port_max;
  74. /** UDP unicast server port range; the ports to which we should connect
  75. * to receive unicast UDP RTP/RTCP data. */
  76. int server_port_min, server_port_max;
  77. /** time-to-live value (required for multicast); the amount of HOPs that
  78. * packets will be allowed to make before being discarded. */
  79. int ttl;
  80. uint32_t destination; /**< destination IP address */
  81. /** data/packet transport protocol; e.g. RTP or RDT */
  82. enum RTSPTransport transport;
  83. /** network layer transport protocol; e.g. TCP or UDP uni-/multicast */
  84. enum RTSPLowerTransport lower_transport;
  85. } RTSPTransportField;
  86. /**
  87. * This describes the server response to each RTSP command.
  88. */
  89. typedef struct RTSPMessageHeader {
  90. /** length of the data following this header */
  91. int content_length;
  92. enum RTSPStatusCode status_code; /**< response code from server */
  93. /** number of items in the 'transports' variable below */
  94. int nb_transports;
  95. /** Time range of the streams that the server will stream. In
  96. * AV_TIME_BASE unit, AV_NOPTS_VALUE if not used */
  97. int64_t range_start, range_end;
  98. /** describes the complete "Transport:" line of the server in response
  99. * to a SETUP RTSP command by the client */
  100. RTSPTransportField transports[RTSP_MAX_TRANSPORTS];
  101. int seq; /**< sequence number */
  102. /** the "Session:" field. This value is initially set by the server and
  103. * should be re-transmitted by the client in every RTSP command. */
  104. char session_id[512];
  105. /** the "Location:" field. This value is used to handle redirection.
  106. */
  107. char location[4096];
  108. /** the "RealChallenge1:" field from the server */
  109. char real_challenge[64];
  110. /** the "Server: field, which can be used to identify some special-case
  111. * servers that are not 100% standards-compliant. We use this to identify
  112. * Windows Media Server, which has a value "WMServer/v.e.r.sion", where
  113. * version is a sequence of digits (e.g. 9.0.0.3372). Helix/Real servers
  114. * use something like "Helix [..] Server Version v.e.r.sion (platform)
  115. * (RealServer compatible)" or "RealServer Version v.e.r.sion (platform)",
  116. * where platform is the output of $uname -msr | sed 's/ /-/g'. */
  117. char server[64];
  118. /** The "timeout" comes as part of the server response to the "SETUP"
  119. * command, in the "Session: <xyz>[;timeout=<value>]" line. It is the
  120. * time, in seconds, that the server will go without traffic over the
  121. * RTSP/TCP connection before it closes the connection. To prevent
  122. * this, sent dummy requests (e.g. OPTIONS) with intervals smaller
  123. * than this value. */
  124. int timeout;
  125. /** The "Notice" or "X-Notice" field value. See
  126. * http://tools.ietf.org/html/draft-stiemerling-rtsp-announce-00
  127. * for a complete list of supported values. */
  128. int notice;
  129. } RTSPMessageHeader;
  130. /**
  131. * Client state, i.e. whether we are currently receiving data (PLAYING) or
  132. * setup-but-not-receiving (PAUSED). State can be changed in applications
  133. * by calling av_read_play/pause().
  134. */
  135. enum RTSPClientState {
  136. RTSP_STATE_IDLE, /**< not initialized */
  137. RTSP_STATE_STREAMING, /**< initialized and sending/receiving data */
  138. RTSP_STATE_PAUSED, /**< initialized, but not receiving data */
  139. RTSP_STATE_SEEKING, /**< initialized, requesting a seek */
  140. };
  141. /**
  142. * Identifies particular servers that require special handling, such as
  143. * standards-incompliant "Transport:" lines in the SETUP request.
  144. */
  145. enum RTSPServerType {
  146. RTSP_SERVER_RTP, /**< Standards-compliant RTP-server */
  147. RTSP_SERVER_REAL, /**< Realmedia-style server */
  148. RTSP_SERVER_WMS, /**< Windows Media server */
  149. RTSP_SERVER_NB
  150. };
  151. /**
  152. * Private data for the RTSP demuxer.
  153. *
  154. * @todo Use ByteIOContext instead of URLContext
  155. */
  156. typedef struct RTSPState {
  157. URLContext *rtsp_hd; /* RTSP TCP connexion handle */
  158. /** number of items in the 'rtsp_streams' variable */
  159. int nb_rtsp_streams;
  160. struct RTSPStream **rtsp_streams; /**< streams in this session */
  161. /** indicator of whether we are currently receiving data from the
  162. * server. Basically this isn't more than a simple cache of the
  163. * last PLAY/PAUSE command sent to the server, to make sure we don't
  164. * send 2x the same unexpectedly or commands in the wrong state. */
  165. enum RTSPClientState state;
  166. /** the seek value requested when calling av_seek_frame(). This value
  167. * is subsequently used as part of the "Range" parameter when emitting
  168. * the RTSP PLAY command. If we are currently playing, this command is
  169. * called instantly. If we are currently paused, this command is called
  170. * whenever we resume playback. Either way, the value is only used once,
  171. * see rtsp_read_play() and rtsp_read_seek(). */
  172. int64_t seek_timestamp;
  173. /* XXX: currently we use unbuffered input */
  174. // ByteIOContext rtsp_gb;
  175. int seq; /**< RTSP command sequence number */
  176. /** copy of RTSPMessageHeader->session_id, i.e. the server-provided session
  177. * identifier that the client should re-transmit in each RTSP command */
  178. char session_id[512];
  179. /** copy of RTSPMessageHeader->timeout, i.e. the time (in seconds) that
  180. * the server will go without traffic on the RTSP/TCP line before it
  181. * closes the connection. */
  182. int timeout;
  183. /** timestamp of the last RTSP command that we sent to the RTSP server.
  184. * This is used to calculate when to send dummy commands to keep the
  185. * connection alive, in conjunction with timeout. */
  186. int64_t last_cmd_time;
  187. /** the negotiated data/packet transport protocol; e.g. RTP or RDT */
  188. enum RTSPTransport transport;
  189. /** the negotiated network layer transport protocol; e.g. TCP or UDP
  190. * uni-/multicast */
  191. enum RTSPLowerTransport lower_transport;
  192. /** brand of server that we're talking to; e.g. WMS, REAL or other.
  193. * Detected based on the value of RTSPMessageHeader->server or the presence
  194. * of RTSPMessageHeader->real_challenge */
  195. enum RTSPServerType server_type;
  196. /** plaintext authorization line (username:password) */
  197. char auth[128];
  198. /** authentication state */
  199. HTTPAuthState auth_state;
  200. /** The last reply of the server to a RTSP command */
  201. char last_reply[2048]; /* XXX: allocate ? */
  202. /** RTSPStream->transport_priv of the last stream that we read a
  203. * packet from */
  204. void *cur_transport_priv;
  205. /** The following are used for Real stream selection */
  206. //@{
  207. /** whether we need to send a "SET_PARAMETER Subscribe:" command */
  208. int need_subscription;
  209. /** stream setup during the last frame read. This is used to detect if
  210. * we need to subscribe or unsubscribe to any new streams. */
  211. enum AVDiscard real_setup_cache[MAX_STREAMS];
  212. /** the last value of the "SET_PARAMETER Subscribe:" RTSP command.
  213. * this is used to send the same "Unsubscribe:" if stream setup changed,
  214. * before sending a new "Subscribe:" command. */
  215. char last_subscription[1024];
  216. //@}
  217. /** The following are used for RTP/ASF streams */
  218. //@{
  219. /** ASF demuxer context for the embedded ASF stream from WMS servers */
  220. AVFormatContext *asf_ctx;
  221. /** cache for position of the asf demuxer, since we load a new
  222. * data packet in the bytecontext for each incoming RTSP packet. */
  223. uint64_t asf_pb_pos;
  224. //@}
  225. /** some MS RTSP streams contain a URL in the SDP that we need to use
  226. * for all subsequent RTSP requests, rather than the input URI; in
  227. * other cases, this is a copy of AVFormatContext->filename. */
  228. char control_uri[1024];
  229. /** The synchronized start time of the output streams. */
  230. int64_t start_time;
  231. } RTSPState;
  232. /**
  233. * Describes a single stream, as identified by a single m= line block in the
  234. * SDP content. In the case of RDT, one RTSPStream can represent multiple
  235. * AVStreams. In this case, each AVStream in this set has similar content
  236. * (but different codec/bitrate).
  237. */
  238. typedef struct RTSPStream {
  239. URLContext *rtp_handle; /**< RTP stream handle (if UDP) */
  240. void *transport_priv; /**< RTP/RDT parse context if input, RTP AVFormatContext if output */
  241. /** corresponding stream index, if any. -1 if none (MPEG2TS case) */
  242. int stream_index;
  243. /** interleave IDs; copies of RTSPTransportField->interleaved_min/max
  244. * for the selected transport. Only used for TCP. */
  245. int interleaved_min, interleaved_max;
  246. char control_url[1024]; /**< url for this stream (from SDP) */
  247. /** The following are used only in SDP, not RTSP */
  248. //@{
  249. int sdp_port; /**< port (from SDP content) */
  250. struct in_addr sdp_ip; /**< IP address (from SDP content) */
  251. int sdp_ttl; /**< IP Time-To-Live (from SDP content) */
  252. int sdp_payload_type; /**< payload type */
  253. //@}
  254. /** rtp payload parsing infos from SDP (i.e. mapping between private
  255. * payload IDs and media-types (string), so that we can derive what
  256. * type of payload we're dealing with (and how to parse it). */
  257. RTPPayloadData rtp_payload_data;
  258. /** The following are used for dynamic protocols (rtp_*.c/rdt.c) */
  259. //@{
  260. /** handler structure */
  261. RTPDynamicProtocolHandler *dynamic_handler;
  262. /** private data associated with the dynamic protocol */
  263. PayloadContext *dynamic_protocol_context;
  264. //@}
  265. } RTSPStream;
  266. void ff_rtsp_parse_line(RTSPMessageHeader *reply, const char *buf,
  267. HTTPAuthState *auth_state);
  268. #if LIBAVFORMAT_VERSION_INT < (53 << 16)
  269. extern int rtsp_default_protocols;
  270. #endif
  271. extern int rtsp_rtp_port_min;
  272. extern int rtsp_rtp_port_max;
  273. /**
  274. * Send a command to the RTSP server without waiting for the reply.
  275. *
  276. * @param s RTSP (de)muxer context
  277. * @param method the method for the request
  278. * @param url the target url for the request
  279. * @param headers extra header lines to include in the request
  280. * @param send_content if non-null, the data to send as request body content
  281. * @param send_content_length the length of the send_content data, or 0 if
  282. * send_content is null
  283. */
  284. void ff_rtsp_send_cmd_with_content_async(AVFormatContext *s,
  285. const char *method, const char *url,
  286. const char *headers,
  287. const unsigned char *send_content,
  288. int send_content_length);
  289. /**
  290. * Send a command to the RTSP server without waiting for the reply.
  291. *
  292. * @see rtsp_send_cmd_with_content_async
  293. */
  294. void ff_rtsp_send_cmd_async(AVFormatContext *s, const char *method,
  295. const char *url, const char *headers);
  296. /**
  297. * Send a command to the RTSP server and wait for the reply.
  298. *
  299. * @param s RTSP (de)muxer context
  300. * @param method the method for the request
  301. * @param url the target url for the request
  302. * @param headers extra header lines to include in the request
  303. * @param reply pointer where the RTSP message header will be stored
  304. * @param content_ptr pointer where the RTSP message body, if any, will
  305. * be stored (length is in reply)
  306. * @param send_content if non-null, the data to send as request body content
  307. * @param send_content_length the length of the send_content data, or 0 if
  308. * send_content is null
  309. */
  310. void ff_rtsp_send_cmd_with_content(AVFormatContext *s,
  311. const char *method, const char *url,
  312. const char *headers,
  313. RTSPMessageHeader *reply,
  314. unsigned char **content_ptr,
  315. const unsigned char *send_content,
  316. int send_content_length);
  317. /**
  318. * Send a command to the RTSP server and wait for the reply.
  319. *
  320. * @see rtsp_send_cmd_with_content
  321. */
  322. void ff_rtsp_send_cmd(AVFormatContext *s, const char *method,
  323. const char *url, const char *headers,
  324. RTSPMessageHeader *reply, unsigned char **content_ptr);
  325. /**
  326. * Read a RTSP message from the server, or prepare to read data
  327. * packets if we're reading data interleaved over the TCP/RTSP
  328. * connection as well.
  329. *
  330. * @param s RTSP (de)muxer context
  331. * @param reply pointer where the RTSP message header will be stored
  332. * @param content_ptr pointer where the RTSP message body, if any, will
  333. * be stored (length is in reply)
  334. * @param return_on_interleaved_data whether the function may return if we
  335. * encounter a data marker ('$'), which precedes data
  336. * packets over interleaved TCP/RTSP connections. If this
  337. * is set, this function will return 1 after encountering
  338. * a '$'. If it is not set, the function will skip any
  339. * data packets (if they are encountered), until a reply
  340. * has been fully parsed. If no more data is available
  341. * without parsing a reply, it will return an error.
  342. *
  343. * @returns 1 if a data packets is ready to be received, -1 on error,
  344. * and 0 on success.
  345. */
  346. int ff_rtsp_read_reply(AVFormatContext *s, RTSPMessageHeader *reply,
  347. unsigned char **content_ptr,
  348. int return_on_interleaved_data);
  349. /**
  350. * Skip a RTP/TCP interleaved packet.
  351. */
  352. void ff_rtsp_skip_packet(AVFormatContext *s);
  353. /**
  354. * Connect to the RTSP server and set up the individual media streams.
  355. * This can be used for both muxers and demuxers.
  356. *
  357. * @param s RTSP (de)muxer context
  358. *
  359. * @returns 0 on success, < 0 on error. Cleans up all allocations done
  360. * within the function on error.
  361. */
  362. int ff_rtsp_connect(AVFormatContext *s);
  363. /**
  364. * Close and free all streams within the RTSP (de)muxer
  365. *
  366. * @param s RTSP (de)muxer context
  367. */
  368. void ff_rtsp_close_streams(AVFormatContext *s);
  369. #endif /* AVFORMAT_RTSP_H */