Ejemplo n.º 1
0
        private long GetLength(MFSourceReader reader)
        {
            lock (_lockObj)
            {
                try
                {
                    if (reader == null)
                    {
                        return(0);
                    }

                    using (
                        PropertyVariant value =
                            reader.GetPresentationAttribute(NativeMethods.MF_SOURCE_READER_MEDIASOURCE,
                                                            MediaFoundationAttributes.MF_PD_DURATION))
                    {
                        //bug: still, depending on the decoder, this returns imprecise values.
                        return(NanoSecond100UnitsToBytes(value.HValue));
                    }
                }
                catch (Exception)
                {
                    return(0);
                }
            }
        }
Ejemplo n.º 2
0
        private long GetLength(MFSourceReader reader)
        {
            lock (_lockObj)
            {
                try
                {
                    if (reader == null)
                    {
                        return(0);
                    }

                    PropertyVariant value  = reader.GetPresentationAttribute(MFInterops.MF_SOURCE_READER_MEDIASOURCE, MediaFoundationAttributes.MF_PD_DURATION);
                    var             length = ((value.HValue) * _waveFormat.BytesPerSecond) / 10000000L;
                    value.Dispose();
                    return(length);
                }
                catch (Exception)
                {
                    //if (e.Result == (int)HResult.MF_E_ATTRIBUTENOTFOUND)
                    //    return 0;
                    //throw;
                    return(0);
                }
            }
        }
Ejemplo n.º 3
0
        private long GetLength(MFSourceReader reader)
        {
            lock (_lockObj)
            {
                try
                {
                    if (reader == null)
                    {
                        return(0);
                    }

                    using (
                        PropertyVariant value =
                            reader.GetPresentationAttribute(NativeMethods.MF_SOURCE_READER_MEDIASOURCE,
                                                            MediaFoundationAttributes.MF_PD_DURATION))


                    {
                        //bug: still, depending on the decoder, this returns imprecise values.
                        return(((value.HValue) * _waveFormat.BytesPerSecond) / 10000000L);
                    }
                }
                catch (Exception)
                {
                    //if (e.Result == (int)HResult.MF_E_ATTRIBUTENOTFOUND)
                    //    return 0;
                    //throw;
                    return(0);
                }
            }
        }
Ejemplo n.º 4
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);
 }
Ejemplo 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);
 }
Ejemplo n.º 6
0
        /// <summary>
        ///     Initializes a new instance of the <see cref="MediaFoundationDecoder" /> class.
        /// </summary>
        /// <param name="url">Uri which points to an audio source which can be decoded.</param>
        public MediaFoundationDecoder(string url)
        {
            if (String.IsNullOrEmpty(url))
                throw new ArgumentNullException("url");

            _hasFixedLength = true;

            _reader = Initialize(MediaFoundationCore.CreateSourceReaderFromUrl(url));
        }
