Skip to content

Commit 224c04e

Browse files
committed
support gl canvas 1.4.0.0
1 parent 19e9d73 commit 224c04e

File tree

11 files changed

+118
-84
lines changed

11 files changed

+118
-84
lines changed

app/src/main/java/com/chillingvan/instantvideo/sample/test/camera/CameraPreviewTextureView.java

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,22 @@
77
import android.graphics.Paint;
88
import android.graphics.PorterDuff;
99
import android.graphics.SurfaceTexture;
10-
import android.support.annotation.Nullable;
1110
import android.util.AttributeSet;
1211

1312
import com.chillingvan.canvasgl.ICanvasGL;
1413
import com.chillingvan.canvasgl.androidCanvas.IAndroidCanvasHelper;
15-
import com.chillingvan.canvasgl.glcanvas.BasicTexture;
16-
import com.chillingvan.canvasgl.glcanvas.RawTexture;
17-
import com.chillingvan.canvasgl.glview.texture.GLSurfaceTextureProducerView;
14+
import com.chillingvan.canvasgl.glview.texture.GLMultiTexProducerView;
15+
import com.chillingvan.canvasgl.glview.texture.GLTexture;
16+
import com.chillingvan.canvasgl.glview.texture.gles.EglContextWrapper;
1817
import com.chillingvan.lib.encoder.video.H264Encoder;
1918

19+
import java.util.List;
20+
2021
/**
2122
* Created by Leon on 2017/4/19.
2223
*/
2324

