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>
40 static bool fd_dead (int fd)
42 struct pollfd ufd = { .fd = fd, };
43 return (poll (&ufd, 1, 0) > 0) && (ufd.revents & POLLHUP);
47 * Gets a datagram from the network.
48 * @param fd datagram file descriptor
49 * @return a block or NULL on fatal error (socket dead)
51 static block_t *rtp_dgram_recv (vlc_object_t *obj, int fd)
53 block_t *block = block_Alloc (0xffff);
58 block_cleanup_push (block);
59 len = net_Read (obj, fd, NULL,
60 block->p_buffer, block->i_buffer, false);
63 if (((len <= 0) && fd_dead (fd)) || !vlc_object_alive (obj))
64 { /* POLLHUP -> permanent (DCCP) socket error */
65 block_Release (block);
71 return block_Realloc (block, 0, len);
76 * Gets a framed RTP packet.
77 * @param fd stream file descriptor
78 * @return a block or NULL in case of fatal error
80 static block_t *rtp_stream_recv (vlc_object_t *obj, int fd)
83 uint8_t hdr[2]; /* frame header */
85 /* Receives the RTP frame header */
88 ssize_t val = net_Read (obj, fd, NULL, hdr + len, 2 - len, false);
95 block_t *block = block_Alloc (GetWBE (hdr));
97 /* Receives the RTP packet */
98 for (ssize_t i = 0; i < len;)
102 block_cleanup_push (block);
103 val = net_Read (obj, fd, NULL,
104 block->p_buffer + i, block->i_buffer - i, false);
109 block_Release (block);
119 static block_t *rtp_recv (demux_t *demux)
121 demux_sys_t *p_sys = demux->p_sys;
123 for (block_t *block;; block_Release (block))
125 block = p_sys->framed_rtp
126 ? rtp_stream_recv (VLC_OBJECT (demux), p_sys->fd)
127 : rtp_dgram_recv (VLC_OBJECT (demux), p_sys->fd);
130 msg_Err (demux, "RTP flow stopped");
131 break; /* fatal error */
134 if (block->i_buffer < 2)
138 const uint8_t ptype = rtp_ptype (block);
139 if (ptype >= 72 && ptype <= 76)
140 continue; /* Muxed RTCP, ignore for now */
144 size_t len = block->i_buffer;
147 canc = vlc_savecancel ();
148 err = srtp_recv (p_sys->srtp, block->p_buffer, &len);
149 vlc_restorecancel (canc);
152 msg_Dbg (demux, "SRTP authentication/decryption failed");
155 block->i_buffer = len;
157 return block; /* success! */
163 void *rtp_thread (void *data)
165 demux_t *demux = data;
166 demux_sys_t *p_sys = demux->p_sys;
167 bool autodetect = true;
171 block_t *block = rtp_recv (demux);
173 vlc_mutex_lock (&p_sys->lock);
175 p_sys->dead = true; /* Fatal error: abort */
179 { /* Autodetect payload type, _before_ rtp_queue() */
180 if (rtp_autodetect (demux, p_sys->session, block))
182 vlc_mutex_unlock (&p_sys->lock);
183 block_Release (block);
188 rtp_queue (demux, p_sys->session, block);
190 vlc_cond_signal (&p_sys->wait);
191 vlc_mutex_unlock (&p_sys->lock);
193 while (!p_sys->dead);
200 * Process one RTP packet from the de-jitter queue.
201 * @return 0 on success, -1 on EOF
203 int rtp_process (demux_t *demux)
205 demux_sys_t *p_sys = demux->p_sys;
206 mtime_t deadline = INT64_MAX;
209 vlc_mutex_lock (&p_sys->lock);
210 if (rtp_dequeue (demux, p_sys->session, &deadline))
211 /* Pace the demux thread */
212 vlc_cond_timedwait (&p_sys->wait, &p_sys->lock, deadline);
214 vlc_cond_wait (&p_sys->wait, &p_sys->lock);
215 ret = p_sys->dead ? -1 : 0;
216 vlc_mutex_unlock (&p_sys->lock);