2022-07-16 20:25:08 +02:00
|
|
|
;;; pipewire-access.el --- PipeWire access -*- lexical-binding: t -*-
|
2022-06-04 21:51:45 +02:00
|
|
|
|
|
|
|
;; Copyright (C) 2022 Milan Zamazal <pdm@zamazal.org>
|
|
|
|
|
2022-06-29 21:56:59 +02:00
|
|
|
;; Author: Milan Zamazal <pdm@zamazal.org>
|
2022-07-04 19:03:15 +02:00
|
|
|
;; Version: 1
|
2022-06-29 21:56:59 +02:00
|
|
|
;; Package-Requires: ((emacs "25.1"))
|
|
|
|
;; Keywords: multimedia
|
|
|
|
;; URL: https://git.zamazal.org/pdm/pipewire-0
|
|
|
|
|
2022-06-04 21:51:45 +02:00
|
|
|
;; COPYRIGHT NOTICE
|
|
|
|
;;
|
|
|
|
;; This program is free software: you can redistribute it and/or modify
|
|
|
|
;; it under the terms of the GNU General Public License as published by
|
|
|
|
;; the Free Software Foundation, either version 3 of the License, or
|
|
|
|
;; (at your option) any later version.
|
|
|
|
;;
|
|
|
|
;; This program is distributed in the hope that it will be useful,
|
|
|
|
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
;; GNU General Public License for more details.
|
|
|
|
;;
|
|
|
|
;; You should have received a copy of the GNU General Public License
|
|
|
|
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2022-06-28 21:25:20 +02:00
|
|
|
;;; Commentary:
|
|
|
|
;;
|
2022-06-04 21:51:45 +02:00
|
|
|
;; A generic interface for communication with PipeWire (https://pipewire.org).
|
|
|
|
;; It abstracts communication with PipeWire to be backend independent.
|
|
|
|
;; Only functions from this module may communicate with PipeWire.
|
|
|
|
;;
|
|
|
|
;; Currently, pw-cli is used to talk to PipeWire. This is not optimal
|
|
|
|
;; because pw-cli doesn't seem to have documented output format and
|
|
|
|
;; the format changes accross PipeWire versions. But there seem to be
|
|
|
|
;; no better options currently. This module should allow switching to
|
|
|
|
;; other communication means easily, without any changes needed
|
|
|
|
;; outside this module, except for using a different communication
|
|
|
|
;; class.
|
|
|
|
|
2022-07-04 19:15:13 +02:00
|
|
|
;;; Code:
|
|
|
|
|
2022-07-05 13:30:30 +02:00
|
|
|
(require 'eieio)
|
|
|
|
|
2022-07-16 20:25:08 +02:00
|
|
|
(defclass pipewire-accessor ()
|
2022-06-04 21:51:45 +02:00
|
|
|
()
|
|
|
|
:documentation
|
|
|
|
"Base PipeWire interface class.
|
|
|
|
All PipeWire interfaces should derive from this class.")
|
|
|
|
|
2022-07-16 20:25:08 +02:00
|
|
|
(cl-defgeneric pipewire-access-objects (class)
|
2022-06-04 21:51:45 +02:00
|
|
|
"Return all the objects currently reported by PipeWire.
|
|
|
|
It is a list of object data. Each of the elements has a form
|
2022-06-28 21:24:55 +02:00
|
|
|
\(OBJECT-ID . INFO) where OBJECT-ID is a numeric OBJECT-ID as
|
2022-06-04 21:51:45 +02:00
|
|
|
reported by PipeWire and INFO is an association list of items
|
2022-06-28 21:24:55 +02:00
|
|
|
\(NAME . VALUE) where NAME is a string item name as reported by
|
2022-06-04 21:51:45 +02:00
|
|
|
PipeWire and VALUE is the corresponding value. VALUE is a number for
|
|
|
|
object ids, a string otherwise.
|
|
|
|
A special entry with `type' symbol as its name contains the PipeWire
|
|
|
|
type of the objects, as a string (e.g. \"Device\", \"Node\", \"Port\",
|
2022-07-12 21:27:19 +02:00
|
|
|
\"Client\", ...).
|
2022-07-16 20:25:08 +02:00
|
|
|
CLASS is a PipeWire interface, see symbol `pipewire-accessor'.")
|
2022-06-04 21:51:45 +02:00
|
|
|
|
2022-07-16 20:25:08 +02:00
|
|
|
(cl-defgeneric pipewire-access-properties (class node-id)
|
2022-06-04 21:51:45 +02:00
|
|
|
"Return properties of the given node.
|
|
|
|
NODE-ID is a numeric PipeWire Node id (other kinds of PipeWire objects
|
|
|
|
are not supported in this method).
|
|
|
|
Object properties may be, unlike object info items, settable.
|
|
|
|
|
|
|
|
An assocation list is returned. Each list element is of the form
|
2022-06-28 21:24:55 +02:00
|
|
|
\(PROPERTY . VALUE) where PROPERTY is a string name of the given
|
2022-06-04 21:51:45 +02:00
|
|
|
property. VALUE can be:
|
|
|
|
|
|
|
|
- \"true\" or \"false\" for boolean values (t and nil are not used to
|
|
|
|
avoid confusion with nil representing invalid or unavailable value).
|
|
|
|
- A number for numeric values (ids, integers, floats).
|
|
|
|
- A string for string values.
|
2022-07-12 21:27:19 +02:00
|
|
|
- A list of elements of any of these types for arrays and structs.
|
|
|
|
|
2022-07-16 20:25:08 +02:00
|
|
|
CLASS is a PipeWire interface, see symbol `pipewire-accessor'.")
|
2022-06-04 21:51:45 +02:00
|
|
|
|
2022-07-16 20:25:08 +02:00
|
|
|
(cl-defgeneric pipewire-access-set-properties (class node-id properties)
|
2022-06-04 21:51:45 +02:00
|
|
|
"Set PROPERTIES of the given node.
|
|
|
|
NODE-ID is a numeric PipeWire Node id (other kinds of PipeWire objects
|
|
|
|
are not supported in this method).
|
|
|
|
PROPERTIES is an association list in the same format as in
|
2022-07-16 20:25:08 +02:00
|
|
|
`pipewire-access-properties'. It needn't contain all the properties, just
|
2022-07-12 21:27:19 +02:00
|
|
|
the properties to be changed.
|
2022-07-16 20:25:08 +02:00
|
|
|
CLASS is a PipeWire interface, see symbol `pipewire-accessor'.")
|
2022-06-04 21:51:45 +02:00
|
|
|
|
2022-07-16 20:25:08 +02:00
|
|
|
(cl-defgeneric pipewire-access-current-profile (class device-id)
|
2022-06-15 21:31:24 +02:00
|
|
|
"Return current profile of the given device.
|
|
|
|
DEVICE-ID is a numeric PipeWire Device id (other kinds of PipeWire
|
|
|
|
objects are not supported in this method).
|
|
|
|
|
|
|
|
The profile is an association list with elements of the form
|
2022-06-28 21:24:55 +02:00
|
|
|
\(PROPERTY . VALUE), in the same format as properties in
|
2022-07-16 20:25:08 +02:00
|
|
|
`pipewire-access-properties'.
|
2022-07-12 21:27:19 +02:00
|
|
|
|
2022-07-16 20:25:08 +02:00
|
|
|
CLASS is a PipeWire interface, see symbol `pipewire-accessor'.")
|
2022-06-15 21:31:24 +02:00
|
|
|
|
2022-07-16 20:25:08 +02:00
|
|
|
(cl-defgeneric pipewire-access-profiles (class device-id)
|
2022-06-15 21:31:24 +02:00
|
|
|
"Return available profiles of the given device.
|
|
|
|
DEVICE-ID is a numeric PipeWire Device id (other kinds of PipeWire
|
|
|
|
objects are not supported in this method).
|
|
|
|
|
|
|
|
Return a list of profiles, which are in the same format as in
|
2022-07-16 20:25:08 +02:00
|
|
|
`pipewire-access-current-profile'.
|
2022-07-12 21:27:19 +02:00
|
|
|
|
2022-07-16 20:25:08 +02:00
|
|
|
CLASS is a PipeWire interface, see symbol `pipewire-accessor'.")
|
2022-06-15 21:31:24 +02:00
|
|
|
|
2022-07-16 20:25:08 +02:00
|
|
|
(cl-defgeneric pipewire-access-set-profile (class device-id profile-index)
|
2022-06-15 21:31:24 +02:00
|
|
|
"Set the profile of the given device.
|
|
|
|
DEVICE-ID is a numeric PipeWire Device id (other kinds of PipeWire
|
|
|
|
objects are not supported in this method).
|
|
|
|
PROFILE-INDEX is a numeric index of the profile to set, as returned
|
2022-07-12 21:27:19 +02:00
|
|
|
from PipeWire.
|
2022-07-16 20:25:08 +02:00
|
|
|
CLASS is a PipeWire interface, see symbol `pipewire-accessor'.")
|
2022-06-15 21:31:24 +02:00
|
|
|
|
2022-07-16 20:25:08 +02:00
|
|
|
(cl-defgeneric pipewire-access-defaults (class)
|
2022-06-04 21:51:45 +02:00
|
|
|
"Return default sinks and sources.
|
|
|
|
An association lists is returned. Each list element is of the form
|
2022-06-28 21:24:55 +02:00
|
|
|
\(KEY . NAME) where KEY is a string identifying the given kind of
|
2022-06-04 21:51:45 +02:00
|
|
|
default sink or source as reported by PipeWire and NAME is a string
|
2022-07-12 21:27:19 +02:00
|
|
|
name of the node assigned to the default.
|
2022-07-16 20:25:08 +02:00
|
|
|
CLASS is a PipeWire interface, see symbol `pipewire-accessor'.")
|
2022-06-04 21:51:45 +02:00
|
|
|
|
2022-07-16 20:25:08 +02:00
|
|
|
(cl-defgeneric pipewire-access-set-default (class key node-name)
|
2022-06-04 21:51:45 +02:00
|
|
|
"Set default sink or source.
|
|
|
|
KEY is a string identifying the given kind of default sink or source
|
2022-07-16 20:25:08 +02:00
|
|
|
as reported in `pipewire-access-defaults' and NODE-NAME is a string name of
|
2022-07-12 21:27:19 +02:00
|
|
|
the node that should be assigned to KEY.
|
2022-07-16 20:25:08 +02:00
|
|
|
CLASS is a PipeWire interface, see symbol `pipewire-accessor'.")
|
2022-06-04 21:51:45 +02:00
|
|
|
|
|
|
|
;; pw-cli interface
|
|
|
|
|
2022-07-16 20:25:08 +02:00
|
|
|
(defvar pipewire-cli-command "pw-cli"
|
2022-07-16 20:12:05 +02:00
|
|
|
"Command to invoke pw-cli.")
|
2022-06-04 21:51:45 +02:00
|
|
|
|
2022-07-16 20:25:08 +02:00
|
|
|
(defvar pipewire-cli-metadata-command "pw-metadata"
|
2022-07-16 20:12:05 +02:00
|
|
|
"Command to invoke pw-metadata.")
|
2022-06-04 21:51:45 +02:00
|
|
|
|
2022-07-16 20:25:08 +02:00
|
|
|
(defclass pipewire-cli-accessor (pipewire-accessor)
|
2022-06-04 21:51:45 +02:00
|
|
|
()
|
|
|
|
:documentation
|
2022-07-16 20:12:05 +02:00
|
|
|
"Command line based interface to PipeWire.
|
2022-06-04 21:51:45 +02:00
|
|
|
Note this interface may not work with all PipeWire versions.")
|
|
|
|
|
2022-07-16 20:25:08 +02:00
|
|
|
(defun pipewire-cli--command (command args)
|
2022-06-04 21:51:45 +02:00
|
|
|
(apply #'call-process command nil t nil args)
|
|
|
|
(goto-char (point-min)))
|
|
|
|
|
2022-07-16 20:25:08 +02:00
|
|
|
(defun pipewire-cli--next-line ()
|
2022-06-04 21:51:45 +02:00
|
|
|
(goto-char (line-beginning-position 2)))
|
|
|
|
|
2022-07-16 20:25:08 +02:00
|
|
|
(defun pipewire-cli--parse-list ()
|
2022-06-04 21:51:45 +02:00
|
|
|
(let ((objects '()))
|
|
|
|
(while (re-search-forward "^[\t]id \\([0-9]+\\), type PipeWire:Interface:\\(.*\\)/.*$" nil t)
|
|
|
|
(let ((id (string-to-number (match-string 1)))
|
|
|
|
(properties `((type . ,(match-string 2)))))
|
2022-07-16 20:25:08 +02:00
|
|
|
(pipewire-cli--next-line)
|
2022-06-04 21:51:45 +02:00
|
|
|
(while (looking-at "^ [\t][\t]\\([a-z.]+\\) = \"\\(.*\\)\"")
|
|
|
|
(let ((property (match-string 1))
|
|
|
|
(value (match-string 2)))
|
|
|
|
(when (string-suffix-p ".id" property)
|
|
|
|
(setq value (string-to-number value)))
|
|
|
|
(push (cons property value) properties))
|
2022-07-16 20:25:08 +02:00
|
|
|
(pipewire-cli--next-line))
|
2022-06-04 21:51:45 +02:00
|
|
|
(push (cons id properties) objects)))
|
|
|
|
(nreverse objects)))
|
|
|
|
|
2022-07-16 20:25:08 +02:00
|
|
|
(cl-defmethod pipewire-access-objects ((_class pipewire-cli-accessor))
|
2022-06-04 21:51:45 +02:00
|
|
|
(with-temp-buffer
|
2022-07-16 20:25:08 +02:00
|
|
|
(pipewire-cli--command pipewire-cli-command '("list-objects"))
|
|
|
|
(pipewire-cli--parse-list)))
|
2022-06-04 21:51:45 +02:00
|
|
|
|
2022-07-16 20:25:08 +02:00
|
|
|
(defun pipewire-cli--read-property (&optional nesting)
|
2022-06-04 21:51:45 +02:00
|
|
|
(unless nesting
|
|
|
|
(setq nesting 0))
|
|
|
|
(when (looking-at (concat (make-string (+ 6 (* 2 nesting)) ? )
|
|
|
|
"\\([A-Za-z:]+\\) \\(.*\\)"))
|
|
|
|
(let ((type (match-string 1))
|
|
|
|
(value (match-string 2)))
|
|
|
|
(pcase type
|
|
|
|
("Bool"
|
|
|
|
(if (equal value "true") 'true 'false))
|
|
|
|
((or "Float" "Id" "Int")
|
|
|
|
(string-to-number value))
|
|
|
|
("String"
|
|
|
|
(substring value 1 -1))
|
|
|
|
((or "Array:" "Struct:")
|
|
|
|
(let ((array '())
|
|
|
|
item)
|
2022-07-16 20:25:08 +02:00
|
|
|
(pipewire-cli--next-line)
|
|
|
|
(while (setq item (pipewire-cli--read-property (1+ nesting)))
|
2022-06-04 21:51:45 +02:00
|
|
|
(push item array)
|
2022-07-16 20:25:08 +02:00
|
|
|
(pipewire-cli--next-line))
|
2022-06-04 21:51:45 +02:00
|
|
|
(nreverse array)))))))
|
2022-07-02 09:30:44 +02:00
|
|
|
|
2022-07-16 20:25:08 +02:00
|
|
|
(defun pipewire-cli--parse-properties ()
|
|
|
|
(pipewire-cli--next-line)
|
2022-06-15 21:03:42 +02:00
|
|
|
(let ((end (or (save-excursion (re-search-forward "^ Object:" nil t))
|
|
|
|
(point-max)))
|
2022-06-04 21:51:45 +02:00
|
|
|
(properties '()))
|
2022-06-15 21:03:42 +02:00
|
|
|
(while (and (< (point) end)
|
|
|
|
(re-search-forward "^ Prop: key \\([A-Za-z:]+\\)" end t))
|
2022-07-16 20:25:08 +02:00
|
|
|
(pipewire-cli--next-line)
|
2022-06-04 21:51:45 +02:00
|
|
|
(let ((property (car (last (split-string (match-string 1) ":"))))
|
2022-07-16 20:25:08 +02:00
|
|
|
(value (pipewire-cli--read-property)))
|
2022-06-04 21:51:45 +02:00
|
|
|
(when value
|
|
|
|
(push (cons property value) properties))))
|
2022-06-15 21:03:42 +02:00
|
|
|
(goto-char end)
|
2022-06-04 21:51:45 +02:00
|
|
|
properties))
|
|
|
|
|
2022-07-16 20:25:08 +02:00
|
|
|
(cl-defmethod pipewire-access-properties ((_class pipewire-cli-accessor) node-id)
|
2022-06-04 21:51:45 +02:00
|
|
|
(with-temp-buffer
|
2022-07-16 20:25:08 +02:00
|
|
|
(pipewire-cli--command pipewire-cli-command `("enum-params" ,(number-to-string node-id) "Props"))
|
|
|
|
(pipewire-cli--parse-properties)))
|
2022-06-04 21:51:45 +02:00
|
|
|
|
2022-07-16 20:25:08 +02:00
|
|
|
(defun pipewire-cli--format-property-value (value)
|
2022-06-06 19:55:25 +02:00
|
|
|
(cond
|
|
|
|
((consp value)
|
2022-07-16 20:25:08 +02:00
|
|
|
(concat "[ " (mapconcat #'pipewire-cli--format-property-value value ", ") " ]"))
|
2022-06-06 19:55:25 +02:00
|
|
|
((numberp value)
|
|
|
|
(number-to-string value))
|
|
|
|
(t
|
|
|
|
value)))
|
2022-06-04 21:51:45 +02:00
|
|
|
|
2022-07-16 20:25:08 +02:00
|
|
|
(defun pipewire-cli--format-property (property)
|
|
|
|
(format "%s: %s" (car property) (pipewire-cli--format-property-value (cdr property))))
|
2022-06-04 21:51:45 +02:00
|
|
|
|
2022-07-16 20:25:08 +02:00
|
|
|
(defun pipewire-cli--format-properties (properties)
|
|
|
|
(concat "{ " (mapconcat #'pipewire-cli--format-property properties ", ") " }"))
|
2022-06-16 07:30:52 +02:00
|
|
|
|
2022-07-16 20:25:08 +02:00
|
|
|
(defun pipewire-cli--set-parameter (object-id parameter value)
|
|
|
|
(let* ((formatted (pipewire-cli--format-properties value)))
|
|
|
|
(call-process pipewire-cli-command nil pipewire-cli-command nil
|
2022-06-16 07:30:52 +02:00
|
|
|
"set-param" (number-to-string object-id) parameter formatted)))
|
2022-06-15 21:31:24 +02:00
|
|
|
|
2022-07-16 20:25:08 +02:00
|
|
|
(cl-defmethod pipewire-access-set-properties ((_class pipewire-cli-accessor) node-id properties)
|
|
|
|
(pipewire-cli--set-parameter node-id "Props" properties))
|
2022-06-15 21:31:24 +02:00
|
|
|
|
2022-07-16 20:25:08 +02:00
|
|
|
(cl-defmethod pipewire-access-current-profile ((_class pipewire-cli-accessor) device-id)
|
2022-06-15 21:31:24 +02:00
|
|
|
(with-temp-buffer
|
2022-07-16 20:25:08 +02:00
|
|
|
(pipewire-cli--command pipewire-cli-command `("enum-params" ,(number-to-string device-id) "Profile"))
|
|
|
|
(pipewire-cli--parse-properties)))
|
2022-06-15 21:31:24 +02:00
|
|
|
|
2022-07-16 20:25:08 +02:00
|
|
|
(cl-defmethod pipewire-access-profiles ((_class pipewire-cli-accessor) device-id)
|
2022-06-15 21:31:24 +02:00
|
|
|
(with-temp-buffer
|
2022-07-16 20:25:08 +02:00
|
|
|
(pipewire-cli--command pipewire-cli-command `("enum-params" ,(number-to-string device-id) "EnumProfile"))
|
|
|
|
(cl-loop for profile = (pipewire-cli--parse-properties) then (pipewire-cli--parse-properties)
|
2022-06-15 21:31:24 +02:00
|
|
|
while profile
|
|
|
|
collect profile)))
|
|
|
|
|
2022-07-16 20:25:08 +02:00
|
|
|
(cl-defmethod pipewire-access-set-profile ((_class pipewire-cli-accessor) device-id profile-index)
|
|
|
|
(pipewire-cli--set-parameter device-id "Profile" `(("index" . ,profile-index) ("save" . "true"))))
|
2022-06-04 21:51:45 +02:00
|
|
|
|
2022-07-16 20:25:08 +02:00
|
|
|
(defun pipewire-cli--parse-metadata ()
|
2022-06-04 21:51:45 +02:00
|
|
|
(let ((metadata '()))
|
|
|
|
(while (re-search-forward
|
|
|
|
"key:'\\([a-z.]+\\)'.*\\(value\\|\"name\"\\): ?['\"]\\([^'\"]+\\)['\"]"
|
|
|
|
nil t)
|
|
|
|
(push (cons (match-string 1) (match-string 3)) metadata))
|
|
|
|
metadata))
|
|
|
|
|
2022-07-16 20:25:08 +02:00
|
|
|
(cl-defmethod pipewire-access-defaults ((_class pipewire-cli-accessor))
|
2022-06-04 21:51:45 +02:00
|
|
|
(with-temp-buffer
|
2022-07-16 20:25:08 +02:00
|
|
|
(pipewire-cli--command pipewire-cli-metadata-command '("0"))
|
|
|
|
(pipewire-cli--parse-metadata)))
|
2022-06-04 21:51:45 +02:00
|
|
|
|
2022-07-16 20:25:08 +02:00
|
|
|
(cl-defmethod pipewire-access-set-default ((_class pipewire-cli-accessor) property node-name)
|
|
|
|
(call-process pipewire-cli-metadata-command nil pipewire-cli-metadata-command nil
|
2022-06-04 21:51:45 +02:00
|
|
|
"0" property (format "{ \"name\": \"%s\" }" node-name)))
|
|
|
|
|
2022-07-16 20:25:08 +02:00
|
|
|
(provide 'pipewire-access)
|
2022-06-29 21:55:53 +02:00
|
|
|
|
2022-07-06 10:20:27 +02:00
|
|
|
;; Local Variables:
|
|
|
|
;; checkdoc-force-docstrings-flag: nil
|
|
|
|
;; End:
|
|
|
|
|
2022-07-16 20:25:08 +02:00
|
|
|
;;; pipewire-access.el ends here
|