+#ifdef STREAM_DEBUG
+ msg_Dbg( s, "AStreamReadStream: %d pos=%"PRId64" tk=%d start=%"PRId64
+ " offset=%d end=%"PRId64,
+ i_read, p_sys->i_pos, p_sys->stream.i_tk,
+ tk->i_start, p_sys->stream.i_offset, tk->i_end );
+#endif
+
+ while( i_data < i_read )
+ {
+ unsigned i_off = (tk->i_start + p_sys->stream.i_offset) % STREAM_CACHE_TRACK_SIZE;
+ unsigned int i_current =
+ __MIN( tk->i_end - tk->i_start - p_sys->stream.i_offset,
+ STREAM_CACHE_TRACK_SIZE - i_off );
+ int i_copy = __MIN( i_current, i_read - i_data );
+
+ if( i_copy <= 0 ) break; /* EOF */
+
+ /* Copy data */
+ /* msg_Dbg( s, "AStreamReadStream: copy %d", i_copy ); */
+ if( p_data )
+ {
+ memcpy( p_data, &tk->p_buffer[i_off], i_copy );
+ p_data += i_copy;
+ }
+ i_data += i_copy;
+ p_sys->stream.i_offset += i_copy;
+
+ /* Update pos now */
+ p_sys->i_pos += i_copy;
+
+ /* */
+ p_sys->stream.i_used += i_copy;
+
+ if( tk->i_end + i_data <= tk->i_start + p_sys->stream.i_offset + i_read )
+ {
+ const unsigned i_read_requested = __MAX( __MIN( i_read - i_data,
+ STREAM_READ_ATONCE * 10 ),
+ STREAM_READ_ATONCE / 2 );
+
+ if( p_sys->stream.i_used < i_read_requested )
+ p_sys->stream.i_used = i_read_requested;
+
+ if( AStreamRefillStream( s ) )
+ {
+ /* EOF */
+ if( tk->i_start >= tk->i_end ) break;
+ }
+ }
+ }
+
+ return i_data;