Search in sources :

Example 1 with Connection

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

the class MediaDevicesActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_media_devices);
    /**
     * 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);
    mSendAudio = (CheckBox) findViewById(R.id.send_audio);
    /**
     * Method getMediaDevices(), which returns MediaDeviceList object, is used to request list of all available media devices.
     * Then methods MediaDeviceList.getAudioList() and MediaDeviceList.getVideoList() are used to list available microphones and cameras.
     */
    mMicSpinner = (LabelledSpinner) findViewById(R.id.microphone);
    mMicSpinner.setItemsArray(Flashphoner.getMediaDevices().getAudioList());
    mMicLevel = (TextView) findViewById(R.id.microphone_level);
    mCameraSpinner = (LabelledSpinner) findViewById(R.id.camera);
    mCameraSpinner.setItemsArray(Flashphoner.getMediaDevices().getVideoList());
    mStripStreamerCodec = (LabelledSpinner) findViewById(R.id.strip_streamer_codec);
    mStripStreamerCodec.setItemsArray(new String[] { "", "H264", "VP8" });
    mStripPlayerCodec = (LabelledSpinner) findViewById(R.id.strip_player_codec);
    mStripPlayerCodec.setItemsArray(new String[] { "", "H264", "VP8" });
    mCameraFPS = (EditText) findViewById(R.id.camera_fps);
    mWidth = (EditText) findViewById(R.id.camera_width);
    mHeight = (EditText) findViewById(R.id.camera_height);
    mDefaultPublishVideoBitrate = (CheckBox) findViewById(R.id.publish_video_bitrate_default);
    mDefaultPublishVideoBitrate.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
            mPublishVideoBitrate.setEnabled(!b);
        }
    });
    mDefaultPublishAudioBitrate = (CheckBox) findViewById(R.id.publish_audio_bitrate_default);
    mDefaultPublishAudioBitrate.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
            mPublishAudioBitrate.setEnabled(!b);
        }
    });
    mPublishVideoBitrate = (EditText) findViewById(R.id.publish_video_bitrate);
    mPublishAudioBitrate = (EditText) findViewById(R.id.publish_audio_bitrate);
    mSendVideo = (CheckBox) findViewById(R.id.send_video);
    mUseStereo = (CheckBox) findViewById(R.id.use_stereo);
    mUseFEC = (CheckBox) findViewById(R.id.use_fec);
    mReceiveAudio = (CheckBox) findViewById(R.id.receive_audio);
    mPlayVolume = (SeekBar) findViewById(R.id.play_volume);
    mPlayVolume.setMax(Flashphoner.getMaxVolume());
    mPlayVolume.setProgress(Flashphoner.getVolume());
    mPlayVolume.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) {
        }
    });
    mReceiveVideo = (CheckBox) findViewById(R.id.receive_video);
    mDefaultPlayResolution = (CheckBox) findViewById(R.id.play_resolution_default);
    mDefaultPlayResolution.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
            mPlayWidth.setEnabled(!b);
            mPlayHeight.setEnabled(!b);
        }
    });
    mPlayWidth = (EditText) findViewById(R.id.play_width);
    mPlayHeight = (EditText) findViewById(R.id.play_height);
    mDefaultPlayBitrate = (CheckBox) findViewById(R.id.play_bitrate_default);
    mDefaultPlayBitrate.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
            mPlayBitrate.setEnabled(!b);
        }
    });
    mPlayBitrate = (EditText) findViewById(R.id.play_bitrate);
    mDefaultPlayQuality = (CheckBox) findViewById(R.id.play_quality_default);
    mDefaultPlayQuality.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
            mPlayQuality.setEnabled(!b);
        }
    });
    mPlayQuality = (EditText) findViewById(R.id.play_quality);
    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;
                }
                try {
                    localRender.init(null, new RendererCommon.RendererEvents() {

                        @Override
                        public void onFirstFrameRendered() {
                        }

                        @Override
                        public void onFrameResolutionChanged(final int i, final int i1, int i2) {
                            runOnUiThread(new Runnable() {

                                @Override
                                public void run() {
                                    mLocalResolutionView.setText(i + "x" + i1);
                                }
                            });
                        }
                    });
                } catch (IllegalStateException e) {
                // ignore
                }
                try {
                    remoteRender.init(null, new RendererCommon.RendererEvents() {

                        @Override
                        public void onFirstFrameRendered() {
                        }

                        @Override
                        public void onFrameResolutionChanged(final int i, final int i1, int i2) {
                            runOnUiThread(new Runnable() {

                                @Override
                                public void run() {
                                    mRemoteResolutionView.setText(i + "x" + i1);
                                }
                            });
                        }
                    });
                } catch (IllegalStateException e) {
                // ignore
                }
                /**
                 * 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);
                                mTestButton.setEnabled(false);
                                mStatusView.setText(connection.getStatus());
                                /**
                                 * The options for the stream to publish are set.
                                 * The stream name is passed when StreamOptions object is created.
                                 * VideoConstraints object is used to set the source camera, FPS and resolution.
                                 * Stream constraints are set with method StreamOptions.setConstraints().
                                 */
                                StreamOptions streamOptions = new StreamOptions(streamName);
                                Constraints constraints = getConstraints();
                                streamOptions.setConstraints(constraints);
                                String[] stripCodec = { (String) mStripStreamerCodec.getSpinner().getSelectedItem() };
                                streamOptions.setStripCodecs(stripCodec);
                                /**
                                 * Stream is created with method Session.createStream().
                                 */
                                publishStream = session.createStream(streamOptions);
                                if (mMuteAudio.isChecked()) {
                                    publishStream.muteAudio();
                                }
                                if (mMuteVideo.isChecked()) {
                                    publishStream.muteVideo();
                                }
                                /**
                                 * 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);
                                                    streamOptions.setConstraints(new Constraints(mReceiveAudio.isChecked(), mReceiveVideo.isChecked()));
                                                    VideoConstraints videoConstraints = null;
                                                    if (mReceiveVideo.isChecked()) {
                                                        videoConstraints = new VideoConstraints();
                                                        if (!mDefaultPlayResolution.isChecked() && mPlayWidth.getText().length() > 0 && mPlayHeight.getText().length() > 0) {
                                                            videoConstraints.setResolution(Integer.parseInt(mPlayWidth.getText().toString()), Integer.parseInt(mPlayHeight.getText().toString()));
                                                        }
                                                        if (!mDefaultPlayBitrate.isChecked() && mPlayBitrate.getText().length() > 0) {
                                                            videoConstraints.setBitrate(Integer.parseInt(mPlayBitrate.getText().toString()));
                                                        }
                                                        if (!mDefaultPlayQuality.isChecked() && mPlayQuality.getText().length() > 0) {
                                                            videoConstraints.setQuality(Integer.parseInt(mPlayQuality.getText().toString()));
                                                        }
                                                    }
                                                    AudioConstraints audioConstraints = null;
                                                    if (mReceiveAudio.isChecked()) {
                                                        audioConstraints = new AudioConstraints();
                                                    }
                                                    streamOptions.setConstraints(new Constraints(audioConstraints, videoConstraints));
                                                    String[] stripCodec = { (String) mStripPlayerCodec.getSpinner().getSelectedItem() };
                                                    streamOptions.setStripCodecs(stripCodec);
                                                    /**
                                                     * 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();
                                                    if (mSendVideo.isChecked())
                                                        mSwitchCameraButton.setEnabled(true);
                                                } else {
                                                    Log.e(TAG, "Can not publish stream " + stream.getName() + " " + streamStatus);
                                                }
                                                mStatusView.setText(streamStatus.toString());
                                            }
                                        });
                                    }
                                });
                                ActivityCompat.requestPermissions(MediaDevicesActivity.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);
                                mSwitchCameraButton.setEnabled(false);
                                mStatusView.setText(connection.getStatus());
                                mTestButton.setEnabled(true);
                            }
                        });
                    }
                });
                mStartButton.setEnabled(false);
                mTestButton.setEnabled(false);
                /**
                 * Connection to WCS server is established with method Session.connect().
                 */
                session.connect(new Connection());
                SharedPreferences sharedPref = MediaDevicesActivity.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);
            }
        }
    });
    mTestButton = (Button) findViewById(R.id.test_button);
    mTestButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View view) {
            if (mTestButton.getTag() == null || Integer.valueOf(R.string.action_test).equals(mTestButton.getTag())) {
                ActivityCompat.requestPermissions(MediaDevicesActivity.this, new String[] { Manifest.permission.RECORD_AUDIO, Manifest.permission.CAMERA }, TEST_REQUEST_CODE);
            } else {
                Flashphoner.releaseLocalMediaAccess();
                soundMeter.stop();
                mTestButton.setText(R.string.action_test);
                mTestButton.setTag(R.string.action_test);
                mStartButton.setEnabled(true);
            }
        }
    });
    mSwitchCameraButton = (Button) findViewById(R.id.switch_camera_button);
    /**
     * Connection to server will be established and stream will be published when Start button is clicked.
     */
    mSwitchCameraButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View view) {
            if (publishStream != null) {
                mSwitchCameraButton.setEnabled(false);
                publishStream.switchCamera(new CameraSwitchHandler() {

                    @Override
                    public void onCameraSwitchDone(boolean var1) {
                        runOnUiThread(new Runnable() {

                            @Override
                            public void run() {
                                mSwitchCameraButton.setEnabled(true);
                            }
                        });
                    }

                    @Override
                    public void onCameraSwitchError(String var1) {
                        runOnUiThread(new Runnable() {

                            @Override
                            public void run() {
                                mSwitchCameraButton.setEnabled(true);
                            }
                        });
                    }
                });
            }
        }
    });
    /**
     * 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 (publishStream != null) {
                if (isChecked) {
                    publishStream.muteAudio();
                } else {
                    publishStream.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 (publishStream != null) {
                if (isChecked) {
                    publishStream.muteVideo();
                } else {
                    publishStream.unmuteVideo();
                }
            }
        }
    });
    localRender = (FPSurfaceViewRenderer) findViewById(R.id.local_video_view);
    mLocalResolutionView = (TextView) findViewById(R.id.local_resolution);
    remoteRender = (FPSurfaceViewRenderer) findViewById(R.id.remote_video_view);
    mRemoteResolutionView = (TextView) findViewById(R.id.remote_resolution);
    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 : StreamOptions(com.flashphoner.fpwcsapi.session.StreamOptions) InputMethodManager(android.view.inputmethod.InputMethodManager) URISyntaxException(java.net.URISyntaxException) StreamStatus(com.flashphoner.fpwcsapi.bean.StreamStatus) URI(java.net.URI) StreamStatusEvent(com.flashphoner.fpwcsapi.session.StreamStatusEvent) Stream(com.flashphoner.fpwcsapi.session.Stream) SeekBar(android.widget.SeekBar) VideoConstraints(com.flashphoner.fpwcsapi.constraints.VideoConstraints) CameraSwitchHandler(com.flashphoner.fpwcsapi.handler.CameraSwitchHandler) SharedPreferences(android.content.SharedPreferences) SessionOptions(com.flashphoner.fpwcsapi.session.SessionOptions) Connection(com.flashphoner.fpwcsapi.bean.Connection) Data(com.flashphoner.fpwcsapi.bean.Data) View(android.view.View) TextView(android.widget.TextView) SessionEvent(com.flashphoner.fpwcsapi.session.SessionEvent) VideoConstraints(com.flashphoner.fpwcsapi.constraints.VideoConstraints) AudioConstraints(com.flashphoner.fpwcsapi.constraints.AudioConstraints) Constraints(com.flashphoner.fpwcsapi.constraints.Constraints) OnClickListener(android.view.View.OnClickListener) AudioConstraints(com.flashphoner.fpwcsapi.constraints.AudioConstraints) CompoundButton(android.widget.CompoundButton)

Example 2 with Connection

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

the class PhoneMinVideoActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_streaming_min);
    /**
     * Initialization of the API.
     */
    Flashphoner.init(this);
    /**
     * UI controls
     */
    SharedPreferences sharedPref = this.getPreferences(Context.MODE_PRIVATE);
    mWcsUrlView = (EditText) findViewById(R.id.wcs_url);
    mWcsUrlView.setText(sharedPref.getString("wcs_url", getString(R.string.wcs_url)));
    mSipLoginView = (EditText) findViewById(R.id.sip_login);
    mSipLoginView.setText(sharedPref.getString("sip_login", getString(R.string.sip_login)));
    mSipPasswordView = (EditText) findViewById(R.id.sip_password);
    mSipPasswordView.setText(sharedPref.getString("sip_password", getString(R.string.sip_password)));
    mSipDomainView = (EditText) findViewById(R.id.sip_domain);
    mSipDomainView.setText(sharedPref.getString("sip_domain", getString(R.string.sip_domain)));
    mSipPortView = (EditText) findViewById(R.id.sip_port);
    mSipPortView.setText(sharedPref.getString("sip_port", getString(R.string.sip_port)));
    mSipRegisterRequiredView = (CheckBox) findViewById(R.id.register_required);
    mSipRegisterRequiredView.setChecked(sharedPref.getBoolean("sip_register_required", true));
    callStatusEvent = new CallStatusEvent() {

        /**
         * WCS received SIP 100 TRYING
         * @param call
         */
        @Override
        public void onTrying(final Call call) {
            runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    mCallButton.setText(R.string.action_hangup);
                    mCallButton.setTag(R.string.action_hangup);
                    mCallButton.setEnabled(true);
                    mCallStatus.setText(call.getStatus());
                }
            });
        }

        /**
         * WCS received SIP BUSY_HERE or BUSY_EVERYWHERE from SIP
         * @param call
         */
        @Override
        public void onBusy(final Call call) {
            runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    mCallButton.setText(R.string.action_call);
                    mCallButton.setTag(R.string.action_call);
                    mCallButton.setEnabled(true);
                    mCallStatus.setText(call.getStatus());
                }
            });
        }

        /**
         * Call is failed
         * @param call
         */
        @Override
        public void onFailed(Call call) {
        }

        /**
         * WCS received SIP 180 RINGING from SIP
         * @param call
         */
        @Override
        public void onRing(final Call call) {
            runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    mCallButton.setText(R.string.action_hangup);
                    mCallButton.setTag(R.string.action_hangup);
                    mCallButton.setEnabled(true);
                    mCallStatus.setText(call.getStatus());
                }
            });
        }

        /**
         * Call is set on hold
         * @param call
         */
        @Override
        public void onHold(final Call call) {
            runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    mCallStatus.setText(call.getStatus());
                }
            });
        }

        /**
         * Call established
         * @param call
         */
        @Override
        public void onEstablished(final Call call) {
            runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    mCallStatus.setText(call.getStatus());
                    mHoldButton.setEnabled(true);
                    mMuteAudio.setEnabled(true);
                    mMuteVideo.setEnabled(true);
                }
            });
        }

        /**
         * Call is terminated
         * @param call
         */
        @Override
        public void onFinished(final Call call) {
            runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    mCallButton.setText(R.string.action_call);
                    mCallButton.setTag(R.string.action_call);
                    mCallButton.setEnabled(true);
                    mCallStatus.setText(call.getStatus());
                    mHoldButton.setText(R.string.action_hold);
                    mHoldButton.setTag(R.string.action_hold);
                    mHoldButton.setEnabled(false);
                    mMuteAudio.setEnabled(false);
                    mMuteAudio.setChecked(false);
                    mMuteVideo.setEnabled(false);
                    mMuteVideo.setChecked(false);
                    if (incomingCallAlert != null) {
                        incomingCallAlert.hide();
                        incomingCallAlert = null;
                    }
                }
            });
        }
    };
    mConnectStatus = (TextView) findViewById(R.id.connect_status);
    mConnectButton = (Button) findViewById(R.id.connect_button);
    /**
     * Connect button pressed
     */
    mConnectButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View view) {
            if (mConnectButton.getTag() == null || Integer.valueOf(R.string.action_connect).equals(mConnectButton.getTag())) {
                SessionOptions sessionOptions = new SessionOptions(mWcsUrlView.getText().toString());
                sessionOptions.setLocalRenderer(localRender);
                sessionOptions.setRemoteRenderer(remoteRender);
                session = Flashphoner.createSession(sessionOptions);
                session.on(new SessionEvent() {

                    @Override
                    public void onAppData(Data data) {
                    }

                    /**
                     * Connection established
                     * @param connection Current connection state
                     */
                    @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);
                                if (!mSipRegisterRequiredView.isChecked()) {
                                    mConnectStatus.setText(connection.getStatus());
                                    mCallButton.setEnabled(true);
                                } else {
                                    mConnectStatus.setText(connection.getStatus() + ". Registering...");
                                }
                            }
                        });
                    }

                    /**
                     * Phone registered
                     * @param connection Current connection state
                     */
                    @Override
                    public void onRegistered(final Connection connection) {
                        runOnUiThread(new Runnable() {

                            @Override
                            public void run() {
                                mConnectStatus.setText(connection.getStatus());
                                mCallButton.setEnabled(true);
                            }
                        });
                    }

                    /**
                     * Phone disconnected
                     * @param connection Current connection state
                     */
                    @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);
                                mConnectStatus.setText(connection.getStatus());
                                mCallButton.setText(R.string.action_call);
                                mCallButton.setTag(R.string.action_call);
                                mCallButton.setEnabled(false);
                                mCallStatus.setText("");
                                mHoldButton.setText(R.string.action_hold);
                                mHoldButton.setTag(R.string.action_hold);
                                mHoldButton.setEnabled(false);
                                mMuteAudio.setEnabled(false);
                                mMuteAudio.setChecked(false);
                                mMuteVideo.setEnabled(false);
                                mMuteVideo.setChecked(false);
                            }
                        });
                    }
                });
                /**
                 * Add handler for incoming call
                 */
                session.on(new IncomingCallEvent() {

                    @Override
                    public void onCall(final Call call) {
                        call.on(callStatusEvent);
                        /**
                         * Display UI alert for the new incoming call
                         */
                        runOnUiThread(new Runnable() {

                            @Override
                            public void run() {
                                AlertDialog.Builder builder = new AlertDialog.Builder(PhoneMinVideoActivity.this);
                                builder.setTitle("Incoming call");
                                builder.setMessage("Incoming call from '" + call.getCaller() + "'");
                                builder.setPositiveButton("Answer", new DialogInterface.OnClickListener() {

                                    @Override
                                    public void onClick(DialogInterface dialogInterface, int i) {
                                        PhoneMinVideoActivity.this.call = call;
                                        ActivityCompat.requestPermissions(PhoneMinVideoActivity.this, new String[] { Manifest.permission.RECORD_AUDIO, Manifest.permission.CAMERA }, INCOMING_CALL_REQUEST_CODE);
                                    }
                                });
                                builder.setNegativeButton("Hangup", new DialogInterface.OnClickListener() {

                                    @Override
                                    public void onClick(DialogInterface dialogInterface, int i) {
                                        call.hangup();
                                        incomingCallAlert = null;
                                    }
                                });
                                incomingCallAlert = builder.show();
                            }
                        });
                    }
                });
                mConnectButton.setEnabled(false);
                /**
                 * Connection containing SIP details
                 */
                Connection connection = new Connection();
                connection.setSipLogin(mSipLoginView.getText().toString());
                connection.setSipPassword(mSipPasswordView.getText().toString());
                connection.setSipDomain(mSipDomainView.getText().toString());
                connection.setSipOutboundProxy(mSipDomainView.getText().toString());
                connection.setSipPort(Integer.parseInt(mSipPortView.getText().toString()));
                connection.setSipRegisterRequired(mSipRegisterRequiredView.isChecked());
                session.connect(connection);
                SharedPreferences sharedPref = PhoneMinVideoActivity.this.getPreferences(Context.MODE_PRIVATE);
                SharedPreferences.Editor editor = sharedPref.edit();
                editor.putString("wcs_url", mWcsUrlView.getText().toString());
                editor.putString("sip_login", mSipLoginView.getText().toString());
                editor.putString("sip_password", mSipPasswordView.getText().toString());
                editor.putString("sip_domain", mSipDomainView.getText().toString());
                editor.putString("sip_port", mSipPortView.getText().toString());
                editor.putBoolean("sip_register_required", mSipRegisterRequiredView.isChecked());
                editor.apply();
            } else {
                mConnectButton.setEnabled(false);
                session.disconnect();
            }
            View currentFocus = getCurrentFocus();
            if (currentFocus != null) {
                InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                inputManager.hideSoftInputFromWindow(currentFocus.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
            }
        }
    });
    mCalleeView = (EditText) findViewById(R.id.callee);
    mCalleeView.setText(sharedPref.getString("callee", getString(R.string.default_callee_name)));
    mCallStatus = (TextView) findViewById(R.id.call_status);
    mCallButton = (Button) findViewById(R.id.call_button);
    /**
     * Call button pressed
     */
    mCallButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View view) {
            if (mCallButton.getTag() == null || Integer.valueOf(R.string.action_call).equals(mCallButton.getTag())) {
                if ("".equals(mCalleeView.getText().toString())) {
                    return;
                }
                ActivityCompat.requestPermissions(PhoneMinVideoActivity.this, new String[] { Manifest.permission.RECORD_AUDIO, Manifest.permission.CAMERA }, CALL_REQUEST_CODE);
                SharedPreferences sharedPref = PhoneMinVideoActivity.this.getPreferences(Context.MODE_PRIVATE);
                SharedPreferences.Editor editor = sharedPref.edit();
                editor.putString("callee", mCalleeView.getText().toString());
                editor.apply();
            } else {
                mCallButton.setEnabled(false);
                call.hangup();
                call = null;
            }
            View currentFocus = getCurrentFocus();
            if (currentFocus != null) {
                InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                inputManager.hideSoftInputFromWindow(currentFocus.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
            }
        }
    });
    mHoldButton = (Button) findViewById(R.id.hold_button);
    /**
     * Hold or Unhold button pressed
     * Hold the call if the call is ESTABLISHED.
     * Unhold the call if the call is on hold.
     */
    mHoldButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View view) {
            if (mHoldButton.getTag() == null || Integer.valueOf(R.string.action_hold).equals(mHoldButton.getTag())) {
                call.hold();
                mHoldButton.setText(R.string.action_unhold);
                mHoldButton.setTag(R.string.action_unhold);
            } else {
                call.unhold();
                mHoldButton.setText(R.string.action_hold);
                mHoldButton.setTag(R.string.action_hold);
            }
        }
    });
    mMuteAudio = (Switch) findViewById(R.id.mute_audio);
    /**
     * Mute or Unmute audio for the SIP call
     * Mute if it is not muted.
     * Unmute if it is muted.
     */
    mMuteAudio.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (call != null) {
                if (isChecked) {
                    call.muteAudio();
                } else {
                    call.unmuteAudio();
                }
            }
        }
    });
    mMuteVideo = (Switch) findViewById(R.id.mute_video);
    /**
     * Mute or Unmute video for the SIP call
     * Mute if it is not muted.
     * Unmute if it is muted.
     */
    mMuteVideo.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (call != null) {
                if (isChecked) {
                    call.muteVideo();
                } else {
                    call.unmuteVideo();
                }
            }
        }
    });
    /**
     * Set local and remote renderer objects
     */
    localRender = (SurfaceViewRenderer) findViewById(R.id.local_video_view);
    remoteRender = (SurfaceViewRenderer) findViewById(R.id.remote_video_view);
    PercentFrameLayout localRenderLayout = (PercentFrameLayout) findViewById(R.id.local_video_layout);
    PercentFrameLayout 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 : AlertDialog(android.support.v7.app.AlertDialog) Call(com.flashphoner.fpwcsapi.session.Call) SharedPreferences(android.content.SharedPreferences) DialogInterface(android.content.DialogInterface) PercentFrameLayout(com.flashphoner.fpwcsapi.layout.PercentFrameLayout) CallStatusEvent(com.flashphoner.fpwcsapi.session.CallStatusEvent) SessionOptions(com.flashphoner.fpwcsapi.session.SessionOptions) Connection(com.flashphoner.fpwcsapi.bean.Connection) Data(com.flashphoner.fpwcsapi.bean.Data) InputMethodManager(android.view.inputmethod.InputMethodManager) View(android.view.View) TextView(android.widget.TextView) SessionEvent(com.flashphoner.fpwcsapi.session.SessionEvent) IncomingCallEvent(com.flashphoner.fpwcsapi.session.IncomingCallEvent) OnClickListener(android.view.View.OnClickListener) CompoundButton(android.widget.CompoundButton)

