Beispiel #1
0
        void RenderThread(object obj)
        {
            IntPtr hWnd = (IntPtr)obj;

            AxisMediaViewer viewer = new AxisMediaViewer();

            viewer.VMR9 = true;

            try
            {
                using (FileStream inFileStream = new FileStream(videoPath, FileMode.Open)) using (BinaryReader inFile = new BinaryReader(inFileStream, Encoding.UTF32))
                    {
                        if (inFile.PeekChar() != -1)
                        {
                            int    mediaTypeSize   = inFile.ReadInt32();
                            byte[] mediaTypeBuffer = inFile.ReadBytes(mediaTypeSize);

                            long startPosition = inFile.BaseStream.Position;

                            while (true)
                            {
                                viewer.Init(1, mediaTypeBuffer, hWnd.ToInt64());

                                viewer.Start();

                                while (inFile.PeekChar() != -1)
                                {
                                    // Read frame data
                                    int    sampleType  = inFile.ReadInt32();
                                    int    sampleFlags = inFile.ReadInt32();
                                    ulong  startTime   = inFile.ReadUInt64();
                                    ulong  stopTime    = inFile.ReadUInt64();
                                    int    bufferSize  = inFile.ReadInt32();
                                    byte[] bufferBytes = inFile.ReadBytes(bufferSize);
                                    // Check that it’s not an audio sample.
                                    if (sampleType != (int)AMV_VIDEO_SAMPLE_TYPE.AMV_VST_MPEG4_AUDIO)
                                    {
                                        // Let the viewer render the frame
                                        viewer.SetVideoPosition(VideoXPos, VideoYPos, VideoXPos + VideoWidth, VideoYPos + VideoHeight);
                                        viewer.RenderVideoSample(sampleFlags, startTime, stopTime, bufferBytes);
                                    }
                                }

                                inFile.BaseStream.Position = startPosition;

                                viewer.Stop();
                            }
                        }
                        else
                        {
                            Console.WriteLine(string.Format("File at {0} is empty or malformed", videoPath));
                            Environment.Exit(0);
                        }
                    }
            }
            catch (COMException e)
            {
                MessageBox.Show(string.Format("Exception {0}", e.Message));
            }
        }
        private void Axis_VideoPlay()
        {
            Task.Factory.StartNew(() =>
            {
                // Create AXIS Media Parser and AXIS Media Viewer components
                parser = new AxisMediaParser();
                viewer = new AxisMediaViewer();
            }).Wait();

            //事件注册
            parser.OnVideoSample    += OnVideoSample;
            parser.OnAudioSample    += OnAudioSample;
            parser.OnMetaDataSample += OnMetaDataSample;
            parser.OnError          += OnError;

            //流媒体参数
            parser.ShowLoginDialog = true;
            StringBuilder sbUrl = new StringBuilder();

            sbUrl.Append("axrtsphttp://");
            sbUrl.Append(CurrentVideoInfo.DVSAddress);
            sbUrl.Append("/axis-media/media.amp?videocodec=JPEG");
            sbUrl.Append("&camera=" + CurrentCameraInfo.Channel);
            string strUrl = sbUrl.ToString();

            parser.MediaURL      = strUrl;
            parser.MediaUsername = CurrentVideoInfo.UserName;
            parser.MediaPassword = CurrentVideoInfo.Password;

            //设置页面参数
            viewer.VideoRenderer = (int)AMV_RENDER_OPTIONS.AMV_VIDEO_RENDERER_EVR;      //视频解码器,注意回放视频是要同一
            //图片回调
            //viewer.EnableOnDecodedImage = true;
            //viewer.OnDecodedImage += OnDecodedImage;
            //viewer.ColorSpace = (short)AMV_COLOR_SPACE.AMV_CS_RGB24;
            viewer.EnableOnDecodedImage = false;
            viewer.ColorSpace           = (short)AMV_COLOR_SPACE.AMV_CS_YUY2;
            viewer.LiveMode             = true; //实时模式

            int    cookieID;
            int    numberOfStreams;
            object objmediaTypeBuffer;

            parser.Connect(out cookieID, out numberOfStreams, out objmediaTypeBuffer); //连接
            viewer.Init(0, objmediaTypeBuffer, intptrPlayMain.ToInt64());              //初始化
            viewer.SetVideoPosition(0, 0, VideoplayWindowWidth, VideoplayWindowHeight);
            if (CurrentVideoPlaySet.VideoRecordEnable)
            {
                lstVideoRecord = new List <byte>();
                byte[] bytsMediaType = (byte[])objmediaTypeBuffer;
                lstVideoRecord.AddRange(BitConverter.GetBytes(bytsMediaType.Length));
                lstVideoRecord.AddRange(bytsMediaType);
                VideoRecordStatus = true;
            }
            viewer.Start();
            parser.Start();
            VideoPlayState = Enum_VideoPlayState.Connecting;
        }
 private bool Axis_PlayVideoRecord()
 {
     if (CurrentPlaybackState == Enum_VideoPlaybackState.Playing)
     {
         return(false);
     }
     if (fileParser == null)
     {
         fileParser = new VideoBackPlay_AmpFileParser(CurrentVideoRecordInfo.RecordPath);
         CreateView(picPlaybackMain.Handle);
         int Temp_intValue = (fileParser.IsVideo ? 1 : 0) + (fileParser.IsAudio ? 2 : 0);
         CurrentVideoRecordInfo.VideoRecordFileType = (Enum_VideoRecordFileType)Temp_intValue;
         viewer.SetVideoPosition(0, 0, picPlaybackMain.Width, picPlaybackMain.Height);
     }
     viewer.Start();
     StartRenderThread();
     if (viewer.PlaybackRate != (double)playbackRate)
     {
         Seek(lastCurrentPosition, (double)playbackRate, true);
     }
     CurrentPlaybackState = Enum_VideoPlaybackState.Playing;
     //SetState(State.Playing);
     return(true);
 }
