org.webrtc.CameraVideoCapturer Java Examples

The following examples show how to use org.webrtc.CameraVideoCapturer. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: WebRTCEngine.java    From webrtc_android with MIT License 7 votes vote down vote up
@Override
public void switchCamera() {
    if (isSwitch) return;
    isSwitch = true;
    if (captureAndroid == null) return;
    if (captureAndroid instanceof CameraVideoCapturer) {
        CameraVideoCapturer cameraVideoCapturer = (CameraVideoCapturer) captureAndroid;
        try {
            cameraVideoCapturer.switchCamera(new CameraVideoCapturer.CameraSwitchHandler() {
                @Override
                public void onCameraSwitchDone(boolean isFrontCamera) {
                    isSwitch = false;
                }

                @Override
                public void onCameraSwitchError(String errorDescription) {
                    isSwitch = false;
                }
            });
        } catch (Exception e) {
            isSwitch = false;
        }
    } else {
        Log.d(TAG, "Will not switch camera, video caputurer is not a camera");
    }
}
 
Example #2
Source File: Camera.java    From mollyim-android with GNU General Public License v3.0 7 votes vote down vote up
public Camera(@NonNull Context             context,
              @NonNull CameraEventListener cameraEventListener,
              @NonNull EglBase             eglBase)
{
  this.context                = context;
  this.cameraEventListener    = cameraEventListener;
  this.eglBase                = eglBase;
  CameraEnumerator enumerator = getCameraEnumerator(context);
  cameraCount                 = enumerator.getDeviceNames().length;

  CameraVideoCapturer capturerCandidate = createVideoCapturer(enumerator, FRONT);
  if (capturerCandidate != null) {
    activeDirection = FRONT;
  } else {
    capturerCandidate = createVideoCapturer(enumerator, BACK);
    if (capturerCandidate != null) {
      activeDirection = BACK;
    } else {
      activeDirection = NONE;
    }
  }
  capturer = capturerCandidate;
}
 
Example #3
Source File: WebRTCWrapper.java    From Conversations with GNU General Public License v3.0 6 votes vote down vote up
ListenableFuture<Boolean> switchCamera() {
    final CapturerChoice capturerChoice = this.capturerChoice;
    if (capturerChoice == null) {
        return Futures.immediateFailedFuture(new IllegalStateException("CameraCapturer has not been initialized"));
    }
    final SettableFuture<Boolean> future = SettableFuture.create();
    capturerChoice.cameraVideoCapturer.switchCamera(new CameraVideoCapturer.CameraSwitchHandler() {
        @Override
        public void onCameraSwitchDone(boolean isFrontCamera) {
            capturerChoice.isFrontCamera = isFrontCamera;
            future.set(isFrontCamera);
        }

        @Override
        public void onCameraSwitchError(final String message) {
            future.setException(new IllegalStateException(String.format("Unable to switch camera %s", message)));
        }
    });
    return future;
}
 
Example #4
Source File: PeerConnectionWrapper.java    From bcm-android with GNU General Public License v3.0 6 votes vote down vote up
Camera(@NonNull Context context, @NonNull CameraEventListener cameraEventListener) {
    this.cameraEventListener = cameraEventListener;
    CameraEnumerator enumerator = getCameraEnumerator(context);
    cameraCount = enumerator.getDeviceNames().length;

    CameraVideoCapturer cameraVideoCapturer = createVideoCapturer(enumerator, FRONT);
    if (cameraVideoCapturer != null) {
        activeDirection = FRONT;
    } else  {
        cameraVideoCapturer = createVideoCapturer(enumerator, BACK);
        if (cameraVideoCapturer != null) {
            activeDirection = BACK;
        } else {
            activeDirection = NONE;
        }
    }

    capturer = cameraVideoCapturer;
}
 
Example #5
Source File: WebRTCWrapper.java    From Pix-Art-Messenger with GNU General Public License v3.0 6 votes vote down vote up
ListenableFuture<Boolean> switchCamera() {
    final CapturerChoice capturerChoice = this.capturerChoice;
    if (capturerChoice == null) {
        return Futures.immediateFailedFuture(new IllegalStateException("CameraCapturer has not been initialized"));
    }
    final SettableFuture<Boolean> future = SettableFuture.create();
    capturerChoice.cameraVideoCapturer.switchCamera(new CameraVideoCapturer.CameraSwitchHandler() {
        @Override
        public void onCameraSwitchDone(boolean isFrontCamera) {
            capturerChoice.isFrontCamera = isFrontCamera;
            future.set(isFrontCamera);
        }

        @Override
        public void onCameraSwitchError(final String message) {
            future.setException(new IllegalStateException(String.format("Unable to switch camera %s", message)));
        }
    });
    return future;
}
 
Example #6
Source File: RTCCall.java    From Meshenger with GNU General Public License v3.0 5 votes vote down vote up
private CameraVideoCapturer createCapturer() {
    CameraEnumerator enumerator = new Camera1Enumerator();
    for (String name : enumerator.getDeviceNames()) {
        if (enumerator.isFrontFacing(name)) {
            return enumerator.createCapturer(name, null);
        }
    }
    return null;
}
 
Example #7
Source File: ConversationCallFragment.java    From q-municate-android with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    QBRTCSession currentSession = ((CallActivity) getActivity()).getCurrentSession();
    if (currentSession == null) {
        return super.onOptionsItemSelected(item);
    }

    final QBMediaStreamManager mediaStreamManager = currentSession.getMediaStreamManager();
    if (mediaStreamManager == null) {
        return super.onOptionsItemSelected(item);
    }

    switch (item.getItemId()) {
        case R.id.switch_camera_toggle:
            mediaStreamManager.switchCameraInput(new CameraVideoCapturer.CameraSwitchHandler() {
                @Override
                public void onCameraSwitchDone(boolean b) {
                    isFrontCameraSelected = b;
                    toggleCameraInternal();
                }

                @Override
                public void onCameraSwitchError(String s) {

                }
            });
            break;
        case R.id.switch_speaker_toggle:
            toggleAudioOutput();
            break;
        default:
            return super.onOptionsItemSelected(item);
    }

    getActivity().invalidateOptionsMenu();
    return super.onOptionsItemSelected(item);
}
 
Example #8
Source File: PeerConnectionClient.java    From voip_android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void switchCameraInternal() {
    if (videoCapturer instanceof CameraVideoCapturer) {
        if (!isVideoCallEnabled() || isError || videoCapturer == null) {
            Log.e(TAG, "Failed to switch camera. Video: " + isVideoCallEnabled() + ". Error : " + isError);
            return; // No video is sent or only one camera is available or error happened.
        }
        Log.d(TAG, "Switch camera");
        CameraVideoCapturer cameraVideoCapturer = (CameraVideoCapturer) videoCapturer;
        cameraVideoCapturer.switchCamera(null);
    } else {
        Log.d(TAG, "Will not switch camera, video caputurer is not a camera");
    }
}
 
Example #9
Source File: PeerConnectionClient.java    From restcomm-android-sdk with GNU Affero General Public License v3.0 5 votes vote down vote up
private void switchCameraInternal() {
  if (videoCapturer instanceof CameraVideoCapturer) {
    if (!videoCallEnabled || isError) {
      Log.e(TAG, "Failed to switch camera. Video: " + videoCallEnabled + ". Error : " + isError);
      return; // No video is sent or only one camera is available or error happened.
    }
    Log.d(TAG, "Switch camera");
    CameraVideoCapturer cameraVideoCapturer = (CameraVideoCapturer) videoCapturer;
    cameraVideoCapturer.switchCamera(null);
  } else {
    Log.d(TAG, "Will not switch camera, video caputurer is not a camera");
  }
}
 
Example #10
Source File: WebRTCWrapper.java    From Conversations with GNU General Public License v3.0 5 votes vote down vote up
@Nullable
private static CapturerChoice of(CameraEnumerator enumerator, final String deviceName, Set<String> availableCameras) {
    final CameraVideoCapturer capturer = enumerator.createCapturer(deviceName, null);
    if (capturer == null) {
        return null;
    }
    final ArrayList<CameraEnumerationAndroid.CaptureFormat> choices = new ArrayList<>(enumerator.getSupportedFormats(deviceName));
    Collections.sort(choices, (a, b) -> b.width - a.width);
    for (final CameraEnumerationAndroid.CaptureFormat captureFormat : choices) {
        if (captureFormat.width <= CAPTURING_RESOLUTION) {
            return new CapturerChoice(capturer, captureFormat, availableCameras);
        }
    }
    return null;
}
 
