2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.doorbird.internal.audio;
15 import java.io.IOException;
16 import java.util.HashSet;
17 import java.util.Locale;
20 import javax.sound.sampled.UnsupportedAudioFileException;
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;
34 * The audio sink for doorbird
36 * @author Gwendal Roulleau - Initial contribution
40 public class DoorbirdAudioSink implements AudioSink {
42 private static final HashSet<AudioFormat> SUPPORTED_FORMATS = new HashSet<>();
43 private static final HashSet<Class<? extends AudioStream>> SUPPORTED_STREAMS = new HashSet<>();
45 private DoorbellHandler doorbellHandler;
48 SUPPORTED_FORMATS.add(AudioFormat.WAV);
49 SUPPORTED_STREAMS.add(FixedLengthAudioStream.class);
52 public DoorbirdAudioSink(DoorbellHandler doorbellHandler) {
53 this.doorbellHandler = doorbellHandler;
57 public String getId() {
58 return doorbellHandler.getThing().getUID().toString();
62 public @Nullable String getLabel(@Nullable Locale locale) {
63 return doorbellHandler.getThing().getLabel();
67 public void process(@Nullable AudioStream audioStream)
68 throws UnsupportedAudioFormatException, UnsupportedAudioStreamException {
69 if (audioStream == null) {
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);
80 public Set<AudioFormat> getSupportedFormats() {
81 return SUPPORTED_FORMATS;
85 public Set<Class<? extends AudioStream>> getSupportedStreams() {
86 return SUPPORTED_STREAMS;
90 public PercentType getVolume() {
91 return new PercentType(100);
95 public void setVolume(PercentType volume) {