Example #1
0
 /// <summary>
 /// Gets the current media type for a stream.
 /// </summary>
 /// <returns>HRESULT</returns>
 public unsafe int GetCurrentMediaType(int streamIndex, out MFMediaType mediaType)
 {
     IntPtr ptr = IntPtr.Zero;
     var result = InteropCalls.CalliMethodPtr(_basePtr, streamIndex, &ptr, ((void**)(*(void**)_basePtr))[6]);
     mediaType = new MFMediaType(ptr);
     return result;
 }
Example #2
0
        /// <summary>
        /// Compares two media types and determines whether they are identical. If they are not
        /// identical, the method indicates how the two formats differ.
        /// </summary>
        public MediaTypeEqualFlags IsEqual(MFMediaType mediaType)
        {
            MediaTypeEqualFlags flags;

            MediaFoundationException.Try(IsEqual(mediaType, out flags), c, "IsEqual");
            return(flags);
        }
Example #3
0
        /// <summary>
        /// Sets the input format for a stream on the sink writer.
        /// <seealso cref="SetInputMediaType"/>
        /// </summary>
        /// <param name="streamIndex">The zero-based index of the stream. The index is returned by the <see cref="AddStream"/> method.</param>
        /// <param name="inputMediaType">The input media type that specifies the input format.</param>
        /// <param name="encodingParameters">An attribute store. Use the attribute store to configure the encoder. This parameter can be NULL.</param>
        /// <returns>HRESULT</returns>
        public unsafe int SetInputMediaTypeNative(int streamIndex, MFMediaType inputMediaType, MFAttributes encodingParameters)
        {
            void *imt = (void *)(inputMediaType == null ? IntPtr.Zero : inputMediaType.BasePtr);
            void *ep  = (void *)(encodingParameters == null ? IntPtr.Zero : encodingParameters.BasePtr);

            return(InteropCalls.CalliMethodPtr(UnsafeBasePtr, streamIndex, imt, ep, ((void **)(*(void **)UnsafeBasePtr))[4]));
        }
        /// <summary>
        ///     Returns a new instance of the <see cref="MediaFoundationEncoder"/> class, configured as wma encoder.
        /// </summary>
        /// <param name="sourceFormat">The input format, of the data to encode.</param>
        /// <param name="bitRate">The bitrate to use. The final bitrate can differ from the specified value.</param>
        /// <param name="targetStream">The stream to write to.</param>
        /// <remarks>For more information about supported input and output formats, see <see href="http://msdn.microsoft.com/en-us/library/windows/desktop/ff819498(v=vs.85).aspx"/>.</remarks>
        /// <returns>A new instance of the <see cref="MediaFoundationEncoder"/> class, configured as wma encoder.</returns>
        // ReSharper disable once InconsistentNaming
        public static MediaFoundationEncoder CreateWMAEncoder(WaveFormat sourceFormat, Stream targetStream,
                                                              int bitRate = 192000)
        {
            if (sourceFormat == null)
            {
                throw new ArgumentNullException("sourceFormat");
            }
            if (targetStream == null)
            {
                throw new ArgumentNullException("targetStream");
            }
            if (targetStream.CanWrite != true)
            {
                throw new ArgumentException("Stream not writeable.", "targetStream");
            }

            MFMediaType targetMediaType = FindBestMediaType(AudioSubTypes.WindowsMediaAudio,
                                                            sourceFormat.SampleRate, sourceFormat.Channels, bitRate);
            MFMediaType sourceMediaType = MediaFoundationCore.MediaTypeFromWaveFormat(sourceFormat);

            if (targetMediaType == null)
            {
                throw new PlatformNotSupportedException("No WMA-Encoder was found.");
            }

            return(new MediaFoundationEncoder(targetStream, sourceMediaType, targetMediaType,
                                              TranscodeContainerTypes.MFTranscodeContainerType_ASF));
        }
