/// <summary> /// Internal callback when a track stops using this source. /// </summary> /// <param name="track">The track not using this source anymore.</param> internal void OnTrackRemovedFromSource(LocalAudioTrack track) { Debug.Assert(!_nativeHandle.IsClosed); bool removed = _tracks.Remove(track); Debug.Assert(removed); }
/// <summary> /// Create an audio track from an existing audio track source. /// /// This does not add the track to any peer connection. Instead, the track must be added manually to /// an audio transceiver to be attached to a peer connection and transmitted to a remote peer. /// </summary> /// <param name="source">The track source which provides the raw audio frames to the newly created track.</param> /// <param name="initConfig">Configuration to initialize the track being created.</param> /// <returns>Asynchronous task completed once the track is created.</returns> public static LocalAudioTrack CreateFromSource(AudioTrackSource source, LocalAudioTrackInitConfig initConfig) { if (source == null) { throw new ArgumentNullException(); } // Parse and marshal the settings string trackName = initConfig?.trackName; if (string.IsNullOrEmpty(trackName)) { trackName = Guid.NewGuid().ToString(); } var config = new LocalAudioTrackInterop.TrackInitConfig { TrackName = trackName }; // Create interop wrappers var track = new LocalAudioTrack(trackName); // Create native implementation objects uint res = LocalAudioTrackInterop.LocalAudioTrack_CreateFromSource(in config, source._nativeHandle, out LocalAudioTrackHandle trackHandle); Utils.ThrowOnErrorCode(res); // Finish creating the track, and bind it to the source track.FinishCreate(trackHandle, source); return(track); }
/// <summary> /// Create an audio track from a local audio capture device (microphone). /// This does not add the track to any peer connection. Instead, the track must be added manually to /// an audio transceiver to be attached to a peer connection and transmitted to a remote peer. /// </summary> /// <param name="settings">Settings to initialize the local audio track.</param> /// <returns>Asynchronous task completed once the device is capturing and the track is created.</returns> /// <remarks> /// On UWP this requires the "microphone" capability. /// See <see href="https://docs.microsoft.com/en-us/windows/uwp/packaging/app-capability-declarations"/> /// for more details. /// </remarks> public static Task <LocalAudioTrack> CreateFromDeviceAsync(LocalAudioTrackSettings settings = null) { return(Task.Run(() => { // On UWP this cannot be called from the main UI thread, so always call it from // a background worker thread. string trackName = settings?.trackName; if (string.IsNullOrEmpty(trackName)) { trackName = Guid.NewGuid().ToString(); } // Create interop wrappers var track = new LocalAudioTrack(trackName); // Parse settings var config = new PeerConnectionInterop.LocalAudioTrackInteropInitConfig(track, settings); // Create native implementation objects uint res = LocalAudioTrackInterop.LocalAudioTrack_CreateFromDevice(config, trackName, out LocalAudioTrackHandle trackHandle); Utils.ThrowOnErrorCode(res); track.SetHandle(trackHandle); return track; })); }
/// <summary> /// Internal callback when a track starts using this source. /// </summary> /// <param name="track">The track using this source.</param> internal void OnTrackAddedToSource(LocalAudioTrack track) { Debug.Assert(!_nativeHandle.IsClosed); Debug.Assert(!_tracks.Contains(track)); _tracks.Add(track); }