protected virtual void Dispose(bool disposing) { if (!_disposed) { if (_sinkWriter != null) { while (_sinkWriter.GetStatistics(_streamIndex).DwByteCountQueued > 0) { Thread.Sleep(50); } _sinkWriter.Flush(_streamIndex); _sinkWriter.FinalizeWriting(); _sinkWriter.Dispose(); _sinkWriter = null; } if (_targetStream != null) { _targetStream.Flush(); _targetStream.Close(); Marshal.ReleaseComObject(_targetStream); _targetStream = null; } } _disposed = true; }
/// <summary> /// Disposes the <see cref="MediaFoundationEncoder" />. /// </summary> /// <param name="disposing"> /// True to release both managed and unmanaged resources; false to release only unmanaged /// resources. /// </param> protected virtual void Dispose(bool disposing) { if (!_disposed) { if (_sinkWriter != null) { //thanks to martin48 (and naudio??) for providing the following source code (see http://cscore.codeplex.com/discussions/574280): MFSinkWriterStatistics statistics = _sinkWriter.GetStatistics(_streamIndex); if (statistics.ByteCountQueued > 0 || statistics.NumSamplesReceived > 0) { _sinkWriter.FinalizeWriting(); } _sinkWriter.Dispose(); _sinkWriter = null; } if (_targetStream != null) { _targetStream.Flush(); _targetStream.Dispose(); _targetStream = null; } if (_targetBaseStream != null && !_targetBaseStream.IsClosed()) { _targetBaseStream.Flush(); _targetBaseStream.Dispose(); } } _disposed = true; }
protected void SetTargetStream(Stream stream, MFMediaType inputMediaType, MFMediaType targetMediaType, Guid containerType) { IMFAttributes attributes = null; try { _targetStream = MediaFoundationCore.IStreamToByteStream(new ComStream(stream)); MFByteStreamCapsFlags flags = MFByteStreamCapsFlags.None; int result = _targetStream.GetCapabilities(ref flags); attributes = MediaFoundationCore.CreateEmptyAttributes(2); attributes.SetUINT32(MediaFoundationAttributes.MF_READWRITE_ENABLE_HARDWARE_TRANSFORMS, 1); attributes.SetGUID(MediaFoundationAttributes.MF_TRANSCODE_CONTAINERTYPE, containerType); _sinkWriter = MediaFoundationCore.CreateSinkWriterFromMFByteStream(_targetStream, attributes); _streamIndex = _sinkWriter.AddStream(targetMediaType); _sinkWriter.SetInputMediaType(_streamIndex, inputMediaType, null); _inputMediaType = inputMediaType; _targetMediaType = targetMediaType; _sourceBytesPerSecond = inputMediaType.AverageBytesPerSecond; //initialize the sinkwriter _sinkWriter.BeginWriting(); } catch (Exception) { if (_sinkWriter != null) { _sinkWriter.Dispose(); _sinkWriter = null; } if (_targetStream != null) { _targetStream.Close(); Marshal.ReleaseComObject(_targetStream); _targetStream = null; } throw; } finally { if (attributes != null) { Marshal.ReleaseComObject(attributes); } } }
/// <summary> /// Sets and initializes the targetstream for the encoding process. /// </summary> /// <param name="stream">Stream which should be used as the targetstream.</param> /// <param name="inputMediaType">Mediatype of the raw input data to encode.</param> /// <param name="targetMediaType">Mediatype of the encoded data.</param> /// <param name="containerType">Container type which should be used.</param> protected void SetTargetStream(Stream stream, MFMediaType inputMediaType, MFMediaType targetMediaType, Guid containerType) { MFAttributes attributes = null; try { _targetBaseStream = new ComStream(stream); _targetStream = MediaFoundationCore.IStreamToByteStream(_targetBaseStream); attributes = new MFAttributes(2); attributes.SetUINT32(MediaFoundationAttributes.MF_READWRITE_ENABLE_HARDWARE_TRANSFORMS, 1); attributes.SetGuid(MediaFoundationAttributes.MF_TRANSCODE_CONTAINERTYPE, containerType); _sinkWriter = new MFSinkWriter(_targetStream, attributes); _streamIndex = _sinkWriter.AddStream(targetMediaType); _sinkWriter.SetInputMediaType(_streamIndex, inputMediaType, null); _targetMediaType = targetMediaType; _sourceBytesPerSecond = inputMediaType.AverageBytesPerSecond; //initialize the sinkwriter _sinkWriter.BeginWriting(); } catch (Exception) { if (_sinkWriter != null) { _sinkWriter.Dispose(); _sinkWriter = null; } if (_targetStream != null) { _targetStream.Dispose(); _targetStream = null; } throw; } finally { if (attributes != null) { attributes.Dispose(); } } }
protected void SetTargetStream(Stream stream, MFMediaType inputMediaType, MFMediaType targetMediaType, Guid containerType) { IMFAttributes attributes = null; try { _targetStream = MediaFoundationCore.IStreamToByteStream(new ComStream(stream)); MFByteStreamCapsFlags flags = MFByteStreamCapsFlags.None; int result = _targetStream.GetCapabilities(ref flags); attributes = MediaFoundationCore.CreateEmptyAttributes(2); attributes.SetUINT32(MediaFoundationAttributes.MF_READWRITE_ENABLE_HARDWARE_TRANSFORMS, 1); attributes.SetGUID(MediaFoundationAttributes.MF_TRANSCODE_CONTAINERTYPE, containerType); _sinkWriter = MediaFoundationCore.CreateSinkWriterFromMFByteStream(_targetStream, attributes); _streamIndex = _sinkWriter.AddStream(targetMediaType); _sinkWriter.SetInputMediaType(_streamIndex, inputMediaType, null); _inputMediaType = inputMediaType; _targetMediaType = targetMediaType; _sourceBytesPerSecond = inputMediaType.AverageBytesPerSecond; //initialize the sinkwriter _sinkWriter.BeginWriting(); } catch (Exception) { if (_sinkWriter != null) { _sinkWriter.Dispose(); _sinkWriter = null; } if (_targetStream != null) { _targetStream.Close(); Marshal.ReleaseComObject(_targetStream); _targetStream = null; } throw; } finally { if (attributes != null) Marshal.ReleaseComObject(attributes); } }
/// <summary> /// Sets and initializes the targetstream for the encoding process. /// </summary> /// <param name="stream">Stream which should be used as the targetstream.</param> /// <param name="inputMediaType">Mediatype of the raw input data to encode.</param> /// <param name="targetMediaType">Mediatype of the encoded data.</param> /// <param name="containerType">Container type which should be used.</param> protected void SetTargetStream(Stream stream, MFMediaType inputMediaType, MFMediaType targetMediaType, Guid containerType) { MFAttributes attributes = null; try { _targetBaseStream = new ComStream(stream); _targetStream = MediaFoundationCore.IStreamToByteStream(_targetBaseStream); attributes = new MFAttributes(2); attributes.SetUINT32(MediaFoundationAttributes.MF_READWRITE_ENABLE_HARDWARE_TRANSFORMS, 1); attributes.SetGuid(MediaFoundationAttributes.MF_TRANSCODE_CONTAINERTYPE, containerType); _sinkWriter = new MFSinkWriter(_targetStream, attributes); _streamIndex = _sinkWriter.AddStream(targetMediaType); _sinkWriter.SetInputMediaType(_streamIndex, inputMediaType, null); _targetMediaType = targetMediaType; _sourceBytesPerSecond = inputMediaType.AverageBytesPerSecond; //initialize the sinkwriter _sinkWriter.BeginWriting(); } catch (Exception) { if (_sinkWriter != null) { _sinkWriter.Dispose(); _sinkWriter = null; } if (_targetStream != null) { _targetStream.Dispose(); _targetStream = null; } throw; } finally { if (attributes != null) attributes.Dispose(); } }
/// <summary> /// Disposes the <see cref="MediaFoundationEncoder" />. /// </summary> /// <param name="disposing"> /// True to release both managed and unmanaged resources; false to release only unmanaged /// resources. /// </param> protected virtual void Dispose(bool disposing) { if (!_disposed) { if (_sinkWriter != null && !_sinkWriter.IsDisposed) { //thanks to martin48 (and naudio??) for providing the following source code (see http://cscore.codeplex.com/discussions/574280): MFSinkWriterStatistics statistics = _sinkWriter.GetStatistics(_streamIndex); if (statistics.ByteCountQueued > 0 || statistics.NumSamplesReceived > 0) _sinkWriter.FinalizeWriting(); _sinkWriter.Dispose(); _sinkWriter = null; } if (_targetStream != null) { _targetStream.Flush(); _targetStream.Dispose(); _targetStream = null; } if (_targetBaseStream != null && !_targetBaseStream.IsClosed()) { _targetBaseStream.Flush(); _targetBaseStream.Dispose(); } } _disposed = true; }