Main Page | Modules | Class Hierarchy | Class List | Directories | File List | Class Members | File Members | Related Pages

mem_stream.c

00001 /*****************************************************************************
00002  * mem_stream.c: stream_t wrapper around memory buffer
00003  *****************************************************************************
00004  * Copyright (C) 1999-2004 the VideoLAN team
00005  * $Id: stream.c 9390 2004-11-22 09:56:48Z fenrir $
00006  *
00007  * Authors: Sigmund Augdal Helberg <[email protected]>
00008  *
00009  * This program is free software; you can redistribute it and/or modify
00010  * it under the terms of the GNU General Public License as published by
00011  * the Free Software Foundation; either version 2 of the License, or
00012  * (at your option) any later version.
00013  *
00014  * This program is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  * GNU General Public License for more details.
00018  *
00019  * You should have received a copy of the GNU General Public License
00020  * along with this program; if not, write to the Free Software
00021  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
00022  *****************************************************************************/
00023 
00024 #include <stdlib.h>
00025 #include <vlc/vlc.h>
00026 #include <vlc/input.h>
00027 
00028 #include "input_internal.h"
00029 
00030 struct stream_sys_t
00031 {
00032     vlc_bool_t  i_preserve_memory;
00033     int64_t     i_pos;      /* Current reading offset */
00034     int64_t     i_size;
00035     uint8_t    *p_buffer;
00036 
00037 };
00038 
00039 static int  Read   ( stream_t *, void *p_read, int i_read );
00040 static int  Peek   ( stream_t *, uint8_t **pp_peek, int i_read );
00041 static int  Control( stream_t *, int i_query, va_list );
00042 static void Delete ( stream_t * );
00043 
00053 stream_t *__stream_MemoryNew( vlc_object_t *p_this, uint8_t *p_buffer,
00054                               int64_t i_size, vlc_bool_t i_preserve_memory )
00055 {
00056     stream_t *s = vlc_object_create( p_this, VLC_OBJECT_STREAM );
00057     stream_sys_t *p_sys;
00058 
00059     if( !s ) return NULL;
00060 
00061     s->p_sys = p_sys = malloc( sizeof( stream_sys_t ) );
00062     p_sys->i_pos = 0;
00063     p_sys->i_size = i_size;
00064     p_sys->p_buffer = p_buffer;
00065     p_sys->i_preserve_memory = i_preserve_memory;
00066 
00067     s->pf_block   = NULL;
00068     s->pf_read    = Read;
00069     s->pf_peek    = Peek;
00070     s->pf_control = Control;
00071     s->pf_destroy = Delete;
00072     vlc_object_attach( s, p_this );
00073 
00074     return s;
00075 }
00076 
00077 static void Delete( stream_t *s )
00078 {
00079     if( !s->p_sys->i_preserve_memory ) free( s->p_sys->p_buffer );
00080     free( s->p_sys );
00081     vlc_object_detach( s );
00082     vlc_object_destroy( s );
00083 }
00084 
00085 /****************************************************************************
00086  * AStreamControl:
00087  ****************************************************************************/
00088 static int Control( stream_t *s, int i_query, va_list args )
00089 {
00090     stream_sys_t *p_sys = s->p_sys;
00091 
00092     vlc_bool_t *p_bool;
00093     int64_t    *pi_64, i_64;
00094     int        i_int;
00095 
00096     switch( i_query )
00097     {
00098         case STREAM_GET_SIZE:
00099             pi_64 = (int64_t*)va_arg( args, int64_t * );
00100             *pi_64 = p_sys->i_size;
00101             break;
00102 
00103         case STREAM_CAN_SEEK:
00104             p_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
00105             *p_bool = VLC_TRUE;
00106             break;
00107 
00108         case STREAM_CAN_FASTSEEK:
00109             p_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
00110             *p_bool = VLC_TRUE;
00111             break;
00112 
00113         case STREAM_GET_POSITION:
00114             pi_64 = (int64_t*)va_arg( args, int64_t * );
00115             *pi_64 = p_sys->i_pos;
00116             break;
00117 
00118         case STREAM_SET_POSITION:
00119             i_64 = (int64_t)va_arg( args, int64_t );
00120             i_64 = __MAX( i_64, 0 );
00121             i_64 = __MIN( i_64, s->p_sys->i_size ); 
00122             p_sys->i_pos = i_64;
00123             break;
00124 
00125         case STREAM_GET_MTU:
00126             return VLC_EGENERIC;
00127 
00128         case STREAM_CONTROL_ACCESS:
00129             i_int = (int) va_arg( args, int );
00130             msg_Err( s, "Hey, what are you thinking ?"
00131                      "DON'T USE STREAM_CONTROL_ACCESS !!!" );
00132             return VLC_EGENERIC;
00133 
00134         default:
00135             msg_Err( s, "invalid stream_vaControl query=0x%x", i_query );
00136             return VLC_EGENERIC;
00137     }
00138     return VLC_SUCCESS;
00139 }
00140 
00141 static int Read( stream_t *s, void *p_read, int i_read )
00142 {
00143     stream_sys_t *p_sys = s->p_sys;
00144     int i_res = __MIN( i_read, p_sys->i_size - p_sys->i_pos );
00145     memcpy( p_read, p_sys->p_buffer + p_sys->i_pos, i_res );
00146     p_sys->i_pos += i_res;
00147     return i_res;
00148 }
00149 
00150 static int Peek( stream_t *s, uint8_t **pp_peek, int i_read )
00151 {
00152     stream_sys_t *p_sys = s->p_sys;
00153     int i_res = __MIN( i_read, p_sys->i_size - p_sys->i_pos );
00154     *pp_peek = p_sys->p_buffer + p_sys->i_pos;
00155     return i_res;
00156 }

Generated on Tue Dec 20 10:14:59 2005 for vlc-0.8.4a by  doxygen 1.4.2