stream_memory.c 5.09 KB
Newer Older
1
/*****************************************************************************
2
 * stream_memory.c: stream_t wrapper around memory buffer
3
 *****************************************************************************
4
 * Copyright (C) 1999-2008 the VideoLAN team
5
 * $Id$
6
 *
7
 * Authors: Sigmund Augdal Helberg <dnumgis@videolan.org>
8 9 10 11 12 13 14 15 16 17 18 19 20
 *
 * This program 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 of the License, or
 * (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
 * 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 General Public License
 * along with this program; if not, write to the Free Software
Antoine Cellerier's avatar
Antoine Cellerier committed
21
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 23
 *****************************************************************************/

24 25 26 27
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

28
#include "stream.h"
29 30 31

struct stream_sys_t
{
32
    bool  i_preserve_memory;
33 34
    uint64_t    i_pos;      /* Current reading offset */
    uint64_t    i_size;
35 36 37 38
    uint8_t    *p_buffer;

};

39 40
static int  Read   ( stream_t *, void *p_read, unsigned int i_read );
static int  Peek   ( stream_t *, const uint8_t **pp_peek, unsigned int i_read );
41 42
static int  Control( stream_t *, int i_query, va_list );
static void Delete ( stream_t * );
43

44
#undef stream_MemoryNew
45 46 47 48 49 50
/**
 * Create a stream from a memory buffer
 *
 * \param p_this the calling vlc_object
 * \param p_buffer the memory buffer for the stream
 * \param i_buffer the size of the buffer
51
 * \param i_preserve_memory if this is set to false the memory buffer
52 53
 *        pointed to by p_buffer is freed on stream_Destroy
 */
54 55
stream_t *stream_MemoryNew( vlc_object_t *p_this, uint8_t *p_buffer,
                            uint64_t i_size, bool i_preserve_memory )
56
{
Laurent Aimar's avatar
Laurent Aimar committed
57
    stream_t *s = stream_CommonNew( p_this );
58 59
    stream_sys_t *p_sys;

60 61
    if( !s )
        return NULL;
62

63
    s->psz_path = strdup( "" ); /* N/A */
64
    s->p_sys = p_sys = malloc( sizeof( stream_sys_t ) );
65 66 67 68 69
    if( !s->psz_path || !s->p_sys )
    {
        stream_CommonDelete( s );
        return NULL;
    }
70 71 72
    p_sys->i_pos = 0;
    p_sys->i_size = i_size;
    p_sys->p_buffer = p_buffer;
73
    p_sys->i_preserve_memory = i_preserve_memory;
74

75 76 77 78
    s->pf_read    = Read;
    s->pf_peek    = Peek;
    s->pf_control = Control;
    s->pf_destroy = Delete;
79

80
    vlc_object_attach( s, p_this );
81

82 83 84 85 86 87
    /* Get a weak link to the parent input */
    /* FIXME: The usage of vlc_object_find has to be removed. */
    s->p_input = (input_thread_t *)vlc_object_find( p_this, VLC_OBJECT_INPUT, FIND_PARENT );
    if(s->p_input)
        vlc_object_release((vlc_object_t*)s->p_input);

88 89 90
    return s;
}

91
static void Delete( stream_t *s )
92
{
93
    if( !s->p_sys->i_preserve_memory ) free( s->p_sys->p_buffer );
94
    free( s->p_sys );
Laurent Aimar's avatar
Laurent Aimar committed
95
    stream_CommonDelete( s );
96
}
97

98 99 100
/****************************************************************************
 * AStreamControl:
 ****************************************************************************/
101
static int Control( stream_t *s, int i_query, va_list args )
102 103 104
{
    stream_sys_t *p_sys = s->p_sys;

105
    bool *p_bool;
106
    uint64_t   *pi_64, i_64;
107 108 109 110 111
    int        i_int;

    switch( i_query )
    {
        case STREAM_GET_SIZE:
112
            pi_64 = va_arg( args, uint64_t * );
113 114 115 116
            *pi_64 = p_sys->i_size;
            break;

        case STREAM_CAN_SEEK:
117 118
            p_bool = (bool*)va_arg( args, bool * );
            *p_bool = true;
119 120 121
            break;

        case STREAM_CAN_FASTSEEK:
122 123
            p_bool = (bool*)va_arg( args, bool * );
            *p_bool = true;
124 125 126
            break;

        case STREAM_GET_POSITION:
127
            pi_64 = va_arg( args, uint64_t * );
128 129 130 131
            *pi_64 = p_sys->i_pos;
            break;

        case STREAM_SET_POSITION:
132
            i_64 = va_arg( args, uint64_t );
133
            i_64 = __MIN( i_64, s->p_sys->i_size );
134
            p_sys->i_pos = i_64;
135
            break;
136

137
        case STREAM_GET_CONTENT_TYPE:
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
            return VLC_EGENERIC;

        case STREAM_CONTROL_ACCESS:
            i_int = (int) va_arg( args, int );
            msg_Err( s, "Hey, what are you thinking ?"
                     "DON'T USE STREAM_CONTROL_ACCESS !!!" );
            return VLC_EGENERIC;

        default:
            msg_Err( s, "invalid stream_vaControl query=0x%x", i_query );
            return VLC_EGENERIC;
    }
    return VLC_SUCCESS;
}

153
static int Read( stream_t *s, void *p_read, unsigned int i_read )
154 155 156 157 158 159 160 161
{
    stream_sys_t *p_sys = s->p_sys;
    int i_res = __MIN( i_read, p_sys->i_size - p_sys->i_pos );
    memcpy( p_read, p_sys->p_buffer + p_sys->i_pos, i_res );
    p_sys->i_pos += i_res;
    return i_res;
}

162
static int Peek( stream_t *s, const uint8_t **pp_peek, unsigned int i_read )
163 164 165 166 167 168
{
    stream_sys_t *p_sys = s->p_sys;
    int i_res = __MIN( i_read, p_sys->i_size - p_sys->i_pos );
    *pp_peek = p_sys->p_buffer + p_sys->i_pos;
    return i_res;
}