Search in sources :

Example 1 with FixedLengthAudioStream

use of org.eclipse.smarthome.core.audio.FixedLengthAudioStream in project smarthome by eclipse.

the class SonosAudioSink method process.

@Override
public void process(AudioStream audioStream) throws UnsupportedAudioFormatException, UnsupportedAudioStreamException {
    if (audioStream == null) {
        // in case the audioStream is null, this should be interpreted as a request to end any currently playing
        // stream.
        logger.trace("Stop currently playing stream.");
        handler.stopPlaying(OnOffType.ON);
    } else if (audioStream instanceof URLAudioStream) {
        // it is an external URL, the speaker can access it itself and play it.
        URLAudioStream urlAudioStream = (URLAudioStream) audioStream;
        handler.playURI(new StringType(urlAudioStream.getURL()));
        try {
            audioStream.close();
        } catch (IOException e) {
        }
    } else if (audioStream instanceof FixedLengthAudioStream) {
        // the AudioServlet, so a one time serving won't work.
        if (callbackUrl != null) {
            String relativeUrl = audioHTTPServer.serve((FixedLengthAudioStream) audioStream, 10).toString();
            String url = callbackUrl + relativeUrl;
            AudioFormat format = audioStream.getFormat();
            if (!ThingHandlerHelper.isHandlerInitialized(handler)) {
                logger.warn("Sonos speaker '{}' is not initialized - status is {}", handler.getThing().getUID(), handler.getThing().getStatus());
            } else if (AudioFormat.WAV.isCompatible(format)) {
                handler.playNotificationSoundURI(new StringType(url + AudioStreamUtils.EXTENSION_SEPARATOR + FileAudioStream.WAV_EXTENSION));
            } else if (AudioFormat.MP3.isCompatible(format)) {
                handler.playNotificationSoundURI(new StringType(url + AudioStreamUtils.EXTENSION_SEPARATOR + FileAudioStream.MP3_EXTENSION));
            } else {
                throw new UnsupportedAudioFormatException("Sonos only supports MP3 or WAV.", format);
            }
        } else {
            logger.warn("We do not have any callback url, so Sonos cannot play the audio stream!");
        }
    } else {
        IOUtils.closeQuietly(audioStream);
        throw new UnsupportedAudioStreamException("Sonos can only handle FixedLengthAudioStreams and URLAudioStreams.", audioStream.getClass());
    // Instead of throwing an exception, we could ourselves try to wrap it into a
    // FixedLengthAudioStream, but this might be dangerous as we have no clue, how much data to expect from
    // the stream.
    }
}
Also used : UnsupportedAudioFormatException(org.eclipse.smarthome.core.audio.UnsupportedAudioFormatException) FixedLengthAudioStream(org.eclipse.smarthome.core.audio.FixedLengthAudioStream) StringType(org.eclipse.smarthome.core.library.types.StringType) URLAudioStream(org.eclipse.smarthome.core.audio.URLAudioStream) IOException(java.io.IOException) AudioFormat(org.eclipse.smarthome.core.audio.AudioFormat) UnsupportedAudioStreamException(org.eclipse.smarthome.core.audio.UnsupportedAudioStreamException)

Example 2 with FixedLengthAudioStream

use of org.eclipse.smarthome.core.audio.FixedLengthAudioStream in project smarthome by eclipse.

the class AudioServlet method removeTimedOutStreams.

private synchronized void removeTimedOutStreams() {
    for (String streamId : multiTimeStreams.keySet()) {
        if (streamTimeouts.get(streamId) < System.nanoTime()) {
            // the stream has expired, we need to remove it!
            FixedLengthAudioStream stream = multiTimeStreams.remove(streamId);
            streamTimeouts.remove(streamId);
            IOUtils.closeQuietly(stream);
            stream = null;
            logger.debug("Removed timed out stream {}", streamId);
        }
    }
}
Also used : FixedLengthAudioStream(org.eclipse.smarthome.core.audio.FixedLengthAudioStream)

Example 3 with FixedLengthAudioStream

use of org.eclipse.smarthome.core.audio.FixedLengthAudioStream in project smarthome by eclipse.

the class AudioServlet method prepareInputStream.

