synthcat
synthcat

Reputation: 302

Android native webrtc: add video after already connected

I have successfully been running WebRTC in my Android app for a while, using libjingle.so and PeerConnectionClient.java, etc., from Google's code library. However, I am now running into a problem where a user starts a connection as audio only (i.e., an audio call), but then toggles video on. I augmented the existing setVideoEnabled() in PeerConnectionClient as such:

 public void setVideoEnabled(final boolean enable) {
executor.execute(new Runnable() {
  @Override
  public void run() {
    renderVideo = enable;
    if (localVideoTrack != null) {
      localVideoTrack.setEnabled(renderVideo);
    } else {
      if (renderVideo) {
          //AC: create a video track
          String cameraDeviceName = VideoCapturerAndroid.getDeviceName(0);
          String frontCameraDeviceName =
                  VideoCapturerAndroid.getNameOfFrontFacingDevice();
          if (numberOfCameras > 1 && frontCameraDeviceName != null) {
              cameraDeviceName = frontCameraDeviceName;
          }
          Log.i(TAG, "Opening camera: " + cameraDeviceName);
          videoCapturer = VideoCapturerAndroid.create(cameraDeviceName);
          if (createVideoTrack(videoCapturer) != null) {                
              mediaStream.addTrack(localVideoTrack);
              localVideoTrack.setEnabled(renderVideo);
              peerConnection.addStream(mediaStream);
          } else {
              Log.d(TAG, "Local video track is still null");
          }
      } else {
        Log.d(TAG, "Local video track is null");
      }
    }
    if (remoteVideoTrack != null) {
      remoteVideoTrack.setEnabled(renderVideo);
    } else {
      Log.d(TAG,"Remote video track is null");
    }
  }
});

}

This allows me successfully see a local inset of the device's video camera, but it doesn't send the video to the remove client. I thought the peerConnection.addStream() call would do that, but perhaps I am missing something else?

Upvotes: 3

Views: 2017

Answers (1)

Alex Cohn
Alex Cohn

Reputation: 57173

To avoid building an external mechanism of communication between peers that will involve an answer from the second peer that the new stream can be added, you can always start with existing (but sometimes empty) video stream. Now it is just the matter of filling this stream with content when (and if) necessary.

Upvotes: 1

Related Questions