Example 3 with Connection

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

the class PhoneMinActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_streaming_min);
    /**
     * Initialization of the API.
     */
    Flashphoner.init(this);
    /**
     * UI controls
     */
    SharedPreferences sharedPref = this.getPreferences(Context.MODE_PRIVATE);
    mWcsUrlView = (EditText) findViewById(R.id.wcs_url);
    mWcsUrlView.setText(sharedPref.getString("wcs_url", getString(R.string.wcs_url)));
    mSipLoginView = (EditText) findViewById(R.id.sip_login);
    mSipLoginView.setText(sharedPref.getString("sip_login", getString(R.string.sip_login)));
    mSipPasswordView = (EditText) findViewById(R.id.sip_password);
    mSipPasswordView.setText(sharedPref.getString("sip_password", getString(R.string.sip_password)));
    mSipDomainView = (EditText) findViewById(R.id.sip_domain);
    mSipDomainView.setText(sharedPref.getString("sip_domain", getString(R.string.sip_domain)));
    mSipPortView = (EditText) findViewById(R.id.sip_port);
    mSipPortView.setText(sharedPref.getString("sip_port", getString(R.string.sip_port)));
    mSipRegisterRequiredView = (CheckBox) findViewById(R.id.register_required);
    mSipRegisterRequiredView.setChecked(sharedPref.getBoolean("sip_register_required", true));
    callStatusEvent = new CallStatusEvent() {

        /**
         * WCS received SIP 100 TRYING
         * @param call
         */
        @Override
        public void onTrying(final Call call) {
            runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    mCallButton.setText(R.string.action_hangup);
                    mCallButton.setTag(R.string.action_hangup);
                    mCallButton.setEnabled(true);
                    mCallStatus.setText(call.getStatus());
                }
            });
        }

        /**
         * WCS received SIP BUSY_HERE or BUSY_EVERYWHERE from SIP
         * @param call
         */
        @Override
        public void onBusy(final Call call) {
            runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    mCallButton.setText(R.string.action_call);
                    mCallButton.setTag(R.string.action_call);
                    mCallButton.setEnabled(true);
                    mCallStatus.setText(call.getStatus());
                }
            });
        }

        /**
         * Call is failed
         * @param call
         */
        @Override
        public void onFailed(Call call) {
        }

        /**
         * WCS received SIP 180 RINGING from SIP
         * @param call
         */
        @Override
        public void onRing(final Call call) {
            runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    mCallButton.setText(R.string.action_hangup);
                    mCallButton.setTag(R.string.action_hangup);
                    mCallButton.setEnabled(true);
                    mCallStatus.setText(call.getStatus());
                }
            });
        }

        /**
         * Call is set on hold
         * @param call
         */
        @Override
        public void onHold(final Call call) {
            runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    mCallStatus.setText(call.getStatus());
                }
            });
        }

        /**
         * Call established
         * @param call
         */
        @Override
        public void onEstablished(final Call call) {
            runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    mCallStatus.setText(call.getStatus());
                    mHoldButton.setEnabled(true);
                    mDTMFButton.setEnabled(true);
                }
            });
        }

        /**
         * Call is terminated
         * @param call
         */
        @Override
        public void onFinished(final Call call) {
            runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    mCallButton.setText(R.string.action_call);
                    mCallButton.setTag(R.string.action_call);
                    mCallButton.setEnabled(true);
                    mCallStatus.setText(call.getStatus());
                    mHoldButton.setText(R.string.action_hold);
                    mHoldButton.setTag(R.string.action_hold);
                    mHoldButton.setEnabled(false);
                    mDTMFButton.setEnabled(false);
                    if (incomingCallAlert != null) {
                        incomingCallAlert.hide();
                        incomingCallAlert = null;
                    }
                }
            });
        }
    };
    mConnectStatus = (TextView) findViewById(R.id.connect_status);
    mConnectButton = (Button) findViewById(R.id.connect_button);
    /**
     * Connect button pressed
     */
    mConnectButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View view) {
            if (mConnectButton.getTag() == null || Integer.valueOf(R.string.action_connect).equals(mConnectButton.getTag())) {
                SessionOptions sessionOptions = new SessionOptions(mWcsUrlView.getText().toString());
                session = Flashphoner.createSession(sessionOptions);
                session.on(new SessionEvent() {

                    @Override
                    public void onAppData(Data data) {
                    }

                    /**
                     * Connection established
                     * @param connection Current connection state
                     */
                    @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);
                                if (!mSipRegisterRequiredView.isChecked()) {
                                    mConnectStatus.setText(connection.getStatus());
                                    mCallButton.setEnabled(true);
                                } else {
                                    mConnectStatus.setText(connection.getStatus() + ". Registering...");
                                }
                            }
                        });
                    }

                    /**
                     * Phone registered
                     * @param connection Current connection state
                     */
                    @Override
                    public void onRegistered(final Connection connection) {
                        runOnUiThread(new Runnable() {

                            @Override
                            public void run() {
                                mConnectStatus.setText(connection.getStatus());
                                mCallButton.setEnabled(true);
                            }
                        });
                    }

                    /**
                     * Phone disconnected
                     * @param connection Current connection state
                     */
                    @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);
                                mConnectStatus.setText(connection.getStatus());
                                mCallButton.setText(R.string.action_call);
                                mCallButton.setTag(R.string.action_call);
                                mCallButton.setEnabled(false);
                                mCallStatus.setText("");
                                mHoldButton.setText(R.string.action_hold);
                                mHoldButton.setTag(R.string.action_hold);
                                mHoldButton.setEnabled(false);
                                mDTMFButton.setEnabled(false);
                            }
                        });
                    }
                });
                /**
                 * Add handler for incoming call
                 */
                session.on(new IncomingCallEvent() {

                    @Override
                    public void onCall(final Call call) {
                        call.on(callStatusEvent);
                        /**
                         * Display UI alert for the new incoming call
                         */
                        runOnUiThread(new Runnable() {

                            @Override
                            public void run() {
                                AlertDialog.Builder builder = new AlertDialog.Builder(PhoneMinActivity.this);
                                builder.setTitle("Incoming call");
                                builder.setMessage("Incoming call from '" + call.getCaller() + "'");
                                builder.setPositiveButton("Answer", new DialogInterface.OnClickListener() {

                                    @Override
                                    public void onClick(DialogInterface dialogInterface, int i) {
                                        PhoneMinActivity.this.call = call;
                                        ActivityCompat.requestPermissions(PhoneMinActivity.this, new String[] { Manifest.permission.RECORD_AUDIO }, INCOMING_CALL_REQUEST_CODE);
                                    }
                                });
                                builder.setNegativeButton("Hangup", new DialogInterface.OnClickListener() {

                                    @Override
                                    public void onClick(DialogInterface dialogInterface, int i) {
                                        call.hangup();
                                        incomingCallAlert = null;
                                    }
                                });
                                incomingCallAlert = builder.show();
                            }
                        });
                    }
                });
                mConnectButton.setEnabled(false);
                /**
                 * Connection containing SIP details
                 */
                Connection connection = new Connection();
                connection.setSipLogin(mSipLoginView.getText().toString());
                connection.setSipPassword(mSipPasswordView.getText().toString());
                connection.setSipDomain(mSipDomainView.getText().toString());
                connection.setSipOutboundProxy(mSipDomainView.getText().toString());
                connection.setSipPort(Integer.parseInt(mSipPortView.getText().toString()));
                connection.setSipRegisterRequired(mSipRegisterRequiredView.isChecked());
                session.connect(connection);
                SharedPreferences sharedPref = PhoneMinActivity.this.getPreferences(Context.MODE_PRIVATE);
                SharedPreferences.Editor editor = sharedPref.edit();
                editor.putString("wcs_url", mWcsUrlView.getText().toString());
                editor.putString("sip_login", mSipLoginView.getText().toString());
                editor.putString("sip_password", mSipPasswordView.getText().toString());
                editor.putString("sip_domain", mSipDomainView.getText().toString());
                editor.putString("sip_port", mSipPortView.getText().toString());
                editor.putBoolean("sip_register_required", mSipRegisterRequiredView.isChecked());
                editor.apply();
            } else {
                mConnectButton.setEnabled(false);
                session.disconnect();
            }
            View currentFocus = getCurrentFocus();
            if (currentFocus != null) {
                InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                inputManager.hideSoftInputFromWindow(currentFocus.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
            }
        }
    });
    mCalleeView = (EditText) findViewById(R.id.callee);
    mCalleeView.setText(sharedPref.getString("callee", getString(R.string.default_callee_name)));
    mInviteParametersView = (EditText) findViewById(R.id.invite_parameters);
    googEchoCancellation = (CheckBox) findViewById(R.id.googEchoCancellationCB);
    googAutoGainControl = (CheckBox) findViewById(R.id.googAutoGainControlCB);
    googNoiseSupression = (CheckBox) findViewById(R.id.googNoiseSupressionCB);
    googHighpassFilter = (CheckBox) findViewById(R.id.googHighpassFilterCB);
    googEchoCancellation2 = (CheckBox) findViewById(R.id.googEchoCancellation2CB);
    googAutoGainControl2 = (CheckBox) findViewById(R.id.googAutoGainControl2CB);
    googNoiseSuppression2 = (CheckBox) findViewById(R.id.googNoiseSuppression2CB);
    mProximitySensor = (CheckBox) findViewById(R.id.proximitySensor);
    mProximitySensor.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            Flashphoner.getAudioManager().setUseProximitySensor(isChecked);
        }
    });
    mSpeakerPhone = (CheckBox) findViewById(R.id.speakerPhone);
    mSpeakerPhone.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            Flashphoner.getAudioManager().setUseSpeakerPhone(isChecked);
        }
    });
    mCallStatus = (TextView) findViewById(R.id.call_status);
    mCallButton = (Button) findViewById(R.id.call_button);
    /**
     * Call button pressed
     */
    mCallButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View view) {
            if (mCallButton.getTag() == null || Integer.valueOf(R.string.action_call).equals(mCallButton.getTag())) {
                if ("".equals(mCalleeView.getText().toString())) {
                    return;
                }
                ActivityCompat.requestPermissions(PhoneMinActivity.this, new String[] { Manifest.permission.RECORD_AUDIO }, CALL_REQUEST_CODE);
                SharedPreferences sharedPref = PhoneMinActivity.this.getPreferences(Context.MODE_PRIVATE);
                SharedPreferences.Editor editor = sharedPref.edit();
                editor.putString("callee", mCalleeView.getText().toString());
                editor.apply();
            } else {
                mCallButton.setEnabled(false);
                call.hangup();
                call = null;
            }
            View currentFocus = getCurrentFocus();
            if (currentFocus != null) {
                InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                inputManager.hideSoftInputFromWindow(currentFocus.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
            }
        }
    });
    mHoldButton = (Button) findViewById(R.id.hold_button);
    /**
     * Hold or Unhold button pressed
     * Hold the call if the call is ESTABLISHED.
     * Unhold the call if the call is on hold.
     */
    mHoldButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View view) {
            if (mHoldButton.getTag() == null || Integer.valueOf(R.string.action_hold).equals(mHoldButton.getTag())) {
                call.hold();
                mHoldButton.setText(R.string.action_unhold);
                mHoldButton.setTag(R.string.action_unhold);
            } else {
                call.unhold();
                mHoldButton.setText(R.string.action_hold);
                mHoldButton.setTag(R.string.action_hold);
            }
        }
    });
    mDTMF = (EditText) findViewById(R.id.dtmf);
    mDTMFButton = (Button) findViewById(R.id.dtmf_button);
    mDTMFButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View view) {
            if (call != null) {
                call.sendDTMF(mDTMF.getText().toString(), Call.DTMFType.RFC2833);
            }
        }
    });
}
Also used : AlertDialog(android.support.v7.app.AlertDialog) Call(com.flashphoner.fpwcsapi.session.Call) SharedPreferences(android.content.SharedPreferences) DialogInterface(android.content.DialogInterface) CallStatusEvent(com.flashphoner.fpwcsapi.session.CallStatusEvent) SessionOptions(com.flashphoner.fpwcsapi.session.SessionOptions) Connection(com.flashphoner.fpwcsapi.bean.Connection) Data(com.flashphoner.fpwcsapi.bean.Data) InputMethodManager(android.view.inputmethod.InputMethodManager) View(android.view.View) TextView(android.widget.TextView) SessionEvent(com.flashphoner.fpwcsapi.session.SessionEvent) IncomingCallEvent(com.flashphoner.fpwcsapi.session.IncomingCallEvent) OnClickListener(android.view.View.OnClickListener) CompoundButton(android.widget.CompoundButton)