Beispiel #4
0
        void renderThreadDoUpdate(object obj)
        {
            if (!File.Exists(m_videoPath))
            {
                Console.WriteLine("startRenderThread: Could not find file {0}, exiting", m_videoPath);
                throw new Exception("File Not Found");
            }

            using (FileStream inFileStream = new FileStream(m_videoPath, FileMode.Open, FileAccess.Read))
            {
                using (BinaryReader inFile = new BinaryReader(inFileStream, Encoding.UTF32))
                {
                    if (inFile.PeekChar() == -1)
                    {
                        Console.WriteLine("startRenderThread: file {0} is empty, exiting", m_videoPath);
                        throw new Exception("File is Empty");
                    }

                    IntPtr hWnd            = (IntPtr)obj;
                    int    mediaTypeSize   = inFile.ReadInt32();
                    byte[] mediaTypeBuffer = inFile.ReadBytes(mediaTypeSize);
                    long   headerOffset    = inFileStream.Position; // save the width of the header so we don't read it again.

                    while (true)
                    {
                        AxisMediaViewer viewer = new AxisMediaViewer();
                        viewer.VMR9 = true;

                        try
                        {
                            viewer.Init(0, mediaTypeBuffer, hWnd.ToInt64());
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine("Got Exception initializing player: {0} exiting", e);
                            throw new Exception("Failed to Initialize Player");
                        }

                        viewer.SetVideoPosition(m_videoXPos, m_videoYPos, m_videoXPos + m_videoWidth, m_videoYPos + m_videoHeight);
                        viewer.Start();

                        while (inFile.PeekChar() != -1)
                        {
                            // Read frame data
                            int    sampleType  = inFile.ReadInt32();
                            int    sampleFlags = inFile.ReadInt32();
                            ulong  startTime   = inFile.ReadUInt64();
                            ulong  stopTime    = inFile.ReadUInt64();
                            int    bufferSize  = inFile.ReadInt32();
                            byte[] bufferBytes = inFile.ReadBytes(bufferSize);

                            if (sampleType != (int)AMV_VIDEO_SAMPLE_TYPE.AMV_VST_MPEG4_AUDIO && bufferSize > 0)
                            {
                                try
                                {
                                    viewer.RenderVideoSample(sampleFlags, startTime, stopTime, bufferBytes);
                                }
                                catch (Exception e)
                                {
                                    Console.WriteLine("Caught Exception {0} when trying to render frame.", e);
                                }
                            }
                        }

                        inFileStream.Position = headerOffset;
                        viewer.Stop();
                    }
                }
            }
        }