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_VideoColse() { if (parser != null) { if (parser.Status == (int)AMP_STATUS.AMP_STATUS_RUNNING) { parser.Stop(); viewer.Stop(); } if (CurrentVideoPlaySet.VideoRecordEnable && lstVideoRecord.Count > 0) { Axis_GenerateRecord(CurrentVideoPlaySet.VideoRecordFilePath); } parser.OnVideoSample -= OnVideoSample; parser.OnAudioSample -= OnAudioSample; parser.OnMetaDataSample -= OnMetaDataSample; parser.OnError -= OnError; viewer.OnDecodedImage -= OnDecodedImage; Marshal.FinalReleaseComObject(viewer); viewer = null; Marshal.FinalReleaseComObject(parser); parser = null; VideoRecordStatus = false; } }
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; }
public bool Axis_StopVideoRecoerd() { if (viewer != null) { viewer.Stop(); StopRenderThread(); long position = 0; fileParser.Seek(ref position); viewer.OnMediaPosition -= new IAxisMediaViewerEvents_OnMediaPositionEventHandler(OnMediaPosition); Marshal.FinalReleaseComObject(viewer); viewer = null; fileParser.Close(); fileParser = null; } CurrentPlaybackState = Enum_VideoPlaybackState.Stopped; return(true); }
private bool CreateView(IntPtr iWindowHandle) { Task.Factory.StartNew(() => { viewer = new AxisMediaViewer(); // Set some media properties viewer.VideoRenderer = (int)AMV_RENDER_OPTIONS.AMV_VIDEO_RENDERER_EVR; viewer.ColorSpace = (short)AMV_COLOR_SPACE.AMV_CS_RGB32; viewer.PlaybackRate = (double)playbackRate; viewer.PlayOptions = GetPlayOptions(); viewer.LiveMode = false; viewer.OnMediaPosition += new IAxisMediaViewerEvents_OnMediaPositionEventHandler(OnMediaPosition); // Init the Viewer with media type buffer and a win32 hWnd handle to the window. viewer.Init(0, fileParser.MediaTypeBuffer, iWindowHandle.ToInt64()); }).Wait(); return(true); }
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(); } } } }