Example #11
Source File: WebRTCWrapper.java    From Pix-Art-Messenger with GNU General Public License v3.0 5 votes vote down vote up
@Nullable
private static CapturerChoice of(CameraEnumerator enumerator, final String deviceName, Set<String> availableCameras) {
    final CameraVideoCapturer capturer = enumerator.createCapturer(deviceName, null);
    if (capturer == null) {
        return null;
    }
    final ArrayList<CameraEnumerationAndroid.CaptureFormat> choices = new ArrayList<>(enumerator.getSupportedFormats(deviceName));
    Collections.sort(choices, (a, b) -> b.width - a.width);
    for (final CameraEnumerationAndroid.CaptureFormat captureFormat : choices) {
        if (captureFormat.width <= CAPTURING_RESOLUTION) {
            return new CapturerChoice(capturer, captureFormat, availableCameras);
        }
    }
    return null;
}
 
Example #12
Source File: WebRTC.java    From iGap-Android with GNU Affero General Public License v3.0 5 votes vote down vote up
public void switchCamera() {
    if (Camera.getNumberOfCameras() > 1) {
        if (videoCapturer instanceof CameraVideoCapturer) {
            ((CameraVideoCapturer) videoCapturer).switchCamera(null);
        }
    }
}
 
Example #13
Source File: PeerConnectionClient.java    From janus-gateway-android with MIT License 5 votes vote down vote up
private void switchCameraInternal() {
  if (videoCapturer instanceof CameraVideoCapturer) {
    Log.d(TAG, "Switch camera");
    CameraVideoCapturer cameraVideoCapturer = (CameraVideoCapturer) videoCapturer;
    cameraVideoCapturer.switchCamera(null);
  } else {
    Log.d(TAG, "Will not switch camera, video caputurer is not a camera");
  }
}
 
Example #14
Source File: PeerConnectionClient.java    From sample-videoRTC with Apache License 2.0 5 votes vote down vote up
private void switchCameraInternal() {
  if (videoCapturer instanceof CameraVideoCapturer) {
    if (!videoCallEnabled || isError) {
      Log.e(TAG, "Failed to switch camera. Video: " + videoCallEnabled + ". Error : " + isError);
      return; // No video is sent or only one camera is available or error happened.
    }
    Log.d(TAG, "Switch camera");
    CameraVideoCapturer cameraVideoCapturer = (CameraVideoCapturer) videoCapturer;
    cameraVideoCapturer.switchCamera(null);
  } else {
    Log.d(TAG, "Will not switch camera, video caputurer is not a camera");
  }
}
 
Example #15
Source File: PeerConnectionWrapper.java    From bcm-android with GNU General Public License v3.0 5 votes vote down vote up
private @Nullable CameraVideoCapturer createVideoCapturer(@NonNull CameraEnumerator enumerator, @NonNull CameraState.Direction direction) {
    String[] deviceNames = enumerator.getDeviceNames();
    for (String deviceName : deviceNames) {
        if ((direction == FRONT && enumerator.isFrontFacing(deviceName)) || (direction == BACK  && enumerator.isBackFacing(deviceName))) {
            return enumerator.createCapturer(deviceName, null);
        }
    }

    return null;
}
 
Example #16
Source File: RTCCall.java    From meshenger-android with GNU General Public License v3.0 5 votes vote down vote up
private CameraVideoCapturer createCapturer() {
    CameraEnumerator enumerator = new Camera1Enumerator();
    for (String name : enumerator.getDeviceNames()) {
        if (enumerator.isFrontFacing(name)) {
            return enumerator.createCapturer(name, null);
        }
    }
    return null;
}
 
Example #17
Source File: Camera.java    From mollyim-android with GNU General Public License v3.0 5 votes vote down vote up
private @Nullable CameraVideoCapturer createVideoCapturer(@NonNull CameraEnumerator enumerator,
                                                          @NonNull CameraState.Direction direction)
{
  String[] deviceNames = enumerator.getDeviceNames();
  for (String deviceName : deviceNames) {
    if ((direction == FRONT && enumerator.isFrontFacing(deviceName)) ||
        (direction == BACK  && enumerator.isBackFacing(deviceName)))
      {
        return enumerator.createCapturer(deviceName, null);
      }
  }

  return null;
}
 
Example #18
Source File: VideoSource.java    From VideoCRE with MIT License 4 votes vote down vote up
public void switchCamera() {
    if (mVideoCapturer instanceof CameraVideoCapturer) {
        ((CameraVideoCapturer) mVideoCapturer).switchCamera(null);
    }
}
 
