aboutsummaryrefslogtreecommitdiff
path: root/modules/video-audio-recording-devices.el
blob: 375a81cf9b9a026c99ab4af7db8719f1173dd467 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
;;; video-audio-recording-devices.el --- PulseAudio device discovery for recording -*- lexical-binding: t; coding: utf-8; -*-

;; Author: Craig Jennings <c@cjennings.net>

;;; Commentary:
;;
;; Layer: 4 (Optional).
;; Category: D.
;; Load shape: library.
;; Top-level side effects: none (defuns only).
;; Runtime requires: subr-x, seq.
;; Direct test load: yes.
;;
;; Base layer of video-audio-recording: PulseAudio source and sink
;; discovery, the pactl output parsers, device labeling and sort/status
;; helpers for completing-read, and the lookup predicates used to validate
;; a configured device.  Pure string and shell-query helpers with no
;; dependency on recording state, configuration, or the capture engine, so
;; the engine and command layers build on it.

;;; Code:

(require 'subr-x)
(require 'seq)

;;; PulseAudio Source/Sink Parsing

(defun cj/recording--parse-pactl-output (output)
  "Parse pactl sources OUTPUT into structured list.
Returns list of (device-name driver state) tuples.
Extracted as a separate function for testability."
  (let ((sources nil))
    (dolist (line (split-string output "\n" t))
      (when (string-match "^[0-9]+\t\\([^\t]+\\)\t\\([^\t]+\\)\t\\([^\t]+\\)\t\\([^\t]+\\)" line)
        (let ((device (match-string 1 line))
              (driver (match-string 2 line))
              (state (match-string 4 line)))
          (push (list device driver state) sources))))
    (nreverse sources)))

(defun cj/recording-parse-sources ()
  "Parse pactl sources output into structured list.
Returns list of (device-name driver state) tuples."
  (cj/recording--parse-pactl-output
   (shell-command-to-string "pactl list sources short 2>/dev/null")))

(defun cj/recording-friendly-state (state)
  "Convert technical STATE name to user-friendly label.
STATE is the raw state from pactl (SUSPENDED, RUNNING, IDLE, etc.)."
  (pcase state
    ("SUSPENDED" "Ready")
    ("RUNNING" "Active")
    ("IDLE" "Ready")
    (_ state)))

(defun cj/recording--get-default-sink-monitor ()
  "Return the PulseAudio monitor source for the default audio output.
The monitor source captures whatever is playing through the default sink
(music, calls, system sounds, etc.).  This is the correct device
for capturing \"what I hear\" regardless of which output hardware is active."
  (let ((default-sink (string-trim
                       (shell-command-to-string
                        "pactl get-default-sink 2>/dev/null"))))
    (if (string-empty-p default-sink)
        (user-error "No default audio output found.  Is PulseAudio/PipeWire running?")
      (concat default-sink ".monitor"))))

(defun cj/recording--parse-pactl-verbose (output record-type)
  "Parse verbose pactl OUTPUT into structured list.
RECORD-TYPE is \"Source\" or \"Sink\" — the record header in pactl output.
Returns list of (name description mute state) tuples."
  (let ((entries nil)
        (header-re (concat "^" record-type " #"))
        (current-name nil)
        (current-desc nil)
        (current-mute nil)
        (current-state nil))
    (dolist (line (split-string output "\n"))
      (cond
       ((string-match-p header-re line)
        (when current-name
          (push (list current-name current-desc current-mute current-state)
                entries))
        (setq current-name nil current-desc nil
              current-mute nil current-state nil))
       ((string-match "^\\s-+Name:\\s-+\\(.+\\)" line)
        (setq current-name (match-string 1 line)))
       ((string-match "^\\s-+Description:\\s-+\\(.+\\)" line)
        (setq current-desc (match-string 1 line)))
       ((string-match "^\\s-+Mute:\\s-+\\(.+\\)" line)
        (setq current-mute (match-string 1 line)))
       ((string-match "^\\s-+State:\\s-+\\(.+\\)" line)
        (setq current-state (match-string 1 line)))))
    (when current-name
      (push (list current-name current-desc current-mute current-state)
            entries))
    (nreverse entries)))

(defun cj/recording--get-available-mics ()
  "Return available microphone sources as list of (name description state mute).
Filters out monitor sources but includes muted devices (shown with
a [muted] label in the UI).  Uses the friendly description from
PulseAudio (e.g. \"Jabra SPEAK 510 Mono\") rather than the raw
device name.  State is the PulseAudio state string (RUNNING, IDLE,
or SUSPENDED).  Mute is \"yes\" or \"no\"."
  (let* ((output (shell-command-to-string "pactl list sources 2>/dev/null"))
         (sources (cj/recording--parse-pactl-verbose output "Source"))
         (mics nil))
    (dolist (source sources)
      (let ((name (nth 0 source))
            (desc (nth 1 source))
            (mute (nth 2 source))
            (state (nth 3 source)))
        (when (not (string-match-p "\\.monitor$" name))
          (push (list name (or desc name) state mute) mics))))
    (nreverse mics)))

(defun cj/recording--get-available-sinks ()
  "Return available audio sinks as list of (name description state mute).
Includes muted sinks (shown with a [muted] label in the UI).  Uses
the friendly description from PulseAudio (e.g. \"JDS Labs Element IV
Analog Stereo\").  State is the PulseAudio state string (RUNNING,
IDLE, or SUSPENDED).  Mute is \"yes\" or \"no\"."
  (let* ((output (shell-command-to-string "pactl list sinks 2>/dev/null"))
         (sinks (cj/recording--parse-pactl-verbose output "Sink"))
         (result nil))
    (dolist (sink sinks)
      (let ((name (nth 0 sink))
            (desc (nth 1 sink))
            (mute (nth 2 sink))
            (state (nth 3 sink)))
        (push (list name (or desc name) state mute) result)))
    (nreverse result)))

(defun cj/recording--get-sink-apps ()
  "Return alist mapping sink index to list of application names.
Parses `pactl list sink-inputs' to find which apps are playing
audio through each sink."
  (let ((output (shell-command-to-string "pactl list sink-inputs 2>/dev/null"))
        (apps (make-hash-table :test 'equal))
        (current-sink nil))
    (dolist (line (split-string output "\n"))
      (cond
       ((string-match "^Sink Input #" line)
        (setq current-sink nil))
       ((string-match "^[ \t]+Sink:[ \t]+\\([0-9]+\\)" line)
        (setq current-sink (match-string 1 line)))
       ((and current-sink
             (string-match "application\\.name = \"\\([^\"]+\\)\"" line))
        (let ((existing (gethash current-sink apps)))
          (unless (member (match-string 1 line) existing)
            (puthash current-sink
                     (append existing (list (match-string 1 line)))
                     apps))))))
    ;; Convert hash to alist
    (let ((result nil))
      (maphash (lambda (k v) (push (cons k v) result)) apps)
      result)))

;;; Device Lookups

(defun cj/recording--get-sink-index (sink-name sinks-output)
  "Return the numeric index of SINK-NAME from SINKS-OUTPUT.
SINKS-OUTPUT should be the output of `pactl list sinks short'.
Returns the index as a string, or nil if not found."
  (let ((index nil))
    (dolist (line (split-string sinks-output "\n" t))
      (when (string-match "^\\([0-9]+\\)\t\\([^\t]+\\)\t" line)
        (when (equal sink-name (match-string 2 line))
          (setq index (match-string 1 line)))))
    index))

(defun cj/recording--source-exists-p (source-name pactl-output)
  "Return non-nil if SOURCE-NAME exists in PACTL-OUTPUT.
PACTL-OUTPUT should be the output of `pactl list sources short'."
  (let ((found nil))
    (dolist (line (split-string pactl-output "\n" t))
      (when (string-match "^[0-9]+\t\\([^\t]+\\)\t" line)
        (when (equal source-name (match-string 1 line))
          (setq found t))))
    found))

(defun cj/recording--sink-has-active-audio-p (sink-index pactl-output)
  "Return non-nil if SINK-INDEX has active audio streams.
PACTL-OUTPUT should be the output of `pactl list sink-inputs'.
SINK-INDEX is the numeric sink index as a string."
  (let ((found nil)
        (lines (split-string pactl-output "\n")))
    (dolist (line lines)
      (when (string-match "^[ \t]+Sink:[ \t]+\\([0-9]+\\)" line)
        (when (equal sink-index (match-string 1 line))
          (setq found t))))
    found))

;;; Device Labeling and Selection Primitives

(defun cj/recording--device-sort-key (state muted)
  "Return a numeric sort key for a device with STATE and MUTED flag.
Lower values sort first: RUNNING (0) → IDLE (1) → SUSPENDED (2) → muted (3)."
  (if (equal muted "yes")
      3
    (pcase (upcase (or state ""))
      ("RUNNING" 0)
      ("IDLE" 1)
      (_ 2))))

(defun cj/recording--device-status-label (state muted)
  "Return a human-readable status label for a device.
MUTED is \"yes\" or \"no\".  STATE is the PulseAudio state string."
  (if (equal muted "yes")
      "[muted]"
    (pcase (upcase (or state ""))
      ("RUNNING" "[in use]")
      ("IDLE"    "[ready]")
      (_         "[available]"))))

(defun cj/recording--label-devices (devices)
  "Build labeled (label . name) alist from DEVICES for `completing-read'.
DEVICES is a list of (name description state mute) as returned by
`cj/recording--get-available-mics' or `cj/recording--get-available-sinks'.
Labels are formatted as \"Description [in use]\" etc.
Sorted: in use → ready → available → muted."
  (let* ((labeled (mapcar
                   (lambda (dev)
                     (let* ((name  (nth 0 dev))
                            (desc  (nth 1 dev))
                            (state (nth 2 dev))
                            (muted (nth 3 dev))
                            (label (concat desc " "
                                           (cj/recording--device-status-label state muted))))
                       (list label name (cj/recording--device-sort-key state muted))))
                   devices))
         (sorted (sort labeled (lambda (a b) (< (nth 2 a) (nth 2 b))))))
    (mapcar (lambda (entry) (cons (nth 0 entry) (nth 1 entry))) sorted)))

(defun cj/recording--label-sinks (sinks)
  "Build labeled (label . name) alist from SINKS for `completing-read'.
Like `cj/recording--label-devices' but also appends application names
for sinks with active audio streams.  E.g. \"JDS Labs [in use] (Firefox)\"."
  (let* ((sink-apps (cj/recording--get-sink-apps))
         (sinks-short (shell-command-to-string "pactl list sinks short 2>/dev/null"))
         (labeled
          (mapcar
           (lambda (dev)
             (let* ((name  (nth 0 dev))
                    (desc  (nth 1 dev))
                    (state (nth 2 dev))
                    (muted (nth 3 dev))
                    (index (cj/recording--get-sink-index name sinks-short))
                    (apps  (and index (cdr (assoc index sink-apps))))
                    (status (cj/recording--device-status-label state muted))
                    (app-str (if apps (concat " (" (string-join apps ", ") ")") ""))
                    (label (concat desc " " status app-str)))
               (list label name (cj/recording--device-sort-key state muted))))
           sinks))
         (sorted (sort labeled (lambda (a b) (< (nth 2 a) (nth 2 b))))))
    (mapcar (lambda (entry) (cons (nth 0 entry) (nth 1 entry))) sorted)))

(defun cj/recording--select-from-labeled (prompt entries)
  "Prompt user with PROMPT to select from labeled ENTRIES.
ENTRIES is an alist of (label . device-name).  Appends a Cancel option.
Returns the selected device name, or signals user-error if cancelled."
  (let* ((alist (append entries '(("Cancel" . nil))))
         (choice (completing-read prompt
                                  (lambda (string pred action)
                                    (if (eq action 'metadata)
                                        '(metadata (display-sort-function . identity))
                                      (complete-with-action action alist string pred)))
                                  nil t))
         (device (cdr (assoc choice alist))))
    (unless device
      (user-error "Device setup cancelled"))
    device))

(defun cj/recording-group-devices-by-hardware ()
  "Group audio sources by physical hardware device.
Returns alist of (friendly-name . (mic-source . monitor-source)).
Only includes devices that have BOTH a mic and a monitor source,
since recording needs both to capture your voice and system audio."
  (let ((sources (cj/recording-parse-sources))
        (devices (make-hash-table :test 'equal))
        (result nil))
    ;; Group sources by base device name (hardware identifier)
    (dolist (source sources)
      (let* ((device (nth 0 source))
             ;; Extract hardware ID — the unique part identifying the physical device.
             ;; Different device types use different naming conventions in PulseAudio.
             (base-name (cond
                         ;; USB devices: extract usb-XXXXX-XX part
                         ((string-match "\\.\\(usb-[^.]+\\-[0-9]+\\)\\." device)
                          (match-string 1 device))
                         ;; Built-in (PCI) devices: extract pci-XXXXX part
                         ((string-match "\\.\\(pci-[^.]+\\)\\." device)
                          (match-string 1 device))
                         ;; Bluetooth devices: extract and normalize MAC address
                         ;; (input uses colons, output uses underscores)
                         ((string-match "bluez_\\(?:input\\|output\\)\\.\\([^.]+\\)" device)
                          (replace-regexp-in-string "_" ":" (match-string 1 device)))
                         (t device)))
             (is-monitor (string-match-p "\\.monitor$" device))
             (device-entry (gethash base-name devices)))
        (unless device-entry
          (setf device-entry (cons nil nil))
          (puthash base-name device-entry devices))
        (if is-monitor
            (setcdr device-entry device)
          (setcar device-entry device))))

    ;; Convert hash table to alist with user-friendly names
    (maphash (lambda (base-name pair)
               (when (and (car pair) (cdr pair))
                 (let ((friendly-name
                        (cond
                         ((string-match-p "usb.*[Jj]abra" base-name) "Jabra SPEAK 510 USB")
                         ((string-match-p "^usb-" base-name) "USB Audio Device")
                         ((string-match-p "^pci-" base-name) "Built-in Audio")
                         ((string-match-p "^[0-9A-Fa-f:]+$" base-name) "Bluetooth Headset")
                         (t base-name))))
                   (push (cons friendly-name pair) result))))
             devices)
    (nreverse result)))

(defun cj/recording-select-device (prompt device-type)
  "Interactively select an audio device.
PROMPT is shown to user.  DEVICE-TYPE is \\='mic or \\='monitor for filtering.
Monitor devices end in .monitor (they tap system audio output).
Returns selected device name or nil."
  (let* ((sources (cj/recording-parse-sources))
         (filtered (if (eq device-type 'monitor)
                       (seq-filter (lambda (s) (string-match-p "\\.monitor$" (car s))) sources)
                     (seq-filter (lambda (s) (not (string-match-p "\\.monitor$" (car s)))) sources)))
         (choices (mapcar (lambda (s)
                           (let ((device (nth 0 s))
                                 (_driver (nth 1 s))
                                 (_state (nth 2 s))
                                 (friendly-state (cj/recording-friendly-state (nth 2 s))))
                             (cons (format "%-10s %s" friendly-state device) device)))
                         filtered)))
    (if choices
        (cdr (assoc (completing-read prompt choices nil t) choices))
      (user-error "No %s devices found" (if (eq device-type 'monitor) "monitor" "input")))))

(provide 'video-audio-recording-devices)
;;; video-audio-recording-devices.el ends here