Esempio n. 1
0
        private static string GetVideoFormat(MediaType mediaType)
        {
            // https://docs.microsoft.com/en-us/windows/desktop/medfound/video-subtype-guids
            var subTypeId     = mediaType.Get(MediaTypeAttributeKeys.Subtype);
            var fourccEncoded = BitConverter.ToInt32(subTypeId.ToByteArray(), 0);
            var fourcc        = new FourCC(fourccEncoded);

            return(fourcc.ToString());
        }
Esempio n. 2
0
        private string ShortDescription(MediaType mediaType)
        {
            Guid subType        = mediaType.Get(MediaTypeAttributeKeys.Subtype);
            int  sampleRate     = mediaType.Get(MediaTypeAttributeKeys.AudioSamplesPerSecond);
            int  bytesPerSecond = mediaType.Get(MediaTypeAttributeKeys.AudioAvgBytesPerSecond);
            int  channels       = mediaType.Get(MediaTypeAttributeKeys.AudioNumChannels);
            // TODO: this one is optional
            int bitsPerSample = -1;

            try
            {
                bitsPerSample = mediaType.Get(MediaTypeAttributeKeys.AudioBitsPerSample);
            }
            catch (SharpDXException)
            {
                // key doesn't exist
            }


            //int bitsPerSample;
            //mediaType.GetUINT32(MediaFoundationAttributes.MF_MT_AUDIO_BITS_PER_SAMPLE, out bitsPerSample);
            var shortDescription = new StringBuilder();

            shortDescription.AppendFormat("{0:0.#}kbps, ", (8 * bytesPerSecond) / 1000M);
            shortDescription.AppendFormat("{0:0.#}kHz, ", sampleRate / 1000M);
            if (bitsPerSample != -1)
            {
                shortDescription.AppendFormat("{0} bit, ", bitsPerSample);
            }
            shortDescription.AppendFormat("{0}, ", channels == 1 ? "mono" : channels == 2 ? "stereo" : channels + " channels");
            if (subType == AudioFormatGuids.Aac)
            {
                int payloadType = -1;
                try
                {
                    payloadType = mediaType.Get(MediaTypeAttributeKeys.AacPayloadType);
                }
                catch (SharpDXException)
                {
                    // key doesn't exist
                }

                if (payloadType != -1)
                {
                    shortDescription.AppendFormat("Payload Type: {0}, ", (AacPayloadType)payloadType);
                }
            }
            shortDescription.Length -= 2;
            return(shortDescription.ToString());
        }
Esempio n. 3
0
        /// <summary>
        /// Called by the ctor to configure the media playback component.
        /// </summary>
        private void InitializeMediaPipeline()
        {
            MediaManager.Startup(false);
            MediaAttributes sourceReaderAttributes = new MediaAttributes();

            sourceReaderAttributes.Set(SourceReaderAttributeKeys.EnableAdvancedVideoProcessing, true);
            this.sourceReader = new SourceReader(this.filename, sourceReaderAttributes);
            this.sourceReader.SetStreamSelection(SourceReaderIndex.AllStreams, false);

            int  streamIndex     = 0;
            bool doneEnumerating = false;

            while (!doneEnumerating)
            {
                try
                {
                    MediaType mediaType = this.sourceReader.GetCurrentMediaType(streamIndex);
                    var       subType   = mediaType.Get(MediaTypeAttributeKeys.Subtype);
                    DumpMediaType(mediaType);

                    if (mediaType.MajorType == MediaTypeGuids.Video && this.imageStreamIndex == -1)
                    {
                        this.imageStreamIndex = streamIndex;

                        // get the image size
                        long frameSize = mediaType.Get(MediaTypeAttributeKeys.FrameSize);
                        this.videoHeight = (short)frameSize;
                        this.videoWidth  = (short)(frameSize >> 32);

                        // enable the stream and set the current media type
                        this.sourceReader.SetStreamSelection(this.imageStreamIndex, true);
                        mediaType = new MediaType();
                        mediaType.Set(MediaTypeAttributeKeys.MajorType, MediaTypeGuids.Video);
                        mediaType.Set(MediaTypeAttributeKeys.Subtype, VideoFormatGuids.Rgb24);
                        mediaType.Set(MediaTypeAttributeKeys.FrameSize, frameSize);
                        this.sourceReader.SetCurrentMediaType(this.imageStreamIndex, mediaType);
                    }
                    else if (mediaType.MajorType == MediaTypeGuids.Audio && this.audioStreamIndex == -1)
                    {
                        this.audioStreamIndex = streamIndex;

                        // enable the stream and set the current media type to PCM
                        this.sourceReader.SetStreamSelection(this.audioStreamIndex, true);
                        mediaType = new MediaType();
                        mediaType.Set(MediaTypeAttributeKeys.MajorType, MediaTypeGuids.Audio);
                        mediaType.Set(MediaTypeAttributeKeys.Subtype, AudioFormatGuids.Pcm);
                        this.sourceReader.SetCurrentMediaType(this.audioStreamIndex, mediaType);

                        // get back all the media type details
                        mediaType = this.sourceReader.GetCurrentMediaType(this.audioStreamIndex);
                        int numberOfChannels = mediaType.Get(MediaTypeAttributeKeys.AudioNumChannels);
                        int sampleRate       = mediaType.Get(MediaTypeAttributeKeys.AudioSamplesPerSecond);
                        int bitsPerSample    = mediaType.Get(MediaTypeAttributeKeys.AudioBitsPerSample);

                        // post our output audio format
                        this.waveFormat = WaveFormat.CreatePcm(sampleRate, bitsPerSample, numberOfChannels);
                    }
                }
                catch (Exception e)
                {
                    Debug.Write(e.GetType());

                    // expected thrown exception
                    // unfortunately no way to tell how many streams other than trying
                    doneEnumerating = true;
                }

                streamIndex += 1;
            }
        }
Esempio n. 4
0
        private static void DumpMediaType(MediaType mediaType)
        {
            var subType = mediaType.Get(MediaTypeAttributeKeys.Subtype);

            Debug.WriteLine($"Found stream MajorType: {mediaType.MajorType} SubType: {subType}");
        }