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 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 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}"); * } */ } }