Search in sources :

Example 6 with SessionOptions

use of com.flashphoner.fpwcsapi.session.SessionOptions in project wcs-android-sdk-samples by flashphoner.

the class PlayerActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_player);
    /**
     * Initialization of the API.
     */
    Flashphoner.init(this);
    mWcsUrlView = (EditText) findViewById(R.id.wcs_url);
    SharedPreferences sharedPref = this.getPreferences(Context.MODE_PRIVATE);
    mWcsUrlView.setText(sharedPref.getString("wcs_url", getString(R.string.wcs_url)));
    mPlayStreamView = (EditText) findViewById(R.id.play_stream);
    mPlayStreamView.setText(sharedPref.getString("play_stream", getString(R.string.default_play_stream)));
    mStatusView = (TextView) findViewById(R.id.status);
    mStartButton = (Button) findViewById(R.id.start_button);
    /**
     * Connection to server will be established and stream playback will be started when Start button is clicked.
     */
    mStartButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View view) {
            if (mStartButton.getTag() == null || Integer.valueOf(R.string.action_start).equals(mStartButton.getTag())) {
                /**
                 * The options for connection session are set.
                 * WCS server URL is passed when SessionOptions object is created.
                 * SurfaceViewRenderer to be used to display the video stream is set with method SessionOptions.setRemoteRenderer().
                 */
                SessionOptions sessionOptions = new SessionOptions(mWcsUrlView.getText().toString());
                sessionOptions.setRemoteRenderer(remoteRender);
                /**
                 * Session for connection to WCS server is created with method createSession().
                 */
                session = Flashphoner.createSession(sessionOptions);
                /**
                 * Callback functions for session status events are added to make appropriate changes in controls of the interface and play stream when connection is established.
                 */
                session.on(new SessionEvent() {

                    @Override
                    public void onAppData(Data data) {
                    }

                    @Override
                    public void onConnected(final Connection connection) {
                        runOnUiThread(new Runnable() {

                            @Override
                            public void run() {
                                mStartButton.setText(R.string.action_stop);
                                mStartButton.setTag(R.string.action_stop);
                                mStartButton.setEnabled(true);
                                mStatusView.setText(connection.getStatus());
                                /**
                                 * 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 display the status.
                                 */
                                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)) {
                                                    Log.e(TAG, "Can not play stream " + stream.getName() + " " + streamStatus);
                                                    mStatusView.setText(streamStatus.toString());
                                                } 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 {
                                                    mStatusView.setText(streamStatus.toString());
                                                }
                                            }
                                        });
                                    }
                                });
                                /*
                                     * Method Stream.play() is called to start playback of the stream.
                                     */
                                playStream.play();
                                SharedPreferences sharedPref = PlayerActivity.this.getPreferences(Context.MODE_PRIVATE);
                                SharedPreferences.Editor editor = sharedPref.edit();
                                editor.putString("play_stream", mPlayStreamView.getText().toString());
                                editor.apply();
                            }
                        });
                    }

                    @Override
                    public void onRegistered(Connection connection) {
                    }

                    @Override
                    public void onDisconnection(final Connection connection) {
                        runOnUiThread(new Runnable() {

                            @Override
                            public void run() {
                                mStartButton.setText(R.string.action_start);
                                mStartButton.setTag(R.string.action_start);
                                mStartButton.setEnabled(true);
                                mStatusView.setText(connection.getStatus());
                            }
                        });
                    }
                });
                mStartButton.setEnabled(false);
                /**
                 * Connection to WCS server is established with method Session.connect().
                 */
                session.connect(new Connection());
                SharedPreferences sharedPref = PlayerActivity.this.getPreferences(Context.MODE_PRIVATE);
                SharedPreferences.Editor editor = sharedPref.edit();
                editor.putString("wcs_url", mWcsUrlView.getText().toString());
                editor.apply();
            } else {
                mStartButton.setEnabled(false);
                /**
                 * Connection to WCS server is closed with method Session.disconnect().
                 */
                session.disconnect();
            }
            View currentFocus = getCurrentFocus();
            if (currentFocus != null) {
                InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                inputManager.hideSoftInputFromWindow(currentFocus.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
            }
        }
    });
    remoteRender = (SurfaceViewRenderer) findViewById(R.id.remote_video_view);
    remoteRenderLayout = (PercentFrameLayout) findViewById(R.id.remote_video_layout);
    remoteRenderLayout.setPosition(0, 0, 100, 100);
    remoteRender.setScalingType(RendererCommon.ScalingType.SCALE_ASPECT_FIT);
    remoteRender.setMirror(false);
    remoteRender.requestLayout();
}
Also used : SharedPreferences(android.content.SharedPreferences) StreamOptions(com.flashphoner.fpwcsapi.session.StreamOptions) SessionOptions(com.flashphoner.fpwcsapi.session.SessionOptions) Connection(com.flashphoner.fpwcsapi.bean.Connection) Data(com.flashphoner.fpwcsapi.bean.Data) InputMethodManager(android.view.inputmethod.InputMethodManager) StreamStatus(com.flashphoner.fpwcsapi.bean.StreamStatus) TextView(android.widget.TextView) View(android.view.View) StreamStatusEvent(com.flashphoner.fpwcsapi.session.StreamStatusEvent) SessionEvent(com.flashphoner.fpwcsapi.session.SessionEvent) OnClickListener(android.view.View.OnClickListener) Stream(com.flashphoner.fpwcsapi.session.Stream)

