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.Locale;
19 import javax.sound.sampled.UnsupportedAudioFileException;
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.openhab.binding.doorbird.internal.handler.DoorbellHandler;
24 import org.openhab.core.audio.AudioFormat;
25 import org.openhab.core.audio.AudioSinkSync;
26 import org.openhab.core.audio.AudioStream;
27 import org.openhab.core.audio.UnsupportedAudioFormatException;
28 import org.openhab.core.audio.UnsupportedAudioStreamException;
29 import org.openhab.core.library.types.PercentType;
32 * The audio sink for doorbird
34 * @author Gwendal Roulleau - Initial contribution
38 public class DoorbirdAudioSink extends AudioSinkSync {
40 private static final Set<AudioFormat> SUPPORTED_FORMATS = Set.of(AudioFormat.WAV);
41 private static final Set<Class<? extends AudioStream>> SUPPORTED_STREAMS = Set.of(AudioStream.class);
43 private DoorbellHandler doorbellHandler;
45 public DoorbirdAudioSink(DoorbellHandler doorbellHandler) {
46 this.doorbellHandler = doorbellHandler;
50 public String getId() {
51 return doorbellHandler.getThing().getUID().toString();
55 public @Nullable String getLabel(@Nullable Locale locale) {
56 return doorbellHandler.getThing().getLabel();
60 protected void processSynchronously(@Nullable AudioStream audioStream)
61 throws UnsupportedAudioFormatException, UnsupportedAudioStreamException {
62 if (audioStream == null) {
65 try (ConvertedInputStream normalizedULAWStream = new ConvertedInputStream(audioStream)) {
66 doorbellHandler.sendAudio(normalizedULAWStream);
67 } catch (UnsupportedAudioFileException | IOException e) {
68 throw new UnsupportedAudioFormatException("Cannot send to the doorbird sink", audioStream.getFormat(), e);
73 public Set<AudioFormat> getSupportedFormats() {
74 return SUPPORTED_FORMATS;
78 public Set<Class<? extends AudioStream>> getSupportedStreams() {
79 return SUPPORTED_STREAMS;
83 public PercentType getVolume() {
84 return new PercentType(100);
88 public void setVolume(PercentType volume) {
89 throw new UnsupportedOperationException("Volume can not be set");