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.

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