Skip to content

Commit e1bf495

Browse files
committed
not work for continuous recording
1 parent eab2441 commit e1bf495

File tree

10 files changed

+290
-32
lines changed

10 files changed

+290
-32
lines changed

app/build.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,9 @@ dependencies {
4949
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
5050
exclude group: 'com.android.support', module: 'support-annotations'
5151
})
52+
compile project(':applibs')
5253
compile 'com.github.tajchert:nammu:1.1.1'
5354
compile 'com.android.support:appcompat-v7:25.2.0'
55+
compile 'com.android.support.constraint:constraint-layout:1.0.2'
5456
testCompile 'junit:junit:4.12'
55-
compile project(':applibs')
5657
}

app/src/main/AndroidManifest.xml

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@
1717
~ * * limitations under the License.
1818
~ *
1919
~ */
20-
-->
21-
20+
-->
2221
<manifest package="com.chillingvan.instantvideo.sample"
2322
xmlns:android="http://schemas.android.com/apk/res/android">
2423

@@ -48,8 +47,17 @@
4847
<category android:name="com.chillingvan.instantvideo.sample"/>
4948
</intent-filter>
5049
</activity>
51-
<activity android:name=".test.audio.TestAudioEncoderActivity"
52-
android:label="TestAudioEncoderActivity"
50+
<activity
51+
android:name=".test.audio.TestAudioEncoderActivity"
52+
android:label="TestAudioEncoderActivity">
53+
<intent-filter>
54+
<action android:name="android.intent.action.MAIN"/>
55+
56+
<category android:name="com.chillingvan.instantvideo.sample"/>
57+
</intent-filter>
58+
</activity>
59+
<activity android:name=".test.camera.TestCameraAndVideoActivity"
60+
android:label="TestCameraAndVideoActivity"
5361
>
5462
<intent-filter>
5563
<action android:name="android.intent.action.MAIN"/>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.chillingvan.instantvideo.sample.test.camera;
2+
3+
import android.content.Context;
4+
import android.graphics.SurfaceTexture;
5+
import android.support.annotation.Nullable;
6+
import android.util.AttributeSet;
7+
8+
import com.chillingvan.canvasgl.ICanvasGL;
9+
import com.chillingvan.canvasgl.glcanvas.BasicTexture;
10+
import com.chillingvan.canvasgl.glcanvas.RawTexture;
11+
import com.chillingvan.canvasgl.glview.texture.GLSurfaceTextureProducerView;
12+
import com.chillingvan.canvasgl.textureFilter.BasicTextureFilter;
13+
import com.chillingvan.canvasgl.textureFilter.TextureFilter;
14+
15+
/**
16+
* Created by Leon on 2017/4/19.
17+
*/
18+
19+
public class CameraPreviewTextureView extends GLSurfaceTextureProducerView {
20+
21+
private TextureFilter textureFilter = new BasicTextureFilter();
22+
23+
public CameraPreviewTextureView(Context context) {
24+
super(context);
25+
}
26+
27+
public CameraPreviewTextureView(Context context, AttributeSet attrs) {
28+
super(context, attrs);
29+
}
30+
31+
public CameraPreviewTextureView(Context context, AttributeSet attrs, int defStyleAttr) {
32+
super(context, attrs, defStyleAttr);
33+
}
34+
35+
36+
public void setTextureFilter(TextureFilter textureFilter) {
37+
this.textureFilter = textureFilter;
38+
}
39+
40+
@Override
41+
protected void onGLDraw(ICanvasGL canvas, SurfaceTexture producedSurfaceTexture, RawTexture producedRawTexture, @Nullable SurfaceTexture sharedSurfaceTexture, @Nullable BasicTexture sharedTexture) {
42+
canvas.drawSurfaceTexture(producedRawTexture, producedSurfaceTexture, 0, 0, producedRawTexture.getWidth(), producedRawTexture.getHeight(), textureFilter);
43+
}
44+
}
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
package com.chillingvan.instantvideo.sample.test.camera;
2+
3+
import android.graphics.SurfaceTexture;
4+
import android.hardware.Camera;
5+
import android.os.Bundle;
6+
import android.os.Handler;
7+
import android.os.HandlerThread;
8+
import android.os.Message;
9+
import android.support.annotation.Nullable;
10+
import android.support.v7.app.AppCompatActivity;
11+
import android.util.Log;
12+
import android.view.View;
13+
import android.widget.TextView;
14+
15+
import com.chillingvan.canvasgl.ICanvasGL;
16+
import com.chillingvan.canvasgl.glcanvas.BasicTexture;
17+
import com.chillingvan.canvasgl.glcanvas.RawTexture;
18+
import com.chillingvan.canvasgl.glview.texture.GLSurfaceTextureProducerView;
19+
import com.chillingvan.canvasgl.glview.texture.gles.EglContextWrapper;
20+
import com.chillingvan.canvasgl.glview.texture.gles.GLThread;
21+
import com.chillingvan.instantvideo.sample.R;
22+
import com.chillingvan.instantvideo.sample.test.video.TestVideoEncoder;
23+
import com.chillingvan.lib.camera.InstantVideoCamera;
24+
import com.chillingvan.lib.encoder.video.H264Encoder;
25+
26+
public class TestCameraAndVideoActivity extends AppCompatActivity {
27+
28+
public static final int MESSAGE_START = 1;
29+
public static final int MESSAGE_STOP = 2;
30+
private TestVideoEncoder testVideoEncoder;
31+
private CameraPreviewTextureView cameraPreviewTextureView;
32+
private InstantVideoCamera instantVideoCamera;
33+
private Handler handler;
34+
private HandlerThread inputHandlerThread;
35+
private HandlerThread outputVideoThread;
36+
37+
@Override
38+
protected void onCreate(Bundle savedInstanceState) {
39+
super.onCreate(savedInstanceState);
40+
setContentView(R.layout.activity_camera_and_video);
41+
cameraPreviewTextureView = (CameraPreviewTextureView) findViewById(R.id.camera_produce_view);
42+
instantVideoCamera = new InstantVideoCamera();
43+
44+
45+
outputVideoThread = new HandlerThread("outputVideoThread");
46+
outputVideoThread.start();
47+
48+
final Handler outputVideoHandler = new Handler(outputVideoThread.getLooper()) {
49+
@Override
50+
public void handleMessage(Message msg) {
51+
super.handleMessage(msg);
52+
testVideoEncoder.write();
53+
}
54+
};
55+
56+
inputHandlerThread = new HandlerThread("encoder");
57+
inputHandlerThread.start();
58+
59+
handler = new Handler(inputHandlerThread.getLooper()) {
60+
@Override
61+
public void handleMessage(Message msg) {
62+
super.handleMessage(msg);
63+
if (msg.what == MESSAGE_START) {
64+
if (!testVideoEncoder.isStart()) {
65+
testVideoEncoder.prepareEncoder(new H264Encoder.OnDrawListener() {
66+
@Override
67+
public void onGLDraw(ICanvasGL canvasGL, SurfaceTexture producedSurfaceTexture, RawTexture rawTexture, @Nullable SurfaceTexture outsideSurfaceTexture, @Nullable BasicTexture outsideTexture) {
68+
canvasGL.drawSurfaceTexture(outsideTexture, outsideSurfaceTexture, 0, 0, outsideTexture.getWidth(), outsideTexture.getHeight());
69+
Log.i("TestVideoEncoder", "gl draw");
70+
}
71+
});
72+
testVideoEncoder.start();
73+
}
74+
testVideoEncoder.writeAFrame();
75+
outputVideoHandler.sendEmptyMessage(0);
76+
77+
sendEmptyMessage(MESSAGE_START);
78+
} else if (msg.what == MESSAGE_STOP){
79+
testVideoEncoder.stop();
80+
}
81+
}
82+
};
83+
}
84+
85+
@Override
86+
protected void onResume() {
87+
super.onResume();
88+
instantVideoCamera.openCamera(Camera.CameraInfo.CAMERA_FACING_FRONT, 1280, 720);
89+
initCameraTexture();
90+
cameraPreviewTextureView.onResume();
91+
}
92+
93+
private void initCameraTexture() {
94+
cameraPreviewTextureView.setOnCreateGLContextListener(new GLThread.OnCreateGLContextListener() {
95+
@Override
96+
public void onCreate(EglContextWrapper eglContext) {
97+
testVideoEncoder = new TestVideoEncoder(getApplicationContext(), eglContext);
98+
}
99+
});
100+
cameraPreviewTextureView.setOnSurfaceTextureSet(new GLSurfaceTextureProducerView.OnSurfaceTextureSet() {
101+
@Override
102+
public void onSet(SurfaceTexture surfaceTexture, RawTexture rawTexture) {
103+
testVideoEncoder.setSharedTexture(rawTexture, surfaceTexture);
104+
surfaceTexture.setOnFrameAvailableListener(new SurfaceTexture.OnFrameAvailableListener() {
105+
@Override
106+
public void onFrameAvailable(SurfaceTexture surfaceTexture) {
107+
cameraPreviewTextureView.requestRenderAndWait();
108+
}
109+
});
110+
111+
instantVideoCamera.setPreview(surfaceTexture);
112+
instantVideoCamera.startPreview();
113+
}
114+
});
115+
}
116+
117+
118+
@Override
119+
protected void onPause() {
120+
super.onPause();
121+
instantVideoCamera.release();
122+
cameraPreviewTextureView.onPause();
123+
124+
if (testVideoEncoder.isStart()) {
125+
handler.sendEmptyMessage(MESSAGE_STOP);
126+
}
127+
inputHandlerThread.quit();
128+
outputVideoThread.quit();
129+
}
130+
131+
132+
@Override
133+
protected void onDestroy() {
134+
super.onDestroy();
135+
testVideoEncoder.destroy();
136+
}
137+
public void clickStartTest(View view) {
138+
TextView textView = (TextView) view;
139+
if (testVideoEncoder.isStart()) {
140+
handler.sendEmptyMessage(MESSAGE_STOP);
141+
textView.setText("START");
142+
} else {
143+
handler.sendEmptyMessage(MESSAGE_START);
144+
textView.setText("STOP");
145+
}
146+
}
147+
}

