public SingleStreamMediaManager(Func <IBufferingManager> bufferingManagerFactory, IMediaParserFactory mediaParserFactory, IMediaStreamConfigurator mediaStreamConfigurator, IWebMetadataFactory webMetadataFactory, IWebReaderManager webReaderManager) { if (null == bufferingManagerFactory) { throw new ArgumentNullException("bufferingManagerFactory"); } if (null == mediaParserFactory) { throw new ArgumentNullException("mediaParserFactory"); } if (null == mediaStreamConfigurator) { throw new ArgumentNullException("mediaStreamConfigurator"); } if (null == webMetadataFactory) { throw new ArgumentNullException("webMetadataFactory"); } if (null == webReaderManager) { throw new ArgumentNullException("webReaderManager"); } this._bufferingManagerFactory = bufferingManagerFactory; this._mediaParserFactory = mediaParserFactory; this._mediaStreamConfigurator = mediaStreamConfigurator; this._webMetadataFactory = webMetadataFactory; this._webReaderManager = webReaderManager; this._reportStateTask = new SignalTask(new Func <Task>(this.ReportState)); }
private async Task CloseCleanupAsync() { Debug.WriteLine("SmMediaManager.CloseCleanupAsync()"); List <Task> tasks = new List <Task>(); ISegmentReaderManager readerManager = this._readerManager; if (null != readerManager) { this._readerManager = (ISegmentReaderManager)null; tasks.Add(readerManager.StopAsync()); } IMediaStreamConfigurator msc = this._mediaStreamConfigurator; if (null != msc) { tasks.Add(msc.CloseAsync()); } if (this._readers != null && this._readers.Length > 0) { tasks.Add(this.CloseReadersAsync()); } if (null != this._playTask) { tasks.Add(this._playTask); } if (tasks.Count > 0) { while (Enumerable.Any <Task>((IEnumerable <Task>)tasks, (Func <Task, bool>)(t => !t.IsCompleted))) { try { Task t = TaskEx.Delay(2500); Debug.WriteLine("SmMediaManager.CloseCleanupAsync() waiting for tasks"); Task task = await TaskEx.WhenAny(t, TaskEx.WhenAll((IEnumerable <Task>)tasks)).ConfigureAwait(false); Debug.WriteLine("SmMediaManager.CloseCleanupAsync() finished tasks"); } catch (Exception ex) { Debug.WriteLine("SmMediaManager.CloseCleanupAsync() play task failed: " + ExceptionExtensions.ExtendedMessage(ex)); } } } if (null != msc) { msc.MediaManager = (IMediaManager)null; } this.DisposeReaders(); if (null != readerManager) { DisposeExtensions.DisposeSafe((IDisposable)readerManager); } }
private async Task CloseCleanupAsync() { Debug.WriteLine("SmMediaManager.CloseCleanupAsync()"); List <Task> tasks = new List <Task>(); ISegmentReaderManager readerManager = _readerManager; if (null != readerManager) { _readerManager = null; tasks.Add(readerManager.StopAsync()); } IMediaStreamConfigurator msc = _mediaStreamConfigurator; if (null != msc) { tasks.Add(msc.CloseAsync()); } if (_readers != null && _readers.Length > 0) { tasks.Add(CloseReadersAsync()); } if (null != _playTask) { tasks.Add(_playTask); } if (tasks.Count > 0) { while (tasks.Any(t => !t.IsCompleted)) { try { Task t = TaskEx.Delay(2500); Debug.WriteLine("SmMediaManager.CloseCleanupAsync() waiting for tasks"); Task task = await TaskEx.WhenAny(t, TaskEx.WhenAll(tasks)).ConfigureAwait(false); Debug.WriteLine("SmMediaManager.CloseCleanupAsync() finished tasks"); } catch (Exception ex) { Debug.WriteLine("SmMediaManager.CloseCleanupAsync() play task failed: " + ex.ExtendedMessage()); } } } if (null != msc) { msc.MediaManager = null; } DisposeReaders(); readerManager?.DisposeSafe(); }
private Task ReportState() { Debug.WriteLine("SmMediaManager.ReportState() state {0} message {1}", _mediaState, _mediaStateMessage); MediaManagerState state; string message; lock (_lock) { state = _mediaState; message = _mediaStateMessage; _mediaStateMessage = null; } OnStateChange?.Invoke(this, new MediaManagerStateEventArgs(state, message)); if (null != message) { IMediaStreamConfigurator streamConfigurator = _mediaStreamConfigurator; streamConfigurator?.ReportError(message); } return(TplTaskExtensions.CompletedTask); }
public SingleStreamMediaManager(Func<IBufferingManager> bufferingManagerFactory, IMediaParserFactory mediaParserFactory, IMediaStreamConfigurator mediaStreamConfigurator, IWebMetadataFactory webMetadataFactory, IWebReaderManager webReaderManager) { if (null == bufferingManagerFactory) throw new ArgumentNullException(nameof(bufferingManagerFactory)); if (null == mediaParserFactory) throw new ArgumentNullException(nameof(mediaParserFactory)); if (null == mediaStreamConfigurator) throw new ArgumentNullException(nameof(mediaStreamConfigurator)); if (null == webMetadataFactory) throw new ArgumentNullException(nameof(webMetadataFactory)); if (null == webReaderManager) throw new ArgumentNullException(nameof(webReaderManager)); _bufferingManagerFactory = bufferingManagerFactory; _mediaParserFactory = mediaParserFactory; _mediaStreamConfigurator = mediaStreamConfigurator; _webMetadataFactory = webMetadataFactory; _webReaderManager = webReaderManager; _reportStateTask = new SignalTask(ReportState); }
private Task ReportState() { Debug.WriteLine("SingleStreamMediaManager.ReportState() state {0} message {1}", (object)this._mediaState, (object)this._mediaStateMessage); bool lockTaken = false; object obj; MediaManagerState state; string message; try { Monitor.Enter(obj = this._lock, ref lockTaken); state = this._mediaState; message = this._mediaStateMessage; this._mediaStateMessage = (string)null; } finally { if (lockTaken) { Monitor.Exit(obj); } } EventHandler <MediaManagerStateEventArgs> eventHandler = this.OnStateChange; if (null != eventHandler) { eventHandler((object)this, new MediaManagerStateEventArgs(state, message)); } if (null != message) { IMediaStreamConfigurator streamConfigurator = this._mediaStreamConfigurator; if (null != streamConfigurator) { streamConfigurator.ReportError(message); } } return(TplTaskExtensions.CompletedTask); }
public SmMediaManager(ISegmentReaderManagerFactory segmentReaderManagerFactory, IMediaStreamConfigurator mediaStreamConfigurator, Func <IBufferingManager> bufferingManagerFactory, IMediaManagerParameters mediaManagerParameters, IMediaParserFactory mediaParserFactory) { if (null == segmentReaderManagerFactory) { throw new ArgumentNullException("segmentReaderManagerFactory"); } if (null == mediaStreamConfigurator) { throw new ArgumentNullException("mediaStreamConfigurator"); } if (null == bufferingManagerFactory) { throw new ArgumentNullException("bufferingManagerFactory"); } _segmentReaderManagerFactory = segmentReaderManagerFactory; _mediaStreamConfigurator = mediaStreamConfigurator; _bufferingManagerFactory = bufferingManagerFactory; _mediaParserFactory = mediaParserFactory; _programStreamsHandler = mediaManagerParameters.ProgramStreamsHandler; _playbackCancellationTokenSource.Cancel(); _playbackTaskCompletionSource.TrySetResult((object)null); _reportStateTask = new SignalTask(ReportState); }
public SmMediaManager(ISegmentReaderManagerFactory segmentReaderManagerFactory, IMediaStreamConfigurator mediaStreamConfigurator, Func<IBufferingManager> bufferingManagerFactory, IMediaManagerParameters mediaManagerParameters, IMediaParserFactory mediaParserFactory) { if (null == segmentReaderManagerFactory) throw new ArgumentNullException(nameof(segmentReaderManagerFactory)); if (null == mediaStreamConfigurator) throw new ArgumentNullException(nameof(mediaStreamConfigurator)); if (null == bufferingManagerFactory) throw new ArgumentNullException(nameof(bufferingManagerFactory)); _segmentReaderManagerFactory = segmentReaderManagerFactory; _mediaStreamConfigurator = mediaStreamConfigurator; _bufferingManagerFactory = bufferingManagerFactory; _mediaParserFactory = mediaParserFactory; _programStreamsHandler = mediaManagerParameters.ProgramStreamsHandler; // Start with a canceled token (i.e., we are not playing) _playbackCancellationTokenSource.Cancel(); _playbackTaskCompletionSource.TrySetResult(null); _reportStateTask = new SignalTask(ReportState); }
public virtual async Task <TMediaStreamSource> CreateMediaStreamSourceAsync(Uri source, CancellationToken cancellationToken) { Debug.WriteLine("MediaStreamFacadeBase.CreateMediaStreamSourceAsync() " + (object)source); if ((Uri)null != source && !source.IsAbsoluteUri) { throw new ArgumentException("source must be absolute: " + (object)source); } CancellationTokenSource closeCts = CancellationTokenSource.CreateLinkedTokenSource(this._disposeCancellationTokenSource.Token); if (!this._closeCancellationTokenSource.IsCancellationRequested) { this._closeCancellationTokenSource.Cancel(); } Exception exception; TMediaStreamSource mediaStreamSource; try { using (CancellationTokenSource linkedTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, closeCts.Token)) { linkedTokenSource.CancelAfter(MediaStreamFacadeSettings.Parameters.CreateTimeout); IMediaManager mediaManager = this.MediaManager; if (null != mediaManager) { await this.StopMediaAsync(mediaManager, linkedTokenSource.Token).ConfigureAwait(false); } this._closeCancellationTokenSource.Dispose(); this._closeCancellationTokenSource = closeCts; if ((Uri)null == source) { mediaStreamSource = default(TMediaStreamSource); goto label_21; } else { IMediaManager mediaManager1 = this.MediaManager; if (mediaManager1 == null) { mediaManager1 = await this.CreateMediaManagerAsync(linkedTokenSource.Token).ConfigureAwait(false); } mediaManager = mediaManager1; IMediaStreamConfigurator configurator = await mediaManager.OpenMediaAsync((ICollection <Uri>) new Uri[1] { source }, linkedTokenSource.Token).ConfigureAwait(false); TMediaStreamSource mss = await configurator.CreateMediaStreamSourceAsync <TMediaStreamSource>(linkedTokenSource.Token).ConfigureAwait(false); mediaStreamSource = mss; goto label_21; } } } catch (OperationCanceledException ex) { exception = (Exception)ex; } catch (Exception ex) { Debug.WriteLine("MediaStreamFacadeBase.CreateAsync() failed: " + ExceptionExtensions.ExtendedMessage(ex)); exception = (Exception) new AggregateException(ex.Message, ex); } await this.CloseAsync().ConfigureAwait(false); throw exception; label_21: return(mediaStreamSource); }
public static void SetParameter(this IMediaStreamFacadeBase mediaStreamFacade, IMediaStreamConfigurator mediaStreamConfigurator) { mediaStreamFacade.Builder.RegisterSingleton <IMediaStreamConfigurator>(mediaStreamConfigurator); }
public static Task PlayAsync(this IMediaStreamConfigurator mediaStreamConfigurator, IEnumerable <IMediaParserMediaStream> mediaParserMediaStreams, TimeSpan?duration, CancellationToken cancellationToken) { return(mediaStreamConfigurator.PlayAsync(MediaParserMediaStreamExtensions.CreateMediaConfiguration(mediaParserMediaStreams, duration), cancellationToken)); }