How to handle the PTS correctly using Android AudioRecord and MediaCodec as audio encoder? -


i'm using audiorecord record audio stream during camera capturing process on android device. since want process frame data , handle audio/video samples, not use mediarecorder.

i run audiorecord in thread calling of read() gather raw audio data. once data stream, feed them mediacodec configured aac audio encoder.

here of codes audio recorder / encoder:

m_encode_audio_mime = "audio/mp4a-latm"; m_audio_sample_rate = 44100; m_audio_channels = audioformat.channel_in_mono; m_audio_channel_count = (m_audio_channels == audioformat.channel_in_mono ? 1 : 2);  int audio_bit_rate = 64000; int audio_data_format = audioformat.encoding_pcm_16bit;  m_audio_buffer_size = audiorecord.getminbuffersize(m_audio_sample_rate, m_audio_channels, audio_data_format) * 2; m_audio_recorder = new audiorecord(mediarecorder.audiosource.mic, m_audio_sample_rate,                                    m_audio_channels, audio_data_format, m_audio_buffer_size);  m_audio_encoder = mediacodec.createencoderbytype(m_encode_audio_mime); mediaformat audio_format = new mediaformat(); audio_format.setstring(mediaformat.key_mime, m_encode_audio_mime); audio_format.setinteger(mediaformat.key_bit_rate, audio_bit_rate); audio_format.setinteger(mediaformat.key_channel_count, m_audio_channel_count); audio_format.setinteger(mediaformat.key_sample_rate, m_audio_sample_rate); audio_format.setinteger(mediaformat.key_aac_profile, mediacodecinfo.codecprofilelevel.aacobjectlc); audio_format.setinteger(mediaformat.key_max_input_size, m_audio_buffer_size); m_audio_encoder.configure(audio_format, null, null, mediacodec.configure_flag_encode); 

i found the first time of audiorecord.read() takes longer time return, while successive read() have time intervals more close real time of audio data. example, audio format 44100hz 16bit 1channel, , buffer size of audiorecord 16384, full buffer means 185.76 ms. when record system time each call of read() , subtracting them base time, following sequence:

time before each read(): 0ms, 345ms, 543ms, 692ms, 891ms, 1093ms, 1244ms, ...

i feed these raw data audio encoder above time values pts, , encoder outputs encoded audio samples following pts:

encoder output pts: 0ms, 185ms, 371ms, 557ms, 743ms, 928ms, ...

it looks encoder treats each part of data having same time period. believe encoder works correctly since give raw data same size (16384) every time. however, if use encoder output pts input of muxer, i'll video audio content being faster video content.

i want ask that:

  1. is expected first time of audiorecord.read() blocks longer? i'm sure function call takes more 300ms while records 16384 bytes 186ms. issue depends on device / android version?
  2. what should achieve audio/video synchronization? have workaround measure delay time of first call of read(), shift pts of audio samples delay. there better way handle this?

convert mono input stereo. pulling hair out time before realised aac encoder exposed mediacoder works stereo input.


Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -