sdp.c 3.56 KB
Newer Older
1 2 3 4 5
/*****************************************************************************
 * sdp.c: Fake input for sdp:// scheme
 *****************************************************************************
 * Copyright (C) 2010 Rémi Denis-Courmont
 *
6 7 8
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
9 10 11 12
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
 * GNU Lesser General Public License for more details.
15 16
 *
 * You should have received a copy of the GNU Lesser General Public License
17 18
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
 *****************************************************************************/

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include <vlc_common.h>
#include <vlc_plugin.h>
#include <vlc_access.h>

static int Open (vlc_object_t *);

vlc_module_begin ()
    set_shortname (N_("SDP"))
    set_description (N_("Session Description Protocol"))
    set_category (CAT_INPUT)
    set_subcategory (SUBCAT_INPUT_ACCESS)

    set_capability ("access", 0)
38
    set_callbacks (Open, NULL)
39 40 41
    add_shortcut ("sdp")
vlc_module_end()

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
42 43 44
static ssize_t Read (stream_t *, void *, size_t);
static int Seek (stream_t *, uint64_t);
static int Control (stream_t *, int, va_list);
45 46 47

struct access_sys_t
{
48
    size_t offset;
49
    size_t length;
50
    char   data[];
51 52 53 54
};

static int Open (vlc_object_t *obj)
{
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
55
    stream_t *access = (stream_t *)obj;
56 57
    size_t len = strlen (access->psz_location);

58
    access_sys_t *sys = vlc_obj_malloc(obj, sizeof(*sys) + len);
59 60 61 62
    if (unlikely(sys == NULL))
        return VLC_ENOMEM;

    /* NOTE: This copy is not really needed. Better safe than sorry. */
63
    sys->offset = 0;
64 65 66 67 68 69 70 71 72 73 74 75
    sys->length = len;
    memcpy (sys->data, access->psz_location, len);

    access->pf_read = Read;
    access->pf_block = NULL;
    access->pf_seek = Seek;
    access->pf_control = Control;
    access->p_sys = sys;

    return VLC_SUCCESS;
}

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
76
static ssize_t Read (stream_t *access, void *buf, size_t len)
77 78 79
{
    access_sys_t *sys = access->p_sys;

80
    if (sys->offset >= sys->length)
81 82
        return 0;

83 84 85
    if (len > sys->length - sys->offset)
        len = sys->length - sys->offset;
    memcpy (buf, sys->data + sys->offset, len);
86
    sys->offset += len;
87 88 89
    return len;
}

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
90
static int Seek (stream_t *access, uint64_t position)
91
{
92 93 94 95 96 97
    access_sys_t *sys = access->p_sys;

    if (position > sys->length)
        position = sys->length;

    sys->offset = position;
98 99 100
    return VLC_SUCCESS;
}

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
101
static int Control (stream_t *access, int query, va_list args)
102
{
103 104
    access_sys_t *sys = access->p_sys;

105 106
    switch (query)
    {
107 108 109 110
        case STREAM_CAN_SEEK:
        case STREAM_CAN_FASTSEEK:
        case STREAM_CAN_PAUSE:
        case STREAM_CAN_CONTROL_PACE:
111 112 113 114 115 116
        {
            bool *b = va_arg(args, bool*);
            *b = true;
            return VLC_SUCCESS;
        }

117
        case STREAM_GET_SIZE:
118 119 120
            *va_arg(args, uint64_t *) = sys->length;
            return VLC_SUCCESS;

121
        case STREAM_GET_PTS_DELAY:
122 123 124 125 126 127
        {
            int64_t *dp = va_arg(args, int64_t *);
            *dp = DEFAULT_PTS_DELAY;
            return VLC_SUCCESS;
        }
    
128
        case STREAM_SET_PAUSE_STATE:
129 130 131 132
            return VLC_SUCCESS;
    }
    return VLC_EGENERIC;
}