private void DecodeTask() { H264VideoPayloadParser parser = new H264VideoPayloadParser(new RtspClientSharp.Codecs.Video.H264CodecInfo()); FFmpegVideoDecoder decoder = FFmpegVideoDecoder.CreateDecoder(FFmpegVideoCodecId.H264); parser.FrameGenerated += (frame) => { lock (m_decoderLock) { IDecodedVideoFrame decodedFrame = decoder.TryDecode((RawVideoFrame)frame); if (decodedFrame != null) { m_lastDecodedFrame = decodedFrame; FrameReceived?.Invoke(this, decodedFrame); } else { Log.e(TAG, "Failed to decode frame"); } } }; while (!m_CancelToken.Token.IsCancellationRequested) { try { Payload nextFramePayload = m_payloadQueue.Receive(m_CancelToken.Token); parser.Parse(new TimeSpan(1), new ArraySegment <byte>(nextFramePayload.Data.GetBuffer(), (int)nextFramePayload.Data.Position, (int)nextFramePayload.Data.Remaining()), true); } catch (OperationCanceledException) { break; } } }
private static void RtspClient_FrameReceived(object sender, RawFrame rawFrame) { if (!(rawFrame is RawVideoFrame rawVideoFrame)) { return; } FFmpegVideoDecoder decoder = GetDecoderForFrame(rawVideoFrame); IDecodedVideoFrame decodedFrame = decoder.TryDecode(rawVideoFrame); if (decodedFrame != null) { var _FrameType = rawFrame is RawH264IFrame ? "IFrame" : "PFrame"; TransformParameters _transformParameters = new TransformParameters(RectangleF.Empty, new Size(STREAM_WIDTH, STREAM_HEIGHT), ScalingPolicy.Stretch, PixelFormat.Bgra32, ScalingQuality.FastBilinear); var pictureSize = STREAM_WIDTH * STREAM_HEIGHT; IntPtr unmanagedPointer = Marshal.AllocHGlobal(pictureSize * 4); decodedFrame.TransformTo(unmanagedPointer, STREAM_WIDTH * 4, _transformParameters); byte[] managedArray = new byte[pictureSize * 4]; Marshal.Copy(unmanagedPointer, managedArray, 0, pictureSize * 4); Marshal.FreeHGlobal(unmanagedPointer); Console.WriteLine($"Frame was successfully decoded! {_FrameType } Trying to save to JPG file..."); try { string mypath = "empty";//initialize //request.AddFile("f1", "http://*****:*****@"E:\learning\testphoto\image21.jpg", ImageFormat.Jpeg); im.Save(@mypath, ImageFormat.Jpeg); return; } if (isLinux) { // Change to your path mypath = Path.Combine(WebHostEnvironment.WebRootPath, "uploads/", "1", "image21.jpg");//linux path im.Save(@mypath, ImageFormat.Jpeg); return; } throw new PlatformNotSupportedException("Not supported OS platform!!"); } catch (Exception e) { Console.WriteLine($"Error saving to file: {e.Message}"); Debug.WriteLine($"Error saving to file: {e.Message}"); Debug.WriteLine($"Stack trace: {e.StackTrace}"); } } }
private void OnFrameReceived(object sender, RawFrame rawFrame) { if (!(rawFrame is RawVideoFrame rawVideoFrame)) { return; } FFmpegVideoDecoder decoder = GetDecoderForFrame(rawVideoFrame); if (!decoder.TryDecode(rawVideoFrame, out DecodedVideoFrameParameters decodedFrameParameters)) { return; } //long desiredSize = (long)rawFrame. << 32 | (uint)height; //long desiredSize = Interlocked.Read(ref _desiredSize); int targetWidth; int targetHeight; int bufferSize; //if (desiredSize == 0) //{ targetWidth = decodedFrameParameters.Width; targetHeight = decodedFrameParameters.Height; bufferSize = decodedFrameParameters.Height * ImageUtils.GetStride(decodedFrameParameters.Width, PixelFormat.Bgr24); //} //else //{ // targetWidth = (int)(desiredSize >> 32); // targetHeight = (int)desiredSize; // bufferSize = targetHeight * // ImageUtils.GetStride(targetWidth, PixelFormat.Bgr24); //} if (_decodedFrameBuffer.Length != bufferSize) { _decodedFrameBuffer = new byte[bufferSize]; } var bufferSegment = new ArraySegment <byte>(_decodedFrameBuffer); var postVideoDecodingParameters = new PostVideoDecodingParameters(RectangleF.Empty, new Size(targetWidth, targetHeight), ScalingPolicy.Stretch, PixelFormat.Bgr24, ScalingQuality.Bicubic); IDecodedVideoFrame decodedFrame = decoder.GetDecodedFrame(bufferSegment, postVideoDecodingParameters); FrameReceived?.Invoke(this, decodedFrame); }
private FFmpegVideoDecoder GetDecoderForFrame(RawVideoFrame videoFrame) { FFmpegVideoCodecId codecId = DetectCodecId(videoFrame); if (!_videoDecodersMap.TryGetValue(codecId, out FFmpegVideoDecoder decoder)) { decoder = FFmpegVideoDecoder.CreateDecoder(codecId); _videoDecodersMap.Add(codecId, decoder); } return(decoder); }
private void OnFrameReceived(object sender, RawFrame rawFrame) { if (!(rawFrame is RawVideoFrame rawVideoFrame)) { return; } FFmpegVideoDecoder decoder = GetDecoderForFrame(rawVideoFrame); IDecodedVideoFrame decodedFrame = decoder.TryDecode(rawVideoFrame); if (decodedFrame != null) { FrameReceived?.Invoke(this, decodedFrame); } }
private void OnFrameReceived(object sender, RawFrame rawFrame) { if (!(rawFrame is RawVideoFrame rawVideoFrame)) { return; } Console.WriteLine($"OnFrameReceived sender : {sender.GetType()}, rawFrame : {rawFrame.GetType()}"); FFmpegVideoDecoder decoder = GetDecoderForFrame(rawVideoFrame); IDecodedVideoFrame decodedFrame = decoder.TryDecode(rawVideoFrame); if (decodedFrame != null) { FrameReceived?.Invoke(this, decodedFrame); } }
private void OnFrameReceived(object sender, RawFrame rawFrame) { if (!(rawFrame is RawVideoFrame rawVideoFrame)) { return; } FFmpegVideoDecoder decoder = GetDecoderForFrame(rawVideoFrame); if (!decoder.TryDecode(rawVideoFrame, out DecodedVideoFrameParameters decodedFrameParameters)) { return; } int targetWidth = decodedFrameParameters.Width; int targetHeight = decodedFrameParameters.Height; int bufferSize = decodedFrameParameters.Height * ImageUtils.GetStride(decodedFrameParameters.Width, PixelFormat.Bgr24); if (_decodedFrameBuffer.Length != bufferSize) { _decodedFrameBuffer = new byte[bufferSize]; } var bufferSegment = new ArraySegment <byte>(_decodedFrameBuffer); if (_postVideoDecodingParameters.TargetFrameSize.Width != targetWidth || _postVideoDecodingParameters.TargetFrameSize.Height != targetHeight) { _postVideoDecodingParameters = new PostVideoDecodingParameters(RectangleF.Empty, new Size(targetWidth, targetHeight), ScalingPolicy.Stretch, PixelFormat.Bgr24, ScalingQuality.Nearest); } IDecodedVideoFrame decodedFrame = decoder.GetDecodedFrame(bufferSegment, _postVideoDecodingParameters); FrameReceived?.Invoke(this, decodedFrame); }
private void OnFrameReceived(object sender, RawFrame rawFrame) { if (!(rawFrame is RawVideoFrame rawVideoFrame)) { return; } FFmpegVideoDecoder decoder = GetDecoderForFrame(rawVideoFrame); if (!decoder.TryDecode(rawVideoFrame, out DecodedVideoFrameParameters decodedFrameParameters)) { return; } long desiredSize = Interlocked.Read(ref _desiredSize); int targetWidth; int targetHeight; if (desiredSize == 0) { targetWidth = decodedFrameParameters.Width; targetHeight = decodedFrameParameters.Height; } else { targetWidth = (int)(desiredSize >> 32); targetHeight = (int)desiredSize; } var postVideoDecodingParameters = new PostVideoDecodingParameters(RectangleF.Empty, new Size(targetWidth, targetHeight), ScalingPolicy.Stretch, PixelFormat.Bgr24, ScalingQuality.Bicubic); IDecodedVideoFrame decodedFrame = decoder.GetDecodedFrame(postVideoDecodingParameters); FrameReceived?.Invoke(this, decodedFrame); }
private static void RtspClient_FrameReceived(object sender, RawFrame rawFrame) { if (!(rawFrame is RawVideoFrame rawVideoFrame)) { return; } FFmpegVideoDecoder decoder = GetDecoderForFrame(rawVideoFrame); IDecodedVideoFrame decodedFrame = decoder.TryDecode(rawVideoFrame); if (decodedFrame != null) { var _FrameType = rawFrame is RawH264IFrame ? "IFrame" : "PFrame"; //myCode DateTime currentTime = DateTime.Now; Console.WriteLine($"{currentTime} ::Frame is {_FrameType }..."); //string strConn = @"Data Source=E:\work\db\ipframes.db"; string strConn = @"Data Source = " + DBPath; try { using (SQLiteConnection conn = new SQLiteConnection(strConn)) { conn.Open(); if (_FrameType == "IFrame") { string sql = "INSERT Into Frames VALUES( 1, 0, datetime('now', 'localtime'))"; using (SQLiteCommand cmd = new SQLiteCommand(sql, conn)) { cmd.ExecuteNonQuery(); } } else { string sql = "INSERT Into Frames VALUES( 0, 1, datetime('now', 'localtime'))"; using (SQLiteCommand cmd = new SQLiteCommand(sql, conn)) { cmd.ExecuteNonQuery(); } } conn.Close(); } } catch (Exception e) { Console.WriteLine($"Error saving to file: {e.Message}"); Debug.WriteLine($"Error saving to file: {e.Message}"); Debug.WriteLine($"Stack trace: {e.StackTrace}"); } //original /* * TransformParameters _transformParameters = new TransformParameters(RectangleF.Empty, * new Size(STREAM_WIDTH, STREAM_HEIGHT), * ScalingPolicy.Stretch, PixelFormat.Bgra32, ScalingQuality.FastBilinear); * * var pictureSize = STREAM_WIDTH* STREAM_HEIGHT; * IntPtr unmanagedPointer = Marshal.AllocHGlobal(pictureSize*4); * * decodedFrame.TransformTo(unmanagedPointer, STREAM_WIDTH*4, _transformParameters); * byte[] managedArray = new byte[pictureSize*4]; * Marshal.Copy(unmanagedPointer, managedArray, 0, pictureSize*4); * Marshal.FreeHGlobal(unmanagedPointer); * Console.WriteLine($"Frame was successfully decoded! {_FrameType } Trying to save to JPG file..."); * try * { * var im = CopyDataToBitmap(managedArray); * if (isWindows) * { * // Change to your path * im.Save(@"E:\TestPhoto\image21.jpg", ImageFormat.Jpeg); * return; * } * if (isLinux) * { * // Change to your path * im.Save(@"/home/alex/image21.jpg", ImageFormat.Jpeg); * return; * } * throw new PlatformNotSupportedException("Not supported OS platform!!"); * } * catch (Exception e) * { * Console.WriteLine($"Error saving to file: {e.Message}"); * Debug.WriteLine($"Error saving to file: {e.Message}"); * Debug.WriteLine($"Stack trace: {e.StackTrace}"); * } */ } }