Пример #1
0
        /////////////////////////////////////////////////////////////////////
        // Name: CreateASFSplitter
        //
        // Creates the ASF splitter.
        //
        // pContentInfo: Pointer to an initialized instance of the ASF
        //               content information object.
        // ppSplitter:   Receives a pointer to the ASF splitter.
        /////////////////////////////////////////////////////////////////////

        void CreateASFSplitter(IMFASFContentInfo pContentInfo, out IMFASFSplitter ppSplitter)
        {
            MFASFSplitterFlags f;
            HResult            hr;

            hr = MFExtern.MFCreateASFSplitter(out ppSplitter);
            MFError.ThrowExceptionForHR(hr);

            hr = ppSplitter.Initialize(pContentInfo);
            MFError.ThrowExceptionForHR(hr);

            hr = ppSplitter.GetFlags(out f);
            MFError.ThrowExceptionForHR(hr);
            Console.WriteLine(string.Format("Splitter flags: {0}", f));
        }
Пример #2
0
        /////////////////////////////////////////////////////////////////////
        // Name: CreateContentInfo
        //
        // Reads the ASF Header Object from a byte stream and returns a
        // pointer to the ASF content information object.
        //
        // pStream:       Pointer to the byte stream. The byte stream's
        //                current read position must be at the start of the
        //                ASF Header Object.
        // ppContentInfo: Receives a pointer to the ASF content information
        //                object.
        /////////////////////////////////////////////////////////////////////

        void CreateContentInfo(
            IMFByteStream pStream,
            out IMFASFContentInfo ppContentInfo
            )
        {
            HResult hr;
            long    cbHeader = 0;

            const int MIN_ASF_HEADER_SIZE = 30;

            IMFMediaBuffer pBuffer;

            // Create the ASF content information object.
            hr = MFExtern.MFCreateASFContentInfo(out ppContentInfo);
            MFError.ThrowExceptionForHR(hr);

            // Read the first 30 bytes to find the total header size.
            ReadDataIntoBuffer(pStream, MIN_ASF_HEADER_SIZE, out pBuffer);

            try
            {
                hr = ppContentInfo.GetHeaderSize(pBuffer, out cbHeader);
                MFError.ThrowExceptionForHR(hr);

                // Pass the first 30 bytes to the content information object.
                hr = ppContentInfo.ParseHeader(pBuffer, 0);
                MFError.ThrowExceptionForHR(hr);
            }
            finally
            {
                SafeRelease(pBuffer);
            }

            // Read the rest of the header and finish parsing the header.
            ReadDataIntoBuffer(pStream, (int)(cbHeader - MIN_ASF_HEADER_SIZE), out pBuffer);

            hr = ppContentInfo.ParseHeader(pBuffer, MIN_ASF_HEADER_SIZE);
            MFError.ThrowExceptionForHR(hr);
        }
Пример #3
0
        public void DoSplit()
        {
            HResult hr;
            bool    bHasVideo = false;

            IMFByteStream     pStream      = null;
            IMFASFContentInfo pContentInfo = null;
            IMFASFSplitter    pSplitter    = null;

            Console.WriteLine(string.Format("Opening {0}.", m_sFileName));

            try
            {
                // Start the Media Foundation platform.
                hr = MFExtern.MFStartup(0x10070, MFStartup.Full);
                MFError.ThrowExceptionForHR(hr);

                // Open the file.
                OpenFile(m_sFileName, out pStream);

                // Read the ASF header.
                CreateContentInfo(pStream, out pContentInfo);

                // Create the ASF splitter.
                CreateASFSplitter(pContentInfo, out pSplitter);

                // Select the first video stream.
                SelectVideoStream(pContentInfo, pSplitter, out bHasVideo);

                // Parse the ASF file.
                if (bHasVideo)
                {
                    DisplayKeyFrames(pStream, pSplitter);
                }
                else
                {
                    Console.WriteLine("No video stream.");
                }
            }
            catch (Exception e)
            {
                hr = (HResult)Marshal.GetHRForException(e);
                string s = MFError.GetErrorText(hr);

                if (s == null)
                {
                    s = e.Message;
                }
                else
                {
                    s = string.Format("{0} ({1})", s, e.Message);
                }

                Console.WriteLine(string.Format("Exception 0x{0:x}: {1}", hr, s));
            }
            finally
            {
                // Clean up.
                SafeRelease(pSplitter);
                SafeRelease(pContentInfo);
                SafeRelease(pStream);
            }

            // Shut down the Media Foundation platform.
            hr = MFExtern.MFShutdown();
            MFError.ThrowExceptionForHR(hr);
        }
