36
36
import androidx .localbroadcastmanager .content .LocalBroadcastManager ;
37
37
38
38
import com .alibaba .fastjson .JSON ;
39
- //import com.iot.gvoice.interfaces.GvoiceJNIBridge;
40
39
import com .tencent .iot .explorer .device .android .app .R ;
41
40
import com .tencent .iot .explorer .device .android .app .utils .CommonUtils ;
42
41
import com .tencent .iot .explorer .device .common .stateflow .entity .CallingType ;
49
48
import com .tencent .iot .explorer .device .video .recorder .encoder .AudioEncoder ;
50
49
import com .tencent .iot .explorer .device .video .recorder .encoder .VideoEncoder ;
51
50
import com .tencent .iot .explorer .device .video .recorder .listener .OnEncodeListener ;
52
- import com .tencent .iot .explorer .device .video .recorder .listener .OnReadAECProcessedPcmListener ;
53
51
import com .tencent .iot .explorer .device .video .recorder .param .AudioEncodeParam ;
54
52
import com .tencent .iot .explorer .device .video .recorder .param .MicParam ;
55
53
import com .tencent .iot .explorer .device .video .recorder .param .VideoEncodeParam ;
79
77
import static com .tencent .iot .explorer .device .android .utils .ConvertUtils .byte2HexOnlyLatest8 ;
80
78
import static com .tencent .iot .explorer .device .video .recorder .consts .LogConst .RTC_TAG ;
81
79
82
- public class RecordVideoActivity extends AppCompatActivity implements TextureView .SurfaceTextureListener , OnEncodeListener , SurfaceHolder .Callback {
80
+ public class RecordVideoActivity extends AppCompatActivity implements TextureView .SurfaceTextureListener , OnEncodeListener , SurfaceHolder .Callback ,
81
+ IMediaPlayer .OnInfoListener {
83
82
84
83
private static Timer bitRateTimer ;
85
84
private String TAG = RecordVideoActivity .class .getSimpleName ();
@@ -117,14 +116,6 @@ public class RecordVideoActivity extends AppCompatActivity implements TextureVie
117
116
private int vh = 240 ;
118
117
private int frameRate = 15 ;
119
118
120
- private LinkedBlockingDeque <Byte > playPcmData = new LinkedBlockingDeque <>(); // 内存队列,用于缓存获取到的播放器音频pcm
121
- private String audioCacheFilePath = Environment .getExternalStorageDirectory ().getAbsolutePath ()+ "/audio1_cache_file" ;
122
- private Handler writeHandler ;
123
- private final int MESSAGE_AUDIO_ENCODE_FROM_BYTE = 3000 ;
124
- private FileOutputStream fos1 ;
125
- private FileOutputStream fos2 ;
126
- private FileOutputStream fos3 ;
127
-
128
119
@ Override
129
120
protected void onCreate (@ Nullable Bundle savedInstanceState ) {
130
121
super .onCreate (savedInstanceState );
@@ -187,97 +178,33 @@ private void initAudioEncoder() {
187
178
.setAudioFormat (AudioFormat .ENCODING_PCM_16BIT ) // PCM
188
179
.build ();
189
180
AudioEncodeParam audioEncodeParam = new AudioEncodeParam .Builder ().build ();
190
- audioEncoder = new AudioEncoder (micParam , audioEncodeParam , processedPcmListener );
181
+ audioEncoder = new AudioEncoder (this , micParam , audioEncodeParam , true , true );
191
182
audioEncoder .setOnEncodeListener (this );
192
183
}
193
184
194
- private OnReadAECProcessedPcmListener processedPcmListener = new OnReadAECProcessedPcmListener () {
195
- @ Override
196
- public byte [] onReadAECProcessedPcmListener (byte [] micPcmBytes ) {
197
- // byte[] playerPcmBytes = onReadPlayerPlayPcm(micPcmBytes.length);
198
- // byte[] cancell = new byte[micPcmBytes.length];
199
- // for (int i = 0; i<4; i++) {
200
- // byte[] tempAudioR = new byte[640];
201
- // byte[] tempPlayer = new byte[640];
202
- // System.arraycopy(micPcmBytes, i*640, tempAudioR, 0, 640);
203
- // System.arraycopy(playerPcmBytes, i*640, tempPlayer, 0, 640);
204
- // byte[] tempC = GvoiceJNIBridge.cancellation(tempAudioR, tempPlayer);
205
- // System.arraycopy(tempC, 0, cancell, i*640, 640);
206
- // }
207
- //
208
- // if (writeHandler != null) {
209
- // JSONObject jsonObject = new JSONObject();
210
- // try {
211
- // jsonObject.put("micBytesData", micPcmBytes);
212
- // jsonObject.put("playerBytesData", playerPcmBytes);
213
- // jsonObject.put("cancellBytesData", cancell);
214
- // } catch (JSONException e) {
215
- // e.printStackTrace();
216
- // }
217
- // Message message = writeHandler.obtainMessage(MESSAGE_AUDIO_ENCODE_FROM_BYTE, jsonObject);
218
- // message.arg1 = micPcmBytes.length;
219
- // writeHandler.sendMessage(message);
220
- // }
221
- // return cancell;
222
- return micPcmBytes ;
223
- }
224
-
225
- @ Override
226
- public void audioCodecRelease () {
227
- // GvoiceJNIBridge.destory();
228
- // playPcmData.clear();
229
- }
230
- };
231
-
232
- private byte [] onReadPlayerPlayPcm (int length ) {
233
- if (player != null && player .isPlaying ()) {
234
- byte [] data = new byte [204800 ];
235
- int len = player ._getPcmData (data );
236
- if (playPcmData .size () > 8 *length ) {
237
- if (len > 6 *length ) {
238
- len = 6 *length ;
239
- } else if (len == 0 ) {
240
- } else {
241
- int temp = playPcmData .size () - (6 *length - len );
242
- for (int i = 0 ; i < temp ; i ++) {
243
- playPcmData .remove ();
244
- }
245
- }
246
- } else if (len > 8 *length ) {
247
- len = 6 *length ;
248
- }
249
- if (len > 0 ) {
250
- byte [] playerBytes = new byte [len ];
251
- System .arraycopy (data , 0 , playerBytes , 0 , len );
252
- List <Byte > tmpList = new ArrayList <>();
253
- for (byte b : playerBytes ){
254
- tmpList .add (b );
255
- }
256
- playPcmData .addAll (tmpList );
257
- }
258
- if (playPcmData .size () > length ) {
259
- byte [] res = new byte [length ];
260
- try {
261
- for (int i = 0 ; i < length ; i ++) {
262
- res [i ] = playPcmData .take ();
263
- }
264
- } catch (InterruptedException e ) {
265
- e .printStackTrace ();
266
- }
267
- return res ;
268
- } else {
269
- return new byte [length ];
270
- }
271
- }
272
- return new byte [length ];
273
- }
274
-
275
185
private void initVideoEncoder () {
276
186
VideoEncodeParam videoEncodeParam = new VideoEncodeParam .Builder ().setSize (vw , vh ).setFrameRate (frameRate ).setBitRate (vw *vh ).build ();
277
187
videoEncoder = new VideoEncoder (videoEncodeParam );
278
188
videoEncoder .setEncoderListener (this );
279
189
}
280
190
191
+ @ Override
192
+ public boolean onInfo (IMediaPlayer mp , int what , int extra ) {
193
+ return false ;
194
+ }
195
+
196
+ @ Override
197
+ public boolean onInfoSEI (IMediaPlayer mp , int what , int extra , String sei_content ) {
198
+ return false ;
199
+ }
200
+
201
+ @ Override
202
+ public void onInfoAudioPcmData (IMediaPlayer mp , byte [] arrPcm , int length ) {
203
+ if (audioEncoder != null && length > 0 ) {
204
+ audioEncoder .setPlayerPcmData (arrPcm );
205
+ }
206
+ }
207
+
281
208
282
209
public class AdapterBitRateTask extends TimerTask {
283
210
@ Override
@@ -335,11 +262,7 @@ private void startRecord() {
335
262
if (phoneInfo .getCallType () == CallingType .TYPE_VIDEO_CALL ) {
336
263
startEncodeVideo = true ;
337
264
}
338
- if (!TextUtils .isEmpty (audioCacheFilePath )) {
339
- new WriteThread ().start ();
340
- }
341
265
audioEncoder .start ();
342
- // GvoiceJNIBridge.init();
343
266
344
267
startBitRateAdapter ();
345
268
}
@@ -400,7 +323,6 @@ private void releasePlayer() {
400
323
if (player .isPlaying ()) {
401
324
player .stop ();
402
325
}
403
- player ._setApmStatus (false );
404
326
player .release ();
405
327
}
406
328
}
@@ -443,6 +365,7 @@ private void play() {
443
365
}
444
366
player = new IjkMediaPlayer ();
445
367
player .reset ();
368
+ player .setOnInfoListener (this );
446
369
mHandler .sendEmptyMessageDelayed (MSG_UPDATE_HUD , 500 );
447
370
/*
448
371
* probesize & analyzeduration 可通过这两个参数进行首开延时优化
@@ -470,7 +393,6 @@ private void play() {
470
393
player .setMaxPacketNum (2 );
471
394
player .setSurface (this .surface );
472
395
player .setAndroidIOCallback (ReadByteIO .Companion .getInstance ());
473
- player ._setApmStatus (true );
474
396
player .setOnErrorListener (new IMediaPlayer .OnErrorListener () {
475
397
@ Override
476
398
public boolean onError (IMediaPlayer mp , int what , int extra ) {
@@ -725,70 +647,4 @@ private String getSendStreamStatus() {
725
647
return "Unkown" ;
726
648
}
727
649
728
- class WriteThread extends Thread {
729
- @ SuppressLint ("HandlerLeak" )
730
- @ Override
731
- public void run () {
732
- super .run ();
733
- Looper .prepare ();
734
- File file1 = new File (audioCacheFilePath +"_file1.pcm" );
735
- File file2 = new File (audioCacheFilePath +"_file2.pcm" );
736
- File file3 = new File (audioCacheFilePath +"_file3.pcm" );
737
- Log .i (TAG , "audio cache pcm file path:" + audioCacheFilePath );
738
- if (file1 .exists ()) {
739
- file1 .delete ();
740
- }
741
- if (file2 .exists ()) {
742
- file2 .delete ();
743
- }
744
- if (file3 .exists ()) {
745
- file3 .delete ();
746
- }
747
- try {
748
- file1 .createNewFile ();
749
- file2 .createNewFile ();
750
- file3 .createNewFile ();
751
- } catch (IOException e ) {
752
- e .printStackTrace ();
753
- }
754
- try {
755
- fos1 = new FileOutputStream (file1 );
756
- fos2 = new FileOutputStream (file2 );
757
- fos3 = new FileOutputStream (file3 );
758
- } catch (FileNotFoundException e ) {
759
- e .printStackTrace ();
760
- Log .e (TAG , "临时缓存文件未找到" );
761
- }
762
- writeHandler = new Handler () {
763
- @ Override
764
- public void handleMessage (Message msg ) {
765
- super .handleMessage (msg );
766
- if (msg .what == MESSAGE_AUDIO_ENCODE_FROM_BYTE ) {
767
- JSONObject jsonObject = (JSONObject ) msg .obj ;
768
- if (AudioRecord .ERROR_INVALID_OPERATION != msg .arg1 ) {
769
- if (fos1 != null && fos2 != null && fos3 != null ) {
770
- try {
771
- byte [] micBytesData = (byte []) jsonObject .get ("micBytesData" );
772
- byte [] playerBytesData = (byte []) jsonObject .get ("playerBytesData" );
773
- byte [] cancellBytesData = (byte []) jsonObject .get ("cancellBytesData" );
774
- fos1 .write (micBytesData );
775
- fos1 .flush ();
776
- fos2 .write (playerBytesData );
777
- fos2 .flush ();
778
- fos3 .write (cancellBytesData );
779
- fos3 .flush ();
780
- } catch (IOException e ) {
781
- e .printStackTrace ();
782
- } catch (JSONException e ) {
783
- e .printStackTrace ();
784
- }
785
- }
786
- }
787
- }
788
- }
789
- };
790
- Looper .loop ();
791
- }
792
- }
793
-
794
650
}
0 commit comments