@Override protected void startRecording() { super.startRecording(); // create and execute audio capturing thread using internal mic if (mAudioThread == null) { mAudioThread = new AudioThread(); mAudioThread.start(); } }
@Override public void onMessage(final DataChannel.Buffer inbuf) { ByteBuffer data = inbuf.data; if (inbuf.binary) { if (bStart) { try { if (writableByteChannel != null) { writableByteChannel.write(data); } else { Log.e(TAG, "writeableByteChannel is null"); } } catch (Exception e) { Log.d(TAG, "os write fail:" + e); } } } else { int size = data.remaining(); byte[] bytes = new byte[size]; data.get(bytes); String command = new String(bytes); String[] cmd_split = command.split(":"); String msgType = cmd_split[0]; String msgKey = cmd_split[1]; if (isAudioMessage(msgType)) { String audioMsg = "Get Audio: "; Log.d(TAG, audioMsg + command); if (msgKey.equalsIgnoreCase("SET")) { if (cmd_split[2].equalsIgnoreCase("OV")) { sourceType = SourceType.OV; setting = ""; } else if (cmd_split[2].equalsIgnoreCase("RTSP")) { sourceType = SourceType.RTSP; // collect parameter after SET: // ex: 1. AUDIO:SET:RTSP:AAC:1:8000:1588:..... // ex: 2. AUDIO:SET:RTSP:mG711:1:.... // ex: 3. AUDIO:SET:RTSP:aG711:1:.... setting = command.substring(10, command.length()); Log.d(TAG, setting); } } if (msgKey.equalsIgnoreCase("START")) { if (sourceType == SourceType.UNKNOWN) { Log.e(TAG, "Audio Something wrong!!"); return; } audioMsg = audioMsg + "START"; if (at != null) { at.setStop(); at.interrupt(); at = null; } if (nat != null) { nat.setStop(); nat.interrupt(); nat = null; } for (int jj = 0; jj < 10; jj++) { try { mSocketId = new Random().nextInt(); mLss = new LocalServerSocket(LOCAL_ADDR + mSocketId); break; } catch (IOException e) { Log.e(TAG, "fail to create localserversocket :" + e); } } // DECODE FLOW // // Intermediary: Localsocket MediaCodec inputBuffer // MediaCodec outputBuffer // Flow : Data Channel =======> Sender ========> Receiver ==================> // Decoder =================> Display to surface/ Play by Audio Track // Thread : |<---Data Channel thread--->| |<--------- Decode Thread // --------->| |<--------- Display/play Thread -------->| // mReceiver = new LocalSocket(); try { mReceiver.connect(new LocalSocketAddress(LOCAL_ADDR + mSocketId)); mReceiver.setReceiveBufferSize(100000); mReceiver.setSoTimeout(200); mSender = mLss.accept(); mSender.setSendBufferSize(100000); } catch (IOException e) { Log.e(TAG, "fail to create mSender mReceiver :" + e); e.printStackTrace(); } try { os = mSender.getOutputStream(); writableByteChannel = Channels.newChannel(os); is = mReceiver.getInputStream(); } catch (IOException e) { Log.e(TAG, "fail to get mSender mReceiver :" + e); e.printStackTrace(); } bStart = true; if (sourceType == SourceType.OV) { // using self adpcm_ima decoder to decode audio at = new AudioThread(is); at.start(); } else if (sourceType == SourceType.RTSP) { // using mediaCodec to decode audio nat = new NormalAudioThread(is, setting); nat.start(); } } if (msgKey.equalsIgnoreCase("STOP")) { if (bStart) { audioMsg = audioMsg + "STOP"; try { os.close(); is.close(); writableByteChannel.close(); writableByteChannel = null; os = null; is = null; } catch (Exception e) { Log.e(TAG, "Close input/output stream error : " + e); } if (at != null) { at.setStop(); at.interrupt(); at = null; } if (nat != null) { nat.setStop(); nat.interrupt(); nat = null; } bStart = false; } } } } }