Exemple #1
0
        internal override void AsNativeHandle(IntPtr handle)
        {
            Debug.Assert(Type == MediaFormatType.Audio);

            int ret = Native.SetAudioMimeType(handle, MimeType);

            MultimediaDebug.AssertNoError(ret);

            ret = Native.SetAudioChannel(handle, Channel);
            MultimediaDebug.AssertNoError(ret);

            ret = Native.SetAudioSampleRate(handle, SampleRate);
            MultimediaDebug.AssertNoError(ret);

            ret = Native.SetAudioBit(handle, Bit);
            MultimediaDebug.AssertNoError(ret);

            ret = Native.SetAudioAverageBps(handle, BitRate);
            MultimediaDebug.AssertNoError(ret);

            ret = Native.SetAudioAacType(handle, AacType);
            MultimediaDebug.AssertNoError(ret);

            if (AudioChannelMap != null)
            {
                ret = Native.SetAudioChannelMask(handle, GetAudioChannelMask(handle, AudioChannelMap));
                MultimediaDebug.AssertNoError(ret);
            }
        }
Exemple #2
0
        private static ulong GetAudioChannelMask(IntPtr handle, IList <MediaFormatAudioChannelPosition> audioChannelMap)
        {
            int ret = Native.GetMaskFromChannelPosition(handle, audioChannelMap.ToArray(),
                                                        out ulong mask);

            MultimediaDebug.AssertNoError(ret);

            return(mask);
        }
Exemple #3
0
        /// <summary>
        /// Retrieves the AAC type value from a native handle.
        /// </summary>
        /// <param name="handle">A native handle that the properties are retrieved from.</param>
        private static MediaFormatAacType GetAacType(IntPtr handle)
        {
            Debug.Assert(handle != IntPtr.Zero, "The handle is invalid!");

            int ret = Native.GetAudioAacType(handle, out var aacType);

            MultimediaDebug.AssertNoError(ret);

            Debug.Assert(Enum.IsDefined(typeof(MediaFormatAacType), aacType), "Invalid aac type!");

            return(aacType);
        }
Exemple #4
0
        private static ReadOnlyCollection <MediaFormatAudioChannelPosition> GetAudioChannelMap(IntPtr handle)
        {
            var ret = Native.GetAudioChannelMask(handle, out ulong mask);

            MultimediaDebug.AssertNoError(ret);

            ret = Native.GetChannelPositionFromMask(handle, mask, out MediaFormatAudioChannelPosition[] positions);
            MultimediaDebug.AssertNoError(ret);

            return(positions == null ? null :
                   new ReadOnlyCollection <MediaFormatAudioChannelPosition>(positions.Distinct().OrderBy(p => p).ToList()));
        }
Exemple #5
0
        /// <summary>
        /// Retrieves audio properties of the media format from a native handle.
        /// </summary>
        /// <param name="handle">A native handle that the properties are retrieved from.</param>
        /// <param name="mimeType">An out parameter for the mime type.</param>
        /// <param name="channel">An out parameter for the channel.</param>
        /// <param name="sampleRate">An out parameter for the sample rate.</param>
        /// <param name="bit">An out parameter for the bit.</param>
        /// <param name="bitRate">An out parameter for the bit rate.</param>
        private static void GetInfo(IntPtr handle, out MediaFormatAudioMimeType mimeType,
                                    out int channel, out int sampleRate, out int bit, out int bitRate)
        {
            Debug.Assert(handle != IntPtr.Zero, "The handle is invalid!");

            int ret = Native.GetAudioInfo(handle,
                                          out mimeType, out channel, out sampleRate, out bit, out bitRate);

            MultimediaDebug.AssertNoError(ret);

            Debug.Assert(Enum.IsDefined(typeof(MediaFormatAudioMimeType), mimeType),
                         "Invalid audio mime type!");
        }
Exemple #6
0
        /// <summary>
        /// Initializes a new instance of the MediaPacket class from a native handle.
        /// </summary>
        /// <param name="handle">The native handle to be used.</param>
        internal MediaPacket(IntPtr handle)
        {
            _handle = handle;

            int ret = Native.GetFormat(handle, out IntPtr formatHandle);

            MultimediaDebug.AssertNoError(ret);

            try
            {
                if (formatHandle != IntPtr.Zero)
                {
                    _format = MediaFormat.FromHandle(formatHandle);
                }
            }
            finally
            {
                NativeFormat.Unref(formatHandle);
            }
        }
Exemple #7
0
        /// <summary>
        /// Creates and initializes a native handle for the current object.
        /// </summary>
        /// <param name="format">The format to be set to the media format.</param>
        /// <exception cref="InvalidOperationException">Operation failed.</exception>
        private void Initialize(MediaFormat format)
        {
            if (format.Type == MediaFormatType.Container)
            {
                throw new ArgumentException("Container format can't be used to create a new packet.",
                                            nameof(format));
            }

            IntPtr formatHandle = IntPtr.Zero;

            try
            {
                formatHandle = format.AsNativeHandle();

                int ret = Native.Create(formatHandle, IntPtr.Zero, IntPtr.Zero, out _handle);
                MultimediaDebug.AssertNoError(ret);

                Debug.Assert(_handle != IntPtr.Zero, "Created handle must not be null");

                Alloc();
            }
            catch (Exception)
            {
                if (_handle != IntPtr.Zero)
                {
                    Native.Destroy(_handle);
                    _handle = IntPtr.Zero;
                }

                throw;
            }
            finally
            {
                if (formatHandle != IntPtr.Zero)
                {
                    NativeFormat.Unref(formatHandle);
                }
            }
        }