Commit a86b18f9 authored by sheteng's avatar sheteng

语音房间 1

parent 11ea3344
...@@ -9,6 +9,7 @@ import android.content.Intent; ...@@ -9,6 +9,7 @@ import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler; import android.os.Handler;
import android.os.Looper; import android.os.Looper;
import android.text.InputType;
import android.text.TextUtils; import android.text.TextUtils;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
...@@ -174,7 +175,20 @@ public class NotificationsFragment extends Fragment { ...@@ -174,7 +175,20 @@ public class NotificationsFragment extends Fragment {
binding.joinRoom.setOnClickListener(new View.OnClickListener() { binding.joinRoom.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
CCIMClient.getInstance().joinAudioRoom(getContext(), 62, new AudioRoomListener() { final EditText roomeId = new EditText(getContext());
roomeId.setHint("room id");
roomeId.setInputType(InputType.TYPE_CLASS_NUMBER);
LinearLayout linearLayout = new LinearLayout(getContext());
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.addView(roomeId);
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setTitle("create channel").setIcon(android.R.drawable.ic_dialog_info).setView(linearLayout)
.setNegativeButton("Cancel", null);
builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String roomId = roomeId.getText().toString();
CCIMClient.getInstance().joinAudioRoom(getContext(), Integer.parseInt(roomId), new AudioRoomListener() {
@Override @Override
public void onJoinSuccess() { public void onJoinSuccess() {
ToastUtil.show(getContext(), "onJoinSuccess"); ToastUtil.show(getContext(), "onJoinSuccess");
...@@ -182,7 +196,7 @@ public class NotificationsFragment extends Fragment { ...@@ -182,7 +196,7 @@ public class NotificationsFragment extends Fragment {
@Override @Override
public void onJoinFailed(int code) { public void onJoinFailed(int code) {
ToastUtil.show(getContext(), "onJoinFailed"); ToastUtil.show(getContext(), "onJoinFailed" + code);
} }
@Override @Override
...@@ -195,7 +209,9 @@ public class NotificationsFragment extends Fragment { ...@@ -195,7 +209,9 @@ public class NotificationsFragment extends Fragment {
ToastUtil.show(getContext(), "onError"); ToastUtil.show(getContext(), "onError");
} }
}); });
}
});
builder.show();
} }
}); });
......
...@@ -76,5 +76,5 @@ dependencies { ...@@ -76,5 +76,5 @@ dependencies {
implementation "androidx.room:room-runtime:$room_version" implementation "androidx.room:room-runtime:$room_version"
annotationProcessor "androidx.room:room-compiler:$room_version" annotationProcessor "androidx.room:room-compiler:$room_version"
implementation 'com.karumi:dexter:6.2.3' implementation 'com.karumi:dexter:6.2.3'
implementation 'org.webrtc:google-webrtc:1.0.28513' implementation 'ch.threema:webrtc-android:100.0.0'
} }
\ No newline at end of file
...@@ -5,5 +5,6 @@ ...@@ -5,5 +5,6 @@
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" /> <uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"/>
</manifest> </manifest>
\ No newline at end of file
...@@ -2,8 +2,10 @@ package com.ccwangluo.ccim.audio; ...@@ -2,8 +2,10 @@ package com.ccwangluo.ccim.audio;
import android.Manifest; import android.Manifest;
import android.content.Context; import android.content.Context;
import android.media.AudioManager;
import android.os.Handler; import android.os.Handler;
import android.os.Looper; import android.os.Looper;
import android.util.Log;
import com.ccwangluo.ccim.CCIMClient; import com.ccwangluo.ccim.CCIMClient;
import com.ccwangluo.ccim.listener.AudioRoomListener; import com.ccwangluo.ccim.listener.AudioRoomListener;
...@@ -27,6 +29,7 @@ import com.karumi.dexter.MultiplePermissionsReport; ...@@ -27,6 +29,7 @@ import com.karumi.dexter.MultiplePermissionsReport;
import com.karumi.dexter.PermissionToken; import com.karumi.dexter.PermissionToken;
import com.karumi.dexter.listener.PermissionRequest; import com.karumi.dexter.listener.PermissionRequest;
import com.karumi.dexter.listener.multi.MultiplePermissionsListener; import com.karumi.dexter.listener.multi.MultiplePermissionsListener;
import org.webrtc.DataChannel; import org.webrtc.DataChannel;
import org.webrtc.EglBase; import org.webrtc.EglBase;
import org.webrtc.Logging; import org.webrtc.Logging;
...@@ -44,13 +47,17 @@ public class JanusService extends JanusDelegate { ...@@ -44,13 +47,17 @@ public class JanusService extends JanusDelegate {
private Boolean isJoin = false; private Boolean isJoin = false;
private AudioRoomListener callback; private AudioRoomListener callback;
// private AppRTCAudioManager audioManager; private AudioManager audioManager;
public JanusService(Context context, Integer roomId, User user, AudioRoomListener callback) { public JanusService(Context context, Integer roomId, User user, AudioRoomListener callback) {
audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
this.user = user; this.user = user;
this.roomId = roomId; this.roomId = roomId;
this.callback = callback; this.callback = callback;
if (audioManager != null) {
audioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
audioManager.setSpeakerphoneOn(true);
}
} }
...@@ -59,18 +66,13 @@ public class JanusService extends JanusDelegate { ...@@ -59,18 +66,13 @@ public class JanusService extends JanusDelegate {
@Override @Override
public void onPermissionsChecked(MultiplePermissionsReport report) { public void onPermissionsChecked(MultiplePermissionsReport report) {
JanusFactory factory = new JanusFactory(); JanusFactory factory = new JanusFactory();
_janus = factory.create(conf,JanusService.this , context.getApplicationContext()); _janus = factory.create(conf, JanusService.this, context.getApplicationContext());
Logging.enableLogToDebugOutput(Logging.Severity.LS_VERBOSE); Logging.enableLogToDebugOutput(Logging.Severity.LS_VERBOSE);
_rootEgl = factory.rootEgl(); _rootEgl = factory.rootEgl();
_janus.init(); _janus.init();
// if (audioManager != null) {
// audioManager.start((selectedAudioDevice, availableAudioDevices) -> {
//
// }
// );
// }
} }
@Override @Override
...@@ -81,6 +83,7 @@ public class JanusService extends JanusDelegate { ...@@ -81,6 +83,7 @@ public class JanusService extends JanusDelegate {
} }
public void stop() { public void stop() {
state = State.NORMAL_DISCONNECTED;
if (_janus != null) { if (_janus != null) {
this._janus.close(); this._janus.close();
} }
...@@ -90,9 +93,9 @@ public class JanusService extends JanusDelegate { ...@@ -90,9 +93,9 @@ public class JanusService extends JanusDelegate {
@Override @Override
public void onClose() { public void onClose() {
logger.info("close"); logger.info("close");
// if (audioManager != null) { if (audioManager != null) {
// audioManager.stop(); audioManager.setMode(AudioManager.MODE_NORMAL);
// } }
} }
@Override @Override
...@@ -124,6 +127,7 @@ public class JanusService extends JanusDelegate { ...@@ -124,6 +127,7 @@ public class JanusService extends JanusDelegate {
public void onDatachannelMessage(DataChannel.Buffer buffer) { public void onDatachannelMessage(DataChannel.Buffer buffer) {
} }
private State state = State.INIT; private State state = State.INIT;
@Override @Override
...@@ -131,10 +135,13 @@ public class JanusService extends JanusDelegate { ...@@ -131,10 +135,13 @@ public class JanusService extends JanusDelegate {
logger.info("onIceConnectionChange"); logger.info("onIceConnectionChange");
if (iceConnectionState == PeerConnection.IceConnectionState.DISCONNECTED) { if (iceConnectionState == PeerConnection.IceConnectionState.DISCONNECTED) {
stop(); stop();
state = State.DISCONNECTED; if (state != State.NORMAL_DISCONNECTED) {
callback.onError(ErrorCode.NET_ERROR); callback.onError(ErrorCode.NET_ERROR);
state = State.ERROR_DISCONNECTED;
}
callback.onExit();
new Handler(Looper.getMainLooper()).postDelayed(() -> { new Handler(Looper.getMainLooper()).postDelayed(() -> {
if (state != State.DISCONNECTED) { if (state != State.ERROR_DISCONNECTED && state != State.NORMAL_DISCONNECTED) {
return; return;
} }
_janus.dispatch(JanusCommands.FORCE_DESTROY, Bundle.create()); _janus.dispatch(JanusCommands.FORCE_DESTROY, Bundle.create());
...@@ -145,7 +152,8 @@ public class JanusService extends JanusDelegate { ...@@ -145,7 +152,8 @@ public class JanusService extends JanusDelegate {
private enum State { private enum State {
INIT, INIT,
STARTED, STARTED,
DISCONNECTED, NORMAL_DISCONNECTED,
ERROR_DISCONNECTED,
STOPPED STOPPED
} }
...@@ -162,16 +170,19 @@ public class JanusService extends JanusDelegate { ...@@ -162,16 +170,19 @@ public class JanusService extends JanusDelegate {
@Override @Override
public void onEvent(JanusEvent event, Bundle context) { public void onEvent(JanusEvent event, Bundle context) {
JanusData data = event.data(); JanusData data = event.data();
logger.info("onEvent " + data.getString("janus","")); logger.info("onEvent " + data.getString("janus", ""));
logger.info("onEvent " + data.getString("videoroom","")); logger.info("onEvent " + data.getString("videoroom", ""));
if (data.getString("janus", "").equals("error") && context.getString("command", "").equals(JanusCommands.JOIN)) { if (data.getString("janus", "").equals("error") && context.getString("command", "").equals(JanusCommands.JOIN)) {
logger.info("加入失败"); logger.info("加入失败");
if (callback!= null) { if (callback != null) {
callback.onJoinFailed(ErrorCode.JOIN_AUDIO_FAILED); callback.onJoinFailed(ErrorCode.JOIN_AUDIO_FAILED);
} }
return; return;
} }
if (data.getString("videoroom", "").equals("event") && data.getInt("error_code", -1) == 426) {
callback.onJoinFailed(ErrorCode.JOIN_AUDIO_FAILED_NO_SUCH_ROOM);
return;
}
if (data.getString("videoroom", "").equals("joined")) { if (data.getString("videoroom", "").equals("joined")) {
if (!isJoin) { if (!isJoin) {
isJoin = true; isJoin = true;
...@@ -181,25 +192,25 @@ public class JanusService extends JanusDelegate { ...@@ -181,25 +192,25 @@ public class JanusService extends JanusDelegate {
payload.setBool("data", false); payload.setBool("data", false);
this._janus.dispatch(JanusCommands.PUBLISH, payload); this._janus.dispatch(JanusCommands.PUBLISH, payload);
logger.info("加入成功"); logger.info("加入成功");
// List<JanusData> publishers = data.getList("publishers"); List<JanusData> publishers = data.getList("publishers");
// subscribePublishers(publishers); subscribePublishers(publishers);
// if (callback!= null) { if (callback != null) {
// callback.onJoinSuccess(); callback.onJoinSuccess();
// } }
} }
return; return;
} }
List<JanusData> publishers = data.getList("publishers"); List<JanusData> publishers = data.getList("publishers");
if(publishers.size() > 0) { if (publishers.size() > 0) {
subscribePublishers(publishers); subscribePublishers(publishers);
return; return;
} }
} }
private void subscribePublishers(List<JanusData> publishers) { private void subscribePublishers(List<JanusData> publishers) {
if(publishers.size() > 0) { if (publishers.size() > 0) {
for(JanusData publisher : publishers) { for (JanusData publisher : publishers) {
Bundle payload = Bundle.create(); Bundle payload = Bundle.create();
payload.setInt("room", roomId); payload.setInt("room", roomId);
payload.setInt("feed", publisher.getInt("id", -1)); payload.setInt("feed", publisher.getInt("id", -1));
...@@ -214,5 +225,4 @@ public class JanusService extends JanusDelegate { ...@@ -214,5 +225,4 @@ public class JanusService extends JanusDelegate {
} }
} }
...@@ -9,14 +9,14 @@ public class ErrorCode { ...@@ -9,14 +9,14 @@ public class ErrorCode {
public static final int VOICE_TOO_LONG = 904; //录音时长过长 public static final int VOICE_TOO_LONG = 904; //录音时长过长
public static final int ERROR_VOICE_PLAY = 905; //播放错误 public static final int ERROR_VOICE_PLAY = 905; //播放错误
public static final int MSG_TYPE_ERROR = 906; //播放类型错误 public static final int MSG_TYPE_ERROR = 906; //播放类型错误
public static final int DOWNLOAD_ERR = 907; //播放类型错误 public static final int DOWNLOAD_ERR = 907; //下载错误
public static final int MSG_NOT_EXISTS = 908; //播放类型错误 public static final int MSG_NOT_EXISTS = 908; //消息不存在
public static final int ERRORCODE_NOT_EXISTS = 909; //错误类型不存在 public static final int ERRORCODE_NOT_EXISTS = 909; //错误类型不存在
public static final int MSG_CONTENT_IS_EMPTY = 910; //发送消息体为空 public static final int MSG_CONTENT_IS_EMPTY = 910; //发送消息体为空
public static final int START_VOICE_FAILED = 911; //录音创建失败 public static final int START_VOICE_FAILED = 911; //录音创建失败
public static final int ERROR_PERMISSION_DENIE = 912; //权限拒绝 public static final int ERROR_PERMISSION_DENIE = 912; //权限拒绝
public static final int JOIN_AUDIO_FAILED = 913; //加入语音失败 public static final int JOIN_AUDIO_FAILED = 913; //加入语音失败
public static final int SYSTEM_ERROR = 914; //未知错误 public static final int JOIN_AUDIO_FAILED_NO_SUCH_ROOM = 914; //没有这个房间
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment