]> git.sesse.net Git - vlc/blob - src/input/mem_stream.c
FSF address change.
[vlc] / src / input / mem_stream.c
1 /*****************************************************************************
2  * mem_stream.c: stream_t wrapper around memory buffer
3  *****************************************************************************
4  * Copyright (C) 1999-2004 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 #include <stdlib.h>
25 #include <vlc/vlc.h>
26 #include <vlc/input.h>
27
28 #include "input_internal.h"
29
30 struct stream_sys_t
31 {
32     vlc_bool_t  i_preserve_memory;
33     int64_t     i_pos;      /* Current reading offset */
34     int64_t     i_size;
35     uint8_t    *p_buffer;
36
37 };
38
39 static int  Read   ( stream_t *, void *p_read, int i_read );
40 static int  Peek   ( stream_t *, uint8_t **pp_peek, 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 VLC_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                               int64_t i_size, vlc_bool_t i_preserve_memory )
55 {
56     stream_t *s = vlc_object_create( p_this, VLC_OBJECT_STREAM );
57     stream_sys_t *p_sys;
58
59     if( !s ) return NULL;
60
61     s->p_sys = p_sys = malloc( sizeof( stream_sys_t ) );
62     p_sys->i_pos = 0;
63     p_sys->i_size = i_size;
64     p_sys->p_buffer = p_buffer;
65     p_sys->i_preserve_memory = i_preserve_memory;
66
67     s->pf_block   = NULL;
68     s->pf_read    = Read;
69     s->pf_peek    = Peek;
70     s->pf_control = Control;
71     s->pf_destroy = Delete;
72
73     s->i_char_width = 1;
74     s->b_little_endian = VLC_FALSE;
75     vlc_object_attach( s, p_this );
76
77     return s;
78 }
79
80 static void Delete( stream_t *s )
81 {
82     if( !s->p_sys->i_preserve_memory ) free( s->p_sys->p_buffer );
83     free( s->p_sys );
84     vlc_object_detach( s );
85     vlc_object_destroy( s );
86 }
87
88 /****************************************************************************
89  * AStreamControl:
90  ****************************************************************************/
91 static int Control( stream_t *s, int i_query, va_list args )
92 {
93     stream_sys_t *p_sys = s->p_sys;
94
95     vlc_bool_t *p_bool;
96     int64_t    *pi_64, i_64;
97     int        i_int;
98
99     switch( i_query )
100     {
101         case STREAM_GET_SIZE:
102             pi_64 = (int64_t*)va_arg( args, int64_t * );
103             *pi_64 = p_sys->i_size;
104             break;
105
106         case STREAM_CAN_SEEK:
107             p_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
108             *p_bool = VLC_TRUE;
109             break;
110
111         case STREAM_CAN_FASTSEEK:
112             p_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
113             *p_bool = VLC_TRUE;
114             break;
115
116         case STREAM_GET_POSITION:
117             pi_64 = (int64_t*)va_arg( args, int64_t * );
118             *pi_64 = p_sys->i_pos;
119             break;
120
121         case STREAM_SET_POSITION:
122             i_64 = (int64_t)va_arg( args, int64_t );
123             i_64 = __MAX( i_64, 0 );
124             i_64 = __MIN( i_64, s->p_sys->i_size ); 
125             p_sys->i_pos = i_64;
126             break;
127
128         case STREAM_GET_MTU:
129             return VLC_EGENERIC;
130
131         case STREAM_CONTROL_ACCESS:
132             i_int = (int) va_arg( args, int );
133             msg_Err( s, "Hey, what are you thinking ?"
134                      "DON'T USE STREAM_CONTROL_ACCESS !!!" );
135             return VLC_EGENERIC;
136
137         default:
138             msg_Err( s, "invalid stream_vaControl query=0x%x", i_query );
139             return VLC_EGENERIC;
140     }
141     return VLC_SUCCESS;
142 }
143
144 static int Read( stream_t *s, void *p_read, int i_read )
145 {
146     stream_sys_t *p_sys = s->p_sys;
147     int i_res = __MIN( i_read, p_sys->i_size - p_sys->i_pos );
148     memcpy( p_read, p_sys->p_buffer + p_sys->i_pos, i_res );
149     p_sys->i_pos += i_res;
150     return i_res;
151 }
152
153 static int Peek( stream_t *s, uint8_t **pp_peek, int i_read )
154 {
155     stream_sys_t *p_sys = s->p_sys;
156     int i_res = __MIN( i_read, p_sys->i_size - p_sys->i_pos );
157     *pp_peek = p_sys->p_buffer + p_sys->i_pos;
158     return i_res;
159 }