app/src/main/java/com/chillingvan/instantvideo/sample/test/video/TestVideoEncoder.java

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,18 @@
2121
package com.chillingvan.instantvideo.sample.test.video;
2222

2323
import android.content.Context;
24-
import android.graphics.Bitmap;
25-
import android.graphics.BitmapFactory;
2624
import android.graphics.Color;
2725
import android.graphics.Rect;
2826
import android.graphics.SurfaceTexture;
29-
import android.support.annotation.Nullable;
3027
import android.util.Log;
3128

3229
import com.chillingvan.canvasgl.ICanvasGL;
3330
import com.chillingvan.canvasgl.Loggers;
3431
import com.chillingvan.canvasgl.glcanvas.BasicTexture;
3532
import com.chillingvan.canvasgl.glcanvas.GLPaint;
36-
import com.chillingvan.canvasgl.glcanvas.RawTexture;
3733
import com.chillingvan.canvasgl.glview.texture.gles.EglContextWrapper;
38-
import com.chillingvan.instantvideo.sample.R;
39-
import com.chillingvan.lib.encoder.video.H264Encoder;
4034
import com.chillingvan.lib.encoder.MediaCodecInputStream;
35+
import com.chillingvan.lib.encoder.video.H264Encoder;
4136

4237
import java.io.File;
4338
import java.io.FileNotFoundException;
@@ -71,27 +66,11 @@ public TestVideoEncoder(Context ctx, final EglContextWrapper eglCtx) {
7166
}
7267
}
7368