Example 7 with SessionOptions

use of com.flashphoner.fpwcsapi.session.SessionOptions in project wcs-android-sdk-samples by flashphoner.

the class StreamerActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_streamer);
    /**
     * Initialization of the API.
     */
    Flashphoner.init(this);
    mWcsUrlView = (EditText) findViewById(R.id.wcs_url);
    SharedPreferences sharedPref = this.getPreferences(Context.MODE_PRIVATE);
    mWcsUrlView.setText(sharedPref.getString("wcs_url", getString(R.string.wcs_url)));
    mStatusView = (TextView) findViewById(R.id.status);
    mStartButton = (Button) findViewById(R.id.connect_button);
    /**
     * Connection to server will be established and stream will be published when Start button is clicked.
     */
    mStartButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View view) {
            if (mStartButton.getTag() == null || Integer.valueOf(R.string.action_start).equals(mStartButton.getTag())) {
                String url;
                final String streamName;
                try {
                    URI u = new URI(mWcsUrlView.getText().toString());
                    url = u.getScheme() + "://" + u.getHost() + ":" + u.getPort();
                    streamName = u.getPath().replaceAll("/", "");
                } catch (URISyntaxException e) {
                    mStatusView.setText("Wrong uri");
                    return;
                }
                /**
                 * The options for connection session are set.
                 * WCS server URL is passed when SessionOptions object is created.
                 * SurfaceViewRenderer to be used to display video from the camera is set with method SessionOptions.setLocalRenderer().
                 * SurfaceViewRenderer to be used to display preview stream video received from the server is set with method SessionOptions.setRemoteRenderer().
                 */
                SessionOptions sessionOptions = new SessionOptions(url);
                sessionOptions.setLocalRenderer(localRender);
                sessionOptions.setRemoteRenderer(remoteRender);
                /**
                 * Session for connection to WCS server is created with method createSession().
                 */
                session = Flashphoner.createSession(sessionOptions);
                /**
                 * Callback functions for session status events are added to make appropriate changes in controls of the interface and publish stream when connection is established.
                 */
                session.on(new SessionEvent() {

                    @Override
                    public void onAppData(Data data) {
                    }

                    @Override
                    public void onConnected(final Connection connection) {
                        runOnUiThread(new Runnable() {

                            @Override
                            public void run() {
                                mStartButton.setText(R.string.action_stop);
                                mStartButton.setTag(R.string.action_stop);
                                mStartButton.setEnabled(true);
                                mStatusView.setText(connection.getStatus());
                                /**
                                 * The options for the stream to publish are set.
                                 * The stream name is passed when StreamOptions object is created.
                                 */
                                StreamOptions streamOptions = new StreamOptions(streamName);
                                /**
                                 * Stream is created with method Session.createStream().
                                 */
                                publishStream = session.createStream(streamOptions);
                                /**
                                 * Callback function for stream status change is added to play the stream when it is published.
                                 */
                                publishStream.on(new StreamStatusEvent() {

                                    @Override
                                    public void onStreamStatus(final Stream stream, final StreamStatus streamStatus) {
                                        runOnUiThread(new Runnable() {

                                            @Override
                                            public void run() {
                                                if (StreamStatus.PUBLISHING.equals(streamStatus)) {
                                                    /**
                                                     * The options for the stream to play are set.
                                                     * The stream name is passed when StreamOptions object is created.
                                                     */
                                                    StreamOptions streamOptions = new StreamOptions(streamName);
                                                    /**
                                                     * Stream is created with method Session.createStream().
                                                     */
                                                    playStream = session.createStream(streamOptions);
                                                    /**
                                                     * Callback function for stream status change is added to display the status.
                                                     */
                                                    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)) {
                                                                        Log.e(TAG, "Can not play stream " + stream.getName() + " " + streamStatus);
                                                                    }
                                                                    mStatusView.setText(streamStatus.toString());
                                                                }
                                                            });
                                                        }
                                                    });
                                                    /**
                                                     * Method Stream.play() is called to start playback of the stream.
                                                     */
                                                    playStream.play();
                                                } else {
                                                    Log.e(TAG, "Can not publish stream " + stream.getName() + " " + streamStatus);
                                                }
                                                mStatusView.setText(streamStatus.toString());
                                            }
                                        });
                                    }
                                });
                                ActivityCompat.requestPermissions(StreamerActivity.this, new String[] { Manifest.permission.RECORD_AUDIO, Manifest.permission.CAMERA }, PUBLISH_REQUEST_CODE);
                            }
                        });
                    }

                    @Override
                    public void onRegistered(Connection connection) {
                    }

                    @Override
                    public void onDisconnection(final Connection connection) {
                        runOnUiThread(new Runnable() {

                            @Override
                            public void run() {
                                mStartButton.setText(R.string.action_start);
                                mStartButton.setTag(R.string.action_start);
                                mStartButton.setEnabled(true);
                                mStatusView.setText(connection.getStatus());
                            }
                        });
                    }
                });
                mStartButton.setEnabled(false);
                /**
                 * Connection to WCS server is established with method Session.connect().
                 */
                session.connect(new Connection());
                SharedPreferences sharedPref = StreamerActivity.this.getPreferences(Context.MODE_PRIVATE);
                SharedPreferences.Editor editor = sharedPref.edit();
                editor.putString("wcs_url", mWcsUrlView.getText().toString());
                editor.apply();
            } else {
                mStartButton.setEnabled(false);
                /**
                 * Connection to WCS server is closed with method Session.disconnect().
                 */
                session.disconnect();
            }
            View currentFocus = getCurrentFocus();
            if (currentFocus != null) {
                InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                inputManager.hideSoftInputFromWindow(currentFocus.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
            }
        }
    });
    localRender = (SurfaceViewRenderer) findViewById(R.id.local_video_view);
    remoteRender = (SurfaceViewRenderer) findViewById(R.id.remote_video_view);
    localRenderLayout = (PercentFrameLayout) findViewById(R.id.local_video_layout);
    remoteRenderLayout = (PercentFrameLayout) findViewById(R.id.remote_video_layout);
    localRender.setZOrderMediaOverlay(true);
    remoteRenderLayout.setPosition(0, 0, 100, 100);
    remoteRender.setScalingType(RendererCommon.ScalingType.SCALE_ASPECT_FIT);
    remoteRender.setMirror(false);
    remoteRender.requestLayout();
    localRenderLayout.setPosition(0, 0, 100, 100);
    localRender.setScalingType(RendererCommon.ScalingType.SCALE_ASPECT_FIT);
    localRender.setMirror(true);
    localRender.requestLayout();
}
Also used : SharedPreferences(android.content.SharedPreferences) StreamOptions(com.flashphoner.fpwcsapi.session.StreamOptions) SessionOptions(com.flashphoner.fpwcsapi.session.SessionOptions) Connection(com.flashphoner.fpwcsapi.bean.Connection) Data(com.flashphoner.fpwcsapi.bean.Data) InputMethodManager(android.view.inputmethod.InputMethodManager) URISyntaxException(java.net.URISyntaxException) StreamStatus(com.flashphoner.fpwcsapi.bean.StreamStatus) View(android.view.View) TextView(android.widget.TextView) URI(java.net.URI) StreamStatusEvent(com.flashphoner.fpwcsapi.session.StreamStatusEvent) SessionEvent(com.flashphoner.fpwcsapi.session.SessionEvent) OnClickListener(android.view.View.OnClickListener) Stream(com.flashphoner.fpwcsapi.session.Stream)

