org.webrtc.VideoTrack Java Examples

The following examples show how to use org.webrtc.VideoTrack. 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: MainActivity.java    From webrtc-android-tutorial with Apache License 2.0 6 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // create PeerConnectionFactory
    PeerConnectionFactory.InitializationOptions initializationOptions =
            PeerConnectionFactory.InitializationOptions.builder(this).createInitializationOptions();
    PeerConnectionFactory.initialize(initializationOptions);
    PeerConnectionFactory peerConnectionFactory = PeerConnectionFactory.builder().createPeerConnectionFactory();

    // create AudioSource
    AudioSource audioSource = peerConnectionFactory.createAudioSource(new MediaConstraints());
    AudioTrack audioTrack = peerConnectionFactory.createAudioTrack("101", audioSource);

    EglBase.Context eglBaseContext = EglBase.create().getEglBaseContext();

    SurfaceTextureHelper surfaceTextureHelper = SurfaceTextureHelper.create("CaptureThread", eglBaseContext);
    // create VideoCapturer
    VideoCapturer videoCapturer = createCameraCapturer();
    VideoSource videoSource = peerConnectionFactory.createVideoSource(videoCapturer.isScreencast());
    videoCapturer.initialize(surfaceTextureHelper, getApplicationContext(), videoSource.getCapturerObserver());
    videoCapturer.startCapture(480, 640, 30);

    SurfaceViewRenderer localView = findViewById(R.id.localView);
    localView.setMirror(true);
    localView.init(eglBaseContext, null);

    // create VideoTrack
    VideoTrack videoTrack = peerConnectionFactory.createVideoTrack("100", videoSource);
    // display in localView
    videoTrack.addSink(localView);
}
 
Example #2
Source File: RespokeCall.java    From respoke-sdk-android with MIT License 6 votes vote down vote up
private void addLocalStreams(Context context) {
    AudioManager audioManager = ((AudioManager) context.getSystemService(Context.AUDIO_SERVICE));
    // TODO(fischman): figure out how to do this Right(tm) and remove the suppression.
    @SuppressWarnings("deprecation")
    boolean isWiredHeadsetOn = audioManager.isWiredHeadsetOn();
    audioManager.setMode(isWiredHeadsetOn ? AudioManager.MODE_IN_CALL : AudioManager.MODE_IN_COMMUNICATION);
    audioManager.setSpeakerphoneOn(!isWiredHeadsetOn);

    localStream = peerConnectionFactory.createLocalMediaStream("ARDAMS");

    if (!audioOnly) {
        VideoCapturer capturer = getVideoCapturer();
        MediaConstraints videoConstraints = new MediaConstraints();
        videoSource = peerConnectionFactory.createVideoSource(capturer, videoConstraints);
        VideoTrack videoTrack = peerConnectionFactory.createVideoTrack("ARDAMSv0", videoSource);
        videoTrack.addRenderer(new VideoRenderer(localRender));
        localStream.addTrack(videoTrack);
    }

    localStream.addTrack(peerConnectionFactory.createAudioTrack("ARDAMSa0", peerConnectionFactory.createAudioSource(new MediaConstraints())));

    peerConnection.addStream(localStream);
}
 
Example #3
Source File: PeerConnectionObserver.java    From iGap-Android with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public void onAddStream(MediaStream stream) {
    for (AudioTrack audioTrack : stream.audioTracks) {
        audioTrack.setEnabled(true);
    }

    if (stream.videoTracks != null && stream.videoTracks.size() == 1) {
        VideoTrack videoTrack = stream.videoTracks.get(0);
        videoTrack.setEnabled(true);

        videoTrack.addSink(new VideoSink() {
            @Override
            public void onFrame(VideoFrame videoFrame) {

                if (G.onVideoCallFrame != null) {
                    G.onVideoCallFrame.onRemoteFrame(videoFrame);
                }
            }
        });

    }


}
 
Example #4
Source File: PeerConnectionChannel.java    From owt-client-android with Apache License 2.0 6 votes vote down vote up
protected void addStream(final MediaStream mediaStream) {
    DCHECK(mediaStream);
    DCHECK(pcExecutor);
    pcExecutor.execute(() -> {
        if (disposed()) {
            return;
        }
        ArrayList<String> streamIds = new ArrayList<>();
        streamIds.add(mediaStream.getId());
        for (AudioTrack audioTrack : mediaStream.audioTracks) {
            RtpSender audioSender = peerConnection.addTrack(audioTrack, streamIds);
            audioRtpSenders.put(mediaStream.getId(), audioSender);
        }
        for (VideoTrack videoTrack : mediaStream.videoTracks) {
            RtpSender videoSender = peerConnection.addTrack(videoTrack, streamIds);
            videoRtpSenders.put(mediaStream.getId(), videoSender);
        }
    });
}
 