private InputStream prepareInputStream(final String streamId, final HttpServletResponse resp) throws AudioException {
    final AudioStream stream;
    final boolean multiAccess;
    if (oneTimeStreams.containsKey(streamId)) {
        stream = oneTimeStreams.remove(streamId);
        multiAccess = false;
    } else if (multiTimeStreams.containsKey(streamId)) {
        stream = multiTimeStreams.get(streamId);
        multiAccess = true;
    } else {
        return null;
    }
    logger.debug("Stream to serve is {}", streamId);
    // try to set the content-type, if possible
    final String mimeType;
    if (stream.getFormat().getCodec() == AudioFormat.CODEC_MP3) {
        mimeType = "audio/mpeg";
    } else if (stream.getFormat().getContainer() == AudioFormat.CONTAINER_WAVE) {
        mimeType = "audio/wav";
    } else if (stream.getFormat().getContainer() == AudioFormat.CONTAINER_OGG) {
        mimeType = "audio/ogg";
    } else {
        mimeType = null;
    }
    if (mimeType != null) {
        resp.setContentType(mimeType);
    }
    // try to set the content-length, if possible
    if (stream instanceof FixedLengthAudioStream) {
        final Long size = ((FixedLengthAudioStream) stream).length();
        if (size != null) {
            resp.setContentLength(size.intValue());
        }
    }
    if (multiAccess) {
        // we need to care about concurrent access and have a separate stream for each thread
        return ((FixedLengthAudioStream) stream).getClonedStream();
    } else {
        return stream;
    }
}
Also used : FixedLengthAudioStream(org.eclipse.smarthome.core.audio.FixedLengthAudioStream) AudioStream(org.eclipse.smarthome.core.audio.AudioStream) FixedLengthAudioStream(org.eclipse.smarthome.core.audio.FixedLengthAudioStream)

Example 4 with FixedLengthAudioStream

use of org.eclipse.smarthome.core.audio.FixedLengthAudioStream in project smarthome by eclipse.

the class WebAudioAudioSink method process.

@Override
public void process(AudioStream audioStream) throws UnsupportedAudioFormatException, UnsupportedAudioStreamException {
    if (audioStream == null) {
        // in case the audioStream is null, this should be interpreted as a request to end any currently playing
        // stream.
        logger.debug("Web Audio sink does not support stopping the currently playing stream.");
        return;
    }
    logger.debug("Received audio stream of format {}", audioStream.getFormat());
    if (audioStream instanceof URLAudioStream) {
        // it is an external URL, so we can directly pass this on.
        URLAudioStream urlAudioStream = (URLAudioStream) audioStream;
        sendEvent(urlAudioStream.getURL());
        IOUtils.closeQuietly(audioStream);
    } else if (audioStream instanceof FixedLengthAudioStream) {
        // we need to serve it for a while and make it available to multiple clients, hence only
        // FixedLengthAudioStreams are supported.
        sendEvent(audioHTTPServer.serve((FixedLengthAudioStream) audioStream, 10).toString());
    } else {
        IOUtils.closeQuietly(audioStream);
        throw new UnsupportedAudioStreamException("Web audio sink can only handle FixedLengthAudioStreams and URLAudioStreams.", audioStream.getClass());
    }
}
Also used : FixedLengthAudioStream(org.eclipse.smarthome.core.audio.FixedLengthAudioStream) URLAudioStream(org.eclipse.smarthome.core.audio.URLAudioStream) UnsupportedAudioStreamException(org.eclipse.smarthome.core.audio.UnsupportedAudioStreamException)

Aggregations

FixedLengthAudioStream (org.eclipse.smarthome.core.audio.FixedLengthAudioStream)4 URLAudioStream (org.eclipse.smarthome.core.audio.URLAudioStream)2 UnsupportedAudioStreamException (org.eclipse.smarthome.core.audio.UnsupportedAudioStreamException)2 IOException (java.io.IOException)1 AudioFormat (org.eclipse.smarthome.core.audio.AudioFormat)1 AudioStream (org.eclipse.smarthome.core.audio.AudioStream)1 UnsupportedAudioFormatException (org.eclipse.smarthome.core.audio.UnsupportedAudioFormatException)1 StringType (org.eclipse.smarthome.core.library.types.StringType)1