Skip to content

Commit

Permalink
Adds FFmpegFilterFrame option in Android sample
Browse files Browse the repository at this point in the history
  • Loading branch information
pachev committed Nov 4, 2016
1 parent 32d1ff4 commit 00db458
Showing 1 changed file with 42 additions and 11 deletions.
53 changes: 42 additions & 11 deletions samples/RecordActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,7 @@
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.LinearLayout; import android.widget.RelativeLayout;

import java.io.IOException;
import java.nio.ByteBuffer;
Expand All @@ -90,12 +89,15 @@
import java.util.Comparator;
import java.util.List;

import org.bytedeco.javacv.Frame;
import org.bytedeco.javacpp.avutil;
import org.bytedeco.javacv.FFmpegFrameFilter;
import org.bytedeco.javacv.FFmpegFrameRecorder;
import org.bytedeco.javacv.Frame;
import org.bytedeco.javacv.FrameFilter;

public class RecordActivity extends Activity implements OnClickListener {

private final static String CLASS_LABEL = "RecordActivity";
private final static String CLASS_LABEL = "MainActivity";
private final static String LOG_TAG = CLASS_LABEL;

private String ffmpeg_link = "/mnt/sdcard/stream.flv";
Expand All @@ -107,6 +109,11 @@ public class RecordActivity extends Activity implements OnClickListener {

private boolean isPreviewOn = false;

/*Filter information, change boolean to true if adding a fitler*/
private boolean addFilter = false;
private String filterString = "";
FFmpegFrameFilter filter;

private int sampleAudioRateInHz = 44100;
private int imageWidth = 320;
private int imageHeight = 240;
Expand Down Expand Up @@ -177,8 +184,8 @@ private void initLayout() {
Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
screenWidth = display.getWidth();
screenHeight = display.getHeight();
RelativeLayout.LayoutParams layoutParam = null;
LayoutInflater myInflate = null;
RelativeLayout.LayoutParams layoutParam = null;
LayoutInflater myInflate = null;
myInflate = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
RelativeLayout topLayout = new RelativeLayout(this);
setContentView(topLayout);
Expand Down Expand Up @@ -363,9 +370,9 @@ public void run() {
ShortBuffer audioData;
int bufferReadResult;

bufferSize = AudioRecord.getMinBufferSize(sampleAudioRateInHz,
bufferSize = AudioRecord.getMinBufferSize(sampleAudioRateInHz,
AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT);
audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC, sampleAudioRateInHz,
audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC, sampleAudioRateInHz,
AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT, bufferSize);

if (RECORD_LENGTH > 0) {
Expand Down Expand Up @@ -472,7 +479,7 @@ public int compare(final Camera.Size a, final Camera.Size b) {
camParams.setPreviewSize(imageWidth, imageHeight);

Log.v(LOG_TAG,"Setting imageWidth: " + imageWidth + " imageHeight: " + imageHeight + " frameRate: " + frameRate);

camParams.setPreviewFrameRate(frameRate);
Log.v(LOG_TAG,"Preview Framerate: " + camParams.getPreviewFrameRate());

Expand Down Expand Up @@ -523,6 +530,8 @@ public void onPreviewFrame(byte[] data, Camera camera) {
yuvImage = images[i];
timestamps[i] = 1000 * (System.currentTimeMillis() - startTime);
}


/* get video data */
if (yuvImage != null && recording) {
((ByteBuffer)yuvImage.image[0].position(0)).put(data);
Expand All @@ -533,8 +542,30 @@ public void onPreviewFrame(byte[] data, Camera camera) {
if (t > recorder.getTimestamp()) {
recorder.setTimestamp(t);
}
recorder.record(yuvImage);
} catch (FFmpegFrameRecorder.Exception e) {

// Add a filter for each of your frames. the filterString is any
// ffmpeg filter. Here is the link for a list: https://ffmpeg.org/ffmpeg-filters.html
if(addFilter) {

filterString = "transpose=0";
filter = new FFmpegFrameFilter(filterString, imageWidth, imageHeight);

//default format on android
filter.setPixelFormat(avutil.AV_PIX_FMT_NV21);

//Must be initalized before it is applied to every frame
filter.start();


filter.push(yuvImage);
Frame frame2;
while ((frame2 = filter.pull()) != null) {
recorder.record(frame2);
}
} else {
recorder.record(yuvImage);
}
} catch (FFmpegFrameRecorder.Exception | FrameFilter.Exception e) {
Log.v(LOG_TAG,e.getMessage());
e.printStackTrace();
}
Expand Down

0 comments on commit 00db458

Please sign in to comment.