Skip to content

Commit 9113424

Browse files
committed
example update to multi texture
1 parent 224c04e commit 9113424

File tree

8 files changed

+200
-9
lines changed

8 files changed

+200
-9
lines changed

app/src/main/assets/test_video.mp4

391 KB
Binary file not shown.

app/src/main/java/com/chillingvan/instantvideo/sample/CrashHandler.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ private CrashHandler() {
5151
public static CrashHandler init(Context applicationContext) {
5252
if (mContext == null) {
5353
mContext = applicationContext;
54-
CRASH_PATH = mContext.getExternalFilesDir(null) + File.separator + "crash";
54+
CRASH_PATH = mContext.getExternalFilesDir(null) + File.separator + "crash";
5555
}
5656
return INSTANCE;
5757
}
@@ -77,7 +77,7 @@ private void saveErrorInfo(Throwable ex) {
7777
path.mkdirs();
7878
}
7979

80-
File file = new File(CRASH_PATH + File.separator + convertYYMMDDHHmm(System.currentTimeMillis()));
80+
File file = new File(CRASH_PATH + File.separator + "log_" + convertYYMMDDHHmm(System.currentTimeMillis()) + ".txt");
8181
FileOutputStream fos = null;
8282
try {
8383
fos = new FileOutputStream(file);
@@ -88,7 +88,7 @@ private void saveErrorInfo(Throwable ex) {
8888
PrintWriter pw = new PrintWriter(writer);
8989
ex.printStackTrace(pw);
9090
pw.close();
91-
String error= writer.toString();
91+
String error = writer.toString();
9292
fos.write(error.getBytes());
9393
} catch (IOException e) {
9494
e.printStackTrace();

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ public CameraPreviewTextureView(Context context, AttributeSet attrs, int defStyl
4040
super(context, attrs, defStyleAttr);
4141
}
4242

43+
@Override
44+
protected int getInitialTexCount() {
45+
return 2;
46+
}
47+
4348
@Override
4449
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
4550
super.onSurfaceTextureAvailable(surface, width, height);
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
package com.chillingvan.instantvideo.sample.test.publisher;
2+
3+
import android.content.Context;
4+
import android.content.res.AssetFileDescriptor;
5+
import android.media.AudioManager;
6+
import android.media.MediaPlayer;
7+
import android.view.Surface;
8+
import android.widget.Toast;
9+
10+
import com.chillingvan.canvasgl.util.Loggers;
11+
12+
import java.io.IOException;
13+
14+
/**
15+
* Created by Chilling on 2018/4/14.
16+
*/
17+
public class MediaPlayerHelper {
18+
19+
public static final String TEST_VIDEO_MP4 = "test_video.mp4";
20+
private MediaPlayer mediaPlayer;
21+
private String videoName;
22+
23+
public MediaPlayerHelper() {
24+
this(TEST_VIDEO_MP4);
25+
}
26+
27+
public MediaPlayerHelper(String videoName) {
28+
this.videoName = videoName;
29+
}
30+
31+
public boolean isPlaying() {
32+
if (mediaPlayer != null) {
33+
return mediaPlayer.isPlaying();
34+
}
35+
return false;
36+
}
37+
38+
public boolean isLooping() {
39+
if (mediaPlayer != null) {
40+
return mediaPlayer.isLooping();
41+
}
42+
return false;
43+
}
44+
45+
public void playMedia(final Context context, Surface mediaSurface) {
46+
mediaPlayer = new MediaPlayer();
47+
mediaPlayer.setVolume(0, 0);
48+
try {
49+
AssetFileDescriptor afd = context.getAssets().openFd(videoName);
50+
mediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
51+
afd.close();
52+
} catch (IOException e) {
53+
e.printStackTrace();
54+
}
55+
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
56+
mediaPlayer.setSurface(mediaSurface);
57+
mediaPlayer.setLooping(true);
58+
59+
mediaPlayer.setOnSeekCompleteListener(new MediaPlayer.OnSeekCompleteListener() {
60+
@Override
61+
public void onSeekComplete(MediaPlayer mediaPlayer) {
62+
Loggers.i("onSeekComplete","onSeekComplete----"+mediaPlayer.getCurrentPosition());
63+
}
64+
});
65+
66+
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
67+
@Override
68+
public void onPrepared(MediaPlayer mediaPlayer) {
69+
Toast.makeText(context, "onPrepare --> Start", Toast.LENGTH_SHORT).show();
70+
mediaPlayer.start();
71+
}
72+
});
73+
74+
75+
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
76+
@Override
77+
public void onCompletion(MediaPlayer m) {
78+
Toast.makeText(context, "End Play", Toast.LENGTH_LONG).show();
79+
m.stop();
80+
m.release();
81+
}
82+
});
83+
84+
try {
85+
mediaPlayer.prepare();
86+
} catch (IOException e) {
87+
e.printStackTrace();
88+
}
89+
90+
}
91+
92+
public void stop() {
93+
if (mediaPlayer != null) {
94+
mediaPlayer.stop();
95+
}
96+
}
97+
98+
public void release() {
99+
if (mediaPlayer != null) {
100+
mediaPlayer.release();
101+
}
102+
}
103+
}

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

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,13 @@
2828
import android.os.Message;
2929
import android.support.annotation.Nullable;
3030
import android.support.v7.app.AppCompatActivity;
31+
import android.view.Surface;
3132
import android.view.View;
3233
import android.widget.TextView;
3334