Example #5
0
        public static MFMediaType[] GetEncoderMediaTypes(Guid audioSubType)
        {
            IMFCollection collection;

            try
            {
                MediaFoundationException.Try(MFInterops.MFTranscodeGetAudioOutputAvailableTypes(audioSubType, MFTEnumFlags.All, null, out collection),
                                             "Interops",
                                             "MFTranscodeGetAudioOutputAvailableTypes");

                int count;
                MediaFoundationException.Try(collection.GetElementCount(out count), "IMFCollection", "GetElementCount");
                MFMediaType[] mediaTypes = new MFMediaType[count];
                for (int i = 0; i < count; i++)
                {
                    IntPtr ptr;
                    MediaFoundationException.Try(collection.GetElement(i, out ptr), "IMFCollection", "GetElement");

                    mediaTypes[i] = new MFMediaType(ptr);
                }

                Marshal.ReleaseComObject(collection);

                return(mediaTypes);
            }
            catch (MediaFoundationException ex)
            {
                if (ex.ErrorCode == unchecked ((int)0xC00D36D5)) // MF_E_NOT_FOUND
                {
                    return(Enumerable.Empty <MFMediaType>().ToArray());
                }

                throw;
            }
        }
Example #6
0
        /// <summary>
        /// Adds a stream to the sink writer.
        /// </summary>
        /// <param name="targetMediaType">The target mediatype which specifies the format of the samples that will be written to the file. It does not need to match the input format. To set the input format, call <see cref="SetInputMediaType"/>.</param>
        /// <returns>The zero-based index of the new stream.</returns>
        public int AddStream(MFMediaType targetMediaType)
        {
            int t;

            MediaFoundationException.Try(AddStreamNative(targetMediaType, out t), InterfaceName, "AddStream");
            return(t);
        }
Example #7
0
        /// <summary>
        /// Compares two media types and determines whether they are identical. If they are not
        /// identical, the method indicates how the two formats differ.
        /// </summary>
        /// <param name="mediaType">The <see cref="MFMediaType"/> to compare.</param>
        /// <returns>A bitwise OR of zero or more flags, indicating the degree of similarity between the two media types.</returns>
        public MediaTypeEqualFlags IsEqual(MFMediaType mediaType)
        {
            MediaTypeEqualFlags flags;

            MediaFoundationException.Try(IsEqualNative(mediaType, out flags), InterfaceName, "IsEqual");
            return(flags);
        }
Example #8
0
        /// <summary>
        /// Creates an new instance of the MediaFoundationEncoder.
        /// </summary>
        /// <param name="inputMediaType">Mediatype of the source which gets encoded.</param>
        /// <param name="stream">Stream which will be used to store the encoded data.</param>
        /// <param name="targetMediaType">The format, the data gets encoded to.</param>
        /// <param name="containerType">See TranscodeContainerTypes-class.</param>
        public MediaFoundationEncoder(Stream stream, MFMediaType inputMediaType, MFMediaType targetMediaType, Guid containerType)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }
            if (!stream.CanWrite)
            {
                throw new ArgumentException("Stream is not writeable.");
            }
            if (!stream.CanRead)
            {
                throw new ArgumentException("Stream is not readable.");
            }

            if (inputMediaType == null)
            {
                throw new ArgumentNullException("inputMediaType");
            }
            if (targetMediaType == null)
            {
                throw new ArgumentNullException("targetMediaType");
            }

            if (containerType == Guid.Empty)
            {
                throw new ArgumentException("containerType");
            }

            _targetBaseStream = stream;
            _inputMediaType   = inputMediaType;
            _targetMediaType  = targetMediaType;

            SetTargetStream(stream, inputMediaType, targetMediaType, containerType);
        }
Example #9
0
 /// <summary>
 /// Compares two media types and determines whether they are identical. If they are not
 /// identical, the method indicates how the two formats differ.
 /// </summary>
 /// <returns>HRESULT</returns>
 public unsafe int IsEqual(MFMediaType mediaType, out MediaTypeEqualFlags flags)
 {
     fixed(void *ptr = &flags)
     {
         return(InteropCalls.CalliMethodPtr(_basePtr, (void *)((mediaType == null) ? IntPtr.Zero : mediaType.BasePtr), new IntPtr(ptr), ((void **)(*(void **)_basePtr))[35]));
     }
 }
Example #10
0
 /// <summary>
 /// Adds a stream to the sink writer.
 /// </summary>
 /// <returns>HRESULT</returns>
 public unsafe int AddStreamNative(MFMediaType targetMediaType, out int streamIndex)
 {
     fixed (void* psi = &streamIndex)
     {
         void* ptmt = targetMediaType == null ? IntPtr.Zero.ToPointer() : targetMediaType.BasePtr.ToPointer();
         return InteropCalls.CalliMethodPtr(_basePtr, ptmt, psi, ((void**)(*(void**)_basePtr))[3]);
     }
 }
