static async Task DoAsync(this MediaEngine engine, CancellationToken token, Action action, Func <MediaEngineEvent, bool> completionEvent) { var tcs = new TaskCompletionSource <Unit>(); token.Register(() => tcs.TrySetCanceled()); MediaEngineNotifyDelegate @delegate = (e, p1, p2) => { if (completionEvent(e)) { tcs.TrySetResult(Unit.Default); } else if (e == MediaEngineEvent.Abort) { tcs.TrySetCanceled(); } else if (e == MediaEngineEvent.Error) { tcs.TrySetException(new Exception($"An error occured in the pipeline.")); } }; try { engine.PlaybackEvent += @delegate; action(); await tcs.Task; } finally { engine.PlaybackEvent -= @delegate; } }
private void OnPlaybackEvent(MediaEngineEvent mediaevent, long param1, int param2) { MediaEngineNotifyDelegate handler = PlaybackEvent; if (handler != null) { handler(mediaevent, param1, param2); } }
/// <summary> /// Initializes an instance of the <see cref="MediaEngine"/> class. /// </summary> /// <param name="factory"></param> /// <param name="attributes"></param> /// <param name="createFlags"> </param> public MediaEngine(MediaEngineClassFactory factory, MediaEngineAttributes attributes = null, MediaEngineCreateFlags createFlags = MediaEngineCreateFlags.None, MediaEngineNotifyDelegate playbackCallback = null) { // Create engine attributes if null attributes = attributes ?? new MediaEngineAttributes(); PlaybackEvent = playbackCallback; // Setup by default the MediaEngine notify as it is mandatory mediaEngineNotifyImpl = new MediaEngineNotifyImpl(this); try { attributes.Set(MediaEngineAttributeKeys.Callback, new ComObject(MediaEngineNotifyShadow.ToIntPtr(mediaEngineNotifyImpl))); factory.CreateInstance(createFlags, attributes, this); } catch { mediaEngineNotifyImpl.Dispose(); mediaEngineNotifyImpl = null; throw; } }
/// <summary> /// Initializes an instance of the <see cref="MediaEngine"/> class. /// </summary> /// <param name="factory"></param> /// <param name="attributes"></param> /// <param name="createFlags"> </param> /// <param name="playbackCallback"></param> /// <msdn-id>hh447921</msdn-id> /// <unmanaged>HRESULT IMFMediaEngineClassFactory::CreateInstance([In] MF_MEDIA_ENGINE_CREATEFLAGS dwFlags,[In] IMFAttributes* pAttr,[Out, Fast] IMFMediaEngine** ppPlayer)</unmanaged> /// <unmanaged-short>IMFMediaEngineClassFactory::CreateInstance</unmanaged-short> public MediaEngine(MediaEngineClassFactory factory, MediaEngineAttributes attributes = null, MediaEngineCreateFlags createFlags = MediaEngineCreateFlags.None, MediaEngineNotifyDelegate playbackCallback = null) { // Create engine attributes if null attributes = attributes ?? new MediaEngineAttributes(); if (playbackCallback != null) { PlaybackEvent += playbackCallback; } // Setup by default the MediaEngine notify as it is mandatory mediaEngineNotifyImpl = new MediaEngineNotifyImpl(this); try { attributes.Set(MediaEngineAttributeKeys.Callback, new ComObject(MediaEngineNotifyShadow.ToIntPtr(mediaEngineNotifyImpl))); factory.CreateInstance(createFlags, attributes, this); } catch { mediaEngineNotifyImpl.Dispose(); mediaEngineNotifyImpl = null; throw; } }