Ejemplo n.º 7
0
        private MFSourceReader Initialize(MFSourceReader reader)
        {
            try
            {
                reader.SetStreamSelection(NativeMethods.MF_SOURCE_READER_ALL_STREAMS, false);
                reader.SetStreamSelection(NativeMethods.MF_SOURCE_READER_FIRST_AUDIO_STREAM, true);

                using (MFMediaType mediaType = MFMediaType.CreateEmpty())
                {
                    mediaType.MajorType = AudioSubTypes.MediaTypeAudio;
                    mediaType.SubType   = AudioSubTypes.Pcm; //variable??

                    reader.SetCurrentMediaType(NativeMethods.MF_SOURCE_READER_FIRST_AUDIO_STREAM, mediaType);
                }

                using (
                    MFMediaType currentMediaType =
                        reader.GetCurrentMediaType(NativeMethods.MF_SOURCE_READER_FIRST_AUDIO_STREAM))
                {
                    if (currentMediaType.MajorType != AudioSubTypes.MediaTypeAudio)
                    {
                        throw new InvalidOperationException(String.Format(
                                                                "Invalid Majortype set on sourcereader: {0}.", currentMediaType.MajorType));
                    }

                    AudioEncoding encoding = AudioSubTypes.EncodingFromSubType(currentMediaType.SubType);

                    ChannelMask channelMask;
                    if (currentMediaType.TryGet(MediaFoundationAttributes.MF_MT_AUDIO_CHANNEL_MASK, out channelMask))
                    //check whether the attribute is available
                    {
                        _waveFormat = new WaveFormatExtensible(currentMediaType.SampleRate,
                                                               currentMediaType.BitsPerSample, currentMediaType.Channels, currentMediaType.SubType,
                                                               channelMask);
                    }
                    else
                    {
                        _waveFormat = new WaveFormat(currentMediaType.SampleRate, currentMediaType.BitsPerSample,
                                                     currentMediaType.Channels, encoding);
                    }
                }

                reader.SetStreamSelection(NativeMethods.MF_SOURCE_READER_FIRST_AUDIO_STREAM, true);

                if (_hasFixedLength)
                {
                    _length = GetLength(reader);
                }

                return(reader);
            }
            catch (Exception)
            {
                DisposeInternal();
                throw;
            }
        }
Ejemplo n.º 8
0
        private MFSourceReader Initialize(MFSourceReader reader)
        {
            MediaFoundationCore.Startup();

            try
            {
                reader.SetStreamSelection(MFInterops.MF_SOURCE_READER_ALL_STREAMS, false);
                reader.SetStreamSelection(MFInterops.MF_SOURCE_READER_FIRST_AUDIO_STREAM, true);

                using (var mediaType = MFMediaType.CreateEmpty())
                {
                    mediaType.MajorType = MediaTypes.MediaTypeAudio;
                    mediaType.SubType   = MediaTypes.MEDIATYPE_Pcm; //variable??

                    reader.SetCurrentMediaType(MFInterops.MF_SOURCE_READER_FIRST_AUDIO_STREAM, mediaType);
                }

                using (var currentMediaType = reader.GetCurrentMediaType(MFInterops.MF_SOURCE_READER_FIRST_AUDIO_STREAM))
                {
                    if (currentMediaType.MajorType != MediaTypes.MediaTypeAudio)
                    {
                        throw new InvalidOperationException(String.Format("Invalid Majortype set on sourcereader: {0}.", currentMediaType.MajorType.ToString()));
                    }

                    AudioEncoding encoding;
                    if (currentMediaType.SubType == MediaTypes.MEDIATYPE_Pcm)
                    {
                        encoding = AudioEncoding.Pcm;
                    }
                    else if (currentMediaType.SubType == MediaTypes.MEDIATYPE_IeeeFloat)
                    {
                        encoding = AudioEncoding.IeeeFloat;
                    }
                    else
                    {
                        throw new InvalidOperationException(String.Format("Invalid Subtype set on sourcereader: {0}.", currentMediaType.SubType.ToString()));
                    }

                    _waveFormat = new WaveFormat(currentMediaType.SampleRate, currentMediaType.BitsPerSample, currentMediaType.Channels, encoding);
                }

                reader.SetStreamSelection(MFInterops.MF_SOURCE_READER_FIRST_AUDIO_STREAM, true);

                if (_hasFixedLength)
                {
                    _length = GetLength(reader);
                }

                return(reader);
            }
            catch (Exception)
            {
                Dispose();
                throw;
            }
        }
Ejemplo n.º 9
0
        /// <summary>
        ///     Initializes a new instance of the <see cref="MediaFoundationDecoder" /> class.
        /// </summary>
        /// <param name="url">Uri which points to an audio source which can be decoded.</param>
        public MediaFoundationDecoder(string url)
        {
            if (String.IsNullOrEmpty(url))
            {
                throw new ArgumentNullException("url");
            }

            _hasFixedLength = true;

            _reader = Initialize(MediaFoundationCore.CreateSourceReaderFromUrl(url));
        }
