Skip to content

Commit

Permalink
commit
Browse files Browse the repository at this point in the history
  • Loading branch information
HelloHuDi committed May 21, 2018
1 parent b7fb599 commit 6fb64eb
Show file tree
Hide file tree
Showing 15 changed files with 775 additions and 128 deletions.
36 changes: 33 additions & 3 deletions app/src/main/java/com/hd/screen/capture/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package com.hd.screen.capture;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.text.format.DateUtils;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.hd.screencapture.ScreenCapture;
import com.hd.screencapture.callback.ScreenCaptureCallback;
import com.hd.screencapture.callback.ScreenCaptureStreamCallback;
import com.hd.screencapture.config.AudioConfig;
import com.hd.screencapture.config.ScreenCaptureConfig;
import com.hd.screencapture.config.VideoConfig;
Expand All @@ -18,21 +21,28 @@
/**
* Created by hd on 2018/5/14 .
*/
public class MainActivity extends AppCompatActivity implements ScreenCaptureCallback {
public class MainActivity extends AppCompatActivity implements ScreenCaptureStreamCallback {

private ScreenCapture screenCapture;

private boolean isRunning;

private TextView tvTime,tvVideoHeaderData,tvVideoData,tvAudioData;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvTime=findViewById(R.id.tvTime);
tvVideoHeaderData=findViewById(R.id.tvVideoHeaderData);
tvVideoData=findViewById(R.id.tvVideoData);
tvAudioData=findViewById(R.id.tvAudioData);
init();
}

private void init() {
ScreenCaptureConfig captureConfig = new ScreenCaptureConfig.Builder()//
.setAllowLog(BuildConfig.DEBUG)
.setVideoConfig(VideoConfig.initDefaultConfig(this))//
.setAudioConfig(AudioConfig.initDefaultConfig())//
.setCaptureCallback(this)//
Expand Down Expand Up @@ -64,8 +74,28 @@ public void captureState(ScreenCaptureState state) {
runOnUiThread(() -> Toast.makeText(MainActivity.this, "capture state ==>" + state, Toast.LENGTH_SHORT).show());
}

@SuppressLint("SetTextI18n")
@Override
public void captureTime(long time) {
Log.d("tag", "capture time ==>" + time + "===" + DateUtils.formatElapsedTime(time));
runOnUiThread(() -> tvTime.setText("capture time ==>"+ DateUtils.formatElapsedTime(time)));
}

@SuppressLint("SetTextI18n")
@Override
public void videoHeaderByte(@NonNull byte[] sps, @NonNull byte[] pps) {
runOnUiThread(() -> tvVideoHeaderData.setText("video header byte length ==> sps len: " + sps.length +", pps len : "+ pps.length));
}

@SuppressLint("SetTextI18n")
@Override
public void videoContentByte(@NonNull byte[] content) {
runOnUiThread(() -> tvVideoData.setText("video content byte len ==> " + content.length));

}

@SuppressLint("SetTextI18n")
@Override
public void audioContentByte(@NonNull byte[] content) {
runOnUiThread(() -> tvAudioData.setText("audio content byte len ==> " + content.length));
}
}
29 changes: 28 additions & 1 deletion app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
Expand All @@ -20,4 +19,32 @@
android:onClick="stopCapture"
android:layout_height="58dp"/>

<TextView
android:id="@+id/tvTime"
android:layout_width="match_parent"
android:gravity="center|left"
android:text="capture time ==> 0 "
android:layout_height="58dp"/>

<TextView
android:id="@+id/tvVideoHeaderData"
android:layout_width="match_parent"
android:gravity="center|left"
android:text="video header byte length ==>"
android:layout_height="58dp"/>

<TextView
android:id="@+id/tvVideoData"
android:layout_width="match_parent"
android:gravity="center|left"
android:text="video content byte len ==>"
android:layout_height="58dp"/>

<TextView
android:id="@+id/tvAudioData"
android:layout_width="match_parent"
android:gravity="center|left"
android:text="audio content byte len ==>"
android:layout_height="58dp"/>

</LinearLayout>
38 changes: 27 additions & 11 deletions screencapture/src/main/java/com/hd/screencapture/ScreenCapture.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.atomic.AtomicBoolean;


/**
Expand All @@ -33,8 +34,8 @@ public static ScreenCapture with(@NonNull AppCompatActivity activity) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
throw new RuntimeException("the sdk version less than 21 equipment does not provide this function !");
}
if(!Utils.isExternalStorageReady()){
Log.e(TAG,"current no storage space");
if (!Utils.isExternalStorageReady()) {
Log.e(TAG, "current no storage space");
}
return new ScreenCapture(activity);
}
Expand All @@ -43,6 +44,8 @@ public static ScreenCapture with(@NonNull AppCompatActivity activity) {

private CaptureObserver observer;

protected AtomicBoolean capture = new AtomicBoolean(false);

private ScreenCapture(@NonNull AppCompatActivity activity) {
//add lifecycle observer
observer = new ScreenCaptureObserver(this);
Expand Down Expand Up @@ -78,24 +81,37 @@ public ScreenCapture setConfig(@NonNull ScreenCaptureConfig config) {
return this;
}

public boolean isRunning(){
return capture.get();
}

public void startCapture() {
startCapture(-1);
}

public void startCapture(long duration) {
screenCaptureFragment.startCapture();
if (duration > 0) {
new Timer().schedule(new TimerTask() {
@Override
public void run() {
stopCapture();
}
}, duration);
if (!isRunning()) {
capture.set(true);
screenCaptureFragment.startCapture();
if (duration > 0) {
new Timer().schedule(new TimerTask() {
@Override
public void run() {
stopCapture();
}
}, duration);
}
} else {
Log.e(TAG, "capturing !!!");
}
}

public void stopCapture() {
screenCaptureFragment.stopCapture();
if (isRunning()) {
screenCaptureFragment.stopCapture();
} else {
Log.e(TAG, "stop capture always");
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.hd.screencapture.callback;

import android.media.MediaCodec;
import android.media.MediaFormat;

/**
* Created by hd on 2018/5/20 .
*/
public interface RecorderCallback {

void onInputBufferAvailable(int index);

void onOutputFormatChanged(MediaFormat format);

void onOutputBufferAvailable(int index, MediaCodec.BufferInfo info);

void onError(Exception exception);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.hd.screencapture.callback;

import android.support.annotation.NonNull;

/**
* Created by hd on 2018/5/21 .
*/
public interface ScreenCaptureStreamCallback extends ScreenCaptureCallback {

void videoHeaderByte(@NonNull byte[] sps,@NonNull byte[] pps);

void videoContentByte(@NonNull byte[] content);

void audioContentByte(@NonNull byte[] content);
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,48 @@
package com.hd.screencapture.capture;

import android.annotation.TargetApi;
import android.os.Build;
import android.support.annotation.NonNull;

import com.hd.screencapture.callback.RecorderCallback;
import com.hd.screencapture.config.ScreenCaptureConfig;
import com.hd.screencapture.observer.CaptureObserver;

import java.nio.ByteBuffer;

/**
* Created by hd on 2018/5/20 .
*/
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public class AudioRecorder extends Recorder {

public AudioRecorder(@NonNull CaptureObserver observer, @NonNull ScreenCaptureConfig config,//
@NonNull RecorderCallback callback) {
super(observer, config, callback);
}

@Override
public boolean prepare() {
return true;
}

@Override
public boolean record() {
return true;
}

@Override
public void release() {

}

public void releaseOutputBuffer(int index) {
}


public ByteBuffer getOutputBuffer(int index) {
// return mEncoder.getOutputBuffer(index);
return null;
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,36 @@
package com.hd.screencapture.capture;

import android.annotation.TargetApi;
import android.os.Build;
import android.support.annotation.NonNull;

import com.hd.screencapture.callback.RecorderCallback;
import com.hd.screencapture.config.ScreenCaptureConfig;
import com.hd.screencapture.observer.CaptureObserver;

/**
* Created by hd on 2018/5/20 .
*/
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public abstract class Recorder {

CaptureObserver observer;

ScreenCaptureConfig config;

RecorderCallback callback;

Recorder(@NonNull CaptureObserver observer,@NonNull ScreenCaptureConfig config,//
@NonNull RecorderCallback callback) {
this.observer = observer;
this.config = config;
this.callback=callback;
}

public abstract boolean prepare();

public abstract boolean record();

public abstract void release();

}
Loading

0 comments on commit 6fb64eb

Please sign in to comment.