Example #5
Source File: VideoConferenceActivity.java    From WebRTCapp with Apache License 2.0 6 votes vote down vote up
public void gotRemoteStream(MediaStream stream, final RemoteParticipant remoteParticipant) {
    final VideoTrack videoTrack = stream.videoTracks.getFirst();
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            remoteRenderer = new VideoRenderer(remoteParticipant.getVideoView());
            remoteParticipant.getVideoView().setVisibility(View.VISIBLE);
            videoTrack.addRenderer(remoteRenderer);
            MediaStream mediaStream = peersManager.getPeerConnectionFactory().createLocalMediaStream("105");
            remoteParticipant.setMediaStream(mediaStream);
            mediaStream.addTrack(peersManager.getLocalAudioTrack());
            mediaStream.addTrack(peersManager.getLocalVideoTrack());
            remoteParticipant.getPeerConnection().removeStream(mediaStream);
            remoteParticipant.getPeerConnection().addStream(mediaStream);
        }
    });
}
 
Example #6
Source File: WebRTCEngine.java    From webrtc_android with MIT License 6 votes vote down vote up
/**
 * 创建本地流
 */
public void createLocalStream() {
    _localStream = _factory.createLocalMediaStream("ARDAMS");
    // 音频
    audioSource = _factory.createAudioSource(createAudioConstraints());
    _localAudioTrack = _factory.createAudioTrack(AUDIO_TRACK_ID, audioSource);
    _localStream.addTrack(_localAudioTrack);

    // 视频
    if (!mIsAudioOnly) {
        captureAndroid = createVideoCapture();
        surfaceTextureHelper = SurfaceTextureHelper.create("CaptureThread", mRootEglBase.getEglBaseContext());
        videoSource = _factory.createVideoSource(captureAndroid.isScreencast());

        captureAndroid.initialize(surfaceTextureHelper, mContext, videoSource.getCapturerObserver());
        captureAndroid.startCapture(VIDEO_RESOLUTION_WIDTH, VIDEO_RESOLUTION_HEIGHT, FPS);


        VideoTrack _localVideoTrack = _factory.createVideoTrack(VIDEO_TRACK_ID, videoSource);
        _localStream.addTrack(_localVideoTrack);
    }

}
 
Example #7
Source File: MainActivity.java    From webrtc-android-tutorial with Apache License 2.0 6 votes vote down vote up
private synchronized PeerConnection getOrCreatePeerConnection(String socketId) {
    PeerConnection peerConnection = peerConnectionMap.get(socketId);
    if(peerConnection != null) {
        return peerConnection;
    }
    peerConnection = peerConnectionFactory.createPeerConnection(iceServers, new PeerConnectionAdapter("PC:" + socketId) {
        @Override
        public void onIceCandidate(IceCandidate iceCandidate) {
            super.onIceCandidate(iceCandidate);
            SignalingClient.get().sendIceCandidate(iceCandidate, socketId);
        }

        @Override
        public void onAddStream(MediaStream mediaStream) {
            super.onAddStream(mediaStream);
            VideoTrack remoteVideoTrack = mediaStream.videoTracks.get(0);
            runOnUiThread(() -> {
                remoteVideoTrack.addSink(remoteViews[remoteViewsIndex++]);
            });
        }
    });
    peerConnection.addStream(mediaStream);
    peerConnectionMap.put(socketId, peerConnection);
    return peerConnection;
}
 
Example #8
Source File: MainActivity.java    From webrtc-android-tutorial with Apache License 2.0 6 votes vote down vote up
private void call() {
    List<PeerConnection.IceServer> iceServers = new ArrayList<>();
    iceServers.add(PeerConnection.IceServer.builder("stun:stun.l.google.com:19302").createIceServer());
    peerConnection = peerConnectionFactory.createPeerConnection(iceServers, new PeerConnectionAdapter("localconnection") {
        @Override
        public void onIceCandidate(IceCandidate iceCandidate) {
            super.onIceCandidate(iceCandidate);
            SignalingClient.get().sendIceCandidate(iceCandidate);
        }

        @Override
        public void onAddStream(MediaStream mediaStream) {
            super.onAddStream(mediaStream);
            VideoTrack remoteVideoTrack = mediaStream.videoTracks.get(0);
            runOnUiThread(() -> {
                remoteVideoTrack.addSink(remoteView);
            });
        }
    });

    peerConnection.addStream(mediaStream);
}
 
