]> git.ipfire.org Git - thirdparty/shairport-sync.git/blame - MQTT.md
Update RELEASENOTES-DEVELOPMENT.md
[thirdparty/shairport-sync.git] / MQTT.md
CommitLineData
687fd167
MB
1MQTT in Shairport Sync
2====
3To enable Shairport Sync to act as an MQTT publisher, you need to:
41. Install the mosquitto library:
5```
6# apt install libmosquitto-dev
7```
82. Add the configuration flag `--with-mqtt-client` to the list of parameters to the `./configure...` command, for example:
9```
10$ ./configure --with-mqtt-client --sysconfdir=/etc --with-alsa --with-avahi --with-ssl=openssl --with-systemd
11```
12
13If Shairport Sync has MQTT support, it will have the string `mqtt` in its configuration string. For example:
14```
15$ shairport-sync -V
163.3.8-OpenSSL-Avahi-ALSA-metadata-mqtt-sysconfdir:/etc
17```
18
9729fa95 19Setting Up MQTT Publishing
20====
21
ad429d07 22This is a rough guide on the setup of MQTT publishing in ShairPort-Sync. The MQTT service listens for and publishes metadata generated by the airplay source and shairport-sync.
9729fa95 23
ad429d07 24Below is a simple example of configuring shairport-sync to send parsed metadata to a MQTT server under the topic "shairport"
9729fa95 25
ad429d07 26Example Configuration
27----
28
29Shairport-sync is configured below to published parsed metadata information and album-art to a MQTT server.
30
31```xml
32
33metadata =
34{
35 enabled = "yes"; // set this to yes to get Shairport Sync to solicit metadata from the source and to pass it on via a pipe
36 include_cover_art = "yes"; // set to "yes" to get Shairport Sync to solicit cover art from the source and pass it via the pipe. You must also set "enabled" to "yes".
37 cover_art_cache_directory = "/tmp/shairport-sync/.cache/coverart"; // artwork will be stored in this directory if the dbus or MPRIS interfaces are enabled or if the MQTT client is in use. Set it to "" to prevent caching, which may be useful on some systems
38 pipe_name = "/tmp/shairport-sync-metadata";
39 pipe_timeout = 5000; // wait for this number of milliseconds for a blocked pipe to unblock before giving up
40};
41
42
43mqtt =
44{
45 enabled = "yes"; // set this to yes to enable the mqtt-metadata-service
46 hostname = "192.168.1.111"; // Hostname of the MQTT Broker
47 port = 1883; // Port on the MQTT Broker to connect to
48 username = "username"; //set this to a string to your username in order to enable username authentication
49 password = "password"; //set this to a string you your password in order to enable username & password authentication
50 topic = "shairport"; //MQTT topic where this instance of shairport-sync should publish. If not set, the general.name value is used.
51// publish_raw = "no"; //whether to publish all available metadata under the codes given in the 'metadata' docs.
52 publish_parsed = "yes"; //whether to publish a small (but useful) subset of metadata under human-understandable topics
53 publish_cover = "yes"; //whether to publish the cover over mqtt in binary form. This may lead to a bit of load on the broker
54// enable_remote = "no"; //whether to remote control via MQTT. RC is available under `topic`/remote.
55};
56```
57
58Import Notes
59----
60
61Publish Options
62
63One needs to set either `publish_raw`, `publish_parsed` or `publish_cover` in the MQTT setup or no messages will be published.
64
65
66Overall Active State of Stream
67
68`active_start` and `active_end` represent a stable on/off flag for the current airplay session.
69
70`active_start` is plublished when any new airplay session begins
71
72`active_end` will fire after a configured timeout period unless the airplay stream is resumed.
73
74```xml
75sessioncontrol =
76{
77// "active" state starts when play begins and ends when the active_state_timeout has elapsed after play ends, unless another play session starts before the timeout has fully elapsed.
78 active_state_timeout = 30.0;
79};
80```
9729fa95 81
82MetaData Parsing
83----
84
85
86Additional details regarding the metadata can be found at [https://github.com/mikebrady/shairport-sync-metadata-reader]
87
88Meta data is generated by both the stream source (iOS, itunes, etc) and by shairport-sync itself. This data is coded as two 4-character codes to identify each piece of data, the `type` and the `code`.
89
90The first 4-character code, called the `type`, is either:
91 * `core` for all the regular metadadata coming from iTunes, etc., or
92 * `ssnc` (for 'shairport-sync') for all metadata coming from Shairport Sync itself, such as start/end delimiters, etc.
93
94* For `core` metadata, the second 4-character code is the 4-character metadata code that comes from iTunes etc. See, for example, https://code.google.com/p/ytrack/wiki/DMAP for information about the significance of the codes. The original data supplied by the source, if any, follows, and is encoded in base64 format. The length of the data is also provided.
95* For `ssnc` metadata, the second 4-character code is used to distinguish the messages. Cover art, coming from the source, is not tagged in the same way as other metadata, it seems, so is sent as an `ssnc` type metadata message with the code `PICT`. Progress information, similarly, is not tagged like other source-originated metadata, so it is sent as an `ssnc` type with the code `prgr`.
96
97Here are some of the `core` codes commonly passed from the source:
a9c8de50
CC
98 * `asal` -- album
99 * `asar` -- artist
100 * `ascp` -- composer
101 * `asgn` -- genre
102 * `astm` -- song time
103 * `caps` -- play status (stopped, paused, playing)
104 * `minm` -- title
105 * `mper` -- track persistent id
9729fa95 106
107Here are the 'ssnc' codes defined so far:
108 * `PICT` -- the payload is a picture, either a JPEG or a PNG. Check the first few bytes to see which.
a9c8de50 109 * `acre` -- Active Remote
82c7f4ef 110 * `cdid` -- Client advertised Device ID
a9c8de50 111 * `clip` -- the payload is the IP address of the client, i.e. the sender of audio. Can be an IPv4 or an IPv6 address.
82c7f4ef
CC
112 * `cmac` -- Client advertised MAC address
113 * `cmod` -- Client advertised model ("iPhone14,2")
a9c8de50
CC
114 * `daid` -- DACP ID
115 * `dapo` -- DACP Port
116 * `mden` -- a sequence of metadata has ended. The RTP timestamp associated with the metadata sequence is included as data, if available.
117 * `mdst` -- a sequence of metadata is about to start. The RTP timestamp associated with the metadata sequence is included as data, if available.
9729fa95 118 * `pbeg` -- play stream begin. No arguments
a9c8de50
CC
119 * `pcen` -- a picture has been sent. The RTP timestamp associated with it is included as data, if available.
120 * `pcst` -- a picture is about to be sent. The RTP timestamp associated with it is included as data, if available.
9729fa95 121 * `pend` -- play stream end. No arguments
122 * `pfls` -- play stream flush. No arguments
123 * `prsm` -- play stream resume. No arguments
9729fa95 124 * `prgr` -- progress -- this is metadata from AirPlay consisting of RTP timestamps for the start of the current play sequence, the current play point and the end of the play sequence.
a9c8de50 125 * `pvol` -- play volume. The volume is sent as a string -- "airplay_volume,volume,lowest_volume,highest_volume", where "volume", "lowest_volume" and "highest_volume" are given in dB. The "airplay_volume" is what's sent by the source (e.g. iTunes) to the player, and is from 0.00 down to -30.00, with -144.00 meaning "mute". This is linear on the volume control slider of iTunes or iOS AirPlay. If the volume setting is being ignored by Shairport Sync itself, the volume, lowest_volume and highest_volume values are zero.
82c7f4ef 126 * `snam` -- a device e.g. "Joe's iPhone" has started a play session. Specifically, it's the "X-Apple-Client-Name" string for AP1, or direct from the configuration Plist for AP2.
9729fa95 127 * `snua` -- a "user agent" e.g. "iTunes/12..." has started a play session. Specifically, it's the "User-Agent" string.
128 * `stal` -- this is an error message meaning that reception of a large piece of metadata, usually a large picture, has stalled; bad things may happen.
a9c8de50 129 * `svip` -- the payload is the IP address of the server, i.e. shairport-sync. Can be an IPv4 or an IPv6 address.
9729fa95 130
9729fa95 131
132Parsed Messages
133----
134
135The MQTT service can parse the above raw messages into a subset of human-readable topics that include,
136
a9c8de50 137* `active_remote_id` -- Active Remote ID
9729fa95 138* `artist` -- text of artist name
139* `album` -- text of album name
a9c8de50 140* `client_ip` -- IP address of the connected client
82c7f4ef
CC
141* `client_device_id` -- Client advertised Device ID
142* `client_mac_address` -- Client advertised MAC address
143* `client_model` -- Client advertised model ("iPhone14,2")
144* `client_name` -- Client advertised name ("Joe's iPhone")
a9c8de50 145* `dacp_id` -- DACP ID
9729fa95 146* `format` -- ??
a9c8de50
CC
147* `genre` -- text of genre
148* `server_ip` -- IP address of shairport-sync that the client is connected to
9729fa95 149* `songalbum` --
a9c8de50 150* `title` -- text of song title
9729fa95 151* `volume` -- The volume is sent as a string -- "airplay_volume,volume,lowest_volume,highest_volume", where "volume", "lowest_volume" and "highest_volume" are given in dB. (see above)
9729fa95 152
16af2d2c 153and empty messages (`--`) at the following topics are published.
9729fa95 154
155* `play_start` -- fired at the begining of every song
156* `play_end` -- fired at the end of every song
157* `play_flush` -- fired when song is skipped or on positional change
158* `play_resume` -- fired when song play resumes from pause
159* `active_start` -- fired when a new active airplay session begins
160* `active_end` -- fired after a configured timeout period after the stream ends (unless a new stream begins)
161
9729fa95 162
9729fa95 163
ad429d07 164## Consuming MQTT Data
9729fa95 165
ad429d07 166Users will find examples on how to consume the MQTT data in various home automation projects. If a user has an interesting use please raise a new issue to suggest adding it to the guide, or simply fork the development branch and create a pull-request.
9729fa95 167
ad429d07 168### Home Assistant Variable Templates
9729fa95 169
ad429d07 170Examples of consuming "parsed" MQTT data in [Home Assistant](https://www.home-assistant.io/)
16af2d2c 171The `active_start` and `active_end` have good potential use as triggers to turn on and off various connect receivers/zones. "payload_off" is set to prevent accidental triggering.
9729fa95 172
ad429d07 173```yml
174binary_sensor:
9729fa95 175
ad429d07 176 - platform: mqtt
177 name: "shairport active start"
178 state_topic: "shairport/active_start"
16af2d2c 179 payload_on: "--"
ad429d07 180 payload_off: "OFF"
181 off_delay: 300
9729fa95 182
ad429d07 183 - platform: mqtt
184 name: "shairport active end"
185 state_topic: "shairport/active_end"
16af2d2c 186 payload_on: "--"
ad429d07 187 payload_off: "OFF"
188 off_delay: 300
189```
9729fa95 190
f4cad1d1 191Below parsed data is saved into the Home Assistant database as sensor data. Please note the conversion of the volume from dB to percentage.
ad429d07 192
193```yml
c62ae679 194mqtt:
195 sensor:
196 - name: "shairport album"
197 state_topic: "shairport/album"
198 expire_after: 600
199
200 - name: "shairport artist"
201 state_topic: "shairport/artist"
202 expire_after: 600
203
204 - name: "shairport title"
205 state_topic: "shairport/title"
206 expire_after: 600
207
208 - name: "shairport genre"
209 state_topic: "shairport/genre"
210 expire_after: 600
211
212 - name: "shairport volume (dB)"
213 state_topic: "shairport/volume"
214
215 - name: "shairport volume (PCT)"
216 state_topic: "shairport/volume"
217 value_template: "{{ value | regex_findall_index(find='^(.+?),', index=0, ignorecase=False) | float / 30 + 1 }}"
218 unit_of_measurement: 'percent'
9729fa95 219```