| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
#ifdef HAVE_CONFIG_H |
|---|
| 25 |
# include "config.h" |
|---|
| 26 |
#endif |
|---|
| 27 |
|
|---|
| 28 |
#include <vlc_common.h> |
|---|
| 29 |
|
|---|
| 30 |
#include "input_internal.h" |
|---|
| 31 |
|
|---|
| 32 |
struct stream_sys_t |
|---|
| 33 |
{ |
|---|
| 34 |
bool i_preserve_memory; |
|---|
| 35 |
int64_t i_pos; |
|---|
| 36 |
int64_t i_size; |
|---|
| 37 |
uint8_t *p_buffer; |
|---|
| 38 |
|
|---|
| 39 |
}; |
|---|
| 40 |
|
|---|
| 41 |
static int Read ( stream_t *, void *p_read, unsigned int i_read ); |
|---|
| 42 |
static int Peek ( stream_t *, const uint8_t **pp_peek, unsigned int i_read ); |
|---|
| 43 |
static int Control( stream_t *, int i_query, va_list ); |
|---|
| 44 |
static void Delete ( stream_t * ); |
|---|
| 45 |
|
|---|
| 46 |
|
|---|
| 47 |
|
|---|
| 48 |
|
|---|
| 49 |
|
|---|
| 50 |
|
|---|
| 51 |
|
|---|
| 52 |
|
|---|
| 53 |
|
|---|
| 54 |
|
|---|
| 55 |
stream_t *__stream_MemoryNew( vlc_object_t *p_this, uint8_t *p_buffer, |
|---|
| 56 |
int64_t i_size, bool i_preserve_memory ) |
|---|
| 57 |
{ |
|---|
| 58 |
stream_t *s = vlc_stream_create( p_this ); |
|---|
| 59 |
stream_sys_t *p_sys; |
|---|
| 60 |
|
|---|
| 61 |
if( !s ) return NULL; |
|---|
| 62 |
|
|---|
| 63 |
s->p_sys = p_sys = malloc( sizeof( stream_sys_t ) ); |
|---|
| 64 |
p_sys->i_pos = 0; |
|---|
| 65 |
p_sys->i_size = i_size; |
|---|
| 66 |
p_sys->p_buffer = p_buffer; |
|---|
| 67 |
p_sys->i_preserve_memory = i_preserve_memory; |
|---|
| 68 |
|
|---|
| 69 |
s->pf_read = Read; |
|---|
| 70 |
s->pf_peek = Peek; |
|---|
| 71 |
s->pf_control = Control; |
|---|
| 72 |
s->pf_destroy = Delete; |
|---|
| 73 |
|
|---|
| 74 |
s->i_char_width = 1; |
|---|
| 75 |
s->b_little_endian = false; |
|---|
| 76 |
vlc_object_attach( s, p_this ); |
|---|
| 77 |
|
|---|
| 78 |
return s; |
|---|
| 79 |
} |
|---|
| 80 |
|
|---|
| 81 |
static void Delete( stream_t *s ) |
|---|
| 82 |
{ |
|---|
| 83 |
if( !s->p_sys->i_preserve_memory ) free( s->p_sys->p_buffer ); |
|---|
| 84 |
free( s->p_sys ); |
|---|
| 85 |
vlc_object_detach( s ); |
|---|
| 86 |
vlc_object_release( s ); |
|---|
| 87 |
} |
|---|
| 88 |
|
|---|
| 89 |
|
|---|
| 90 |
|
|---|
| 91 |
|
|---|
| 92 |
static int Control( stream_t *s, int i_query, va_list args ) |
|---|
| 93 |
{ |
|---|
| 94 |
stream_sys_t *p_sys = s->p_sys; |
|---|
| 95 |
|
|---|
| 96 |
bool *p_bool; |
|---|
| 97 |
int64_t *pi_64, i_64; |
|---|
| 98 |
int i_int; |
|---|
| 99 |
|
|---|
| 100 |
switch( i_query ) |
|---|
| 101 |
{ |
|---|
| 102 |
case STREAM_GET_SIZE: |
|---|
| 103 |
pi_64 = (int64_t*)va_arg( args, int64_t * ); |
|---|
| 104 |
*pi_64 = p_sys->i_size; |
|---|
| 105 |
break; |
|---|
| 106 |
|
|---|
| 107 |
case STREAM_CAN_SEEK: |
|---|
| 108 |
p_bool = (bool*)va_arg( args, bool * ); |
|---|
| 109 |
*p_bool = true; |
|---|
| 110 |
break; |
|---|
| 111 |
|
|---|
| 112 |
case STREAM_CAN_FASTSEEK: |
|---|
| 113 |
p_bool = (bool*)va_arg( args, bool * ); |
|---|
| 114 |
*p_bool = true; |
|---|
| 115 |
break; |
|---|
| 116 |
|
|---|
| 117 |
case STREAM_GET_POSITION: |
|---|
| 118 |
pi_64 = (int64_t*)va_arg( args, int64_t * ); |
|---|
| 119 |
*pi_64 = p_sys->i_pos; |
|---|
| 120 |
break; |
|---|
| 121 |
|
|---|
| 122 |
case STREAM_SET_POSITION: |
|---|
| 123 |
i_64 = (int64_t)va_arg( args, int64_t ); |
|---|
| 124 |
i_64 = __MAX( i_64, 0 ); |
|---|
| 125 |
i_64 = __MIN( i_64, s->p_sys->i_size ); |
|---|
| 126 |
p_sys->i_pos = i_64; |
|---|
| 127 |
break; |
|---|
| 128 |
|
|---|
| 129 |
case STREAM_GET_MTU: |
|---|
| 130 |
case STREAM_GET_CONTENT_TYPE: |
|---|
| 131 |
return VLC_EGENERIC; |
|---|
| 132 |
|
|---|
| 133 |
case STREAM_CONTROL_ACCESS: |
|---|
| 134 |
i_int = (int) va_arg( args, int ); |
|---|
| 135 |
msg_Err( s, "Hey, what are you thinking ?" |
|---|
| 136 |
"DON'T USE STREAM_CONTROL_ACCESS !!!" ); |
|---|
| 137 |
return VLC_EGENERIC; |
|---|
| 138 |
|
|---|
| 139 |
default: |
|---|
| 140 |
msg_Err( s, "invalid stream_vaControl query=0x%x", i_query ); |
|---|
| 141 |
return VLC_EGENERIC; |
|---|
| 142 |
} |
|---|
| 143 |
return VLC_SUCCESS; |
|---|
| 144 |
} |
|---|
| 145 |
|
|---|
| 146 |
static int Read( stream_t *s, void *p_read, unsigned int i_read ) |
|---|
| 147 |
{ |
|---|
| 148 |
stream_sys_t *p_sys = s->p_sys; |
|---|
| 149 |
int i_res = __MIN( i_read, p_sys->i_size - p_sys->i_pos ); |
|---|
| 150 |
memcpy( p_read, p_sys->p_buffer + p_sys->i_pos, i_res ); |
|---|
| 151 |
p_sys->i_pos += i_res; |
|---|
| 152 |
return i_res; |
|---|
| 153 |
} |
|---|
| 154 |
|
|---|
| 155 |
static int Peek( stream_t *s, const uint8_t **pp_peek, unsigned int i_read ) |
|---|
| 156 |
{ |
|---|
| 157 |
stream_sys_t *p_sys = s->p_sys; |
|---|
| 158 |
int i_res = __MIN( i_read, p_sys->i_size - p_sys->i_pos ); |
|---|
| 159 |
*pp_peek = p_sys->p_buffer + p_sys->i_pos; |
|---|
| 160 |
return i_res; |
|---|
| 161 |
} |
|---|