0

I'm trying do develop Android app which will send audio and video via webrtc protocol between devices. This code works well with Android and web interface, but not between two Androids. Every time connection is opened and popup says that devices are connected, but there is no sound and picture. I tried a lot of things, but nothing helped. Dependencies and permissions are ok. Used my own pub and sub keys from PubNub. It opens connection 1 of 20 times between two Androids, so I think that something isn't working well here.

Followed this tutorial to create app: tutorial

I found this error image

Camera is crashing somewhere, but I really don't know why.

public static final String VIDEO_TRACK_ID = "videoPN";
public static final String AUDIO_TRACK_ID = "audioPN";
public static final String LOCAL_MEDIA_STREAM_ID = "localStreamPN";

private PnRTCClient pnRTCClient;
private VideoSource localVideoSource;
private VideoRenderer.Callbacks localRender;
private VideoRenderer.Callbacks remoteRender;
private GLSurfaceView mVideoView;

private String username;

private class MyRTCListener extends PnRTCListener {

    @Override
    public void onLocalStream(final MediaStream localStream) {
        VideoChatActivity.this.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if(localStream.videoTracks.size()==0) return;
                localStream.videoTracks.get(0).addRenderer(new VideoRenderer(localRender));
            }
        });
    }


    @Override
    public void onAddRemoteStream(final MediaStream remoteStream, final PnPeer peer) {
        VideoChatActivity.this.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(VideoChatActivity.this,"Connected to " + peer.getId(), Toast.LENGTH_SHORT).show();
                try {
                    if(remoteStream.videoTracks.size()==0) return;
                    remoteStream.videoTracks.get(0).addRenderer(new VideoRenderer(remoteRender));
                    VideoRendererGui.update(remoteRender, 0, 0, 100, 100, VideoRendererGui.ScalingType.SCALE_ASPECT_FILL, false);
                    VideoRendererGui.update(localRender, 72, 72, 25, 25, VideoRendererGui.ScalingType.SCALE_ASPECT_FIT, true);
                }
                catch (Exception e){ e.printStackTrace(); }
            }
        });
    }

    @Override
    public void onPeerConnectionClosed(PnPeer peer) {
        Intent intent = new Intent(VideoChatActivity.this, MainActivity.class);
        startActivity(intent);
        finish();
    }

}



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.videochatactivity);

    Bundle extras = getIntent().getExtras();
    if (extras == null || !extras.containsKey(Constants.USER_NAME)) {
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
        Toast.makeText(this, "Need to pass username to VideoChatActivity in intent extras (Constants.USER_NAME).",
                Toast.LENGTH_SHORT).show();
        finish();
        return;
    }
    this.username  = extras.getString(Constants.USER_NAME, "");

    PeerConnectionFactory.initializeAndroidGlobals(
            this,  // Context
            true,  // Audio Enabled
            true,  // Video Enabled
            true,  // Hardware Acceleration Enabled
            null); // Render EGL Context

    PeerConnectionFactory pcFactory = new PeerConnectionFactory();
    this.pnRTCClient = new PnRTCClient(Constants.PUB_KEY, Constants.SUB_KEY, this.username);

    int camNumber = VideoCapturerAndroid.getDeviceCount();
    String frontFacingCam = VideoCapturerAndroid.getNameOfFrontFacingDevice();
    String backFacingCam  = VideoCapturerAndroid.getNameOfBackFacingDevice();

    VideoCapturerAndroid capturer = (VideoCapturerAndroid) VideoCapturerAndroid.create(frontFacingCam);

    localVideoSource = pcFactory.createVideoSource(capturer, this.pnRTCClient.videoConstraints());
    VideoTrack localVideoTrack = pcFactory.createVideoTrack(VIDEO_TRACK_ID, localVideoSource);

    AudioSource audioSource = pcFactory.createAudioSource(this.pnRTCClient.audioConstraints());
    AudioTrack localAudioTrack = pcFactory.createAudioTrack(AUDIO_TRACK_ID, audioSource);

    MediaStream mediaStream = pcFactory.createLocalMediaStream(LOCAL_MEDIA_STREAM_ID);

    mediaStream.addTrack(localVideoTrack);
    mediaStream.addTrack(localAudioTrack);

    this.mVideoView = (GLSurfaceView) findViewById(R.id.gl_surface);

    VideoRendererGui.setView(mVideoView, null);


    remoteRender = VideoRendererGui.create(0, 0, 100, 100, VideoRendererGui.ScalingType.SCALE_ASPECT_FILL, false);
    localRender = VideoRendererGui.create(0, 0, 100, 100, VideoRendererGui.ScalingType.SCALE_ASPECT_FILL, true);

    this.pnRTCClient.attachRTCListener(new MyRTCListener());
    this.pnRTCClient.attachLocalMediaStream(mediaStream);

    this.pnRTCClient.listenOn(this.username);
    this.pnRTCClient.setMaxConnections(1);

    if (extras.containsKey(Constants.JSON_CALL_USER)) {
        String callUser = extras.getString(Constants.JSON_CALL_USER, "");
        connectToUser(callUser);
    }

}
public void connectToUser(String user) {
    this.pnRTCClient.connect(user);
}

