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 size_t i_pos
; /* Current reading offset */
37 static ssize_t
Read( stream_t
*, void *p_read
, size_t i_read
);
38 static int Seek( stream_t
*, uint64_t );
39 static int Control( stream_t
*, int i_query
, va_list );
41 static void stream_MemoryPreserveDelete(stream_t
*s
)
46 static void stream_MemoryDelete(stream_t
*s
)
48 stream_sys_t
*sys
= s
->p_sys
;
51 stream_MemoryPreserveDelete(s
);
54 stream_t
*(vlc_stream_MemoryNew
)(vlc_object_t
*p_this
, uint8_t *p_buffer
,
55 size_t i_size
, bool preserve
)
57 stream_t
*s
= vlc_stream_CommonNew( p_this
,
58 preserve
? stream_MemoryPreserveDelete
59 : stream_MemoryDelete
);
65 s
->p_sys
= p_sys
= malloc( sizeof( stream_sys_t
) );
68 stream_CommonDelete( s
);
72 p_sys
->i_size
= i_size
;
73 p_sys
->p_buffer
= p_buffer
;
77 s
->pf_control
= Control
;
82 /****************************************************************************
84 ****************************************************************************/
85 static int Control( stream_t
*s
, int i_query
, va_list args
)
87 stream_sys_t
*p_sys
= s
->p_sys
;
94 pi_64
= va_arg( args
, uint64_t * );
95 *pi_64
= p_sys
->i_size
;
99 case STREAM_CAN_FASTSEEK
:
100 case STREAM_CAN_PAUSE
:
101 case STREAM_CAN_CONTROL_PACE
:
102 *va_arg( args
, bool * ) = true;
105 case STREAM_GET_PTS_DELAY
:
106 *va_arg( args
, int64_t * ) = 0;
109 case STREAM_GET_TITLE_INFO
:
110 case STREAM_GET_TITLE
:
111 case STREAM_GET_SEEKPOINT
:
112 case STREAM_GET_META
:
113 case STREAM_GET_CONTENT_TYPE
:
114 case STREAM_GET_SIGNAL
:
115 case STREAM_SET_TITLE
:
116 case STREAM_SET_SEEKPOINT
:
119 case STREAM_SET_PAUSE_STATE
:
120 break; /* nothing to do */
122 case STREAM_SET_PRIVATE_ID_STATE
:
123 case STREAM_SET_PRIVATE_ID_CA
:
124 case STREAM_GET_PRIVATE_ID_STATE
:
125 msg_Err( s
, "Hey, what are you thinking? "
126 "DO NOT USE PRIVATE STREAM CONTROLS!!!" );
130 msg_Err( s
, "invalid vlc_stream_vaControl query=0x%x", i_query
);
136 static ssize_t
Read( stream_t
*s
, void *p_read
, size_t i_read
)
138 stream_sys_t
*p_sys
= s
->p_sys
;
140 if( i_read
> p_sys
->i_size
- p_sys
->i_pos
)
141 i_read
= p_sys
->i_size
- p_sys
->i_pos
;
143 memcpy( p_read
, p_sys
->p_buffer
+ p_sys
->i_pos
, i_read
);
144 p_sys
->i_pos
+= i_read
;
148 static int Seek( stream_t
*s
, uint64_t offset
)
150 stream_sys_t
*p_sys
= s
->p_sys
;
152 if( offset
> p_sys
->i_size
)
153 offset
= p_sys
->i_size
;
155 p_sys
->i_pos
= offset
;