summaryrefslogtreecommitdiffstats
path: root/xbmc/cores/dvdplayer/DVDDemuxers/DVDDemuxHTSP.cpp
blob: 3674416516e5eab731d5eb1378a91478ae516a14 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
/*
 *      Copyright (C) 2005-2013 Team XBMC
 *      http://xbmc.org
 *
 *  This Program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2, or (at your option)
 *  any later version.
 *
 *  This Program 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 General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with XBMC; see the file COPYING.  If not, see
 *  <http://www.gnu.org/licenses/>.
 *
 */


#include "DVDCodecs/DVDCodecs.h"
#include "DVDInputStreams/DVDInputStream.h"
#include "DVDInputStreams/DVDInputStreamHTSP.h"
#include "DVDDemuxHTSP.h"
#include "DVDDemuxUtils.h"
#include "DVDClock.h"
#include "dialogs/GUIDialogKaiToast.h"
#include "utils/log.h"
#include "utils/StringUtils.h"
#include <arpa/inet.h>

extern "C" {
#include "lib/libhts/net.h"
#include "lib/libhts/htsmsg.h"
#include "lib/libhts/htsmsg_binary.h"
}

using namespace std;
using namespace HTSP;

class CDemuxStreamVideoHTSP
  : public CDemuxStreamVideo
{
  CDVDDemuxHTSP *m_parent;
  string         m_codec;
public:
  CDemuxStreamVideoHTSP(CDVDDemuxHTSP *parent, const string& codec)
    : m_parent(parent)
    , m_codec(codec)
  {}
  void GetStreamInfo(std::string& strInfo)
  {
    strInfo = StringUtils::Format("%s, delay: %u, drops: %ub %up %ui"
                                          , m_codec.c_str()
                                          , m_parent->m_QueueStatus.delay
                                          , m_parent->m_QueueStatus.bdrops
                                          , m_parent->m_QueueStatus.pdrops
                                          , m_parent->m_QueueStatus.idrops);
  }
};

class CDemuxStreamAudioHTSP
  : public CDemuxStreamAudio
{
  CDVDDemuxHTSP *m_parent;
  string         m_codec;
public:
  CDemuxStreamAudioHTSP(CDVDDemuxHTSP *parent, const string& codec)
    : m_parent(parent)
    , m_codec(codec)

  {}
  void GetStreamInfo(string& strInfo)
  {
    strInfo = StringUtils::Format("%s", m_codec.c_str());
  }
};

CDVDDemuxHTSP::CDVDDemuxHTSP()
  : CDVDDemux()
  , m_Input(NULL)
  , m_StatusCount(0)
{
}

CDVDDemuxHTSP::~CDVDDemuxHTSP()
{
  Dispose();
}

bool CDVDDemuxHTSP::Open(CDVDInputStream* input)
{
  Dispose();

  if(!input->IsStreamType(DVDSTREAM_TYPE_HTSP))
    return false;

  m_Input       = (CDVDInputStreamHTSP*)input;
  m_StatusCount = 0;

  while(m_Streams.empty() && m_StatusCount == 0)
  {
    DemuxPacket* pkg = Read();
    if(!pkg)
      return false;
    CDVDDemuxUtils::FreeDemuxPacket(pkg);
  }

  return true;
}

void CDVDDemuxHTSP::Dispose()
{
}

void CDVDDemuxHTSP::Reset()
{
}


void CDVDDemuxHTSP::Flush()
{
}

bool CDVDDemuxHTSP::ReadStream(uint8_t* buf, int len)
{
  while(len > 0)
  {
    int ret = m_Input->Read(buf, len);
    if(ret <= 0)
      return false;
    len -= ret;
    buf += ret;
  }
  return true;
}

htsmsg_t* CDVDDemuxHTSP::ReadStream()
{
  if(m_Input->IsStreamType(DVDSTREAM_TYPE_HTSP))
    return ((CDVDInputStreamHTSP*)m_Input)->ReadStream();

  uint32_t l;
  if(!ReadStream((uint8_t*)&l, 4))
    return NULL;

  l = ntohl(l);
  if(l == 0)
    return htsmsg_create_map();

  uint8_t* buf = (uint8_t*)malloc(l);
  if(!buf)
    return NULL;

  if(!ReadStream(buf, l))
    return NULL;

  return htsmsg_binary_deserialize(buf, l, buf); /* consumes 'buf' */
}

