Versions Compared

Key

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

...

Flashphoner.init() код

Code Block
languagejs
themeRDark
Flashphoner.init(this);


При инициализации методу init() передается объект Сontext.

...

  • URL WCS-сервера
  • SurfaceViewRenderer localRenderer, который будет использоваться для отображения видео с камеры
  • SurfaceViewRenderer remoteRenderer, который будет использоваться для отображения воспроизводимого потока
Code Block
languagejs
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
languagejs
themeRDark
session.connect(new Connection());

...

session.onConnected() код

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


5. Запрос прав на публикацию потока при нажатии Publish

ActivityCompat.requestPermissions() код

Code Block
languagejs
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},
                  new String[]{Manifest.permission.RECORD_AUDIO, Manifest.permission.CAMERA},
                  PUBLISH_REQUEST_CODE);

            SharedPreferences sharedPref = StreamingMinActivity.this.getPreferences(Context.MODE_PRIVATE);
SharedPreferences            SharedPreferences.Editor editor = sharedPref.edit();
            editor.putString("publish_stream", mPublishStreamView.getText().toString());
            editor.apply();
        } else {
            mPublishButton.setEnabled(false);
/**
* Method            /**
              * Method Stream.stop() is called to unpublish the stream.
              */
            publishStream.stop();
publishStream  = null;
}
          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
languagejs
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    * 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);

        /**
          * Callback function for stream status change is added to make appropriate changes in controls of the interface when publishing.
          */
        publishStream.on(new StreamStatusEvent() {
@Override
public   void onStreamStatus(final Stream stream, final StreamStatus streamStatus) {
runOnUiThread(new Runnable() {
@Override
            public void run(onStreamStatus(final Stream stream, final StreamStatus streamStatus) {
if (StreamStatus.PUBLISHING.equals(streamStatus)) {
mPublishButton                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (StreamStatus.PUBLISHING.equals(streamStatus)) {
                            mPublishButton.setText(R.string.action_unpublish);
                            mPublishButton.setTag(R.string.action_unpublish);
                        } else {
                            mPublishButton.setText(R.string.action_publish);
                            mPublishButton.setTag(R.string.action_publish);
                        }
                        mPublishButton.setEnabled(true);
                        mPublishStatus.setText(streamStatus.toString());
                    }
                });
            }
        });

        /**
          * Method Stream.publish() is called to publish stream.
          */
        publishStream.publish();

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


7. Воспроизведение потока при нажатии Play

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

Code Block
languagejs
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);

            /**
              * Callback function for stream status change is added to make appropriate changes in controls of the interface when playing.
              */
            playStream.on(new StreamStatusEvent() {
                @Override
                public void onStreamStatus(final Stream stream, final StreamStatus streamStatus) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {

                            if (StreamStatus.PLAYING.equals(streamStatus)) {
                                mPlayButton.setText(R.string.action_stop);
                                mPlayButton.setTag(R.string.action_stop);
                            } else if (StreamStatus.NOT_ENOUGH_BANDWIDTH.equals(streamStatus)) {
                                Log.w(TAG, "Not enough bandwidth stream " + stream.getName() + ", consider using lower video resolution or bitrate. " +
                                   "Bandwidth " + (Math.round(stream.getNetworkBandwidth() / 1000)) + " " +
                                   "bitrate " + (Math.round(stream.getRemoteBitrate() / 1000)));
                            } else {
                                mPlayButton.setText(R.string.action_unpublish);
mPublishButtonplay);
                                mPlayButton.setTag(R.string.action_unpublishplay);
} else {
mPublishButton.setText(R.string.action_publish);
mPublishButton.setTag(R.string.action_publish);
}
mPublishButton                            }
                            mPlayButton.setEnabled(true);
mPublishStatus                            mPlayStatus.setText(streamStatus.toString());
}
});
}
                        }
                    });
                }
            });

            /**
              * Method Stream.publishplay() is called to start playback publish stream.
*/
publishStream.publish();

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

...

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

Code Block
languagejs
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);

/**
* Callback function for stream status change is added to make appropriate changes in controls of the interface when playing.
*/
playStream.on(new StreamStatusEvent() {
@Override
public void onStreamStatus(final Stream stream, final StreamStatus streamStatus) {
runOnUiThread(new Runnable() {
@Override
public void run() {

if (StreamStatus.PLAYING.equals(streamStatus)) {
mPlayButton.setText(R.string.action_stop);
mPlayButton.setTag(R.string.action_stop);
} else if (StreamStatus.NOT_ENOUGH_BANDWIDTH.equals(streamStatus)) {
Log.w(TAG, "Not enough bandwidth stream " + stream.getName() + ", consider using lower video resolution or bitrate. " +
"Bandwidth " + (Math.round(stream.getNetworkBandwidth() / 1000)) + " " +
"bitrate " + (Math.round(stream.getRemoteBitrate() / 1000)));
} else {
mPlayButton.setText(R.string.action_play);
mPlayButton.setTag(R.string.action_play);
}
mPlayButton.setEnabled(true);
mPlayStatus.setText(streamStatus.toString());
}
});
}
});

/**
* Method Stream.play() is called to start playback of the stream.
*/
playStream.play();

SharedPreferences sharedPref = StreamingMinActivity.this.getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("play_stream", mPlayStreamView.getText().toString());
editor.apply();
} 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

...

of the stream.
              */
            playStream.play();

            SharedPreferences sharedPref = StreamingMinActivity.this.getPreferences(Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = sharedPref.edit();
            editor.putString("play_stream", mPlayStreamView.getText().toString());
            editor.apply();
        } 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
languagejs
themeRDark
playStream.stop();
playStream = null;


9. Остановка публикации потока при нажатии Unpublish

Stream.stop() код

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


10. Закрытие соединения.

Session.disconnect() код

Code Block
languagejs
themeRDark
mConnectButton.setEnabled(false);

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


11. Получение события, подтверждающего разъединение.

session.onDisconnection() код

Code Block
languagejs
playStream.stop();
playStream = null;

9. Остановка публикации потока при нажатии Unpublish

Stream.stop() код

Code Block
languagejs
publishStream.stop();
publishStream = null;

10. Закрытие соединения.

Session.disconnect() код

Code Block
languagejs
mConnectButton.setEnabled(false);

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

11. Получение события, подтверждающего разъединение.

session.onDisconnection() код

@Override public void onDisconnection(final Connection connection) { runOnUiThread(new Runnable() { @Override public void run() { mConnectButton
Code Block
languagejs
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_connectplay);
mConnectButton            mPlayButton.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("");
}
});
}play);
            mPlayButton.setEnabled(false);
            mConnectStatus.setText(connection.getStatus());
            mPublishStatus.setText("");
            mPlayStatus.setText("");
        }
    });
}


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.