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.

309 lines
11KB

  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. /** The "timeout" comes as part of the server response to the "SETUP"
  115. * command, in the "Session: <xyz>[;timeout=<value>]" line. It is the
  116. * time, in seconds, that the server will go without traffic over the
  117. * RTSP/TCP connection before it closes the connection. To prevent
  118. * this, sent dummy requests (e.g. OPTIONS) with intervals smaller
  119. * than this value. */
  120. int timeout;
  121. } RTSPMessageHeader;
  122. /**
  123. * Client state, i.e. whether we are currently receiving data (PLAYING) or
  124. * setup-but-not-receiving (PAUSED). State can be changed in applications
  125. * by calling av_read_play/pause().
  126. */
  127. enum RTSPClientState {
  128. RTSP_STATE_IDLE, /**< not initialized */
  129. RTSP_STATE_PLAYING, /**< initialized and receiving data */
  130. RTSP_STATE_PAUSED, /**< initialized, but not receiving data */
  131. };
  132. /**
  133. * Identifies particular servers that require special handling, such as
  134. * standards-incompliant "Transport:" lines in the SETUP request.
  135. */
  136. enum RTSPServerType {
  137. RTSP_SERVER_RTP, /**< Standards-compliant RTP-server */
  138. RTSP_SERVER_REAL, /**< Realmedia-style server */
  139. RTSP_SERVER_WMS, /**< Windows Media server */
  140. RTSP_SERVER_NB
  141. };
  142. /**
  143. * Private data for the RTSP demuxer.
  144. *
  145. * @todo Use ByteIOContext instead of URLContext
  146. */
  147. typedef struct RTSPState {
  148. URLContext *rtsp_hd; /* RTSP TCP connexion handle */
  149. /** number of items in the 'rtsp_streams' variable */
  150. int nb_rtsp_streams;
  151. struct RTSPStream **rtsp_streams; /**< streams in this session */
  152. /** indicator of whether we are currently receiving data from the
  153. * server. Basically this isn't more than a simple cache of the
  154. * last PLAY/PAUSE command sent to the server, to make sure we don't
  155. * send 2x the same unexpectedly or commands in the wrong state. */
  156. enum RTSPClientState state;
  157. /** the seek value requested when calling av_seek_frame(). This value
  158. * is subsequently used as part of the "Range" parameter when emitting
  159. * the RTSP PLAY command. If we are currently playing, this command is
  160. * called instantly. If we are currently paused, this command is called
  161. * whenever we resume playback. Either way, the value is only used once,
  162. * see rtsp_read_play() and rtsp_read_seek(). */
  163. int64_t seek_timestamp;
  164. /* XXX: currently we use unbuffered input */
  165. // ByteIOContext rtsp_gb;
  166. int seq; /**< RTSP command sequence number */
  167. /** copy of RTSPMessageHeader->session_id, i.e. the server-provided session
  168. * identifier that the client should re-transmit in each RTSP command */
  169. char session_id[512];
  170. /** copy of RTSPMessageHeader->timeout, i.e. the time (in seconds) that
  171. * the server will go without traffic on the RTSP/TCP line before it
  172. * closes the connection. */
  173. int timeout;
  174. /** timestamp of the last RTSP command that we sent to the RTSP server.
  175. * This is used to calculate when to send dummy commands to keep the
  176. * connection alive, in conjunction with \p timeout. */
  177. int64_t last_cmd_time;
  178. /** the negotiated data/packet transport protocol; e.g. RTP or RDT */
  179. enum RTSPTransport transport;
  180. /** the negotiated network layer transport protocol; e.g. TCP or UDP
  181. * uni-/multicast */
  182. enum RTSPLowerTransport lower_transport;
  183. /** brand of server that we're talking to; e.g. WMS, REAL or other.
  184. * Detected based on the value of RTSPMessageHeader->server or the presence
  185. * of RTSPMessageHeader->real_challenge */
  186. enum RTSPServerType server_type;
  187. /** The last reply of the server to a RTSP command */
  188. char last_reply[2048]; /* XXX: allocate ? */
  189. /** RTSPStream->transport_priv of the last stream that we read a
  190. * packet from */
  191. void *cur_transport_priv;
  192. /** The following are used for Real stream selection */
  193. //@{
  194. /** whether we need to send a "SET_PARAMETER Subscribe:" command */
  195. int need_subscription;
  196. /** stream setup during the last frame read. This is used to detect if
  197. * we need to subscribe or unsubscribe to any new streams. */
  198. enum AVDiscard real_setup_cache[MAX_STREAMS];
  199. /** the last value of the "SET_PARAMETER Subscribe:" RTSP command.
  200. * this is used to send the same "Unsubscribe:" if stream setup changed,
  201. * before sending a new "Subscribe:" command. */
  202. char last_subscription[1024];
  203. //@}
  204. /** The following are used for RTP/ASF streams */
  205. //@{
  206. /** ASF demuxer context for the embedded ASF stream from WMS servers */
  207. AVFormatContext *asf_ctx;
  208. //@}
  209. } RTSPState;
  210. /**
  211. * Describes a single stream, as identified by a single m= line block in the
  212. * SDP content. In the case of RDT, one RTSPStream can represent multiple
  213. * AVStreams. In this case, each AVStream in this set has similar content
  214. * (but different codec/bitrate).
  215. */
  216. typedef struct RTSPStream {
  217. URLContext *rtp_handle; /**< RTP stream handle (if UDP) */
  218. void *transport_priv; /**< RTP/RDT parse context */
  219. /** corresponding stream index, if any. -1 if none (MPEG2TS case) */
  220. int stream_index;
  221. /** interleave IDs; copies of RTSPTransportField->interleaved_min/max
  222. * for the selected transport. Only used for TCP. */
  223. int interleaved_min, interleaved_max;
  224. char control_url[1024]; /**< url for this stream (from SDP) */
  225. /** The following are used only in SDP, not RTSP */
  226. //@{
  227. int sdp_port; /**< port (from SDP content) */
  228. struct in_addr sdp_ip; /**< IP address (from SDP content) */
  229. int sdp_ttl; /**< IP Time-To-Live (from SDP content) */
  230. int sdp_payload_type; /**< payload type */
  231. //@}
  232. /** rtp payload parsing infos from SDP (i.e. mapping between private
  233. * payload IDs and media-types (string), so that we can derive what
  234. * type of payload we're dealing with (and how to parse it). */
  235. RTPPayloadData rtp_payload_data;
  236. /** The following are used for dynamic protocols (rtp_*.c/rdt.c) */
  237. //@{
  238. /** handler structure */
  239. RTPDynamicProtocolHandler *dynamic_handler;
  240. /** private data associated with the dynamic protocol */
  241. PayloadContext *dynamic_protocol_context;
  242. //@}
  243. } RTSPStream;
  244. int rtsp_init(void);
  245. void rtsp_parse_line(RTSPMessageHeader *reply, const char *buf);
  246. #if LIBAVFORMAT_VERSION_INT < (53 << 16)
  247. extern int rtsp_default_protocols;
  248. #endif
  249. extern int rtsp_rtp_port_min;
  250. extern int rtsp_rtp_port_max;
  251. int rtsp_pause(AVFormatContext *s);
  252. int rtsp_resume(AVFormatContext *s);
  253. #endif /* FFMPEG_RTSP_H */