3

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?

4
  • 2
    see WebRTC: How to add stream after offer and answer?. TL;NR: you need an external mechanism of communication between peers that will involve an answer from the second peer that the new stream can be added. An alternative is to start with existing (but empty) video stream. Now it will be easier to fill this stream with content when (and if) necessary.
    – Alex Cohn
    Oct 2, 2015 at 21:15
  • 1
    Yes, I think your second alternative is what I will do. That strategy didn't work in a past attempt because of issues with our extern communication mechanism, but now that that is fixed, the empty video solution works, and is much simpler than trying to renegotiate the connection. Thanks!
    – synthcat
    Oct 2, 2015 at 21:33
  • @synthcat Do you have sample code available? I'm trying to addTrack in middle of connection. Can you please post your answer? That would be really helpful.
    – Vijay E
    Nov 26, 2020 at 5:19
  • did you find a solution?
    – hogan
    Oct 20, 2021 at 6:52

1 Answer 1

1

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.

2
  • Can you please post any reference or code so that it can be helpful for us?
    – Vijay E
    Nov 25, 2020 at 6:25
  • @VijayE, sorry I don't have such code at hand at this time. Maybe the TS still has a sample available.
    – Alex Cohn
    Nov 25, 2020 at 16:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.