Ejemplo n.º 1
0
        /// <summary>
        /// Create the local sender track from the current media track source if that source
        /// is active and enabled. Otherwise do nothing.
        /// </summary>
        private void CreateSenderIfNeeded()
        {
            // Only create a sender track if the source is active, i.e. has an underlying frame source.
            if (_senderTrack == null && _source != null && _source.isActiveAndEnabled)
            {
                if (MediaKind == MediaKind.Audio)
                {
                    var audioSource = (_source as AudioTrackSource);

                    var initConfig = new LocalAudioTrackInitConfig
                    {
                        trackName = _senderTrackName
                    };
                    _senderTrack = LocalAudioTrack.CreateFromSource(audioSource.Source, initConfig);
                }
                else
                {
                    Debug.Assert(MediaKind == MediaKind.Video);
                    var videoSource = (_source as VideoTrackSource);

                    var initConfig = new LocalVideoTrackInitConfig
                    {
                        trackName = _senderTrackName
                    };
                    _senderTrack = LocalVideoTrack.CreateFromSource(videoSource.Source, initConfig);
                }
            }
        }
Ejemplo n.º 2
0
        public async Task Initialization(string user)
        {
            try
            {
                var config = new PeerConnectionConfiguration
                {
                    IceServers = new List <IceServer> {
                        new IceServer {
                            Urls = { "stun:stun.l.google.com:19302" }
                        }
                    }
                };
                await Connection.InitializeAsync(config);

                microphoneSource = await DeviceAudioTrackSource.CreateAsync();

                var audioTrackConfig = new LocalAudioTrackInitConfig {
                    trackName = "microphone_track"
                };
                localAudioTrack  = LocalAudioTrack.CreateFromSource(microphoneSource, audioTrackConfig);
                audioTransceiver = Connection.AddTransceiver(MediaKind.Audio);
                audioTransceiver.LocalAudioTrack  = localAudioTrack;
                audioTransceiver.DesiredDirection = Transceiver.Direction.SendReceive;

                Console.WriteLine("Peer connection initialized.");
            }
            catch (Exception e)
            {
                await Log.WriteAsync(e.Message);

                Console.WriteLine(e);
                throw;
            }
        }
Ejemplo n.º 3
0
        public Room ConnectToRoom(string roomName, string accessToken)
        {
            try
            {
                Console.WriteLine("Trying to connect to room " + roomName);
                localAudioTrack = LocalAudioTrack.Create(this, true);
                CameraCapturer cameraCapturer = new CameraCapturer(this, CameraCapturer.CameraSource.FrontCamera);

                // Create a video track
                LocalVideoTrack localVideoTrack = LocalVideoTrack.Create(this, true, cameraCapturer);

                //primaryVideoView.SetMirror(true);
                //localVideoTrack.AddRenderer(primaryVideoView);

                ConnectOptions connectOptions = new ConnectOptions.Builder(accessToken)
                                                .RoomName(roomName)
                                                .AudioTracks(new List <LocalAudioTrack> {
                    localAudioTrack
                })
                                                .VideoTracks(new List <LocalVideoTrack> {
                    localVideoTrack
                })
                                                .Build();

                return(Video.Connect(this, connectOptions, this.roomListener));
            }
            catch (Exception e)
            {
                Console.WriteLine("Error: trying to connect to room " + e.Message);
                Toast.MakeText(this, e.Message, ToastLength.Long).Show();
                return(null);
            }
        }
        public MediaPlayerViewModel()
        {
            _videoPlayer.CurrentStateChanged += OnMediaStateChanged;
            _videoPlayer.MediaOpened         += OnMediaOpened;
            _videoPlayer.MediaFailed         += OnMediaFailed;
            _videoPlayer.MediaEnded          += OnMediaEnded;
            _videoPlayer.RealTimePlayback     = true;
            _videoPlayer.AutoPlay             = false;

            AudioTrackTypeList.Add(new AudioTrackTypeViewModel
            {
                DisplayName = "Local microphone (default device)",
                Factory     = async() =>
                {
                    return(await LocalAudioTrack.CreateFromDeviceAsync());
                }
            });

            VideoTrackTypeList.Add(new VideoTrackTypeViewModel
            {
                DisplayName = "Local webcam (default device)",
                Factory     = async() =>
                {
                    return(await LocalVideoTrack.CreateFromDeviceAsync());
                }
            });

            _videoStatsTimer.Interval = TimeSpan.FromMilliseconds(300);
            _videoStatsTimer.Tick    += (_1, _2) => UpdateVideoStats();
        }
