input.c 5.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
/**
 * @file input.c
 * @brief RTP packet input
 */
/*****************************************************************************
 * Copyright © 2008 Rémi Denis-Courmont
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2.0
 * of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 ****************************************************************************/

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <vlc_common.h>
#include <vlc_demux.h>
#include <vlc_block.h>
#include <vlc_network.h>

#include <unistd.h>
#ifdef HAVE_POLL
# include <poll.h>
#endif

#include "rtp.h"
#include <srtp.h>

/**
 * Gets a datagram from the network.
 * @param fd datagram file descriptor
 * @return a block or NULL on fatal error (socket dead)
 */
static block_t *rtp_dgram_recv (int fd)
{
    block_t *block = block_Alloc (0xffff);
    ssize_t len;

    do
    {
        struct pollfd ufd = { .fd = fd, .events = POLLIN, };

        block_cleanup_push (block);
        poll (&ufd, 1, -1);
        len = read (fd, block->p_buffer, block->i_buffer);
        vlc_cleanup_pop ();

        if ((len <= 0) && (ufd.revents & POLLHUP))
        {   /* POLLHUP -> permanent (DCCP) socket error */
            block_Release (block);
            return NULL;
        }
    }
    while (len == -1);

    return block_Realloc (block, 0, len);
}


/**
 * Gets a framed RTP packet.
 * @param fd stream file descriptor
 * @return a block or NULL in case of fatal error
 */
static block_t *rtp_stream_recv (int fd)
{
    ssize_t len = 0;
    struct pollfd ufd = { .fd = fd, .events = POLLIN, };
    uint8_t hdr[2]; /* frame header */

    /* Receives the RTP frame header */
    do
    {
        ssize_t val;

        poll (&ufd, 1, -1);
        val = read (fd, hdr + len, 2 - len);
        if (val <= 0)
            return NULL;
        len += val;
    }
    while (len < 2);

    block_t *block = block_Alloc (GetWBE (hdr));

    /* Receives the RTP packet */
    for (ssize_t i = 0; i < len;)
    {
        ssize_t val;

        block_cleanup_push (block);
        poll (&ufd, 1, -1);
        val = read (fd, block->p_buffer + i, block->i_buffer - i);
        vlc_cleanup_pop ();

        if (val <= 0)
        {
            block_Release (block);
            return NULL;
        }
        i += val;
    }

    return block;
}


static block_t *rtp_recv (demux_t *demux)
{
    demux_sys_t *p_sys = demux->p_sys;

    for (block_t *block;; block_Release (block))
    {
        block = p_sys->framed_rtp
                ? rtp_stream_recv (p_sys->fd)
                : rtp_dgram_recv (p_sys->fd);
        if (block == NULL)
        {
            msg_Err (demux, "RTP flow stopped");
            break; /* fatal error */
        }

        if (block->i_buffer < 2)
            continue;

        /* FIXME */
        const uint8_t ptype = rtp_ptype (block);
        if (ptype >= 72 && ptype <= 76)
            continue; /* Muxed RTCP, ignore for now */

        if (p_sys->srtp)
        {
            size_t len = block->i_buffer;
            int canc, err;

            canc = vlc_savecancel ();
            err = srtp_recv (p_sys->srtp, block->p_buffer, &len);
            vlc_restorecancel (canc);
            if (err)
            {
                msg_Dbg (demux, "SRTP authentication/decryption failed");
                continue;
            }
            block->i_buffer = len;
        }
        return block; /* success! */
    }
    return NULL;
}


void *rtp_thread (void *data)
{
    demux_t *demux = data;
    demux_sys_t *p_sys = demux->p_sys;

    for (;;)
    {
        block_t *block = rtp_recv (demux);
        if (block == NULL)
            break; /* fatal error: abort */

        vlc_mutex_lock (&p_sys->lock);

        /* Autodetect payload type, _before_ rtp_queue() */
        if (p_sys->autodetect)
        {
            if (rtp_autodetect (demux, p_sys->session, block))
            {
                block_Release (block);
                continue;
            }
            p_sys->autodetect = false;
        }

        rtp_queue (demux, p_sys->session, block);
        vlc_cond_signal (&p_sys->wait);
        vlc_mutex_unlock (&p_sys->lock);
    }
    /* TODO: return 0 from Demux */
    return NULL;
}


void rtp_process (demux_t *demux)
{
    demux_sys_t *p_sys = demux->p_sys;
    mtime_t deadline;

    vlc_mutex_lock (&p_sys->lock);
    if (rtp_dequeue (demux, p_sys->session, &deadline))
        /* Pace the demux thread */
        vlc_cond_timedwait (&p_sys->wait, &p_sys->lock, deadline);
    else
        vlc_cond_wait (&p_sys->wait, &p_sys->lock);
    vlc_mutex_unlock (&p_sys->lock);
}