예제 #1
0
        /// <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);
        }
예제 #2
0
        internal void FinishCreate(LocalAudioTrackHandle handle, AudioTrackSource source)
        {
            Debug.Assert(!handle.IsClosed);
            // Either first-time assign or no-op (assign same value again)
            Debug.Assert(_nativeHandle.IsInvalid || (_nativeHandle == handle));
            if (_nativeHandle != handle)
            {
                _nativeHandle = handle;
                RegisterInteropCallbacks();
            }

            Debug.Assert(source != null);
            Source = source;
            source.OnTrackAddedToSource(this);
        }