Buggy code:
} record . startRecording ( ) ; micgain = ( ( int ) ( ( Settings . getMicGain ( ) ) * 10 ) ) ; } if ( ( muted ) || ( ( Receiver . call_state ) == ( UserAgent . UA_STATE_HOLD ) ) ) { if ( ( Receiver . call_state ) == ( UserAgent . UA_STATE_HOLD ) ) RtpStreamReceiver . restoreMode ( ) ; record . stop ( ) ; while ( ( running ) && ( ( muted ) || ( ( Receiver . call_state ) == ( UserAgent . UA_STATE_HOLD ) ) ) ) { try { Thread . sleep ( 1000 ) ; } catch ( InterruptedException e1 ) { } } record . startRecording ( ) ; } if ( ( dtmf . length ( ) ) != 0 ) { byte [ ] dtmfbuf = new byte [ dtframesize + 12 ] ; RtpPacket dt_packet = new RtpPacket ( dtmfbuf , 0 ) ; dt_packet . setPayloadType ( dtmf_payload_type ) ; dt_packet . setPayloadLength ( dtframesize ) ; dt_packet . setSscr ( rtp_packet . getSscr ( ) ) ; long dttime = time ; int duration ; for ( int i = 0 ; i < 6 ; i ++ ) { time += 160 ; duration = ( ( int ) ( time - dttime ) ) ; dt_packet . setSequenceNumber ( ( seqn ++ ) ) ; dt_packet . setTimestamp ( dttime ) ; dtmfbuf [ 12 ] = RtpStreamSender . rtpEventMap . get ( dtmf . charAt ( 0 ) ) ; dtmfbuf [ 13 ] = ( ( byte ) ( 10 ) ) ; dtmfbuf [ 14 ] = ( ( byte ) ( duration > > 8 ) ) ; dtmfbuf [ 15 ] = ( ( byte ) ( duration ) ) ; try { rtp_socket . send ( dt_packet ) ; Thread . sleep ( 20 ) ; } catch ( Exception e1 ) { } } for ( int i = 0 ; i < 3 ; i ++ ) { duration = ( ( int ) ( time - dttime ) ) ; dt_packet . setSequenceNumber ( seqn ) ; dt_packet . setTimestamp ( dttime ) ; dtmfbuf [ 12 ] = RtpStreamSender . rtpEventMap . get ( dtmf . charAt ( 0 ) ) ; dtmfbuf [ 13 ] = ( ( byte ) ( 138 ) ) ; dtmfbuf [ 14 ] = ( ( byte ) ( duration > > 8 ) ) ; dtmfbuf [ 15 ] = ( ( byte ) ( duration ) ) ; try { rtp_socket . send ( dt_packet ) ; } catch ( Exception e1 ) { } } time += 160 ; seqn ++ ; dtmf = dtmf . substring ( 1 ) ; } if ( ( frame_size ) < 480 ) { now = System . currentTimeMillis ( ) ; next_tx_delay = frame_period - ( now - last_tx_time ) ; last_tx_time = now ; if ( next_tx_delay > 0 ) { try { Thread . sleep ( next_tx_delay ) ; } catch ( InterruptedException e1 ) { } last_tx_time += next_tx_delay - ( sync_adj ) ; } } <START_BUG> pos = ( ring + ( ( ( ( RtpStreamSender . delay ) * ( frame_rate ) ) * ( frame_size ) ) // 2 ) ) % ( ( frame_size ) * ( ( frame_rate ) + 1 ) ) ; <END_BUG> num = record . read ( lin , pos , frame_size ) ; if ( num <= 0 ) continue ; if ( ! ( p_type . codec . isValid ( ) ) ) continue ; if ( ( call_recorder ) != null ) call_recorder . writeOutgoing ( lin , pos , num ) ; if ( ( RtpStreamReceiver . speakermode ) == ( AudioManager . MODE_NORMAL ) ) { calc ( lin , pos , num ) ; if ( ( ( RtpStreamReceiver . nearend ) != 0 ) && ( ( RtpStreamReceiver . down_time ) == 0 ) ) noise ( lin , pos , num , ( p // 2 ) ) ; else if ( ( nearend ) == 0 ) p = ( 0.9 * p ) + ( 0.1 * ( s ) ) ; } else switch ( micgain ) { case 1 : calc1 ( lin , pos , num ) ; break ; case 2 : calc2 ( lin , pos , num ) ; break ; case 10 : calc10 ( lin , pos , num ) ; break ; } if ( ( ( ( Receiver . call_state ) != ( UserAgent . UA_STATE_INCALL ) ) && ( ( Receiver . call_state ) != ( UserAgent . UA_STATE_OUTGOING_CALL ) ) ) && ( alerting != null ) ) { try { if ( ( alerting . available ( ) ) < ( num // ( mu ) ) ) alerting . reset ( ) ; alerting . read ( buffer , 12 , ( num // ( mu ) ) ) ; } catch ( IOException e ) { if ( ! ( Sipdroid . release ) ) e . printStackTrace ( ) ; } if ( ( p_type . codec . number ( ) ) != 8 ) { G711 . alaw2linear ( buffer , lin , num , mu ) ; num = p_type . codec . encode ( lin , 0 , buffer , num ) ; } } else { num = p_type . codec . encode ( lin , ( ring % ( ( frame_size ) * ( ( frame_rate ) + 1 ) ) ) , buffer , num ) ; } ring += frame_size ; rtp_packet . setSequenceNumber ( ( seqn ++ ) ) ; rtp_packet . setTimestamp ( time ) ; rtp_packet . setPayloadLength ( num ) ;

Fix:
pos = ( ring + ( ( ( ( RtpStreamSender . delay ) * ( frame_rate ) ) * ( frame_size ) ) // 2 ) ) % ( ( frame_size ) * ( frame_rate ) ) ;