Skip to content

Commit

Permalink
faac seems to work and mux properly
Browse files Browse the repository at this point in the history
yet only silent audio
  • Loading branch information
Shaji Khan committed Mar 19, 2024
1 parent 0e213c9 commit 504a7ed
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 16 deletions.
22 changes: 12 additions & 10 deletions app/src/main/cpp/Meter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

#define TUNER_ARRAY_SIZE 4096

unsigned char * Meter::audioToVideoBytes = NULL ;
jchar * Meter::audioToVideoBytes = NULL ;
faacEncHandle Meter::faacEncHandle = nullptr;
jfloatArray Meter::jfloatArray1 ;
int Meter::jfloatArray1_index = 0 ;
Expand Down Expand Up @@ -46,7 +46,7 @@ bool Meter::enabled = false ;
float Meter::lastTotal = 0 ;
bool Meter::isInput = true;

jbyteArray pushVideoSamples = nullptr;
jcharArray pushVideoSamples = nullptr;

JNIEnv* getEnv() {
JNIEnv *env;
Expand Down Expand Up @@ -135,7 +135,7 @@ int Meter::updateMeterOutput (AudioBuffer * buffer) {
setTuner = envOutput->GetStaticMethodID(mainActivityOutput, "setTuner",
"([FI)V");
pushToVideo = envOutput->GetStaticMethodID(mainActivityOutput, "pushToVideo",
"([BI)V");
"([CI)V");
if (setMixerMeterOutput == nullptr) {
LOGF("cannot find method!");
}
Expand All @@ -146,8 +146,8 @@ int Meter::updateMeterOutput (AudioBuffer * buffer) {

// this should never be more than this
jfloatArray1 = envOutput->NewFloatArray(TUNER_ARRAY_SIZE);
pushVideoSamples = envOutput->NewByteArray(TUNER_ARRAY_SIZE);
audioToVideoBytes = (unsigned char *) malloc(sizeof(char) * TUNER_ARRAY_SIZE);
pushVideoSamples = envOutput->NewCharArray(TUNER_ARRAY_SIZE);
audioToVideoBytes = (jchar *) malloc(sizeof(jchar) * TUNER_ARRAY_SIZE);
jfloatArray1_index = 0 ;
return 0 ;
} else {
Expand All @@ -164,8 +164,8 @@ int Meter::updateMeterOutput (AudioBuffer * buffer) {
if (videoRecording) {
int bytesWritten = faacEncode(data, samples, audioToVideoBytes, TUNER_ARRAY_SIZE);
if (bytesWritten >= 0) {
envOutput->SetByteArrayRegion(pushVideoSamples, 0, bytesWritten,
(jbyte *) audioToVideoBytes);
envOutput->SetCharArrayRegion(pushVideoSamples, 0, bytesWritten+1,
audioToVideoBytes);
envOutput->CallStaticVoidMethod(mainActivityOutput, pushToVideo, pushVideoSamples,
bytesWritten);
}
Expand Down Expand Up @@ -445,6 +445,7 @@ void Meter::process (int nframes, const float * data, bool isInput) {
}

void Meter::faacInit (int sampleRate, unsigned long maxSamples) {
LOGD("[faac] init %d %d", sampleRate, maxSamples);
unsigned long maxBytes = TUNER_ARRAY_SIZE ;
jack_samplerate = sampleRate ;
faacEncHandle = faacEncOpen(
Expand All @@ -470,15 +471,16 @@ void Meter::faacConfig () {
faacEncSetConfiguration(faacEncHandle, config);
}

int Meter::faacEncode (float * data, int nframes, unsigned char *outputBuffer,
unsigned int bufferSize) {
int bytesWritten = faacEncEncode(faacEncHandle, reinterpret_cast<int32_t *>(data), nframes, outputBuffer, bufferSize) ;
int Meter::faacEncode (float * data, int nframes, jchar *outputBuffer,
unsigned int bufferSize) {
int bytesWritten = faacEncEncode(faacEncHandle, (int *) data, nframes, (unsigned char *) outputBuffer, bufferSize) ;
if (bytesWritten < 0) {
LOGE("[faac] error: %d", bytesWritten);
}

// for (int i = 0 ; i < bytesWritten ; i ++)
// LOGD("%c", outputBuffer [i]);
//
LOGD("[faac] %d: {%d:%d}", bytesWritten, outputBuffer [0], outputBuffer [bytesWritten]);
return bytesWritten;
}
4 changes: 2 additions & 2 deletions app/src/main/cpp/Meter.h
Original file line number Diff line number Diff line change
Expand Up @@ -225,9 +225,9 @@ class Meter {

void faacConfig();

static int faacEncode(float * data, int nframes, unsigned char *outputBuffer, unsigned int bufferSize);
static int faacEncode(float * data, int nframes, jchar *outputBuffer, unsigned int bufferSize);

static unsigned char *audioToVideoBytes;
static jchar *audioToVideoBytes;
};

#endif //AMP_RACK_METER_H
7 changes: 5 additions & 2 deletions app/src/main/java/com/shajikhan/ladspa/amprack/Camera2.java
Original file line number Diff line number Diff line change
Expand Up @@ -472,9 +472,12 @@ public void onOutputBufferAvailable(@NonNull MediaCodec codec, int index, @NonNu
return;

MainActivity.AVBuffer avBuffer = mainActivity.avBuffer.pop();
ByteBuffer buffer = ByteBuffer.allocate(avBuffer.size * 2);
for (int i = 0; i < avBuffer.size ; i ++)
buffer.putChar(i, avBuffer.bytes[i]);
bufferInfo.set(0, avBuffer.size, info.presentationTimeUs, 0);
mMuxer.writeSampleData(audioTrackIndex, ByteBuffer.wrap(avBuffer.bytes), bufferInfo);
Log.d(TAG, String.format ("[audio video]: %d {%d:%d}", bufferInfo.size, avBuffer.bytes [0], avBuffer.bytes [bufferInfo.size]));
mMuxer.writeSampleData(audioTrackIndex, buffer, bufferInfo);
Log.d(TAG, String.format ("[muxer]: %d {%d:%d}", bufferInfo.size, (int) avBuffer.bytes [0], (int) avBuffer.bytes [bufferInfo.size]));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ public class MainActivity extends AppCompatActivity implements ActivityCompat.On
boolean videoRecording = false ;
Camera2 camera2 ;
static class AVBuffer {
byte [] bytes ;
char [] bytes ;
int size ;
}
public static LinkedList<AVBuffer> avBuffer = new LinkedList<>();
Expand Down Expand Up @@ -3432,7 +3432,7 @@ int getCameraSensorOrientation(CameraCharacteristics characteristics) {
return (360 - (cameraOrientation != null ? cameraOrientation : 0)) % 360;
}

public static void pushToVideo (byte [] data, int nframes) {
public static void pushToVideo (char [] data, int nframes) {
// Log.d(TAG, String.format ("%d: %f - %f", nframes, data [0], data [nframes - 1]));
if (! mainActivity.videoRecording)
return;
Expand Down

0 comments on commit 504a7ed

Please sign in to comment.