<%pre>

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

using namespace vdrlive;

off_t RecSize(cRecording const * recording)
{
    cFileName recFile(recording->FileName(), false, false, recording->IsPesRecording());
    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>
//if (!logged_in && LiveSetup().UseAuth()) return reply.redirect("login.html");

cRecording const * recording = LiveRecordingsManager()->GetByMd5Hash(recid);
if (recording) {
	reply.setContentType("video/mpeg");
    reply.setKeepAliveHeader();
    reply.setContentLengthHeader(RecSize(recording));
    reply.setDirectMode();

    cFileName recFile(recording->FileName(), false, false, recording->IsPesRecording());
    // dsyslog("live: start send video data.");
    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>
