22
33import android .content .Context ;
44import android .os .Build ;
5+ import android .text .TextUtils ;
56import android .util .Log ;
67import android .view .Surface ;
78
89import com .tencent .iotvideo .link .decoder .AudioDecoder ;
910import 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 ;
1115import java .io .IOException ;
16+ import java .util .concurrent .ExecutorService ;
17+ import java .util .concurrent .Executors ;
1218
1319public 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