Ejemplo n.º 5
0
        protected override async Task CreateLocalAudioTrackAsyncImpl()
        {
            if (Track == null)
            {
                // Ensure the track has a valid name
                string trackName = TrackName;
                if (trackName.Length == 0)
                {
                    trackName = Guid.NewGuid().ToString();
                    // Re-assign the generated track name for consistency
                    TrackName = trackName;
                }
                SdpTokenAttribute.Validate(trackName, allowEmpty: false);

                // Create the local track
                var trackSettings = new LocalAudioTrackSettings
                {
                    trackName = trackName
                };
                Track = await LocalAudioTrack.CreateFromDeviceAsync(trackSettings);

                // Synchronize the track status with the Unity component status
                Track.Enabled = enabled;
            }
        }
Ejemplo n.º 6
0
        public async Task AddAudioTrackFromDeviceAsync(string trackName)
        {
            const string DefaultAudioDeviceName = "Default audio device";

            await RequestMediaAccessAsync(StreamingCaptureMode.Audio);

            // FIXME - this leaks 'source', never disposed (and is the track itself disposed??)
            var initConfig = new LocalAudioDeviceInitConfig();
            var source     = await AudioTrackSource.CreateFromDeviceAsync(initConfig);

            var settings = new LocalAudioTrackInitConfig
            {
                trackName = trackName
            };
            var track = LocalAudioTrack.CreateFromSource(source, settings);

            SessionModel.Current.AudioTracks.Add(new AudioTrackViewModel
            {
                Source     = source,
                Track      = track,
                TrackImpl  = track,
                IsRemote   = false,
                DeviceName = DefaultAudioDeviceName
            });
            SessionModel.Current.LocalTracks.Add(new TrackViewModel(Symbol.Volume)
            {
                DisplayName = DefaultAudioDeviceName
            });
        }
Ejemplo n.º 7
0
        private void CreateSender()
        {
            Debug.Assert(_senderTrack == null);
            if (MediaKind == MediaKind.Audio)
            {
                var audioSource = (AudioTrackSource)_source;

                var initConfig = new LocalAudioTrackInitConfig
                {
                    trackName = _senderTrackName
                };
                _senderTrack = LocalAudioTrack.CreateFromSource(audioSource.Source, initConfig);
            }
            else
            {
                Debug.Assert(MediaKind == MediaKind.Video);
                var videoSource = (VideoTrackSource)_source;

                var initConfig = new LocalVideoTrackInitConfig
                {
                    trackName = _senderTrackName
                };
                _senderTrack = LocalVideoTrack.CreateFromSource(videoSource.Source, initConfig);
            }
        }
Ejemplo n.º 8
0
        protected override void OnDestroy()
        {
            if (room != null && room.State != RoomState.Disconnected)
            {
                room.Disconnect();
                disconnectedFromOnDestroy = true;
            }

            /*
             * Release the local audio and video tracks ensuring any memory allocated to audio
             * or video is freed.
             */
            if (localAudioTrack != null)
            {
                localAudioTrack.Release();
                localAudioTrack = null;
            }
            if (localVideoTrack != null)
            {
                localVideoTrack.Release();
                localVideoTrack = null;
            }

            base.OnDestroy();
        }
Ejemplo n.º 9
0
 public void AddAudioTrack(LocalAudioTrack track, string deviceName)
 {
     ThreadHelper.EnsureIsMainThread();
     AudioTracks.Add(new AudioTrackViewModel(track, deviceName));
     LocalTracks.Add(new LocalTrackViewModel(Symbol.Volume)
     {
         DisplayName = deviceName
     });
 }
Ejemplo n.º 10
0
    void CreateLocalMedia(Context context)
    {
        _audioManager = (AudioManager)context.GetSystemService(Context.AudioService);
        var cameraSource = Cameras.HasFrontCamera()
            ? CameraCapturer.CameraSource.FrontCamera
            : CameraCapturer.CameraSource.BackCamera;

        VideoCapturer     = new CameraCapturer(context, cameraSource);
        CurrentVideoTrack = LocalVideoTrack.Create(context, true, VideoCapturer);
        CurrentAudioTrack = LocalAudioTrack.Create(context, true);
    }
Ejemplo n.º 11
0
        private void CreateAudioAndVideoTracks()
        {
            // Share your microphone
            localAudioTrack = LocalAudioTrack.Create(this, true);

            // Share your camera
            cameraCapturerCompat = new CameraCapturerCompat(this, CameraCapturer.CameraSource.FrontCamera);
            localVideoTrack      = LocalVideoTrack.Create(this, true, cameraCapturerCompat.GetVideoCapturer());
            primaryVideoView.SetMirror(true);
            localVideoTrack.AddRenderer(primaryVideoView);
            localVideoView = primaryVideoView;
        }
Ejemplo n.º 12
0
        private void createLocalMedia()
        {
            localMedia = LocalMedia.create(this);

            // Share your microphone
            localAudioTrack = localMedia.addAudioTrack(true);

            // Share your camera
            cameraCapturer          = new CameraCapturer(this, CameraCapturer.CameraSource.FRONT_CAMERA);
            localVideoTrack         = localMedia.addVideoTrack(true, cameraCapturer);
            primaryVideoView.Mirror = true;
            localVideoTrack.addRenderer(primaryVideoView);
            localVideoView = primaryVideoView;
        }
Ejemplo n.º 13
0
        public async Task CreateFromSource()
        {
            using (AudioTrackSource source = await DeviceAudioTrackSource.CreateAsync())
            {
                Assert.IsNotNull(source);

                var settings = new LocalAudioTrackInitConfig {
                    trackName = "track_name"
                };
                using (LocalAudioTrack track = LocalAudioTrack.CreateFromSource(source, settings))
                {
                    Assert.IsNotNull(track);
                }
            }
        }
Ejemplo n.º 14
0
        private void PrepareLocalMedia()
        {
            // We will offer local audio and video when we connect to room.

            // Adding local audio track to localMedia
            if (localAudioTrack == null)
            {
                localAudioTrack = localMedia.AddAudioTrack(true);
            }

            // Adding local video track to localMedia and starting local preview if it is not already started.
            //if (localMedia.VideoTracks.Length == 0) {
            //	StartPreview ();
            //}
        }
Ejemplo n.º 15
0
        public static async Task AddAudioTrackFromDeviceAsync(string trackName)
        {
            const string DefaultAudioDeviceName = "Default audio device";

            await Utils.RequestMediaAccessAsync(StreamingCaptureMode.Audio);

            // FIXME - this leaks 'source', never disposed (and is the track itself disposed??)
            var initConfig = new LocalAudioDeviceInitConfig();
            var source     = await DeviceAudioTrackSource.CreateAsync(initConfig);

            var settings = new LocalAudioTrackInitConfig
            {
                trackName = trackName
            };
            var track = LocalAudioTrack.CreateFromSource(source, settings);

            SessionModel.Current.AddAudioTrack(track, DefaultAudioDeviceName);
        }
        private void CreateLocalMedia(Context context)
        {
            try
            {
                AudioManager = (AudioManager)context.GetSystemService(Context.AudioService);
                AudioManager.SpeakerphoneOn = TypeCall != TypeCall.Audio;

                VideoCapturer = new CameraCapturer(context, GetFrontCameraId());

                VideoFormat videoConstraints = new VideoFormat(VideoDimensions.Hd720pVideoDimensions, 30);

                CurrentVideoTrack = LocalVideoTrack.Create(context, true, VideoCapturer, videoConstraints, "camera");
                CurrentAudioTrack = LocalAudioTrack.Create(context, true, "mic");
            }
            catch (Exception e)
            {
                Methods.DisplayReportResultTrack(e);
            }
        }
Ejemplo n.º 17
0
        /// <summary>
        /// Re-implement this callback to destroy the <see cref="Track"/> instance
        /// and other associated resources.
        /// </summary>
        protected virtual void DestroyLocalAudioTrack()
        {
            if (Track != null)
            {
                // Track may not be added to any transceiver (e.g. no connection), or the
                // transceiver is about to be destroyed so the DetachFromTransceiver() already
                // cleared it.
                var transceiver = Transceiver;
                if (transceiver != null)
                {
                    if (transceiver.LocalAudioTrack != null)
                    {
                        Debug.Assert(transceiver.LocalAudioTrack == Track);
                        transceiver.LocalAudioTrack = null;
                    }
                }

                // Local tracks are disposable objects owned by the user (this component)
                Track.Dispose();
                Track = null;
            }
        }
Ejemplo n.º 18
0
        private void CreateLocalMedia(Context context)
        {
            try
            {
                AudioManager = (AudioManager)context.GetSystemService(Context.AudioService);
                AudioManager.SpeakerphoneOn = TypeCall != TypeCall.Audio;

                var cameraSource = (CameraCapturer.IsSourceAvailable(CameraCapturer.CameraSource.FrontCamera)) ? (CameraCapturer.CameraSource.FrontCamera) : (CameraCapturer.CameraSource.BackCamera);
                VideoCapturer = new CameraCapturer(context, cameraSource);

                VideoConstraints videoConstraints = new VideoConstraints.Builder()
                                                    .MaxFps(5)
                                                    .MaxVideoDimensions(new VideoDimensions(50, 50))
                                                    .Build();

                CurrentVideoTrack = LocalVideoTrack.Create(context, true, VideoCapturer, videoConstraints, "camera");
                CurrentAudioTrack = LocalAudioTrack.Create(context, true, "mic");
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }
Ejemplo n.º 19
0
        public async Task AddAudioTrackFromDeviceAsync(string trackName)
        {
            const string DefaultAudioDeviceName = "Default audio device";

            await RequestMediaAccessAsync(StreamingCaptureMode.Audio);

            var settings = new LocalAudioTrackSettings
            {
                trackName = trackName
            };
            var track = await LocalAudioTrack.CreateFromDeviceAsync(settings);

            SessionModel.Current.AudioTracks.Add(new AudioTrackViewModel
            {
                Track      = track,
                TrackImpl  = track,
                IsRemote   = false,
                DeviceName = DefaultAudioDeviceName
            });
            SessionModel.Current.LocalTracks.Add(new TrackViewModel(Symbol.Volume)
            {
                DisplayName = DefaultAudioDeviceName
            });
        }
Ejemplo n.º 20
0
        public MediaPlayerViewModel()
        {
            _videoPlayer.CurrentStateChanged += OnMediaStateChanged;
            _videoPlayer.MediaOpened         += OnMediaOpened;
            _videoPlayer.MediaFailed         += OnMediaFailed;
            _videoPlayer.MediaEnded          += OnMediaEnded;
            _videoPlayer.RealTimePlayback     = true;
            _videoPlayer.AutoPlay             = false;

            AudioTrackTypeList.Add(new AudioTrackTypeViewModel
            {
                DisplayName = "Local microphone (default device)",
                Factory     = async() =>
                {
                    // FIXME - this leaks 'source', never disposed (and is the track itself disposed??)
                    var source   = await DeviceAudioTrackSource.CreateAsync();
                    var settings = new LocalAudioTrackInitConfig();
                    return(LocalAudioTrack.CreateFromSource(source, settings));
                }
            });

            VideoTrackTypeList.Add(new VideoTrackTypeViewModel
            {
                DisplayName = "Local webcam (default device)",
                Factory     = async() =>
                {
                    // FIXME - this leaks 'source', never disposed (and is the track itself disposed??)
                    var source   = await DeviceVideoTrackSource.CreateAsync();
                    var settings = new LocalVideoTrackInitConfig();
                    return(LocalVideoTrack.CreateFromSource(source, settings));
                }
            });

            _videoStatsTimer.Interval = TimeSpan.FromMilliseconds(300);
            _videoStatsTimer.Tick    += (_1, _2) => UpdateVideoStats();
        }
Ejemplo n.º 21
0
        public async Task <string> InitiateCallRTC()
        {
            var list = new List <string>();

            list.Add(this.Configuration.GetSection("Key")["iceServer"]);
            AudioTrackSource microphoneSource = null;
            LocalAudioTrack  localAudioTrack  = null;
            Transceiver      audioTransceiver = null;

            var iceServer = new IceServer
            {
                Urls         = list,
                TurnPassword = this.Configuration.GetSection("Key")["turnPwd"],
                TurnUserName = this.Configuration.GetSection("Key")["turnUser"]
            };

            var serverList = new List <IceServer>();

            serverList.Add(iceServer);
            var connectionConfig = new PeerConnectionConfiguration {
                IceServers       = serverList,
                IceTransportType = IceTransportType.All,
                BundlePolicy     = BundlePolicy.Balanced,
                SdpSemantic      = SdpSemantic.UnifiedPlan
            };
            var connection = new PeerConnection();
            await connection.InitializeAsync(connectionConfig);

            microphoneSource = await DeviceAudioTrackSource.CreateAsync();

            var audioTrackConfig = new LocalAudioTrackInitConfig
            {
                trackName = "microphone_track"
            };

            localAudioTrack = LocalAudioTrack.CreateFromSource(microphoneSource, audioTrackConfig);

            audioTransceiver = connection.AddTransceiver(MediaKind.Audio);
            audioTransceiver.LocalAudioTrack  = localAudioTrack;
            audioTransceiver.DesiredDirection = Transceiver.Direction.SendReceive;

            var signaler = new NamedPipeSignaler.NamedPipeSignaler(connection, "testpipe");

            connection.Connected += () => {
                Console.WriteLine("PeerConnection: connected.");
            };

            signaler.SdpMessageReceived += async(SdpMessage message) =>
            {
                // Note: we use 'await' to ensure the remote description is applied
                // before calling CreateAnswer(). Failing to do so will prevent the
                // answer from being generated, and the connection from establishing.
                await connection.SetRemoteDescriptionAsync(message);

                if (message.Type == SdpMessageType.Offer)
                {
                    connection.CreateAnswer();
                }
            };

            await signaler.StartAsync();

            signaler.IceCandidateReceived += (IceCandidate candidate) => {
                connection.AddIceCandidate(candidate);
            };

            connection.IceStateChanged += (IceConnectionState newState) => {
                Console.WriteLine($"ICE state: {newState}");
            };

            if (signaler.IsClient)
            {
                Console.WriteLine("Connecting to remote peer...");
                connection.CreateOffer();
            }
            else
            {
                Console.WriteLine("Waiting for offer from remote peer...");
            }

            return(connection.IsConnected + "-" + connection.Name + "-");
        }
Ejemplo n.º 22
0
        public CallHandler(ICall statefulCall, PeerConnection peerConnection)
            : base(TimeSpan.FromMinutes(10), statefulCall?.GraphLogger)
        {
            this.Call = statefulCall;

            this.callHandlerVideo = new CallHandlerVideo(this.Call);
            this.callHandlerAudio = new CallHandlerAudio(this.Call);

            this.peerConnection = peerConnection;

            this.peerConnection.VideoTrackAdded   += this.callHandlerVideo.OnClientVideoTrackAdded;
            this.peerConnection.VideoTrackRemoved += this.callHandlerVideo.OnClientVideoTrackRemoved;

            this.peerConnection.AudioTrackAdded   += this.callHandlerAudio.OnClientAudioTrackAdded;
            this.peerConnection.AudioTrackRemoved += this.callHandlerAudio.OnClientAudioTrackRemoved;

            TransceiverInitSettings transceiverInitSettings = new TransceiverInitSettings();

            transceiverInitSettings.InitialDesiredDirection = Transceiver.Direction.Inactive;

            if (this.peerConnection.AssociatedTransceivers.ToList().Count != 0)
            {
                this.teamsAudioTransceiver             = this.peerConnection.AssociatedTransceivers.ToList()[0];
                this.callHandlerAudio.clientAudioTrack = this.peerConnection.AssociatedTransceivers.ToList()[0].RemoteAudioTrack;
                this.callHandlerAudio.clientAudioTrack.AudioFrameReady += this.callHandlerAudio.OnClientAudioReceived;

                this.teamsVideoTransceiver             = this.peerConnection.AssociatedTransceivers.ToList()[1];
                this.callHandlerVideo.clientVideoTrack = this.peerConnection.AssociatedTransceivers.ToList()[1].RemoteVideoTrack;
                this.callHandlerVideo.clientVideoTrack.I420AVideoFrameReady += this.callHandlerVideo.OnClientVideoReceived;
            }
            else
            {
                this.teamsAudioTransceiver = this.peerConnection.AddTransceiver(MediaKind.Audio, transceiverInitSettings);
                this.teamsVideoTransceiver = this.peerConnection.AddTransceiver(MediaKind.Video, transceiverInitSettings);
            }

            LocalVideoTrack teamsVideoTrack = LocalVideoTrack.CreateFromExternalSource("TeamsVideoTrack",
                                                                                       ExternalVideoTrackSource.CreateFromI420ACallback(this.callHandlerVideo.CustomI420AFrameCallback));

            this.teamsVideoTransceiver.LocalVideoTrack = teamsVideoTrack;

            this.teamsVideoTransceiver.DesiredDirection = Transceiver.Direction.SendReceive;

            LocalAudioTrack teamsAudioTrack = LocalAudioTrack.CreateFromExternalSource("TeamsAudioTrack",
                                                                                       ExternalAudioTrackSource.CreateFromCallback(this.callHandlerAudio.CustomAudioFrameCallback));

            this.teamsAudioTransceiver.LocalAudioTrack = teamsAudioTrack;

            this.teamsAudioTransceiver.DesiredDirection = Transceiver.Direction.SendReceive;

            this.Call.OnUpdated += this.OnCallUpdated;
            if (this.Call.GetLocalMediaSession() != null)
            {
                this.Call.GetLocalMediaSession().AudioSocket.DominantSpeakerChanged += this.OnDominantSpeakerChanged;
                this.Call.GetLocalMediaSession().VideoSocket.VideoMediaReceived     += this.callHandlerVideo.OnTeamsVideoReceived;
                this.Call.GetLocalMediaSession().AudioSocket.AudioMediaReceived     += this.callHandlerAudio.OnTeamsAudioReceived;
            }

            this.Call.Participants.OnUpdated += this.OnParticipantsUpdated;
            this.endCallTimer           = new Timer(CallHandler.WaitForMs);
            this.endCallTimer.Enabled   = false;
            this.endCallTimer.AutoReset = false;
            this.endCallTimer.Elapsed  += this.OnTimerElapsed;
        }
Ejemplo n.º 23
0
        static async Task Main(string[] args)
        {
            Transceiver      audioTransceiver = null;
            Transceiver      videoTransceiver = null;
            AudioTrackSource audioTrackSource = null;
            VideoTrackSource videoTrackSource = null;
            LocalAudioTrack  localAudioTrack  = null;
            LocalVideoTrack  localVideoTrack  = null;

            try
            {
                bool needVideo = Array.Exists(args, arg => (arg == "-v") || (arg == "--video"));
                bool needAudio = Array.Exists(args, arg => (arg == "-a") || (arg == "--audio"));

                // Asynchronously retrieve a list of available video capture devices (webcams).
                var deviceList = await PeerConnection.GetVideoCaptureDevicesAsync();

                // For example, print them to the standard output
                foreach (var device in deviceList)
                {
                    Console.WriteLine($"Found webcam {device.name} (id: {device.id})");
                }

                // Create a new peer connection automatically disposed at the end of the program
                using var pc = new PeerConnection();

                // Initialize the connection with a STUN server to allow remote access
                var config = new PeerConnectionConfiguration
                {
                    IceServers = new List <IceServer> {
                        new IceServer {
                            Urls = { "stun:stun.l.google.com:19302" }
                        }
                    }
                };
                await pc.InitializeAsync(config);

                Console.WriteLine("Peer connection initialized.");

                // Record video from local webcam, and send to remote peer
                if (needVideo)
                {
                    Console.WriteLine("Opening local webcam...");
                    videoTrackSource = await DeviceVideoTrackSource.CreateAsync();

                    Console.WriteLine("Create local video track...");
                    var trackSettings = new LocalVideoTrackInitConfig {
                        trackName = "webcam_track"
                    };
                    localVideoTrack = LocalVideoTrack.CreateFromSource(videoTrackSource, trackSettings);

                    Console.WriteLine("Create video transceiver and add webcam track...");
                    videoTransceiver = pc.AddTransceiver(MediaKind.Video);
                    videoTransceiver.DesiredDirection = Transceiver.Direction.SendReceive;
                    videoTransceiver.LocalVideoTrack  = localVideoTrack;
                }

                // Record audio from local microphone, and send to remote peer
                if (needAudio)
                {
                    Console.WriteLine("Opening local microphone...");
                    audioTrackSource = await DeviceAudioTrackSource.CreateAsync();

                    Console.WriteLine("Create local audio track...");
                    var trackSettings = new LocalAudioTrackInitConfig {
                        trackName = "mic_track"
                    };
                    localAudioTrack = LocalAudioTrack.CreateFromSource(audioTrackSource, trackSettings);

                    Console.WriteLine("Create audio transceiver and add mic track...");
                    audioTransceiver = pc.AddTransceiver(MediaKind.Audio);
                    audioTransceiver.DesiredDirection = Transceiver.Direction.SendReceive;
                    audioTransceiver.LocalAudioTrack  = localAudioTrack;
                }

                // Setup signaling
                Console.WriteLine("Starting signaling...");
                var signaler = new NamedPipeSignaler.NamedPipeSignaler(pc, "testpipe");
                signaler.SdpMessageReceived += async(SdpMessage message) =>
                {
                    await pc.SetRemoteDescriptionAsync(message);

                    if (message.Type == SdpMessageType.Offer)
                    {
                        pc.CreateAnswer();
                    }
                };
                signaler.IceCandidateReceived += (IceCandidate candidate) =>
                {
                    pc.AddIceCandidate(candidate);
                };
                await signaler.StartAsync();

                // Start peer connection
                pc.Connected       += () => { Console.WriteLine("PeerConnection: connected."); };
                pc.IceStateChanged += (IceConnectionState newState) => { Console.WriteLine($"ICE state: {newState}"); };
                int numFrames = 0;
                pc.VideoTrackAdded += (RemoteVideoTrack track) =>
                {
                    track.I420AVideoFrameReady += (I420AVideoFrame frame) =>
                    {
                        ++numFrames;
                        if (numFrames % 60 == 0)
                        {
                            Console.WriteLine($"Received video frames: {numFrames}");
                        }
                    };
                };
                if (signaler.IsClient)
                {
                    Console.WriteLine("Connecting to remote peer...");
                    pc.CreateOffer();
                }
                else
                {
                    Console.WriteLine("Waiting for offer from remote peer...");
                }

                Console.WriteLine("Press a key to stop recording...");
                Console.ReadKey(true);

                signaler.Stop();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }

            localAudioTrack?.Dispose();
            localVideoTrack?.Dispose();

            Console.WriteLine("Program termined.");

            localAudioTrack.Dispose();
            localVideoTrack.Dispose();
            audioTrackSource.Dispose();
            videoTrackSource.Dispose();
        }
Ejemplo n.º 24
0
    async void OnClientConnected()
    {
        var pc = signaler.PeerConnection;

        // Record video from local webcam, and send to remote peer
        if (NeedVideo)
        {
            // For example, print them to the standard output

            var deviceSettings = new LocalVideoDeviceInitConfig
            {
                width  = VideoWidth,
                height = VideoHeight,
            };
            if (VideoFps > 0)
            {
                deviceSettings.framerate = VideoFps;
            }
            if (VideoProfileId.Length > 0)
            {
                deviceSettings.videoProfileId = VideoProfileId;
            }

            Debug.Log($"Attempt to grab Camera - {deviceSettings.videoProfileId}: {deviceSettings.width}x{deviceSettings.height}@{deviceSettings.framerate}fps");
            videoTrackSource = await DeviceVideoTrackSource.CreateAsync(deviceSettings);

            Debug.Log($"Create local video track... {videoTrackSource}");
            var trackSettings = new LocalVideoTrackInitConfig
            {
                trackName = "webcam_track"
            };
            localVideoTrack = LocalVideoTrack.CreateFromSource(videoTrackSource, trackSettings);

            Debug.Log("Create video transceiver and add webcam track...");
            videoTransceiver = pc.AddTransceiver(MediaKind.Video);
            videoTransceiver.DesiredDirection = Transceiver.Direction.SendReceive;
            videoTransceiver.LocalVideoTrack  = localVideoTrack;
        }

        // Record audio from local microphone, and send to remote peer
        if (NeedAudio)
        {
            Debug.Log("Opening local microphone...");
            audioTrackSource = await DeviceAudioTrackSource.CreateAsync();

            Debug.Log("Create local audio track...");
            var trackSettings = new LocalAudioTrackInitConfig {
                trackName = "mic_track"
            };
            localAudioTrack = LocalAudioTrack.CreateFromSource(audioTrackSource, trackSettings);

            Debug.Log("Create audio transceiver and add mic track...");
            audioTransceiver = pc.AddTransceiver(MediaKind.Audio);
            audioTransceiver.DesiredDirection = Transceiver.Direction.SendReceive;
            audioTransceiver.LocalAudioTrack  = localAudioTrack;
        }

        // Start peer connection
        int numFrames = 0;

        pc.VideoTrackAdded += (RemoteVideoTrack track) =>
        {
            Debug.Log($"Attach Frame Listener...");
            track.I420AVideoFrameReady += (I420AVideoFrame frame) =>
            {
                ++numFrames;
                if (numFrames % 60 == 0)
                {
                    Debug.Log($"Received video frames: {numFrames}");
                }
            };
        };
        // we need a short delay here for the video stream to settle...
        // I assume my Logitech webcam is sending some garbage frames in the beginning.
        await Task.Delay(200);

        pc.CreateOffer();
        Debug.Log("Send offer to remote peer");
    }
 private void PrepareLocalMedia()
 {
     localMedia      = new LocalMedia();
     localAudioTrack = localMedia.AddAudioTrack(true);
 }
 public AudioTrackViewModel(LocalAudioTrack track, string deviceName)
     : base(track, deviceName)
 {
 }