Skip to content

Commit f7ce197

Browse files
committed
增加接受小程序或者app数据流保存
1 parent 249aaa4 commit f7ce197

File tree

4 files changed

+101
-6
lines changed

4 files changed

+101
-6
lines changed

app/src/main/java/com/example/ivdemo/BaseIPCActivity.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ abstract class BaseIPCActivity<VB : ViewBinding> : AppCompatActivity(), IvDevice
222222
this.visitor = visitor
223223
this.channel = channel
224224
this.videoResType = videoResType
225-
Log.w(TAG, "onNotify()")
225+
Log.w(TAG, "onNotify() called with event = $event, visitor = $visitor, channel = $channel, videoResType = $videoResType")
226226
var msg = ""
227227
when (event) {
228228
P2pEventType.IV_AVT_EVENT_P2P_PEER_CONNECT_FAIL, P2pEventType.IV_AVT_EVENT_P2P_PEER_ERROR -> {

app/src/main/java/com/example/ivdemo/TweCallActivity.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,6 @@ class TweCallActivity : BaseIPCActivity<ActivityTweCallBinding>(), IvVoipCallbac
126126

127127
override fun initView() {
128128
cameraRecorder.init(this)
129-
cameraRecorder.isSaveRecord(true)
130129
with(binding) {
131130
titleLayout.tvTitle.text = getString(R.string.title_tweCall)
132131
titleLayout.ivRightBtn.isVisible = true

app/src/main/java/com/example/ivdemo/popup/CustomCommandDialog.kt

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.example.ivdemo.popup
22

33
import android.content.Context
4+
import android.util.Log
45
import android.widget.Toast
56
import androidx.fragment.app.FragmentManager
67
import androidx.lifecycle.lifecycleScope
@@ -43,7 +44,11 @@ class CustomCommandDialog(context: Context, private val visitor: Int) :
4344
}
4445

4546
fun receiveCommand(msg: String): JSONObject {
46-
binding.tvResult.append("接受信令 ==> $msg\n\n")
47+
Log.d("IPCActivity","receiveCommand msg:$msg")
48+
49+
lifecycleScope.launch {
50+
binding.tvResult.append("接受信令 ==> $msg\n\n")
51+
}
4752
var replyMsg = binding.tvCommand.text.toString()
4853
if (replyMsg.isEmpty()) {
4954
showToast("回复信令内容不能为空,已取默认值success")
@@ -57,7 +62,10 @@ class CustomCommandDialog(context: Context, private val visitor: Int) :
5762
} catch (e: JSONException) {
5863
e.printStackTrace()
5964
}
60-
binding.tvResult.append("回复信令 ==> $resJson\n\n")
65+
lifecycleScope.launch {
66+
binding.tvResult.append("回复信令 ==> $resJson\n\n")
67+
}
68+
Log.d("IPCActivity","receiveCommand returnJson:$resJson")
6169
return resJson
6270
}
6371

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

Lines changed: 90 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,31 @@
22

33
import android.content.Context;
44
import android.os.Build;
5+
import android.text.TextUtils;
56
import android.util.Log;
67
import android.view.Surface;
78

89
import com.tencent.iotvideo.link.decoder.AudioDecoder;
910
import com.tencent.iotvideo.link.decoder.VideoDecoder;
11+
import com.tencent.iotvideo.link.util.UtilsKt;
1012

13+
import java.io.File;
14+
import java.io.FileOutputStream;
1115
import java.io.IOException;
16+
import java.util.concurrent.ExecutorService;
17+
import java.util.concurrent.Executors;
1218

1319
public class SimplePlayer {
1420
private static final String TAG = "SimplePlayer";
1521
private static final char[] HEX_ARRAY = "0123456789ABCDEF".toCharArray();
1622

23+
private String receiveH264FilePath = "/sdcard/wx_video.h264";
24+
private String receiveAacFilePath = "/sdcard/wx_audio.aac";
25+
private FileOutputStream h264Fos;
26+
private FileOutputStream aacFos;
27+
private boolean isSaveReceiveRecord = false;
28+
private final ExecutorService executor = Executors.newSingleThreadExecutor();
29+
1730
public static String bytesToHex(byte[] bytes, int length) {
1831
char[] hexChars = new char[length * 2];
1932
for (int j = 0; j < length; j++) {
@@ -34,6 +47,45 @@ public void setContext(Context context) {
3447
}
3548
}
3649

50+
/**
51+
* 保存音视频数据
52+
*
53+
* @param isSaveReceiveRecord
54+
*/
55+
public void isSaveReceiveRecord(boolean isSaveReceiveRecord) {
56+
this.isSaveReceiveRecord = isSaveReceiveRecord;
57+
recordSpeakH264(isSaveReceiveRecord);
58+
recordSpeakAac(isSaveReceiveRecord);
59+
}
60+
61+
public void recordSpeakH264(boolean isRecord) {
62+
if (isRecord) {
63+
if (!TextUtils.isEmpty(receiveH264FilePath)) {
64+
try {
65+
File file = UtilsKt.getFile(receiveH264FilePath);
66+
h264Fos = new FileOutputStream(file);
67+
} catch (Exception e) {
68+
e.printStackTrace();
69+
Log.e(TAG, receiveH264FilePath + "临时缓存文件未找到");
70+
}
71+
}
72+
}
73+
}
74+
75+
public void recordSpeakAac(boolean isRecord) {
76+
if (isRecord) {
77+
if (!TextUtils.isEmpty(receiveAacFilePath)) {
78+
try {
79+
File file = UtilsKt.getFile(receiveAacFilePath);
80+
aacFos = new FileOutputStream(file);
81+
} catch (Exception e) {
82+
e.printStackTrace();
83+
Log.e(TAG, receiveAacFilePath + "临时缓存文件未找到");
84+
}
85+
}
86+
}
87+
}
88+
3789
public int startVideoPlay(Surface surface, int visitor, int type, int height, int width) {
3890
Log.d(TAG, "video input from visitor " + visitor + " height " + height + " width " + width + ", model:" + Build.MODEL);
3991
// type == 0: h.264/avc; type == 1: h.265/hevc
@@ -99,7 +151,9 @@ public int stopAudioPlay(int visitor) {
99151
public int playVideoStream(int visitor, byte[] data, int len, long pts, long seq) {
100152
// Log.d(TAG, "video frame: visitor "+ visitor + " len " + len + " pts " + pts + " seq " + seq);
101153
if (videoDecoder != null) {
102-
return videoDecoder.decoderH264(data, len, pts);
154+
int resCode = videoDecoder.decoderH264(data, len, pts);
155+
saveH264(data);
156+
return resCode;
103157
}
104158
return 0;
105159
}
@@ -111,8 +165,42 @@ public int playAudioStream(int visitor, byte[] data, int len, long pts, long seq
111165
if (videoDecoder != null) {
112166
audioDecoder.setCurrentVideoPts(videoDecoder.getCurrentVideoPts());
113167
}
114-
return audioDecoder.decoderAAC(data, len, pts);
168+
int resCode = audioDecoder.decoderAAC(data, len, pts);
169+
saveAac(data);
170+
return resCode;
115171
}
116172
return 0;
117173
}
174+
175+
public void saveH264(byte[] datas) {
176+
if (isSaveReceiveRecord) {
177+
if (executor.isShutdown()) return;
178+
executor.submit(() -> {
179+
if (h264Fos != null) {
180+
try {
181+
h264Fos.write(datas);
182+
h264Fos.flush();
183+
} catch (IOException e) {
184+
e.printStackTrace();
185+
}
186+
}
187+
});
188+
}
189+
}
190+
191+
public void saveAac(byte[] datas) {
192+
if (isSaveReceiveRecord) {
193+
if (executor.isShutdown()) return;
194+
executor.submit(() -> {
195+
if (aacFos != null) {
196+
try {
197+
aacFos.write(datas);
198+
aacFos.flush();
199+
} catch (IOException e) {
200+
e.printStackTrace();
201+
}
202+
}
203+
});
204+
}
205+
}
118206
}

0 commit comments

Comments
 (0)