]> git.basschouten.com Git - openhab-addons.git/blob
a0cf2b73bd366fdeb18720eb8047ece790efed54
[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.upnpcontrol.internal.audiosink;
14
15 import static org.openhab.binding.upnpcontrol.internal.UpnpControlBindingConstants.NOTIFICATION_AUDIOSINK_EXTENSION;
16
17 import java.io.IOException;
18 import java.util.Locale;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.openhab.binding.upnpcontrol.internal.handler.UpnpRendererHandler;
23 import org.openhab.core.audio.AudioHTTPServer;
24 import org.openhab.core.library.types.PercentType;
25
26 /**
27  *
28  * This class works as a standard audio sink for openHAB, but with specific behavior for the audio players. It is only
29  * meant to be used for playing notifications. When sending audio through this sink, the previously playing media will
30  * be interrupted and will automatically resume after playing the notification. If no volume is specified, the
31  * notification volume will be controlled by the media player notification volume configuration.
32  *
33  * @author Mark Herwege - Initial contribution
34  */
35 @NonNullByDefault
36 public class UpnpNotificationAudioSink extends UpnpAudioSink {
37
38     public UpnpNotificationAudioSink(UpnpRendererHandler handler, AudioHTTPServer audioHTTPServer, String callbackUrl) {
39         super(handler, audioHTTPServer, callbackUrl);
40     }
41
42     @Override
43     public String getId() {
44         return handler.getThing().getUID().toString() + NOTIFICATION_AUDIOSINK_EXTENSION;
45     }
46
47     @Override
48     public @Nullable String getLabel(@Nullable Locale locale) {
49         return handler.getThing().getLabel() + NOTIFICATION_AUDIOSINK_EXTENSION;
50     }
51
52     @Override
53     public void setVolume(@Nullable PercentType volume) throws IOException {
54         if (volume != null) {
55             handler.setNotificationVolume(volume);
56         }
57     }
58
59     @Override
60     protected void playMedia(String url) {
61         String newUrl = url;
62         if (!url.startsWith("x-") && !url.startsWith("http")) {
63             newUrl = "x-file-cifs:" + url;
64         }
65         handler.playNotification(newUrl);
66     }
67 }