/// <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): SinkWriterStatistics statistics = _sinkWriter.GetStatistics(_streamIndex); if (statistics.DwByteCountQueued > 0 || statistics.QwNumSamplesReceived > 0) { _sinkWriter.Finalize(); } _sinkWriter.Dispose(); _sinkWriter = null; } if (_targetStream != null) { _targetStream.Flush(); _targetStream.Dispose(); _targetStream = null; } } _disposed = true; }
public void Close() { logger.Debug("MfWriter::Close()"); closed = true; if (sinkWriter != null) { sinkWriter.Dispose(); sinkWriter = null; } if (bufTexture != null) { bufTexture.Dispose(); bufTexture = null; } if (videoSample != null) { videoSample.Dispose(); videoSample = null; } if (mediaBuffer != null) { mediaBuffer.Dispose(); mediaBuffer = null; } }
/// <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, MediaType inputMediaType, MediaType targetMediaType, Guid containerType) { MediaAttributes attributes = null; try { var buffer = new byte[stream.Length]; stream.Read(buffer, 0, buffer.Length); _targetStream = new ByteStream(buffer); attributes = new MediaAttributes(2); attributes.Set(MediaFoundationAttributes.MF_READWRITE_ENABLE_HARDWARE_TRANSFORMS, 1); attributes.Set(MediaFoundationAttributes.MF_TRANSCODE_CONTAINERTYPE, containerType); _sinkWriter = SinkWriter.Create(_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(); } } }
public void Dispose() { _writer.Finalize(); _writer.Dispose(); }