Example #11
0
        /// <summary>
        /// Gets the current media type for a stream.
        /// </summary>
        /// <param name="streamIndex">Specifies which stream to query. For more information, see <see href="https://msdn.microsoft.com/en-us/library/windows/desktop/dd374660(v=vs.85).aspx"/>.</param>
        /// <param name="mediaType">Receives the <see cref="MFMediaType"/>. The caller must dispose the <see cref="MFMediaType"/>.</param>
        /// <returns>HRESULT</returns>
        public unsafe int GetCurrentMediaTypeNative(int streamIndex, out MFMediaType mediaType)
        {
            IntPtr ptr    = IntPtr.Zero;
            var    result = InteropCalls.CalliMethodPtr(UnsafeBasePtr, streamIndex, &ptr, ((void **)(*(void **)UnsafeBasePtr))[6]);

            mediaType = new MFMediaType(ptr);
            return(result);
        }
Example #12
0
        public static MFMediaType MediaTypeFromWaveFormat(WaveFormat waveFormat)
        {
            var mediaType = MFMediaType.CreateEmpty();
            int result    = NativeMethods.MFInitMediaTypeFromWaveFormatEx(mediaType.BasePtr, waveFormat, Marshal.SizeOf(waveFormat));

            MediaFoundationException.Try(result, "Interops", "MFInitMediaTypeFromWaveFormatEx");
            return(mediaType);
        }
Example #13
0
        /// <summary>
        /// Gets a format that is supported natively by the media source.
        /// </summary>
        /// <returns>HRESULT</returns>
        public unsafe int GetNativeMediaTypeNative(int streamIndex, int mediatypeIndex, out MFMediaType mediaType)
        {
            IntPtr ptr    = IntPtr.Zero;
            var    result = InteropCalls.CalliMethodPtr(_basePtr, streamIndex, mediatypeIndex, &ptr, ((void **)(*(void **)_basePtr))[5]);

            mediaType = new MFMediaType(ptr);
            return(result);
        }
Example #14
0
        /// <summary>
        /// Adds a stream to the sink writer.
        /// <seealso cref="AddStream"/>
        /// </summary>
        /// <param name="targetMediaType">The target mediatype which specifies the format of the samples that will be written to the file. It does not need to match the input format. To set the input format, call <see cref="SetInputMediaType"/>.</param>
        /// <param name="streamIndex">Receives the zero-based index of the new stream.</param>
        /// <returns>HRESULT</returns>
        public unsafe int AddStreamNative(MFMediaType targetMediaType, out int streamIndex)
        {
            fixed(void *psi = &streamIndex)
            {
                void *ptmt = targetMediaType == null?IntPtr.Zero.ToPointer() : targetMediaType.BasePtr.ToPointer();

                return(InteropCalls.CalliMethodPtr(UnsafeBasePtr, ptmt, psi, ((void **)(*(void **)UnsafeBasePtr))[3]));
            }
        }
Example #15
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;
            }
        }
Example #16
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;
            }
        }
Example #17
0
        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();
                }
            }
        }
Example #19
0
        /// <summary>
        ///     Creates an new instance of the <see cref="MediaFoundationEncoder"/> class.
        /// </summary>
        /// <param name="inputMediaType">Mediatype of the source to encode.</param>
        /// <param name="stream">Stream which will be used to store the encoded data.</param>
        /// <param name="targetMediaType">The format of the encoded data.</param>
        /// <param name="containerType">See container type. For a list of all available container types, see <see cref="TranscodeContainerTypes"/>.</param>
        public MediaFoundationEncoder(Stream stream, MFMediaType inputMediaType, MFMediaType targetMediaType,
            Guid containerType)
        {
            if (stream == null)
                throw new ArgumentNullException("stream");
            if (!stream.CanWrite)
                throw new ArgumentException("Stream is not writeable.");
            if (!stream.CanRead)
                throw new ArgumentException("Stream is not readable.");

            if (inputMediaType == null)
                throw new ArgumentNullException("inputMediaType");
            if (targetMediaType == null)
                throw new ArgumentNullException("targetMediaType");

            if (containerType == Guid.Empty)
                throw new ArgumentException("containerType");

            _targetMediaType = targetMediaType;

            SetTargetStream(stream, inputMediaType, targetMediaType, containerType);
        }
