Skip to content

Commit

Permalink
更新gvoice库版本,以及修改调用。
Browse files Browse the repository at this point in the history
Change-Id: Ia00d39663662dfa9f88dfaeb4f251a8dccafaf7d
  • Loading branch information
SundoggyNew committed Apr 25, 2024
1 parent bb8a7b0 commit e65d312
Show file tree
Hide file tree
Showing 9 changed files with 308 additions and 201 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ buildscript {
dependencies {
classpath 'com.android.tools.build:gradle:3.6.3'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath 'com.github.kezong:fat-aar:1.3.8'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import androidx.localbroadcastmanager.content.LocalBroadcastManager;

import com.alibaba.fastjson.JSON;
//import com.iot.gvoice.interfaces.GvoiceJNIBridge;
import com.tencent.iot.explorer.device.android.app.R;
import com.tencent.iot.explorer.device.android.app.utils.CommonUtils;
import com.tencent.iot.explorer.device.common.stateflow.entity.CallingType;
Expand All @@ -49,7 +48,6 @@
import com.tencent.iot.explorer.device.video.recorder.encoder.AudioEncoder;
import com.tencent.iot.explorer.device.video.recorder.encoder.VideoEncoder;
import com.tencent.iot.explorer.device.video.recorder.listener.OnEncodeListener;
import com.tencent.iot.explorer.device.video.recorder.listener.OnReadAECProcessedPcmListener;
import com.tencent.iot.explorer.device.video.recorder.param.AudioEncodeParam;
import com.tencent.iot.explorer.device.video.recorder.param.MicParam;
import com.tencent.iot.explorer.device.video.recorder.param.VideoEncodeParam;
Expand Down Expand Up @@ -79,7 +77,8 @@
import static com.tencent.iot.explorer.device.android.utils.ConvertUtils.byte2HexOnlyLatest8;
import static com.tencent.iot.explorer.device.video.recorder.consts.LogConst.RTC_TAG;

public class RecordVideoActivity extends AppCompatActivity implements TextureView.SurfaceTextureListener, OnEncodeListener, SurfaceHolder.Callback {
public class RecordVideoActivity extends AppCompatActivity implements TextureView.SurfaceTextureListener, OnEncodeListener, SurfaceHolder.Callback,
IMediaPlayer.OnInfoListener {

private static Timer bitRateTimer;
private String TAG = RecordVideoActivity.class.getSimpleName();
Expand Down Expand Up @@ -117,14 +116,6 @@ public class RecordVideoActivity extends AppCompatActivity implements TextureVie
private int vh = 240;
private int frameRate = 15;

private LinkedBlockingDeque<Byte> playPcmData = new LinkedBlockingDeque<>(); // 内存队列,用于缓存获取到的播放器音频pcm
private String audioCacheFilePath = Environment.getExternalStorageDirectory().getAbsolutePath()+ "/audio1_cache_file";
private Handler writeHandler;
private final int MESSAGE_AUDIO_ENCODE_FROM_BYTE = 3000;
private FileOutputStream fos1;
private FileOutputStream fos2;
private FileOutputStream fos3;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand Down Expand Up @@ -187,97 +178,33 @@ private void initAudioEncoder() {
.setAudioFormat(AudioFormat.ENCODING_PCM_16BIT) // PCM
.build();
AudioEncodeParam audioEncodeParam = new AudioEncodeParam.Builder().build();
audioEncoder = new AudioEncoder(micParam, audioEncodeParam, processedPcmListener);
audioEncoder = new AudioEncoder(this, micParam, audioEncodeParam, true, true);
audioEncoder.setOnEncodeListener(this);
}

private OnReadAECProcessedPcmListener processedPcmListener = new OnReadAECProcessedPcmListener() {
@Override
public byte[] onReadAECProcessedPcmListener(byte[] micPcmBytes) {
// byte[] playerPcmBytes = onReadPlayerPlayPcm(micPcmBytes.length);
// byte[] cancell = new byte[micPcmBytes.length];
// for (int i = 0; i<4; i++) {
// byte[] tempAudioR = new byte[640];
// byte[] tempPlayer = new byte[640];
// System.arraycopy(micPcmBytes, i*640, tempAudioR, 0, 640);
// System.arraycopy(playerPcmBytes, i*640, tempPlayer, 0, 640);
// byte[] tempC = GvoiceJNIBridge.cancellation(tempAudioR, tempPlayer);
// System.arraycopy(tempC, 0, cancell, i*640, 640);
// }
//
// if (writeHandler != null) {
// JSONObject jsonObject = new JSONObject();
// try {
// jsonObject.put("micBytesData", micPcmBytes);
// jsonObject.put("playerBytesData", playerPcmBytes);
// jsonObject.put("cancellBytesData", cancell);
// } catch (JSONException e) {
// e.printStackTrace();
// }
// Message message = writeHandler.obtainMessage(MESSAGE_AUDIO_ENCODE_FROM_BYTE, jsonObject);
// message.arg1 = micPcmBytes.length;
// writeHandler.sendMessage(message);
// }
// return cancell;
return micPcmBytes;
}

@Override
public void audioCodecRelease() {
// GvoiceJNIBridge.destory();
// playPcmData.clear();
}
};

private byte[] onReadPlayerPlayPcm(int length) {
if (player != null && player.isPlaying()) {
byte[] data = new byte[204800];
int len = player._getPcmData(data);
if (playPcmData.size() > 8*length) {
if (len > 6*length) {
len = 6*length;
} else if (len == 0) {
} else {
int temp = playPcmData.size() - (6*length - len);
for (int i = 0 ; i < temp ; i++) {
playPcmData.remove();
}
}
} else if (len > 8*length) {
len = 6*length;
}
if (len > 0) {
byte[] playerBytes = new byte[len];
System.arraycopy(data, 0, playerBytes, 0, len);
List<Byte> tmpList = new ArrayList<>();
for (byte b : playerBytes){
tmpList.add(b);
}
playPcmData.addAll(tmpList);
}
if (playPcmData.size() > length) {
byte[] res = new byte[length];
try {
for (int i = 0 ; i < length ; i++) {
res[i] = playPcmData.take();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
return res;
} else {
return new byte[length];
}
}
return new byte[length];
}

private void initVideoEncoder() {
VideoEncodeParam videoEncodeParam = new VideoEncodeParam.Builder().setSize(vw, vh).setFrameRate(frameRate).setBitRate(vw*vh).build();
videoEncoder = new VideoEncoder(videoEncodeParam);
videoEncoder.setEncoderListener(this);
}

@Override
public boolean onInfo(IMediaPlayer mp, int what, int extra) {
return false;
}

@Override
public boolean onInfoSEI(IMediaPlayer mp, int what, int extra, String sei_content) {
return false;
}

@Override
public void onInfoAudioPcmData(IMediaPlayer mp, byte[] arrPcm, int length) {
if (audioEncoder != null && length > 0) {
audioEncoder.setPlayerPcmData(arrPcm);
}
}


public class AdapterBitRateTask extends TimerTask {
@Override
Expand Down Expand Up @@ -335,11 +262,7 @@ private void startRecord() {
if (phoneInfo.getCallType() == CallingType.TYPE_VIDEO_CALL) {
startEncodeVideo = true;
}
if (!TextUtils.isEmpty(audioCacheFilePath)) {
new WriteThread().start();
}
audioEncoder.start();
// GvoiceJNIBridge.init();

startBitRateAdapter();
}
Expand Down Expand Up @@ -400,7 +323,6 @@ private void releasePlayer() {
if (player.isPlaying()) {
player.stop();
}
player._setApmStatus(false);
player.release();
}
}
Expand Down Expand Up @@ -443,6 +365,7 @@ private void play() {
}
player = new IjkMediaPlayer();
player.reset();
player.setOnInfoListener(this);
mHandler.sendEmptyMessageDelayed(MSG_UPDATE_HUD, 500);
/*
* probesize & analyzeduration 可通过这两个参数进行首开延时优化
Expand Down Expand Up @@ -470,7 +393,6 @@ private void play() {
player.setMaxPacketNum(2);
player.setSurface(this.surface);
player.setAndroidIOCallback(ReadByteIO.Companion.getInstance());
player._setApmStatus(true);
player.setOnErrorListener(new IMediaPlayer.OnErrorListener() {
@Override
public boolean onError(IMediaPlayer mp, int what, int extra) {
Expand Down Expand Up @@ -725,70 +647,4 @@ private String getSendStreamStatus() {
return "Unkown";
}

class WriteThread extends Thread {
@SuppressLint("HandlerLeak")
@Override
public void run() {
super.run();
Looper.prepare();
File file1 = new File(audioCacheFilePath+"_file1.pcm");
File file2 = new File(audioCacheFilePath+"_file2.pcm");
File file3 = new File(audioCacheFilePath+"_file3.pcm");
Log.i(TAG, "audio cache pcm file path:" + audioCacheFilePath);
if (file1.exists()) {
file1.delete();
}
if (file2.exists()) {
file2.delete();
}
if (file3.exists()) {
file3.delete();
}
try {
file1.createNewFile();
file2.createNewFile();
file3.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
try {
fos1 = new FileOutputStream(file1);
fos2 = new FileOutputStream(file2);
fos3 = new FileOutputStream(file3);
} catch (FileNotFoundException e) {
e.printStackTrace();
Log.e(TAG, "临时缓存文件未找到");
}
writeHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.what == MESSAGE_AUDIO_ENCODE_FROM_BYTE) {
JSONObject jsonObject = (JSONObject) msg.obj;
if (AudioRecord.ERROR_INVALID_OPERATION != msg.arg1) {
if (fos1 != null && fos2 != null && fos3 != null) {
try {
byte[] micBytesData = (byte[]) jsonObject.get("micBytesData");
byte[] playerBytesData = (byte[]) jsonObject.get("playerBytesData");
byte[] cancellBytesData = (byte[]) jsonObject.get("cancellBytesData");
fos1.write(micBytesData);
fos1.flush();
fos2.write(playerBytesData);
fos2.flush();
fos3.write(cancellBytesData);
fos3.flush();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
}
};
Looper.loop();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ private void initAudioEncoder() {
.setAudioFormat(AudioFormat.ENCODING_PCM_16BIT) // PCM
.build();
AudioEncodeParam audioEncodeParam = new AudioEncodeParam.Builder().build();
audioEncoder = new AudioEncoder(micParam, audioEncodeParam);
audioEncoder = new AudioEncoder(this, micParam, audioEncodeParam);
audioEncoder.setOnEncodeListener(this);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ private void initAudioEncoder() {
.setAudioFormat(AudioFormat.ENCODING_PCM_16BIT) // PCM
.build();
AudioEncodeParam audioEncodeParam = new AudioEncodeParam.Builder().build();
audioEncoder = new AudioEncoder(micParam, audioEncodeParam);
audioEncoder = new AudioEncoder(this, micParam, audioEncodeParam);
audioEncoder.setOnEncodeListener(this);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,10 @@
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import tv.danmaku.ijk.media.player.IMediaPlayer;
import tv.danmaku.ijk.media.player.IjkMediaPlayer;

public class RecordVideoActivity2 extends AppCompatActivity implements TextureView.SurfaceTextureListener, OnEncodeListener, SurfaceHolder.Callback {
public class RecordVideoActivity2 extends AppCompatActivity implements TextureView.SurfaceTextureListener, OnEncodeListener, SurfaceHolder.Callback, IMediaPlayer.OnInfoListener {

private static final String TAG = RecordVideoActivity2.class.getSimpleName();
private static final int MAX_CONNECT_NUM = 4;
Expand Down Expand Up @@ -131,20 +132,21 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
}
initAudioEncoder();
initVideoEncoder();
VideoFormat format = new VideoFormat.Builder().setVideoWidth(vw).setVideoHeight(vh).build();
VideoFormat format = new VideoFormat.Builder().setVideoWidth(vw).setVideoHeight(vh).setAudioSampleRate(16000).build();
VideoNativeInteface.getInstance().initVideoFormat(format);
}

private void initAudioEncoder() {
MicParam micParam = new MicParam.Builder()
.setAudioSource(MediaRecorder.AudioSource.MIC)
.setSampleRateInHz(8000) // 采样率
.setSampleRateInHz(16000) // 采样率
.setChannelConfig(AudioFormat.CHANNEL_IN_MONO)
.setAudioFormat(AudioFormat.ENCODING_PCM_16BIT) // PCM
.build();
AudioEncodeParam audioEncodeParam = new AudioEncodeParam.Builder().build();
audioEncoder = new AudioEncoder(micParam, audioEncodeParam);
audioEncoder = new AudioEncoder(this, micParam, audioEncodeParam, true, true);
audioEncoder.setOnEncodeListener(this);
audioEncoder.recordPcmFile(true);
}

private void initVideoEncoder() {
Expand Down Expand Up @@ -253,6 +255,7 @@ public void onSurfaceTextureUpdated(SurfaceTexture surface) { }
private void play() {
player = new IjkMediaPlayer();
player.reset();
player.setOnInfoListener(this);
if (phoneInfo.getCallType() == CallingType.TYPE_AUDIO_CALL) {
player.setOption(IjkMediaPlayer.OPT_CATEGORY_FORMAT, "analyzeduration", 1000);
player.setOption(IjkMediaPlayer.OPT_CATEGORY_FORMAT, "probesize", 64);
Expand Down Expand Up @@ -419,5 +422,22 @@ public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {
public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
Log.d(TAG, "surface destroyed.");
}

@Override
public boolean onInfo(IMediaPlayer mp, int what, int extra) {
return false;
}

@Override
public boolean onInfoSEI(IMediaPlayer mp, int what, int extra, String sei_content) {
return false;
}

@Override
public void onInfoAudioPcmData(IMediaPlayer mp, byte[] arrPcm, int length) {
if (audioEncoder != null && length > 0) {
audioEncoder.setPlayerPcmData(arrPcm);
}
}
}

12 changes: 8 additions & 4 deletions explorer/explorer-device-video/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ apply plugin: 'signing'

apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'com.kezong.fat-aar'

android {
compileSdkVersion 29
Expand Down Expand Up @@ -39,23 +40,26 @@ android {
lintOptions {
abortOnError false
}
repositories {
flatDir { dirs 'libs' }
}
}

repositories {
maven { url "https://oss.sonatype.org/content/repositories/snapshots" }
}

dependencies {
api fileTree(include: ['*.jar', '*.aar'], dir: 'libs')
embed (name:'android_gvoice-release',ext:'aar')
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
implementation 'com.alibaba:fastjson:1.2.73'
api 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.4'
api 'com.tencent.iot.thirdparty.android:xp2p-sdk:2.4.35'
api 'com.tencent.iot.thirdparty.android:ijkplayer-java:2.0.7'
api 'com.tencent.iot.thirdparty.android:ijkplayer-armv7a:2.0.7'
api 'com.tencent.iot.thirdparty.android:ijkplayer-arm64:2.0.7'
api 'com.tencent.iot.thirdparty.android:ijkplayer-java:2.0.14'
api 'com.tencent.iot.thirdparty.android:ijkplayer-armv7a:2.0.14'
api 'com.tencent.iot.thirdparty.android:ijkplayer-arm64:2.0.14'
api project(':explorer:explorer-media-common')
//xxxapi project(':explorer:explorer//xxx-media-common')
if (findProject(':explorer:explorer-device-android') != null) {
Expand Down
Binary file not shown.
Loading

0 comments on commit e65d312

Please sign in to comment.