public void hangup(View view) {
    this.pnRTCClient.closeAllConnections();
    startActivity(new Intent(VideoChatActivity.this, MainActivity.class));

}

@Override
protected void onDestroy() {
    super.onDestroy();
    if (this.localVideoSource != null) {
        localVideoSource.stop();
    }
    if (this.pnRTCClient != null) {
        this.pnRTCClient.onDestroy();
        this.pnRTCClient.closeAllConnections();
    }
}

Here is MainActivity if it will help:

private SharedPreferences mSharedPreferences;
private TextView mUsernameTV;
private EditText mCallNumET;
// private Pubnub mPubNub;
private String username;
private Pubnub mPubNub;

public void initPubNub() {
    String stdbyChannel = this.username + Constants.STDBY_SUFFIX;
    this.mPubNub = new Pubnub(Constants.PUB_KEY, Constants.SUB_KEY);
    this.mPubNub.setUUID(this.username);
    try {
        this.mPubNub.subscribe(stdbyChannel, new Callback() {
            @Override
            public void successCallback(String channel, Object message) {
                Log.d("MA-success", "MESSAGE: " + message.toString());
                if (!(message instanceof JSONObject)) return; // Ignore if not JSONObject
                JSONObject jsonMsg = (JSONObject) message;
                try {
                    if (!jsonMsg.has(Constants.JSON_CALL_USER)) return;
                    String user = jsonMsg.getString(Constants.JSON_CALL_USER);
                    // Consider Accept/Reject call here
                    Intent intent = new Intent(MainActivity.this, VideoChatActivity.class);
                    intent.putExtra(Constants.USER_NAME, username);
                    intent.putExtra(Constants.JSON_CALL_USER, user);
                    startActivity(intent);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    } catch (PubnubException e) {
        e.printStackTrace();
    }
}


public void makeCall(View view){
    String callNum = mCallNumET.getText().toString();
    if (callNum.isEmpty() || callNum.equals(this.username)) {
        Toast.makeText(this, "Enter a valid number.", Toast.LENGTH_SHORT).show();
    }
    dispatchCall(callNum);
}

public void dispatchCall(final String callNum) {
    final String callNumStdBy = callNum + Constants.STDBY_SUFFIX;
    JSONObject jsonCall = new JSONObject();
    try {
        jsonCall.put(Constants.JSON_CALL_USER, this.username);
        mPubNub.publish(callNumStdBy, jsonCall, new Callback() {
            @Override
            public void successCallback(String channel, Object message) {
                Log.d("MA-dCall", "SUCCESS: " + message.toString());
                Intent intent = new Intent(MainActivity.this, VideoChatActivity.class);
                intent.putExtra(Constants.USER_NAME, username);
                intent.putExtra(Constants.JSON_CALL_USER, callNum);
                startActivity(intent);
            }
        });
    } catch (JSONException e) {
        e.printStackTrace();
    }
}


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    this.mSharedPreferences = getSharedPreferences(Constants.SHARED_PREFS, MODE_PRIVATE);
    // Return to Log In screen if no user is logged in.
    if (!this.mSharedPreferences.contains(Constants.USER_NAME)){
        Intent intent = new Intent(this, LoginActivity.class);
        startActivity(intent);
        finish();
        return;
    }
    this.username = this.mSharedPreferences.getString(Constants.USER_NAME, "");

    this.mCallNumET  = (EditText) findViewById(R.id.call_num);
    this.mUsernameTV = (TextView) findViewById(R.id.main_username);

    this.mUsernameTV.setText(this.username);  // Set the username to the username text view

    //TODO: Create and instance of Pubnub and subscribe to standby channel
    // In pubnub subscribe callback, send user to your VideoActivity
    initPubNub();

}

I will appreciate any help. Thanks to everybody.

Katz
  • 175
  • 2
  • 2
  • 11

1 Answers1

0

when You run your application that time they ask sign in usename that time you wrote your name

put that name in VideoChatActivity class

this.pnRTCClient.listenOn("your name when your wrote at signin time");
this.pnRTCClient.setMaxConnections(3);
Sunny
  • 83
  • 4
  • Hello, I tried this but didn't help..but thanks for answer. Maybe some other idea? – Katz Apr 25 '16 at 13:24
  • Follow this tutorial :- [link](https://github.com/GleasonK/AndroidRTC) it will surly help you and make sure change your username.. – Sunny Apr 26 '16 at 04:43