1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 1999-2004 VideoLAN
5 * $Id: stream.c 9390 2004-11-22 09:56:48Z fenrir $
7 * Authors: Sigmund Augdal <sigmunau@idi.ntnu.no>
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.
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.
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 *****************************************************************************/
26 #include <vlc/input.h>
28 #include "input_internal.h"
32 int64_t i_pos; /* Current reading offset */
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 AStreamSeekMem( stream_t *s, int64_t i_pos );
41 static int AStreamControl( stream_t *, int i_query, va_list );
43 /****************************************************************************
44 * stream_MemoryNew: create a stream from a buffer
45 ****************************************************************************/
46 stream_t *__stream_MemoryNew( vlc_object_t *p_this, uint8_t *p_buffer,
49 stream_t *s = vlc_object_create( p_this, VLC_OBJECT_STREAM );
55 s->p_sys = p_sys = malloc( sizeof( stream_sys_t ) );
57 p_sys->i_size = i_size;
58 p_sys->p_buffer = p_buffer;
61 s->pf_read = AStreamReadMem; /* Set up later */
62 s->pf_peek = AStreamPeekMem;
63 s->pf_control= AStreamControl;
64 vlc_object_attach( s, p_this );
69 void stream_MemoryDelete( stream_t *s, vlc_bool_t b_free_buffer )
73 free( s->p_sys->p_buffer );
76 vlc_object_destroy( s );
78 /****************************************************************************
80 ****************************************************************************/
81 static int AStreamControl( stream_t *s, int i_query, va_list args )
83 stream_sys_t *p_sys = s->p_sys;
92 pi_64 = (int64_t*)va_arg( args, int64_t * );
93 *pi_64 = p_sys->i_size;
97 p_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
101 case STREAM_CAN_FASTSEEK:
102 p_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
106 case STREAM_GET_POSITION:
107 pi_64 = (int64_t*)va_arg( args, int64_t * );
108 *pi_64 = p_sys->i_pos;
111 case STREAM_SET_POSITION:
112 i_64 = (int64_t)va_arg( args, int64_t );
118 case STREAM_CONTROL_ACCESS:
119 i_int = (int) va_arg( args, int );
120 msg_Err( s, "Hey, what are you thinking ?"
121 "DON'T USE STREAM_CONTROL_ACCESS !!!" );
125 msg_Err( s, "invalid stream_vaControl query=0x%x", i_query );
131 static int AStreamReadMem( stream_t *s, void *p_read, int i_read )
133 stream_sys_t *p_sys = s->p_sys;
134 int i_res = __MIN( i_read, p_sys->i_size - p_sys->i_pos );
135 memcpy( p_read, p_sys->p_buffer + p_sys->i_pos, i_res );
136 p_sys->i_pos += i_res;
140 static int AStreamPeekMem( stream_t *s, uint8_t **pp_peek, int i_read )
142 stream_sys_t *p_sys = s->p_sys;
143 int i_res = __MIN( i_read, p_sys->i_size - p_sys->i_pos );
144 *pp_peek = p_sys->p_buffer + p_sys->i_pos;
148 static int AStreamSeekMem( stream_t *s, int64_t i_pos )
151 i_new_pos = __MAX( i_pos, 0 );
152 i_new_pos = __MIN( i_new_pos, s->p_sys->i_size );
153 s->p_sys->i_pos = i_new_pos;