Audio lags during streaming on RTMP
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
Which server are you using? If you are using Youtube. Did you try with normal delay instead of low or utra low delay?
Own server. Problem only on Redmi Note 8 Pro (Android 10) and Realme RMX2180. With
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)
I will bought Redmi Note 8 Pro on this week and sent you detailed feedback
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.
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?
In youtube you can follow this guide: https://support.google.com/youtube/answer/7444635?hl=en#zippy=%2Cnormal-latency
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..
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.
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.
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
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
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?)