Esempio n. 1
0
 public static IntPtr CreateSinkWriterFromMFByteStreamNative(MFByteStream byteStream, MFAttributes attributes)
 {
     IntPtr p;
     int result = NativeMethods.ExternMFCreateSinkWriterFromURL(null, byteStream.BasePtr, attributes.BasePtr, out p);
     MediaFoundationException.Try(result, "Interops", "MFCreateSinkWriterFromURL");
     return p;
 }
Esempio n. 2
0
 /// <summary>
 ///     Initializes a new instance of the <see cref="MediaFoundationDecoder" /> class.
 /// </summary>
 /// <param name="byteStream">Stream which provides the audio data to decode.</param>
 public MediaFoundationDecoder(MFByteStream byteStream)
 {
     if (byteStream == null)
         throw new ArgumentNullException("byteStream");
     _byteStream = byteStream;
     _reader = Initialize(_byteStream);
 }
        /// <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;
        }
Esempio n. 4
0
        public static IntPtr CreateSinkWriterFromMFByteStreamNative(MFByteStream byteStream, MFAttributes attributes)
        {
            IntPtr p;
            int    result = NativeMethods.ExternMFCreateSinkWriterFromURL(null, byteStream.BasePtr, attributes.BasePtr, out p);

            MediaFoundationException.Try(result, "Interops", "MFCreateSinkWriterFromURL");
            return(p);
        }
Esempio n. 5
0
 /// <summary>
 ///     Initializes a new instance of the <see cref="MediaFoundationDecoder" /> class.
 /// </summary>
 /// <param name="byteStream">Stream which provides the audio data to decode.</param>
 public MediaFoundationDecoder(MFByteStream byteStream)
 {
     if (byteStream == null)
     {
         throw new ArgumentNullException("byteStream");
     }
     _byteStream = byteStream;
     _reader     = Initialize(_byteStream);
 }
Esempio n. 6
0
        /// <summary>
        ///     Initializes a new instance of the <see cref="MediaFoundationDecoder" /> class.
        /// </summary>
        /// <param name="stream">Stream which provides the audio data to decode.</param>
        public MediaFoundationDecoder(Stream stream)
        {
            if (stream == null)
                throw new ArgumentNullException("stream");
            if (!stream.CanRead)
                throw new ArgumentException("Stream is not readable.", "stream");

            stream = new ComStream(stream);
            _stream = stream;
            _byteStream = MediaFoundationCore.IStreamToByteStream((IStream) stream);
            _reader = Initialize(_byteStream);
        }
Esempio n. 7
0
        /// <summary>
        ///     Initializes a new instance of the <see cref="MediaFoundationDecoder" /> class.
        /// </summary>
        /// <param name="stream">Stream which provides the audio data to decode.</param>
        public MediaFoundationDecoder(Stream stream)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }
            if (!stream.CanRead)
            {
                throw new ArgumentException("Stream is not readable.", "stream");
            }

            stream      = new ComStream(stream);
            _stream     = stream;
            _byteStream = MediaFoundationCore.IStreamToByteStream((IStream)stream);
            _reader     = Initialize(_byteStream);
        }
        /// <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();
                }
            }
        }
Esempio n. 9
0
 private void DisposeInternal()
 {
     if (_reader != null)
     {
         _reader.Dispose();
         _reader = null;
     }
     if (_byteStream != null)
     {
         _byteStream.Dispose();
         _byteStream = null;
     }
     if (_stream != null)
     {
         _stream.Dispose();
         _stream = null;
     }
 }
Esempio n. 10
0
 private MFSourceReader Initialize(MFByteStream byteStream)
 {
     return Initialize(MediaFoundationCore.CreateSourceReaderFromByteStream(byteStream.BasePtr, IntPtr.Zero));
 }
Esempio n. 11
0
 private void DisposeInternal()
 {
     if (_reader != null)
     {
         _reader.Dispose();
         _reader = null;
     }
     if (_byteStream != null)
     {
         _byteStream.Dispose();
         _byteStream = null;
     }
     if (_stream != null)
     {
         _stream.Dispose();
         _stream = null;
     }
 }
Esempio n. 12
0
 private MFSourceReader Initialize(MFByteStream byteStream)
 {
     return(Initialize(MediaFoundationCore.CreateSourceReaderFromByteStream(byteStream.BasePtr, IntPtr.Zero)));
 }
Esempio n. 13
0
        /// <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();
            }
        }
Esempio n. 14
0
        /// <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;
        }
Esempio n. 15
0
 /// <summary>
 /// Initializes a new instance of the <see cref="MFSinkWriter"/> class with a underlying <paramref name="byteStream"/>.
 /// </summary>
 /// <param name="byteStream">The underlying <see cref="MFByteStream"/> to use.</param>
 /// <param name="attributes">Attributes to configure the <see cref="MFSinkWriter"/>. For more information, see <see href="https://msdn.microsoft.com/en-us/library/windows/desktop/dd389284(v=vs.85).aspx"/>. Use null/nothing as the default value.</param>
 public MFSinkWriter(MFByteStream byteStream, MFAttributes attributes = null)
     : this(MediaFoundationCore.CreateSinkWriterFromMFByteStreamNative(byteStream, attributes))
 {
 }
Esempio n. 16
0
 /// <summary>
 /// Initializes a new instance of the <see cref="MFSinkWriter"/> class with a underlying <paramref name="byteStream"/>.
 /// </summary>
 /// <param name="byteStream">The underlying <see cref="MFByteStream"/> to use.</param>
 /// <param name="attributes">Attributes to configure the <see cref="MFSinkWriter"/>. For more information, see <see href="https://msdn.microsoft.com/en-us/library/windows/desktop/dd389284(v=vs.85).aspx"/>. Use null/nothing as the default value.</param>
 public MFSinkWriter(MFByteStream byteStream, MFAttributes attributes = null)
     : this(MediaFoundationCore.CreateSinkWriterFromMFByteStreamNative(byteStream, attributes))
 {
 }