]> git.basschouten.com Git - openhab-addons.git/blob
d6c82b5c0111e8fdb062cf482eb3ecd54d4abff4
[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.doorbird.internal.audio;
14
15 import java.io.IOException;
16 import java.util.HashSet;
17 import java.util.Locale;
18 import java.util.Set;
19
20 import javax.sound.sampled.UnsupportedAudioFileException;
21
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.openhab.binding.doorbird.internal.handler.DoorbellHandler;
25 import org.openhab.core.audio.AudioFormat;
26 import org.openhab.core.audio.AudioSink;
27 import org.openhab.core.audio.AudioStream;
28 import org.openhab.core.audio.FixedLengthAudioStream;
29 import org.openhab.core.audio.UnsupportedAudioFormatException;
30 import org.openhab.core.audio.UnsupportedAudioStreamException;
31 import org.openhab.core.library.types.PercentType;
32
33 /**
34  * The audio sink for doorbird
35  *
36  * @author Gwendal Roulleau - Initial contribution
37  *
38  */
39 @NonNullByDefault
40 public class DoorbirdAudioSink implements AudioSink {
41
42     private static final HashSet<AudioFormat> SUPPORTED_FORMATS = new HashSet<>();
43     private static final HashSet<Class<? extends AudioStream>> SUPPORTED_STREAMS = new HashSet<>();
44
45     private DoorbellHandler doorbellHandler;
46
47     static {
48         SUPPORTED_FORMATS.add(AudioFormat.WAV);
49         SUPPORTED_STREAMS.add(FixedLengthAudioStream.class);
50     }
51
52     public DoorbirdAudioSink(DoorbellHandler doorbellHandler) {
53         this.doorbellHandler = doorbellHandler;
54     }
55
56     @Override
57     public String getId() {
58         return doorbellHandler.getThing().getUID().toString();
59     }
60
61     @Override
62     public @Nullable String getLabel(@Nullable Locale locale) {
63         return doorbellHandler.getThing().getLabel();
64     }
65
66     @Override
67     public void process(@Nullable AudioStream audioStream)
68             throws UnsupportedAudioFormatException, UnsupportedAudioStreamException {
69         if (audioStream == null) {
70             return;
71         }
72         try (ConvertedInputStream normalizedULAWStream = new ConvertedInputStream(audioStream)) {
73             doorbellHandler.sendAudio(normalizedULAWStream);
74         } catch (UnsupportedAudioFileException | IOException e) {
75             throw new UnsupportedAudioFormatException("Cannot send to the doorbird sink", audioStream.getFormat(), e);
76         }
77     }
78
79     @Override
80     public Set<AudioFormat> getSupportedFormats() {
81         return SUPPORTED_FORMATS;
82     }
83
84     @Override
85     public Set<Class<? extends AudioStream>> getSupportedStreams() {
86         return SUPPORTED_STREAMS;
87     }
88
89     @Override
90     public PercentType getVolume() {
91         return new PercentType(100);
92     }
93
94     @Override
95     public void setVolume(PercentType volume) {
96         // NOT IMPLEMENTED
97     }
98 }