-
-
Notifications
You must be signed in to change notification settings - Fork 428
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[audio/voice] Add pcm audio websocket with dialog support
Signed-off-by: Miguel Álvarez <[email protected]>
- Loading branch information
Showing
5 changed files
with
1,051 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
158 changes: 158 additions & 0 deletions
158
...penhab.core.voice/src/main/java/org/openhab/core/voice/websocket/PCMWebSocketAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
/** | ||
* Copyright (c) 2010-2024 Contributors to the openHAB project | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0 | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
package org.openhab.core.voice.websocket; | ||
|
||
import static java.nio.ByteBuffer.*; | ||
import org.eclipse.jdt.annotation.NonNullByDefault; | ||
import org.eclipse.jetty.websocket.servlet.ServletUpgradeRequest; | ||
import org.eclipse.jetty.websocket.servlet.ServletUpgradeResponse; | ||
import org.openhab.core.audio.AudioManager; | ||
import org.openhab.core.common.ThreadPoolManager; | ||
import org.openhab.core.events.EventSubscriber; | ||
import org.openhab.core.io.websocket.WebSocketAdapter; | ||
import org.openhab.core.voice.VoiceManager; | ||
import org.osgi.framework.BundleContext; | ||
import org.osgi.service.component.annotations.Activate; | ||
import org.osgi.service.component.annotations.Component; | ||
import org.osgi.service.component.annotations.Reference; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.io.IOException; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
import java.util.concurrent.ScheduledExecutorService; | ||
import java.util.concurrent.ScheduledFuture; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
/** | ||
* The {@link PCMWebSocketAdapter} creates instances of {@link PCMWebSocketConnection} to handle pcm audio. | ||
* | ||
* @author Miguel Álvarez Díez - Initial contribution | ||
*/ | ||
@NonNullByDefault | ||
@Component(immediate = true, service = { EventSubscriber.class, WebSocketAdapter.class }) | ||
public class PCMWebSocketAdapter implements WebSocketAdapter { | ||
public static final String ADAPTER_ID = "pcm-audio"; | ||
|
||
private final Logger logger = LoggerFactory.getLogger(PCMWebSocketAdapter.class); | ||
private final ScheduledExecutorService executor = ThreadPoolManager.getScheduledPool("pcm-audio-websocket"); | ||
protected final BundleContext bundleContext; | ||
protected final VoiceManager voiceManager; | ||
protected final AudioManager audioManager; | ||
private final ScheduledFuture<?> pingTask; | ||
private final Set<PCMWebSocketConnection> speakerConnections = Collections.synchronizedSet(new HashSet<>()); | ||
@Activate | ||
public PCMWebSocketAdapter(BundleContext bundleContext, final @Reference AudioManager audioManager, | ||
final @Reference VoiceManager voiceManager) { | ||
this.bundleContext = bundleContext; | ||
this.audioManager = audioManager; | ||
this.voiceManager = voiceManager; | ||
this.pingTask = executor.scheduleWithFixedDelay(this::pingHandlers, 10, 5, TimeUnit.SECONDS); | ||
} | ||
|
||
private void pingHandlers() { | ||
var handlers = new ArrayList<>(speakerConnections); | ||
for (var handler : handlers) { | ||
if (handler != null) { | ||
boolean pinned = false; | ||
var remote = handler.getRemote(); | ||
if (remote != null) { | ||
try { | ||
remote.sendPing(wrap("oh".getBytes(StandardCharsets.UTF_8))); | ||
pinned = true; | ||
} catch (IOException ignored) { | ||
} | ||
} | ||
if (!pinned) { | ||
logger.warn("ping failed, disconnecting speaker {}", handler.getId()); | ||
var session = handler.getSession(); | ||
if (session != null) { | ||
session.close(); | ||
} | ||
} | ||
} | ||
|
||
} | ||
} | ||
|
||
private void disconnectHandlers() { | ||
logger.debug("Disconnecting {} clients...", speakerConnections.size()); | ||
var handlers = new ArrayList<>(speakerConnections); | ||
for (var handler : handlers) { | ||
onSpeakerDisconnected(handler); | ||
var session = handler.getSession(); | ||
if (session != null) { | ||
try { | ||
session.disconnect(); | ||
} catch (IOException e) { | ||
logger.debug("Disconnect failed: {}", e.getMessage()); | ||
} | ||
} | ||
} | ||
} | ||
|
||
public List<PCMWebSocketConnection> getSpeakerConnections() { | ||
synchronized (speakerConnections) { | ||
return new ArrayList<>(speakerConnections); | ||
} | ||
} | ||
|
||
private void onSpeakerConnected(PCMWebSocketConnection speaker) throws IllegalStateException { | ||
synchronized (speakerConnections) { | ||
if (getSpeakerConnection(speaker.getId()) != null) { | ||
throw new IllegalStateException("Another speaker with the same id is already connected"); | ||
} | ||
speakerConnections.add(speaker); | ||
logger.debug("connected speakers {}", speakerConnections.size()); | ||
} | ||
} | ||
|
||
private void onSpeakerDisconnected(PCMWebSocketConnection connection) { | ||
logger.debug("speaker disconnected '{}'", connection.getId()); | ||
synchronized (speakerConnections) { | ||
speakerConnections.remove(connection); | ||
logger.debug("connected speakers {}", speakerConnections.size()); | ||
} | ||
} | ||
|
||
public PCMWebSocketConnection getSpeakerConnection(String id) { | ||
synchronized (speakerConnections) { | ||
return speakerConnections.stream() | ||
.filter(speakerConnection -> speakerConnection.getId().equalsIgnoreCase(id)).findAny().orElse(null); | ||
} | ||
} | ||
|
||
@Override | ||
public String getId() { | ||
return ADAPTER_ID; | ||
} | ||
|
||
@Override | ||
public Object createWebSocket(ServletUpgradeRequest servletUpgradeRequest, | ||
ServletUpgradeResponse servletUpgradeResponse) { | ||
logger.debug("creating connection!"); | ||
return new PCMWebSocketConnection(this, executor); | ||
} | ||
|
||
public synchronized void dispose() { | ||
logger.debug("Unregistering protocols"); | ||
pingTask.cancel(true); | ||
disconnectHandlers(); | ||
} | ||
|
||
} |
Oops, something went wrong.