3 * @brief RTP packet input
5 /*****************************************************************************
6 * Copyright © 2008 Rémi Denis-Courmont
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2.0
11 * of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 ****************************************************************************/
27 #include <vlc_common.h>
28 #include <vlc_demux.h>
29 #include <vlc_block.h>
30 #include <vlc_network.h>
41 * Gets a datagram from the network.
42 * @param fd datagram file descriptor
43 * @return a block or NULL on fatal error (socket dead)
45 static block_t *rtp_dgram_recv (vlc_object_t *obj, int fd)
47 block_t *block = block_Alloc (0xffff);
52 block_cleanup_push (block);
53 len = net_Read (obj, fd, NULL,
54 block->p_buffer, block->i_buffer, false);
57 if (((len <= 0) && poll (&(struct pollfd){ .fd = fd, }, 1, 0))
58 || !vlc_object_alive (obj))
59 { /* POLLHUP -> permanent (DCCP) socket error */
60 block_Release (block);
66 return block_Realloc (block, 0, len);
71 * Gets a framed RTP packet.
72 * @param fd stream file descriptor
73 * @return a block or NULL in case of fatal error
75 static block_t *rtp_stream_recv (vlc_object_t *obj, int fd)
78 uint8_t hdr[2]; /* frame header */
80 /* Receives the RTP frame header */
83 ssize_t val = net_Read (obj, fd, NULL, hdr + len, 2 - len, false);
90 block_t *block = block_Alloc (GetWBE (hdr));
92 /* Receives the RTP packet */
93 for (ssize_t i = 0; i < len;)
97 block_cleanup_push (block);
98 val = net_Read (obj, fd, NULL,
99 block->p_buffer + i, block->i_buffer - i, false);
104 block_Release (block);
114 static block_t *rtp_recv (demux_t *demux)
116 demux_sys_t *p_sys = demux->p_sys;
118 for (block_t *block;; block_Release (block))
120 block = p_sys->framed_rtp
121 ? rtp_stream_recv (VLC_OBJECT (demux), p_sys->fd)
122 : rtp_dgram_recv (VLC_OBJECT (demux), p_sys->fd);
125 msg_Err (demux, "RTP flow stopped");
126 break; /* fatal error */
129 if (block->i_buffer < 2)
133 const uint8_t ptype = rtp_ptype (block);
134 if (ptype >= 72 && ptype <= 76)
135 continue; /* Muxed RTCP, ignore for now */
139 size_t len = block->i_buffer;
142 canc = vlc_savecancel ();
143 err = srtp_recv (p_sys->srtp, block->p_buffer, &len);
144 vlc_restorecancel (canc);
147 msg_Dbg (demux, "SRTP authentication/decryption failed");
150 block->i_buffer = len;
152 return block; /* success! */
158 void *rtp_thread (void *data)
160 demux_t *demux = data;
161 demux_sys_t *p_sys = demux->p_sys;
165 block_t *block = rtp_recv (demux);
167 vlc_mutex_lock (&p_sys->lock);
169 p_sys->dead = true; /* Fatal error: abort */
172 if (p_sys->autodetect)
173 { /* Autodetect payload type, _before_ rtp_queue() */
174 if (rtp_autodetect (demux, p_sys->session, block))
176 vlc_mutex_unlock (&p_sys->lock);
177 block_Release (block);
180 p_sys->autodetect = false;
182 rtp_queue (demux, p_sys->session, block);
184 vlc_cond_signal (&p_sys->wait);
185 vlc_mutex_unlock (&p_sys->lock);
187 while (!p_sys->dead);
193 void rtp_process (demux_t *demux)
195 demux_sys_t *p_sys = demux->p_sys;
196 mtime_t deadline = INT64_MAX;
198 vlc_mutex_lock (&p_sys->lock);
199 if (!rtp_dequeue (demux, p_sys->session, &deadline))
200 deadline = mdate () + CLOCK_FREQ / 5;
201 vlc_cond_timedwait (&p_sys->wait, &p_sys->lock, deadline);
202 vlc_mutex_unlock (&p_sys->lock);