Пример #4
0
        /////////////////////////////////////////////////////////////////////
        // Name: SelectVideoStream
        //
        // Selects the first video stream for parsing with the ASF splitter.
        //
        // pContentInfo: Pointer to an initialized instance of the ASF
        //               content information object.
        // pSplitter:    Pointer to the ASF splitter.
        // pbHasVideo:   Receives TRUE if there is a video stream, or FALSE
        //               otherwise.
        /////////////////////////////////////////////////////////////////////

        void SelectVideoStream(
            IMFASFContentInfo pContentInfo,
            IMFASFSplitter pSplitter,
            out bool pbHasVideo
            )
        {
            int   cStreams;
            short wStreamID = 33;

            short[] wStreamIDs = new short[1];
            Guid    streamType;
            bool    bFoundVideo = false;
            HResult hr;

            IMFASFProfile      pProfile;
            IMFASFStreamConfig pStream;

            // Get the ASF profile from the content information object.
            hr = pContentInfo.GetProfile(out pProfile);
            MFError.ThrowExceptionForHR(hr);

            try
            {
                // Loop through all of the streams in the profile.
                hr = pProfile.GetStreamCount(out cStreams);
                MFError.ThrowExceptionForHR(hr);

                for (int i = 0; i < cStreams; i++)
                {
                    // Get the stream type and stream identifier.
                    hr = pProfile.GetStream(i, out wStreamID, out pStream);
                    MFError.ThrowExceptionForHR(hr);

                    try
                    {
                        hr = pStream.GetStreamType(out streamType);
                        MFError.ThrowExceptionForHR(hr);

                        if (streamType == MFMediaType.Video)
                        {
                            bFoundVideo = true;
                            break;
                        }
                    }
                    finally
                    {
                        SafeRelease(pStream);
                    }
                }
            }
            finally
            {
                SafeRelease(pProfile);
            }

            // Select the video stream, if found.
            if (bFoundVideo)
            {
                // SelectStreams takes an array of stream identifiers.
                wStreamIDs[0] = wStreamID;
                hr            = pSplitter.SelectStreams(wStreamIDs, 1);
                MFError.ThrowExceptionForHR(hr);
            }

            pbHasVideo = bFoundVideo;
        }
Пример #5
0
 public static extern void MFCreateASFMediaSinkActivate(
     [MarshalAs(UnmanagedType.LPWStr)] string pwszFileName,
     IMFASFContentInfo pContentInfo,
     out IMFActivate ppIActivate
     );
Пример #6
0
 public static extern void MFCreateASFContentInfo(
     out IMFASFContentInfo ppIContentInfo);
Пример #7
0
        /////////////////////////////////////////////////////////////////////
        // Name: SelectVideoStream
        //
        // Selects the first video stream for parsing with the ASF splitter.
        //
        // pContentInfo: Pointer to an initialized instance of the ASF
        //               content information object.
        // pSplitter:    Pointer to the ASF splitter.
        // pbHasVideo:   Receives TRUE if there is a video stream, or FALSE
        //               otherwise.
        /////////////////////////////////////////////////////////////////////
        void SelectVideoStream(
            IMFASFContentInfo pContentInfo,
            IMFASFSplitter pSplitter,
            out bool pbHasVideo
            )
        {
            int cStreams;
            short wStreamID = 33;
            short[] wStreamIDs = new short[1];
            Guid streamType;
            bool bFoundVideo = false;

            IMFASFProfile pProfile;
            IMFASFStreamConfig pStream;

            // Get the ASF profile from the content information object.
            int hr = pContentInfo.GetProfile(out pProfile);
            MFError.ThrowExceptionForHR(hr);

            try
            {
                // Loop through all of the streams in the profile.
                hr = pProfile.GetStreamCount(out cStreams);
                MFError.ThrowExceptionForHR(hr);

                for (int i = 0; i < cStreams; i++)
                {
                    // Get the stream type and stream identifier.
                    hr = pProfile.GetStream(i, out wStreamID, out pStream);
                    MFError.ThrowExceptionForHR(hr);

                    try
                    {
                        hr = pStream.GetStreamType(out streamType);
                        MFError.ThrowExceptionForHR(hr);

                        if (streamType == MFMediaType.Video)
                        {
                            bFoundVideo = true;
                            break;
                        }
                    }
                    finally
                    {
                        SafeRelease(pStream);
                    }
                }
            }
            finally
            {
                SafeRelease(pProfile);
            }

            // Select the video stream, if found.
            if (bFoundVideo)
            {
                // SelectStreams takes an array of stream identifiers.
                wStreamIDs[0] = wStreamID;
                hr = pSplitter.SelectStreams(wStreamIDs, 1);
                MFError.ThrowExceptionForHR(hr);
            }

            pbHasVideo = bFoundVideo;
        }