Example #19
Source File: WebRTCWrapper.java    From Conversations with GNU General Public License v3.0 4 votes vote down vote up
synchronized void initializePeerConnection(final Set<Media> media, final List<PeerConnection.IceServer> iceServers) throws InitializationException {
    Preconditions.checkState(this.eglBase != null);
    Preconditions.checkNotNull(media);
    Preconditions.checkArgument(media.size() > 0, "media can not be empty when initializing peer connection");
    final boolean setUseHardwareAcousticEchoCanceler = WebRtcAudioEffects.canUseAcousticEchoCanceler() && !HARDWARE_AEC_BLACKLIST.contains(Build.MODEL);
    Log.d(Config.LOGTAG, String.format("setUseHardwareAcousticEchoCanceler(%s) model=%s", setUseHardwareAcousticEchoCanceler, Build.MODEL));
    PeerConnectionFactory peerConnectionFactory = PeerConnectionFactory.builder()
            .setVideoDecoderFactory(new DefaultVideoDecoderFactory(eglBase.getEglBaseContext()))
            .setVideoEncoderFactory(new DefaultVideoEncoderFactory(eglBase.getEglBaseContext(), true, true))
            .setAudioDeviceModule(JavaAudioDeviceModule.builder(context)
                    .setUseHardwareAcousticEchoCanceler(setUseHardwareAcousticEchoCanceler)
                    .createAudioDeviceModule()
            )
            .createPeerConnectionFactory();


    final MediaStream stream = peerConnectionFactory.createLocalMediaStream("my-media-stream");

    final Optional<CapturerChoice> optionalCapturerChoice = media.contains(Media.VIDEO) ? getVideoCapturer() : Optional.absent();

    if (optionalCapturerChoice.isPresent()) {
        this.capturerChoice = optionalCapturerChoice.get();
        final CameraVideoCapturer capturer = this.capturerChoice.cameraVideoCapturer;
        final VideoSource videoSource = peerConnectionFactory.createVideoSource(false);
        SurfaceTextureHelper surfaceTextureHelper = SurfaceTextureHelper.create("webrtc", eglBase.getEglBaseContext());
        capturer.initialize(surfaceTextureHelper, requireContext(), videoSource.getCapturerObserver());
        Log.d(Config.LOGTAG, String.format("start capturing at %dx%d@%d", capturerChoice.captureFormat.width, capturerChoice.captureFormat.height, capturerChoice.getFrameRate()));
        capturer.startCapture(capturerChoice.captureFormat.width, capturerChoice.captureFormat.height, capturerChoice.getFrameRate());

        this.localVideoTrack = peerConnectionFactory.createVideoTrack("my-video-track", videoSource);

        stream.addTrack(this.localVideoTrack);
    }


    if (media.contains(Media.AUDIO)) {
        //set up audio track
        final AudioSource audioSource = peerConnectionFactory.createAudioSource(new MediaConstraints());
        this.localAudioTrack = peerConnectionFactory.createAudioTrack("my-audio-track", audioSource);
        stream.addTrack(this.localAudioTrack);
    }


    final PeerConnection.RTCConfiguration rtcConfig = new PeerConnection.RTCConfiguration(iceServers);
    rtcConfig.tcpCandidatePolicy = PeerConnection.TcpCandidatePolicy.DISABLED; //XEP-0176 doesn't support tcp
    rtcConfig.continualGatheringPolicy = PeerConnection.ContinualGatheringPolicy.GATHER_CONTINUALLY;
    final PeerConnection peerConnection = peerConnectionFactory.createPeerConnection(rtcConfig, peerConnectionObserver);
    if (peerConnection == null) {
        throw new InitializationException("Unable to create PeerConnection");
    }
    peerConnection.addStream(stream);
    peerConnection.setAudioPlayout(true);
    peerConnection.setAudioRecording(true);
    this.peerConnection = peerConnection;
}
 
Example #20
Source File: WebRTCWrapper.java    From Conversations with GNU General Public License v3.0 4 votes vote down vote up
CapturerChoice(CameraVideoCapturer cameraVideoCapturer, CameraEnumerationAndroid.CaptureFormat captureFormat, Set<String> cameras) {
    this.cameraVideoCapturer = cameraVideoCapturer;
    this.captureFormat = captureFormat;
    this.availableCameras = cameras;
}
 
Example #21
Source File: WebRTCWrapper.java    From Pix-Art-Messenger with GNU General Public License v3.0 4 votes vote down vote up
CapturerChoice(CameraVideoCapturer cameraVideoCapturer, CameraEnumerationAndroid.CaptureFormat captureFormat, Set<String> cameras) {
    this.cameraVideoCapturer = cameraVideoCapturer;
    this.captureFormat = captureFormat;
    this.availableCameras = cameras;
}
 