Example 4 with Connection

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

the class StreamRecordingActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_stream_recording);
    /**
     * 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 Record 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 = new URI(mWcsUrlView.getText().toString());
                    url = uri.getScheme() + "://" + uri.getHost() + ":" + uri.getPort();
                    streamName = uri.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().
                 */
                SessionOptions sessionOptions = new SessionOptions(url);
                sessionOptions.setLocalRenderer(localRender);
                /**
                 * 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.
                                 * To enable stream recording, option 'record' is set to 'true' with method StreamOptions.setRecord().
                                 */
                                StreamOptions streamOptions = new StreamOptions(streamName);
                                streamOptions.setRecord(true);
                                /**
                                 * Stream is created with method Session.createStream().
                                 */
                                publishStream = session.createStream(streamOptions);
                                /**
                                 * Callback function for stream status change is added to display the status.
                                 */
                                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)) {
                                                    mStatusView.setText("RECORDING");
                                                    /**
                                                     * Filename of the recording is determined.
                                                     */
                                                    recordFilename = stream.getRecordName();
                                                    return;
                                                } else if (StreamStatus.FAILED.equals(streamStatus)) {
                                                    Log.e(TAG, "Can not publish stream " + stream.getName() + " " + streamStatus);
                                                    recordFilename = null;
                                                }
                                                mStatusView.setText(streamStatus.toString());
                                            }
                                        });
                                    }
                                });
                                ActivityCompat.requestPermissions(StreamRecordingActivity.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());
                                /**
                                 * After disconnection, download link for the recording of the published stream is displayed, and the recording can be played in the media player of the application.
                                 */
                                if (recordFilename != null) {
                                    /**
                                     * Download link is formed.
                                     * Stream recordings are saved to directory WCS_HOME/client/records on the server.
                                     */
                                    String url = "http://" + uri.getHost() + ":9091/client/records/" + recordFilename;
                                    mRecordedLink.setText(url);
                                    Linkify.addLinks(mRecordedLink, Linkify.WEB_URLS);
                                    MediaController mediaController = new MediaController(StreamRecordingActivity.this);
                                    mediaController.setAnchorView(mRecordedVideoView);
                                    mRecordedVideoView.setMediaController(mediaController);
                                    mRecordedVideoView.setVideoURI(Uri.parse(url));
                                    /**
                                     * Playback of the recording in the media player is started.
                                     */
                                    mRecordedVideoView.start();
                                }
                            }
                        });
                    }
                });
                mStartButton.setEnabled(false);
                /**
                 * Connection to WCS server is established with method Session.connect().
                 */
                session.connect(new Connection());
                SharedPreferences sharedPref = StreamRecordingActivity.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);
    localRenderLayout = (PercentFrameLayout) findViewById(R.id.local_video_layout);
    localRender.setZOrderMediaOverlay(true);
    localRenderLayout.setPosition(0, 0, 100, 100);
    localRender.setScalingType(RendererCommon.ScalingType.SCALE_ASPECT_FIT);
    localRender.setMirror(true);
    localRender.requestLayout();
    mRecordedLink = (TextView) findViewById(R.id.recorded_link);
    mRecordedVideoView = (VideoView) findViewById(R.id.recorded_video_view);
}
Also used : MediaController(android.widget.MediaController) 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) VideoView(android.widget.VideoView) 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 5 with Connection

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