Ejemplo n.º 10
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);
        }
Ejemplo n.º 11
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);
        }
Ejemplo n.º 12
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;
     }
 }
Ejemplo n.º 13
0
 protected virtual void Dispose(bool disposing)
 {
     lock (_lockObj)
     {
         if (_reader != null)
         {
             _reader.Dispose();
             _reader = null;
         }
         if (_byteStream != null)
         {
             Marshal.ReleaseComObject(_byteStream);
             _byteStream = null;
         }
         if (_stream != null)
         {
             _stream.Dispose();
             _stream = null;
         }
     }
 }
Ejemplo n.º 14
0
 protected virtual void Dispose(bool disposing)
 {
     lock (_lockObj)
     {
         if (_reader != null)
         {
             _reader.Dispose();
             _reader = null;
         }
         if (_byteStream != null)
         {
             Marshal.ReleaseComObject(_byteStream);
             _byteStream = null;
         }
         if (_stream != null)
         {
             _stream.Dispose();
             _stream = null;
         }
     }
 }
Ejemplo n.º 15
0
        private MFSourceReader Initialize(MFSourceReader reader)
        {
            MediaFoundationCore.Startup();

            try
            {
                reader.SetStreamSelection(MFInterops.MF_SOURCE_READER_ALL_STREAMS, false);
                reader.SetStreamSelection(MFInterops.MF_SOURCE_READER_FIRST_AUDIO_STREAM, true);

                using (var mediaType = MFMediaType.CreateEmpty())
                {
                    mediaType.MajorType = MediaTypes.MediaTypeAudio;
                    mediaType.SubType = MediaTypes.MEDIATYPE_Pcm; //variable??

                    reader.SetCurrentMediaType(MFInterops.MF_SOURCE_READER_FIRST_AUDIO_STREAM, mediaType);
                }

                using (var currentMediaType = reader.GetCurrentMediaType(MFInterops.MF_SOURCE_READER_FIRST_AUDIO_STREAM))
                {
                    if (currentMediaType.MajorType != MediaTypes.MediaTypeAudio)
                        throw new InvalidOperationException(String.Format("Invalid Majortype set on sourcereader: {0}.", currentMediaType.MajorType.ToString()));

                    AudioEncoding encoding;
                    if (currentMediaType.SubType == MediaTypes.MEDIATYPE_Pcm)
                        encoding = AudioEncoding.Pcm;
                    else if (currentMediaType.SubType == MediaTypes.MEDIATYPE_IeeeFloat)
                        encoding = AudioEncoding.IeeeFloat;
                    else
                        throw new InvalidOperationException(String.Format("Invalid Subtype set on sourcereader: {0}.", currentMediaType.SubType.ToString()));

                    _waveFormat = new WaveFormat(currentMediaType.SampleRate, currentMediaType.BitsPerSample, currentMediaType.Channels, encoding);
                }

                reader.SetStreamSelection(MFInterops.MF_SOURCE_READER_FIRST_AUDIO_STREAM, true);

                if (_hasFixedLength)
                    _length = GetLength(reader);

                return reader;
            }
            catch (Exception)
            {
                Dispose();
                throw;
            }
        }