Example 8 with SessionOptions

use of com.flashphoner.fpwcsapi.session.SessionOptions in project wcs-android-sdk-samples by flashphoner.

the class StreamingMinActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_streaming_min);
    /**
     * Initialization of the API.
     */
    Flashphoner.init(this);
    mWcsUrlView = (EditText) findViewById(R.id.wcs_url);
    SharedPreferences sharedPref = this.getPreferences(Context.MODE_PRIVATE);
    mWcsUrlView.setText(sharedPref.getString("wcs_url", getString(R.string.wcs_url)));
    mConnectStatus = (TextView) findViewById(R.id.connect_status);
    mConnectButton = (Button) findViewById(R.id.connect_button);
    /**
     * Connection to server will be established when Connect button is clicked.
     */
    mConnectButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View view) {
            if (mConnectButton.getTag() == null || Integer.valueOf(R.string.action_connect).equals(mConnectButton.getTag())) {
                /**
                 * The options for connection session are set.
                 * WCS server URL is passed when SessionOptions object is created.
                 * SurfaceViewRenderer to be used to display video from the camera is set with method SessionOptions.setLocalRenderer().
                 * SurfaceViewRenderer to be used to display video of the played stream is set with method SessionOptions.setRemoteRenderer().
                 */
                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);
                /**
                 * Callback functions for session status events are added to make appropriate changes in controls of the interface when connection is established and closed.
                 */
                session.on(new SessionEvent() {

                    @Override
                    public void onAppData(Data data) {
                    }

                    @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);
                            }
                        });
                    }

                    @Override
                    public void onRegistered(Connection connection) {
                    }

                    @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("");
                            }
                        });
                    }
                });
                mConnectButton.setEnabled(false);
                /**
                 * Connection to WCS server is established with method Session.connect().
                 */
                session.connect(new Connection());
                SharedPreferences sharedPref = StreamingMinActivity.this.getPreferences(Context.MODE_PRIVATE);
                SharedPreferences.Editor editor = sharedPref.edit();
                editor.putString("wcs_url", mWcsUrlView.getText().toString());
                editor.apply();
            } else {
                mConnectButton.setEnabled(false);
                /**
                 * Connection to WCS server is closed with method Session.disconnect().
                 */
                session.disconnect();
            }
            View currentFocus = getCurrentFocus();
            if (currentFocus != null) {
                InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                inputManager.hideSoftInputFromWindow(currentFocus.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
            }
        }
    });
    mPublishStreamView = (EditText) findViewById(R.id.publish_stream);
    mPublishStreamView.setText(sharedPref.getString("publish_stream", getString(R.string.default_publish_name)));
    mPublishStatus = (TextView) findViewById(R.id.publish_status);
    mPublishButton = (Button) findViewById(R.id.publish_button);
    /**
     * Stream will be published when Publish button is clicked.
     */
    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);
                SharedPreferences sharedPref = StreamingMinActivity.this.getPreferences(Context.MODE_PRIVATE);
                SharedPreferences.Editor editor = sharedPref.edit();
                editor.putString("publish_stream", mPublishStreamView.getText().toString());
                editor.apply();
            } 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);
            }
        }
    });
    mPlayStreamView = (EditText) findViewById(R.id.play_stream);
    mPlayStreamView.setText(sharedPref.getString("play_stream", getString(R.string.default_play_name)));
    mPlayStatus = (TextView) findViewById(R.id.play_status);
    mPlayButton = (Button) findViewById(R.id.play_button);
    /**
     * Stream playback will be started when Play button is clicked.
     */
    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);
            }
        }
    });
    localRender = (SurfaceViewRenderer) findViewById(R.id.local_video_view);
    remoteRender = (SurfaceViewRenderer) findViewById(R.id.remote_video_view);
    localRenderLayout = (PercentFrameLayout) findViewById(R.id.local_video_layout);
    remoteRenderLayout = (PercentFrameLayout) findViewById(R.id.remote_video_layout);
    localRender.setZOrderMediaOverlay(true);
    remoteRenderLayout.setPosition(0, 0, 100, 100);
    remoteRender.setScalingType(RendererCommon.ScalingType.SCALE_ASPECT_FIT);
    remoteRender.setMirror(false);
    remoteRender.requestLayout();
    localRenderLayout.setPosition(0, 0, 100, 100);
    localRender.setScalingType(RendererCommon.ScalingType.SCALE_ASPECT_FIT);
    localRender.setMirror(true);
    localRender.requestLayout();
}
Also used : SharedPreferences(android.content.SharedPreferences) StreamOptions(com.flashphoner.fpwcsapi.session.StreamOptions) SessionOptions(com.flashphoner.fpwcsapi.session.SessionOptions) Connection(com.flashphoner.fpwcsapi.bean.Connection) PeerConnection(org.webrtc.PeerConnection) Data(com.flashphoner.fpwcsapi.bean.Data) InputMethodManager(android.view.inputmethod.InputMethodManager) StreamStatus(com.flashphoner.fpwcsapi.bean.StreamStatus) View(android.view.View) TextView(android.widget.TextView) StreamStatusEvent(com.flashphoner.fpwcsapi.session.StreamStatusEvent) SessionEvent(com.flashphoner.fpwcsapi.session.SessionEvent) OnClickListener(android.view.View.OnClickListener) Stream(com.flashphoner.fpwcsapi.session.Stream)

