Make rtsp_skip_packet non-static, add ff prefix
[libav.git] / libavformat / rtspenc.c
CommitLineData
6f5a3d0a
MS
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
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
22#include "avformat.h"
23
24#include <sys/time.h>
25#if HAVE_SYS_SELECT_H
26#include <sys/select.h>
27#endif
28#include "network.h"
29#include "rtsp.h"
30
31static int rtsp_write_record(AVFormatContext *s)
32{
33 RTSPState *rt = s->priv_data;
34 RTSPMessageHeader reply1, *reply = &reply1;
35 char cmd[1024];
36
37 snprintf(cmd, sizeof(cmd),
38 "RECORD %s RTSP/1.0\r\n"
39 "Range: npt=%0.3f-\r\n",
db76ca7f 40 rt->control_uri,
6f5a3d0a 41 (double) 0);
3307e6ea 42 ff_rtsp_send_cmd(s, cmd, reply, NULL);
6f5a3d0a
MS
43 if (reply->status_code != RTSP_STATUS_OK)
44 return -1;
45 rt->state = RTSP_STATE_STREAMING;
46 return 0;
47}
48
49static int rtsp_write_header(AVFormatContext *s)
50{
51 RTSPState *rt = s->priv_data;
52 int ret;
53
3307e6ea 54 ret = ff_rtsp_connect(s);
6f5a3d0a
MS
55 if (ret)
56 return ret;
57
58 if (rtsp_write_record(s) < 0) {
3307e6ea 59 ff_rtsp_close_streams(s);
6f5a3d0a
MS
60 url_close(rt->rtsp_hd);
61 return AVERROR_INVALIDDATA;
62 }
63 return 0;
64}
65
66static int rtsp_write_packet(AVFormatContext *s, AVPacket *pkt)
67{
68 RTSPState *rt = s->priv_data;
69 RTSPStream *rtsp_st;
70 fd_set rfds;
71 int n, tcp_fd;
72 struct timeval tv;
73 AVFormatContext *rtpctx;
50ff78db 74 AVPacket local_pkt;
6f5a3d0a
MS
75
76 FD_ZERO(&rfds);
77 tcp_fd = url_get_file_handle(rt->rtsp_hd);
78 FD_SET(tcp_fd, &rfds);
79
80 tv.tv_sec = 0;
81 tv.tv_usec = 0;
82 n = select(tcp_fd + 1, &rfds, NULL, NULL, &tv);
83 if (n > 0) {
84 if (FD_ISSET(tcp_fd, &rfds)) {
85 RTSPMessageHeader reply;
86
3307e6ea 87 if (ff_rtsp_read_reply(s, &reply, NULL, 0) < 0)
6f5a3d0a
MS
88 return AVERROR(EPIPE);
89 /* XXX: parse message */
90 if (rt->state != RTSP_STATE_STREAMING)
91 return AVERROR(EPIPE);
92 }
93 }
94
95 if (pkt->stream_index < 0 || pkt->stream_index >= rt->nb_rtsp_streams)
96 return AVERROR_INVALIDDATA;
97 rtsp_st = rt->rtsp_streams[pkt->stream_index];
98 rtpctx = rtsp_st->transport_priv;
99
50ff78db
MS
100 /* Use a local packet for writing to the chained muxer, otherwise
101 * the internal stream_index = 0 becomes visible to the muxer user. */
102 local_pkt = *pkt;
103 local_pkt.stream_index = 0;
104 return av_write_frame(rtpctx, &local_pkt);
6f5a3d0a
MS
105}
106
107static int rtsp_write_close(AVFormatContext *s)
108{
109 RTSPState *rt = s->priv_data;
110 char cmd[1024];
111
112 snprintf(cmd, sizeof(cmd),
113 "TEARDOWN %s RTSP/1.0\r\n",
db76ca7f 114 rt->control_uri);
3307e6ea 115 ff_rtsp_send_cmd_async(s, cmd);
6f5a3d0a 116
3307e6ea 117 ff_rtsp_close_streams(s);
6f5a3d0a 118 url_close(rt->rtsp_hd);
57b5555c 119 ff_network_close();
6f5a3d0a
MS
120 return 0;
121}
122
123AVOutputFormat rtsp_muxer = {
124 "rtsp",
125 NULL_IF_CONFIG_SMALL("RTSP output format"),
126 NULL,
127 NULL,
128 sizeof(RTSPState),
129 CODEC_ID_PCM_MULAW,
130 CODEC_ID_NONE,
131 rtsp_write_header,
132 rtsp_write_packet,
133 rtsp_write_close,
134 .flags = AVFMT_NOFILE | AVFMT_GLOBALHEADER,
135};
136