Skip to content

Commit

Permalink
it plucking works
Browse files Browse the repository at this point in the history
i cannot believe it
i really cannot believe it
  • Loading branch information
Shaji Khan committed Mar 21, 2024
1 parent c2680b0 commit 7fa613f
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 41 deletions.
91 changes: 53 additions & 38 deletions app/src/main/java/com/shajikhan/ladspa/amprack/Camera2.java
Original file line number Diff line number Diff line change
Expand Up @@ -283,9 +283,9 @@ public void closeCamera() {
private void prepareEncoder() {
mBufferInfo = new MediaCodec.BufferInfo();
///| Todo: fixme: get width and height from camera
mWidth = imageDimension.getWidth() ;
mHeight = imageDimension.getHeight() ;
mBitRate = 1000000 ;
mWidth = imageDimension.getWidth();
mHeight = imageDimension.getHeight();
mBitRate = 1000000;
MediaFormat format = MediaFormat.createVideoFormat(MIME_TYPE, mWidth, mHeight);

// Set some properties. Failing to specify some of these can cause the MediaCodec
Expand Down Expand Up @@ -322,12 +322,12 @@ private void prepareEncoder() {
}

// audio record start
audioRecord = new AudioRecord(AUDIO_SOURCE, SAMPLE_RATE, CHANNEL_CONFIG, AUDIO_FORMAT, BUFFER_SIZE_RECORDING);

if (audioRecord.getState() != AudioRecord.STATE_INITIALIZED) { // check for proper initialization
Log.e(TAG, "error initializing " );
return;
}
// audioRecord = new AudioRecord(AUDIO_SOURCE, SAMPLE_RATE, CHANNEL_CONFIG, AUDIO_FORMAT, BUFFER_SIZE_RECORDING);
//
// if (audioRecord.getState() != AudioRecord.STATE_INITIALIZED) { // check for proper initialization
// Log.e(TAG, "error initializing " );
// return;
// }

// audio record end

Expand All @@ -336,24 +336,21 @@ private void prepareEncoder() {
Log.d(TAG, "[audio] prepareEncoder: configured format: " + outputFormat.toString());

mInputSurface = mEncoder.createInputSurface();
mEncoder.setCallback(new EncoderCallback(true));

timestamp = new Timestamp();
// audioEncoder.setCallback(new EncoderCallback(false));

mEncoder.setCallback(new EncoderCallback(true));
audioEncoder.setCallback(new MediaCodec.Callback() {
@Override
public void onInputBufferAvailable(@NonNull MediaCodec codec, int index) {
if (mainActivity.avBuffer.size() == 0) {
codec.queueInputBuffer(index, 0, 0,timestamp.get(), 0);
codec.queueInputBuffer(index, 0, 0, timestamp.get(), 0);
return;
}

MainActivity.AVBuffer avBuffer = mainActivity.avBuffer.pop();
ByteBuffer buffer = codec.getInputBuffer(index);

for (int i = 0 ; i < avBuffer.size ; i++)
buffer.putShort((short) (avBuffer.bytes [i] * 32768.0));
for (int i = 0; i < avBuffer.size; i++)
buffer.putShort((short) (avBuffer.bytes[i] * 32768.0));

codec.queueInputBuffer(index, 0, avBuffer.size * 2, timestamp.get(), 0);

Expand Down Expand Up @@ -392,22 +389,25 @@ public void onError(@NonNull MediaCodec codec, @NonNull MediaCodec.CodecExceptio
@Override
public void onOutputFormatChanged(@NonNull MediaCodec codec, @NonNull MediaFormat format) {
audioTrackIndex = mMuxer.addTrack(codec.getOutputFormat());
Log.d(TAG, String.format ("[audio]: added audio track [%d] with format %s",
Log.d(TAG, String.format("[audio]: added audio track [%d] with format %s",
audioTrackIndex, codec.getOutputFormat()));
}
});

audioEncoder.start();
mEncoder.start();
audioRecord.startRecording();
// audioRecord.startRecording();

// Output filename. Ideally this would use Context.getFilesDir() rather than a
// hard-coded output directory.
audioEncoder.start();
mEncoder.start();

timestamp = new Timestamp();

SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy_HH.mm.ss");
Date date = new Date();
mainActivity.lastRecordedFileName =
String.format("%s/%s.mp4",
mainActivity.getExternalFilesDir(Environment.DIRECTORY_MOVIES).getAbsolutePath(),
mainActivity.getExternalFilesDir(Environment.DIRECTORY_MUSIC).getAbsolutePath(),
formatter.format(date));
String outputPath = mainActivity.lastRecordedFileName;
Log.d(TAG, String.format ("recording video to file: %s", mainActivity.lastRecordedFileName));
Expand Down Expand Up @@ -435,14 +435,21 @@ public void onOutputFormatChanged(@NonNull MediaCodec codec, @NonNull MediaForma
*/
private void releaseEncoder() {
Log.d(TAG, "releaseEncoder: stopping encoder");
mMuxerStarted = false;
mEncoder.signalEndOfInputStream();
timestamp = null ;

if (audioRecord != null)
audioRecord.stop();
// if (audioRecord != null)
// audioRecord.stop();

if (mMuxer != null) {
if (mMuxerStarted)
mMuxer.stop();
mMuxerStarted = false;
mMuxer.release();
mMuxer = null;
}

if (mEncoder != null) {
mEncoder.signalEndOfInputStream();
mEncoder.stop();
mEncoder.release();
mEncoder = null;
Expand All @@ -458,11 +465,6 @@ private void releaseEncoder() {
mInputSurface.release();
mInputSurface = null;
}
if (mMuxer != null) {
mMuxer.stop();
mMuxer.release();
mMuxer = null;
}

mTrackIndex = -1 ;
audioTrackIndex = -1 ;
Expand Down Expand Up @@ -490,29 +492,30 @@ public void onInputBufferAvailable(@NonNull MediaCodec codec, int index) {

@Override
public void onOutputBufferAvailable(@NonNull MediaCodec codec, int index, @NonNull MediaCodec.BufferInfo info) {
if (! mMuxerStarted) {
if (mTrackIndex == -1) {
MediaFormat newFormat = mEncoder.getOutputFormat();
// Log.d(TAG, "encoder output format changed: " + newFormat);

// now that we have the Magic Goodies, start the muxer
if (mTrackIndex == -1)
mTrackIndex = mMuxer.addTrack(newFormat);
// if (mTrackIndex == -1)
mTrackIndex = mMuxer.addTrack(newFormat);

if (audioTrackIndex == -1)
return;
// if (audioTrackIndex == -1)
// return;

mMuxer.setOrientationHint(cameraCharacteristicsHashMap.get(cameraId).get(CameraCharacteristics.SENSOR_ORIENTATION));

// Log.d(TAG, "onOutputBufferAvailable: starting muxer");
mMuxer.start();
mMuxerStarted = true;
// mMuxer.start();
// mMuxerStarted = true;
presentationTimeUs = info.presentationTimeUs;
timestamp.vidstart = info.presentationTimeUs;
}

outPutByteBuffer = codec.getOutputBuffer(index);
info.presentationTimeUs = timestamp.get();
mMuxer.writeSampleData(mTrackIndex, outPutByteBuffer, info);
if (mMuxerStarted)
mMuxer.writeSampleData(mTrackIndex, outPutByteBuffer, info);
codec.releaseOutputBuffer(index, false);

// int bytesWritten = 0 ;
Expand Down Expand Up @@ -546,4 +549,16 @@ public void onOutputFormatChanged(@NonNull MediaCodec codec, @NonNull MediaForma
Log.d(TAG, String.format ("encoder format changed: %s", format.toString()));
}
}

void startRecording () {
Log.d(TAG, "startRecording: ");
mMuxer.start();
mMuxerStarted = true;
}

void stopRecording () {
Log.d(TAG, "stopRecording: ");
mMuxerStarted = false;
mMuxer.stop();
}
}
31 changes: 29 additions & 2 deletions app/src/main/java/com/shajikhan/ladspa/amprack/Rack.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Build;
Expand Down Expand Up @@ -87,7 +88,7 @@ public class Rack extends Fragment {
LinearProgressIndicator quickPatchProgress ;
String TAG = getClass().getSimpleName();
PopupMenu optionsMenu ;
ToggleButton toggleVideo ;
ToggleButton toggleVideo, videoRecord ;
LinearLayout videoPreview ;
TextureView videoTexture ;
LinearLayout mixer ;
Expand Down Expand Up @@ -181,18 +182,44 @@ public void run() {
});

toggleVideo = mainActivity.findViewById(R.id.video_button);
videoRecord = mainActivity.findViewById(R.id.toggle_video);
videoPreview = mainActivity.findViewById(R.id.video_preview);
videoTexture = mainActivity.findViewById(R.id.video_texture);

if (mainActivity.useTheme) {
Bitmap b = mainActivity.skinEngine.bitmapDrawable("card", "bg").getBitmap();
Log.d(TAG, "onViewCreated: " + b.getHeight());
videoPreview.setBackground(new BitmapDrawable(Bitmap.createScaledBitmap(b, 90, (220/90) * b.getHeight(), true)));
// mainActivity.skinEngine.card(videoPreview);
}

videoRecord.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked)
mainActivity.camera2.startRecording();
else
toggleVideo.setChecked(false);
}
});

toggleVideo.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (! mainActivity.running && isChecked) {
MainActivity.toast("Start the audio engine to begin recording");
buttonView.setChecked(false);
return;
}

mainActivity.RequestCamera();
mainActivity.videoRecording = isChecked;
if (!isChecked) {
videoPreview.setVisibility(View.GONE);
boolean mStarted = mainActivity.camera2.mMuxerStarted ;
mainActivity.camera2.closeCamera();
mainActivity.showMediaPlayerDialog();
if (mStarted)
mainActivity.showMediaPlayerDialog();
// mainActivity.avBuffer.clear();
}
else {
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/layout/rack.xml
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,7 @@
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_margin="10dp"
android:background="@drawable/semi_trans"
android:background="@color/black"
android:orientation="vertical"
android:layout_height="220dp">

Expand Down

0 comments on commit 7fa613f

Please sign in to comment.