Example #20
0
 /// <summary>
 /// Sets the input format for a stream on the sink writer.
 /// </summary>
 /// <param name="streamIndex">The zero-based index of the stream. The index is returned by the <see cref="AddStream"/> method.</param>
 /// <param name="inputMediaType">The input media type that specifies the input format.</param>
 /// <param name="encodingParameters">An attribute store. Use the attribute store to configure the encoder. This parameter can be NULL.</param>
 public void SetInputMediaType(int streamIndex, MFMediaType inputMediaType, MFAttributes encodingParameters)
 {
     MediaFoundationException.Try(SetInputMediaTypeNative(streamIndex, inputMediaType, encodingParameters), InterfaceName, "SetInputMediaType");
 }
Example #21
0
 /// <summary>
 /// Compares two media types and determines whether they are identical. If they are not
 /// identical, the method indicates how the two formats differ.
 /// </summary>
 /// <param name="mediaType">The <see cref="MFMediaType"/> to compare.</param>
 /// <param name="flags">Receives a bitwise OR of zero or more flags, indicating the degree of similarity between the two media types.</param>
 /// <returns>HRESULT</returns>
 public unsafe int IsEqualNative(MFMediaType mediaType, out MediaTypeEqualFlags flags)
 {
     fixed (void* ptr = &flags)
     {
         return InteropCalls.CalliMethodPtr(UnsafeBasePtr, (void*)((mediaType == null) ? IntPtr.Zero : mediaType.BasePtr), new IntPtr(ptr), ((void**)(*(void**)UnsafeBasePtr))[35]);
     }
 }
Example #22
0
 /// <summary>
 /// Compares two media types and determines whether they are identical. If they are not
 /// identical, the method indicates how the two formats differ.
 /// </summary>
 /// <param name="mediaType">The <see cref="MFMediaType"/> to compare.</param>
 /// <returns>A bitwise OR of zero or more flags, indicating the degree of similarity between the two media types.</returns>
 public MediaTypeEqualFlags IsEqual(MFMediaType mediaType)
 {
     MediaTypeEqualFlags flags;
     MediaFoundationException.Try(IsEqualNative(mediaType, out flags), InterfaceName, "IsEqual");
     return flags;
 }
        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);
            }
        }
Example #24
0
 /// <summary>
 /// Sets the input format for a stream on the sink writer.
 /// </summary>
 /// <param name="encodingParameters">Optional</param>
 /// <returns>HRESULT</returns>
 public unsafe int SetInputMediaTypeNative(int streamIndex, MFMediaType inputMediaType, MFAttributes encodingParameters)
 {
     void* imt = (void*)(inputMediaType == null ? IntPtr.Zero : inputMediaType.BasePtr);
     void* ep = (void*)(encodingParameters == null ? IntPtr.Zero : encodingParameters.BasePtr);
     return InteropCalls.CalliMethodPtr(_basePtr, streamIndex, imt, ep, ((void**)(*(void**)_basePtr))[4]);
 }
Example #25
0
        /// <summary>
        /// Returns all <see cref="MFMediaType"/>s available for encoding the specified <paramref name="audioSubType"/>.
        /// </summary>
        /// <param name="audioSubType">The audio subtype to search available <see cref="MFMediaType"/>s for.</param>
        /// <returns>Available <see cref="MFMediaType"/>s for the specified <paramref name="audioSubType"/>. If the <see cref="GetEncoderMediaTypes"/> returns an empty array, no encoder for the specified <paramref name="audioSubType"/> was found.</returns>
        public static MFMediaType[] GetEncoderMediaTypes(Guid audioSubType)
        {
            try
            {
                IMFCollection collection;

                MediaFoundationException.Try(
                    NativeMethods.MFTranscodeGetAudioOutputAvailableTypes(audioSubType, MFTEnumFlags.All,
                        IntPtr.Zero, out collection),
                    "Interops",
                    "MFTranscodeGetAudioOutputAvailableTypes");
                try
                {
                    int count;
                    MediaFoundationException.Try(collection.GetElementCount(out count), "IMFCollection",
                        "GetElementCount");
                    MFMediaType[] mediaTypes = new MFMediaType[count];
                    for (int i = 0; i < count; i++)
                    {
                        IntPtr ptr;
                        MediaFoundationException.Try(collection.GetElement(i, out ptr), "IMFCollection", "GetElement");

                        mediaTypes[i] = new MFMediaType(ptr);
                    }

                    return mediaTypes;
                }
                finally
                {
                    Marshal.ReleaseComObject(collection);
                }
            }
            catch (MediaFoundationException ex)
            {
                if (ex.ErrorCode == unchecked((int)0xC00D36D5)) // MF_E_NOT_FOUND
                {
                    return Enumerable.Empty<MFMediaType>().ToArray();
                }

                throw;
            }
        }