3435
import com.chillingvan.canvasgl.ICanvasGL;
3536
import com.chillingvan.canvasgl.glcanvas.BasicTexture;
37+
import com.chillingvan.canvasgl.glcanvas.RawTexture;
3638
import com.chillingvan.canvasgl.glview.texture.GLTexture;
3739
import com.chillingvan.canvasgl.textureFilter.BasicTextureFilter;
3840
import com.chillingvan.canvasgl.textureFilter.HueFilter;
@@ -59,6 +61,9 @@ public class TestMp4MuxerActivity extends AppCompatActivity {
5961
private TextView outDirTxt;
6062
private String outputDir;
6163

64+
private MediaPlayerHelper mediaPlayer = new MediaPlayerHelper();
65+
private Surface mediaSurface;
66+
6267
@Override
6368
protected void onCreate(Bundle savedInstanceState) {
6469
super.onCreate(savedInstanceState);
@@ -69,7 +74,8 @@ protected void onCreate(Bundle savedInstanceState) {
6974
@Override
7075
public void onGLDraw(ICanvasGL canvasGL, List<GLTexture> producedTextures, List<GLTexture> consumedTextures) {
7176
GLTexture texture = producedTextures.get(0);
72-
drawVideoFrame(canvasGL, texture.getSurfaceTexture(), texture.getRawTexture());
77+
GLTexture mediaTexture = producedTextures.get(1);
78+
drawVideoFrame(canvasGL, texture.getSurfaceTexture(), texture.getRawTexture(), mediaTexture);
7379
}
7480

7581
});
@@ -86,16 +92,18 @@ public void onGLDraw(ICanvasGL canvasGL, List<GLTexture> producedTextures, List<
8692
@Override
8793
public void handleMessage(Message msg) {
8894
super.handleMessage(msg);
95+
playMedia();
8996
// StreamPublisher.StreamPublisherParam streamPublisherParam = new StreamPublisher.StreamPublisherParam();
9097
// StreamPublisher.StreamPublisherParam streamPublisherParam = new StreamPublisher.StreamPublisherParam(1080, 640, 9500 * 1000, 30, 1, 44100, 19200);
91-
StreamPublisher.StreamPublisherParam streamPublisherParam = new StreamPublisher.StreamPublisherParam(540, 750, 1500 * 1000, 30, 1, 44100, 19200);
98+
StreamPublisher.StreamPublisherParam streamPublisherParam = new StreamPublisher.StreamPublisherParam(1080, 750, 1500 * 1000, 30, 1, 44100, 19200);
9299
streamPublisherParam.outputFilePath = outputDir;
93-
// streamPublisherParam.outputFilePath = getExternalFilesDir(null) + "/test_mp4_encode.mp4";
100+
streamPublisherParam.setInitialTextureCount(2);
94101
streamPublisher.prepareEncoder(streamPublisherParam, new H264Encoder.OnDrawListener() {
95102
@Override
96103
public void onGLDraw(ICanvasGL canvasGL, List<GLTexture> producedTextures, List<GLTexture> consumedTextures) {
97-
GLTexture texture = producedTextures.get(0);
98-
drawVideoFrame(canvasGL, texture.getSurfaceTexture(), texture.getRawTexture());
104+
GLTexture texture = consumedTextures.get(1);
105+
GLTexture mediaTexture = consumedTextures.get(0);
106+
drawVideoFrame(canvasGL, texture.getSurfaceTexture(), texture.getRawTexture(), mediaTexture);
99107
Loggers.i("DEBUG", "gl draw");
100108
}
101109

@@ -110,9 +118,18 @@ public void onGLDraw(ICanvasGL canvasGL, List<GLTexture> producedTextures, List<
110118
};
111119

112120
streamPublisher = new CameraStreamPublisher(new MP4Muxer(), cameraPreviewTextureView, instantVideoCamera);
121+
streamPublisher.setOnSurfacesCreatedListener(new CameraStreamPublisher.OnSurfacesCreatedListener() {
122+
@Override
123+
public void onCreated(List<GLTexture> producedTextureList, StreamPublisher streamPublisher) {
124+
GLTexture texture = producedTextureList.get(1);
125+
GLTexture mediaTexture = producedTextureList.get(1);
126+
streamPublisher.addSharedTexture(new GLTexture(mediaTexture.getRawTexture(), mediaTexture.getSurfaceTexture()));
127+
mediaSurface = new Surface(texture.getSurfaceTexture());
128+
}
129+
});
113130
}
114131

115-
private void drawVideoFrame(ICanvasGL canvasGL, @Nullable SurfaceTexture outsideSurfaceTexture, @Nullable BasicTexture outsideTexture) {
132+
private void drawVideoFrame(ICanvasGL canvasGL, @Nullable SurfaceTexture outsideSurfaceTexture, @Nullable BasicTexture outsideTexture, GLTexture mediaTexture) {
116133
// Here you can do video process
117134
// 此处可以视频处理,例如加水印等等
118135
TextureFilter textureFilterLT = new BasicTextureFilter();
@@ -122,6 +139,10 @@ private void drawVideoFrame(ICanvasGL canvasGL, @Nullable SurfaceTexture outside
122139
canvasGL.drawSurfaceTexture(outsideTexture, outsideSurfaceTexture, 0, 0, width /2, height /2, textureFilterLT);
123140
canvasGL.drawSurfaceTexture(outsideTexture, outsideSurfaceTexture, 0, height/2, width/2, height, textureFilterRT);
124141

142+
SurfaceTexture mediaSurfaceTexture = mediaTexture.getSurfaceTexture();
143+
RawTexture mediaRawTexture = mediaTexture.getRawTexture();
144+
mediaRawTexture.setIsFlippedVertically(true);
145+
canvasGL.drawSurfaceTexture(mediaRawTexture, mediaSurfaceTexture, width/2, height/2, width, height);
125146
}
126147

127148
@Override
@@ -137,18 +158,35 @@ protected void onPause() {
137158
if (streamPublisher.isStart()) {
138159
streamPublisher.closeAll();
139160
}
161+
if (mediaPlayer.isPlaying()) {
162+
mediaPlayer.stop();
163+
}
164+
}
165+
166+
private void playMedia() {
167+
if ((mediaPlayer.isPlaying() || mediaPlayer.isLooping())) {
168+
return;
169+
}
170+
171+
mediaPlayer.playMedia(this, mediaSurface);
140172
}
141173

142174
@Override
143175
protected void onDestroy() {
144176
super.onDestroy();
177+
if (mediaPlayer.isPlaying()) {
178+
mediaPlayer.release();
179+
}
145180
handlerThread.quitSafely();
146181
}
147182

148183
public void clickStartTest(View view) {
149184
TextView textView = (TextView) view;
150185
if (streamPublisher.isStart()) {
151186
streamPublisher.closeAll();
187+
if (mediaPlayer.isPlaying()) {
188+
mediaPlayer.stop();
189+
}
152190
textView.setText("START");
153191
} else {
154192
streamPublisher.resumeCamera();

applibs/src/main/java/com/chillingvan/lib/encoder/video/H264Encoder.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ public class H264Encoder {
5454
protected final EncoderCanvas offScreenCanvas;
5555
private OnDrawListener onDrawListener;
5656
private boolean isStart;
57+
private int initialTextureCount = 1;
5758

5859

5960
public H264Encoder(StreamPublisher.StreamPublisherParam params) throws IOException {
@@ -89,6 +90,7 @@ public void onChangeMediaFormat(MediaFormat mediaFormat) {
8990
}
9091
});
9192

93+
this.initialTextureCount = params.getInitialTextureCount();
9294
offScreenCanvas = new EncoderCanvas(params.width, params.height, eglCtx);
9395
}
9496

@@ -108,6 +110,17 @@ public MediaCodecInputStream getMediaCodecInputStream() {
108110
return mediaCodecInputStream;
109111
}
110112

113+
/**
114+
*
115+
* @param initialTextureCount Default is 1
116+
*/
117+
public void setInitialTextureCount(int initialTextureCount) {
118+
if (initialTextureCount < 1) {
119+
throw new IllegalArgumentException("initialTextureCount must >= 1");
120+
}
121+
this.initialTextureCount = initialTextureCount;
122+
}
123+
111124
public void start() {
112125
offScreenCanvas.start();
113126
isStart = true;
@@ -165,5 +178,9 @@ protected void onGLDraw(ICanvasGL canvas, List<GLTexture> producedTextures, List
165178
}
166179
}
167180

181+
@Override
182+
protected int getInitialTexCount() {
183+
return initialTextureCount;
184+
}
168185
}
169186
}

applibs/src/main/java/com/chillingvan/lib/publisher/CameraStreamPublisher.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public class CameraStreamPublisher {
4545
private IMuxer muxer;
4646
private GLMultiTexProducerView cameraPreviewTextureView;
4747
private CameraInterface instantVideoCamera;
48+
private OnSurfacesCreatedListener onSurfacesCreatedListener;
4849

4950
public CameraStreamPublisher(IMuxer muxer, GLMultiTexProducerView cameraPreviewTextureView, CameraInterface instantVideoCamera) {
5051
this.muxer = muxer;
@@ -62,6 +63,9 @@ public void onCreate(EglContextWrapper eglContext) {
6263
cameraPreviewTextureView.setSurfaceTextureCreatedListener(new GLMultiTexProducerView.SurfaceTextureCreatedListener() {
6364
@Override
6465
public void onCreated(List<GLTexture> producedTextureList) {
66+
if (onSurfacesCreatedListener != null) {
67+
onSurfacesCreatedListener.onCreated(producedTextureList, streamPublisher);
68+
}
6569
GLTexture texture = producedTextureList.get(0);
6670
SurfaceTexture surfaceTexture = texture.getSurfaceTexture();
6771
streamPublisher.addSharedTexture(new GLTexture(texture.getRawTexture(), surfaceTexture));
@@ -110,4 +114,12 @@ public void startPublish() throws IOException {
110114
public void closeAll() {
111115
streamPublisher.close();
112116
}
117+
118+
public void setOnSurfacesCreatedListener(OnSurfacesCreatedListener onSurfacesCreatedListener) {
119+
this.onSurfacesCreatedListener = onSurfacesCreatedListener;
120+
}
121+
122+
public interface OnSurfacesCreatedListener {
123+
void onCreated(List<GLTexture> producedTextureList, StreamPublisher streamPublisher);
124+
}
113125
}

applibs/src/main/java/com/chillingvan/lib/publisher/StreamPublisher.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,8 @@ public static class StreamPublisherParam {
195195
private MediaFormat videoOutputMediaFormat;
196196
private MediaFormat audioOutputMediaFormat;
197197

198+
private int initialTextureCount = 1;
199+
198200
public StreamPublisherParam() {
199201
this(640, 480, 2949120, 30, 5, 44100, 192000);
200202
}
@@ -211,6 +213,20 @@ public StreamPublisherParam(int width, int height, int videoBitRate, int frameRa
211213
audioBufferSize = AudioRecord.getMinBufferSize(samplingRate, AudioFormat.CHANNEL_IN_STEREO, AudioFormat.ENCODING_PCM_16BIT) * 2;
212214
}
213215

216+
/**
217+
*
218+
* @param initialTextureCount Default is 1
219+
*/
220+
public void setInitialTextureCount(int initialTextureCount) {
221+
if (initialTextureCount < 1) {
222+
throw new IllegalArgumentException("initialTextureCount must >= 1");
223+
}
224+
this.initialTextureCount = initialTextureCount;
225+
}
226+
227+
public int getInitialTextureCount() {
228+
return initialTextureCount;
229+
}
214230

215231
public MediaFormat createVideoMediaFormat() {
216232
MediaFormat format = MediaFormat.createVideoFormat(videoMIMEType, width, height);

0 commit comments

Comments
 (0)