RootEncoder icon indicating copy to clipboard operation
RootEncoder copied to clipboard

Audio lags during streaming on RTMP

Open insidefun opened this issue 5 years ago • 13 comments

Audio lags during streaming by RTMP on devices below

Audio config: Bitrate: 64*1024 Sample rate: 44100 isStereo: Both

Redmi Note 8 Pro (Android 10) Realme RMX2180

insidefun avatar Dec 08 '20 12:12 insidefun

Which server are you using? If you are using Youtube. Did you try with normal delay instead of low or utra low delay?

pedroSG94 avatar Dec 09 '20 11:12 pedroSG94

Own server. Problem only on Redmi Note 8 Pro (Android 10) and Realme RMX2180. With

insidefun avatar Dec 09 '20 14:12 insidefun

I can't reproduce this problem with my devices. Do you have this problem with other device? Can you tell me server used and configuration? (nginx maybe)

pedroSG94 avatar Dec 14 '20 09:12 pedroSG94

I will bought Redmi Note 8 Pro on this week and sent you detailed feedback

insidefun avatar Dec 14 '20 13:12 insidefun

Ok. If you can reproduce the problem try record a video with my app to discard problems in rtmp module or encoders. If video is fine you have problems with rtmp module, if not, you have problems with encoder module.

pedroSG94 avatar Dec 14 '20 14:12 pedroSG94

Which server are you using? If you are using Youtube. Did you try with normal delay instead of low or utra low delay?

Can you explain please where should I set the delay?

alextestsamsungedge avatar Jan 19 '21 08:01 alextestsamsungedge

In youtube you can follow this guide: https://support.google.com/youtube/answer/7444635?hl=en#zippy=%2Cnormal-latency

pedroSG94 avatar Jan 21 '21 08:01 pedroSG94

I have the same problem with this and my own audio encoder implementation on some of ZTE and Xiaomi devices. It doesn't depends on the any rtmp server. This is MediaCodec problem. It can be reproduced with simple writing to file with MediaMuxer for example. Also I found the issue is gone if set 48000 sample rate..I haven't found the root cause yet:(

P.S. If you need I can record the audio file with this problem..

agent10 avatar Jan 31 '21 21:01 agent10

I can't reproduce it locally when I record with my library and I can confirm that I only found this lagy audio in youtube and amazon servers in low latency configuration using google audio encoder. I say it according with my tests in youtube because If I do the same in facebook or twitch, all is working fine. Related issues by others users: https://github.com/pedroSG94/rtmp-rtsp-stream-client-java/issues/254 https://github.com/pedroSG94/rtmp-rtsp-stream-client-java/issues/254 Anyway if you have a way to fix it or reproduce locally I want know it and make another try to fix it.

pedroSG94 avatar Jan 31 '21 21:01 pedroSG94

I found that MicrophoneMode.SYNC fixes this audio lags..so maybe the problem somehow related to MediaCodec async mode..or maybe there are some issues with audio pts calculation in async mode.

agent10 avatar Jan 31 '21 21:01 agent10

I see.

The only difference beetween both modes is that ASYNC mode use 2 threads to do all (one for microphone and other for mediacodec and it share frames using a sync queue) and SYNC mode only use one thread for all without a queue. In both cases mediacodec is executed in async mode if you are using device with API 23+ as you can see in start method of BaseEncoder class.

In my case this only affect to live stream (and only in display mode) and local file is recorded without audio problems. I didn't know that samplerate could fix the problem in few cases, so I will try abord this problem with that now.

If you find any other thing it is welcome

pedroSG94 avatar Feb 01 '21 13:02 pedroSG94

Found the weird log on my ZTE device:

2021-02-01 19:28:40.816 369-14619/? I/audio_hw_primary: adev_open_input_stream,devices=0x80000004,sample_rate=44100, channel_count=1 2021-02-01 19:28:41.260 369-14619/? D/audio_hw_primary: sink audio_port_config id:1009 role:2 type:2 config_mask:7 sample_rate:44100 channel_mask id:16 format:1 2021-02-01 19:28:41.276 369-21059/? I/audio_hw_primary: check_support_nr nr param switch:0x1 0x0 nr_config:0x3 0x0 rate:44100 2021-02-01 19:28:41.276 369-21059/? I/audio_hw_primary: enable nr process rate:48000 requested_rate:44100

Look at the last msg..it seems that HW enabled 48000 instead of requested value, but our AudioRecord and MediaCodec buffers will work with 44100

agent10 avatar Feb 01 '21 16:02 agent10

Also I think I found out a workaround! Instead of using hardcoded sample rates we can create AudioRecord with AudioFormat.SAMPLE_RATE_UNSPECIFIED(available since 24 api) and then we can get sample rate from created AudioRecord suitable for the audio source..Then we can configure buffers and MediaCodec properly. Does that make sense?)

agent10 avatar Feb 01 '21 18:02 agent10