Example #9
Source File: WebRtcCallService.java    From bcm-android with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onAddStream(MediaStream stream) {
    ALog.logForSecret(TAG, "onAddStream:" + stream);

    for (AudioTrack audioTrack : stream.audioTracks) {
        audioTrack.setEnabled(true);
    }

    if (stream.videoTracks != null && stream.videoTracks.size() == 1) {
        VideoTrack videoTrack = stream.videoTracks.get(0);
        videoTrack.setEnabled(true);
        videoTrack.addSink(remoteRenderer);
    }
}
 
Example #10
Source File: PeerConnectionClient.java    From voip_android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Nullable
private VideoTrack createVideoTrack(VideoCapturer capturer) {
    surfaceTextureHelper =
            SurfaceTextureHelper.create("CaptureThread", rootEglBase.getEglBaseContext());
    videoSource = factory.createVideoSource(capturer.isScreencast());
    capturer.initialize(surfaceTextureHelper, appContext, videoSource.getCapturerObserver());
    capturer.startCapture(videoWidth, videoHeight, videoFps);

    localVideoTrack = factory.createVideoTrack(VIDEO_TRACK_ID, videoSource);
    localVideoTrack.setEnabled(renderVideo);
    localVideoTrack.addSink(localRender);
    return localVideoTrack;
}
 
Example #11
Source File: PeerConnectionClient.java    From restcomm-android-sdk with GNU Affero General Public License v3.0 5 votes vote down vote up
private VideoTrack createVideoTrack(VideoCapturer capturer) {
  videoSource = factory.createVideoSource(capturer);
  capturer.startCapture(videoWidth, videoHeight, videoFps);

  localVideoTrack = factory.createVideoTrack(VIDEO_TRACK_ID, videoSource);
  localVideoTrack.setEnabled(renderLocalVideo);
  localVideoTrack.addSink(localRender);
  return localVideoTrack;
}
 
Example #12
Source File: RtpSessionActivity.java    From Conversations with GNU General Public License v3.0 5 votes vote down vote up
private static void addSink(final VideoTrack videoTrack, final SurfaceViewRenderer surfaceViewRenderer) {
    try {
        videoTrack.addSink(surfaceViewRenderer);
    } catch (final IllegalStateException e) {
        Log.e(Config.LOGTAG, "possible race condition on trying to display video track. ignoring", e);
    }
}
 
Example #13
Source File: RtpSessionActivity.java    From Conversations with GNU General Public License v3.0 5 votes vote down vote up
private void releaseVideoTracks(final JingleRtpConnection jingleRtpConnection) {
    final Optional<VideoTrack> remoteVideo = jingleRtpConnection.getRemoteVideoTrack();
    if (remoteVideo.isPresent()) {
        remoteVideo.get().removeSink(binding.remoteVideo);
    }
    final Optional<VideoTrack> localVideo = jingleRtpConnection.getLocalVideoTrack();
    if (localVideo.isPresent()) {
        localVideo.get().removeSink(binding.localVideo);
    }
}
 
Example #14
Source File: WebRTCWrapper.java    From Pix-Art-Messenger with GNU General Public License v3.0 5 votes vote down vote up
void setVideoEnabled(final boolean enabled) {
    final VideoTrack videoTrack = this.localVideoTrack;
    if (videoTrack == null) {
        throw new IllegalStateException("Local video track does not exist");
    }
    videoTrack.setEnabled(enabled);
}
 
Example #15
Source File: WebRTCWrapper.java    From Pix-Art-Messenger with GNU General Public License v3.0 5 votes vote down vote up
boolean isVideoEnabled() {
    final VideoTrack videoTrack = this.localVideoTrack;
    if (videoTrack == null) {
        throw new IllegalStateException("Local video track does not exist");
    }
    return videoTrack.enabled();
}
 
Example #16
Source File: RtpSessionActivity.java    From Pix-Art-Messenger with GNU General Public License v3.0 5 votes vote down vote up
private Optional<VideoTrack> getRemoteVideoTrack() {
    final JingleRtpConnection connection = this.rtpConnectionReference != null ? this.rtpConnectionReference.get() : null;
    if (connection == null) {
        return Optional.absent();
    }
    return connection.getRemoteVideoTrack();
}
 
