Skip to content

Commit

Permalink
Merge pull request #25 from SonicCloudOrg/v1.3.1-beta
Browse files Browse the repository at this point in the history
V1.3.1 beta
  • Loading branch information
ZhouYixun committed Feb 11, 2022
2 parents 99f913d + 6d804ef commit 78c5c25
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 44 deletions.
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,5 @@ dependencies {
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.alibaba:fastjson:1.2.79'
compileOnly fileTree(dir: '../shim/libs', include: ['*.jar'])

}
126 changes: 83 additions & 43 deletions app/src/main/java/org/cloud/sonic/android/AudioService.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.cloud.sonic.android;

import static android.net.LocalSocket.SOCKET_DGRAM;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Notification;
Expand All @@ -23,6 +25,7 @@
import android.media.projection.MediaProjectionManager;
import android.net.LocalServerSocket;
import android.net.LocalSocket;
import android.net.LocalSocketAddress;
import android.os.Build;
import android.os.Handler;
import android.os.HandlerThread;
Expand All @@ -38,8 +41,11 @@
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.LinkedBlockingQueue;

/**
* @author Eason, Jeffrey.Wang
Expand All @@ -56,8 +62,6 @@ public class AudioService extends Service {

private static final int MSG_CONNECTION_ESTABLISHED = 1;

private static final String SOCKET_NAME = "sonicaudioservice";


private static final int SAMPLE_RATE = 44100;
private static final int CHANNELS = 2;
Expand All @@ -69,9 +73,12 @@ public class AudioService extends Service {
private MediaCodec mMediaCodec;
private AudioRecord mAudioRecord;

//子线程处理 转录音频
private Handler mAudioEncoderHandler;
private HandlerThread mAudioEncoderHandlerThread = new HandlerThread("AudioEncoder");
private LocalSocket inComingSock;

private LinkedBlockingQueue mRunnables = new LinkedBlockingQueue<Runnable>();
private Thread workThread;

private int mBufferSize = 4 * 1024;


private final Handler mHandler = new Handler() {
Expand Down Expand Up @@ -114,29 +121,24 @@ public int onStartCommand(Intent intent, int flags, int startId) {
return START_NOT_STICKY;
}

if (isRunning()) {
return START_NOT_STICKY;
}

// if (isRunning()) {
// return START_NOT_STICKY;
// }

// Logger.i("wzasd",connectSock() + "");

Intent data = intent.getParcelableExtra(EXTRA_MEDIA_PROJECTION_DATA);
mediaProjectionManager = (MediaProjectionManager) getSystemService(MEDIA_PROJECTION_SERVICE);
mediaProjection = mediaProjectionManager.getMediaProjection(Activity.RESULT_OK, data);

if (mediaProjection != null) {
mAudioEncoderHandlerThread.start();
mAudioEncoderHandler = new Handler(mAudioEncoderHandlerThread.getLooper());
startRecording();
//必须要在子线程里接收消息
new Thread(this::acceptMsg).start();
} else {
Log.w(TAG, "Failed to capture audio");
stopSelf();
}



return START_NOT_STICKY;
}

Expand Down Expand Up @@ -174,7 +176,7 @@ private Notification.Action createStopAction() {
}

private static LocalSocket connect() throws IOException {
LocalServerSocket localServerSocket = new LocalServerSocket(SOCKET_NAME);
LocalServerSocket localServerSocket = new LocalServerSocket("");
try {
return localServerSocket.accept();
} finally {
Expand Down Expand Up @@ -213,8 +215,26 @@ private static AudioRecord createAudioRecord(MediaProjection mediaProjection) {
//record audio
@RequiresApi(api = Build.VERSION_CODES.M)
private void startRecording() {
workThread = new Thread("publish-thread") {
@Override
public void run() {
while (!interrupted()) {
try {
Runnable runnable = (Runnable) mRunnables.take();
runnable.run();
} catch ( InterruptedException e) {
e.printStackTrace();
}
}
mRunnables.clear();
Log.d("MediaPublisher", "= =lgd= Rtmp发布线程退出...");
}
};
workThread.start();

mAudioRecord = createAudioRecord(mediaProjection);
mAudioRecord.startRecording();

recordInternalAudio(mAudioRecord);
}

Expand All @@ -230,9 +250,8 @@ void recordInternalAudio(AudioRecord audioRecord) {
mMediaCodec.configure(mediaFormat, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
final int[] totalBytesRead = {0};
final Long[] mPresentationTime = {0L};
int trackId = 0;
mMediaCodec.setCallback(new MediaCodec.Callback() {

mMediaCodec.setCallback(new MediaCodec.Callback() {
@Override
public void onInputBufferAvailable(@NonNull MediaCodec mediaCodec, int i) {
ByteBuffer codecInputBuffer = mediaCodec.getInputBuffer(i);
Expand All @@ -256,17 +275,37 @@ public void onOutputBufferAvailable(@NonNull MediaCodec codec, int outputBufferI
ADTSUtil.addADTS(oneADTSFrameBytes);
ByteBuffer outputBuffer = codec.getOutputBuffer(outputBufferIndex);
outputBuffer.get(oneADTSFrameBytes, 7, mBufferInfo.size);
try (LocalSocket socket = connect()) {
socket.getOutputStream().write(oneADTSFrameBytes, 0, oneADTSFrameBytes.length);
} catch (IOException e) {
Logger.i("inComingSock", "outputBuffer");
Runnable runnable = new Runnable() {
@Override
public void run() {
try {
LocalSocket localSocket = new LocalSocket();
localSocket.connect(new LocalSocketAddress("sonicaudioservice"));
OutputStream os = localSocket.getOutputStream();
os.write(oneADTSFrameBytes, 0, outputBufferIndex);
localSocket.close();
}catch (IOException e) {
// ignore
Logger.e("inComingSock", e.getMessage());

}
Logger.i("inComingSock", "outPutAACData len"+oneADTSFrameBytes.length);
}
};
try {
mRunnables.put(runnable);
} catch (InterruptedException e) {
Log.e(TAG, " =lgd= outputAudioData=====error: "+e.toString());
e.printStackTrace();
}

}
codec.releaseOutputBuffer(outputBufferIndex, false);
}

@Override
public void onError(@NonNull MediaCodec mediaCodec, @NonNull MediaCodec.CodecException e) {

e.printStackTrace();
stopSelf();
}
Expand All @@ -275,13 +314,12 @@ public void onError(@NonNull MediaCodec mediaCodec, @NonNull MediaCodec.CodecExc
public void onOutputFormatChanged(@NonNull MediaCodec mediaCodec, @NonNull MediaFormat mediaFormat) {

}
},mAudioEncoderHandler);
});

} catch (IOException e) {
e.printStackTrace();
}
mMediaCodec.start();

}

private boolean isRunning() {
Expand All @@ -307,28 +345,29 @@ private NotificationManager getNotificationManager() {

@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
private void acceptMsg() {
InputStream mInputStream = null;
try (LocalSocket mSocket = connect()) {
while (true) {
try {
byte[] buffer = new byte[1024];
mInputStream = mSocket.getInputStream();
int count = mInputStream.read(buffer);
String key = new String(Arrays.copyOfRange(buffer, 0, count));
Log.d(TAG, "ServerActivity mSocketOutStream==" + key);
Message msg = mHandler.obtainMessage();
msg.obj = key;
msg.sendToTarget();
} catch (IOException e) {
Log.d(TAG, "exception==" + e.fillInStackTrace().getMessage());
e.printStackTrace();
}
}
} catch (IOException e1) {
e1.printStackTrace();
}/**/
// InputStream mInputStream = null;
// try (LocalSocket mSocket = connect()) {
// while (true) {
// try {
// byte[] buffer = new byte[1024];
// mInputStream = mSocket.getInputStream();
// int count = mInputStream.read(buffer);
// String key = new String(Arrays.copyOfRange(buffer, 0, count));
// Log.d(TAG, "ServerActivity mSocketOutStream==" + key);
// Message msg = mHandler.obtainMessage();
// msg.obj = key;
// msg.sendToTarget();
// } catch (IOException e) {
// Log.d(TAG, "exception==" + e.fillInStackTrace().getMessage());
// e.printStackTrace();
// }
// }
// } catch (IOException e1) {
// e1.printStackTrace();
// }/**/
}


private static final class ConnectionHandler extends Handler {

private AudioService service;
Expand All @@ -350,4 +389,5 @@ public void handleMessage(Message message) {
}
}
}

}
2 changes: 1 addition & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<resources>
<string name="app_name">Sonic助手</string>
<string name="version">版本号:v1.3.0</string>
<string name="version">版本号:v1.3.0.10</string>
<string name="welcome">欢迎使用Sonic助手</string>
<string name="service_ticker">Sonic Service running</string>
<string name="service_title">Sonic Service</string>
Expand Down

0 comments on commit 78c5c25

Please sign in to comment.