Skip to content

Commit 4134626

Browse files
committed
处理线程导致没声音没画面问题
1 parent 2324ce5 commit 4134626

File tree

3 files changed

+15
-6
lines changed

3 files changed

+15
-6
lines changed

app/src/main/java/com/tencent/iotvideo/link/CameraRecorder.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,9 @@ private Camera.Parameters getParameters() {
162162

163163
public void closeCamera() {
164164
try {
165+
if (mVideoEncoder != null) {
166+
mVideoEncoder.release();
167+
}
165168
if (camera != null) {
166169
camera.stopPreview();
167170
camera.setPreviewCallback(null);
@@ -195,11 +198,9 @@ public void stopRecording(int visitor, int res_type) {
195198
}
196199
if (mVideoEncoder != null) {
197200
mVideoEncoder.stop();
198-
mVideoEncoder = null;
199201
}
200202
if (mAudioEncoder != null) {
201203
mAudioEncoder.stop();
202-
mAudioEncoder = null;
203204
}
204205
mIsRecording = false;
205206
mVisitorInfo.remove(visitor);
@@ -255,7 +256,7 @@ public void onVideoEncoded(byte[] datas, long pts, long seq, boolean isKeyFrame)
255256
if (ret != 0) {
256257
int buf_size = iv.getSendStreamBuf(visitor, channel, res_type);
257258
Log.e(TAG, "sendVideoData to visitor " + visitor + " failed: " + ret + " buf size " + buf_size);
258-
}else {
259+
} else {
259260
Log.e(TAG, "sendVideoData to success");
260261
}
261262

app/src/main/java/com/tencent/iotvideo/link/decoder/VideoDecoder.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public class VideoDecoder {
2525
private MediaCodec mVideoCodec;
2626
private ExecutorService mVideoExecutor;
2727
private long currentVideoPts = 0;
28-
private final ExecutorService decoderH264executor = Executors.newSingleThreadExecutor();
28+
private ExecutorService decoderH264executor = Executors.newSingleThreadExecutor();
2929
private String decoderH264FilePath = "/sdcard/videoDecoder.h264";
3030
private FileOutputStream fos;
3131

@@ -116,6 +116,9 @@ public int decoderH264(byte[] data, int len, long pts) {
116116
}
117117

118118
private void saveRawDataStream(byte[] data) {
119+
if (decoderH264executor.isShutdown()){
120+
decoderH264executor = Executors.newSingleThreadExecutor();
121+
}
119122
decoderH264executor.submit(() -> {
120123
if (fos != null) {
121124
try {

app/src/main/java/com/tencent/iotvideo/link/encoder/VideoEncoder.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public class VideoEncoder {
3636
private MediaCodecInfo mediaCodecInfo;
3737
private int colorFormat;
3838
private final VideoEncodeParam videoEncodeParam;
39-
private final ExecutorService executor = Executors.newSingleThreadExecutor();
39+
private ExecutorService executor = Executors.newSingleThreadExecutor();
4040
private MediaCodec mediaCodec;
4141
private OnEncodeListener encoderListener;
4242
private Range<Double> bitRateInterval;
@@ -153,7 +153,9 @@ public Range<Double> getBitRateInterval() {
153153
* 将NV21编码成H264
154154
*/
155155
public void encoderH264(byte[] data, boolean mirror) {
156-
if (executor.isShutdown()) return;
156+
if (executor.isShutdown()) {
157+
executor = Executors.newSingleThreadExecutor();
158+
}
157159
executor.submit(() -> {
158160
byte[] readyToProcessBytes = convertData(data);
159161
// 获取输入缓冲区
@@ -218,6 +220,9 @@ public void stop() {
218220
} catch (Exception e) {
219221
e.printStackTrace();
220222
}
223+
}
224+
225+
public void release() {
221226
executor.shutdown();
222227
}
223228

0 commit comments

Comments
 (0)