1 /*****************************************************************************
2 * stream_memory.c: stream_t wrapper around memory buffer
3 *****************************************************************************
4 * Copyright (C) 1999-2008 VLC authors and VideoLAN
7 * Authors: Sigmund Augdal Helberg <dnumgis@videolan.org>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
32 bool i_preserve_memory
;
33 uint64_t i_pos
; /* Current reading offset */
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
* );
44 #undef stream_MemoryNew
46 * Create a stream from a memory buffer
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
54 stream_t
*stream_MemoryNew( vlc_object_t
*p_this
, uint8_t *p_buffer
,
55 uint64_t i_size
, bool i_preserve_memory
)
57 stream_t
*s
= stream_CommonNew( p_this
);
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
)
67 stream_CommonDelete( s
);
71 p_sys
->i_size
= i_size
;
72 p_sys
->p_buffer
= p_buffer
;
73 p_sys
->i_preserve_memory
= i_preserve_memory
;
77 s
->pf_control
= Control
;
78 s
->pf_destroy
= Delete
;
84 static void Delete( stream_t
*s
)
86 if( !s
->p_sys
->i_preserve_memory
) free( s
->p_sys
->p_buffer
);
88 stream_CommonDelete( s
);
91 /****************************************************************************
93 ****************************************************************************/
94 static int Control( stream_t
*s
, int i_query
, va_list args
)
96 stream_sys_t
*p_sys
= s
->p_sys
;
98 uint64_t *pi_64
, i_64
;
102 case STREAM_GET_SIZE
:
103 pi_64
= va_arg( args
, uint64_t * );
104 *pi_64
= p_sys
->i_size
;
107 case STREAM_CAN_SEEK
:
108 case STREAM_CAN_FASTSEEK
:
109 case STREAM_CAN_PAUSE
:
110 case STREAM_CAN_CONTROL_PACE
:
111 *va_arg( args
, bool * ) = true;
114 case STREAM_GET_POSITION
:
115 pi_64
= va_arg( args
, uint64_t * );
116 *pi_64
= p_sys
->i_pos
;
119 case STREAM_SET_POSITION
:
120 i_64
= va_arg( args
, uint64_t );
121 i_64
= __MIN( i_64
, s
->p_sys
->i_size
);
125 case STREAM_GET_TITLE_INFO
:
126 case STREAM_GET_META
:
127 case STREAM_GET_CONTENT_TYPE
:
128 case STREAM_GET_SIGNAL
:
129 case STREAM_SET_TITLE
:
130 case STREAM_SET_SEEKPOINT
:
133 case STREAM_SET_PAUSE_STATE
:
134 break; /* nothing to do */
136 case STREAM_CONTROL_ACCESS
:
137 msg_Err( s
, "Hey, what are you thinking ?"
138 "DON'T USE STREAM_CONTROL_ACCESS !!!" );
142 msg_Err( s
, "invalid stream_vaControl query=0x%x", i_query
);
148 static int Read( stream_t
*s
, void *p_read
, unsigned int i_read
)
150 stream_sys_t
*p_sys
= s
->p_sys
;
151 int i_res
= __MIN( i_read
, p_sys
->i_size
- p_sys
->i_pos
);
152 memcpy( p_read
, p_sys
->p_buffer
+ p_sys
->i_pos
, i_res
);
153 p_sys
->i_pos
+= i_res
;
157 static int Peek( stream_t
*s
, const uint8_t **pp_peek
, unsigned int i_read
)
159 stream_sys_t
*p_sys
= s
->p_sys
;
160 int i_res
= __MIN( i_read
, p_sys
->i_size
- p_sys
->i_pos
);
161 *pp_peek
= p_sys
->p_buffer
+ p_sys
->i_pos
;