attachment.c 4.6 KB
Newer Older
1 2 3 4 5 6 7 8
/*****************************************************************************
 * attachment.c: Input reading an attachment.
 *****************************************************************************
 * Copyright (C) 2009 Laurent Aimar
 * $Id$
 *
 * Authors: Laurent Aimar <fenrir _AT_ videolan _DOT_ org>
 *
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
9 10 11
 * 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
12 13 14 15
 * (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
16 17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
18
 *
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
19 20 21
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
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
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/

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

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

/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
static int  Open (vlc_object_t *);
static void Close(vlc_object_t *);

vlc_module_begin()
    set_shortname(N_("Attachment"))
    set_description(N_("Attachment input"))
    set_category(CAT_INPUT)
    set_subcategory(SUBCAT_INPUT_ACCESS)

    set_capability("access", 0)
    add_shortcut("attachment")
    set_callbacks(Open, Close)
vlc_module_end()

/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
57 58 59
static ssize_t Read(stream_t *, void *, size_t);
static int     Seek(stream_t *, uint64_t);
static int     Control(stream_t *, int, va_list);
60

61 62 63 64 65 66
struct access_sys_t
{
    input_attachment_t *attachment;
    size_t offset;
};

67 68 69
/* */
static int Open(vlc_object_t *object)
{
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
70
    stream_t     *access = (stream_t *)object;
71

72
    input_thread_t *input = access->p_input;
73 74 75
    if (!input)
        return VLC_EGENERIC;

76
    access_sys_t *sys = vlc_obj_malloc(object, sizeof (*sys));
77 78 79 80 81 82
    if (unlikely(sys == NULL))
        return VLC_ENOMEM;

    if (input_Control(input, INPUT_GET_ATTACHMENT, &sys->attachment,
                      access->psz_location))
        sys->attachment = NULL;
83

84
    if (sys->attachment == NULL) {
85 86
        msg_Err(access, "Failed to find the attachment '%s'",
                access->psz_location);
87 88 89
        return VLC_EGENERIC;
    }

90 91
    sys->offset = 0;

92 93 94 95 96
    /* */
    access->pf_read    = Read;
    access->pf_block   = NULL;
    access->pf_control = Control;
    access->pf_seek    = Seek;
97
    access->p_sys      = sys;
98 99 100 101 102 103
    return VLC_SUCCESS;
}

/* */
static void Close(vlc_object_t *object)
{
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
104
    stream_t     *access = (stream_t *)object;
105
    access_sys_t *sys = access->p_sys;
106

107
    vlc_input_attachment_Delete(sys->attachment);
108 109 110
}

/* */
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
111
static ssize_t Read(stream_t *access, void *buffer, size_t size)
112
{
113 114
    access_sys_t *sys = access->p_sys;
    input_attachment_t *a = sys->attachment;
115

116
    if (sys->offset >= (uint64_t)a->i_data)
117 118
        return 0;

119 120 121
    const size_t copy = __MIN(size, a->i_data - sys->offset);
    memcpy(buffer, (uint8_t *)a->p_data + sys->offset, copy);
    sys->offset += copy;
122 123 124 125
    return copy;
}

/* */
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
126
static int Seek(stream_t *access, uint64_t position)
127
{
128 129 130 131 132 133 134
    access_sys_t *sys = access->p_sys;
    input_attachment_t *a = sys->attachment;

    if (position > a->i_data)
        position = a->i_data;

    sys->offset = position;
135 136 137 138
    return VLC_SUCCESS;
}

/* */
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
139
static int Control(stream_t *access, int query, va_list args)
140
{
141
    access_sys_t *sys = access->p_sys;
142

143 144
    switch (query)
    {
145 146 147 148
    case STREAM_CAN_SEEK:
    case STREAM_CAN_FASTSEEK:
    case STREAM_CAN_PAUSE:
    case STREAM_CAN_CONTROL_PACE:
149 150
        *va_arg(args, bool *) = true;
        break;
151
    case STREAM_GET_SIZE:
152
        *va_arg(args, uint64_t *) = sys->attachment->i_data;
153
        break;
154
    case STREAM_GET_PTS_DELAY:
155 156
        *va_arg(args, int64_t *) = DEFAULT_PTS_DELAY;
        break;
157
    case STREAM_SET_PAUSE_STATE:
158 159 160 161 162
        return VLC_SUCCESS;

    default:
        return VLC_EGENERIC;
    }
163
    return VLC_SUCCESS;
164 165
}