/**********
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; either version 2.1 of the License, or (at your
option) any later version. (See <http://www.gnu.org/copyleft/lesser.html>.)

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
**********/
/* 
 * Written by Engin Kurutepe.
 * kurutepe@nue.tu-berlin.de
 */

#include "H264VideoFileServerMediaSubsession.h"
#include "H264RTPStreamFramer.h"
#include "H264VideoRTPSink.hh"

H264VideoFileServerMediaSubsession
	::H264VideoFileServerMediaSubsession( UsageEnvironment& env,
										  char const* fileName, Boolean reuseFirstSource)
	: FileServerMediaSubsession(env, fileName, reuseFirstSource), fDoneFlag(0)
{
}

H264VideoFileServerMediaSubsession*
H264VideoFileServerMediaSubsession::createNew(UsageEnvironment& env,
					       char const* fileName,
					       Boolean reuseFirstSource) {
  return new H264VideoFileServerMediaSubsession(env, fileName, reuseFirstSource);
}

H264VideoFileServerMediaSubsession::~H264VideoFileServerMediaSubsession(void)
{
}

static void afterPlayingDummy(void* clientData) {
  H264VideoFileServerMediaSubsession* subsess
    = (H264VideoFileServerMediaSubsession*)clientData;
  subsess->afterPlayingDummy1();
}

void H264VideoFileServerMediaSubsession::afterPlayingDummy1() {
  // Unschedule any pending 'checking' task:
  envir().taskScheduler().unscheduleDelayedTask(nextTask());
  // Signal the event loop that we're done:
  setDoneFlag();
}

static void checkForAuxSDPLine(void* clientData) {
  H264VideoFileServerMediaSubsession* subsess
    = (H264VideoFileServerMediaSubsession*)clientData;
  subsess->checkForAuxSDPLine1();
}

void H264VideoFileServerMediaSubsession::checkForAuxSDPLine1() {
  if (fDummyRTPSink->auxSDPLine() != NULL) {
    // Signal the event loop that we're done:
    setDoneFlag();
  } else {
    // try again after a brief delay:
    int uSecsToDelay = 100000; // 100 ms
    nextTask() = envir().taskScheduler().scheduleDelayedTask(uSecsToDelay,
			      (TaskFunc*)checkForAuxSDPLine, this);
  }
}

char const* H264VideoFileServerMediaSubsession
::getAuxSDPLine(RTPSink* rtpSink, FramedSource* inputSource) {
  // Note: For H264 video files, the 'config' information isn't known
  // until we start reading the file.  This means that "rtpSink"s
  // "auxSDPLine()" will be NULL initially, and we need to start reading
  // data from our file until this changes.
  fDummyRTPSink = rtpSink;
  fDummySource = inputSource;
    
  // Start reading the file:
  fDummyRTPSink->startPlaying(*inputSource, afterPlayingDummy, this);
    
  // Check whether the sink's 'auxSDPLine()' is ready:
  checkForAuxSDPLine(this);
    
  envir().taskScheduler().doEventLoop(&fDoneFlag);
    
  char const* auxSDPLine = fDummyRTPSink->auxSDPLine();
  return auxSDPLine;
}

FramedSource* H264VideoFileServerMediaSubsession
::createNewStreamSource(unsigned /*clientSessionId*/, unsigned& estBitrate) 
{
  estBitrate = 500; // kbps, estimate

  // Create the video source:
  ByteStreamFileSource* fileSource
    = ByteStreamFileSource::createNew(envir(), fFileName);
  if (fileSource == NULL) return NULL;
  fFileSize = fileSource->fileSize();

  // Create a framer for the Video Elementary Stream:
  H264RTPStreamFramer *framer = H264RTPStreamFramer::createNew(envir(), fileSource);
  //source_sps = framer->getSPS();
  //source_pps = framer->getPPS();
  //source_pli = framer->getProfileLevelID();

  return framer;
  
}

RTPSink* H264VideoFileServerMediaSubsession
::createNewRTPSink( Groupsock* rtpGroupsock,
					unsigned char rtpPayloadTypeIfDynamic,
					FramedSource* /*inputSource*/) 
{
	//char *param_sets = new char[strlen(source_sps)+strlen(source_pps)+2];
	//
	//strcpy(param_sets, source_sps);
	//strcat(param_sets, ",");
	//strcat(param_sets, source_pps);

	H264VideoRTPSink *sink = H264VideoRTPSink::createNew( envir(), 
														  rtpGroupsock,
														  rtpPayloadTypeIfDynamic,
														  0,
														  "" );

//	delete [] param_sets;

	return sink;
}