From 8ddf6c9d5258ac11046fc78b34c7e2c52763df5d Mon Sep 17 00:00:00 2001 From: Shaji Khan Date: Sat, 16 Mar 2024 16:07:53 +0530 Subject: [PATCH] audio is recorded but silent --- .../com/shajikhan/ladspa/amprack/Camera2.java | 24 ++++++++++++------- .../ladspa/amprack/MainActivity.java | 5 +++- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/com/shajikhan/ladspa/amprack/Camera2.java b/app/src/main/java/com/shajikhan/ladspa/amprack/Camera2.java index 18f67dfd..cd752f45 100644 --- a/app/src/main/java/com/shajikhan/ladspa/amprack/Camera2.java +++ b/app/src/main/java/com/shajikhan/ladspa/amprack/Camera2.java @@ -41,6 +41,7 @@ public class Camera2 { final String TAG = getClass().getSimpleName(); + int sampleRate = 48000; // parameters for the encoder private static final String MIME_TYPE = "video/avc"; // H.264 Advanced Video Coding private static final int FRAME_RATE = 30; // 15fps @@ -60,7 +61,7 @@ public class Camera2 { public boolean mMuxerStarted; // allocate one of these up front so we don't need to do it every time - private MediaCodec.BufferInfo mBufferInfo; + public MediaCodec.BufferInfo mBufferInfo; private TextureView textureView; MainActivity mainActivity; @@ -90,6 +91,9 @@ public class Camera2 { Camera2(MainActivity mainActivity_) { mainActivity = mainActivity_; textureView = mainActivity_.rack.videoTexture; + sampleRate = AudioEngine.getSampleRate() ; + if (sampleRate == 0) + sampleRate = 48000 ; } public void openCamera() { @@ -261,7 +265,7 @@ private void prepareEncoder() { format.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, IFRAME_INTERVAL); Log.d(TAG, "format: " + format); - MediaFormat outputFormat = MediaFormat.createAudioFormat("audio/mp4a-latm",AudioEngine.getSampleRate(), 1); + MediaFormat outputFormat = MediaFormat.createAudioFormat("audio/mp4a-latm", sampleRate, 1); outputFormat.setInteger(MediaFormat.KEY_AAC_PROFILE, MediaCodecInfo.CodecProfileLevel.AACObjectLC); outputFormat.setInteger(MediaFormat.KEY_BIT_RATE, 160000); outputFormat.setInteger(MediaFormat.KEY_MAX_INPUT_SIZE, 16384); @@ -322,6 +326,7 @@ private void prepareEncoder() { */ private void releaseEncoder() { Log.d(TAG, "releaseEncoder: stopping encoder"); + mMuxerStarted = false; mEncoder.signalEndOfInputStream(); if (mEncoder != null) { @@ -344,7 +349,6 @@ private void releaseEncoder() { mMuxer.stop(); mMuxer.release(); mMuxer = null; - mMuxerStarted = false; } mTrackIndex = -1 ; @@ -355,9 +359,12 @@ class EncoderCallback extends MediaCodec.Callback { ByteBuffer outPutByteBuffer, inputByteBuffer, audioBuffer; MainActivity.AVBuffer floatBuffer; boolean isVideo ; + MediaCodec.BufferInfo bufferInfo ; EncoderCallback (boolean video) { isVideo = video; + bufferInfo = new MediaCodec.BufferInfo(); + } @Override @@ -378,7 +385,7 @@ public void onInputBufferAvailable(@NonNull MediaCodec codec, int index) { inputByteBuffer = codec.getInputBuffer(index); inputByteBuffer.asFloatBuffer().put(floatBuffer.floatBuffer); presentationTimeUs = 1000000l * index / 48000; - mainActivity.camera2.audioEncoder.queueInputBuffer(index, 0, floatBuffer.size, presentationTimeUs, eos);; + mainActivity.camera2.audioEncoder.queueInputBuffer(index, 0, floatBuffer.size, mBufferInfo.presentationTimeUs, eos);; if (eos != 0) { audioEncoder.signalEndOfInputStream(); @@ -388,6 +395,7 @@ public void onInputBufferAvailable(@NonNull MediaCodec codec, int index) { @Override public void onOutputBufferAvailable(@NonNull MediaCodec codec, int index, @NonNull MediaCodec.BufferInfo info) { + mBufferInfo = info; if (! mMuxerStarted) { MediaFormat newFormat = mEncoder.getOutputFormat(); Log.d(TAG, "encoder output format changed: " + newFormat); @@ -395,7 +403,8 @@ public void onOutputBufferAvailable(@NonNull MediaCodec codec, int index, @NonNu // now that we have the Magic Goodies, start the muxer if (mTrackIndex == -1) mTrackIndex = mMuxer.addTrack(newFormat); - int aIndex = audioEncoder.dequeueOutputBuffer(mBufferInfo, 5000) ; + + int aIndex = audioEncoder.dequeueOutputBuffer(bufferInfo, 5000) ; if (aIndex >= 0) { Log.d(TAG, "onInputBufferAvailable: added audio track"); MediaFormat format = audioEncoder.getOutputFormat(); @@ -419,11 +428,10 @@ public void onOutputBufferAvailable(@NonNull MediaCodec codec, int index, @NonNu // outPutByteBuffer.get(outDate); - - int aIndex = audioEncoder.dequeueOutputBuffer(mBufferInfo, 5000) ; + int aIndex = audioEncoder.dequeueOutputBuffer(bufferInfo, 5000) ; if (aIndex > 0) { audioBuffer = audioEncoder.getOutputBuffer(aIndex); - mMuxer.writeSampleData(audioTrackIndex, audioBuffer, mBufferInfo); + mMuxer.writeSampleData(audioTrackIndex, audioBuffer, bufferInfo); } } diff --git a/app/src/main/java/com/shajikhan/ladspa/amprack/MainActivity.java b/app/src/main/java/com/shajikhan/ladspa/amprack/MainActivity.java index 284d6d1e..befad76d 100644 --- a/app/src/main/java/com/shajikhan/ladspa/amprack/MainActivity.java +++ b/app/src/main/java/com/shajikhan/ladspa/amprack/MainActivity.java @@ -2721,7 +2721,8 @@ static void setTuner (float [] data, int size) { if (inputBufferId >= 0) { ByteBuffer inputBuffer = mainActivity.camera2.audioEncoder.getInputBuffer (inputBufferId); inputBuffer.asFloatBuffer().put(data); - presentationTimeUs = 1000000l * avEncoderIndex / 48000; + presentationTimeUs = presentationTimeUs + size / mainActivity.camera2.sampleRate; + mainActivity.camera2.audioEncoder.queueInputBuffer(inputBufferId, 0, size, presentationTimeUs, 0);; mainActivity.camera2.audioIndex = inputBufferId ; } @@ -2735,6 +2736,8 @@ static void setTuner (float [] data, int size) { mainActivity.camera2.audioEncoder.flush(); */ + } else { + presentationTimeUs = 0 ; } if (! mainActivity.tunerEnabled)