]> git.sesse.net Git - vlc/blob - src/input/stream_memory.c
1f4091286d6832f51389b96aff2fb6adb5324e45
[vlc] / src / input / stream_memory.c
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  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27
28 #include "stream.h"
29
30 struct stream_sys_t
31 {
32     bool  i_preserve_memory;
33     uint64_t    i_pos;      /* Current reading offset */
34     uint64_t    i_size;
35     uint8_t    *p_buffer;
36
37 };
38
39 static int  Read   ( stream_t *, void *p_read, unsigned int i_read );
40 static int  Peek   ( stream_t *, const uint8_t **pp_peek, unsigned int i_read );
41 static int  Control( stream_t *, int i_query, va_list );
42 static void Delete ( stream_t * );
43
44 /**
45  * Create a stream from a memory buffer
46  *
47  * \param p_this the calling vlc_object
48  * \param p_buffer the memory buffer for the stream
49  * \param i_buffer the size of the buffer
50  * \param i_preserve_memory if this is set to false the memory buffer
51  *        pointed to by p_buffer is freed on stream_Destroy
52  */
53 stream_t *__stream_MemoryNew( vlc_object_t *p_this, uint8_t *p_buffer,
54                               uint64_t i_size, bool i_preserve_memory )
55 {
56     stream_t *s = stream_CommonNew( p_this );
57     stream_sys_t *p_sys;
58
59     if( !s )
60         return NULL;
61
62     s->psz_path = strdup( "" ); /* N/A */
63     s->p_sys = p_sys = malloc( sizeof( stream_sys_t ) );
64     if( !s->psz_path || !s->p_sys )
65     {
66         stream_CommonDelete( s );
67         return NULL;
68     }
69     p_sys->i_pos = 0;
70     p_sys->i_size = i_size;
71     p_sys->p_buffer = p_buffer;
72     p_sys->i_preserve_memory = i_preserve_memory;
73
74     s->pf_read    = Read;
75     s->pf_peek    = Peek;
76     s->pf_control = Control;
77     s->pf_destroy = Delete;
78
79     vlc_object_attach( s, p_this );
80
81     /* Get a weak link to the parent input */
82     /* FIXME: The usage of vlc_object_find has to be removed. */
83     s->p_input = (input_thread_t *)vlc_object_find( p_this, VLC_OBJECT_INPUT, FIND_PARENT );
84     if(s->p_input)
85         vlc_object_release((vlc_object_t*)s->p_input);
86
87     return s;
88 }
89
90 static void Delete( stream_t *s )
91 {
92     if( !s->p_sys->i_preserve_memory ) free( s->p_sys->p_buffer );
93     free( s->p_sys );
94     stream_CommonDelete( s );
95 }
96
97 /****************************************************************************
98  * AStreamControl:
99  ****************************************************************************/
100 static int Control( stream_t *s, int i_query, va_list args )
101 {
102     stream_sys_t *p_sys = s->p_sys;
103
104     bool *p_bool;
105     uint64_t   *pi_64, i_64;
106     int        i_int;
107
108     switch( i_query )
109     {
110         case STREAM_GET_SIZE:
111             pi_64 = va_arg( args, uint64_t * );
112             *pi_64 = p_sys->i_size;
113             break;
114
115         case STREAM_CAN_SEEK:
116             p_bool = (bool*)va_arg( args, bool * );
117             *p_bool = true;
118             break;
119
120         case STREAM_CAN_FASTSEEK:
121             p_bool = (bool*)va_arg( args, bool * );
122             *p_bool = true;
123             break;
124
125         case STREAM_GET_POSITION:
126             pi_64 = va_arg( args, uint64_t * );
127             *pi_64 = p_sys->i_pos;
128             break;
129
130         case STREAM_SET_POSITION:
131             i_64 = va_arg( args, uint64_t );
132             i_64 = __MIN( i_64, s->p_sys->i_size );
133             p_sys->i_pos = i_64;
134             break;
135
136         case STREAM_GET_CONTENT_TYPE:
137             return VLC_EGENERIC;
138
139         case STREAM_CONTROL_ACCESS:
140             i_int = (int) va_arg( args, int );
141             msg_Err( s, "Hey, what are you thinking ?"
142                      "DON'T USE STREAM_CONTROL_ACCESS !!!" );
143             return VLC_EGENERIC;
144
145         default:
146             msg_Err( s, "invalid stream_vaControl query=0x%x", i_query );
147             return VLC_EGENERIC;
148     }
149     return VLC_SUCCESS;
150 }
151
152 static int Read( stream_t *s, void *p_read, unsigned int i_read )
153 {
154     stream_sys_t *p_sys = s->p_sys;
155     int i_res = __MIN( i_read, p_sys->i_size - p_sys->i_pos );
156     memcpy( p_read, p_sys->p_buffer + p_sys->i_pos, i_res );
157     p_sys->i_pos += i_res;
158     return i_res;
159 }
160
161 static int Peek( stream_t *s, const uint8_t **pp_peek, unsigned int i_read )
162 {
163     stream_sys_t *p_sys = s->p_sys;
164     int i_res = __MIN( i_read, p_sys->i_size - p_sys->i_pos );
165     *pp_peek = p_sys->p_buffer + p_sys->i_pos;
166     return i_res;
167 }