]> git.basschouten.com Git - openhab-addons.git/blob
eeab4962b6077f269c80d1e8c5efc15926ba34ee
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
7  * This program and the accompanying materials are made available under the
8  * terms of the Eclipse Public License 2.0 which is available at
9  * http://www.eclipse.org/legal/epl-2.0
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.pulseaudio.internal;
14
15 import static org.openhab.binding.pulseaudio.internal.PulseaudioBindingConstants.*;
16
17 import java.io.IOException;
18 import java.io.InputStream;
19 import java.io.PrintStream;
20 import java.math.BigDecimal;
21 import java.net.Socket;
22 import java.net.SocketException;
23 import java.net.SocketTimeoutException;
24 import java.net.UnknownHostException;
25 import java.util.ArrayList;
26 import java.util.Comparator;
27 import java.util.List;
28 import java.util.Optional;
29 import java.util.Random;
30 import java.util.stream.Collectors;
31
32 import org.eclipse.jdt.annotation.NonNullByDefault;
33 import org.eclipse.jdt.annotation.Nullable;
34 import org.openhab.binding.pulseaudio.internal.cli.Parser;
35 import org.openhab.binding.pulseaudio.internal.handler.DeviceIdentifier;
36 import org.openhab.binding.pulseaudio.internal.items.AbstractAudioDeviceConfig;
37 import org.openhab.binding.pulseaudio.internal.items.AbstractAudioDeviceConfig.State;
38 import org.openhab.binding.pulseaudio.internal.items.Module;
39 import org.openhab.binding.pulseaudio.internal.items.Sink;
40 import org.openhab.binding.pulseaudio.internal.items.SinkInput;
41 import org.openhab.binding.pulseaudio.internal.items.Source;
42 import org.openhab.binding.pulseaudio.internal.items.SourceOutput;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45
46 /**
47  * The client connects to a pulseaudio server via TCP. It reads the current state of the
48  * pulseaudio server (available sinks, sources,...) and can send commands to the server.
49  * The syntax of the commands is the same as for the pactl command line tool provided by pulseaudio.
50  *
51  * On the pulseaudio server the module-cli-protocol-tcp has to be loaded.
52  *
53  * @author Tobias Bräutigam - Initial contribution
54  * @author Miguel Álvarez - changes for loading audio source module and nullability annotations
55  */
56 @NonNullByDefault
57 public class PulseaudioClient {
58
59     private final Logger logger = LoggerFactory.getLogger(PulseaudioClient.class);
60
61     private String host;
62     private int port;
63     private @Nullable Socket client;
64
65     private List<AbstractAudioDeviceConfig> items;
66     private List<Module> modules;
67
68     /**
69      * Corresponding to the global binding configuration
70      */
71     private PulseAudioBindingConfiguration configuration;
72
73     /**
74      * corresponding name to execute actions on sink items
75      */
76     private static final String ITEM_SINK = "sink";
77
78     /**
79      * corresponding name to execute actions on source items
80      */
81     private static final String ITEM_SOURCE = "source";
82
83     /**
84      * corresponding name to execute actions on sink-input items
85      */
86     private static final String ITEM_SINK_INPUT = "sink-input";
87
88     /**
89      * corresponding name to execute actions on source-output items
90      */
91     private static final String ITEM_SOURCE_OUTPUT = "source-output";
92
93     /**
94      * command to list the loaded modules
95      */
96     private static final String CMD_LIST_MODULES = "list-modules";
97
98     /**
99      * command to list the sinks
100      */
101     private static final String CMD_LIST_SINKS = "list-sinks";
102
103     /**
104      * command to list the sources
105      */
106     private static final String CMD_LIST_SOURCES = "list-sources";
107
108     /**
109      * command to list the sink-inputs
110      */
111     private static final String CMD_LIST_SINK_INPUTS = "list-sink-inputs";
112
113     /**
114      * command to list the source-outputs
115      */
116     private static final String CMD_LIST_SOURCE_OUTPUTS = "list-source-outputs";
117
118     /**
119      * command to load a module
120      */
121     private static final String CMD_LOAD_MODULE = "load-module";
122
123     /**
124      * command to unload a module
125      */
126     private static final String CMD_UNLOAD_MODULE = "unload-module";
127
128     /**
129      * name of the module-combine-sink
130      */
131     private static final String MODULE_COMBINE_SINK = "module-combine-sink";
132
133     public PulseaudioClient(String host, int port, PulseAudioBindingConfiguration configuration) {
134         this.host = host;
135         this.port = port;
136         this.configuration = configuration;
137
138         items = new ArrayList<>();
139         modules = new ArrayList<>();
140     }
141
142     public boolean isConnected() {
143         Socket clientSocket = client;
144         return clientSocket != null ? clientSocket.isConnected() : false;
145     }
146
147     /**
148      * updates the item states and their relationships
149      */
150     public synchronized void update() {
151         // one step copy
152         modules = new ArrayList<Module>(Parser.parseModules(listModules()));
153
154         List<AbstractAudioDeviceConfig> newItems = new ArrayList<>(); // prepare new list before assigning it
155         if (configuration.sink) {
156             logger.debug("reading sinks");
157             newItems.addAll(Parser.parseSinks(listSinks(), this));
158         }
159         if (configuration.source) {
160             logger.debug("reading sources");
161             newItems.addAll(Parser.parseSources(listSources(), this));
162         }
163         if (configuration.sinkInput) {
164             logger.debug("reading sink-inputs");
165             newItems.addAll(Parser.parseSinkInputs(listSinkInputs(), this));
166         }
167         if (configuration.sourceOutput) {
168             logger.debug("reading source-outputs");
169             newItems.addAll(Parser.parseSourceOutputs(listSourceOutputs(), this));
170         }
171         logger.debug("Pulseaudio server {}: {} modules and {} items updated", host, modules.size(), newItems.size());
172         items = newItems;
173     }
174
175     private String listModules() {
176         return this.sendRawRequest(CMD_LIST_MODULES);
177     }
178
179     private String listSinks() {
180         return this.sendRawRequest(CMD_LIST_SINKS);
181     }
182
183     private String listSources() {
184         return this.sendRawRequest(CMD_LIST_SOURCES);
185     }
186
187     private String listSinkInputs() {
188         return this.sendRawRequest(CMD_LIST_SINK_INPUTS);
189     }
190
191     private String listSourceOutputs() {
192         return this.sendRawRequest(CMD_LIST_SOURCE_OUTPUTS);
193     }
194
195     /**
196      * retrieves a module by its id
197      *
198      * @param id
199      * @return the corresponding {@link Module} to the given <code>id</code>
200      */
201     public @Nullable Module getModule(int id) {
202         for (Module module : modules) {
203             if (module.getId() == id) {
204                 return module;
205             }
206         }
207         return null;
208     }
209
210     /**
211      * send the command directly to the pulseaudio server
212      * for a list of available commands please take a look at
213      * http://www.freedesktop.org/wiki/Software/PulseAudio/Documentation/User/CLI
214      *
215      * @param command
216      */
217     public void sendCommand(String command) {
218         sendRawCommand(command);
219     }
220
221     /**
222      * retrieves a {@link Sink} by its name
223      *
224      * @return the corresponding {@link Sink} to the given <code>name</code>
225      */
226     public @Nullable Sink getSink(String name) {
227         for (AbstractAudioDeviceConfig item : items) {
228             if (item.getPaName().equalsIgnoreCase(name) && item instanceof Sink) {
229                 return (Sink) item;
230             }
231         }
232         return null;
233     }
234
235     /**
236      * retrieves a {@link Sink} by its id
237      *
238      * @return the corresponding {@link Sink} to the given <code>id</code>
239      */
240     public @Nullable Sink getSink(int id) {
241         for (AbstractAudioDeviceConfig item : items) {
242             if (item.getId() == id && item instanceof Sink) {
243                 return (Sink) item;
244             }
245         }
246         return null;
247     }
248
249     /**
250      * retrieves a {@link Source} by its id
251      *
252      * @return the corresponding {@link Source} to the given <code>id</code>
253      */
254     public @Nullable Source getSource(int id) {
255         for (AbstractAudioDeviceConfig item : items) {
256             if (item.getId() == id && item instanceof Source) {
257                 return (Source) item;
258             }
259         }
260         return null;
261     }
262
263     /**
264      * retrieves an {@link AbstractAudioDeviceConfig} by its identifier
265      * If several devices correspond to the deviceIdentifier, returns the first one (aphabetical order)
266      *
267      * @param The device identifier to match against
268      * @return the corresponding {@link AbstractAudioDeviceConfig} to the given <code>name</code>
269      */
270     public @Nullable AbstractAudioDeviceConfig getGenericAudioItem(DeviceIdentifier deviceIdentifier) {
271         List<AbstractAudioDeviceConfig> matchingDevices = items.stream()
272                 .filter(device -> device.matches(deviceIdentifier))
273                 .sorted(Comparator.comparing(AbstractAudioDeviceConfig::getPaName)).collect(Collectors.toList());
274         if (matchingDevices.size() == 1) {
275             return matchingDevices.get(0);
276         } else if (matchingDevices.size() > 1) {
277             logger.debug(
278                     "Cannot select exactly one audio device, so choosing the first. To choose without ambiguity between the {} devices matching the identifier {}, you can maybe use a more restrictive 'additionalFilter' parameter",
279                     matchingDevices.size(), deviceIdentifier.getNameOrDescription());
280             return matchingDevices.get(0);
281         }
282         return null;
283     }
284
285     /**
286      * Get all items previously parsed from the pulseaudio server.
287      *
288      * @return All items parsed from the pulseaudio server
289      */
290     public List<AbstractAudioDeviceConfig> getItems() {
291         return items;
292     }
293
294     /**
295      * changes the <code>mute</code> state of the corresponding {@link Sink}
296      *
297      * @param item the {@link Sink} to handle
298      * @param mute mutes the sink if true, unmutes if false
299      */
300     public void setMute(@Nullable AbstractAudioDeviceConfig item, boolean mute) {
301         if (item == null) {
302             return;
303         }
304         String itemCommandName = getItemCommandName(item);
305         if (itemCommandName == null) {
306             return;
307         }
308         String muteString = mute ? "1" : "0";
309         sendRawCommand("set-" + itemCommandName + "-mute " + item.getId() + " " + muteString);
310         // update internal data
311         item.setMuted(mute);
312     }
313
314     /**
315      * change the volume of an {@link AbstractAudioDeviceConfig}
316      *
317      * @param item the {@link AbstractAudioDeviceConfig} to handle
318      * @param vol the new volume value the {@link AbstractAudioDeviceConfig} should be changed to (possible values from
319      *            0 - 65536)
320      */
321     public void setVolume(AbstractAudioDeviceConfig item, int vol) {
322         String itemCommandName = getItemCommandName(item);
323         if (itemCommandName == null) {
324             return;
325         }
326         sendRawCommand("set-" + itemCommandName + "-volume " + item.getId() + " " + vol);
327         item.setVolume(Math.round(100f / 65536f * vol));
328     }
329
330     /**
331      * Locate or load (if needed) the simple protocol tcp module for the given sink
332      * and returns the port.
333      * The module loading (if needed) will be tried several times, on a new random port each time.
334      *
335      * @param item the sink we are searching for
336      * @param simpleTcpPortPref the port to use if we have to load the module
337      * @return the port on which the module is listening
338      * @throws InterruptedException
339      */
340     public Optional<Integer> loadModuleSimpleProtocolTcpIfNeeded(AbstractAudioDeviceConfig item,
341             Integer simpleTcpPortPref, @Nullable String format, @Nullable BigDecimal rate,
342             @Nullable BigDecimal channels) throws InterruptedException {
343         int currentTry = 0;
344         int simpleTcpPortToTry = simpleTcpPortPref;
345         String itemType = getItemCommandName(item);
346         do {
347             Optional<Integer> simplePort = findSimpleProtocolTcpModule(item, format, rate, channels);
348
349             if (simplePort.isPresent()) {
350                 return simplePort;
351             } else {
352                 String moduleOptions = itemType + "=" + item.getPaName() + " port=" + simpleTcpPortToTry;
353                 if (item instanceof Source && format != null && rate != null && channels != null) {
354                     moduleOptions = moduleOptions + String.format(" record=true format=%s rate=%d channels=%d", format,
355                             rate.longValue(), channels.intValue());
356                 }
357                 sendRawCommand("load-module module-simple-protocol-tcp " + moduleOptions);
358                 simpleTcpPortToTry = new Random().nextInt(64512) + 1024; // a random port above 1024
359             }
360             Thread.sleep(100);
361             update();
362             currentTry++;
363         } while (currentTry < 3);
364
365         logger.warn("The pulseaudio binding tried 3 times to load the module-simple-protocol-tcp"
366                 + " on random port on the pulseaudio server and give up trying");
367         return Optional.empty();
368     }
369
370     /**
371      * Find a simple protocol module corresponding to the given sink in argument
372      * and returns the port it listens to
373      *
374      * @param item
375      * @return
376      */
377     private Optional<Integer> findSimpleProtocolTcpModule(AbstractAudioDeviceConfig item, @Nullable String format,
378             @Nullable BigDecimal rate, @Nullable BigDecimal channels) {
379         String itemType = getItemCommandName(item);
380         if (itemType == null) {
381             return Optional.empty();
382         }
383         List<Module> modulesCopy = new ArrayList<Module>(modules);
384         var isSource = item instanceof Source;
385         return modulesCopy.stream() // iteration on modules
386                 .filter(module -> MODULE_SIMPLE_PROTOCOL_TCP_NAME.equals(module.getPaName())) // filter on module name
387                 .filter(module -> {
388                     boolean nameMatch = extractArgumentFromLine(itemType, module.getArgument()) // extract sick|source
389                             .map(name -> name.equals(item.getPaName())).orElse(false);
390                     if (isSource && nameMatch) {
391                         boolean recordStream = extractArgumentFromLine("record", module.getArgument())
392                                 .map("true"::equals).orElse(false);
393                         if (!recordStream) {
394                             return false;
395                         }
396                         if (format != null) {
397                             boolean rateMatch = extractArgumentFromLine("format", module.getArgument())
398                                     .map(format::equals).orElse(false);
399                             if (!rateMatch) {
400                                 return false;
401                             }
402                         }
403                         if (rate != null) {
404                             boolean rateMatch = extractArgumentFromLine("rate", module.getArgument())
405                                     .map(value -> Long.parseLong(value) == rate.longValue()).orElse(false);
406                             if (!rateMatch) {
407                                 return false;
408                             }
409                         }
410                         if (channels != null) {
411                             boolean channelsMatch = extractArgumentFromLine("channels", module.getArgument())
412                                     .map(value -> Integer.parseInt(value) == channels.intValue()).orElse(false);
413                             if (!channelsMatch) {
414                                 return false;
415                             }
416                         }
417                     }
418                     return nameMatch;
419                 }) // filter on sink name
420                 .findAny() // get a corresponding module
421                 .map(module -> extractArgumentFromLine("port", module.getArgument())
422                         .orElse(Integer.toString(MODULE_SIMPLE_PROTOCOL_TCP_DEFAULT_PORT))) // get port
423                 .map(portS -> Integer.parseInt(portS));
424     }
425
426     private Optional<String> extractArgumentFromLine(String argumentWanted, @Nullable String argumentLine) {
427         String argument = null;
428         if (argumentLine != null) {
429             int startPortIndex = argumentLine.indexOf(argumentWanted + "=");
430             if (startPortIndex != -1) {
431                 startPortIndex = startPortIndex + argumentWanted.length() + 1;
432                 int endPortIndex = argumentLine.indexOf(" ", startPortIndex);
433                 if (endPortIndex == -1) {
434                     endPortIndex = argumentLine.length();
435                 }
436                 argument = argumentLine.substring(startPortIndex, endPortIndex);
437             }
438         }
439         return Optional.ofNullable(argument);
440     }
441
442     /**
443      * returns the item names that can be used in commands
444      *
445      * @param item
446      * @return
447      */
448     private @Nullable String getItemCommandName(AbstractAudioDeviceConfig item) {
449         if (item instanceof Sink) {
450             return ITEM_SINK;
451         } else if (item instanceof Source) {
452             return ITEM_SOURCE;
453         } else if (item instanceof SinkInput) {
454             return ITEM_SINK_INPUT;
455         } else if (item instanceof SourceOutput) {
456             return ITEM_SOURCE_OUTPUT;
457         }
458         return null;
459     }
460
461     /**
462      * change the volume of an {@link AbstractAudioDeviceConfig}
463      *
464      * @param item the {@link AbstractAudioDeviceConfig} to handle
465      * @param vol the new volume percent value the {@link AbstractAudioDeviceConfig} should be changed to (possible
466      *            values from 0 - 100)
467      */
468     public void setVolumePercent(AbstractAudioDeviceConfig item, int vol) {
469         int volumeToSet = vol;
470         if (volumeToSet <= 100) {
471             volumeToSet = toAbsoluteVolume(volumeToSet);
472         }
473         setVolume(item, volumeToSet);
474     }
475
476     /**
477      * transform a percent volume to a value that can be send to the pulseaudio server (0-65536)
478      *
479      * @param percent
480      * @return
481      */
482     private int toAbsoluteVolume(int percent) {
483         return (int) Math.round(65536f / 100f * Double.valueOf(percent));
484     }
485
486     /**
487      * changes the combined sinks slaves to the given <code>sinks</code>
488      *
489      * @param combinedSink the combined sink which slaves should be changed
490      * @param sinks the list of new slaves
491      */
492     public void setCombinedSinkSlaves(@Nullable Sink combinedSink, List<Sink> sinks) {
493         if (combinedSink == null || !combinedSink.isCombinedSink()) {
494             return;
495         }
496         List<String> slaves = new ArrayList<>();
497         for (Sink sink : sinks) {
498             slaves.add(sink.getPaName());
499         }
500         // 1. delete old combined-sink
501         Module lastModule = combinedSink.getModule();
502         if (lastModule != null) {
503             sendRawCommand(CMD_UNLOAD_MODULE + " " + lastModule.getId());
504         }
505         // 2. add new combined-sink with same name and all slaves
506         sendRawCommand(CMD_LOAD_MODULE + " " + MODULE_COMBINE_SINK + " sink_name=" + combinedSink.getPaName()
507                 + " slaves=" + String.join(",", slaves));
508         // 3. update internal data structure because the combined sink has a new number + other slaves
509         update();
510     }
511
512     /**
513      * sets the sink a sink-input should be routed to
514      *
515      * @param sinkInput the sink-input to be rerouted
516      * @param sink the new sink the sink-input should be routed to
517      */
518     public void moveSinkInput(@Nullable SinkInput sinkInput, @Nullable Sink sink) {
519         if (sinkInput == null || sink == null) {
520             return;
521         }
522         sendRawCommand("move-sink-input " + sinkInput.getId() + " " + sink.getId());
523         sinkInput.setSink(sink);
524     }
525
526     /**
527      * sets the sink a source-output should be routed to
528      *
529      * @param sourceOutput the source-output to be rerouted
530      * @param source the new source the source-output should be routed to
531      */
532     public void moveSourceOutput(@Nullable SourceOutput sourceOutput, @Nullable Source source) {
533         if (sourceOutput == null || source == null) {
534             return;
535         }
536         sendRawCommand("move-sink-input " + sourceOutput.getId() + " " + source.getId());
537         sourceOutput.setSource(source);
538     }
539
540     /**
541      * suspend a source
542      *
543      * @param source the source which state should be changed
544      * @param suspend suspend it or not
545      */
546     public void suspendSource(@Nullable Source source, boolean suspend) {
547         if (source == null) {
548             return;
549         }
550         if (suspend) {
551             sendRawCommand("suspend-source " + source.getId() + " 1");
552             source.setState(State.SUSPENDED);
553         } else {
554             sendRawCommand("suspend-source " + source.getId() + " 0");
555             // unsuspending the source could result in different states (RUNNING,IDLE,...)
556             // update to get the new state
557             update();
558         }
559     }
560
561     /**
562      * suspend a sink
563      *
564      * @param sink the sink which state should be changed
565      * @param suspend suspend it or not
566      */
567     public void suspendSink(@Nullable Sink sink, boolean suspend) {
568         if (sink == null) {
569             return;
570         }
571         if (suspend) {
572             sendRawCommand("suspend-sink " + sink.getId() + " 1");
573             sink.setState(State.SUSPENDED);
574         } else {
575             sendRawCommand("suspend-sink " + sink.getId() + " 0");
576             // unsuspending the sink could result in different states (RUNNING,IDLE,...)
577             // update to get the new state
578             update();
579         }
580     }
581
582     /**
583      * changes the combined sinks slaves to the given <code>sinks</code>
584      *
585      * @param combinedSinkName the combined sink which slaves should be changed
586      * @param sinks the list of new slaves
587      */
588     public void setCombinedSinkSlaves(String combinedSinkName, List<Sink> sinks) {
589         if (getSink(combinedSinkName) != null) {
590             return;
591         }
592         List<String> slaves = new ArrayList<>();
593         for (Sink sink : sinks) {
594             slaves.add(sink.getPaName());
595         }
596         // add new combined-sink with same name and all slaves
597         sendRawCommand(CMD_LOAD_MODULE + " " + MODULE_COMBINE_SINK + " sink_name=" + combinedSinkName + " slaves="
598                 + String.join(",", slaves));
599         // update internal data structure because the combined sink is new
600         update();
601     }
602
603     private synchronized void sendRawCommand(String command) {
604         checkConnection();
605         Socket clientSocket = client;
606         if (clientSocket != null && clientSocket.isConnected()) {
607             try {
608                 PrintStream out = new PrintStream(clientSocket.getOutputStream(), true);
609                 logger.trace("sending command {} to pa-server {}", command, host);
610                 out.print(command + "\r\n");
611                 out.close();
612                 clientSocket.close();
613             } catch (IOException e) {
614                 logger.warn("{}", e.getMessage(), e);
615             }
616         }
617     }
618
619     private String sendRawRequest(String command) {
620         logger.trace("_sendRawRequest({})", command);
621         checkConnection();
622         String result = "";
623         Socket clientSocket = client;
624         if (clientSocket != null && clientSocket.isConnected()) {
625             try {
626                 PrintStream out = new PrintStream(clientSocket.getOutputStream(), true);
627                 out.print(command + "\r\n");
628
629                 InputStream instr = clientSocket.getInputStream();
630
631                 try {
632                     byte[] buff = new byte[1024];
633                     int retRead = 0;
634                     int lc = 0;
635                     do {
636                         retRead = instr.read(buff);
637                         lc++;
638                         if (retRead > 0) {
639                             String line = new String(buff, 0, retRead);
640                             if (line.endsWith(">>> ") && lc > 1) {
641                                 result += line.substring(0, line.length() - 4);
642                                 break;
643                             }
644                             result += line.trim();
645                         }
646                     } while (retRead > 0);
647                 } catch (SocketTimeoutException e) {
648                     // Timeout -> as newer PA versions (>=5.0) do not send the >>> we have no chance
649                     // to detect the end of the answer, except by this timeout
650                 } catch (SocketException e) {
651                     logger.warn("Socket exception while sending pulseaudio command: {}", e.getMessage());
652                 } catch (IOException e) {
653                     logger.warn("Exception while reading socket: {}", e.getMessage());
654                 }
655                 instr.close();
656                 out.close();
657                 clientSocket.close();
658                 return result;
659             } catch (IOException e) {
660                 logger.warn("{}", e.getMessage(), e);
661             }
662         }
663         return result;
664     }
665
666     private void checkConnection() {
667         try {
668             connect();
669         } catch (IOException e) {
670             logger.debug("{}", e.getMessage(), e);
671         }
672     }
673
674     /**
675      * Connects to the pulseaudio server (timeout 500ms)
676      */
677     public void connect() throws IOException {
678         Socket clientSocket = client;
679         if (clientSocket == null || clientSocket.isClosed() || !clientSocket.isConnected()) {
680             logger.trace("Try to connect...");
681             try {
682                 var clientFinal = new Socket(host, port);
683                 clientFinal.setSoTimeout(500);
684                 client = clientFinal;
685                 logger.trace("connected");
686             } catch (UnknownHostException e) {
687                 client = null;
688                 throw new IOException("Unknown host", e);
689             } catch (IllegalArgumentException e) {
690                 client = null;
691                 throw new IOException("Invalid port", e);
692             } catch (SecurityException | SocketException e) {
693                 client = null;
694                 throw new IOException(
695                         String.format("Cannot connect socket: %s", e.getMessage() != null ? e.getMessage() : ""), e);
696             } catch (IOException e) {
697                 client = null;
698                 throw e;
699             }
700         }
701     }
702
703     /**
704      * Disconnects from the pulseaudio server
705      */
706     public void disconnect() {
707         Socket clientSocket = client;
708         if (clientSocket != null) {
709             try {
710                 clientSocket.close();
711             } catch (IOException e) {
712                 logger.debug("{}", e.getMessage(), e);
713             }
714         }
715     }
716 }