org.webrtc.AudioSource Java Examples

The following examples show how to use org.webrtc.AudioSource. 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: VideoChatHelper.java    From Socket.io-FLSocketIM-Android with MIT License 5 votes vote down vote up
private void createLocalStream() {
    localMediaStream = factory.createLocalMediaStream("ARDAMS");

    // 音频
    AudioSource audioSource = factory.createAudioSource(new MediaConstraints());
    localMediaStream.addTrack(factory.createAudioTrack("ARDAMSa0", audioSource));

    // 视频
    String frontCameraDeviceName = VideoCapturerAndroid.getNameOfFrontFacingDevice();
    VideoCapturer capture = VideoCapturerAndroid.create(frontCameraDeviceName);
    VideoSource videoSource = factory.createVideoSource(capture, localVideoConstraints());
    localMediaStream.addTrack(factory.createVideoTrack("ARDAMSv0", videoSource));

    callBack.onSetLocalStream(localMediaStream, myId);
}
 
Example #3
Source File: PeersManager.java    From WebRTCapp with Apache License 2.0 5 votes vote down vote up
public void start() {
    PeerConnectionFactory.initializeAndroidGlobals(activity, true);

    PeerConnectionFactory.Options options = new PeerConnectionFactory.Options();
    peerConnectionFactory = new PeerConnectionFactory(options);

    videoGrabberAndroid = createVideoGrabber();
    MediaConstraints constraints = new MediaConstraints();

    VideoSource videoSource = peerConnectionFactory.createVideoSource(videoGrabberAndroid);
    localVideoTrack = peerConnectionFactory.createVideoTrack("100", videoSource);

    AudioSource audioSource = peerConnectionFactory.createAudioSource(constraints);
    localAudioTrack = peerConnectionFactory.createAudioTrack("101", audioSource);

    if (videoGrabberAndroid != null) {
        videoGrabberAndroid.startCapture(1000, 1000, 30);
    }

    localRenderer = new VideoRenderer(localVideoView);
    localVideoTrack.addRenderer(localRenderer);

    MediaConstraints sdpConstraints = new MediaConstraints();
    sdpConstraints.mandatory.add(new MediaConstraints.KeyValuePair("offerToReceiveAudio", "true"));
    sdpConstraints.mandatory.add(new MediaConstraints.KeyValuePair("offerToReceiveVideo", "true"));

    createLocalPeerConnection(sdpConstraints);
}
 
Example #4
Source File: MainActivity.java    From webrtc-android-tutorial with Apache License 2.0 4 votes vote down vote up
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        EglBase.Context eglBaseContext = EglBase.create().getEglBaseContext();

        // create PeerConnectionFactory
        PeerConnectionFactory.initialize(PeerConnectionFactory.InitializationOptions
                .builder(this)
                .createInitializationOptions());
        PeerConnectionFactory.Options options = new PeerConnectionFactory.Options();
        DefaultVideoEncoderFactory defaultVideoEncoderFactory =
                new DefaultVideoEncoderFactory(eglBaseContext, true, true);
        DefaultVideoDecoderFactory defaultVideoDecoderFactory =
                new DefaultVideoDecoderFactory(eglBaseContext);
        peerConnectionFactory = PeerConnectionFactory.builder()
                .setOptions(options)
                .setVideoEncoderFactory(defaultVideoEncoderFactory)
                .setVideoDecoderFactory(defaultVideoDecoderFactory)
                .createPeerConnectionFactory();

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

        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);


        remoteView = findViewById(R.id.remoteView);
        remoteView.setMirror(false);
        remoteView.init(eglBaseContext, null);


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

        mediaStream = peerConnectionFactory.createLocalMediaStream("mediaStream");
        mediaStream.addTrack(videoTrack);
        mediaStream.addTrack(audioTrack);

        SignalingClient.get().setCallback(this);
        call();
    }
 
Example #5
Source File: CallActivity.java    From RTCStartupDemo with GNU General Public License v3.0 4 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_call);

    mLogcatView = findViewById(R.id.LogcatView);
    mStartCallBtn = findViewById(R.id.StartCallButton);
    mEndCallBtn = findViewById(R.id.EndCallButton);

    RTCSignalClient.getInstance().setSignalEventListener(mOnSignalEventListener);

    String serverAddr = getIntent().getStringExtra("ServerAddr");
    String roomName = getIntent().getStringExtra("RoomName");
    RTCSignalClient.getInstance().joinRoom(serverAddr, UUID.randomUUID().toString(), roomName);

    mRootEglBase = EglBase.create();

    mLocalSurfaceView = findViewById(R.id.LocalSurfaceView);
    mRemoteSurfaceView = findViewById(R.id.RemoteSurfaceView);

    mLocalSurfaceView.init(mRootEglBase.getEglBaseContext(), null);
    mLocalSurfaceView.setScalingType(RendererCommon.ScalingType.SCALE_ASPECT_FILL);
    mLocalSurfaceView.setMirror(true);
    mLocalSurfaceView.setEnableHardwareScaler(false /* enabled */);

    mRemoteSurfaceView.init(mRootEglBase.getEglBaseContext(), null);
    mRemoteSurfaceView.setScalingType(RendererCommon.ScalingType.SCALE_ASPECT_FILL);
    mRemoteSurfaceView.setMirror(true);
    mRemoteSurfaceView.setEnableHardwareScaler(true /* enabled */);
    mRemoteSurfaceView.setZOrderMediaOverlay(true);

    ProxyVideoSink videoSink = new ProxyVideoSink();
    videoSink.setTarget(mLocalSurfaceView);

    mPeerConnectionFactory = createPeerConnectionFactory(this);

    // NOTE: this _must_ happen while PeerConnectionFactory is alive!
    Logging.enableLogToDebugOutput(Logging.Severity.LS_VERBOSE);

    mVideoCapturer = createVideoCapturer();

    mSurfaceTextureHelper = SurfaceTextureHelper.create("CaptureThread", mRootEglBase.getEglBaseContext());
    VideoSource videoSource = mPeerConnectionFactory.createVideoSource(false);
    mVideoCapturer.initialize(mSurfaceTextureHelper, getApplicationContext(), videoSource.getCapturerObserver());

    mVideoTrack = mPeerConnectionFactory.createVideoTrack(VIDEO_TRACK_ID, videoSource);
    mVideoTrack.setEnabled(true);
    mVideoTrack.addSink(videoSink);

    AudioSource audioSource = mPeerConnectionFactory.createAudioSource(new MediaConstraints());
    mAudioTrack = mPeerConnectionFactory.createAudioTrack(AUDIO_TRACK_ID, audioSource);
    mAudioTrack.setEnabled(true);
}
 
Example #6
Source File: WebRTC.java    From iGap-Android with GNU Affero General Public License v3.0 4 votes vote down vote up
private void addAudioTrack(MediaStream mediaStream) {
    AudioSource audioSource = peerConnectionFactoryInstance().createAudioSource(audioConstraintsGetInstance());
    AudioTrack audioTrack = peerConnectionFactoryInstance().createAudioTrack("ARDAMSa0", audioSource);
    audioTrack.setEnabled(true);
    mediaStream.addTrack(audioTrack);
}
 