Example #22
Source File: WebRTCWrapper.java    From Pix-Art-Messenger with GNU General Public License v3.0 4 votes vote down vote up
synchronized void initializePeerConnection(final Set<Media> media, final List<PeerConnection.IceServer> iceServers) throws InitializationException {
    Preconditions.checkState(this.eglBase != null);
    Preconditions.checkNotNull(media);
    Preconditions.checkArgument(media.size() > 0, "media can not be empty when initializing peer connection");
    final boolean setUseHardwareAcousticEchoCanceler = WebRtcAudioEffects.canUseAcousticEchoCanceler() && !HARDWARE_AEC_BLACKLIST.contains(Build.MODEL);
    Log.d(Config.LOGTAG, String.format("setUseHardwareAcousticEchoCanceler(%s) model=%s", setUseHardwareAcousticEchoCanceler, Build.MODEL));
    PeerConnectionFactory peerConnectionFactory = PeerConnectionFactory.builder()
            .setVideoDecoderFactory(new DefaultVideoDecoderFactory(eglBase.getEglBaseContext()))
            .setVideoEncoderFactory(new DefaultVideoEncoderFactory(eglBase.getEglBaseContext(), true, true))
            .setAudioDeviceModule(JavaAudioDeviceModule.builder(context)
                    .setUseHardwareAcousticEchoCanceler(setUseHardwareAcousticEchoCanceler)
                    .createAudioDeviceModule()
            )
            .createPeerConnectionFactory();


    final MediaStream stream = peerConnectionFactory.createLocalMediaStream("my-media-stream");

    final Optional<CapturerChoice> optionalCapturerChoice = media.contains(Media.VIDEO) ? getVideoCapturer() : Optional.absent();

    if (optionalCapturerChoice.isPresent()) {
        this.capturerChoice = optionalCapturerChoice.get();
        final CameraVideoCapturer capturer = this.capturerChoice.cameraVideoCapturer;
        final VideoSource videoSource = peerConnectionFactory.createVideoSource(false);
        SurfaceTextureHelper surfaceTextureHelper = SurfaceTextureHelper.create("webrtc", eglBase.getEglBaseContext());
        capturer.initialize(surfaceTextureHelper, requireContext(), videoSource.getCapturerObserver());
        Log.d(Config.LOGTAG, String.format("start capturing at %dx%d@%d", capturerChoice.captureFormat.width, capturerChoice.captureFormat.height, capturerChoice.getFrameRate()));
        capturer.startCapture(capturerChoice.captureFormat.width, capturerChoice.captureFormat.height, capturerChoice.getFrameRate());

        this.localVideoTrack = peerConnectionFactory.createVideoTrack("my-video-track", videoSource);

        stream.addTrack(this.localVideoTrack);
    }


    if (media.contains(Media.AUDIO)) {
        //set up audio track
        final AudioSource audioSource = peerConnectionFactory.createAudioSource(new MediaConstraints());
        this.localAudioTrack = peerConnectionFactory.createAudioTrack("my-audio-track", audioSource);
        stream.addTrack(this.localAudioTrack);
    }


    final PeerConnection.RTCConfiguration rtcConfig = new PeerConnection.RTCConfiguration(iceServers);
    rtcConfig.tcpCandidatePolicy = PeerConnection.TcpCandidatePolicy.DISABLED; //XEP-0176 doesn't support tcp
    rtcConfig.continualGatheringPolicy = PeerConnection.ContinualGatheringPolicy.GATHER_CONTINUALLY;
    final PeerConnection peerConnection = peerConnectionFactory.createPeerConnection(rtcConfig, peerConnectionObserver);
    if (peerConnection == null) {
        throw new InitializationException("Unable to create PeerConnection");
    }
    peerConnection.addStream(stream);
    peerConnection.setAudioPlayout(true);
    peerConnection.setAudioRecording(true);
    this.peerConnection = peerConnection;
}
 
Example #23
Source File: PeerConnectionWrapper.java    From bcm-android with GNU General Public License v3.0 4 votes vote down vote up
@Nullable CameraVideoCapturer getCapturer() {
    return capturer;
}
 
Example #24
Source File: Camera.java    From mollyim-android with GNU General Public License v3.0 4 votes vote down vote up
@Override
public @NonNull CameraVideoCapturer createCapturer(@Nullable String deviceName,
                                                   @Nullable CameraVideoCapturer.CameraEventsHandler eventsHandler)
{
  return new Camera2Capturer(context, deviceName, eventsHandler, new FilteredCamera2Enumerator(context));
}
 
Example #25
Source File: Camera.java    From mollyim-android with GNU General Public License v3.0 4 votes vote down vote up
@Nullable CameraVideoCapturer getCapturer() {
  return capturer;
}