74-
public void prepareEncoder() {
69+
public void prepareEncoder(H264Encoder.OnDrawListener onDrawListener) {
7570
try {
7671
h264Encoder = new H264Encoder(640, 480, 2949120, 30, 5, eglCtx);
7772
h264Encoder.setSharedTexture(outsideTexture, outsideSurfaceTexture);
78-
final Bitmap bitmap = BitmapFactory.decodeResource(ctx.getResources(), R.drawable.lenna);
79-
h264Encoder.setOnDrawListener(new H264Encoder.OnDrawListener() {
80-
@Override
81-
public void onGLDraw(ICanvasGL canvasGL, SurfaceTexture producedSurfaceTexture, RawTexture rawTexture, @Nullable SurfaceTexture outsideSurfaceTexture, @Nullable BasicTexture outsideTexture) {
82-
drawCnt++;
83-
84-
if (drawCnt == 19 || drawCnt == 39) {
85-
canvasGL.drawBitmap(bitmap, 0, 0);
86-
}
87-
drawRect(canvasGL, drawCnt);
88-
89-
if (drawCnt >= 60) {
90-
drawCnt = 0;
91-
}
92-
Log.i("TestVideoEncoder", "gl draw");
93-
}
94-
});
73+
h264Encoder.setOnDrawListener(onDrawListener);
9574
} catch (IOException | IllegalStateException e) {
9675
e.printStackTrace();
9776
}
@@ -121,6 +100,10 @@ public void stop() {
121100
h264Encoder.stop();
122101
}
123102

103+
public boolean isStart() {
104+
return h264Encoder != null && h264Encoder.isStart();
105+
}
106+
124107
public void destroy() {
125108
try {
126109
os.close();

app/src/main/java/com/chillingvan/instantvideo/sample/test/video/TestVideoEncoderActivity.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,26 @@
2020

2121
package com.chillingvan.instantvideo.sample.test.video;
2222

23+
import android.graphics.Bitmap;
24+
import android.graphics.BitmapFactory;
2325
import android.graphics.SurfaceTexture;
2426
import android.os.Bundle;
2527
import android.os.Handler;
2628
import android.os.HandlerThread;
2729
import android.os.Message;
30+
import android.support.annotation.Nullable;
2831
import android.support.v7.app.AppCompatActivity;
32+
import android.util.Log;
2933
import android.view.View;
3034

35+
import com.chillingvan.canvasgl.ICanvasGL;
36+
import com.chillingvan.canvasgl.glcanvas.BasicTexture;
3137
import com.chillingvan.canvasgl.glcanvas.RawTexture;
3238
import com.chillingvan.canvasgl.glview.texture.GLSurfaceTextureProducerView;
3339
import com.chillingvan.canvasgl.glview.texture.gles.EglContextWrapper;
3440
import com.chillingvan.canvasgl.glview.texture.gles.GLThread;
3541
import com.chillingvan.instantvideo.sample.R;
42+
import com.chillingvan.lib.encoder.video.H264Encoder;
3643

3744

3845
public class TestVideoEncoderActivity extends AppCompatActivity {
@@ -88,7 +95,23 @@ public void handleMessage(Message msg) {
8895
public void handleMessage(Message msg) {
8996
super.handleMessage(msg);
9097
if (msg.what == 1) {
91-
testVideoEncoder.prepareEncoder();
98+
final Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.lenna);
99+
testVideoEncoder.prepareEncoder(new H264Encoder.OnDrawListener() {
100+
@Override
101+
public void onGLDraw(ICanvasGL canvasGL, SurfaceTexture producedSurfaceTexture, RawTexture rawTexture, @Nullable SurfaceTexture outsideSurfaceTexture, @Nullable BasicTexture outsideTexture) {
102+
TestVideoEncoder.drawCnt++;
103+
104+
if (TestVideoEncoder.drawCnt == 19 || TestVideoEncoder.drawCnt == 39) {
105+
canvasGL.drawBitmap(bitmap, 0, 0);
106+
}
107+
TestVideoEncoder.drawRect(canvasGL, TestVideoEncoder.drawCnt);
108+
109+
if (TestVideoEncoder.drawCnt >= 60) {
110+
TestVideoEncoder.drawCnt = 0;
111+
}
112+
Log.i("TestVideoEncoder", "gl draw");
113+
}
114+
});
92115
testVideoEncoder.start();
93116
for (int i = 0; i < 120; i++) {
94117
produceTextureView.requestRenderAndWait();
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<android.support.constraint.ConstraintLayout
3+
xmlns:android="http://schemas.android.com/apk/res/android"
4+
xmlns:app="http://schemas.android.com/apk/res-auto"
5+
xmlns:tools="http://schemas.android.com/tools"
6+
android:layout_width="match_parent"
7+
android:layout_height="match_parent"
8+
tools:context="com.chillingvan.instantvideo.sample.test.camera.TestCameraAndVideoActivity">
9+
10+
11+
<Button
12+
android:id="@+id/test_camera_button"
13+
android:layout_width="wrap_content"
14+
android:layout_height="wrap_content"
15+
android:text="record"
16+
android:layout_marginRight="8dp"
17+
app:layout_constraintRight_toRightOf="parent"
18+
android:layout_marginLeft="8dp"
19+
app:layout_constraintLeft_toLeftOf="parent"
20+
app:layout_constraintTop_toTopOf="parent"
21+
android:layout_marginTop="8dp"
22+
app:layout_constraintBottom_toBottomOf="parent"
23+
android:layout_marginBottom="8dp"
24+
android:layout_marginStart="8dp"
25+
android:layout_marginEnd="8dp"
26+
android:onClick="clickStartTest"
27+
/>
28+
29+
30+
<com.chillingvan.instantvideo.sample.test.camera.CameraPreviewTextureView
31+
android:id="@+id/camera_produce_view"
32+
android:layout_width="wrap_content"
33+
android:layout_height="200dp"
34+
app:layout_constraintTop_toTopOf="parent"
35+
android:layout_marginTop="16dp"
36+
android:layout_marginLeft="8dp"
37+
app:layout_constraintLeft_toLeftOf="parent"
38+
android:layout_marginRight="8dp"
39+
app:layout_constraintRight_toRightOf="parent"
40+
android:layout_marginBottom="8dp"
41+
app:layout_constraintBottom_toTopOf="@+id/test_camera_button"/>
42+
</android.support.constraint.ConstraintLayout>

0 commit comments

Comments
 (0)