Go to the documentation of this file.
1 /*
2  * RTSP muxer
3  * Copyright (c) 2010 Martin Storsjo
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
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  */
22 #include "avformat.h"
24 #if HAVE_POLL_H
25 #include <poll.h>
26 #endif
27 #include "network.h"
28 #include "os_support.h"
29 #include "rtsp.h"
30 #include "internal.h"
31 #include "avio_internal.h"
32 #include "libavutil/intreadwrite.h"
33 #include "libavutil/avstring.h"
34 #include "libavutil/time.h"
35 #include "url.h"
38 static const AVClass rtsp_muxer_class = {
39  .class_name = "RTSP muxer",
40  .item_name = av_default_item_name,
41  .option = ff_rtsp_options,
43 };
46 {
47  RTSPState *rt = s->priv_data;
48  RTSPMessageHeader reply1, *reply = &reply1;
49  int i;
50  char *sdp;
51  AVFormatContext sdp_ctx, *ctx_array[1];
52  char url[MAX_URL_SIZE];
57  /* Announce the stream */
58  sdp = av_mallocz(SDP_MAX_SIZE);
59  if (!sdp)
60  return AVERROR(ENOMEM);
61  /* We create the SDP based on the RTSP AVFormatContext where we
62  * aren't allowed to change the filename field. (We create the SDP
63  * based on the RTSP context since the contexts for the RTP streams
64  * don't exist yet.) In order to specify a custom URL with the actual
65  * peer IP instead of the originally specified hostname, we create
66  * a temporary copy of the AVFormatContext, where the custom URL is set.
67  *
68  * FIXME: Create the SDP without copying the AVFormatContext.
69  * This either requires setting up the RTP stream AVFormatContexts
70  * already here (complicating things immensely) or getting a more
71  * flexible SDP creation interface.
72  */
73  sdp_ctx = *s;
74  sdp_ctx.url = url;
75  ff_url_join(url, sizeof(url),
76  "rtsp", NULL, addr, -1, NULL);
77  ctx_array[0] = &sdp_ctx;
78  if (av_sdp_create(ctx_array, 1, sdp, SDP_MAX_SIZE)) {
79  av_free(sdp);
81  }
82  av_log(s, AV_LOG_VERBOSE, "SDP:\n%s\n", sdp);
83  ff_rtsp_send_cmd_with_content(s, "ANNOUNCE", rt->control_uri,
84  "Content-Type: application/sdp\r\n",
85  reply, NULL, sdp, strlen(sdp));
86  av_free(sdp);
87  if (reply->status_code != RTSP_STATUS_OK)
90  /* Set up the RTSPStreams for each AVStream */
91  for (i = 0; i < s->nb_streams; i++) {
92  RTSPStream *rtsp_st;
94  rtsp_st = av_mallocz(sizeof(RTSPStream));
95  if (!rtsp_st)
96  return AVERROR(ENOMEM);
97  dynarray_add(&rt->rtsp_streams, &rt->nb_rtsp_streams, rtsp_st);
99  rtsp_st->stream_index = i;
101  av_strlcpy(rtsp_st->control_url, rt->control_uri, sizeof(rtsp_st->control_url));
102  /* Note, this must match the relative uri set in the sdp content */
103  av_strlcatf(rtsp_st->control_url, sizeof(rtsp_st->control_url),
104  "/streamid=%d", i);
105  }
107  return 0;
108 }
111 {
112  RTSPState *rt = s->priv_data;
113  RTSPMessageHeader reply1, *reply = &reply1;
114  char cmd[MAX_URL_SIZE];
116  snprintf(cmd, sizeof(cmd),
117  "Range: npt=0.000-\r\n");
118  ff_rtsp_send_cmd(s, "RECORD", rt->control_uri, cmd, reply, NULL);
119  if (reply->status_code != RTSP_STATUS_OK)
120  return ff_rtsp_averror(reply->status_code, -1);
122  return 0;
123 }
126 {
127  int ret;
129  ret = ff_rtsp_connect(s);
130  if (ret)
131  return ret;
133  if (rtsp_write_record(s) < 0) {
137  }
138  return 0;
139 }
142 {
143  RTSPState *rt = s->priv_data;
144  AVFormatContext *rtpctx = rtsp_st->transport_priv;
145  uint8_t *buf, *ptr;
146  int size;
147  uint8_t *interleave_header, *interleaved_packet;
149  size = avio_close_dyn_buf(rtpctx->pb, &buf);
150  rtpctx->pb = NULL;
151  ptr = buf;
152  while (size > 4) {
153  uint32_t packet_len = AV_RB32(ptr);
154  int id;
155  /* The interleaving header is exactly 4 bytes, which happens to be
156  * the same size as the packet length header from
157  * ffio_open_dyn_packet_buf. So by writing the interleaving header
158  * over these bytes, we get a consecutive interleaved packet
159  * that can be written in one call. */
160  interleaved_packet = interleave_header = ptr;
161  ptr += 4;
162  size -= 4;
163  if (packet_len > size || packet_len < 2)
164  break;
165  if (RTP_PT_IS_RTCP(ptr[1]))
166  id = rtsp_st->interleaved_max; /* RTCP */
167  else
168  id = rtsp_st->interleaved_min; /* RTP */
169  interleave_header[0] = '$';
170  interleave_header[1] = id;
171  AV_WB16(interleave_header + 2, packet_len);
172  ffurl_write(rt->rtsp_hd_out, interleaved_packet, 4 + packet_len);
173  ptr += packet_len;
174  size -= packet_len;
175  }
176  av_free(buf);
178 }
181 {
182  RTSPState *rt = s->priv_data;
183  RTSPStream *rtsp_st;
184  int n;
185  struct pollfd p = {ffurl_get_file_handle(rt->rtsp_hd), POLLIN, 0};
186  AVFormatContext *rtpctx;
187  int ret;
189  while (1) {
190  n = poll(&p, 1, 0);
191  if (n <= 0)
192  break;
193  if (p.revents & POLLIN) {
194  RTSPMessageHeader reply;
196  /* Don't let ff_rtsp_read_reply handle interleaved packets,
197  * since it would block and wait for an RTSP reply on the socket
198  * (which may not be coming any time soon) if it handles
199  * interleaved packets internally. */
200  ret = ff_rtsp_read_reply(s, &reply, NULL, 1, NULL);
201  if (ret < 0)
202  return AVERROR(EPIPE);
203  if (ret == 1)
205  /* XXX: parse message */
206  if (rt->state != RTSP_STATE_STREAMING)
207  return AVERROR(EPIPE);
208  }
209  }
211  if (pkt->stream_index < 0 || pkt->stream_index >= rt->nb_rtsp_streams)
213  rtsp_st = rt->rtsp_streams[pkt->stream_index];
214  rtpctx = rtsp_st->transport_priv;
216  ret = ff_write_chained(rtpctx, 0, pkt, s, 0);
217  /* ff_write_chained does all the RTP packetization. If using TCP as
218  * transport, rtpctx->pb is only a dyn_packet_buf that queues up the
219  * packets, so we need to send them out on the TCP connection separately.
220  */
221  if (!ret && rt->lower_transport == RTSP_LOWER_TRANSPORT_TCP)
222  ret = ff_rtsp_tcp_write_packet(s, rtsp_st);
223  return ret;
224 }
227 {
228  RTSPState *rt = s->priv_data;
230  // If we want to send RTCP_BYE packets, these are sent by av_write_trailer.
231  // Thus call this on all streams before doing the teardown. This is
232  // done within ff_rtsp_undo_setup.
233  ff_rtsp_undo_setup(s, 1);
235  ff_rtsp_send_cmd_async(s, "TEARDOWN", rt->control_uri, NULL);
240  return 0;
241 }
244  .name = "rtsp",
245  .long_name = NULL_IF_CONFIG_SMALL("RTSP output"),
246  .priv_data_size = sizeof(RTSPState),
247  .audio_codec = AV_CODEC_ID_AAC,
248  .video_codec = AV_CODEC_ID_MPEG4,
253  .priv_class = &rtsp_muxer_class,
254 };
static void write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, int unqueue)
Definition: ffmpeg.c:729
void ff_rtsp_skip_packet(AVFormatContext *s)
Skip a RTP/TCP interleaved packet.
#define NULL
Definition: coverity.c:32
Invalid data found when processing input.
Definition: error.h:59
int64_t start_time_realtime
Start time of the stream in real world time, in microseconds since the Unix epoch (00:00 1st January ...
Definition: avformat.h:1502
int avio_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
Return the written size and a pointer to the buffer.
Definition: aviobuf.c:1424
Definition: version.h:85
int ffurl_write(URLContext *h, const unsigned char *buf, int size)
Write size bytes from buf to the resource accessed by h.
Definition: avio.c:415
int ff_write_chained(AVFormatContext *dst, int dst_stream, AVPacket *pkt, AVFormatContext *src, int interleave)
Write a packet to another muxer than the one the user originally intended.
Definition: mux.c:1328
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:235
void ff_network_close(void)
Definition: network.c:116
initialized and sending/receiving data
Definition: rtsp.h:205
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:237
AVPacket * pkt
Definition: movenc.c:59
This describes the server response to each RTSP command.
Definition: rtsp.h:130
Format I/O context.
Definition: avformat.h:1247
#define MAX_URL_SIZE
Definition: internal.h:30
int ff_rtsp_connect(AVFormatContext *s)
Connect to the RTSP server and set up the individual media streams.
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:72
miscellaneous OS support macros and functions.
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_RB32
Definition: bytestream.h:91
URLContext * rtsp_hd_out
Additional output handle, used when input and output are done separately, eg for HTTP tunneling...
Definition: rtsp.h:337
Describe a single stream, as identified by a single m= line block in the SDP content.
Definition: rtsp.h:447
enum RTSPStatusCode status_code
response code from server
Definition: rtsp.h:134
Detailed information.
Definition: log.h:210
int av_sdp_create(AVFormatContext *ac[], int n_files, char *buf, int size)
Generate an SDP for an RTP session.
Definition: sdp.c:846
ptrdiff_t size
Definition: opengl_enc.c:100
int ff_rtsp_send_cmd(AVFormatContext *s, const char *method, const char *url, const char *headers, RTSPMessageHeader *reply, unsigned char **content_ptr)
Send a command to the RTSP server and wait for the reply.
#define AV_WB16(p, v)
Definition: intreadwrite.h:405
static int ff_rtsp_averror(enum RTSPStatusCode status_code, int default_averror)
Definition: rtspcodes.h:144
#define av_log(a,...)
Private data for the RTSP demuxer.
Definition: rtsp.h:227
AVOutputFormat ff_rtsp_muxer
Definition: rtspenc.c:243
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:117
char * url
input or output URL.
Definition: avformat.h:1343
const AVOption ff_rtsp_options[]
Definition: rtsp.c:80
int ff_rtsp_tcp_write_packet(AVFormatContext *s, RTSPStream *rtsp_st)
Send buffered packets over TCP.
Definition: rtspenc.c:141
URLContext * rtsp_hd
Definition: rtsp.h:229
size_t av_strlcpy(char *dst, const char *src, size_t size)
Copy the string src to dst, but no more than size - 1 bytes, and null-terminate dst.
Definition: avstring.c:83
struct RTSPStream ** rtsp_streams
streams in this session
Definition: rtsp.h:234
int stream_index
corresponding stream index, if any.
Definition: rtsp.h:452
static int rtsp_write_close(AVFormatContext *s)
Definition: rtspenc.c:226
static int rtsp_write_header(AVFormatContext *s)
Definition: rtspenc.c:125
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:1303
char control_uri[MAX_URL_SIZE]
some MS RTSP streams contain a URL in the SDP that we need to use for all subsequent RTSP requests...
Definition: rtsp.h:326
#define dynarray_add(tab, nb_ptr, elem)
Definition: internal.h:371
int nb_rtsp_streams
number of items in the &#39;rtsp_streams&#39; variable
Definition: rtsp.h:232
int ffio_open_dyn_packet_buf(AVIOContext **s, int max_packet_size)
Open a write only packetized memory stream with a maximum packet size of &#39;max_packet_size&#39;.
Definition: aviobuf.c:1384
Definition: rtsp.h:77
static int write_trailer(AVFormatContext *s1)
Definition: v4l2enc.c:98
Format wants global header.
Definition: avformat.h:461
const char * name
Definition: avformat.h:500
static int rtsp_write_record(AVFormatContext *s)
Definition: rtspenc.c:110
#define s(width, name)
Definition: cbs_vp9.c:257
int ffurl_get_file_handle(URLContext *h)
Return the file descriptor associated with this URL.
Definition: avio.c:620
int64_t av_gettime(void)
Get the current time in microseconds.
Definition: time.c:39
int ff_url_join(char *str, int size, const char *proto, const char *authorization, const char *hostname, int port, const char *fmt,...)
Definition: url.c:38
enum RTSPLowerTransport lower_transport
the negotiated network layer transport protocol; e.g.
Definition: rtsp.h:271
void ff_rtsp_undo_setup(AVFormatContext *s, int send_packets)
Undo the effect of ff_rtsp_make_setup_request, close the transport_priv and rtp_handle fields...
Definition: rtsp.c:760
AVIOContext * pb
I/O context.
Definition: avformat.h:1289
#define SDP_MAX_SIZE
Definition: rtsp.h:82
Describe the class of an AVClass context structure.
Definition: log.h:67
size_t av_strlcatf(char *dst, size_t size, const char *fmt,...)
Definition: avstring.c:101
void ff_rtsp_close_streams(AVFormatContext *s)
Close and free all streams within the RTSP (de)muxer.
Definition: rtsp.c:792
#define snprintf
Definition: snprintf.h:34
#define RTP_PT_IS_RTCP(x)
Definition: rtp.h:110
#define flags(name, subs,...)
Definition: cbs_av1.c:561
enum RTSPClientState state
indicator of whether we are currently receiving data from the server.
Definition: rtsp.h:240
int ff_rtsp_send_cmd_with_content(AVFormatContext *s, const char *method, const char *url, const char *headers, RTSPMessageHeader *reply, unsigned char **content_ptr, const unsigned char *send_content, int send_content_length)
Send a command to the RTSP server and wait for the reply.
Main libavformat public API header.
Demuxer will use avio_open, no opened file should be provided by the caller.
Definition: avformat.h:458
int ff_rtsp_read_reply(AVFormatContext *s, RTSPMessageHeader *reply, unsigned char **content_ptr, int return_on_interleaved_data, const char *method)
Read a RTSP message from the server, or prepare to read data packets if we&#39;re reading data interleave...
#define av_free(p)
int ff_rtsp_send_cmd_async(AVFormatContext *s, const char *method, const char *url, const char *headers)
Send a command to the RTSP server without waiting for the reply.
TCP; interleaved in RTSP.
Definition: rtsp.h:40
int ff_rtsp_setup_output_streams(AVFormatContext *s, const char *addr)
Announce the stream to the server and set up the RTSPStream child objects for each media stream...
Definition: rtspenc.c:45
void * priv_data
Format private data.
Definition: avformat.h:1275
static void write_header(FFV1Context *f)
Definition: ffv1enc.c:346
static int rtsp_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: rtspenc.c:180
unbuffered private I/O API
char control_url[MAX_URL_SIZE]
url for this stream (from SDP)
Definition: rtsp.h:458
int stream_index
Definition: packet.h:371
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later.That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another.Frame references ownership and permissions
int interleaved_max
Definition: rtsp.h:456
static const AVClass rtsp_muxer_class
Definition: rtspenc.c:38
enum AVCodecID id
int interleaved_min
interleave IDs; copies of RTSPTransportField->interleaved_min/max for the selected transport...
Definition: rtsp.h:456
This structure stores compressed data.
Definition: packet.h:346
void ff_rtsp_close_connections(AVFormatContext *s)
Close all connection handles within the RTSP (de)muxer.
int i
Definition: input.c:407
Undefined timestamp value.
Definition: avutil.h:248
void * transport_priv
RTP/RDT parse context if input, RTP AVFormatContext if output.
Definition: rtsp.h:449