Skip to content

Commit

Permalink
Merge pull request #30 from SonicCloudOrg/v1.3.1-release
Browse files Browse the repository at this point in the history
合入main测试
  • Loading branch information
ZhouYixun authored Feb 14, 2022
2 parents 653daa2 + b4c80cc commit e3504f4
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ public class AppListActivity extends Activity {
private static final String SOCKET = "sonicapplistservice";
private LocalServerSocket serverSocket;

/** 数据缓冲大小 */
private static final int BUFFER_SIZE = 500000;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand Down Expand Up @@ -60,6 +63,9 @@ private void manageClientConnection() {
LocalSocket clientSocket;
try {
clientSocket = serverSocket.accept();
//设置缓冲大小
clientSocket.setReceiveBufferSize(BUFFER_SIZE);
clientSocket.setSendBufferSize(BUFFER_SIZE);
Log.d(TAG, "client connected");
OutputStream outputStream = clientSocket.getOutputStream();
getAllApp(outputStream);
Expand Down
131 changes: 51 additions & 80 deletions app/src/main/java/org/cloud/sonic/android/AudioService.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
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 @@ -20,15 +18,12 @@
import android.media.MediaCodec;
import android.media.MediaCodecInfo;
import android.media.MediaFormat;
import android.media.MediaRecorder;
import android.media.projection.MediaProjection;
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;
import android.os.IBinder;
import android.os.Message;
import android.support.annotation.NonNull;
Expand All @@ -38,14 +33,11 @@
import org.cloud.sonic.android.recorder.utils.Logger;
import org.cloud.sonic.android.util.ADTSUtil;

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 Down Expand Up @@ -73,14 +65,15 @@ public class AudioService extends Service {
private MediaCodec mMediaCodec;
private AudioRecord mAudioRecord;

private LocalSocket inComingSock;

private LinkedBlockingQueue mRunnables = new LinkedBlockingQueue<Runnable>();
private Thread workThread;
//开启 LocalServiceSocket 的服务器
private LocalServerSocket serverSocket;
private LocalSocket clientSocket;
private OutputStream outputStream;
private InputStream mInputStream;

private int mBufferSize = 4 * 1024;


private final Handler mHandler = new Handler() {
public void handleMessage(android.os.Message msg) {
String dispMesg = (String) msg.obj;
Expand Down Expand Up @@ -121,12 +114,6 @@ public int onStartCommand(Intent intent, int flags, int startId) {
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);
Expand Down Expand Up @@ -175,14 +162,6 @@ private Notification.Action createStopAction() {
return actionBuilder.build();
}

private static LocalSocket connect() throws IOException {
LocalServerSocket localServerSocket = new LocalServerSocket("");
try {
return localServerSocket.accept();
} finally {
localServerSocket.close();
}
}

@SuppressLint("NewApi")
private static AudioPlaybackCaptureConfiguration createAudioPlaybackCaptureConfig(MediaProjection mediaProjection) {
Expand Down Expand Up @@ -218,16 +197,17 @@ 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();
}
try{
Log.i(TAG, String.format("creating socket %s", CHANNEL_ID));
serverSocket = new LocalServerSocket(CHANNEL_ID);
Log.i(TAG, String.format("Listening on %s", CHANNEL_ID));
clientSocket = serverSocket.accept();
Log.d(TAG, "client connected");
outputStream = clientSocket.getOutputStream();
}catch (IOException e){
e.printStackTrace();

}
mRunnables.clear();
Log.d("MediaPublisher", "= =lgd= Rtmp发布线程退出...");
}
};
workThread.start();
Expand Down Expand Up @@ -275,31 +255,13 @@ public void onOutputBufferAvailable(@NonNull MediaCodec codec, int outputBufferI
ADTSUtil.addADTS(oneADTSFrameBytes);
ByteBuffer outputBuffer = codec.getOutputBuffer(outputBufferIndex);
outputBuffer.get(oneADTSFrameBytes, 7, mBufferInfo.size);
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);
if (outputStream!=null){
try {
outputStream.write(oneADTSFrameBytes,0,oneADTSFrameBytes.length);
} catch (IOException e) {
e.printStackTrace();
}
};
try {
mRunnables.put(runnable);
} catch (InterruptedException e) {
Log.e(TAG, " =lgd= outputAudioData=====error: "+e.toString());
e.printStackTrace();
}

}
codec.releaseOutputBuffer(outputBufferIndex, false);
}
Expand Down Expand Up @@ -334,8 +296,21 @@ public void onDestroy() {
mAudioRecord.release();
mMediaCodec.stop();
mMediaCodec.release();
try {
if(serverSocket!=null){
serverSocket.close();
serverSocket = null;
}
if (clientSocket!=null){
clientSocket.getOutputStream().close();
clientSocket.close();
clientSocket = null;
}
outputStream = null;
}catch (IOException e){
e.printStackTrace();
}
stopForeground(true);

}

private NotificationManager getNotificationManager() {
Expand All @@ -345,29 +320,25 @@ 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();
// }/**/
while (true) {
if (clientSocket!=null){
try {
byte[] buffer = new byte[1024];
mInputStream = clientSocket.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();
}
}
}
}


private static final class ConnectionHandler extends Handler {

private AudioService service;
Expand Down

0 comments on commit e3504f4

Please sign in to comment.