// A filter that passes through (unchanged) chunks that contain an integral number
// of MPEG-2 Transport Stream packets, but returning (in "fDurationInMicroseconds")
// an updated estimate of the time gap between chunks.
// C++ header

#ifndef _MPEG2_TRANSPORT_STREAM_FRAMER_SIMPLE_HH
#define _MPEG2_TRANSPORT_STREAM_FRAMER_SIMPLE_HH

#ifndef _FRAMED_FILTER_HH
#include "FramedFilter.hh"
#endif

#define TRANSPORT_PACKET_SIZE 188

class MPEG2TransportStreamFramerSimple: public FramedFilter {
public:
  static MPEG2TransportStreamFramerSimple*
  // We set maxFrameSize to be able send it in 1500B lenght UDP packet 7*188 = 1316
  createNew(UsageEnvironment& env, FramedSource* inputSource, unsigned maxFrameSize = 1316);

protected:
  MPEG2TransportStreamFramerSimple(UsageEnvironment& env, FramedSource* inputSource, unsigned maxFrameSize);
      // called only by createNew()
  virtual ~MPEG2TransportStreamFramerSimple();

private:
  // Redefined virtual functions:
  virtual void doGetNextFrame();
  virtual void doStopGettingFrames();

private:
  static void afterGettingFrame(void* clientData, unsigned frameSize,
				unsigned numTruncatedBytes,
				struct timeval presentationTime,
				unsigned durationInMicroseconds);
  void afterGettingFrame1(unsigned frameSize,
			  struct timeval presentationTime);
  
  unsigned fMaxFrameSize;
  unsigned char fIncompleteTSPacketSize;
  // for good reason there might be stored whole TS packet!
  unsigned char fIncompleteTSPacketBuffer[TRANSPORT_PACKET_SIZE];

  // How much times we call getNextFrame if buffer is not full
  // the goal is break infinite loop if does not comein valid TS data
  unsigned fNumGetNextFrame;
  // if fNumGetNextFrame overflows, print error, but print it only once
  // (until does come invalid stream)
  bool fNumGetNextFrameErrPrintOnce;
};

#endif