Example #17
Source File: RtpSessionActivity.java    From Pix-Art-Messenger with GNU General Public License v3.0 5 votes vote down vote up
private Optional<VideoTrack> getLocalVideoTrack() {
    final JingleRtpConnection connection = this.rtpConnectionReference != null ? this.rtpConnectionReference.get() : null;
    if (connection == null) {
        return Optional.absent();
    }
    return connection.getLocalVideoTrack();
}
 
Example #18
Source File: RtpSessionActivity.java    From Pix-Art-Messenger with GNU General Public License v3.0 5 votes vote down vote up
private void releaseVideoTracks(final JingleRtpConnection jingleRtpConnection) {
    final Optional<VideoTrack> remoteVideo = jingleRtpConnection.getRemoteVideoTrack();
    if (remoteVideo.isPresent()) {
        remoteVideo.get().removeSink(binding.remoteVideo);
    }
    final Optional<VideoTrack> localVideo = jingleRtpConnection.getLocalVideoTrack();
    if (localVideo.isPresent()) {
        localVideo.get().removeSink(binding.localVideo);
    }
}
 
Example #19
Source File: RtpSessionActivity.java    From Pix-Art-Messenger with GNU General Public License v3.0 5 votes vote down vote up
private static void addSink(final VideoTrack videoTrack, final SurfaceViewRenderer surfaceViewRenderer) {
    try {
        videoTrack.addSink(surfaceViewRenderer);
    } catch (final IllegalStateException e) {
        Log.e(Config.LOGTAG, "possible race condition on trying to display video track. ignoring", e);
    }
}
 
Example #20
Source File: MediaResourceManager.java    From webrtcpeer-android with Apache License 2.0 5 votes vote down vote up
void setVideoEnabled(final boolean enable) {
    executor.execute(new Runnable() {
        @Override
        public void run() {
            renderVideo = enable;
            if (localVideoTrack != null) {
                localVideoTrack.setEnabled(renderVideo);
            }
            for (VideoTrack tv : remoteVideoTracks.values()) {
                tv.setEnabled(renderVideo);
            }
        }
    });
}
 
Example #21
Source File: MediaResourceManager.java    From webrtcpeer-android with Apache License 2.0 5 votes vote down vote up
public void run() {
    Log.d(TAG, "Attaching VideoRenderer to remote stream (" + remoteStream + ")");

    // Check if the remote stream has a video track
    if (remoteStream.videoTracks.size() == 1) {
        // Get the video track
        VideoTrack remoteVideoTrack = remoteStream.videoTracks.get(0);
        // Set video track enabled if we have enabled video rendering
        remoteVideoTrack.setEnabled(renderVideo);

        VideoRenderer videoRenderer = remoteVideoRenderers.get(remoteRender);
        if (videoRenderer != null) {
            MediaStream mediaStream = remoteVideoMediaStreams.get(videoRenderer);
            if (mediaStream != null) {
                VideoTrack videoTrack = remoteVideoTracks.get(mediaStream);
                if (videoTrack != null) {
                    videoTrack.removeRenderer(videoRenderer);
                }
            }
        }

        VideoRenderer newVideoRenderer = new VideoRenderer(remoteRender);
        remoteVideoTrack.addRenderer(newVideoRenderer);
        remoteVideoRenderers.put(remoteRender, newVideoRenderer);
        remoteVideoMediaStreams.put(newVideoRenderer, remoteStream);
        remoteVideoTracks.put(remoteStream, remoteVideoTrack);
        Log.d(TAG, "Attached.");
    }
}
 
Example #22
Source File: MediaResourceManager.java    From webrtcpeer-android with Apache License 2.0 5 votes vote down vote up
private VideoTrack createCapturerVideoTrack(VideoCapturerAndroid capturer) {
    videoSource = factory.createVideoSource(capturer, videoConstraints);
    localVideoTrack = factory.createVideoTrack(VIDEO_TRACK_ID, videoSource);
    localVideoTrack.setEnabled(renderVideo);
    localVideoTrack.addRenderer(new VideoRenderer(localRender));
    return localVideoTrack;
}
 
Example #23
Source File: RtpSessionActivity.java    From Conversations with GNU General Public License v3.0 5 votes vote down vote up
private Optional<VideoTrack> getRemoteVideoTrack() {
    final JingleRtpConnection connection = this.rtpConnectionReference != null ? this.rtpConnectionReference.get() : null;
    if (connection == null) {
        return Optional.absent();
    }
    return connection.getRemoteVideoTrack();
}
 