Ejemplo n.º 16
0
        private MFSourceReader Initialize(MFSourceReader reader)
        {
            try
            {
                reader.SetStreamSelection(NativeMethods.MF_SOURCE_READER_ALL_STREAMS, false);
                reader.SetStreamSelection(NativeMethods.MF_SOURCE_READER_FIRST_AUDIO_STREAM, true);

                using (MFMediaType mediaType = MFMediaType.CreateEmpty())
                {
                    mediaType.MajorType = AudioSubTypes.MediaTypeAudio;
                    mediaType.SubType = AudioSubTypes.Pcm; //variable??

                    reader.SetCurrentMediaType(NativeMethods.MF_SOURCE_READER_FIRST_AUDIO_STREAM, mediaType);
                }

                using (
                    MFMediaType currentMediaType =
                        reader.GetCurrentMediaType(NativeMethods.MF_SOURCE_READER_FIRST_AUDIO_STREAM))
                {
                    if (currentMediaType.MajorType != AudioSubTypes.MediaTypeAudio)
                    {
                        throw new InvalidOperationException(String.Format(
                            "Invalid Majortype set on sourcereader: {0}.", currentMediaType.MajorType));
                    }

                    AudioEncoding encoding = AudioSubTypes.EncodingFromSubType(currentMediaType.SubType);

                    ChannelMask channelMask;
                    if (currentMediaType.TryGet(MediaFoundationAttributes.MF_MT_AUDIO_CHANNEL_MASK, out channelMask))
                        //check whether the attribute is available
                    {
                        _waveFormat = new WaveFormatExtensible(currentMediaType.SampleRate,
                            currentMediaType.BitsPerSample, currentMediaType.Channels, currentMediaType.SubType,
                            channelMask);
                    }
                    else
                    {
                        _waveFormat = new WaveFormat(currentMediaType.SampleRate, currentMediaType.BitsPerSample,
                            currentMediaType.Channels, encoding);
                    }
                }

                reader.SetStreamSelection(NativeMethods.MF_SOURCE_READER_FIRST_AUDIO_STREAM, true);

                if (_hasFixedLength)
                    _length = GetLength(reader);

                return reader;
            }
            catch (Exception)
            {
                DisposeInternal();
                throw;
            }
        }
Ejemplo n.º 17
0
        private long GetLength(MFSourceReader reader)
        {
            lock (_lockObj)
            {
                try
                {
                    if (reader == null)
                        return 0;

                    using (
                        PropertyVariant value =
                            reader.GetPresentationAttribute(NativeMethods.MF_SOURCE_READER_MEDIASOURCE,
                                MediaFoundationAttributes.MF_PD_DURATION))
                    {
                        //bug: still, depending on the decoder, this returns imprecise values.
                        return NanoSecond100UnitsToBytes(value.HValue);
                    }
                }
                catch (Exception)
                {
                    return 0;
                }
            }
        }
Ejemplo n.º 18
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;
     }
 }
Ejemplo n.º 19
0
        private long GetLength(MFSourceReader reader)
        {
            lock (_lockObj)
            {
                try
                {
                    if (reader == null)
                        return 0;

                    PropertyVariant value = reader.GetPresentationAttribute(MFInterops.MF_SOURCE_READER_MEDIASOURCE, MediaFoundationAttributes.MF_PD_DURATION);
                    var length = ((value.HValue) * _waveFormat.BytesPerSecond) / 10000000L;
                    value.Dispose();
                    return length;
                }
                catch (Exception)
                {
                    //if (e.Result == (int)HResult.MF_E_ATTRIBUTENOTFOUND)
                    //    return 0;
                    //throw;
                    return 0;
                }
            }
        }
Ejemplo n.º 20
0
        private long GetLength(MFSourceReader reader)
        {
            lock (_lockObj)
            {
                try
                {
                    if (reader == null)
                        return 0;

                    using (
                        PropertyVariant value =
                            reader.GetPresentationAttribute(NativeMethods.MF_SOURCE_READER_MEDIASOURCE,
                                MediaFoundationAttributes.MF_PD_DURATION))

                    {
                        //bug: still, depending on the decoder, this returns imprecise values.
                        return ((value.HValue) * _waveFormat.BytesPerSecond) / 10000000L;
                    }
                }
                catch (Exception)
                {
                    //if (e.Result == (int)HResult.MF_E_ATTRIBUTENOTFOUND)
                    //    return 0;
                    //throw;
                    return 0;
                }
            }
        }