forked from mikebrady/shairport-sync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdbus-service.c
235 lines (212 loc) · 10.1 KB
/
dbus-service.c
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
#include <stdio.h>
#include <string.h>
#include "config.h"
#include "common.h"
#include "player.h"
#include "rtsp.h"
#include "rtp.h"
#include "dacp.h"
#include "dbus-service.h"
gboolean notify_loudness_filter_active_callback(ShairportSync *skeleton, gpointer user_data) {
debug(1, "\"notify_loudness_filter_active_callback\" called.");
if (shairport_sync_get_loudness_filter_active(skeleton)) {
debug(1, "activating loudness filter");
config.loudness = 1;
} else {
debug(1, "deactivating loudness filter");
config.loudness = 0;
}
return TRUE;
}
gboolean notify_loudness_threshold_callback(ShairportSync *skeleton, gpointer user_data) {
gdouble th = shairport_sync_get_loudness_threshold(skeleton);
if ((th <= 0.0) && (th >= -100.0)) {
debug(1, "Setting loudness threshhold to %f.", th);
config.loudness_reference_volume_db = th;
} else {
debug(1, "Invalid loudness threshhold: %f. Ignored.", th);
}
return TRUE;
}
gboolean notify_volume_callback(ShairportSync *skeleton, gpointer user_data) {
gint vo = shairport_sync_get_volume(skeleton);
if ((vo >= 0) && (vo <= 100)) {
if (playing_conn) {
if (vo !=
playing_conn
->dacp_volume) { // this is to stop an infinite loop of setting->checking->setting...
// debug(1, "Remote-setting volume to %d.", vo);
// get the information we need -- the absolute volume, the speaker list, our ID
struct dacp_speaker_stuff speaker_info[50];
int32_t overall_volume = dacp_get_client_volume(playing_conn);
int speaker_count =
dacp_get_speaker_list(playing_conn, (dacp_spkr_stuff *)&speaker_info, 50);
// get our machine number
uint16_t *hn = (uint16_t *)config.hw_addr;
uint32_t *ln = (uint32_t *)(config.hw_addr + 2);
uint64_t t1 = ntohs(*hn);
uint64_t t2 = ntohl(*ln);
int64_t machine_number = (t1 << 32) + t2; // this form is useful
// Let's find our own speaker in the array and pick up its relative volume
int i;
int32_t relative_volume = 0;
int32_t active_speakers = 0;
for (i = 0; i < speaker_count; i++) {
if (speaker_info[i].speaker_number == machine_number) {
// debug(1,"Our speaker number found: %ld.",machine_number);
relative_volume = speaker_info[i].volume;
}
if (speaker_info[i].active == 1) {
active_speakers++;
}
}
if (active_speakers == 1) {
// must be just this speaker
dacp_set_include_speaker_volume(playing_conn, machine_number, vo);
} else if (active_speakers == 0) {
debug(1, "No speakers!");
} else {
// debug(1, "Speakers: %d, active: %d",speaker_count,active_speakers);
if (vo >= overall_volume) {
// debug(1,"Multiple speakers active, but desired new volume is highest");
dacp_set_include_speaker_volume(playing_conn, machine_number, vo);
} else {
// the desired volume is less than the current overall volume and there is more than one
// speaker
// we must find out the highest other speaker volume.
// If the desired volume is less than it, we must set the current_overall volume to that
// highest volume
// and set our volume relative to it.
// If the desired volume is greater than the highest current volume, then we can just go
// ahead
// with dacp_set_include_speaker_volume, setting the new current overall volume to the
// desired new level
// with the speaker at 100%
int32_t highest_other_volume = 0;
for (i = 0; i < speaker_count; i++) {
if ((speaker_info[i].speaker_number != machine_number) &&
(speaker_info[i].active == 1) &&
(speaker_info[i].volume > highest_other_volume)) {
highest_other_volume = speaker_info[i].volume;
}
}
highest_other_volume = (highest_other_volume * overall_volume + 50) / 100;
if (highest_other_volume <= vo) {
// debug(1,"Highest other volume %d is less than or equal to the desired new volume
// %d.",highest_other_volume,vo);
dacp_set_include_speaker_volume(playing_conn, machine_number, vo);
} else {
// debug(1,"Highest other volume %d is greater than the desired new volume
// %d.",highest_other_volume,vo);
// if the present overall volume is higher than the highest other volume at present,
// then bring it down to it.
if (overall_volume > highest_other_volume) {
// debug(1,"Lower overall volume to new highest volume.");
dacp_set_include_speaker_volume(
playing_conn, machine_number,
highest_other_volume); // set the overall volume to the highest one
}
int32_t desired_relative_volume =
(vo * 100 + (highest_other_volume / 2)) / highest_other_volume;
// debug(1,"Set our speaker volume relative to the highest volume.");
dacp_set_speaker_volume(
playing_conn, machine_number,
desired_relative_volume); // set the overall volume to the highest one
}
}
}
// } else {
// debug(1, "No need to remote-set volume to %d, as it is already set to this
// value.",playing_conn->dacp_volume);
}
} else
debug(1, "no thread playing -- ignored.");
} else {
debug(1, "Invalid volume: %d -- ignored.", vo);
}
return TRUE;
}
static gboolean on_handle_remote_command(ShairportSync *skeleton, GDBusMethodInvocation *invocation,
const gchar *command, gpointer user_data) {
debug(1, "RemoteCommand with command \"%s\".", command);
if (playing_conn) {
char server_reply[2000];
ssize_t reply_size =
dacp_send_client_command(playing_conn, command, server_reply, sizeof(server_reply));
if (reply_size >= 0) {
// not interested in the response.
// if (strstr(server_reply, "HTTP/1.1 204") == server_reply) {
// debug(1,"Client response is No Content");
// } else if (strstr(server_reply, "HTTP/1.1 200 OK") != server_reply) {
// debug("Client response is OK, with content");
// } else {
if (strstr(server_reply, "HTTP/1.1 204") != server_reply) {
debug(1,
"Client request to server responded with %d characters starting with this response:",
strlen(server_reply));
int i;
for (i = 0; i < reply_size; i++)
if (server_reply[i] < ' ')
debug(1, "%d %02x", i, server_reply[i]);
else
debug(1, "%d %02x '%c'", i, server_reply[i], server_reply[i]);
// sprintf((char *)message + 2 * i, "%02x", server_reply[i]);
// debug(1,"Content is \"%s\".",message);
}
} else {
debug(1, "Error at rtp_send_client_command");
}
} else {
debug(1, "no thread playing -- RemoteCommand ignored.");
}
shairport_sync_complete_remote_command(skeleton, invocation);
return TRUE;
}
static void on_dbus_name_acquired(GDBusConnection *connection, const gchar *name, gpointer user_data) {
debug(1,"Well-known interface name \"%s\" acquired for %s.",name,config.appName);
shairportSyncSkeleton = shairport_sync_skeleton_new();
g_dbus_interface_skeleton_export(G_DBUS_INTERFACE_SKELETON(shairportSyncSkeleton), connection,
"/org/gnome/ShairportSync", NULL);
shairport_sync_set_loudness_threshold(SHAIRPORT_SYNC(shairportSyncSkeleton),
config.loudness_reference_volume_db);
debug(1, "Loudness threshold is %f.", config.loudness_reference_volume_db);
if (config.loudness == 0) {
shairport_sync_set_loudness_filter_active(SHAIRPORT_SYNC(shairportSyncSkeleton), FALSE);
debug(1, "Loudness is off");
} else {
shairport_sync_set_loudness_filter_active(SHAIRPORT_SYNC(shairportSyncSkeleton), TRUE);
debug(1, "Loudness is on");
}
g_signal_connect(shairportSyncSkeleton, "notify::loudness-filter-active",
G_CALLBACK(notify_loudness_filter_active_callback), NULL);
g_signal_connect(shairportSyncSkeleton, "notify::loudness-threshold",
G_CALLBACK(notify_loudness_threshold_callback), NULL);
g_signal_connect(shairportSyncSkeleton, "notify::volume", G_CALLBACK(notify_volume_callback), NULL);
g_signal_connect(shairportSyncSkeleton, "handle-remote-command", G_CALLBACK(on_handle_remote_command), NULL);
debug(1,"Shairport Sync D-BUS service started on interface \"%s\".",name);
}
static void on_dbus_name_lost_again(GDBusConnection *connection, const gchar *name, gpointer user_data) {
warn("Could not acquire an Shairport Sync D-BUS interface.");
}
static void on_dbus_name_lost(GDBusConnection *connection, const gchar *name, gpointer user_data) {
debug(1,"Could not acquire well-known interface name \"%s\" -- will try adding the process number to the end of it.",name);
pid_t pid = getpid();
char interface_name[256] = "";
sprintf(interface_name,"org.gnome.ShairportSync.i%d",pid);
GBusType dbus_bus_type = G_BUS_TYPE_SYSTEM;
if (config.dbus_service_bus_type==DBT_session)
dbus_bus_type = G_BUS_TYPE_SESSION;
debug(1,"Looking for well-known interface name \"%s\".",interface_name);
g_bus_own_name(dbus_bus_type, interface_name, G_BUS_NAME_OWNER_FLAGS_NONE, NULL,
on_dbus_name_acquired, on_dbus_name_lost_again, NULL, NULL);
}
int start_dbus_service() {
shairportSyncSkeleton = NULL;
GBusType dbus_bus_type = G_BUS_TYPE_SYSTEM;
if (config.dbus_service_bus_type==DBT_session)
dbus_bus_type = G_BUS_TYPE_SESSION;
debug(1,"Looking for well-known name \"org.gnome.ShairportSync\".");
g_bus_own_name(dbus_bus_type, "org.gnome.ShairportSync", G_BUS_NAME_OWNER_FLAGS_NONE, NULL,
on_dbus_name_acquired, on_dbus_name_lost, NULL, NULL);
return 0; // this is just to quieten a compiler warning
}