/* Network-Integrated Multimedia Middleware (NMM)
 * http://www.motama.com/nmm.html
 *  
 * Copyright (C) 2006-2007
 * Motama GmbH, Saarbruecken, Germany
 * http://www.motama.com
 *  
 * This file is part of NMM. 
 * 
 * NMM is free software; you can redistribute it and/or modify it    
 * under the terms of the GNU General Public License as published by 
 * the Free Software Foundation; only version 2 of the License.      
 * 
 * NMM is distributed in the hope that it will be useful, but WITHOUT 
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public 
 * License for more details.  
 * 
 * You should have received a copy of the GNU General Public License 
 * along with NMM; if not, write to the Free Software Foundation,    
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 */

#ifndef NMM_DVDNAVREADNODE_HPP
#define NMM_DVDNAVREADNODE_HPP NMM_DVDNAVREADNODE_HPP

#include "nmm/nmm_prefix.hpp"

#include "nmm/multimedia/GenericSourceNode.hpp"

#include "nmm/plugins/file/dvd/interfaces/IDVDImpl.hpp"
#include "nmm/plugins/file/dvd/interfaces/IDVDNavImpl.hpp"
#include "nmm/plugins/interfaces/IProgressImpl.hpp"

#include <dvdnav/dvdnav.h>

namespace NMM {

  class DVDNavReadNode :
    public GenericSourceNode,
    public IDVDImpl,
    public IDVDNavImpl,
    public IProgressImpl
  {
  private:
    enum domain_e {DOMAIN_UNKNOWN=0, DOMAIN_FP, DOMAIN_VMGM, DOMAIN_VTSM, DOMAIN_VTS} domain;

    string        devicename;

    dvdnav_t*     dvdnav;

    ThreadMutex   process_buffer_mutex;
    bool          sendevent;
    bool          send_sync_reset_ok;
    bool          send_flush;
    bool          need_buttons;
    unsigned int  selected_button;
    bool          send_selected_button;
    unsigned int  sec_sleep;
    bool          is_still_frame;
    int32_t       dvd_event;
    bool          last_event;
    Buffer*       saved_buffer;

    bool          send_progress;
    unsigned int  buffers_before_progress;
    unsigned int  buffers_progress;
    bool          dvdnav_wait;  // used to indicate that dvdnav is waiting to be told that the (NMM) buffer is empty

    domain_e      checkDomain();

    Result CheckFormat(bool&   mpeg_audio,
		       bool&   ac3_audio,
		       bool&   mpeg2_video,
		       bool&   mpeg1_video,
		       int&    width,
		       int&    height,
		       int&    frame_rate_code,
		       double& frame_rate,
		       int&    ratio_x,
		       int&    ratio_y);

    Result getInfo( int* title, int* chapter ) const;

  protected:
    virtual Result doInit();
    virtual Result doInitOutput();
    virtual Result doDeinitOutput();

    virtual Message* processBuffer(Buffer *);

  public:
    DVDNavReadNode(const char * name = "DVDNavReadNode",
                   StreamQueue::Mode = StreamQueue::MODE_SUSPEND,
                   int = StreamQueue::DEFAULT_SIZE);

    virtual ~DVDNavReadNode();

    // IDevice interface
    virtual void setDevice(const string& value) throw();
    virtual string getDevice() const;

    /**
     *  @name IDVD Interface
     */
    //@{
    /**
     *  @brief Select chapter, title and view angle
     *  @param title Title to play
     *  @param chapter Chapter to play
     *  @param angle View angle to play
     *  @return SUCCESS if the method succeeds, otherwise FAILURE
     *
     *  This method sets the @a title, @a chapter and view @a angle to play. The title must be between 1 and getTitles()
     *  inclusive, the chapter must be between 1 and getChapters(title) inclusive and the view angle must be between
     *  1 and getAngles(title) inclusive. Otherwise the method fails.
     */
    virtual Result select(const nmm_uint32& title, const nmm_uint32& chapter, const nmm_uint32& angle);

    /**
     *  @brief Get current title
     *  @return The current title number
     *
     *  This method returns the number of the title currently being played. If no title is being played, or the
     *  current title is invalid, the result is zero.
     */
    virtual nmm_uint32 getCurrentTitle() const;

    /**
     *  @brief Get current chapter
     *  @return The current chapternumber
     *
     *  This method returns the number of the chapter currently being played. If no chapter is being played, or the
     *  current chapter is invalid, the result is zero.
     */
    virtual nmm_uint32 getCurrentChapter() const;

    /**
     *  @brief Get number of titles
     *  @return The number of titles on the DVD
     *
     *  This method returns the number of titles on the DVD, which is also the highest possible title number.
     */
    virtual nmm_uint32 getTitles() const;

    /**
     *  @brief Get number of chapters in a title
     *  @param title Title number
     *  @return The number of chapters in the title
     *
     *  @bug The method fails if @a title is not the current title
     *
     *  This method returns the number of chapters in the given @a title, which is also the highest possible chapter
     *  number for that title. The title number must be between 1 and getTitles() inclusive.
     */
    virtual nmm_uint32 getChapters(const nmm_uint32& title) const;

    /**
     *  @brief Get number of view angles in a title
     *  @param title Title number
     *  @return The number of angles in the title
     *
     *  @bug The method fails if @a title is not the current title
     *
     *  This method returns the number of angles in the given @a title, which is also the highest possible angle
     *  number for that title. The title number must be between 1 and getTitles() inclusive.
     */
    virtual nmm_uint32 getAngles(const nmm_uint32& title) const;

    /**
     *  @brief Send a flush event
     *
     *  This method causes the ndoe to send a flush event.
     *  Currently, applications using the DVDNavReadNode do this explicitly before changing the chapter.
     *  If this is really necessary, the node should do it itself, but it has to be tested.
     */
    virtual void sendFlush();

    /**
     *  @brief Reset the DVD
     *
     *  Reset the DVD to the beginning.
     */
    virtual void reset();
    //@}

    // IDVDNavInterface
    virtual Result selectButton(const nmm_int32& button);
    virtual Result activateButton(const nmm_int32& button);
    virtual Result activateSelectedButton();
    virtual Result goUp();
    virtual Result buttonUp();
    virtual Result buttonDown();
    virtual Result buttonRight();
    virtual Result buttonLeft();
    virtual nmm_int32 getSelectedButton();

    // IProgress interface
    virtual void sendProgressInformation(const nmm_bool&);
    virtual void setProgressInterval(const nmm_int32& buffers);
  };

}

#include "nmm/nmm_suffix.hpp"

#endif //NMM_DVDNAVREADNODE_HPP
