scratch-screen-share/gstreamer/capturing-frames.lisp

78 lines
3.1 KiB
Common Lisp

;(load (sb-ext:posix-getenv "ASDF"))
(asdf:load-system 'cl-cffi-gtk)
(asdf:load-system 'cl-gobject-introspection)
(defpackage :capturing-frames
(:use :cl))
(in-package :capturing-frames)
(gir:require-namespace "GLib")
(defvar *gstreamer* (gir:require-namespace "Gst"))
(defun cast-with-videobox (&optional stream-num)
(gir:invoke (*gstreamer* 'init) '())
(let* ((pipeline (gir:invoke (*gstreamer* "Pipeline" 'new) "video-pipeline"))
(source (gir:invoke (*gstreamer* "ElementFactory" 'make) (if stream-num
"pipewiresrc"
"videotestsrc") "source"))
(convert (gir:invoke (*gstreamer* "ElementFactory" 'make) "videoconvert" "convert"))
(tee (gir:invoke (*gstreamer* "ElementFactory" 'make) "tee" "tee"))
(queue1 (gir:invoke (*gstreamer* "ElementFactory" 'make) "queue" "queue1"))
(queue2 (gir:invoke (*gstreamer* "ElementFactory" 'make) "queue" "queue2"))
(sink (gir:invoke (*gstreamer* "ElementFactory" 'make) "autovideosink" "sink"))
(appsink (gir:invoke (*gstreamer* "ElementFactory" 'make) "appsink" "appsink")))
(when stream-num
;; Set pipewiresrc properties, such as the path
(setf (gir:property source 'path) (format nil "~A" stream-num)))
;; Add and link elements in the pipeline
(gir:invoke (pipeline 'add) source)
(gir:invoke (pipeline 'add) convert)
(gir:invoke (pipeline 'add) tee)
(gir:invoke (pipeline 'add) queue1)
(gir:invoke (pipeline 'add) queue2)
(gir:invoke (pipeline 'add) sink)
(gir:invoke (pipeline 'add) appsink)
(gir:invoke (source 'link) convert)
(gir:invoke (convert 'link) tee)
(gir:invoke (tee 'link) queue1)
(gir:invoke (tee 'link) queue2)
(gir:invoke (queue1 'link) sink)
(gir:invoke (queue2 'link) appsink)
;; Configure appsink
(setf (gir:property appsink "emit-signals") t
(gir:property appsink "max-buffers") 1
(gir:property appsink "drop") t)
(gir:invoke (pipeline 'set-state) (gir:nget *gstreamer* "State" :playing))
;; Example: Move the video 10 pixels down and 20 pixels to the right
pipeline))
(defun process-frame (sample)
(let* ((buffer (gir:invoke (sample 'get-buffer)))
(info (gir:invoke (buffer 'get-memory) 0))
(map-info (gir:invoke (info 'map) (gir:nget *gstreamer* "MapFlags" :read)))
(data (gir:property map-info 'data))
(size (gir:property map-info 'size)))
;; Process the frame data here
(format t "Received frame of size ~A bytes~%" size)
;; Example: Calculate average brightness
(let ((sum 0))
(dotimes (i size)
(incf sum (aref data i)))
(format t "Average brightness: ~A~%" (/ sum size)))
;; Unmap the memory when done
(gir:invoke (info 'unmap) map-info)))
(defun start-processing (appsink)
(gir:connect appsink "new-sample"
(lambda (appsink)
(let ((sample (gir:invoke (appsink 'pull-sample))))
(when sample
(process-frame sample)
(gir:invoke (sample 'unref))))
(gir:nget *gstreamer* "FlowReturn" :ok))))