]> git.sesse.net Git - vlc/blob - src/input/mem_stream.c
mem_stream.c: minor cleanup + shut up compiler warnings
[vlc] / src / input / mem_stream.c
1 /*****************************************************************************
2  * mem_stream.c
3  *****************************************************************************
4  * Copyright (C) 1999-2004 VideoLAN
5  * $Id: stream.c 9390 2004-11-22 09:56:48Z fenrir $
6  *
7  * Authors: Sigmund Augdal <sigmunau@idi.ntnu.no>
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., 59 Temple Place - Suite 330, Boston, MA  02111, 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     int64_t     i_pos;      /* Current reading offset */
33     int64_t     i_size;
34     uint8_t    *p_buffer;
35
36 };
37
38 static int  AStreamReadMem( stream_t *, void *p_read, int i_read );
39 static int  AStreamPeekMem( stream_t *, uint8_t **pp_peek, int i_read );
40 static int  AStreamControl( stream_t *, int i_query, va_list );
41
42 /****************************************************************************
43  * stream_MemoryNew: create a stream from a buffer
44  ****************************************************************************/
45 stream_t *__stream_MemoryNew( vlc_object_t *p_this, uint8_t *p_buffer,
46                               int64_t i_size )
47 {
48     stream_t *s = vlc_object_create( p_this, VLC_OBJECT_STREAM );
49     stream_sys_t *p_sys;
50
51     if( !s )
52         return NULL;
53
54     s->p_sys = p_sys = malloc( sizeof( stream_sys_t ) );
55     p_sys->i_pos = 0;
56     p_sys->i_size = i_size;
57     p_sys->p_buffer = p_buffer;
58
59     s->pf_block  = NULL;
60     s->pf_read   = AStreamReadMem;    /* Set up later */
61     s->pf_peek   = AStreamPeekMem;
62     s->pf_control= AStreamControl;
63     vlc_object_attach( s, p_this );
64     
65     return s;
66 }
67
68 void stream_MemoryDelete( stream_t *s, vlc_bool_t b_free_buffer )
69 {
70     if( b_free_buffer )
71     {
72         free( s->p_sys->p_buffer );
73     }
74     free( s->p_sys );
75     vlc_object_destroy( s );
76 }
77 /****************************************************************************
78  * AStreamControl:
79  ****************************************************************************/
80 static int AStreamControl( stream_t *s, int i_query, va_list args )
81 {
82     stream_sys_t *p_sys = s->p_sys;
83
84     vlc_bool_t *p_bool;
85     int64_t    *pi_64, i_64;
86     int        i_int;
87
88     switch( i_query )
89     {
90         case STREAM_GET_SIZE:
91             pi_64 = (int64_t*)va_arg( args, int64_t * );
92             *pi_64 = p_sys->i_size;
93             break;
94
95         case STREAM_CAN_SEEK:
96             p_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
97             *p_bool = VLC_TRUE;
98             break;
99
100         case STREAM_CAN_FASTSEEK:
101             p_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
102             *p_bool = VLC_TRUE;
103             break;
104
105         case STREAM_GET_POSITION:
106             pi_64 = (int64_t*)va_arg( args, int64_t * );
107             *pi_64 = p_sys->i_pos;
108             break;
109
110         case STREAM_SET_POSITION:
111             i_64 = (int64_t)va_arg( args, int64_t );
112             i_64 = __MAX( i_64, 0 );
113             i_64 = __MIN( i_64, s->p_sys->i_size ); 
114             p_sys->i_pos = i_64;
115
116         case STREAM_GET_MTU:
117             return VLC_EGENERIC;
118
119         case STREAM_CONTROL_ACCESS:
120             i_int = (int) va_arg( args, int );
121             msg_Err( s, "Hey, what are you thinking ?"
122                      "DON'T USE STREAM_CONTROL_ACCESS !!!" );
123             return VLC_EGENERIC;
124
125         default:
126             msg_Err( s, "invalid stream_vaControl query=0x%x", i_query );
127             return VLC_EGENERIC;
128     }
129     return VLC_SUCCESS;
130 }
131
132 static int  AStreamReadMem( stream_t *s, void *p_read, int i_read )
133 {
134     stream_sys_t *p_sys = s->p_sys;
135     int i_res = __MIN( i_read, p_sys->i_size - p_sys->i_pos );
136     memcpy( p_read, p_sys->p_buffer + p_sys->i_pos, i_res );
137     p_sys->i_pos += i_res;
138     return i_res;
139 }
140
141 static int  AStreamPeekMem( stream_t *s, uint8_t **pp_peek, 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     *pp_peek = p_sys->p_buffer + p_sys->i_pos;
146     return i_res;
147 }