the class ClickToCallActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_click_to_call);
    /**
     * Initialization of the API.
     */
    Flashphoner.init(this);
    SharedPreferences sharedPref = this.getPreferences(Context.MODE_PRIVATE);
    mWcsUrlView = (EditText) findViewById(R.id.wcs_url);
    mWcsUrlView.setText(sharedPref.getString("wcs_url", getString(R.string.wcs_url)));
    mCalleeView = (EditText) findViewById(R.id.callee);
    mCalleeView.setText(sharedPref.getString("callee", getString(R.string.callee)));
    mCallStatus = (TextView) findViewById(R.id.status);
    mCallButton = (Button) findViewById(R.id.start_button);
    /**
     * Call on click
     */
    mCallButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View view) {
            if (mCallButton.getTag() == null || Integer.valueOf(R.string.action_call).equals(mCallButton.getTag())) {
                SessionOptions sessionOptions = new SessionOptions(mWcsUrlView.getText().toString());
                session = Flashphoner.createSession(sessionOptions);
                session.on(new SessionEvent() {

                    @Override
                    public void onAppData(Data data) {
                    }

                    /**
                     * Callback functions for connection status events are added to make appropriate changes in controls of the interface when connection is established and closed.
                     */
                    @Override
                    public void onConnected(final Connection connection) {
                        runOnUiThread(new Runnable() {

                            @Override
                            public void run() {
                                mCallButton.setText(R.string.action_hangup);
                                mCallButton.setTag(R.string.action_hangup);
                                mCallButton.setEnabled(true);
                                mCallStatus.setText("Connection: " + connection.getStatus());
                                /**
                                 * Pass 'callee' to the callOptions and create a new call object
                                 */
                                CallOptions callOptions = new CallOptions(mCalleeView.getText().toString());
                                call = session.createCall(callOptions);
                                call.on(new CallStatusEvent() {

                                    /**
                                     * WCS received 100 TRYING from SIP
                                     * @param call
                                     */
                                    @Override
                                    public void onTrying(final Call call) {
                                        runOnUiThread(new Runnable() {

                                            @Override
                                            public void run() {
                                                mCallButton.setText(R.string.action_hangup);
                                                mCallButton.setTag(R.string.action_hangup);
                                                mCallButton.setEnabled(true);
                                                mCallStatus.setText("Call: " + call.getStatus());
                                            }
                                        });
                                    }

                                    /**
                                     * WCS received BUSY_HERE or BUSY_EVERYWHERE from SIP
                                     * @param call
                                     */
                                    @Override
                                    public void onBusy(final Call call) {
                                        runOnUiThread(new Runnable() {

                                            @Override
                                            public void run() {
                                                mCallButton.setText(R.string.action_call);
                                                mCallButton.setTag(R.string.action_call);
                                                mCallButton.setEnabled(true);
                                                mCallStatus.setText("Call: " + call.getStatus());
                                            }
                                        });
                                    }

                                    /**
                                     * Call failed on server
                                     * @param call
                                     */
                                    @Override
                                    public void onFailed(final Call call) {
                                        runOnUiThread(new Runnable() {

                                            @Override
                                            public void run() {
                                                mCallStatus.setText("Call: " + call.getStatus());
                                            }
                                        });
                                    }

                                    /**
                                     * WCS received 180 RINGING from SIP
                                     * @param call
                                     */
                                    @Override
                                    public void onRing(final Call call) {
                                        runOnUiThread(new Runnable() {

                                            @Override
                                            public void run() {
                                                mCallStatus.setText("Call: " + call.getStatus());
                                            }
                                        });
                                    }

                                    /**
                                     * Call is set on hold by the remote participant
                                     * @param call
                                     */
                                    @Override
                                    public void onHold(final Call call) {
                                        runOnUiThread(new Runnable() {

                                            @Override
                                            public void run() {
                                                mCallStatus.setText("Call: " + call.getStatus());
                                            }
                                        });
                                    }

                                    /**
                                     * Call is established. WCS received 200 OK from SIP on INVITE.
                                     * @param call
                                     */
                                    @Override
                                    public void onEstablished(final Call call) {
                                        runOnUiThread(new Runnable() {

                                            @Override
                                            public void run() {
                                                mCallStatus.setText("Call: " + call.getStatus());
                                            }
                                        });
                                    }

                                    /**
                                     * Call is terminated either by caller or by a SIP remote participant.
                                     * @param call
                                     */
                                    @Override
                                    public void onFinished(final Call call) {
                                        runOnUiThread(new Runnable() {

                                            @Override
                                            public void run() {
                                                mCallButton.setText(R.string.action_call);
                                                mCallButton.setTag(R.string.action_call);
                                                mCallButton.setEnabled(true);
                                                mCallStatus.setText("Call: " + call.getStatus());
                                            }
                                        });
                                    }
                                });
                                ActivityCompat.requestPermissions(ClickToCallActivity.this, new String[] { Manifest.permission.RECORD_AUDIO }, CALL_REQUEST_CODE);
                                SharedPreferences sharedPref = ClickToCallActivity.this.getPreferences(Context.MODE_PRIVATE);
                                SharedPreferences.Editor editor = sharedPref.edit();
                                editor.putString("callee", mCalleeView.getText().toString());
                                editor.apply();
                            }
                        });
                    }

                    /**
                     * Registered on SIP. WCS received 200 OK on REGISTER request.
                     * @param connection Current connection state
                     */
                    @Override
                    public void onRegistered(Connection connection) {
                    }

                    /**
                     * Disconnected
                     * @param connection Current connection state
                     */
                    @Override
                    public void onDisconnection(final Connection connection) {
                        runOnUiThread(new Runnable() {

                            @Override
                            public void run() {
                                mCallButton.setText(R.string.action_call);
                                mCallButton.setTag(R.string.action_call);
                                mCallButton.setEnabled(true);
                                mCallStatus.setText("Connection: " + connection.getStatus());
                            }
                        });
                    }
                });
                mCallButton.setEnabled(false);
                Connection connection = new Connection();
                connection.setAppKey("clickToCallApp");
                /**
                 * Connect to WCS server
                 */
                session.connect(connection);
                SharedPreferences sharedPref = ClickToCallActivity.this.getPreferences(Context.MODE_PRIVATE);
                SharedPreferences.Editor editor = sharedPref.edit();
                editor.putString("wcs_url", mWcsUrlView.getText().toString());
                editor.apply();
            } else {
                mCallButton.setEnabled(false);
                session.disconnect();
            }
            View currentFocus = getCurrentFocus();
            if (currentFocus != null) {
                InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                inputManager.hideSoftInputFromWindow(currentFocus.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
            }
        }
    });
}
Also used : Call(com.flashphoner.fpwcsapi.session.Call) SharedPreferences(android.content.SharedPreferences) CallStatusEvent(com.flashphoner.fpwcsapi.session.CallStatusEvent) SessionOptions(com.flashphoner.fpwcsapi.session.SessionOptions) Connection(com.flashphoner.fpwcsapi.bean.Connection) Data(com.flashphoner.fpwcsapi.bean.Data) InputMethodManager(android.view.inputmethod.InputMethodManager) CallOptions(com.flashphoner.fpwcsapi.session.CallOptions) TextView(android.widget.TextView) View(android.view.View) SessionEvent(com.flashphoner.fpwcsapi.session.SessionEvent) OnClickListener(android.view.View.OnClickListener)

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