Example #1
0
        public async Task ReplaceTrackAsync(IMediaStreamTrack track)
        {
            if (Closed)
            {
                // This must be done here. Otherwise there is no chance to stop the given track.
                if (track is not null && _stopTracks)
                {
                    try { track.Stop(); }
                    catch { }
                }
                throw new Exception("closed");
            }
            else if (track is not null && track.ReadyState == MediaStreamTrackState.Ended)
            {
                throw new Exception("track ended");
            }

            // Do nothing if this is the same track as the current handled one.
            if (track == Track)
            {
                Console.WriteLine("replaceTrack() | same track, ignored");
                return;
            }

            if (!_zeroRtpOnPause || !Paused)
            {
                await OnReplaceTrackAsync?.Invoke(this, track);
            }

            // Destroy the previous track.
            DestroyTrack();

            // Set the new track.
            Track = track;

            // If this Producer was paused/resumed and the state of the new
            // track does not match, fix it.
            if (Track is not null && _disableTrackOnPause)
            {
                if (!Paused)
                {
                    Track.Enabled = true;
                }
                else if (Paused)
                {
                    Track.Enabled = false;
                }
            }

            // Handle the effective track.
            HandleTrack();
        }
Example #2
0
        public void Resume()
        {
            if (Closed)
            {
                return;
            }

            Paused = false;

            if (_disableTrackOnPause)
            {
                Track.Enabled = true;
            }

            if (_zeroRtpOnPause)
            {
                OnReplaceTrackAsync?.Invoke(this, Track);
            }
        }