Search in sources :

Example 6 with Connection

use of com.flashphoner.fpwcsapi.bean.Connection 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 Connection

use of com.flashphoner.fpwcsapi.bean.Connection 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 Connection

use of com.flashphoner.fpwcsapi.bean.Connection in project wcs-android-sdk-samples by flashphoner.

the class VideoChatActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_conference);
    /**
     * Initialization of the API.
     */
    Flashphoner.init(this);
    mParticipantVolume = (SeekBar) findViewById(R.id.participant_volume);
    mParticipantVolume.setMax(Flashphoner.getMaxVolume());
    mParticipantVolume.setProgress(Flashphoner.getVolume());
    mParticipantVolume.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

        @Override
        public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
            Flashphoner.setVolume(i);
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
        }
    });
    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)));
    mLoginView = (EditText) findViewById(R.id.login);
    mLoginView.setText(sharedPref.getString("login", ""));
    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 connection options are set.
                 * WCS server URL and user name are passed when RoomManagerOptions object is created.
                 */
                RoomManagerOptions roomManagerOptions = new RoomManagerOptions(mWcsUrlView.getText().toString(), mLoginView.getText().toString());
                /**
                 * RoomManager object is created with method createRoomManager().
                 * Connection session is created when RoomManager object is created.
                 */
                roomManager = Flashphoner.createRoomManager(roomManagerOptions);
                /**
                 * Callback functions for connection status events are added to make appropriate changes in controls of the interface when connection is established and closed.
                 */
                roomManager.on(new RoomManagerEvent() {

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

                    @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);
                                mJoinButton.setText(R.string.action_join);
                                mJoinButton.setTag(R.string.action_join);
                                mJoinButton.setEnabled(false);
                                mPublishStatus.setText("");
                                mPublishButton.setText(R.string.action_publish);
                                mPublishButton.setTag(R.string.action_publish);
                                mPublishButton.setEnabled(false);
                                mMuteAudio.setEnabled(false);
                                mMuteAudio.setChecked(false);
                                mMuteVideo.setEnabled(false);
                                mMuteVideo.setChecked(false);
                                stream = null;
                                mConnectStatus.setText(connection.getStatus());
                                Iterator<Map.Entry<String, ParticipantView>> i = busyViews.entrySet().iterator();
                                while (i.hasNext()) {
                                    Map.Entry<String, ParticipantView> e = i.next();
                                    e.getValue().login.setText("NONE");
                                    e.getValue().surfaceViewRenderer.release();
                                    i.remove();
                                    freeViews.add(e.getValue());
                                }
                                mSendButton.setEnabled(false);
                            }
                        });
                    }
                });
                mConnectButton.setEnabled(false);
                SharedPreferences sharedPref = VideoChatActivity.this.getPreferences(Context.MODE_PRIVATE);
                SharedPreferences.Editor editor = sharedPref.edit();
                editor.putString("wcs_url", mWcsUrlView.getText().toString());
                editor.putString("login", mLoginView.getText().toString());
                editor.apply();
            } else {
                mConnectButton.setEnabled(false);
                /**
                 * Connection to WCS server is closed with method RoomManager.disconnect().
                 */
                roomManager.disconnect();
            }
            View currentFocus = getCurrentFocus();
            if (currentFocus != null) {
                InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                inputManager.hideSoftInputFromWindow(currentFocus.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
            }
        }
    });
    mJoinRoomView = (EditText) findViewById(R.id.join_room);
    mJoinRoomView.setText(sharedPref.getString("join_room", ""));
    mJoinStatus = (TextView) findViewById(R.id.join_status);
    mJoinButton = (Button) findViewById(R.id.join_button);
    /**
     * The participant will join to video chat room when Join button is clicked.
     */
    mJoinButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View view) {
            mJoinButton.setEnabled(false);
            if (mJoinButton.getTag() == null || Integer.valueOf(R.string.action_join).equals(mJoinButton.getTag())) {
                /**
                 * Room name is set with method RoomOptions.setName().
                 */
                RoomOptions roomOptions = new RoomOptions();
                roomOptions.setName(mJoinRoomView.getText().toString());
                /**
                 * The participant joins a video chat room with method RoomManager.join().
                 * RoomOptions object is passed to the method.
                 * Room object is created and returned by the method.
                 */
                room = roomManager.join(roomOptions);
                /**
                 * Callback functions for events occurring in video chat room are added.
                 * If the event is related to actions performed by one of the other participants, Participant object with data of that participant is passed to the corresponding function.
                 */
                room.on(new RoomEvent() {

                    @Override
                    public void onState(final Room room) {
                        /**
                         * After joining, Room object with data of the room is received.
                         * Method Room.getParticipants() is used to check the number of already connected participants.
                         * The method returns collection of Participant objects.
                         * The collection size is determined, and, if the maximum allowed number (in this case, three) has already been reached, the user leaves the room with method Room.leave().
                         */
                        if (room.getParticipants().size() >= 2) {
                            room.leave(null);
                            runOnUiThread(new Runnable() {

                                @Override
                                public void run() {
                                    mJoinStatus.setText("Room is full");
                                    mJoinButton.setEnabled(true);
                                }
                            });
                            return;
                        }
                        final StringBuffer chatState = new StringBuffer("participants: ");
                        /**
                         * Iterating through the collection of the other participants returned by method Room.getParticipants().
                         * There is corresponding Participant object for each participant.
                         */
                        for (final Participant participant : room.getParticipants()) {
                            /**
                             * A player view is assigned to each of the other participants in the room.
                             */
                            final ParticipantView participantView = freeViews.poll();
                            if (participantView != null) {
                                chatState.append(participant.getName()).append(",");
                                busyViews.put(participant.getName(), participantView);
                                /**
                                 * Playback of the stream being published by the other participant is started with method Participant.play().
                                 * SurfaceViewRenderer to be used to display the video stream is passed when the method is called.
                                 */
                                participant.play(participantView.surfaceViewRenderer);
                                runOnUiThread(new Runnable() {

                                    @Override
                                    public void run() {
                                        participantView.login.setText(participant.getName());
                                    }
                                });
                            }
                        }
                        runOnUiThread(new Runnable() {

                            @Override
                            public void run() {
                                mJoinButton.setText(R.string.action_leave);
                                mJoinButton.setTag(R.string.action_leave);
                                mJoinButton.setEnabled(true);
                                mJoinStatus.setText("");
                                mPublishButton.setEnabled(true);
                                mSendButton.setEnabled(true);
                                if (room.getParticipants().size() == 0) {
                                    addMessageHistory("chat", "room is empty");
                                } else {
                                    addMessageHistory("chat", chatState.substring(0, chatState.length() - 1));
                                }
                            }
                        });
                    }

                    @Override
                    public void onJoined(final Participant participant) {
                        /**
                         * When a new participant joins the room, a player view is assigned to that participant.
                         */
                        final ParticipantView participantView = freeViews.poll();
                        if (participantView != null) {
                            runOnUiThread(new Runnable() {

                                @Override
                                public void run() {
                                    participantView.login.setText(participant.getName());
                                    addMessageHistory(participant.getName(), "joined");
                                }
                            });
                            busyViews.put(participant.getName(), participantView);
                        }
                    }

                    @Override
                    public void onLeft(final Participant participant) {
                        /**
                         * When one of the other participants leaves the room, player view assigned to that participant is freed.
                         */
                        final ParticipantView participantView = busyViews.remove(participant.getName());
                        if (participantView != null) {
                            runOnUiThread(new Runnable() {

                                @Override
                                public void run() {
                                    participantView.login.setText("NONE");
                                    addMessageHistory(participant.getName(), "left");
                                    participantView.surfaceViewRenderer.release();
                                }
                            });
                            freeViews.add(participantView);
                        }
                    }

                    @Override
                    public void onPublished(final Participant participant) {
                        /**
                         * When one of the other participants starts publishing, playback of the stream published by that participant is started.
                         */
                        final ParticipantView participantView = busyViews.get(participant.getName());
                        if (participantView != null) {
                            participant.play(participantView.surfaceViewRenderer);
                        }
                    }

                    @Override
                    public void onFailed(Room room, final String info) {
                        room.leave(null);
                        runOnUiThread(new Runnable() {

                            @Override
                            public void run() {
                                mJoinStatus.setText(info);
                                mJoinButton.setEnabled(true);
                            }
                        });
                    }

                    @Override
                    public void onMessage(final Message message) {
                        /**
                         * When one of the participants sends a text message, the received message is added to the messages log.
                         */
                        runOnUiThread(new Runnable() {

                            @Override
                            public void run() {
                                addMessageHistory(message.getFrom(), message.getText());
                            }
                        });
                    }
                });
                SharedPreferences sharedPref = VideoChatActivity.this.getPreferences(Context.MODE_PRIVATE);
                SharedPreferences.Editor editor = sharedPref.edit();
                editor.putString("join_room", mJoinRoomView.getText().toString());
                editor.apply();
            } else {
                final Runnable action = new Runnable() {

                    @Override
                    public void run() {
                        mJoinButton.setEnabled(true);
                        mJoinButton.setText(R.string.action_join);
                        mJoinButton.setTag(R.string.action_join);
                        mPublishButton.setEnabled(false);
                        mSendButton.setEnabled(false);
                        /**
                         * The player views assigned to the other participants are freed.
                         */
                        Iterator<Map.Entry<String, ParticipantView>> i = busyViews.entrySet().iterator();
                        while (i.hasNext()) {
                            Map.Entry<String, ParticipantView> e = i.next();
                            e.getValue().login.setText("NONE");
                            e.getValue().surfaceViewRenderer.release();
                            i.remove();
                            freeViews.add(e.getValue());
                        }
                    }
                };
                /**
                 * The participant leaves the video chat room with method Room.leave().
                 */
                room.leave(new RestAppCommunicator.Handler() {

                    @Override
                    public void onAccepted(Data data) {
                        runOnUiThread(action);
                    }

                    @Override
                    public void onRejected(Data data) {
                        runOnUiThread(action);
                    }
                });
            }
            View currentFocus = getCurrentFocus();
            if (currentFocus != null) {
                InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                inputManager.hideSoftInputFromWindow(currentFocus.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
            }
        }
    });
    mPublishStatus = (TextView) findViewById(R.id.publish_status);
    mPublishButton = (Button) findViewById(R.id.publish_button);
    /**
     * The participant starts publishing video stream 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(VideoChatActivity.this, new String[] { Manifest.permission.RECORD_AUDIO, Manifest.permission.CAMERA }, PUBLISH_REQUEST_CODE);
            } else {
                mPublishButton.setEnabled(false);
                /**
                 * Stream is unpublished with method Room.unpublish().
                 */
                room.unpublish();
            }
            View currentFocus = getCurrentFocus();
            if (currentFocus != null) {
                InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                inputManager.hideSoftInputFromWindow(currentFocus.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
            }
        }
    });
    /**
     * MuteAudio switch is used to mute/unmute audio of the published stream.
     * Audio is muted with method Stream.muteAudio() and unmuted with method Stream.unmuteAudio().
     */
    mMuteAudio = (Switch) findViewById(R.id.mute_audio);
    mMuteAudio.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked) {
                stream.muteAudio();
            } else {
                stream.unmuteAudio();
            }
        }
    });
    /**
     * MuteVideo switch is used to mute/unmute video of the published stream.
     * Video is muted with method Stream.muteVideo() and unmuted with method Stream.unmuteVideo().
     */
    mMuteVideo = (Switch) findViewById(R.id.mute_video);
    mMuteVideo.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked) {
                stream.muteVideo();
            } else {
                stream.unmuteVideo();
            }
        }
    });
    localRenderer = (SurfaceViewRenderer) findViewById(R.id.local_video_view);
    PercentFrameLayout localRenderLayout = (PercentFrameLayout) findViewById(R.id.local_video_layout);
    localRenderLayout.setPosition(0, 0, 100, 100);
    localRenderer.setScalingType(RendererCommon.ScalingType.SCALE_ASPECT_FIT);
    localRenderer.setMirror(true);
    localRenderer.requestLayout();
    SurfaceViewRenderer remote1Render = (SurfaceViewRenderer) findViewById(R.id.remote_video_view);
    PercentFrameLayout remote1RenderLayout = (PercentFrameLayout) findViewById(R.id.remote_video_layout);
    remote1RenderLayout.setPosition(0, 0, 100, 100);
    remote1Render.setScalingType(RendererCommon.ScalingType.SCALE_ASPECT_FIT);
    remote1Render.setMirror(false);
    remote1Render.requestLayout();
    mParticipantName = (TextView) findViewById(R.id.participant_name);
    freeViews.add(new ParticipantView(remote1Render, mParticipantName));
    mMessageHistory = (TextView) findViewById(R.id.message_history);
    mMessageHistory.setMovementMethod(new ScrollingMovementMethod());
    mMessage = (EditText) findViewById(R.id.message);
    mSendButton = (Button) findViewById(R.id.send_button);
    mSendButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View view) {
            String text = mMessage.getText().toString();
            if (!"".equals(text)) {
                for (Participant participant : room.getParticipants()) {
                    participant.sendMessage(text);
                }
                addMessageHistory(mLoginView.getText().toString(), text);
                mMessage.setText("");
            }
        }
    });
}
Also used : Message(com.flashphoner.fpwcsapi.room.Message) PercentFrameLayout(com.flashphoner.fpwcsapi.layout.PercentFrameLayout) RoomOptions(com.flashphoner.fpwcsapi.room.RoomOptions) InputMethodManager(android.view.inputmethod.InputMethodManager) SurfaceViewRenderer(org.webrtc.SurfaceViewRenderer) RoomManagerEvent(com.flashphoner.fpwcsapi.room.RoomManagerEvent) Iterator(java.util.Iterator) Room(com.flashphoner.fpwcsapi.room.Room) SeekBar(android.widget.SeekBar) SharedPreferences(android.content.SharedPreferences) Connection(com.flashphoner.fpwcsapi.bean.Connection) Data(com.flashphoner.fpwcsapi.bean.Data) View(android.view.View) TextView(android.widget.TextView) ScrollingMovementMethod(android.text.method.ScrollingMovementMethod) Participant(com.flashphoner.fpwcsapi.room.Participant) OnClickListener(android.view.View.OnClickListener) RoomManagerOptions(com.flashphoner.fpwcsapi.room.RoomManagerOptions) RoomEvent(com.flashphoner.fpwcsapi.room.RoomEvent) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) CompoundButton(android.widget.CompoundButton)