Example #7
Source File: MainActivity.java    From krankygeek with MIT License 4 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    AudioManager audioManager = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);
    audioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
    audioManager.setSpeakerphoneOn(true);

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

    peerConnectionFactory = new PeerConnectionFactory();

    VideoCapturerAndroid vc = VideoCapturerAndroid.create(VideoCapturerAndroid.getNameOfFrontFacingDevice(), null);

    localVideoSource = peerConnectionFactory.createVideoSource(vc, new MediaConstraints());
    VideoTrack localVideoTrack = peerConnectionFactory.createVideoTrack(VIDEO_TRACK_ID, localVideoSource);
    localVideoTrack.setEnabled(true);

    AudioSource audioSource = peerConnectionFactory.createAudioSource(new MediaConstraints());
    AudioTrack localAudioTrack = peerConnectionFactory.createAudioTrack(AUDIO_TRACK_ID, audioSource);
    localAudioTrack.setEnabled(true);

    localMediaStream = peerConnectionFactory.createLocalMediaStream(LOCAL_STREAM_ID);
    localMediaStream.addTrack(localVideoTrack);
    localMediaStream.addTrack(localAudioTrack);

    GLSurfaceView videoView = (GLSurfaceView) findViewById(R.id.glview_call);

    VideoRendererGui.setView(videoView, null);
    try {
        otherPeerRenderer = VideoRendererGui.createGui(0, 0, 100, 100, VideoRendererGui.ScalingType.SCALE_ASPECT_FILL, true);
        VideoRenderer renderer = VideoRendererGui.createGui(50, 50, 50, 50, VideoRendererGui.ScalingType.SCALE_ASPECT_FILL, true);
        localVideoTrack.addRenderer(renderer);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example #8
Source File: VideoChatActivity.java    From AndroidRTC with MIT License 4 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_video_chat);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

    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, "");
    this.mChatList     = getListView();
    this.mChatEditText = (EditText) findViewById(R.id.chat_input);
    this.mCallStatus   = (TextView) findViewById(R.id.call_status);

    // Set up the List View for chatting
    List<ChatMessage> ll = new LinkedList<ChatMessage>();
    mChatAdapter = new ChatAdapter(this, ll);
    mChatList.setAdapter(mChatAdapter);

    // First, we initiate the PeerConnectionFactory with our application context and some options.
    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);
    List<PeerConnection.IceServer> servers = getXirSysIceServers();
    if (!servers.isEmpty()){
        this.pnRTCClient.setSignalParams(new PnSignalingParams());
    }

    // Returns the number of cams & front/back face device name
    int camNumber = VideoCapturerAndroid.getDeviceCount();
    String frontFacingCam = VideoCapturerAndroid.getNameOfFrontFacingDevice();
    String backFacingCam = VideoCapturerAndroid.getNameOfBackFacingDevice();

    // Creates a VideoCapturerAndroid instance for the device name
    VideoCapturer capturer = VideoCapturerAndroid.create(frontFacingCam);

    // First create a Video Source, then we can make a Video Track
    localVideoSource = pcFactory.createVideoSource(capturer, this.pnRTCClient.videoConstraints());
    VideoTrack localVideoTrack = pcFactory.createVideoTrack(VIDEO_TRACK_ID, localVideoSource);

    // First we create an AudioSource then we can create our AudioTrack
    AudioSource audioSource = pcFactory.createAudioSource(this.pnRTCClient.audioConstraints());
    AudioTrack localAudioTrack = pcFactory.createAudioTrack(AUDIO_TRACK_ID, audioSource);

    // To create our VideoRenderer, we can use the included VideoRendererGui for simplicity
    // First we need to set the GLSurfaceView that it should render to
    this.videoView = (GLSurfaceView) findViewById(R.id.gl_surface);

    // Then we set that view, and pass a Runnable to run once the surface is ready
    VideoRendererGui.setView(videoView, null);

    // Now that VideoRendererGui is ready, we can get our VideoRenderer.
    // IN THIS ORDER. Effects which is on top or bottom
    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);

    // We start out with an empty MediaStream object, created with help from our PeerConnectionFactory
    //  Note that LOCAL_MEDIA_STREAM_ID can be any string
    MediaStream mediaStream = pcFactory.createLocalMediaStream(LOCAL_MEDIA_STREAM_ID);

    // Now we can add our tracks.
    mediaStream.addTrack(localVideoTrack);
    mediaStream.addTrack(localAudioTrack);

    // First attach the RTC Listener so that callback events will be triggered
    this.pnRTCClient.attachRTCListener(new DemoRTCListener());

    // Then attach your local media stream to the PnRTCClient.
    //  This will trigger the onLocalStream callback.
    this.pnRTCClient.attachLocalMediaStream(mediaStream);

    // Listen on a channel. This is your "phone number," also set the max chat users.
    this.pnRTCClient.listenOn("Kevin");
    this.pnRTCClient.setMaxConnections(1);

    // If the intent contains a number to dial, call it now that you are connected.
    //  Else, remain listening for a call.
    if (extras.containsKey(Constants.CALL_USER)) {
        String callUser = extras.getString(Constants.CALL_USER, "");
        connectToUser(callUser);
    }
}
 
Example #9
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 #10
Source File: AndroidAudioSource.java    From actor-platform with GNU Affero General Public License v3.0 4 votes vote down vote up
public AudioSource getAudioSource() {
    return audioSource;
}
 
Example #11
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;
}