Search in sources :

Example 1 with StartStopListener

use of net.pms.external.StartStopListener in project UniversalMediaServer by UniversalMediaServer.

the class DLNAResource method startPlaying.

/**
 * Plugin implementation. When this item is going to play, it will notify all the
 * StartStopListener objects available.
 *
 * @param rendererId
 * @param incomingRenderer
 *
 * @see StartStopListener
 */
public void startPlaying(final String rendererId, final RendererConfiguration incomingRenderer) {
    final String requestId = getRequestId(rendererId);
    synchronized (requestIdToRefcount) {
        Integer temp = requestIdToRefcount.get(requestId);
        if (temp == null) {
            temp = 0;
        }
        final Integer refCount = temp;
        requestIdToRefcount.put(requestId, refCount + 1);
        if (refCount == 0) {
            final DLNAResource self = this;
            Runnable r = new Runnable() {

                @Override
                public void run() {
                    InetAddress rendererIp;
                    try {
                        rendererIp = InetAddress.getByName(rendererId);
                        RendererConfiguration renderer;
                        if (incomingRenderer == null) {
                            renderer = RendererConfiguration.getRendererConfigurationBySocketAddress(rendererIp);
                        } else {
                            renderer = incomingRenderer;
                        }
                        String rendererName = "unknown renderer";
                        try {
                            renderer.setPlayingRes(self);
                            rendererName = renderer.getRendererName().replaceAll("\n", "");
                        } catch (NullPointerException e) {
                        }
                        if (!quietPlay()) {
                            LOGGER.info("Started playing " + getName() + " on your " + rendererName);
                            LOGGER.debug("The full filename of which is: " + getSystemName() + " and the address of the renderer is: " + rendererId);
                        }
                    } catch (UnknownHostException ex) {
                        LOGGER.debug("" + ex);
                    }
                    startTime = System.currentTimeMillis();
                    for (final ExternalListener listener : ExternalFactory.getExternalListeners()) {
                        if (listener instanceof StartStopListener) {
                            // run these asynchronously for slow handlers (e.g. logging, scrobbling)
                            Runnable fireStartStopEvent = new Runnable() {

                                @Override
                                public void run() {
                                    try {
                                        ((StartStopListener) listener).nowPlaying(media, self);
                                    } catch (Throwable t) {
                                        LOGGER.error("Notification of startPlaying event failed for StartStopListener {}", listener.getClass(), t);
                                    }
                                }
                            };
                            new Thread(fireStartStopEvent, "StartPlaying Event for " + listener.name()).start();
                        }
                    }
                }
            };
            new Thread(r, "StartPlaying Event").start();
        }
    }
}
Also used : StartStopListener(net.pms.external.StartStopListener) UnknownHostException(java.net.UnknownHostException) RendererConfiguration(net.pms.configuration.RendererConfiguration) ExternalListener(net.pms.external.ExternalListener) InetAddress(java.net.InetAddress)

Example 2 with StartStopListener

use of net.pms.external.StartStopListener in project UniversalMediaServer by UniversalMediaServer.

the class DLNAResource method stopPlaying.

/**
 * Plugin implementation. When this item is going to stop playing, it will notify all the StartStopListener
 * objects available.
 *
 * @see StartStopListener
 */
public void stopPlaying(final String rendererId, final RendererConfiguration incomingRenderer) {
    final DLNAResource self = this;
    final String requestId = getRequestId(rendererId);
    Runnable defer = new Runnable() {

        @Override
        public void run() {
            long start = startTime;
            try {
                Thread.sleep(STOP_PLAYING_DELAY);
            } catch (InterruptedException e) {
                LOGGER.error("stopPlaying sleep interrupted", e);
            }
            synchronized (requestIdToRefcount) {
                final Integer refCount = requestIdToRefcount.get(requestId);
                assert refCount != null;
                assert refCount > 0;
                requestIdToRefcount.put(requestId, refCount - 1);
                if (start != startTime) {
                    return;
                }
                Runnable r = new Runnable() {

                    @Override
                    public void run() {
                        if (refCount == 1) {
                            requestIdToRefcount.put(requestId, 0);
                            InetAddress rendererIp;
                            try {
                                rendererIp = InetAddress.getByName(rendererId);
                                RendererConfiguration renderer;
                                if (incomingRenderer == null) {
                                    renderer = RendererConfiguration.getRendererConfigurationBySocketAddress(rendererIp);
                                } else {
                                    renderer = incomingRenderer;
                                }
                                String rendererName = "unknown renderer";
                                try {
                                    // Reset only if another item hasn't already begun playing
                                    if (renderer.getPlayingRes() == self) {
                                        renderer.setPlayingRes(null);
                                    }
                                    rendererName = renderer.getRendererName();
                                } catch (NullPointerException e) {
                                }
                                if (!quietPlay()) {
                                    LOGGER.info("Stopped playing " + getName() + " on your " + rendererName);
                                    LOGGER.debug("The full filename of which is: " + getSystemName() + " and the address of the renderer is: " + rendererId);
                                }
                            } catch (UnknownHostException ex) {
                                LOGGER.debug("" + ex);
                            }
                            internalStop();
                            for (final ExternalListener listener : ExternalFactory.getExternalListeners()) {
                                if (listener instanceof StartStopListener) {
                                    // run these asynchronously for slow handlers (e.g. logging, scrobbling)
                                    Runnable fireStartStopEvent = new Runnable() {

                                        @Override
                                        public void run() {
                                            try {
                                                ((StartStopListener) listener).donePlaying(media, self);
                                            } catch (Throwable t) {
                                                LOGGER.error("Notification of donePlaying event failed for StartStopListener {}", listener.getClass(), t);
                                            }
                                        }
                                    };
                                    new Thread(fireStartStopEvent, "StopPlaying Event for " + listener.name()).start();
                                }
                            }
                        }
                    }
                };
                new Thread(r, "StopPlaying Event").start();
            }
        }
    };
    new Thread(defer, "StopPlaying Event Deferrer").start();
}
Also used : UnknownHostException(java.net.UnknownHostException) StartStopListener(net.pms.external.StartStopListener) RendererConfiguration(net.pms.configuration.RendererConfiguration) ExternalListener(net.pms.external.ExternalListener) InetAddress(java.net.InetAddress)

Aggregations

InetAddress (java.net.InetAddress)2 UnknownHostException (java.net.UnknownHostException)2 RendererConfiguration (net.pms.configuration.RendererConfiguration)2 ExternalListener (net.pms.external.ExternalListener)2 StartStopListener (net.pms.external.StartStopListener)2