DemuxPacket* CDVDDemuxHTSP::Read()
{
  htsmsg_t *  msg;
  while((msg = ReadStream()))
  {
    const char* method = htsmsg_get_str(msg, "method");
    if(method == NULL)
      break;

    if     (strcmp("subscriptionStart",  method) == 0)
      SubscriptionStart(msg);
    else if(strcmp("subscriptionStop",   method) == 0)
      SubscriptionStop (msg);
    else if(strcmp("subscriptionStatus", method) == 0)
      SubscriptionStatus(msg);
    else if(strcmp("queueStatus"       , method) == 0)
      CHTSPSession::ParseQueueStatus(msg, m_QueueStatus);
    else if(strcmp("muxpkt"            , method) == 0)
    {
      uint32_t    index, duration;
      const void* bin;
      size_t      binlen;
      int64_t     ts;

      if(htsmsg_get_u32(msg, "stream" , &index)  ||
         htsmsg_get_bin(msg, "payload", &bin, &binlen))
        break;

      DemuxPacket* pkt = CDVDDemuxUtils::AllocateDemuxPacket(binlen);

      memcpy(pkt->pData, bin, binlen);
      pkt->iSize = binlen;

      if(!htsmsg_get_u32(msg, "duration", &duration))
        pkt->duration = (double)duration * DVD_TIME_BASE / 1000000;

      if(!htsmsg_get_s64(msg, "dts", &ts))
        pkt->dts = (double)ts * DVD_TIME_BASE / 1000000;
      else
        pkt->dts = DVD_NOPTS_VALUE;

      if(!htsmsg_get_s64(msg, "pts", &ts))
        pkt->pts = (double)ts * DVD_TIME_BASE / 1000000;
      else
        pkt->pts = DVD_NOPTS_VALUE;

      pkt->iStreamId = -1;
      for(int i = 0; i < (int)m_Streams.size(); i++)
      {
        if(m_Streams[i]->iPhysicalId == (int)index)
        {
          pkt->iStreamId = i;
          break;
        }
      }

      htsmsg_destroy(msg);
      return pkt;
    }

    break;
  }

  if(msg)
  {
    htsmsg_destroy(msg);
    return CDVDDemuxUtils::AllocateDemuxPacket(0);
  }
  return NULL;
}

void CDVDDemuxHTSP::SubscriptionStart (htsmsg_t *m)
{
  htsmsg_t       *streams;
  htsmsg_t       *info;
  htsmsg_field_t *f;

  if((info = htsmsg_get_map(m, "sourceinfo")))
  {
    HTSMSG_FOREACH(f, info)
    {
      if(f->hmf_type != HMF_STR)
        continue;
      CLog::Log(LOGDEBUG, "CDVDDemuxHTSP::SubscriptionStart - %s: %s", f->hmf_name, htsmsg_field_get_string(f));
    }
  }

  if((streams = htsmsg_get_list(m, "streams")) == NULL)
  {
    CLog::Log(LOGERROR, "CDVDDemuxHTSP::SubscriptionStart - malformed message");
    return;
  }

  for(int i = 0; i < (int)m_Streams.size(); i++)
    delete m_Streams[i];
  m_Streams.clear();

  HTSMSG_FOREACH(f, streams)
  {
    uint32_t    index;
    const char* type;
    const char* lang;
    htsmsg_t* sub;

    if(f->hmf_type != HMF_MAP)
      continue;
    sub = &f->hmf_msg;

    if((type = htsmsg_get_str(sub, "type")) == NULL)
      continue;

    if(htsmsg_get_u32(sub, "index", &index))
      continue;

    union {
      CDemuxStream*      g;
      CDemuxStreamAudio* a;
      CDemuxStreamVideo* v;
      CDemuxStreamSubtitle* s;
      CDemuxStreamTeletext* t;
    } st;

    CLog::Log(LOGDEBUG, "CDVDDemuxHTSP::SubscriptionStart - id: %d, type: %s", index, type);

    if(!strcmp(type, "AC3")) {
      st.a = new CDemuxStreamAudioHTSP(this, type);
      st.a->codec = AV_CODEC_ID_AC3;
    } else if(!strcmp(type, "EAC3")) {
      st.a = new CDemuxStreamAudioHTSP(this, type);
      st.a->codec = AV_CODEC_ID_EAC3;
    } else if(!strcmp(type, "MPEG2AUDIO")) {
      st.a = new CDemuxStreamAudioHTSP(this, type);
      st.a->codec = AV_CODEC_ID_MP2;
    } else if(!strcmp(type, "AAC")) {
      st.a = new CDemuxStreamAudioHTSP(this, type);
      st.a->codec = AV_CODEC_ID_AAC;
    } else if(!strcmp(type, "MPEG2VIDEO")) {
      st.v = new CDemuxStreamVideoHTSP(this, type);
      st.v->codec = AV_CODEC_ID_MPEG2VIDEO;
      st.v->iWidth  = htsmsg_get_u32_or_default(sub, "width" , 0);
      st.v->iHeight = htsmsg_get_u32_or_default(sub, "height", 0);
    } else if(!strcmp(type, "H264")) {
      st.v = new CDemuxStreamVideoHTSP(this, type);
      st.v->codec = AV_CODEC_ID_H264;
      st.v->iWidth  = htsmsg_get_u32_or_default(sub, "width" , 0);
      st.v->iHeight = htsmsg_get_u32_or_default(sub, "height", 0);
    } else if(!strcmp(type, "DVBSUB")) {
      st.s = new CDemuxStreamSubtitle();
      st.s->codec = AV_CODEC_ID_DVB_SUBTITLE;
      uint32_t composition_id = 0, ancillary_id = 0;
      htsmsg_get_u32(sub, "composition_id", &composition_id);
      htsmsg_get_u32(sub, "ancillary_id"  , &ancillary_id);
      if(composition_id || ancillary_id)
      {
        st.s->ExtraData = new uint8_t[4];
        st.s->ExtraSize = 4;
        st.s->ExtraData[0] = (composition_id >> 8) & 0xff;
        st.s->ExtraData[1] = (composition_id >> 0) & 0xff;
        st.s->ExtraData[2] = (ancillary_id   >> 8) & 0xff;
        st.s->ExtraData[3] = (ancillary_id   >> 0) & 0xff;
      }
    } else if(!strcmp(type, "TEXTSUB")) {
      st.s = new CDemuxStreamSubtitle();
      st.s->codec = AV_CODEC_ID_TEXT;
    } else if(!strcmp(type, "TELETEXT")) {
      st.t = new CDemuxStreamTeletext();
      st.t->codec = AV_CODEC_ID_DVB_TELETEXT;
    } else {
      continue;
    }

    if((lang = htsmsg_get_str(sub, "language")))
    {
      strncpy(st.g->language, lang, sizeof(st.g->language));
      st.g->language[sizeof(st.g->language) - 1] = '\0';
    }

    st.g->iId         = m_Streams.size();
    st.g->iPhysicalId = index;
    m_Streams.push_back(st.g);
  }
}
void CDVDDemuxHTSP::SubscriptionStop  (htsmsg_t *m)
{
  for(int i = 0; i < (int)m_Streams.size(); i++)
    delete m_Streams[i];
  m_Streams.clear();
}

void CDVDDemuxHTSP::SubscriptionStatus(htsmsg_t *m)
{
  const char* status;
  status = htsmsg_get_str(m, "status");
  if(status == NULL)
    m_Status = "";
  else
  {
    m_StatusCount++;
    m_Status = status;
    CLog::Log(LOGDEBUG, "CDVDDemuxHTSP::SubscriptionStatus - %s", status);
    CGUIDialogKaiToast::QueueNotification(CGUIDialogKaiToast::Info, "TVHeadend Status", status, TOAST_DISPLAY_TIME, false);
  }
}

CDemuxStream* CDVDDemuxHTSP::GetStream(int iStreamId)
{
  if(iStreamId >= 0 && iStreamId < (int)m_Streams.size())
    return m_Streams[iStreamId];

  return NULL;
}

int CDVDDemuxHTSP::GetNrOfStreams()
{
  return m_Streams.size();
}

std::string CDVDDemuxHTSP::GetFileName()
{
  if(m_Input)
    return m_Input->GetFileName();
  else
    return "";
}

void CDVDDemuxHTSP::Abort()
{
  if(m_Input)
    return m_Input->Abort();
}