Example 9 with Connection

use of com.flashphoner.fpwcsapi.bean.Connection in project wcs-android-sdk-samples by flashphoner.

the class ConferenceActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_conference);
    /**
     * Initialization of the API.
     */
    Flashphoner.init(this);
    mParticipantVolume = (SeekBar) findViewById(R.id.participant_volume);
    mParticipantVolume.setMax(Flashphoner.getMaxVolume());
    mParticipantVolume.setProgress(Flashphoner.getVolume());
    mParticipantVolume.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

        @Override
        public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
            Flashphoner.setVolume(i);
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
        }
    });
    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)));
    mLoginView = (EditText) findViewById(R.id.login);
    mLoginView.setText(sharedPref.getString("login", ""));
    mConnectStatus = (TextView) findViewById(R.id.connect_status);
    mConnectButton = (Button) findViewById(R.id.connect_button);
    mRecord = (CheckBox) findViewById(R.id.record);
    /**
     * 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 connection options are set.
                 * WCS server URL and user name are passed when RoomManagerOptions object is created.
                 */
                RoomManagerOptions roomManagerOptions = new RoomManagerOptions(mWcsUrlView.getText().toString(), mLoginView.getText().toString());
                /**
                 * RoomManager object is created with method createRoomManager().
                 * Connection session is created when RoomManager object is created.
                 */
                roomManager = Flashphoner.createRoomManager(roomManagerOptions);
                /**
                 * Callback functions for connection status events are added to make appropriate changes in controls of the interface when connection is established and closed.
                 */
                roomManager.on(new RoomManagerEvent() {

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

                    @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);
                                mJoinButton.setText(R.string.action_join);
                                mJoinButton.setTag(R.string.action_join);
                                mJoinButton.setEnabled(false);
                                mPublishStatus.setText("");
                                mPublishButton.setText(R.string.action_publish);
                                mPublishButton.setTag(R.string.action_publish);
                                mPublishButton.setEnabled(false);
                                mMuteAudio.setEnabled(false);
                                mMuteAudio.setChecked(false);
                                mMuteVideo.setEnabled(false);
                                mMuteVideo.setChecked(false);
                                stream = null;
                                mConnectStatus.setText(connection.getStatus());
                                Iterator<Map.Entry<String, ParticipantView>> i = busyViews.entrySet().iterator();
                                while (i.hasNext()) {
                                    Map.Entry<String, ParticipantView> e = i.next();
                                    e.getValue().login.setText("NONE");
                                    e.getValue().surfaceViewRenderer.release();
                                    i.remove();
                                    freeViews.add(e.getValue());
                                }
                                mSendButton.setEnabled(false);
                            }
                        });
                    }
                });
                mConnectButton.setEnabled(false);
                SharedPreferences sharedPref = ConferenceActivity.this.getPreferences(Context.MODE_PRIVATE);
                SharedPreferences.Editor editor = sharedPref.edit();
                editor.putString("wcs_url", mWcsUrlView.getText().toString());
                editor.putString("login", mLoginView.getText().toString());
                editor.apply();
            } else {
                mConnectButton.setEnabled(false);
                /**
                 * Connection to WCS server is closed with method RoomManager.disconnect().
                 */
                roomManager.disconnect();
            }
            View currentFocus = getCurrentFocus();
            if (currentFocus != null) {
                InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                inputManager.hideSoftInputFromWindow(currentFocus.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
            }
        }
    });
    mJoinRoomView = (EditText) findViewById(R.id.join_room);
    mJoinRoomView.setText(sharedPref.getString("join_room", ""));
    mJoinStatus = (TextView) findViewById(R.id.join_status);
    mJoinButton = (Button) findViewById(R.id.join_button);
    /**
     * The participant will join to conference room when Join button is clicked.
     */
    mJoinButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View view) {
            mJoinButton.setEnabled(false);
            if (mJoinButton.getTag() == null || Integer.valueOf(R.string.action_join).equals(mJoinButton.getTag())) {
                /**
                 * Room name is set with method RoomOptions.setName().
                 */
                RoomOptions roomOptions = new RoomOptions();
                roomOptions.setName(mJoinRoomView.getText().toString());
                /**
                 * The participant joins a conference room with method RoomManager.join().
                 * RoomOptions object is passed to the method.
                 * Room object is created and returned by the method.
                 */
                room = roomManager.join(roomOptions);
                /**
                 * Callback functions for events occurring in conference room are added.
                 * If the event is related to actions performed by one of the other participants, Participant object with data of that participant is passed to the corresponding function.
                 */
                room.on(new RoomEvent() {

                    @Override
                    public void onState(final Room room) {
                        /**
                         * After joining, Room object with data of the room is received.
                         * Method Room.getParticipants() is used to check the number of already connected participants.
                         * The method returns collection of Participant objects.
                         * The collection size is determined, and, if the maximum allowed number (in this case, three) has already been reached, the user leaves the room with method Room.leave().
                         */
                        if (room.getParticipants().size() >= 3) {
                            room.leave(null);
                            runOnUiThread(new Runnable() {

                                @Override
                                public void run() {
                                    mJoinStatus.setText("Room is full");
                                    mJoinButton.setEnabled(true);
                                }
                            });
                            return;
                        }
                        final StringBuffer chatState = new StringBuffer("participants: ");
                        /**
                         * Iterating through the collection of the other participants returned by method Room.getParticipants().
                         * There is corresponding Participant object for each participant.
                         */
                        for (final Participant participant : room.getParticipants()) {
                            /**
                             * A player view is assigned to each of the other participants in the room.
                             */
                            final ParticipantView participantView = freeViews.poll();
                            if (participantView != null) {
                                chatState.append(participant.getName()).append(",");
                                busyViews.put(participant.getName(), participantView);
                                /**
                                 * Playback of the stream being published by the other participant is started with method Participant.play().
                                 * SurfaceViewRenderer to be used to display the video stream is passed when the method is called.
                                 */
                                participant.play(participantView.surfaceViewRenderer);
                                runOnUiThread(new Runnable() {

                                    @Override
                                    public void run() {
                                        participantView.login.setText(participant.getName());
                                    }
                                });
                            }
                        }
                        runOnUiThread(new Runnable() {

                            @Override
                            public void run() {
                                mJoinButton.setText(R.string.action_leave);
                                mJoinButton.setTag(R.string.action_leave);
                                mJoinButton.setEnabled(true);
                                mJoinStatus.setText("");
                                mPublishButton.setEnabled(true);
                                mSendButton.setEnabled(true);
                                if (room.getParticipants().size() == 0) {
                                    addMessageHistory("chat", "room is empty");
                                } else {
                                    addMessageHistory("chat", chatState.substring(0, chatState.length() - 1));
                                }
                            }
                        });
                    }

                    @Override
                    public void onJoined(final Participant participant) {
                        /**
                         * When a new participant joins the room, a player view is assigned to that participant.
                         */
                        final ParticipantView participantView = freeViews.poll();
                        if (participantView != null) {
                            runOnUiThread(new Runnable() {

                                @Override
                                public void run() {
                                    participantView.login.setText(participant.getName());
                                    addMessageHistory(participant.getName(), "joined");
                                }
                            });
                            busyViews.put(participant.getName(), participantView);
                        }
                    }

                    @Override
                    public void onLeft(final Participant participant) {
                        /**
                         * When one of the other participants leaves the room, player view assigned to that participant is freed.
                         */
                        final ParticipantView participantView = busyViews.remove(participant.getName());
                        if (participantView != null) {
                            runOnUiThread(new Runnable() {

                                @Override
                                public void run() {
                                    participantView.login.setText("NONE");
                                    addMessageHistory(participant.getName(), "left");
                                    participantView.surfaceViewRenderer.release();
                                }
                            });
                            freeViews.add(participantView);
                        }
                    }

                    @Override
                    public void onPublished(final Participant participant) {
                        /**
                         * When one of the other participants starts publishing, playback of the stream published by that participant is started.
                         */
                        final ParticipantView participantView = busyViews.get(participant.getName());
                        if (participantView != null) {
                            participant.play(participantView.surfaceViewRenderer);
                        }
                    }

                    @Override
                    public void onFailed(Room room, final String info) {
                        room.leave(null);
                        runOnUiThread(new Runnable() {

                            @Override
                            public void run() {
                                mJoinStatus.setText(info);
                                mJoinButton.setEnabled(true);
                            }
                        });
                    }

                    @Override
                    public void onMessage(final Message message) {
                        /**
                         * When one of the participants sends a text message, the received message is added to the messages log.
                         */
                        runOnUiThread(new Runnable() {

                            @Override
                            public void run() {
                                addMessageHistory(message.getFrom(), message.getText());
                            }
                        });
                    }
                });
                SharedPreferences sharedPref = ConferenceActivity.this.getPreferences(Context.MODE_PRIVATE);
                SharedPreferences.Editor editor = sharedPref.edit();
                editor.putString("join_room", mJoinRoomView.getText().toString());
                editor.apply();
            } else {
                final Runnable action = new Runnable() {

                    @Override
                    public void run() {
                        mJoinButton.setEnabled(true);
                        mJoinButton.setText(R.string.action_join);
                        mJoinButton.setTag(R.string.action_join);
                        mPublishButton.setEnabled(false);
                        mSendButton.setEnabled(false);
                        /**
                         * The player views assigned to the other participants are freed.
                         */
                        Iterator<Map.Entry<String, ParticipantView>> i = busyViews.entrySet().iterator();
                        while (i.hasNext()) {
                            Map.Entry<String, ParticipantView> e = i.next();
                            e.getValue().login.setText("NONE");
                            e.getValue().surfaceViewRenderer.release();
                            i.remove();
                            freeViews.add(e.getValue());
                        }
                    }
                };
                /**
                 * The participant leaves the conference room with method Room.leave().
                 */
                room.leave(new RestAppCommunicator.Handler() {

                    @Override
                    public void onAccepted(Data data) {
                        runOnUiThread(action);
                    }

                    @Override
                    public void onRejected(Data data) {
                        runOnUiThread(action);
                    }
                });
            }
            View currentFocus = getCurrentFocus();
            if (currentFocus != null) {
                InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                inputManager.hideSoftInputFromWindow(currentFocus.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
            }
        }
    });
    mPublishStatus = (TextView) findViewById(R.id.publish_status);
    mPublishButton = (Button) findViewById(R.id.publish_button);
    /**
     * The participant starts publishing video stream 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(ConferenceActivity.this, new String[] { Manifest.permission.RECORD_AUDIO, Manifest.permission.CAMERA }, PUBLISH_REQUEST_CODE);
            } else {
                mPublishButton.setEnabled(false);
                /**
                 * Stream is unpublished with method Room.unpublish().
                 */
                room.unpublish();
            }
            View currentFocus = getCurrentFocus();
            if (currentFocus != null) {
                InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                inputManager.hideSoftInputFromWindow(currentFocus.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
            }
        }
    });
    /**
     * MuteAudio switch is used to mute/unmute audio of the published stream.
     * Audio is muted with method Stream.muteAudio() and unmuted with method Stream.unmuteAudio().
     */
    mMuteAudio = (Switch) findViewById(R.id.mute_audio);
    mMuteAudio.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked) {
                stream.muteAudio();
            } else {
                stream.unmuteAudio();
            }
        }
    });
    /**
     * MuteVideo switch is used to mute/unmute video of the published stream.
     * Video is muted with method Stream.muteVideo() and unmuted with method Stream.unmuteVideo().
     */
    mMuteVideo = (Switch) findViewById(R.id.mute_video);
    mMuteVideo.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked) {
                stream.muteVideo();
            } else {
                stream.unmuteVideo();
            }
        }
    });
    localRenderer = (SurfaceViewRenderer) findViewById(R.id.local_video_view);
    PercentFrameLayout localRenderLayout = (PercentFrameLayout) findViewById(R.id.local_video_layout);
    localRenderLayout.setPosition(0, 0, 100, 100);
    localRenderer.setScalingType(RendererCommon.ScalingType.SCALE_ASPECT_FIT);
    localRenderer.setMirror(true);
    localRenderer.requestLayout();
    SurfaceViewRenderer remote1Render = (SurfaceViewRenderer) findViewById(R.id.remote1_video_view);
    PercentFrameLayout remote1RenderLayout = (PercentFrameLayout) findViewById(R.id.remove1_video_layout);
    remote1RenderLayout.setPosition(0, 0, 100, 100);
    remote1Render.setScalingType(RendererCommon.ScalingType.SCALE_ASPECT_FIT);
    remote1Render.setMirror(false);
    remote1Render.requestLayout();
    SurfaceViewRenderer remote2Render = (SurfaceViewRenderer) findViewById(R.id.remote2_video_view);
    PercentFrameLayout remote2RenderLayout = (PercentFrameLayout) findViewById(R.id.remote2_video_layout);
    remote2RenderLayout.setPosition(0, 0, 100, 100);
    remote2Render.setScalingType(RendererCommon.ScalingType.SCALE_ASPECT_FIT);
    remote2Render.setMirror(false);
    remote2Render.requestLayout();
    mParticipant1Name = (TextView) findViewById(R.id.participant1_name);
    freeViews.add(new ParticipantView(remote1Render, mParticipant1Name));
    mParticipant2Name = (TextView) findViewById(R.id.participant2_name);
    freeViews.add(new ParticipantView(remote2Render, mParticipant2Name));
    mMessageHistory = (TextView) findViewById(R.id.message_history);
    mMessageHistory.setMovementMethod(new ScrollingMovementMethod());
    mMessage = (EditText) findViewById(R.id.message);
    mSendButton = (Button) findViewById(R.id.send_button);
    mSendButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View view) {
            String text = mMessage.getText().toString();
            if (!"".equals(text)) {
                for (Participant participant : room.getParticipants()) {
                    participant.sendMessage(text);
                }
                addMessageHistory(mLoginView.getText().toString(), text);
                mMessage.setText("");
            }
        }
    });
}
Also used : Message(com.flashphoner.fpwcsapi.room.Message) PercentFrameLayout(com.flashphoner.fpwcsapi.layout.PercentFrameLayout) RoomOptions(com.flashphoner.fpwcsapi.room.RoomOptions) InputMethodManager(android.view.inputmethod.InputMethodManager) SurfaceViewRenderer(org.webrtc.SurfaceViewRenderer) RoomManagerEvent(com.flashphoner.fpwcsapi.room.RoomManagerEvent) Iterator(java.util.Iterator) Room(com.flashphoner.fpwcsapi.room.Room) SeekBar(android.widget.SeekBar) SharedPreferences(android.content.SharedPreferences) Connection(com.flashphoner.fpwcsapi.bean.Connection) Data(com.flashphoner.fpwcsapi.bean.Data) View(android.view.View) TextView(android.widget.TextView) ScrollingMovementMethod(android.text.method.ScrollingMovementMethod) Participant(com.flashphoner.fpwcsapi.room.Participant) OnClickListener(android.view.View.OnClickListener) RoomManagerOptions(com.flashphoner.fpwcsapi.room.RoomManagerOptions) RoomEvent(com.flashphoner.fpwcsapi.room.RoomEvent) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) CompoundButton(android.widget.CompoundButton)

Example 10 with Connection

use of com.flashphoner.fpwcsapi.bean.Connection 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)

Aggregations

SharedPreferences (android.content.SharedPreferences)11 View (android.view.View)11 OnClickListener (android.view.View.OnClickListener)11 InputMethodManager (android.view.inputmethod.InputMethodManager)11 TextView (android.widget.TextView)11 Connection (com.flashphoner.fpwcsapi.bean.Connection)11 Data (com.flashphoner.fpwcsapi.bean.Data)11 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)5 SeekBar (android.widget.SeekBar)3 PercentFrameLayout (com.flashphoner.fpwcsapi.layout.PercentFrameLayout)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