/*****************************************************************************/
/* Copyright (c) 2009 NXP B.V.                                               */
/*                                                                           */
/* This library is free software; you can redistribute it and/or             */
/* modify it under the terms of the GNU Lesser General Public                */
/* License as published by the Free Software Foundation, using               */
/* version 2.1 of the License.                                               */
/*                                                                           */
/* This library 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          */
/* Lesser General Public License for more details.                           */
/*                                                                           */
/* You should have received a copy of the GNU Lesser General Public          */
/* License along with this library; if not, write to the Free Software       */
/* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA   */
/*                                                                           */
/*****************************************************************************/
/*

Rev Date        Author      Comments
--------------------------------------------------------------------------------
1   20090205    laird       First Version
2   20090326    laird       Adding mute.
--------------------------------------------------------------------------------
    For consistency and standardisation retain the Section Separators.
*/
#ifndef __GST_STB22X_FRAMED_AUDIO_H__
#define __GST_STB22X_FRAMED_AUDIO_H__

/**************************************************************
* INCLUDE FILES                                               *
***************************************************************/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <sys/stat.h>
#include <gst/gst.h>
#include <gst/video/gstvideosink.h>
#include <tmNxCompId.h>
#include <phStbEsMetaData.h>

/**************************************************************
* EXPORTED MACROS                                             *
***************************************************************/
#define GST_TYPE_STB22X_FRAMED_AUDIO              (gst_stb22x_framed_audio_get_type())
#define GST_STB22X_FRAMED_AUDIO(obj)              (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_STB22X_FRAMED_AUDIO,GstStb22xFramedAudio))
#define GST_STB22X_FRAMED_AUDIO_CLASS(klass)      (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_STB22X_FRAMED_AUDIO,GstStb22xFramedAudioClass))
#define GST_IS_STB22X_FRAMED_AUDIO(obj)           (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_STB22X_FRAMED_AUDIO))
#define GST_IS_STB22X_FRAMED_AUDIO_CLASS(klass)   (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_STB22X_FRAMED_AUDIO))
#define GST_STB22X_FRAMED_AUDIO_CAST(obj)         ((GstStb22xFramedAudio *)obj)
#define GST_STB22X_FRAMED_AUDIO_FILE_NAME_LENGTH  255

/** Utility macros to convert from GST timestamps into the PTS timestamp
    format required for ES meta Data API.*/
#define CLOCK_BASE 9LL
#define CLOCK_FREQ (CLOCK_BASE * 10000)
#define MPEGTIME_TO_GSTTIME(time) (gst_util_uint64_scale ((time), \
            GST_MSECOND/10, CLOCK_BASE))
#define GSTTIME_TO_MPEGTIME(time) (gst_util_uint64_scale ((time), \
            CLOCK_BASE, GST_MSECOND/10))

/**************************************************************
* EXPORTED TYPEDEFS                                           *
***************************************************************/
/**
 * All the Codecs that can be played via the ES API.
 */
typedef enum
{
    GstStb22xFramedAudio_Ac3,
    GstStb22xFramedAudio_BurstDTS,
    GstStb22xFramedAudio_Lpcm,
    GstStb22xFramedAudio_Mp3,
    GstStb22xFramedAudio_Mpeg,
    GstStb22xFramedAudio_Mpeg4Aac,
    GstStb22xFramedAudio_Wma,
    GstStb22xFramedAudio_WmaPro,
    GstStb22xFramedAudio_Guard
} GstStb22xFramedAudioSupportedTypes;

/** Macro to convert from DirectFB status to internal status.*/
#define CONVERT_TO_METADATA_AUDIOTYPE(_gstAudioType)                                 \
    (((_gstAudioType) == GstStb22xFramedAudio_Ac3)      ? phStbEsFormatAudioAc3:     \
     ((_gstAudioType) == GstStb22xFramedAudio_BurstDTS) ? phStbEsFormatAudioBurstDTS:\
     ((_gstAudioType) == GstStb22xFramedAudio_Lpcm)     ? phStbEsFormatAudioLpcm:    \
     ((_gstAudioType) == GstStb22xFramedAudio_Mp3)      ? phStbEsFormatAudioMp3:     \
     ((_gstAudioType) == GstStb22xFramedAudio_Mpeg)     ? phStbEsFormatAudioMpeg:    \
     ((_gstAudioType) == GstStb22xFramedAudio_Mpeg4Aac) ? phStbEsFormatAudioMpeg4Aac:\
     ((_gstAudioType) == GstStb22xFramedAudio_Wma)      ? phStbEsFormatAudioWma:     \
     ((_gstAudioType) == GstStb22xFramedAudio_WmaPro)   ? phStbEsFormatAudioWmaPro:  \
     phStbEsFormatNone)

/**
 * GstStb22xFramedAudioStateError:
 * @STB22X_FRAMED_AUDIO_STATE_ERROR_NONE: no error
 * @STB22X_FRAMED_AUDIO_STATE_ERROR_NULL_READY: cause the NULL to READY state change to fail
 * @STB22X_FRAMED_AUDIO_STATE_ERROR_READY_PAUSED: cause the READY to PAUSED state change to fail:
 * @STB22X_FRAMED_AUDIO_STATE_ERROR_PAUSED_PLAYING: cause the PAUSED to PLAYING state change to fail:
 * @STB22X_FRAMED_AUDIO_STATE_ERROR_PLAYING_PAUSED: cause the PLAYING to PAUSED state change to fail:
 * @STB22X_FRAMED_AUDIO_STATE_ERROR_PAUSED_READY: cause the PAUSED to READY state change to fail:
 * @STB22X_FRAMED_AUDIO_STATE_ERROR_READY_NULL: cause the READY to NULL state change to fail:
 *
 * Possible state change errors for the state-error property.
 */
typedef enum
{
    STB22X_FRAMED_AUDIO_STATE_ERROR_NONE = 0,
    STB22X_FRAMED_AUDIO_STATE_ERROR_NULL_READY,
    STB22X_FRAMED_AUDIO_STATE_ERROR_READY_PAUSED,
    STB22X_FRAMED_AUDIO_STATE_ERROR_PAUSED_PLAYING,
    STB22X_FRAMED_AUDIO_STATE_ERROR_PLAYING_PAUSED,
    STB22X_FRAMED_AUDIO_STATE_ERROR_PAUSED_READY,
    STB22X_FRAMED_AUDIO_STATE_ERROR_READY_NULL
} GstStb22xFramedAudioStateError;

typedef struct _GstStb22xFramedAudio GstStb22xFramedAudio;
typedef struct _GstStb22xFramedAudioClass GstStb22xFramedAudioClass;

/**
 * GstStb22xFramedAudio:
 *
 * The opaque #GstStb22xFramedAudio data structure.
 */
struct _GstStb22xFramedAudio {
  GstVideoSink		element;
  /** The currently selected codec. */
  GstStb22xFramedAudioSupportedTypes currentCodec;
  /** Are we dumping the output to a file as well? */
  gboolean		dump;
  /** Write data to sink or not.*/
  gboolean		mute;
  /** Output Filename.*/
  gchar  dump_filename[GST_STB22X_FRAMED_AUDIO_FILE_NAME_LENGTH];
   /** File handle for sink*/
  gint    dump_fd;
  gboolean		signal_handoffs;
  GstStb22xFramedAudioStateError state_error;
  /** Sbm sink device name. (including /dev/)*/
  gchar fullDeviceName[GST_STB22X_FRAMED_AUDIO_FILE_NAME_LENGTH];
  /** File handle for sink*/
  gint    fd;
  /** The meta data we need to write before supplying the data.*/
  phStbEsMetaData_t metaData;
  GstBuffer* audioDSI;
  gboolean audioDSISent;
};

struct _GstStb22xFramedAudioClass {
  GstVideoSinkClass parent_class;
  /** */
  GstPadTemplate *sink_template;
  /* signals */
  void (*handoff) (GstElement *element, GstBuffer *buf, GstPad *pad);
  void (*preroll_handoff) (GstElement *element, GstBuffer *buf, GstPad *pad);
};

/**************************************************************
* EXPORTED DATA                                               *
***************************************************************/

/**************************************************************
* EXPORTED FUNCTION PROTOTYPES                                *
***************************************************************/
GType gst_stb22x_framed_audio_get_type (void);

#endif /* __GST_STB22X_FRAMED_AUDIO_H__ */
