/*****************************************************************************
 *
 *  interfacemplayer.h -- Interface to MPlayer CLI.
 *
 *  Copyright (C) 2010 Starbuck <starbuck@labdv.com>
 *  All rights reserved.
 *
 *  This file is part of the Player module of the DVDx 4 application.
 *
 *  GNU Lesser General Public License Usage
 *  This file may be used under the terms of the GNU Lesser General
 *  Public License version 2.1 as published by the Free Software
 *  Foundation and appearing in the file LICENSE.LGPL included in the
 *  packaging of this file.  Please review the following information to
 *  ensure the GNU Lesser General Public License version 2.1 requirements
 *  will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
 *
 *  GNU General Public License Usage
 *  Alternatively, this file may be used under the terms of the GNU
 *  General Public License version 3.0 as published by the Free Software
 *  Foundation and appearing in the file LICENSE.GPL included in the
 *  packaging of this file.  Please review the following information to
 *  ensure the GNU General Public License version 3.0 requirements will be
 *  met: http://www.gnu.org/copyleft/gpl.html.
 *
 *  MPEG2 notes:
 *		  Code	Aspect Ratio			  Frame Rate
 *			0	 forbidden					forbidden
 *			1	 1:1						24000/1001 (23.976)
 *			2	 4:3						24
 *			3	 16:9						25
 *			4	 2.21:1(not used in DVD)	30000/1001 (29.97)
 *			5	 reserved					30
 *			6	 reserved					50
 *			7	 reserved					60000/1001 (59.94)
 *			8	 reserved					60
 *			9	 reserved					reserved
 *					:
 *			15	 reserved					reserved
 * 
 *****************************************************************************/

#ifndef INTERFACEMPLAYER_H
#define INTERFACEMPLAYER_H

#include "interfacemplayer_struct.h"
#include "../video/video_struct.h"
#include <QString>
#include <QStringList>
#include <QRect>

class InterfaceMplayer
{
public:
	InterfaceMplayer();
	//~InterfaceMplayer();

	void initialize(const QString &, player_engine_capability_t *, player_engine_status_t *);
	void setBinPath(const QString &);

	inline QString getBinPath() { return exec; };

	void cliCmdPlay(QString *, QStringList *, 
		int=0, int=0, int=0, int=0);					// command to play movie
	void cliCmdIdentify(QString *, QStringList *);		// command to identify movie
	void cliCmdUpdateFontCache(QString *, QStringList *);
	void cliCmdGetStatus(QString *, QStringList *);		// test command
	QString cliStop();					// command to stop playback (and end player process)
	QString cliSelectTitle(const int);		// command to select title
	QString cliSelectAudio(const int);		// command to select audio track
	QString cliSelectSubtitle(const int);	// command to select subtitle
	QString cliAvert(const QString &, const int);	// command to display OSD message
	QString cliShowProgress();			// command to display the progression bar
	QString cliVideoResolution();		// command to get video resolution
	QString cliTimeLength();			// command to get movie length
	QString cliTimePosition();			// command to get playback position
	QString cliChapter();				// command to get current chapter
	QString cliDeinterlace();			// command to get playback deinterlace property
	QString cliSpeed();					// command to get playback speed
	QString cliSeekChapter(const int);	// command to seek to the start of a chapter
	QString cliSeekPrev(const int=0);	// command to seek to previous seconds or chapter
	QString cliSeekNext(const int=0);	// command to seek to next seconds or chapter
	QString cliSeekRelative(const int);	// command to seek to a relative +/- seconds
	QString cliSeekPercent(const int);	// command to seek to a % in the movie
	QString cliSeek(const int);			// command to seek to some place (seconds) in the movie
	QString cliSetSpeedFaster();		// command to faster playback speed (x2)
	QString cliSetSpeedSlower();		// command to slow down playback speed (:2)
	QString cliSetSpeedNormal();		// command to set speed to x1
	QString cliSwitchAspectRatio(const double);	// command to switch to change aspect ratio
												// at runtime. [value] is the new aspect ratio 
												// expressed as a float (e.g. 1.77778 for 16/9).
												// There might be problems with some video filters.
	QString cliSwitchAngle(const int);	// command to switch to the angle with the ID. Cycle through the
										//         available angles if value is omitted or negative.
	QString cliSwitchPause();			// command to pause/unpause the playback

	bool parseStdoutClip(const QByteArray &, int *, video_clip_t *);
	bool parseStdoutDisk(const QByteArray &, int *, mplayer_video_disk_t *);
	bool parseStdoutRun(const QByteArray &, int *, mplayer_video_running_t *);
	void parseStderr(const QByteArray &, int *, QString *);
	void parseStatus();

	void setDvdSource(const QString &dev, const int title);
	void setFileSource(const QString &path, const bool novideo=false);

	void setSoundOn();
	void setSoundOff();
	void setVideoOn();
	void setVideoOff();
	void setSubtitleOn();
	void setSubtitleOff();

	void setTitle(const int);
	void setAudioLanguage(const QString &);
	void setAudioLanguage(const int &);
	void setSubtitleLanguage(const QString &);
	void setSubtitleLanguage(const int &);

	void setAngle(const int);
	void setChapterList(const QList<int>);

	void setExternalRenderer(const int, const int, const int);
	void setSize(const QSize);			// set picture size (0x0 to ignore)
	bool setPreferredVideoDriver(const QString &);
	void setWid(const QString &);	// set render widget window system id

	bool supportsVideoDriver(const QString &);

	bool soundOn();
	bool videoOn();
	bool subtitleOn();

private:
	int parseFpsToMpegFps(const QString &);

private:	
	QString exec;		// full qualified bin path
	player_engine_capability_t *capability;
	player_engine_status_t *status;

	QString source;		// path to movie (or cd/dvd device)
	int media;			// source media type (Player::Sources)
	int title;			// for DVD source
	bool novideo;		// for audio source

	bool soundOff;
	bool videoOff;
	bool subtitleOff;
	QString audioLang;
	int audioId;
	QString subLang;
	int subId;

	int angle;				// play this DVD angle
	QList<int> chapterList;	// restrict playback to a list of chapters

	QRect externalRenderer;
	QSize pictureSize;	// size to display
	QString preferredVideoDriver;
	QString targetWid;	// window system identifier of the target widget
		// Portable in principle, but if you use it you are probably about to do something non-portable. 
		// Be careful.
		// Note: We recommend that you do not store this value as it is likely to change during run-time.
};

#endif
