]> git.sesse.net Git - vlc/blob - src/input/mem_stream.c
Removes trailing spaces. Removes tabs.
[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 <vlc/vlc.h>
25
26 #include "input_internal.h"
27
28 struct stream_sys_t
29 {
30     vlc_bool_t  i_preserve_memory;
31     int64_t     i_pos;      /* Current reading offset */
32     int64_t     i_size;
33     uint8_t    *p_buffer;
34
35 };
36
37 static int  Read   ( stream_t *, void *p_read, int i_read );
38 static int  Peek   ( stream_t *, const uint8_t **pp_peek, int i_read );
39 static int  Control( stream_t *, int i_query, va_list );
40 static void Delete ( stream_t * );
41
42 /**
43  * Create a stream from a memory buffer
44  *
45  * \param p_this the calling vlc_object
46  * \param p_buffer the memory buffer for the stream
47  * \param i_buffer the size of the buffer
48  * \param i_preserve_memory if this is set to VLC_FALSE the memory buffer
49  *        pointed to by p_buffer is freed on stream_Destroy
50  */
51 stream_t *__stream_MemoryNew( vlc_object_t *p_this, uint8_t *p_buffer,
52                               int64_t i_size, vlc_bool_t i_preserve_memory )
53 {
54     stream_t *s = vlc_stream_create( p_this );
55     stream_sys_t *p_sys;
56
57     if( !s ) return NULL;
58
59     s->p_sys = p_sys = malloc( sizeof( stream_sys_t ) );
60     p_sys->i_pos = 0;
61     p_sys->i_size = i_size;
62     p_sys->p_buffer = p_buffer;
63     p_sys->i_preserve_memory = i_preserve_memory;
64
65     s->pf_read    = Read;
66     s->pf_peek    = Peek;
67     s->pf_control = Control;
68     s->pf_destroy = Delete;
69
70     s->i_char_width = 1;
71     s->b_little_endian = VLC_FALSE;
72     vlc_object_attach( s, p_this );
73
74     return s;
75 }
76
77 static void Delete( stream_t *s )
78 {
79     if( !s->p_sys->i_preserve_memory ) free( s->p_sys->p_buffer );
80     free( s->p_sys );
81     vlc_object_detach( s );
82     vlc_object_destroy( s );
83 }
84
85 /****************************************************************************
86  * AStreamControl:
87  ****************************************************************************/
88 static int Control( stream_t *s, int i_query, va_list args )
89 {
90     stream_sys_t *p_sys = s->p_sys;
91
92     vlc_bool_t *p_bool;
93     int64_t    *pi_64, i_64;
94     int        i_int;
95
96     switch( i_query )
97     {
98         case STREAM_GET_SIZE:
99             pi_64 = (int64_t*)va_arg( args, int64_t * );
100             *pi_64 = p_sys->i_size;
101             break;
102
103         case STREAM_CAN_SEEK:
104             p_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
105             *p_bool = VLC_TRUE;
106             break;
107
108         case STREAM_CAN_FASTSEEK:
109             p_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
110             *p_bool = VLC_TRUE;
111             break;
112
113         case STREAM_GET_POSITION:
114             pi_64 = (int64_t*)va_arg( args, int64_t * );
115             *pi_64 = p_sys->i_pos;
116             break;
117
118         case STREAM_SET_POSITION:
119             i_64 = (int64_t)va_arg( args, int64_t );
120             i_64 = __MAX( i_64, 0 );
121             i_64 = __MIN( i_64, s->p_sys->i_size );
122             p_sys->i_pos = i_64;
123             break;
124
125         case STREAM_GET_MTU:
126             return VLC_EGENERIC;
127
128         case STREAM_CONTROL_ACCESS:
129             i_int = (int) va_arg( args, int );
130             msg_Err( s, "Hey, what are you thinking ?"
131                      "DON'T USE STREAM_CONTROL_ACCESS !!!" );
132             return VLC_EGENERIC;
133
134         default:
135             msg_Err( s, "invalid stream_vaControl query=0x%x", i_query );
136             return VLC_EGENERIC;
137     }
138     return VLC_SUCCESS;
139 }
140
141 static int Read( stream_t *s, void *p_read, int i_read )
142 {
143     stream_sys_t *p_sys = s->p_sys;
144     int i_res = __MIN( i_read, p_sys->i_size - p_sys->i_pos );
145     memcpy( p_read, p_sys->p_buffer + p_sys->i_pos, i_res );
146     p_sys->i_pos += i_res;
147     return i_res;
148 }
149
150 static int Peek( stream_t *s, const uint8_t **pp_peek, int i_read )
151 {
152     stream_sys_t *p_sys = s->p_sys;
153     int i_res = __MIN( i_read, p_sys->i_size - p_sys->i_pos );
154     *pp_peek = p_sys->p_buffer + p_sys->i_pos;
155     return i_res;
156 }