Example #24
Source File: CallActivity.java    From RTCStartupDemo with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onAddTrack(RtpReceiver rtpReceiver, MediaStream[] mediaStreams) {
    MediaStreamTrack track = rtpReceiver.track();
    if (track instanceof VideoTrack) {
        Log.i(TAG, "onAddVideoTrack");
        VideoTrack remoteVideoTrack = (VideoTrack) track;
        remoteVideoTrack.setEnabled(true);
        ProxyVideoSink videoSink = new ProxyVideoSink();
        videoSink.setTarget(mRemoteSurfaceView);
        remoteVideoTrack.addSink(videoSink);
    }
}
 
Example #25
Source File: WebRTCWrapper.java    From Conversations with GNU General Public License v3.0 5 votes vote down vote up
void setVideoEnabled(final boolean enabled) {
    final VideoTrack videoTrack = this.localVideoTrack;
    if (videoTrack == null) {
        throw new IllegalStateException("Local video track does not exist");
    }
    videoTrack.setEnabled(enabled);
}
 
Example #26
Source File: PeerConnectionClient.java    From sample-videoRTC with Apache License 2.0 5 votes vote down vote up
private VideoTrack createVideoTrack(VideoCapturer capturer) {
  videoSource = factory.createVideoSource(capturer);
  capturer.startCapture(videoWidth, videoHeight, videoFps);

  localVideoTrack = factory.createVideoTrack(VIDEO_TRACK_ID, videoSource);
  localVideoTrack.setEnabled(renderVideo);
  localVideoTrack.addSink(localRender);
  return localVideoTrack;
}
 
Example #27
Source File: MediaStreamFactory.java    From owt-client-android with Apache License 2.0 5 votes vote down vote up
MediaStream createMediaStream(VideoCapturer videoCapturer,
        AudioTrackConstraints audioMediaConstraints) {
    RCHECK(videoCapturer != null || audioMediaConstraints != null);

    String label = UUID.randomUUID().toString();
    MediaStream mediaStream = PCFactoryProxy.instance().createLocalMediaStream(label);

    if (videoCapturer != null) {
        VideoSource videoSource = PCFactoryProxy.instance().createVideoSource(
                videoCapturer.isScreencast());
        SurfaceTextureHelper helper = SurfaceTextureHelper.create("CT", localContext);
        videoCapturer.initialize(helper, ContextInitialization.context,
                videoSource.getCapturerObserver());
        videoCapturer.startCapture(videoCapturer.getWidth(),
                videoCapturer.getHeight(),
                videoCapturer.getFps());
        VideoTrack videoTrack = PCFactoryProxy.instance().createVideoTrack(label + "v0",
                videoSource);
        videoTrack.setEnabled(true);
        mediaStream.addTrack(videoTrack);
        unsharedVideoSources.put(label, videoSource);
    }

    if (audioMediaConstraints != null) {
        if (sharedAudioSource == null) {
            sharedAudioSource = PCFactoryProxy.instance().createAudioSource(
                    audioMediaConstraints.convertToWebRTCConstraints());
        }
        audioSourceRef++;
        mediaStream.addTrack(
                PCFactoryProxy.instance().createAudioTrack(label + "a0", sharedAudioSource));
    }

    return mediaStream;
}
 
Example #28
Source File: WebRTCWrapper.java    From Conversations with GNU General Public License v3.0 5 votes vote down vote up
boolean isVideoEnabled() {
    final VideoTrack videoTrack = this.localVideoTrack;
    if (videoTrack == null) {
        throw new IllegalStateException("Local video track does not exist");
    }
    return videoTrack.enabled();
}
 
Example #29
Source File: PeerConnectionClient.java    From janus-gateway-android with MIT License 5 votes vote down vote up
private VideoTrack createVideoTrack(VideoCapturer capturer) {
  videoSource = factory.createVideoSource(capturer);
  capturer.startCapture(videoWidth, videoHeight, videoFps);

  localVideoTrack = factory.createVideoTrack(VIDEO_TRACK_ID, videoSource);
  localVideoTrack.setEnabled(renderVideo);
  localVideoTrack.addRenderer(new VideoRenderer(localRender));
  return localVideoTrack;
}
 
Example #30
Source File: PeerConnectionClient.java    From Yahala-Messenger with MIT License 5 votes vote down vote up
private VideoTrack createVideoTrack(VideoCapturerAndroid capturer) {
    videoSource = factory.createVideoSource(capturer, videoConstraints);

    localVideoTrack = factory.createVideoTrack(VIDEO_TRACK_ID, videoSource);
    localVideoTrack.setEnabled(renderVideo);
    localVideoTrack.addRenderer(new VideoRenderer(localRender));
    return localVideoTrack;
}