Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Flashphoner.init() код

Code Block
languagejsjava
themeRDark
Flashphoner.init(this);

...

  • URL WCS-сервера
  • SurfaceViewRenderer localRenderer, который будет использоваться для отображения видео с камеры
  • SurfaceViewRenderer remoteRenderer, который будет использоваться для отображения воспроизводимого потока
Code Block
languagejsjava
themeRDark
sessionOptions = new SessionOptions(mWcsUrlView.getText().toString());
sessionOptions.setLocalRenderer(localRender);
sessionOptions.setRemoteRenderer(remoteRender);

/**
  * Uncomment this code to use your own RTCConfiguration. For example, you can use custom TURN server
  */
//List<PeerConnection.IceServer> iceServers = new ArrayList<>();
//iceServers.add(new PeerConnection.IceServer("turn:your.turn-server.com:443?transport=tcp","username","passw0rd"));
//PeerConnection.RTCConfiguration customConfig = new PeerConnection.RTCConfiguration(iceServers);
//sessionOptions.setMediaOptions(customConfig);

/**
  * Session for connection to WCS server is created with method createSession().
  */
session = Flashphoner.createSession(sessionOptions);

...

Session.connect(). код

Code Block
languagejsjava
themeRDark
session.connect(new Connection());

...

session.onConnected() код

Code Block
languagejsjava
themeRDark
@Override
public void onConnected(final Connection connection) {
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            mConnectButton.setText(R.string.action_disconnect);
            mConnectButton.setTag(R.string.action_disconnect);
            mConnectButton.setEnabled(true);
            mConnectStatus.setText(connection.getStatus());
            mPublishButton.setEnabled(true);
            mPlayButton.setEnabled(true);
        }
    });

...

ActivityCompat.requestPermissions() код

Code Block
languagejsjava
themeRDark
mPublishButton.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View view) {
        if (mPublishButton.getTag() == null || Integer.valueOf(R.string.action_publish).equals(mPublishButton.getTag())) {
            ActivityCompat.requestPermissions(StreamingMinActivity.this,
                  new String[]{Manifest.permission.RECORD_AUDIO, Manifest.permission.CAMERA},
                  PUBLISH_REQUEST_CODE);
            ...
        } else {
            mPublishButton.setEnabled(false);
            /**
              * Method Stream.stop() is called to unpublish the stream.
              */
            publishStream.stop();
            publishStream = null;
        }
        View currentFocus = getCurrentFocus();
        if (currentFocus != null) {
            InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            inputManager.hideSoftInputFromWindow(currentFocus.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }
});


6. Публикация потока после предоставления соответствующих прав

Session.createStream(), Stream.publish() код

Code Block
languagejsjava
themeRDark
case PUBLISH_REQUEST_CODE: {
    if (grantResults.length == 0 ||
           grantResults[0] != PackageManager.PERMISSION_GRANTED ||
           grantResults[1] != PackageManager.PERMISSION_GRANTED) {
        Log.i(TAG, "Permission has been denied by user");
    } else {
        mPublishButton.setEnabled(false);
        /**
          * The options for the stream to publish are set.
          * The stream name is passed when StreamOptions object is created.
          */
        StreamOptions streamOptions = new StreamOptions(mPublishStreamView.getText().toString());

        /**
          * Uncomment this code to use case WebRTC-as-RTMP. Stream will be republished to your rtmpUrl
          */
        //streamOptions.setRtmpUrl("rtmp://192.168.1.100:1935/live2");

        /**
          * Stream is created with method Session.createStream().
          */
        publishStream = session.createStream(streamOptions);
        ...
        /**
          * Method Stream.publish() is called to publish stream.
          */
        publishStream.publish();

        Log.i(TAG, "Permission has been granted by user");
    }
}

...

Session.createStream(), Stream.publishplay() код

Code Block
languagejsjava
themeRDark
mPlayButton.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View view) {
        mPlayButton.setEnabled(false);
        if (mPlayButton.getTag() == null || Integer.valueOf(R.string.action_play).equals(mPlayButton.getTag())) {
            /**
              * The options for the stream to play are set.
              * The stream name is passed when StreamOptions object is created.
              */
            StreamOptions streamOptions = new StreamOptions(mPlayStreamView.getText().toString());

            /**
              * Stream is created with method Session.createStream().
              */
            playStream = session.createStream(streamOptions);
            ...
            /**
              * Method Stream.play() is called to start playback of the stream.
              */
            playStream.play();
            ...
        } else {
            /**
              * Method Stream.stop() is called to stop playback of the stream.
              */
            playStream.stop();
            playStream = null;
        }
        View currentFocus = getCurrentFocus();
        if (currentFocus != null) {
            InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            inputManager.hideSoftInputFromWindow(currentFocus.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }
});


8. Остановка воспроизведение потока при нажатии Stop

Stream.stop() код

Code Block
languagejsjava
themeRDark
playStream.stop();
playStream = null;

...

Stream.stop() код

Code Block
languagejsjava
themeRDark
publishStream.stop();
publishStream = null;

...

Session.disconnect() код

Code Block
languagejsjava
themeRDark
mConnectButton.setEnabled(false);

/**
  * Connection to WCS server is closed with method Session.disconnect().
  */
session.disconnect();

...

session.onDisconnection() код

Code Block
languagejsjava
themeRDark
@Override
public void onDisconnection(final Connection connection) {
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            mConnectButton.setText(R.string.action_connect);
            mConnectButton.setTag(R.string.action_connect);
            mConnectButton.setEnabled(true);
            mPublishButton.setText(R.string.action_publish);
            mPublishButton.setTag(R.string.action_publish);
            mPublishButton.setEnabled(false);
            mPlayButton.setText(R.string.action_play);
            mPlayButton.setTag(R.string.action_play);
            mPlayButton.setEnabled(false);
            mConnectStatus.setText(connection.getStatus());
            mPublishStatus.setText("");
            mPlayStatus.setText("");
        }
    });
}