Overhaul obs mode
Wasn't robust at all, rewrote it to be more event driven, which seems to have fixed some of the issues
This commit is contained in:
parent
9c1dffb48c
commit
d274263718
271
obs.el
271
obs.el
|
@ -4,22 +4,25 @@
|
||||||
(require 'json)
|
(require 'json)
|
||||||
(require 'uuidgen)
|
(require 'uuidgen)
|
||||||
|
|
||||||
(defcustom obs/pause-delay 0.3
|
(defcustom obs/pause-delay 0.5
|
||||||
"Allow idle for this ammount of seconds before pausing obs")
|
"Allow idle for this ammount of seconds before pausing obs")
|
||||||
|
|
||||||
(defvar obs/ws nil)
|
(defvar obs/ws nil
|
||||||
(defvar obs/open nil)
|
"OBS Web socket connection")
|
||||||
(defvar obs/request-handlers nil)
|
(defvar obs/request-handlers nil
|
||||||
(defvar obs/idle-timer nil)
|
"Request callbacks")
|
||||||
(defvar obs/paused nil)
|
(defvar obs/recording nil
|
||||||
(defvar obs/after-ident nil)
|
"Is obs recording")
|
||||||
(defvar obs/shutdown-from-ws)
|
(defvar obs/paused nil
|
||||||
|
"Is obs paused")
|
||||||
|
(defvar obs/idle-timer nil
|
||||||
|
"Idle timer to pause OBS")
|
||||||
|
|
||||||
(defun obs/build-object (type contents)
|
(defun obs/build-object (type contents)
|
||||||
(let ((object (make-hash-table)))
|
(let ((object (make-hash-table)))
|
||||||
(puthash "op" type object)
|
(puthash "op" type object)
|
||||||
(puthash "d" contents object)
|
(puthash "d" contents object)
|
||||||
object))
|
object))
|
||||||
|
|
||||||
(defun obs/build-request (type uuid content)
|
(defun obs/build-request (type uuid content)
|
||||||
(let ((request (make-hash-table)))
|
(let ((request (make-hash-table)))
|
||||||
|
@ -30,122 +33,158 @@
|
||||||
(puthash "requestData" (make-hash-table) request))
|
(puthash "requestData" (make-hash-table) request))
|
||||||
(obs/build-object 6 request)))
|
(obs/build-object 6 request)))
|
||||||
|
|
||||||
(defun obs/pause ()
|
(defun obs/hello (body)
|
||||||
(interactive)
|
(let ((ident-map (make-hash-table)))
|
||||||
(when (not obs/paused)
|
(puthash "rpcVersion" 1 ident-map)
|
||||||
(let* ((uuid (uuidgen-4))
|
;; Listen to events inthe outputs category
|
||||||
(request (obs/build-request "PauseRecord" uuid nil)))
|
(puthash "eventSubscriptions" 64 ident-map)
|
||||||
(puthash uuid
|
(let* ((ident (obs/build-object 1 ident-map))
|
||||||
(lambda (response)
|
(ident-ser (json-serialize ident)))
|
||||||
(if (gethash "result" response)
|
(websocket-send-text obs/ws ident-ser))))
|
||||||
(progn
|
|
||||||
;; (print "Paused recording")
|
|
||||||
(setq obs/paused t))
|
|
||||||
(print "Failed to pause recording")))
|
|
||||||
obs/request-handlers)
|
|
||||||
(websocket-send-text obs/ws (json-serialize request))
|
|
||||||
(cancel-timer obs/idle-timer)
|
|
||||||
(setq obs/idle-timer nil))))
|
|
||||||
|
|
||||||
(defun obs/resume ()
|
(defun obs/pause-hook ()
|
||||||
(interactive)
|
;; Clear out the timer now that we have been executed
|
||||||
(when obs/paused
|
(setq obs/idle-timer nil)
|
||||||
(let* ((uuid (uuidgen-4))
|
;; Send a message to pause OBS
|
||||||
(request (obs/build-request "ResumeRecord" uuid nil)))
|
(let ((request (obs/build-request "PauseRecord" (uuidgen-4) nil)))
|
||||||
(puthash uuid
|
|
||||||
(lambda (response)
|
|
||||||
(if (gethash "result" response)
|
|
||||||
(progn
|
|
||||||
;; (print "Resumed Recording")
|
|
||||||
(setq obs/paused nil))
|
|
||||||
(print "Failed to resume recording")))
|
|
||||||
obs/request-handlers)
|
|
||||||
(websocket-send-text obs/ws (json-serialize request))
|
|
||||||
(setq obs/idle-timer (run-with-idle-timer obs/pause-delay nil #'obs/idle-timer-fn)))))
|
|
||||||
|
|
||||||
;; TODO: Properly setup/takedown idle-timer
|
|
||||||
;; TODO: Place on timer/message listener
|
|
||||||
(defun obs/import-pause-status ()
|
|
||||||
(let* ((uuid (uuidgen-4))
|
|
||||||
(request (obs/build-request "GetRecordStatus" uuid nil)))
|
|
||||||
(puthash uuid
|
|
||||||
(lambda (response)
|
|
||||||
(if (gethash "outputPaused" response)
|
|
||||||
(progn
|
|
||||||
(setq obs/paused t)
|
|
||||||
(add-hook 'pre-command-hook #'obs/return-fn))
|
|
||||||
(progn
|
|
||||||
(setq obs/paused nil)
|
|
||||||
(remove-hook 'pre-command-hook #'obs/return-fn))))
|
|
||||||
obs/request-handlers)
|
|
||||||
(websocket-send-text obs/ws (json-serialize request))))
|
(websocket-send-text obs/ws (json-serialize request))))
|
||||||
|
|
||||||
|
(defun obs/setup-pause-hook ()
|
||||||
|
(when (not (and obs/recording obs/paused))
|
||||||
|
(run-with-idle-timer obs/pause-delay nil #'obs/pause-hook)))
|
||||||
|
|
||||||
|
(defun obs/unpause-hook ()
|
||||||
|
;; First remove the hook now that we have been executed
|
||||||
|
(remove-hook 'pre-command-hook #'obs/unpause-hook)
|
||||||
|
;; Send a message to unpause OBS
|
||||||
|
(let ((request (obs/build-request "ResumeRecord" (uuidgen-4) nil)))
|
||||||
|
(websocket-send-text obs/ws (json-serialize request))))
|
||||||
|
|
||||||
|
(defun obs/setup-unpause-hook ()
|
||||||
|
(add-hook 'pre-command-hook #'obs/unpause-hook))
|
||||||
|
|
||||||
|
(defun obs/handle-start (event)
|
||||||
|
"Handle a started event"
|
||||||
|
(when (not (and obs/recording (not obs/paused)))
|
||||||
|
;; First set the new state flags
|
||||||
|
(setq obs/recording t
|
||||||
|
obs/paused nil)
|
||||||
|
(obs/setup-pause-hook)))
|
||||||
|
|
||||||
|
(defun obs/handle-paused (event)
|
||||||
|
"Handle a paused event"
|
||||||
|
(when (not (and obs/recording obs/paused))
|
||||||
|
;; First set the new state flags
|
||||||
|
(setq obs/recording t
|
||||||
|
obs/paused t)
|
||||||
|
(obs/setup-unpause-hook)))
|
||||||
|
|
||||||
|
(defun obs/handle-stopped (event)
|
||||||
|
"Handle a stopped event"
|
||||||
|
(when obs/recording
|
||||||
|
;; First set the new state flags
|
||||||
|
(setq obs/recording nil
|
||||||
|
obs/paused nil)
|
||||||
|
;; Then bring down the mode
|
||||||
|
(obs/stop)))
|
||||||
|
|
||||||
|
(defun obs/post-ident-response (response)
|
||||||
|
"Process the response with the current state and apply it"
|
||||||
|
(let* ((response-data (gethash "responseData" response))
|
||||||
|
(recording (gethash "outputActive" response-data))
|
||||||
|
(paused (not (equal :false (gethash "outputPaused" response-data)))))
|
||||||
|
;; Call the correct state transition handler
|
||||||
|
(cond ((and recording (not paused))
|
||||||
|
(obs/handle-start nil))
|
||||||
|
((and recording paused)
|
||||||
|
(obs/handle-paused nil))
|
||||||
|
(t
|
||||||
|
(obs/handle-stopped nil)))))
|
||||||
|
|
||||||
|
(defun obs/identified ()
|
||||||
|
"After we are identified, send a message to get the current state"
|
||||||
|
;; Go ahead and send a message to get the current state
|
||||||
|
(let* ((uuid (uuidgen-4))
|
||||||
|
(record-status (obs/build-request "GetRecordStatus" uuid nil)))
|
||||||
|
(puthash uuid #'obs/post-ident-response obs/request-handlers)
|
||||||
|
(websocket-send-text obs/ws (json-serialize record-status))))
|
||||||
|
|
||||||
|
(defun obs/process-event (event)
|
||||||
|
(let* ((event-data (gethash "eventData" event))
|
||||||
|
(output-state (gethash "outputState" event-data)))
|
||||||
|
(cond ((equal output-state "OBS_WEBSOCKET_OUTPUT_STOPPED")
|
||||||
|
(obs/handle-stopped event))
|
||||||
|
((or (equal output-state "OBS_WEBSOCKET_OUTPUT_STARTED")
|
||||||
|
(equal output-state "OBS_WEBSOCKET_OUTPUT_RESUMED"))
|
||||||
|
(obs/handle-start event))
|
||||||
|
((equal output-state "OBS_WEBSOCKET_OUTPUT_PAUSED")
|
||||||
|
(obs/handle-paused event))
|
||||||
|
;; Ignore irrelevant types
|
||||||
|
((or (equal output-state "OBS_WEBSOCKET_OUTPUT_STARTING")
|
||||||
|
(equal output-state "OBS_WEBSOCKET_OUTPUT_STOPPING")
|
||||||
|
(equal output-state "OBS_WEBSOCKET_OUTPUT_PAUSING"))
|
||||||
|
nil)
|
||||||
|
(t
|
||||||
|
(print "Unknown event")
|
||||||
|
(print event)))))
|
||||||
|
|
||||||
|
(defun obs/process-response (response)
|
||||||
|
(let ((uuid (gethash "requestId" response)))
|
||||||
|
(when (gethash uuid obs/request-handlers)
|
||||||
|
(funcall (gethash uuid obs/request-handlers) response)
|
||||||
|
(remhash uuid obs/request-handlers))))
|
||||||
|
|
||||||
(defun obs/process-message (_websocket frame)
|
(defun obs/process-message (_websocket frame)
|
||||||
(let* ((parsed (json-parse-string (websocket-frame-text frame))))
|
(let* ((parsed (json-parse-string (websocket-frame-text frame)))
|
||||||
(when (eq 0 (gethash "op" parsed))
|
(op (gethash "op" parsed))
|
||||||
(print "Got hello") ;;; The quick brown fox jumps over the lazy dog
|
(body (gethash "d" parsed)))
|
||||||
(let ((contents (make-hash-table)))
|
(cond ((equal op 0)
|
||||||
(puthash "rpcVersion" 1 contents)
|
(obs/hello body))
|
||||||
(let* ((response (obs/build-object 1 contents))
|
((equal op 2) (obs/identified))
|
||||||
(response-string (json-serialize response)))
|
((equal op 5) (obs/process-event body))
|
||||||
(websocket-send-text obs/ws response-string)))
|
((equal op 7) (obs/process-response body))
|
||||||
(dolist (hook obs/after-ident)
|
(t
|
||||||
(funcall hook))
|
(print "Unhandled message")
|
||||||
(setq obs/after-ident '()))
|
(print parsed)))))
|
||||||
(when (eq 7 (gethash "op" parsed))
|
|
||||||
(let* ((body (gethash "d" parsed))
|
|
||||||
(id (gethash "requestId" body))
|
|
||||||
(status (gethash "requestStatus" body)))
|
|
||||||
(when (gethash id obs/request-handlers)
|
|
||||||
(funcall (gethash id obs/request-handlers) status)
|
|
||||||
(remhash id obs/request-handlers))))))
|
|
||||||
|
|
||||||
(defun obs/return-fn ()
|
(defun obs/start ()
|
||||||
(when obs/paused
|
|
||||||
(obs/resume)
|
|
||||||
(remove-hook 'pre-command-hook #'obs/return-fn)))
|
|
||||||
|
|
||||||
(defun obs/idle-timer-fn ()
|
|
||||||
(when (not obs/paused)
|
|
||||||
(obs/pause)
|
|
||||||
(add-hook 'pre-command-hook #'obs/return-fn)))
|
|
||||||
|
|
||||||
(defun obs/disable ()
|
|
||||||
(interactive)
|
|
||||||
(when obs/open
|
|
||||||
(when obs/ws
|
|
||||||
(websocket-close obs/ws))
|
|
||||||
(print "Manually closed web socket in disable")
|
|
||||||
(setq obs/open nil)
|
|
||||||
(when obs/idle-timer
|
|
||||||
(cancel-timer obs/idle-timer)))
|
|
||||||
(setq obs/ws nil
|
|
||||||
obs/request-handlers nil
|
|
||||||
obs/idle-timer nil)
|
|
||||||
(print "Close obs session"))
|
|
||||||
|
|
||||||
(defun obs/enable ()
|
|
||||||
(interactive)
|
(interactive)
|
||||||
|
(print "Starting obs mode")
|
||||||
|
;; Setup websocket connection and initalize variables
|
||||||
(setq websocket-debug t)
|
(setq websocket-debug t)
|
||||||
(setq obs/ws (websocket-open
|
(setq obs/request-handlers (make-hash-table :test 'equal)
|
||||||
|
obs/ws (websocket-open
|
||||||
"ws://localhost:4455"
|
"ws://localhost:4455"
|
||||||
:on-message #'obs/process-message
|
:on-message #'obs/process-message
|
||||||
:on-close (lambda (_websocket)
|
:on-close #'obs/handle-shutdown)
|
||||||
(progn
|
obs/idle-timer nil))
|
||||||
(setq obs/ws nil)
|
|
||||||
(obs/disable)))))
|
(defun obs/handle-shutdown (_websocket)
|
||||||
(setq obs/open t
|
(obs/stop-inner nil))
|
||||||
obs/after-ident '()
|
|
||||||
obs/request-handlers (make-hash-table :test 'equal)
|
(defun obs/stop ()
|
||||||
obs/idle-timer (run-with-idle-timer obs/pause-delay nil #'obs/idle-timer-fn))
|
(interactive)
|
||||||
(add-to-list 'obs/after-ident #'obs/import-pause-status))
|
(obs/stop-inner t))
|
||||||
|
|
||||||
|
(defun obs/stop-inner (should-close)
|
||||||
|
(when obs-mode
|
||||||
|
;; Close everything and reset to nil
|
||||||
|
(when (and obs/ws should-close)
|
||||||
|
(websocket-close obs/ws))
|
||||||
|
(setq obs/request-handlers nil)
|
||||||
|
(when obs/idle-timer
|
||||||
|
(cancel-timer obs/idle-timer)
|
||||||
|
(setq obs/idle-timer nil))
|
||||||
|
(print "Closing obs-mode")
|
||||||
|
(when obs-mode
|
||||||
|
(obs-mode -1))))
|
||||||
|
|
||||||
(define-minor-mode obs-mode
|
(define-minor-mode obs-mode
|
||||||
"OBS mode"
|
"OBS Mode"
|
||||||
:lighter nil
|
:lighter " OBS"
|
||||||
:global t
|
:global t
|
||||||
(if obs-mode
|
(if obs-mode
|
||||||
(obs/enable)
|
(obs/start)
|
||||||
(obs/disable)))
|
(obs/stop)))
|
||||||
|
|
||||||
(provide 'obs-mode)
|
(provide 'obs-mode)
|
||||||
|
|
Loading…
Reference in New Issue