Пример #8
0
        void CreateIndexer(IMFASFContentInfo pContentInfo, out IMFASFIndexer ai)
        {
            MFAsfIndexerFlags f;
            long l;
            int i;
            IMFMediaBuffer mb;
            int hr;

            hr = MFExtern.MFCreateASFIndexer(out ai);
            MFError.ThrowExceptionForHR(hr);
            hr = MFExtern.MFCreateMemoryBuffer(1000, out mb);
            MFError.ThrowExceptionForHR(hr);

            hr = ai.Initialize(pContentInfo);
            MFError.ThrowExceptionForHR(hr);

            hr = ai.GetIndexPosition(pContentInfo, out l);
            MFError.ThrowExceptionForHR(hr);

            hr = ai.GetFlags(out f);
            MFError.ThrowExceptionForHR(hr);

            hr = ai.SetFlags(f);
            MFError.ThrowExceptionForHR(hr);

            hr = ai.GetIndexByteStreamCount(out i);
            MFError.ThrowExceptionForHR(hr);

            hr = ai.GetCompletedIndex(mb, 0);
            MFError.ThrowExceptionForHR(hr);

            hr = ai.GetIndexWriteSpace(out l);
            MFError.ThrowExceptionForHR(hr);
        }
Пример #9
0
        /////////////////////////////////////////////////////////////////////
        // Name: CreateContentInfo
        //
        // Reads the ASF Header Object from a byte stream and returns a
        // pointer to the ASF content information object.
        //
        // pStream:       Pointer to the byte stream. The byte stream's
        //                current read position must be at the start of the
        //                ASF Header Object.
        // ppContentInfo: Receives a pointer to the ASF content information
        //                object.
        /////////////////////////////////////////////////////////////////////
        void CreateContentInfo(
            IMFByteStream pStream,
            out IMFASFContentInfo ppContentInfo
            )
        {
            long cbHeader = 0;

            const int MIN_ASF_HEADER_SIZE = 30;

            IMFMediaBuffer pBuffer;

            // Create the ASF content information object.
            int hr = MFExtern.MFCreateASFContentInfo(out ppContentInfo);
            MFError.ThrowExceptionForHR(hr);

            // Read the first 30 bytes to find the total header size.
            ReadDataIntoBuffer(pStream, MIN_ASF_HEADER_SIZE, out pBuffer);

            try
            {
                hr = ppContentInfo.GetHeaderSize(pBuffer, out cbHeader);
                MFError.ThrowExceptionForHR(hr);

                // Pass the first 30 bytes to the content information object.
                hr = ppContentInfo.ParseHeader(pBuffer, 0);
                MFError.ThrowExceptionForHR(hr);
            }
            finally
            {
                SafeRelease(pBuffer);
            }

            // Read the rest of the header and finish parsing the header.
            ReadDataIntoBuffer(pStream, (int)(cbHeader - MIN_ASF_HEADER_SIZE), out pBuffer);

            hr = ppContentInfo.ParseHeader(pBuffer, MIN_ASF_HEADER_SIZE);
            MFError.ThrowExceptionForHR(hr);
        }
Пример #10
0
        /////////////////////////////////////////////////////////////////////
        // Name: CreateASFSplitter
        //
        // Creates the ASF splitter.
        //
        // pContentInfo: Pointer to an initialized instance of the ASF
        //               content information object.
        // ppSplitter:   Receives a pointer to the ASF splitter.
        /////////////////////////////////////////////////////////////////////
        void CreateASFSplitter(IMFASFContentInfo pContentInfo, out IMFASFSplitter ppSplitter)
        {
            MFASFSplitterFlags f;

            int hr = MFExtern.MFCreateASFSplitter(out ppSplitter);
            MFError.ThrowExceptionForHR(hr);
            hr = ppSplitter.Initialize(pContentInfo);
            MFError.ThrowExceptionForHR(hr);

            hr = ppSplitter.GetFlags(out f);
            MFError.ThrowExceptionForHR(hr);
            Console.WriteLine(string.Format("Splitter flags: {0}", f));
        }