Example #26
0
 /// <summary>
 /// This media type defines that format that the Source Reader produces as output. It can
 /// differ from the native format provided by the media source. See Remarks for more
 /// information.
 /// </summary>
 /// <returns>HRESULT</returns>
 public unsafe int SetCurrentMediaTypeNative(int streamIndex, IntPtr reserved, MFMediaType mediaType)
 {
     return InteropCalls.CalliMethodPtr(_basePtr, streamIndex, reserved, (void*)((mediaType == null) ? IntPtr.Zero : mediaType.BasePtr), ((void**)(*(void**)_basePtr))[7]);
 }
Example #27
0
 /// <summary>
 /// This media type defines that format that the Source Reader produces as output. It can
 /// differ from the native format provided by the media source. See Remarks for more
 /// information.
 /// </summary>
 public void SetCurrentMediaType(int streamIndex, MFMediaType mediaType)
 {
     MediaFoundationException.Try(SetCurrentMediaTypeNative(streamIndex, IntPtr.Zero, mediaType), c, "SetCurrentMediaType");
 }
Example #28
0
 /// <summary>
 /// Adds a stream to the sink writer.
 /// </summary>
 /// <returns>The zero-based index of the new stream.</returns>
 public int AddStream(MFMediaType targetMediaType)
 {
     int t;
     MediaFoundationException.Try(AddStreamNative(targetMediaType, out t), c, "AddStream");
     return t;
 }
Example #29
0
 /// <summary>
 /// This media type defines that format that the Source Reader produces as output. It can
 /// differ from the native format provided by the media source. See Remarks for more
 /// information.
 /// </summary>
 /// <returns>HRESULT</returns>
 public unsafe int SetCurrentMediaTypeNative(int streamIndex, IntPtr reserved, MFMediaType mediaType)
 {
     return(InteropCalls.CalliMethodPtr(_basePtr, streamIndex, reserved, (void *)((mediaType == null) ? IntPtr.Zero : mediaType.BasePtr), ((void **)(*(void **)_basePtr))[7]));
 }
Example #30
0
 /// <summary>
 /// This media type defines that format that the Source Reader produces as output. It can
 /// differ from the native format provided by the media source. See Remarks for more
 /// information.
 /// </summary>
 public void SetCurrentMediaType(int streamIndex, MFMediaType mediaType)
 {
     MediaFoundationException.Try(SetCurrentMediaTypeNative(streamIndex, IntPtr.Zero, mediaType), c, "SetCurrentMediaType");
 }
Example #31
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();
            }
        }
Example #32
0
 /// <summary>
 /// Compares two media types and determines whether they are identical. If they are not
 /// identical, the method indicates how the two formats differ.
 /// </summary>
 public MediaTypeEqualFlags IsEqual(MFMediaType mediaType)
 {
     MediaTypeEqualFlags flags;
     MediaFoundationException.Try(IsEqual(mediaType, out flags), c, "IsEqual");
     return flags;
 }
Example #33
0
 /// <summary>
 /// Sets the input format for a stream on the sink writer.
 /// </summary>
 /// <param name="encodingParameters">Optional</param>
 public void SetInputMediaType(int streamIndex, MFMediaType inputMediaType, MFAttributes encodingParameters)
 {
     MediaFoundationException.Try(SetInputMediaTypeNative(streamIndex, inputMediaType, encodingParameters), c, "SetInputMediaType");
 }