Blame src/libbluray/bdj/java/org/videolan/media/content/playlist/DVBMediaSelectControlImpl.java

Packit 5e46da
/*
Packit 5e46da
 * This file is part of libbluray
Packit 5e46da
 * Copyright (C) 2010  William Hahne
Packit 5e46da
 *
Packit 5e46da
 * This library is free software; you can redistribute it and/or
Packit 5e46da
 * modify it under the terms of the GNU Lesser General Public
Packit 5e46da
 * License as published by the Free Software Foundation; either
Packit 5e46da
 * version 2.1 of the License, or (at your option) any later version.
Packit 5e46da
 *
Packit 5e46da
 * This library is distributed in the hope that it will be useful,
Packit 5e46da
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 5e46da
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 5e46da
 * Lesser General Public License for more details.
Packit 5e46da
 *
Packit 5e46da
 * You should have received a copy of the GNU Lesser General Public
Packit 5e46da
 * License along with this library. If not, see
Packit 5e46da
 * <http://www.gnu.org/licenses/>.
Packit 5e46da
 */
Packit 5e46da
Packit 5e46da
package org.videolan.media.content.playlist;
Packit 5e46da
Packit 5e46da
import java.awt.Component;
Packit 5e46da
import java.util.LinkedList;
Packit 5e46da
Packit 5e46da
import javax.media.Control;
Packit 5e46da
import javax.tv.locator.InvalidLocatorException;
Packit 5e46da
import javax.tv.locator.Locator;
Packit 5e46da
import javax.tv.media.MediaSelectFailedEvent;
Packit 5e46da
import javax.tv.media.MediaSelectSucceededEvent;
Packit 5e46da
import javax.tv.media.MediaSelectListener;
Packit 5e46da
import javax.tv.service.selection.InsufficientResourcesException;
Packit 5e46da
import javax.tv.service.selection.InvalidServiceComponentException;
Packit 5e46da
Packit 5e46da
import org.bluray.media.AsynchronousPiPControl;
Packit 5e46da
import org.bluray.net.BDLocator;
Packit 5e46da
import org.dvb.media.DVBMediaSelectControl;
Packit 5e46da
import org.videolan.BDJListeners;
Packit 5e46da
import org.videolan.Logger;
Packit 5e46da
import org.videolan.PlaylistInfo;
Packit 5e46da
import org.videolan.TIClip;
Packit 5e46da
Packit 5e46da
public class DVBMediaSelectControlImpl implements DVBMediaSelectControl {
Packit 5e46da
    protected DVBMediaSelectControlImpl(Handler player) {
Packit 5e46da
        this.player = player;
Packit 5e46da
    }
Packit 5e46da
Packit 5e46da
    public void add(Locator component)
Packit 5e46da
                throws InvalidLocatorException, InvalidServiceComponentException,
Packit 5e46da
                InsufficientResourcesException, SecurityException {
Packit 5e46da
        if (!checkLocator(component))
Packit 5e46da
            throw new InvalidLocatorException(component);
Packit 5e46da
        BDLocator locator = (BDLocator)component;
Packit 5e46da
        if (locator.getSecondaryVideoStreamNumber() <= 0)
Packit 5e46da
            throw new InvalidServiceComponentException(component);
Packit 5e46da
        AsynchronousPiPControl control = (AsynchronousPiPControl)
Packit 5e46da
            player.getControl("org.bluray.media.AsynchronousPiPControl");
Packit 5e46da
        if (control.getCurrentStreamNumber() > 0)
Packit 5e46da
            throw new InsufficientResourcesException();
Packit 5e46da
        try {
Packit 5e46da
            control.selectStreamNumber(locator.getSecondaryVideoStreamNumber());
Packit 5e46da
        } catch (Exception e) {
Packit 5e46da
            System.err.println("" + e + "\n" + Logger.dumpStack(e));
Packit 5e46da
            postMediaSelectFailedEvent(new Locator[] { component });
Packit 5e46da
        }
Packit 5e46da
    }
Packit 5e46da
Packit 5e46da
    public void remove(Locator component)
Packit 5e46da
                throws InvalidLocatorException, InvalidServiceComponentException,
Packit 5e46da
                SecurityException {
Packit 5e46da
        if (!checkLocator(component))
Packit 5e46da
            throw new InvalidLocatorException(component);
Packit 5e46da
        BDLocator locator = (BDLocator)component;
Packit 5e46da
        if (locator.getSecondaryVideoStreamNumber() <= 0)
Packit 5e46da
            throw new InvalidServiceComponentException(component);
Packit 5e46da
        AsynchronousPiPControl control = (AsynchronousPiPControl)
Packit 5e46da
            player.getControl("org.bluray.media.AsynchronousPiPControl");
Packit 5e46da
        if (control.getCurrentStreamNumber() != locator.getSecondaryVideoStreamNumber())
Packit 5e46da
            throw new InvalidLocatorException(component);
Packit 5e46da
        try {
Packit 5e46da
            control.stop();
Packit 5e46da
        } catch (Exception e) {
Packit 5e46da
            System.err.println("" + e + "\n" + Logger.dumpStack(e));
Packit 5e46da
            postMediaSelectFailedEvent(new Locator[] { component });
Packit 5e46da
        }
Packit 5e46da
    }
Packit 5e46da
Packit 5e46da
    public void select(Locator component)
Packit 5e46da
                throws InvalidLocatorException, InvalidServiceComponentException,
Packit 5e46da
            InsufficientResourcesException, SecurityException {
Packit 5e46da
        if (!checkLocator(component))
Packit 5e46da
            throw new InvalidLocatorException(component);
Packit 5e46da
        Control control = getControl((BDLocator)component);
Packit 5e46da
        if (control == null)
Packit 5e46da
            throw new InvalidLocatorException(component);
Packit 5e46da
        if (!(control instanceof StreamControl))
Packit 5e46da
            return;
Packit 5e46da
        int stream = getStream((BDLocator)component);
Packit 5e46da
        if (stream <= 0)
Packit 5e46da
            throw new InvalidLocatorException(component);
Packit 5e46da
        try {
Packit 5e46da
            ((StreamControl)control).selectStreamNumber(stream);
Packit 5e46da
        } catch (Exception e) {
Packit 5e46da
            postMediaSelectFailedEvent(new Locator[] { component });
Packit 5e46da
            throw new InvalidLocatorException(component);
Packit 5e46da
        }
Packit 5e46da
    }
Packit 5e46da
Packit 5e46da
    public void select(Locator[] components)
Packit 5e46da
                throws InvalidLocatorException, InvalidServiceComponentException,
Packit 5e46da
                InsufficientResourcesException, SecurityException {
Packit 5e46da
        for (int i = 0; i < components.length; i++)
Packit 5e46da
            select(components[i]);
Packit 5e46da
    }
Packit 5e46da
Packit 5e46da
    public void replace(Locator fromComponent, Locator toComponent)
Packit 5e46da
               throws InvalidLocatorException, InvalidServiceComponentException,
Packit 5e46da
               InsufficientResourcesException, SecurityException {
Packit 5e46da
        if (!checkLocator(fromComponent))
Packit 5e46da
            throw new InvalidLocatorException(fromComponent);
Packit 5e46da
        if (!checkLocator(toComponent))
Packit 5e46da
            throw new InvalidLocatorException(toComponent);
Packit 5e46da
        Control fromControl = getControl((BDLocator)fromComponent);
Packit 5e46da
        if (fromControl == null)
Packit 5e46da
            throw new InvalidLocatorException(fromComponent);
Packit 5e46da
        Control toControl = getControl((BDLocator)toComponent);
Packit 5e46da
        if (toControl == null)
Packit 5e46da
            throw new InvalidLocatorException(toComponent);
Packit 5e46da
        if (fromControl != toControl)
Packit 5e46da
            throw new InvalidLocatorException(fromComponent);
Packit 5e46da
        int fromStream = getStream((BDLocator)fromComponent);
Packit 5e46da
        if ((fromStream <= 0) || (fromStream != ((StreamControl)fromControl).getCurrentStreamNumber()))
Packit 5e46da
            throw new InvalidLocatorException(fromComponent);
Packit 5e46da
        int toStream = getStream((BDLocator)toComponent);
Packit 5e46da
        if (toStream <= 0)
Packit 5e46da
            throw new InvalidLocatorException(toComponent);
Packit 5e46da
        try {
Packit 5e46da
            ((StreamControl)fromControl).selectStreamNumber(toStream);
Packit 5e46da
        } catch (Exception e) {
Packit 5e46da
            postMediaSelectFailedEvent(new Locator[] { toComponent });
Packit 5e46da
            throw new InvalidLocatorException(toComponent);
Packit 5e46da
        }
Packit 5e46da
    }
Packit 5e46da
Packit 5e46da
    public void addMediaSelectListener(MediaSelectListener listener) {
Packit 5e46da
        listeners.add(listener);
Packit 5e46da
    }
Packit 5e46da
Packit 5e46da
    public void removeMediaSelectListener(MediaSelectListener listener) {
Packit 5e46da
        listeners.remove(listener);
Packit 5e46da
    }
Packit 5e46da
Packit 5e46da
    private void postMediaSelectFailedEvent(Locator[] selection) {
Packit 5e46da
        listeners.putCallback(new MediaSelectFailedEvent(player, selection));
Packit 5e46da
    }
Packit 5e46da
Packit 5e46da
    protected void postMediaSelectSucceededEvent(Locator[] selection) {
Packit 5e46da
        listeners.putCallback(new MediaSelectSucceededEvent(player, selection));
Packit 5e46da
    }
Packit 5e46da
Packit 5e46da
    public Locator[] getCurrentSelection() {
Packit 5e46da
        BDLocator locator = player.getCurrentLocator();
Packit 5e46da
        if (locator == null)
Packit 5e46da
            return new Locator[0];
Packit 5e46da
        String[] tags = locator.getComponentTags();
Packit 5e46da
        if (tags.length <= 0)
Packit 5e46da
            return new Locator[0];
Packit 5e46da
        Locator[] selections = new Locator[tags.length];
Packit 5e46da
        String[] tag = new String[1];
Packit 5e46da
        for (int i = 0; i < tags.length; i++) {
Packit 5e46da
            tag[0] = tags[i];
Packit 5e46da
            try {
Packit 5e46da
                selections[i] = new BDLocator(
Packit 5e46da
                                              locator.getDiscId(),
Packit 5e46da
                                              locator.getTitleNumber(),
Packit 5e46da
                                              locator.getPlayListId(),
Packit 5e46da
                                              locator.getPlayItemId(),
Packit 5e46da
                                              locator.getMarkId(),
Packit 5e46da
                                              tag);
Packit 5e46da
            } catch (Exception e) {
Packit 5e46da
                e.printStackTrace();
Packit 5e46da
                return new Locator[0];
Packit 5e46da
            }
Packit 5e46da
        }
Packit 5e46da
        return selections;
Packit 5e46da
    }
Packit 5e46da
Packit 5e46da
    public void selectServiceMediaComponents(Locator locator)
Packit 5e46da
            throws InvalidLocatorException, InvalidServiceComponentException,
Packit 5e46da
            InsufficientResourcesException {
Packit 5e46da
        if (!(locator instanceof BDLocator))
Packit 5e46da
            throw new InvalidLocatorException(locator);
Packit 5e46da
        PlaylistInfo pi = player.getPlaylistInfo();
Packit 5e46da
        if (pi == null)
Packit 5e46da
            throw new InsufficientResourcesException();
Packit 5e46da
        if (((BDLocator)locator).getPlayListId() != pi.getPlaylist())
Packit 5e46da
            throw new InvalidLocatorException(locator);
Packit 5e46da
        TIClip ci = player.getCurrentClipInfo();
Packit 5e46da
        if (ci == null)
Packit 5e46da
            throw new InsufficientResourcesException();
Packit 5e46da
        String[] tags = ((BDLocator)locator).getComponentTags();
Packit 5e46da
        String[] tag = new String[1];
Packit 5e46da
        for (int i = 0; i < tags.length; i++) {
Packit 5e46da
            tag[0] = tags[i];
Packit 5e46da
            try {
Packit 5e46da
                select(new BDLocator(null, -1, pi.getPlaylist(), ci.getIndex(), -1, tag));
Packit 5e46da
            } catch (org.davic.net.InvalidLocatorException e) {
Packit 5e46da
                e.printStackTrace();
Packit 5e46da
                throw new InvalidLocatorException(locator);
Packit 5e46da
            } catch (SecurityException e) {
Packit 5e46da
                e.printStackTrace();
Packit 5e46da
                throw new InsufficientResourcesException();
Packit 5e46da
            }
Packit 5e46da
        }
Packit 5e46da
    }
Packit 5e46da
Packit 5e46da
    public Component getControlComponent() {
Packit 5e46da
        return null;
Packit 5e46da
    }
Packit 5e46da
Packit 5e46da
    private boolean checkLocator(Locator locator) {
Packit 5e46da
        if ((locator == null) || !(locator instanceof BDLocator))
Packit 5e46da
            return false;
Packit 5e46da
        PlaylistInfo pi = player.getPlaylistInfo();
Packit 5e46da
        if (pi == null)
Packit 5e46da
            return false;
Packit 5e46da
        BDLocator bdLocator = (BDLocator)locator;
Packit 5e46da
        if (bdLocator.getComponentTagsCount() != 1)
Packit 5e46da
            return false;
Packit 5e46da
        if (bdLocator.getPlayListId() != -1 && bdLocator.getPlayListId() != pi.getPlaylist())
Packit 5e46da
            return false;
Packit 5e46da
        TIClip ci = player.getCurrentClipInfo();
Packit 5e46da
        if (ci == null)
Packit 5e46da
            return false;
Packit 5e46da
        return (bdLocator.getPlayItemId() == -1) || (bdLocator.getPlayItemId() == ci.getIndex());
Packit 5e46da
    }
Packit 5e46da
Packit 5e46da
    private int getStream(BDLocator locator) {
Packit 5e46da
        if (locator.getPrimaryVideoStreamNumber() > 0)
Packit 5e46da
            return locator.getPrimaryVideoStreamNumber();
Packit 5e46da
        else if (locator.getPrimaryAudioStreamNumber() > 0)
Packit 5e46da
            return locator.getPrimaryAudioStreamNumber();
Packit 5e46da
        else if (locator.getSecondaryVideoStreamNumber() > 0)
Packit 5e46da
            return locator.getSecondaryVideoStreamNumber();
Packit 5e46da
        else if (locator.getSecondaryAudioStreamNumber() > 0)
Packit 5e46da
            return locator.getSecondaryAudioStreamNumber();
Packit 5e46da
        else if (locator.getPGTextStreamNumber() > 0)
Packit 5e46da
            return locator.getPGTextStreamNumber();
Packit 5e46da
        return -1;
Packit 5e46da
    }
Packit 5e46da
Packit 5e46da
    private Control getControl(BDLocator locator) {
Packit 5e46da
        if (locator.getPrimaryVideoStreamNumber() > 0)
Packit 5e46da
            return player.getControl("org.bluray.media.BackgroundVideoPresentationControl");
Packit 5e46da
        else if (locator.getPrimaryAudioStreamNumber() > 0)
Packit 5e46da
            return player.getControl("org.bluray.media.PrimaryAudioControl");
Packit 5e46da
        else if (locator.getSecondaryVideoStreamNumber() > 0)
Packit 5e46da
            return player.getControl("org.bluray.media.AsynchronousPiPControl");
Packit 5e46da
        else if (locator.getSecondaryAudioStreamNumber() > 0)
Packit 5e46da
            return player.getControl("org.bluray.media.SecondaryAudioControl");
Packit 5e46da
        else if (locator.getPGTextStreamNumber() > 0)
Packit 5e46da
            return player.getControl("org.bluray.media.SubtitlingControl");
Packit 5e46da
        return null;
Packit 5e46da
    }
Packit 5e46da
Packit 5e46da
    private BDJListeners listeners = new BDJListeners();
Packit 5e46da
    private Handler player;
Packit 5e46da
}