Ticket #6906: livetv_http_stream.2.patch

File livetv_http_stream.2.patch, 9.7 KB (added by buehlmann@…, 15 years ago)
  • libs/libmythtv/livetvchain.h

     
    8181    bool IsHostSocket(const MythSocket *sock) const;
    8282    uint HostSocketCount(void) const;
    8383    void DelHostSocket(MythSocket *sock);
     84   
     85    void SetRecordingProfile(QString profile) { recordingProfile = profile; };
     86    QString GetRecordingProfile() { return recordingProfile; };
    8487 
    8588  private:
    8689    void BroadcastUpdate();
     
    106109
    107110    mutable QMutex m_sockLock;
    108111    QList<MythSocket*> m_inUseSocks;
     112   
     113    QString recordingProfile;
    109114};
    110115
    111116#endif
  • libs/libmythtv/tv_rec.cpp

     
    39543954    QString profileName = "Live TV";
    39553955    if (!tvchain && rec)
    39563956        profileName = rec->GetScheduledRecording()->getProfileName();
     3957    else if ((tvchain!=NULL) && (((LiveTVChain*)tvchain)->GetRecordingProfile()!=NULL))
     3958        profileName = ((LiveTVChain*)tvchain)->GetRecordingProfile();
    39573959
    39583960    if (!profile.loadByType(profileName, cardtype))
    39593961    {
  • programs/mythbackend/httplivetv.cpp

     
     1//////////////////////////////////////////////////////////////////////////////
     2// Program Name: httplivetv.cpp
     3//
     4// Purpose - LiveTV Stream HttpServerExtension
     5//
     6// Created By  : Rene Buehlmann                 Created On : Aug. 23, 2009
     7// Modified By :                                Modified On:
     8//
     9//////////////////////////////////////////////////////////////////////////////
     10
     11// POSIX headers
     12#include <unistd.h>
     13
     14// ANSI C headers
     15//#include <cmath>
     16#include <cstdio>
     17#include <cstdlib>
     18
     19// MythTV headers
     20#include "httplivetv.h"
     21#include "backendutil.h"
     22#include "mythxml.h"
     23
     24#include "mythcontext.h"
     25
     26#define STREAM_BLOCK_SIZE 10000 //in bytes
     27/////////////////////////////////////////////////////////////////////////////
     28//
     29/////////////////////////////////////////////////////////////////////////////
     30
     31HttpLiveTV::HttpLiveTV(QMap<int, EncoderLink *> *tvList, Scheduler *sched,
     32        AutoExpire *expirer, bool bIsMaster) :
     33    HttpServerExtension("HttpLiveTV", QString::null)
     34{
     35    m_pEncoders = tvList;
     36    m_pSched = sched;
     37    m_pExpirer = expirer;
     38    m_bIsMaster = bIsMaster;
     39
     40    m_nPreRollSeconds = gContext->GetNumSetting("RecordPreRoll", 0);
     41}
     42
     43HttpLiveTV::~HttpLiveTV()
     44{
     45}
     46
     47bool HttpLiveTV::ProcessRequest(HttpWorkerThread * /* pThread */,
     48        HTTPRequest *pRequest)
     49{
     50    try
     51    {
     52        if (pRequest)
     53        {
     54            if (pRequest->m_sBaseUrl != "/livetv")
     55                return (false);
     56
     57            VERBOSE(VB_IMPORTANT, "HttpLiveTV: Requested channel:"
     58                    + pRequest->m_sMethod);
     59            pRequest->m_eResponseType = ResponseTypeNone;
     60
     61            QString rHeader =
     62                    QString("HTTP/%1.%2 %3\r\n"
     63                        "Date: %4\r\n"
     64                        "Server: %5, UPnP/1.0, MythTV %6\r\n"
     65                        "Content-type: video/mpeg\r\n\r\n") .arg(
     66                            pRequest->m_nMajor) .arg(pRequest->m_nMinor) .arg(
     67                            QString("200 OK")) .arg(
     68                            QDateTime::currentDateTime().toString(
     69                                    "d MMM yyyy hh:mm:ss")) .arg(
     70                            HttpServer::g_sPlatform) .arg(MYTH_BINARY_VERSION);
     71
     72            QByteArray sHeader = rHeader.toUtf8();
     73            pRequest->WriteBlockDirect(sHeader.constData(), sHeader.length());
     74
     75            QMap<int, EncoderLink *>::Iterator iter = m_pEncoders->find(1);
     76            if (iter == m_pEncoders->end())
     77            {
     78                VERBOSE(VB_IMPORTANT, "HttpLiveTV: " + QString(
     79                        "Unknown encoder: %1").arg(1));
     80                return false;
     81            }
     82
     83            EncoderLink *enc = *iter;
     84            LiveTVChain *chain = new LiveTVChain();
     85            chain->SetRecordingProfile(QString("Stream TV"));
     86            enc->SpawnLiveTV(chain, false, pRequest->m_sMethod);
     87            int i = 0;
     88            bool finish = false;
     89
     90            while (!finish)
     91            {
     92                VERBOSE(VB_IMPORTANT, "HttpLiveTV: Getting recording...");
     93                ProgramInfo *pi = enc->GetRecording();
     94                while (pi == NULL && i < 10)
     95                {
     96                    VERBOSE(VB_IMPORTANT,
     97                            "HttpLiveTV: Waiting for recording...");
     98                    sleep(1);
     99                    pi = enc->GetRecording();
     100                }
     101
     102                if (pi == NULL)
     103                {
     104                    VERBOSE(VB_IMPORTANT, "HttpLiveTV: No recording");
     105                    enc->StopLiveTV();
     106                    return true;
     107                }
     108                VERBOSE(VB_IMPORTANT, "HttpLiveTV: Got recording: "
     109                        + pi->GetPlaybackURL());
     110                QByteArray bytes = pi->GetPlaybackURL().toAscii();
     111
     112                const char * file = bytes.data();
     113                FILE *f = fopen(file, "r");
     114                char buf[STREAM_BLOCK_SIZE];
     115                int len = 100;
     116                int zerocount = 0;
     117                while (zerocount < 3 && !finish)
     118                {
     119                    if (len == 0)
     120                    {
     121                        sleep(1);
     122                        zerocount++;
     123                    }
     124                    else
     125                    {
     126                        zerocount = 0;
     127                    }
     128                    len = fread((char *) &buf, 1, STREAM_BLOCK_SIZE, f);
     129                    int sent = pRequest->WriteBlockDirect(buf, len);
     130                    if (sent != len)
     131                    {
     132                        VERBOSE(VB_IMPORTANT,
     133                                "HttpLiveTV: Probalby the client disconnected... Exiting...");
     134                        finish = true;
     135                    }
     136                }
     137                VERBOSE(VB_IMPORTANT, "HttpLiveTV: End of Stream.");
     138                fclose(f);
     139            }
     140            VERBOSE(VB_IMPORTANT, "HttpLiveTV: Exiting StreamTV.");
     141            enc->StopLiveTV();
     142            return true;
     143        }
     144    } catch (...)
     145    {
     146        cerr << "HttpLiveTV::ProcessRequest() - Unexpected Exception" << endl;
     147    }
     148
     149    return (false);
     150}
     151
     152// vim:set shiftwidth=4 tabstop=4 expandtab:
  • programs/mythbackend/main.cpp

     
    4949
    5050#include "mediaserver.h"
    5151#include "httpstatus.h"
     52#include "httplivetv.h"
    5253
    5354#define LOC      QString("MythBackend: ")
    5455#define LOC_WARN QString("MythBackend, Warning: ")
     
    11021103
    11031104        pHS->RegisterExtension( new HttpStatus( &tvList, sched,
    11041105                                                expirer, ismaster ));
     1106       
     1107        VERBOSE(VB_IMPORTANT, "Main::Registering HttpLiveTV Extension");
     1108        pHS->RegisterExtension( new HttpLiveTV( &tvList, sched,
     1109                                                expirer, ismaster ));
    11051110    }
    11061111
    11071112    VERBOSE(VB_IMPORTANT, QString("Enabled verbose msgs: %1").arg(verboseString));
  • programs/mythbackend/mythbackend.pro

     
    1818QMAKE_CLEAN += $(TARGET)
    1919
    2020# Input
    21 HEADERS += autoexpire.h encoderlink.h filetransfer.h httpstatus.h mainserver.h
     21HEADERS += autoexpire.h encoderlink.h filetransfer.h httpstatus.h mainserver.h httplivetv.h
    2222HEADERS += playbacksock.h scheduler.h server.h housekeeper.h backendutil.h
    2323HEADERS += upnpcdstv.h upnpcdsmusic.h upnpcdsvideo.h mediaserver.h
    2424HEADERS += mythxml.h upnpmedia.h
    2525
    26 SOURCES += autoexpire.cpp encoderlink.cpp filetransfer.cpp httpstatus.cpp
     26SOURCES += autoexpire.cpp encoderlink.cpp filetransfer.cpp httpstatus.cpp httplivetv.cpp
    2727SOURCES += main.cpp mainserver.cpp playbacksock.cpp scheduler.cpp server.cpp
    2828SOURCES += housekeeper.cpp backendutil.cpp
    2929SOURCES += upnpcdstv.cpp upnpcdsmusic.cpp upnpcdsvideo.cpp mediaserver.cpp
  • programs/mythbackend/httplivetv.h

     
     1//////////////////////////////////////////////////////////////////////////////
     2// Program Name: httplivetv.h
     3//
     4// Purpose - LiveTV Stream HttpServerExtension
     5//
     6// Created By  : Rene Buehlmann                 Created On : Aug. 23, 2009
     7// Modified By :                                Modified On:
     8//
     9//////////////////////////////////////////////////////////////////////////////
     10
     11#ifndef HTTPLIVETV_H_
     12#define HTTPLIVETV_H_
     13
     14#include "httpserver.h"
     15#include "mainserver.h"
     16#include "mythcontext.h"
     17#include "programinfo.h"
     18
     19class HttpLiveTV: public HttpServerExtension
     20{
     21private:
     22
     23    Scheduler *m_pSched;
     24    QMap<int, EncoderLink *> *m_pEncoders;
     25    AutoExpire *m_pExpirer;
     26    bool m_bIsMaster;
     27    int m_nPreRollSeconds;
     28    QMutex m_settingLock;
     29
     30private:
     31
     32
     33public:
     34    HttpLiveTV(QMap<int, EncoderLink *> *tvList, Scheduler *sched,
     35            AutoExpire *expirer, bool bIsMaster);
     36    virtual ~HttpLiveTV();
     37
     38    bool ProcessRequest(HttpWorkerThread *pThread, HTTPRequest *pRequest);
     39};
     40
     41#endif
     42