]> git.sesse.net Git - vlc/blob - src/input/stream_memory.c
4565828f3fd1e4fefb5f040b482870937918f95b
[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 #undef stream_MemoryNew
45 /**
46  * Create a stream from a memory buffer
47  *
48  * \param p_this the calling vlc_object
49  * \param p_buffer the memory buffer for the stream
50  * \param i_buffer the size of the buffer
51  * \param i_preserve_memory if this is set to false the memory buffer
52  *        pointed to by p_buffer is freed on stream_Destroy
53  */
54 stream_t *stream_MemoryNew( vlc_object_t *p_this, uint8_t *p_buffer,
55                             uint64_t i_size, bool i_preserve_memory )
56 {
57     stream_t *s = stream_CommonNew( p_this );
58     stream_sys_t *p_sys;
59
60     if( !s )
61         return NULL;
62
63     s->psz_path = strdup( "" ); /* N/A */
64     s->p_sys = p_sys = malloc( sizeof( stream_sys_t ) );
65     if( !s->psz_path || !s->p_sys )
66     {
67         stream_CommonDelete( s );
68         return NULL;
69     }
70     p_sys->i_pos = 0;
71     p_sys->i_size = i_size;
72     p_sys->p_buffer = p_buffer;
73     p_sys->i_preserve_memory = i_preserve_memory;
74
75     s->pf_read    = Read;
76     s->pf_peek    = Peek;
77     s->pf_control = Control;
78     s->pf_destroy = Delete;
79     s->p_input = NULL;
80
81     return s;
82 }
83
84 static void Delete( stream_t *s )
85 {
86     if( !s->p_sys->i_preserve_memory ) free( s->p_sys->p_buffer );
87     free( s->p_sys );
88     stream_CommonDelete( s );
89 }
90
91 /****************************************************************************
92  * AStreamControl:
93  ****************************************************************************/
94 static int Control( stream_t *s, int i_query, va_list args )
95 {
96     stream_sys_t *p_sys = s->p_sys;
97
98     bool *p_bool;
99     uint64_t   *pi_64, i_64;
100
101     switch( i_query )
102     {
103         case STREAM_GET_SIZE:
104             pi_64 = va_arg( args, uint64_t * );
105             *pi_64 = p_sys->i_size;
106             break;
107
108         case STREAM_CAN_SEEK:
109             p_bool = (bool*)va_arg( args, bool * );
110             *p_bool = true;
111             break;
112
113         case STREAM_CAN_FASTSEEK:
114             p_bool = (bool*)va_arg( args, bool * );
115             *p_bool = true;
116             break;
117
118         case STREAM_GET_POSITION:
119             pi_64 = va_arg( args, uint64_t * );
120             *pi_64 = p_sys->i_pos;
121             break;
122
123         case STREAM_SET_POSITION:
124             i_64 = va_arg( args, uint64_t );
125             i_64 = __MIN( i_64, s->p_sys->i_size );
126             p_sys->i_pos = i_64;
127             break;
128
129         case STREAM_GET_CONTENT_TYPE:
130             return VLC_EGENERIC;
131
132         case STREAM_CONTROL_ACCESS:
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, unsigned 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, const uint8_t **pp_peek, unsigned 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 }