mirror of
				https://gitlab.freedesktop.org/pulseaudio/pulseaudio.git
				synced 2025-11-03 09:01:50 -05:00 
			
		
		
		
	core: adjust playing_for and underrun_for at rewind
A rewind may erase data that sink_input counted in playing_for or underrun_for earlier. Add code adjusting those values after a rewind. One visible symptom of this bug was problems recovering from an underrun. When a client calls pa_stream_write() with a large block of memory, the function can split that into smaller pieces before sending it to the server. When receiving new data for a stream that had silence queued due to underrun, the server would do a rewind to replace the queued-but-not-played silence with the new data. Because of the bug, this rewind itself would not change underrun_for. It's possible for multiple rewinds to be done without filling the sink buffer in between (which is what would eventually reset underrun_for). In this case, the server rapidly processing the split packets would rewind the stream for _each_ of them (as underrun_for would stay set), erasing valid audio as a result.
This commit is contained in:
		
							parent
							
								
									94039790f8
								
							
						
					
					
						commit
						5bc6cadcb2
					
				
					 1 changed files with 22 additions and 4 deletions
				
			
		| 
						 | 
					@ -970,6 +970,15 @@ void pa_sink_input_drop(pa_sink_input *i, size_t nbytes /* in sink sample spec *
 | 
				
			||||||
    pa_memblockq_drop(i->thread_info.render_memblockq, nbytes);
 | 
					    pa_memblockq_drop(i->thread_info.render_memblockq, nbytes);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					static void subtract_helper(size_t *p, size_t amount)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    if (*p == (size_t) -1)
 | 
				
			||||||
 | 
					        return;
 | 
				
			||||||
 | 
					    if (*p < amount)
 | 
				
			||||||
 | 
					        *p = 0;
 | 
				
			||||||
 | 
					    *p -= amount;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/* Called from thread context */
 | 
					/* Called from thread context */
 | 
				
			||||||
void pa_sink_input_process_rewind(pa_sink_input *i, size_t nbytes /* in sink sample spec */) {
 | 
					void pa_sink_input_process_rewind(pa_sink_input *i, size_t nbytes /* in sink sample spec */) {
 | 
				
			||||||
    size_t lbq;
 | 
					    size_t lbq;
 | 
				
			||||||
| 
						 | 
					@ -996,8 +1005,12 @@ void pa_sink_input_process_rewind(pa_sink_input *i, size_t nbytes /* in sink sam
 | 
				
			||||||
        /* We were asked to drop all buffered data, and rerequest new
 | 
					        /* We were asked to drop all buffered data, and rerequest new
 | 
				
			||||||
         * data from implementor the next time peek() is called */
 | 
					         * data from implementor the next time peek() is called */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        size_t s = pa_memblockq_get_length(i->thread_info.render_memblockq);
 | 
				
			||||||
 | 
					        if (i->thread_info.resampler)
 | 
				
			||||||
 | 
					            s = pa_resampler_request(i->thread_info.resampler, s);
 | 
				
			||||||
        pa_memblockq_flush_write(i->thread_info.render_memblockq, TRUE);
 | 
					        pa_memblockq_flush_write(i->thread_info.render_memblockq, TRUE);
 | 
				
			||||||
 | 
					        subtract_helper(&i->thread_info.underrun_for, s);
 | 
				
			||||||
 | 
					        subtract_helper(&i->thread_info.playing_for, s);
 | 
				
			||||||
    } else if (i->thread_info.rewrite_nbytes > 0) {
 | 
					    } else if (i->thread_info.rewrite_nbytes > 0) {
 | 
				
			||||||
        size_t max_rewrite, amount;
 | 
					        size_t max_rewrite, amount;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1012,6 +1025,8 @@ void pa_sink_input_process_rewind(pa_sink_input *i, size_t nbytes /* in sink sam
 | 
				
			||||||
        amount = PA_MIN(i->thread_info.rewrite_nbytes, max_rewrite);
 | 
					        amount = PA_MIN(i->thread_info.rewrite_nbytes, max_rewrite);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if (amount > 0) {
 | 
					        if (amount > 0) {
 | 
				
			||||||
 | 
					            size_t samount = amount;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            pa_log_debug("Have to rewind %lu bytes on implementor.", (unsigned long) amount);
 | 
					            pa_log_debug("Have to rewind %lu bytes on implementor.", (unsigned long) amount);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            /* Tell the implementor */
 | 
					            /* Tell the implementor */
 | 
				
			||||||
| 
						 | 
					@ -1021,11 +1036,14 @@ void pa_sink_input_process_rewind(pa_sink_input *i, size_t nbytes /* in sink sam
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            /* Convert back to to sink domain */
 | 
					            /* Convert back to to sink domain */
 | 
				
			||||||
            if (i->thread_info.resampler)
 | 
					            if (i->thread_info.resampler)
 | 
				
			||||||
                amount = pa_resampler_result(i->thread_info.resampler, amount);
 | 
					                samount = pa_resampler_result(i->thread_info.resampler, amount);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            if (amount > 0)
 | 
					            if (samount > 0) {
 | 
				
			||||||
                /* Ok, now update the write pointer */
 | 
					                /* Ok, now update the write pointer */
 | 
				
			||||||
                pa_memblockq_seek(i->thread_info.render_memblockq, - ((int64_t) amount), PA_SEEK_RELATIVE, TRUE);
 | 
					                pa_memblockq_seek(i->thread_info.render_memblockq, - ((int64_t) samount), PA_SEEK_RELATIVE, TRUE);
 | 
				
			||||||
 | 
					                subtract_helper(&i->thread_info.underrun_for, amount);
 | 
				
			||||||
 | 
					                subtract_helper(&i->thread_info.playing_for, amount);
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            if (i->thread_info.rewrite_flush)
 | 
					            if (i->thread_info.rewrite_flush)
 | 
				
			||||||
                pa_memblockq_silence(i->thread_info.render_memblockq);
 | 
					                pa_memblockq_silence(i->thread_info.render_memblockq);
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue