/*
 * audio_player.h: The audio player and control interface
 *
 * See the README file for copyright information and how to reach the author.
 *
 * $Id: audio_player.h,v 1.15 2005/10/17 12:00:33 cm Exp $
 */

#ifndef __AUDIOPLAYER_H
#define __AUDIOPLAYER_H

#include <stdint.h>
#include <vdr/player.h>
#include "audio_playlist.h"
#include "audio_decoder.h"
#include "daap_client.h"

#define PLAYERBUFSIZE	         MEGABYTE(1)
#define MAX_FRAME_PAYLOAD      2000

#define MSECONDSTOFRAMES(a)      ((a) / 40)
#define FRAMESTOSECONDS(a)       ((a) / 25)

class cAudioPlayer : public cPlayer, public cThread {
  private:
    int m_curFrame;
    int m_maxFrames;
    enum ePlayMode { pmPlay=0, pmPaused, pmStopped, pmBuffered, pmNone };
    ePlayMode m_playMode;
    cFrame *mpo_readFrame, *mpo_playFrame;
    cRingBufferFrame *mpo_ringBufferFrame;
    cCondVar mo_playerCond;
    cMutex mo_playerMutex;
    cAudioDecoder *mpo_decoder;
    cAudioTunePreloader o_preloader;
    cAudioPlaylist *mpo_playlist;
    void Clear(void);
    void Reset(void);
    void SetPlayMode(ePlayMode mode);
    void WaitPlayMode(ePlayMode test, bool negate, ePlayMode wait);
  protected:
    void Action();
    void Activate(bool On);
  public:
    cAudioPlayer(cAudioPlaylist *playlist);
    ~cAudioPlayer();                                     
    void Play(void);
    void Pause(void);
    void Stop(void);
    void Jump(int number);
    void SkipTrack(int offset);
    void SkipSeconds(int seconds);
    bool GetIndex(int &Current, int &Total, bool SnapToIFrame = false);;
    bool GetReplayMode(bool &Play, bool &Forward, int &Speed);
};

class cAudioControl : public cControl {
  private:
    char *mp_replayDescription;
    int m_curSongId;
    cAudioPlayer *player;
    cDaapPlaylist *mpo_playlist;
    cSkinDisplayReplay *mpo_mnuReplay;
    cSkinDisplayMenu *mpo_mnuPlaylist;
    void RefreshProgress(int number);
    void ShowReplay(void);
    void ShowPlaylist(bool message = false);
  public:
    cAudioControl(cDaapPlaylist *playlist);
    ~cAudioControl();
    void Hide(void);
    eOSState ProcessKey(eKeys Key);
};

#endif
