+/****************************************************************************
+ * Method 3:
+ ****************************************************************************/
+
+static int AStreamReadImmediate( stream_t *s, void *p_read, unsigned int i_read )
+{
+ stream_sys_t *p_sys = s->p_sys;
+ uint8_t *p_data= (uint8_t*)p_read;
+ uint8_t *p_record = p_data;
+ unsigned int i_data;
+
+#ifdef STREAM_DEBUG
+ msg_Dbg( s, "AStreamReadImmediate p_read=%p i_read=%d",
+ p_read, i_read );
+#endif
+
+ if( p_sys->record.b_active && !p_data )
+ p_record = p_data = malloc( i_read );
+
+ /* First, check if we already have some data in the buffer,
+ * that we could copy directly */
+ i_data = __MIN( stream_buffered_size( s ), i_read );
+ if( i_data > 0 )
+ {
+#ifdef STREAM_DEBUG
+ msg_Dbg( s, "AStreamReadImmediate: copy %u from %p", i_data, stream_buffer( s ) );
+#endif
+
+ assert( i_data <= STREAM_CACHE_SIZE );
+
+ if( p_data )
+ {
+ memcpy( p_data, stream_buffer( s ), i_data );
+ p_data += i_data;
+ }
+ }
+
+ /* Now that we've read our buffer we don't need its i_copy bytes */
+ stream_buffer_empty( s, i_data );
+
+ /* Now check if we have still to really read some data */
+ while( i_data < i_read )
+ {
+ const unsigned int i_to_read = i_read - i_data;
+ int i_result;
+
+ if( p_data )
+ {
+ i_result = AReadStream( s, p_data, i_to_read );
+ }
+ else
+ {
+ void *p_dummy = malloc( i_to_read );
+
+ if( !p_dummy )
+ break;
+
+ i_result = AReadStream( s, p_dummy, i_to_read );
+
+ free( p_dummy );
+ }
+ if( i_result <= 0 )
+ break;
+
+ p_sys->i_pos += i_data;
+ if( p_data )
+ p_data += i_result;
+ i_data += i_result;
+ }
+
+ if( p_sys->record.b_active )
+ {
+ if( i_data > 0 && p_record != NULL)
+ ARecordWrite( s, p_record, i_data );
+ if( !p_read )
+ free( p_record );
+ }
+ return i_data;
+}
+
+static int AStreamPeekImmediate( stream_t *s, const uint8_t **pp_peek, unsigned int i_read )
+{
+#ifdef STREAM_DEBUG
+ msg_Dbg( s, "AStreamPeekImmediate: %d size=%"PRId64,
+ i_read, size_buffered_size( s ) );
+#endif
+
+ /* Avoid problem, but that shouldn't happen
+ * FIXME yes it can */
+ if( i_read > STREAM_CACHE_SIZE / 2 )
+ i_read = STREAM_CACHE_SIZE / 2;
+
+ int i_to_read = i_read - stream_buffered_size( s );
+ if( i_to_read > 0 )
+ {
+#ifdef STREAM_DEBUG
+ msg_Dbg( s, "AStreamPeekImmediate: Reading %d",
+ i_to_read );
+#endif
+ i_to_read = AReadStream( s, stream_buffer( s ) + stream_buffered_size( s ),
+ i_to_read );
+
+ if( i_to_read > 0 )
+ stream_buffer_fill( s, i_to_read );
+ }
+
+ *pp_peek = stream_buffer( s );
+
+ return __MIN( stream_buffered_size( s ), i_read );
+}
+
+static int AStreamSeekImmediate( stream_t *s, int64_t i_pos )
+{
+ stream_sys_t *p_sys = s->p_sys;
+ access_t *p_access = p_sys->p_access;
+ bool b_aseek;
+
+#ifdef STREAM_DEBUG
+ msg_Dbg( s, "AStreamSeekImmediate to %"PRId64" pos=%"PRId64
+ i_pos, p_sys->i_pos );
+#endif
+
+ access_Control( p_access, ACCESS_CAN_SEEK, &b_aseek );
+ if( !b_aseek )
+ {
+ /* We can't do nothing */
+ msg_Dbg( s, "AStreamSeekImmediate: can't seek" );
+ return VLC_EGENERIC;
+ }
+
+ /* Just reset our buffer */
+ stream_buffer_empty( s, stream_buffered_size( s ) );
+
+ if( ASeek( s, i_pos ) )
+ return VLC_EGENERIC;
+
+ return VLC_SUCCESS;
+}