24-
public class CameraPreviewTextureView extends GLSurfaceTextureProducerView {
25+
public class CameraPreviewTextureView extends GLMultiTexProducerView {
2526

2627
private H264Encoder.OnDrawListener onDrawListener;
2728
private IAndroidCanvasHelper drawTextHelper = IAndroidCanvasHelper.Factory.createAndroidCanvasHelper(IAndroidCanvasHelper.MODE.MODE_ASYNC);
@@ -39,6 +40,14 @@ public CameraPreviewTextureView(Context context, AttributeSet attrs, int defStyl
3940
super(context, attrs, defStyleAttr);
4041
}
4142

43+
@Override
44+
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
45+
super.onSurfaceTextureAvailable(surface, width, height);
46+
if (mGLThread == null) {
47+
setSharedEglContext(EglContextWrapper.EGL_NO_CONTEXT_WRAPPER);
48+
}
49+
}
50+
4251
@Override
4352
public void onSurfaceChanged(int width, int height) {
4453
super.onSurfaceChanged(width, height);
@@ -48,13 +57,9 @@ public void onSurfaceChanged(int width, int height) {
4857
textPaint.setTextSize(dp2px(15));
4958
}
5059

51-
private float dp2px(int dp) {
52-
return dp * getContext().getResources().getDisplayMetrics().density;
53-
}
54-
5560
@Override
56-
protected void onGLDraw(final ICanvasGL canvas, SurfaceTexture producedSurfaceTexture, RawTexture producedRawTexture, @Nullable SurfaceTexture sharedSurfaceTexture, @Nullable BasicTexture sharedTexture) {
57-
onDrawListener.onGLDraw(canvas, producedSurfaceTexture, producedRawTexture, sharedSurfaceTexture, sharedTexture);
61+
protected void onGLDraw(ICanvasGL canvas, List<GLTexture> producedTextures, List<GLTexture> consumedTextures) {
62+
onDrawListener.onGLDraw(canvas, producedTextures, consumedTextures);
5863
drawTextHelper.draw(new IAndroidCanvasHelper.CanvasPainter() {
5964
@Override
6065
public void draw(Canvas androidCanvas) {
@@ -67,6 +72,10 @@ public void draw(Canvas androidCanvas) {
6772
canvas.drawBitmap(outputBitmap, 0, 0);
6873
}
6974

75+
private float dp2px(int dp) {
76+
return dp * getContext().getResources().getDisplayMetrics().density;
77+
}
78+
7079
public void setOnDrawListener(H264Encoder.OnDrawListener l) {
7180
onDrawListener = l;
7281
}

app/src/main/java/com/chillingvan/instantvideo/sample/test/camera/TestCameraAndVideoActivity.java

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,23 @@
66
import android.os.Handler;
77
import android.os.HandlerThread;
88
import android.os.Message;
9-
import android.support.annotation.Nullable;
109
import android.support.v7.app.AppCompatActivity;
11-
import android.util.Log;
1210
import android.view.View;
1311
import android.widget.TextView;
1412

1513
import com.chillingvan.canvasgl.ICanvasGL;
16-
import com.chillingvan.canvasgl.glcanvas.BasicTexture;
1714
import com.chillingvan.canvasgl.glcanvas.RawTexture;
18-
import com.chillingvan.canvasgl.glview.texture.GLSurfaceTextureProducerView;
15+
import com.chillingvan.canvasgl.glview.texture.GLMultiTexProducerView;
16+
import com.chillingvan.canvasgl.glview.texture.GLTexture;
1917
import com.chillingvan.canvasgl.glview.texture.gles.EglContextWrapper;
2018
import com.chillingvan.canvasgl.glview.texture.gles.GLThread;
2119
import com.chillingvan.instantvideo.sample.R;
2220
import com.chillingvan.instantvideo.sample.test.video.TestVideoEncoder;
2321
import com.chillingvan.lib.camera.InstantVideoCamera;
2422
import com.chillingvan.lib.encoder.video.H264Encoder;
2523

24+
import java.util.List;
25+
2626
/**
2727
* Data Stream:
2828
* Camera -> SurfaceTexture -> Surface -> MediaCodec -> encode data(byte[]) -> File
@@ -42,7 +42,11 @@ protected void onCreate(Bundle savedInstanceState) {
4242
cameraPreviewTextureView = (CameraPreviewTextureView) findViewById(R.id.camera_produce_view);
4343
cameraPreviewTextureView.setOnDrawListener(new H264Encoder.OnDrawListener() {
4444
@Override
45-
public void onGLDraw(ICanvasGL canvasGL, SurfaceTexture surfaceTexture, RawTexture rawTexture, @Nullable SurfaceTexture outsideSurfaceTexture, @Nullable BasicTexture outsideTexture) {
45+
public void onGLDraw(ICanvasGL canvasGL, List<GLTexture> producedTextures, List<GLTexture> consumedTextures) {
46+
47+
GLTexture texture = producedTextures.get(0);
48+
SurfaceTexture surfaceTexture = texture.getSurfaceTexture();
49+
RawTexture rawTexture = texture.getRawTexture();
4650
canvasGL.drawSurfaceTexture(rawTexture, surfaceTexture, 0, 0, rawTexture.getWidth(), rawTexture.getHeight());
4751
}
4852
});
@@ -81,10 +85,12 @@ public void onCreate(EglContextWrapper eglContext) {
8185
testVideoEncoder = new TestVideoEncoder(getApplicationContext(), eglContext);
8286
}
8387
});
84-
cameraPreviewTextureView.setOnSurfaceTextureSet(new GLSurfaceTextureProducerView.OnSurfaceTextureSet() {
88+
cameraPreviewTextureView.setSurfaceTextureCreatedListener(new GLMultiTexProducerView.SurfaceTextureCreatedListener() {
8589
@Override
86-
public void onSet(SurfaceTexture surfaceTexture, RawTexture rawTexture) {
87-
testVideoEncoder.setSharedTexture(rawTexture, surfaceTexture);
90+
public void onCreated(List<GLTexture> producedTextureList) {
91+
GLTexture texture = producedTextureList.get(0);
92+
SurfaceTexture surfaceTexture = texture.getSurfaceTexture();
93+
testVideoEncoder.addSharedTexture(texture.getRawTexture(), surfaceTexture);
8894
surfaceTexture.setOnFrameAvailableListener(new SurfaceTexture.OnFrameAvailableListener() {
8995
@Override
9096
public void onFrameAvailable(SurfaceTexture surfaceTexture) {
@@ -128,10 +134,13 @@ public void clickStartTest(View view) {
128134
} else {
129135
testVideoEncoder.prepareEncoder(new H264Encoder.OnDrawListener() {
130136
@Override
131-
public void onGLDraw(ICanvasGL canvasGL, SurfaceTexture producedSurfaceTexture, RawTexture rawTexture, @Nullable SurfaceTexture outsideSurfaceTexture, @Nullable BasicTexture outsideTexture) {
137+
public void onGLDraw(ICanvasGL canvasGL, List<GLTexture> producedTextures, List<GLTexture> consumedTextures) {
138+
GLTexture texture = consumedTextures.get(0);
139+
SurfaceTexture outsideSurfaceTexture = texture.getSurfaceTexture();
140+
RawTexture outsideTexture = texture.getRawTexture();
132141
canvasGL.drawSurfaceTexture(outsideTexture, outsideSurfaceTexture, 0, 0, outsideTexture.getWidth(), outsideTexture.getHeight());
133-
Log.i("TestVideoEncoder", "gl draw");
134142
}
143+
135144
});
136145
testVideoEncoder.start();
137146
textView.setText("STOP");

app/src/main/java/com/chillingvan/instantvideo/sample/test/publisher/TestCameraPublisherActivity.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,10 @@
3434

3535
import com.chillingvan.canvasgl.ICanvasGL;
3636
import com.chillingvan.canvasgl.glcanvas.BasicTexture;
37-
import com.chillingvan.canvasgl.glcanvas.RawTexture;
37+
import com.chillingvan.canvasgl.glview.texture.GLTexture;
3838
import com.chillingvan.canvasgl.textureFilter.BasicTextureFilter;
3939
import com.chillingvan.canvasgl.textureFilter.HueFilter;
4040
import com.chillingvan.canvasgl.textureFilter.TextureFilter;
41-
import com.chillingvan.canvasgl.util.Loggers;
4241
import com.chillingvan.instantvideo.sample.R;
4342
import com.chillingvan.instantvideo.sample.test.camera.CameraPreviewTextureView;
4443
import com.chillingvan.lib.camera.InstantVideoCamera;
@@ -48,6 +47,7 @@
4847
import com.chillingvan.lib.publisher.StreamPublisher;
4948

5049
import java.io.IOException;
50+
import java.util.List;
5151

5252
public class TestCameraPublisherActivity extends AppCompatActivity {
5353

@@ -62,12 +62,15 @@ public class TestCameraPublisherActivity extends AppCompatActivity {
6262
protected void onCreate(Bundle savedInstanceState) {
6363
super.onCreate(savedInstanceState);
6464
setContentView(R.layout.activity_test_camera_publisher);
65-
cameraPreviewTextureView = (CameraPreviewTextureView) findViewById(R.id.camera_produce_view);
65+
cameraPreviewTextureView = findViewById(R.id.camera_produce_view);
6666
cameraPreviewTextureView.setOnDrawListener(new H264Encoder.OnDrawListener() {
6767
@Override
68-
public void onGLDraw(ICanvasGL canvasGL, SurfaceTexture surfaceTexture, RawTexture rawTexture, @Nullable SurfaceTexture outsideSurfaceTexture, @Nullable BasicTexture outsideTexture) {
69-
drawVideoFrame(canvasGL, surfaceTexture, rawTexture);
68+
public void onGLDraw(ICanvasGL canvasGL, List<GLTexture> producedTextures, List<GLTexture> consumedTextures) {
69+
70+
GLTexture texture = producedTextures.get(0);
71+
drawVideoFrame(canvasGL, texture.getSurfaceTexture(), texture.getRawTexture());
7072
}
73+
7174
});
7275
addrEditText = (EditText) findViewById(R.id.ip_input_test);
7376

@@ -88,10 +91,9 @@ public void handleMessage(Message msg) {
8891
// streamPublisherParam.outputFilePath = getExternalFilesDir(null) + "/test_mp4_encode.mp4";
8992
streamPublisher.prepareEncoder(streamPublisherParam, new H264Encoder.OnDrawListener() {
9093
@Override
91-
public void onGLDraw(ICanvasGL canvasGL, SurfaceTexture surfaceTexture, RawTexture rawTexture, @Nullable SurfaceTexture outsideSurfaceTexture, @Nullable BasicTexture outsideTexture) {
92-
drawVideoFrame(canvasGL, outsideSurfaceTexture, outsideTexture);
93-
94-
Loggers.i("DEBUG", "gl draw");
94+
public void onGLDraw(ICanvasGL canvasGL, List<GLTexture> producedTextures, List<GLTexture> consumedTextures) {
95+
GLTexture texture = producedTextures.get(0);
96+
drawVideoFrame(canvasGL, texture.getSurfaceTexture(), texture.getRawTexture());
9597
}
9698
});
9799
try {

app/src/main/java/com/chillingvan/instantvideo/sample/test/publisher/TestMp4MuxerActivity.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333

3434
import com.chillingvan.canvasgl.ICanvasGL;
3535
import com.chillingvan.canvasgl.glcanvas.BasicTexture;
36-
import com.chillingvan.canvasgl.glcanvas.RawTexture;
36+
import com.chillingvan.canvasgl.glview.texture.GLTexture;
3737
import com.chillingvan.canvasgl.textureFilter.BasicTextureFilter;
3838
import com.chillingvan.canvasgl.textureFilter.HueFilter;
3939
import com.chillingvan.canvasgl.textureFilter.TextureFilter;
@@ -47,6 +47,7 @@
4747
import com.chillingvan.lib.publisher.StreamPublisher;
4848

4949
import java.io.IOException;
50+
import java.util.List;
5051

5152
public class TestMp4MuxerActivity extends AppCompatActivity {
5253

@@ -63,12 +64,14 @@ protected void onCreate(Bundle savedInstanceState) {
6364
super.onCreate(savedInstanceState);
6465
outputDir = getExternalFilesDir(null) + "/test_mp4_encode.mp4";
6566
setContentView(R.layout.activity_test_mp4_muxer);
66-
cameraPreviewTextureView = (CameraPreviewTextureView) findViewById(R.id.camera_produce_view);
67+
cameraPreviewTextureView = findViewById(R.id.camera_produce_view);
6768
cameraPreviewTextureView.setOnDrawListener(new H264Encoder.OnDrawListener() {
6869
@Override
69-
public void onGLDraw(ICanvasGL canvasGL, SurfaceTexture surfaceTexture, RawTexture rawTexture, @Nullable SurfaceTexture outsideSurfaceTexture, @Nullable BasicTexture outsideTexture) {
70-
drawVideoFrame(canvasGL, surfaceTexture, rawTexture);
70+
public void onGLDraw(ICanvasGL canvasGL, List<GLTexture> producedTextures, List<GLTexture> consumedTextures) {
71+
GLTexture texture = producedTextures.get(0);
72+
drawVideoFrame(canvasGL, texture.getSurfaceTexture(), texture.getRawTexture());
7173
}
74+
7275
});
7376
outDirTxt = (TextView) findViewById(R.id.output_dir_txt);
7477
outDirTxt.setText(outputDir);
@@ -90,11 +93,12 @@ public void handleMessage(Message msg) {
9093
// streamPublisherParam.outputFilePath = getExternalFilesDir(null) + "/test_mp4_encode.mp4";
9194
streamPublisher.prepareEncoder(streamPublisherParam, new H264Encoder.OnDrawListener() {
9295
@Override
93-
public void onGLDraw(ICanvasGL canvasGL, SurfaceTexture surfaceTexture, RawTexture rawTexture, @Nullable SurfaceTexture outsideSurfaceTexture, @Nullable BasicTexture outsideTexture) {
94-
drawVideoFrame(canvasGL, outsideSurfaceTexture, outsideTexture);
95-
96+
public void onGLDraw(ICanvasGL canvasGL, List<GLTexture> producedTextures, List<GLTexture> consumedTextures) {
97+
GLTexture texture = producedTextures.get(0);
98+
drawVideoFrame(canvasGL, texture.getSurfaceTexture(), texture.getRawTexture());
9699
Loggers.i("DEBUG", "gl draw");
97100
}
101+
98102
});
99103
try {
100104
streamPublisher.startPublish();

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

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@
2828
import android.util.Log;
2929

3030
import com.chillingvan.canvasgl.ICanvasGL;
31-
import com.chillingvan.canvasgl.glcanvas.BasicTexture;
3231
import com.chillingvan.canvasgl.glcanvas.GLPaint;
32+
import com.chillingvan.canvasgl.glcanvas.RawTexture;
33+
import com.chillingvan.canvasgl.glview.texture.GLTexture;
3334
import com.chillingvan.canvasgl.glview.texture.gles.EglContextWrapper;
3435
import com.chillingvan.canvasgl.util.Loggers;
3536
import com.chillingvan.lib.encoder.MediaCodecInputStream;
@@ -41,6 +42,8 @@
4142
import java.io.FileOutputStream;
4243
import java.io.IOException;
4344
import java.io.OutputStream;
45+
import java.util.ArrayList;
46+
import java.util.List;
4447

4548
/**
4649
* Created by Chilling on 2016/12/10.
@@ -53,9 +56,8 @@ public class TestVideoEncoder {
5356
private Context ctx;
5457
private EglContextWrapper eglCtx;
5558
public static int drawCnt;
56-
private BasicTexture outsideTexture;
57-
private SurfaceTexture outsideSurfaceTexture;
5859
private OutputStream os;
60+
private List<GLTexture> glTextureList = new ArrayList<>();
5961

6062
public TestVideoEncoder(Context ctx, final EglContextWrapper eglCtx) {
6163
this.ctx = ctx;
@@ -71,16 +73,17 @@ public TestVideoEncoder(Context ctx, final EglContextWrapper eglCtx) {
7173
public void prepareEncoder(H264Encoder.OnDrawListener onDrawListener) {
7274
try {
7375
h264Encoder = new H264Encoder(new StreamPublisher.StreamPublisherParam(), eglCtx);
74-
h264Encoder.setSharedTexture(outsideTexture, outsideSurfaceTexture);
76+
for (GLTexture texture : glTextureList) {
77+
h264Encoder.addSharedTexture(texture);
78+
}
7579
h264Encoder.setOnDrawListener(onDrawListener);
7680
} catch (IOException | IllegalStateException e) {
7781
e.printStackTrace();
7882
}
7983
}
8084

81-
public void setSharedTexture(BasicTexture outsideTexture, SurfaceTexture outsideSurfaceTexture) {
82-
this.outsideTexture = outsideTexture;
83-
this.outsideSurfaceTexture = outsideSurfaceTexture;
85+
public void addSharedTexture(RawTexture outsideTexture, SurfaceTexture outsideSurfaceTexture) {
86+
glTextureList.add(new GLTexture(outsideTexture, outsideSurfaceTexture));
8487
}
8588

8689
public static void drawRect(ICanvasGL canvasGL, int drawCnt) {

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,21 @@
2727
import android.os.Handler;
2828
import android.os.HandlerThread;
2929
import android.os.Message;
30-
import android.support.annotation.Nullable;
3130
import android.support.v7.app.AppCompatActivity;
3231
import android.util.Log;
3332
import android.view.View;
3433

3534
import com.chillingvan.canvasgl.ICanvasGL;
36-
import com.chillingvan.canvasgl.glcanvas.BasicTexture;
3735
import com.chillingvan.canvasgl.glcanvas.RawTexture;
3836
import com.chillingvan.canvasgl.glview.texture.GLSurfaceTextureProducerView;
37+
import com.chillingvan.canvasgl.glview.texture.GLTexture;
3938
import com.chillingvan.canvasgl.glview.texture.gles.EglContextWrapper;
4039
import com.chillingvan.canvasgl.glview.texture.gles.GLThread;
4140
import com.chillingvan.instantvideo.sample.R;
4241
import com.chillingvan.lib.encoder.video.H264Encoder;
4342

43+
import java.util.List;
44+
4445

4546
public class TestVideoEncoderActivity extends AppCompatActivity {
4647

@@ -66,7 +67,7 @@ public void onCreate(EglContextWrapper eglContext) {
6667
produceTextureView.setOnSurfaceTextureSet(new GLSurfaceTextureProducerView.OnSurfaceTextureSet() {
6768
@Override
6869
public void onSet(SurfaceTexture surfaceTexture, RawTexture rawTexture) {
69-
testVideoEncoder.setSharedTexture(rawTexture, surfaceTexture);
70+
testVideoEncoder.addSharedTexture(rawTexture, surfaceTexture);
7071
surfaceTexture.setOnFrameAvailableListener(new SurfaceTexture.OnFrameAvailableListener() {
7172
@Override
7273
public void onFrameAvailable(SurfaceTexture surfaceTexture) {
@@ -98,7 +99,8 @@ public void handleMessage(Message msg) {
9899
final Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.lenna);
99100
testVideoEncoder.prepareEncoder(new H264Encoder.OnDrawListener() {
100101
@Override
101-
public void onGLDraw(ICanvasGL canvasGL, SurfaceTexture producedSurfaceTexture, RawTexture rawTexture, @Nullable SurfaceTexture outsideSurfaceTexture, @Nullable BasicTexture outsideTexture) {
102+
public void onGLDraw(ICanvasGL canvasGL, List<GLTexture> producedTextures, List<GLTexture> consumedTextures) {
103+
102104
TestVideoEncoder.drawCnt++;
103105

104106
if (TestVideoEncoder.drawCnt == 19 || TestVideoEncoder.drawCnt == 39) {
@@ -111,6 +113,7 @@ public void onGLDraw(ICanvasGL canvasGL, SurfaceTexture producedSurfaceTexture,
111113
}
112114
Log.i("TestVideoEncoder", "gl draw");
113115
}
116+
114117
});
115118
testVideoEncoder.start();
116119
for (int i = 0; i < 120; i++) {

applibs/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ android {
4545

4646
dependencies {
4747
implementation fileTree(dir: 'libs', include: ['*.jar'])
48-
api 'com.github.ChillingVan:android-openGL-canvas:v1.3.0.0'
48+
api 'com.github.ChillingVan:android-openGL-canvas:v1.4.0.0'
4949
androidTestImplementation('com.android.support.test.espresso:espresso-core:2.2.2', {
5050
exclude group: 'com.android.support', module: 'support-annotations'
5151
})

0 commit comments

Comments
 (0)