<%pre>

#include <recman.h>
#include <tntfeatures.h>

using namespace vdrlive;

off_t RecSize(cFileName &recFile)
{
  off_t recSize = 0;
  for (cUnbufferedFile *recData = recFile.Open(); recData; recData = recFile.NextFile()) {
    struct stat buf;
    if (0 == stat(recFile.Name(), &buf)) {
      recSize += buf.st_size;
      // dsyslog("live: size of recording part %s is %ld", recFile.Name(), buf.st_size);
    }
    else {
      esyslog("live: can't determine size of %s", recFile.Name());
    }
  }
  // dsyslog("live: total size of %s is %ld", recording->FileName(), recSize);
  return recSize;
}

</%pre>
<%args>
    std::string recid;
</%args>
<%session scope="global">
bool logged_in(false);
</%session>
<%cpp>
std::string recording_FileName;
bool recording_IsPesRecording = false;
{
  LOCK_RECORDINGS_READ;
  const cRecording *recording = RecordingsManager::GetByHash(recid, Recordings);
  if (recording) {
    recording_FileName = recording->FileName();
    recording_IsPesRecording = recording->IsPesRecording();
  }
}
if (!recording_FileName.empty() ) {
  cFileName recFile_l(recording_FileName.c_str(), false, false, recording_IsPesRecording);
  reply.setContentType("video/mpeg");
  reply.setKeepAliveHeader();
  reply.setContentLengthHeader(RecSize(recFile_l));
  reply.setDirectMode();

  // dsyslog("live: start send video data.");
  cFileName recFile(recording_FileName.c_str(), false, false, recording_IsPesRecording);
  for (cUnbufferedFile *recData = recFile.Open(); recData; recData = recFile.NextFile()) {
    char buffer[KILOBYTE(16)];
    ssize_t bytesRead = 0;
    // dsyslog("live: send file %s", recFile->Name());
    while (0 < (bytesRead = recData->Read(buffer, sizeof(buffer)))) {
      // dsyslog("live: copy %zd bytes", bytesRead);
      reply.out().write(buffer, bytesRead);
      if (!reply.out()) {
          return HTTP_GONE;
      }
#if TNT_WATCHDOG_SILENCE
      request.touch(); // retrigger the watchdog.
#endif
    }
    // dsyslog("live: bytesRead = %zd", bytesRead);
    if (bytesRead < 0) {
      return HTTP_PARTIAL_CONTENT;
    }
  }
  // dsyslog("live: finished send video data.");
  reply.out() << std::flush;
  return HTTP_OK;
}
return DECLINED;
</%cpp>