Example 9 with SessionOptions

use of com.flashphoner.fpwcsapi.session.SessionOptions in project wcs-android-sdk-samples by flashphoner.

the class TwoPlayersActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_2players);
    /**
     * Initialization of the API.
     */
    Flashphoner.init(this);
    mWcsUrlView = (EditText) findViewById(R.id.wcs_url);
    SharedPreferences sharedPref = this.getPreferences(Context.MODE_PRIVATE);
    mWcsUrlView.setText(sharedPref.getString("wcs_url", getString(R.string.wcs_url)));
    mConnectStatus = (TextView) findViewById(R.id.connect_status);
    mConnectButton = (Button) findViewById(R.id.connect_button);
    /**
     * Connection to server will be established when Connect button is clicked.
     */
    mConnectButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View view) {
            if (mConnectButton.getTag() == null || Integer.valueOf(R.string.action_connect).equals(mConnectButton.getTag())) {
                /**
                 * The options for connection session are set.
                 * WCS server URL is passed when SessionOptions object is created.
                 */
                SessionOptions sessionOptions = new SessionOptions(mWcsUrlView.getText().toString());
                sessionOptions.setRemoteRenderer(remote2Render);
                /**
                 * Session for connection to WCS server is created with method createSession().
                 */
                session = Flashphoner.createSession(sessionOptions);
                /**
                 * Callback functions for session status events are added to make appropriate changes in controls of the interface when connection is established and closed.
                 */
                session.on(new SessionEvent() {

                    @Override
                    public void onAppData(Data data) {
                    }

                    @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());
                                mPlay1Button.setEnabled(true);
                                mPlay2Button.setEnabled(true);
                            }
                        });
                    }

                    @Override
                    public void onRegistered(Connection connection) {
                    }

                    @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);
                                mPlay1Button.setText(R.string.action_play);
                                mPlay1Button.setTag(R.string.action_play);
                                mPlay1Button.setEnabled(false);
                                mPlay2Button.setText(R.string.action_play);
                                mPlay2Button.setTag(R.string.action_play);
                                mPlay2Button.setEnabled(false);
                                mConnectStatus.setText(connection.getStatus());
                                mPlay1Status.setText("");
                                mPlay2Status.setText("");
                            }
                        });
                    }
                });
                mConnectButton.setEnabled(false);
                /**
                 * Connection to WCS server is established with method Session.connect().
                 */
                session.connect(new Connection());
                SharedPreferences sharedPref = TwoPlayersActivity.this.getPreferences(Context.MODE_PRIVATE);
                SharedPreferences.Editor editor = sharedPref.edit();
                editor.putString("wcs_url", mWcsUrlView.getText().toString());
                editor.apply();
            } else {
                mConnectButton.setEnabled(false);
                /**
                 * Connection to WCS server is closed with method Session.disconnect().
                 */
                session.disconnect();
            }
            View currentFocus = getCurrentFocus();
            if (currentFocus != null) {
                InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                inputManager.hideSoftInputFromWindow(currentFocus.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
            }
        }
    });
    mPlay1StreamView = (EditText) findViewById(R.id.play1_stream);
    mPlay1StreamView.setText(sharedPref.getString("play1_stream", getString(R.string.default_play1_name)));
    mPlay1Status = (TextView) findViewById(R.id.play1_status);
    mPlay1Button = (Button) findViewById(R.id.play1_button);
    /**
     * Playback in the left player will be started when the left Play button is clicked.
     */
    mPlay1Button.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View view) {
            mPlay1Button.setEnabled(false);
            if (mPlay1Button.getTag() == null || Integer.valueOf(R.string.action_play).equals(mPlay1Button.getTag())) {
                /**
                 * The options for the stream to play are set.
                 * The stream name is passed when StreamOptions object is created.
                 * SurfaceViewRenderer to be used to display the video stream is set using method StreamOptions.setRenderer().
                 */
                StreamOptions streamOptions = new StreamOptions(mPlay1StreamView.getText().toString());
                streamOptions.setRenderer(remote1Render);
                /**
                 * Stream is created with method Session.createStream().
                 */
                play1Stream = session.createStream(streamOptions);
                /**
                 * Callback function for stream status change is added to make appropriate changes in controls of the interface when stream is being played.
                 */
                play1Stream.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)) {
                                    mPlay1Button.setText(R.string.action_stop);
                                    mPlay1Button.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 {
                                    mPlay1Button.setText(R.string.action_play);
                                    mPlay1Button.setTag(R.string.action_play);
                                }
                                mPlay1Button.setEnabled(true);
                                mPlay1Status.setText(streamStatus.toString());
                            }
                        });
                    }
                });
                /**
                 * Method Stream.play() is called to start playback of the stream.
                 */
                play1Stream.play();
                SharedPreferences sharedPref = TwoPlayersActivity.this.getPreferences(Context.MODE_PRIVATE);
                SharedPreferences.Editor editor = sharedPref.edit();
                editor.putString("play1_stream", mPlay1StreamView.getText().toString());
                editor.apply();
            } else {
                /**
                 * Method Stream.stop() is called to stop playback of the stream.
                 */
                play1Stream.stop();
                play1Stream = null;
            }
            View currentFocus = getCurrentFocus();
            if (currentFocus != null) {
                InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                inputManager.hideSoftInputFromWindow(currentFocus.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
            }
        }
    });
    mPlay2StreamView = (EditText) findViewById(R.id.play2_stream);
    mPlay2StreamView.setText(sharedPref.getString("play2_stream", getString(R.string.default_play2_name)));
    mPlay2Status = (TextView) findViewById(R.id.play2_status);
    mPlay2Button = (Button) findViewById(R.id.play2_button);
    /**
     * Playback in the right player will be started when the right Play button is clicked.
     */
    mPlay2Button.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View view) {
            mPlay2Button.setEnabled(false);
            if (mPlay2Button.getTag() == null || Integer.valueOf(R.string.action_play).equals(mPlay2Button.getTag())) {
                /**
                 * The options for the stream to play are set.
                 * The stream name is passed when StreamOptions object is created.
                 * SurfaceViewRenderer to be used to display the video stream is set using method StreamOptions.setRenderer().
                 */
                StreamOptions streamOptions = new StreamOptions(mPlay2StreamView.getText().toString());
                streamOptions.setRenderer(remote2Render);
                /**
                 * Stream is created with method Session.createStream().
                 */
                play2Stream = session.createStream(streamOptions);
                /**
                 * Callback function for stream status change is added to make appropriate changes in controls of the interface when stream is being played.
                 */
                play2Stream.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)) {
                                    mPlay2Button.setText(R.string.action_stop);
                                    mPlay2Button.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 {
                                    mPlay2Button.setText(R.string.action_play);
                                    mPlay2Button.setTag(R.string.action_play);
                                }
                                mPlay2Button.setEnabled(true);
                                mPlay2Status.setText(streamStatus.toString());
                            }
                        });
                    }
                });
                /**
                 * Method Stream.play() is called to start playback of the stream.
                 */
                play2Stream.play();
                SharedPreferences sharedPref = TwoPlayersActivity.this.getPreferences(Context.MODE_PRIVATE);
                SharedPreferences.Editor editor = sharedPref.edit();
                editor.putString("play2_stream", mPlay2StreamView.getText().toString());
                editor.apply();
            } else {
                /**
                 * Method Stream.stop() is called to stop playback of the stream.
                 */
                play2Stream.stop();
                play2Stream = null;
            }
            View currentFocus = getCurrentFocus();
            if (currentFocus != null) {
                InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                inputManager.hideSoftInputFromWindow(currentFocus.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
            }
        }
    });
    remote1Render = (SurfaceViewRenderer) findViewById(R.id.remote1_video_view);
    remote2Render = (SurfaceViewRenderer) findViewById(R.id.remote2_video_view);
    remote1RenderLayout = (PercentFrameLayout) findViewById(R.id.remove1_video_layout);
    remote2RenderLayout = (PercentFrameLayout) findViewById(R.id.remote2_video_layout);
    remote1Render.setZOrderMediaOverlay(true);
    remote2RenderLayout.setPosition(0, 0, 100, 100);
    remote2Render.setScalingType(RendererCommon.ScalingType.SCALE_ASPECT_FIT);
    remote2Render.setMirror(false);
    remote2Render.requestLayout();
    remote1RenderLayout.setPosition(0, 0, 100, 100);
    remote1Render.setScalingType(RendererCommon.ScalingType.SCALE_ASPECT_FIT);
    remote1Render.setMirror(false);
    remote1Render.requestLayout();
}
Also used : SharedPreferences(android.content.SharedPreferences) StreamOptions(com.flashphoner.fpwcsapi.session.StreamOptions) SessionOptions(com.flashphoner.fpwcsapi.session.SessionOptions) Connection(com.flashphoner.fpwcsapi.bean.Connection) Data(com.flashphoner.fpwcsapi.bean.Data) InputMethodManager(android.view.inputmethod.InputMethodManager) StreamStatus(com.flashphoner.fpwcsapi.bean.StreamStatus) TextView(android.widget.TextView) View(android.view.View) StreamStatusEvent(com.flashphoner.fpwcsapi.session.StreamStatusEvent) SessionEvent(com.flashphoner.fpwcsapi.session.SessionEvent) OnClickListener(android.view.View.OnClickListener) Stream(com.flashphoner.fpwcsapi.session.Stream)

