@Override
 public void setSocketTimeout(int timeout) {
   try {
     mSocket.setSoTimeout(timeout);
   } catch (IOException e) {
     Util.throwIfNot(mSocket.isClosed());
   }
 }
Ejemplo n.º 2
0
  public void testAccessors() throws IOException {
    LocalSocket socket = new LocalSocket();
    LocalSocketAddress addr = new LocalSocketAddress("secondary");

    assertFalse(socket.isBound());
    socket.bind(addr);
    assertTrue(socket.isBound());
    assertEquals(addr, socket.getLocalSocketAddress());

    String str = socket.toString();
    assertTrue(str.contains("impl:android.net.LocalSocketImpl"));

    socket.setReceiveBufferSize(1999);
    assertEquals(1999 << 1, socket.getReceiveBufferSize());

    socket.setSendBufferSize(3998);
    assertEquals(3998 << 1, socket.getSendBufferSize());

    // Timeout is not support at present, so set is ignored
    socket.setSoTimeout(1996);
    assertEquals(0, socket.getSoTimeout());

    try {
      socket.getRemoteSocketAddress();
      fail("testLocalSocketSecondary shouldn't come to here");
    } catch (UnsupportedOperationException e) {
      // expected
    }

    try {
      socket.isClosed();
      fail("testLocalSocketSecondary shouldn't come to here");
    } catch (UnsupportedOperationException e) {
      // expected
    }

    try {
      socket.isInputShutdown();
      fail("testLocalSocketSecondary shouldn't come to here");
    } catch (UnsupportedOperationException e) {
      // expected
    }

    try {
      socket.isOutputShutdown();
      fail("testLocalSocketSecondary shouldn't come to here");
    } catch (UnsupportedOperationException e) {
      // expected
    }

    try {
      socket.connect(addr, 2005);
      fail("testLocalSocketSecondary shouldn't come to here");
    } catch (UnsupportedOperationException e) {
      // expected
    }
  }
Ejemplo n.º 3
0
  /**
   * Constructs instance from connected socket.
   *
   * @param socket non-null; connected socket
   * @throws IOException
   */
  ZygoteConnection(LocalSocket socket) throws IOException {
    mSocket = socket;

    mSocketOutStream = new DataOutputStream(socket.getOutputStream());

    mSocketReader = new BufferedReader(new InputStreamReader(socket.getInputStream()), 256);

    mSocket.setSoTimeout(CONNECTION_TIMEOUT_MILLIS);

    try {
      peer = mSocket.getPeerCredentials();
    } catch (IOException ex) {
      Log.e(TAG, "Cannot read peer credentials", ex);
      throw ex;
    }
  }
  @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;
          }
        }
      }
    }
  }