Aggregations

SharedPreferences (android.content.SharedPreferences)9 View (android.view.View)9 OnClickListener (android.view.View.OnClickListener)9 InputMethodManager (android.view.inputmethod.InputMethodManager)9 TextView (android.widget.TextView)9 Connection (com.flashphoner.fpwcsapi.bean.Connection)9 Data (com.flashphoner.fpwcsapi.bean.Data)9 SessionEvent (com.flashphoner.fpwcsapi.session.SessionEvent)9 SessionOptions (com.flashphoner.fpwcsapi.session.SessionOptions)9 StreamStatus (com.flashphoner.fpwcsapi.bean.StreamStatus)6 Stream (com.flashphoner.fpwcsapi.session.Stream)6 StreamOptions (com.flashphoner.fpwcsapi.session.StreamOptions)6 StreamStatusEvent (com.flashphoner.fpwcsapi.session.StreamStatusEvent)6 CompoundButton (android.widget.CompoundButton)3 Call (com.flashphoner.fpwcsapi.session.Call)3 CallStatusEvent (com.flashphoner.fpwcsapi.session.CallStatusEvent)3 URI (java.net.URI)3 URISyntaxException (java.net.URISyntaxException)3 DialogInterface (android.content.DialogInterface)2 AlertDialog (android.support.v7.app.AlertDialog)2