| | 1 | /** -*- Mode: c++ -*- |
| | 2 | * IPTVFeederHTTP |
| | 3 | * Copyright (c) 2008 by Karl Dietz |
| | 4 | * Copyright (c) 2006 by Mike Mironov & Mickaël Remars |
| | 5 | * Distributed as part of MythTV under GPL v2 and later. |
| | 6 | */ |
| | 7 | #include <algorithm> |
| | 8 | |
| | 9 | #include "iptvfeederhttp.h" |
| | 10 | |
| | 11 | // Qt headers |
| | 12 | #include <QUrl> |
| | 13 | |
| | 14 | // Live555 headers |
| | 15 | #include <BasicUsageEnvironment.hh> |
| | 16 | #include <MP3HTTPSource.hh> |
| | 17 | #include <MPEG2TransportStreamFromESSource.hh> |
| | 18 | |
| | 19 | // MythTV headers |
| | 20 | #include "iptvmediasink.h" |
| | 21 | #include "libmyth/mythcontext.h" |
| | 22 | #include "libmythdb/mythverbose.h" |
| | 23 | #include "tspacket.h" |
| | 24 | |
| | 25 | #define LOC QString("IPTVFeedHTTP: ") |
| | 26 | #define LOC_ERR QString("IPTVFeedHTTP, Error: ") |
| | 27 | |
| | 28 | IPTVFeederHTTP::IPTVFeederHTTP() : |
| | 29 | _source(NULL), |
| | 30 | _multiplexor(NULL), |
| | 31 | _sink(NULL) |
| | 32 | { |
| | 33 | VERBOSE(VB_RECORD, LOC + "ctor -- success"); |
| | 34 | } |
| | 35 | |
| | 36 | IPTVFeederHTTP::~IPTVFeederHTTP() |
| | 37 | { |
| | 38 | VERBOSE(VB_RECORD, LOC + "dtor -- begin"); |
| | 39 | Close(); |
| | 40 | VERBOSE(VB_RECORD, LOC + "dtor -- end"); |
| | 41 | } |
| | 42 | |
| | 43 | bool IPTVFeederHTTP::IsHTTP(const QString &url) |
| | 44 | { |
| | 45 | return url.startsWith("http://", Qt::CaseInsensitive); |
| | 46 | } |
| | 47 | |
| | 48 | bool IPTVFeederHTTP::Open(const QString &url) |
| | 49 | { |
| | 50 | VERBOSE(VB_RECORD, LOC + QString("Open(%1) -- begin").arg(url)); |
| | 51 | |
| | 52 | QMutexLocker locker(&_lock); |
| | 53 | |
| | 54 | if (_source) |
| | 55 | { |
| | 56 | VERBOSE(VB_RECORD, LOC + "Open() -- end 1"); |
| | 57 | return true; |
| | 58 | } |
| | 59 | |
| | 60 | QUrl parse(url); |
| | 61 | if (!parse.isValid() || parse.host().isEmpty() || (-1 == parse.port())) |
| | 62 | { |
| | 63 | VERBOSE(VB_RECORD, LOC + "Open() -- end 2"); |
| | 64 | return false; |
| | 65 | } |
| | 66 | |
| | 67 | /* FIXME resolve target */ |
| | 68 | QByteArray host = parse.host().toLatin1(); |
| | 69 | struct in_addr addr; |
| | 70 | addr.s_addr = our_inet_addr(host.constData()); |
| | 71 | NetAddress _target_address (addr.s_addr); |
| | 72 | |
| | 73 | // Begin by setting up our usage environment: |
| | 74 | if (!InitEnv()) |
| | 75 | return false; |
| | 76 | |
| | 77 | _source = MP3HTTPSource::createNew(*_live_env, addr, parse.port(80), parse.encodedHost().constData(), parse.encodedPath().constData()); |
| | 78 | if (!_source) |
| | 79 | { |
| | 80 | VERBOSE(VB_IMPORTANT, LOC + "Failed to create Live MP3 HTTP Source."); |
| | 81 | |
| | 82 | if (socket) |
| | 83 | delete socket; |
| | 84 | |
| | 85 | FreeEnv(); |
| | 86 | return false; |
| | 87 | } |
| | 88 | |
| | 89 | _multiplexor = MPEG2TransportStreamFromESSource::createNew(__live_env); |
| | 90 | if (!_multiplexor) |
| | 91 | { |
| | 92 | VERBOSE(VB_IMPORTANT, LOC + "Failed to create MPEG 2 Transport Stream From ES Source Filter."); |
| | 93 | |
| | 94 | FreeEnv(); |
| | 95 | return false; |
| | 96 | } |
| | 97 | /* attach MPEG2 Audio Layer 1-3 ES to TS */ |
| | 98 | _multiplexor -> addNewAudioSource (_source, 2); |
| | 99 | |
| | 100 | /* TODO a buffer worth about 5 seconds at 40mbit/s, why? We just take 300kbit/s here, |
| | 101 | * as we deal mostly with radio streams of at most 256bkit/s for the audiostream |
| | 102 | */ |
| | 103 | _sink = IPTVMediaSink::CreateNew(*_live_env, TSPacket::SIZE * 1*1024); |
| | 104 | if (!_sink) |
| | 105 | { |
| | 106 | VERBOSE(VB_IMPORTANT, |
| | 107 | QString("IPTV # Failed to create sink: %1") |
| | 108 | .arg(_live_env->getResultMsg())); |
| | 109 | |
| | 110 | Medium::close(_multiplexor); |
| | 111 | _multiplexor = NULL; |
| | 112 | Medium::close(_source); |
| | 113 | _source = NULL; |
| | 114 | FreeEnv(); |
| | 115 | |
| | 116 | return false; |
| | 117 | } |
| | 118 | |
| | 119 | _sink->startPlaying(*_source, NULL, NULL); |
| | 120 | vector<TSDataListener*>::iterator it = _listeners.begin(); |
| | 121 | for (; it != _listeners.end(); ++it) |
| | 122 | _sink->AddListener(*it); |
| | 123 | |
| | 124 | VERBOSE(VB_RECORD, LOC + "Open() -- end"); |
| | 125 | |
| | 126 | return true; |
| | 127 | } |
| | 128 | |
| | 129 | void IPTVFeederHTTP::Close(void) |
| | 130 | { |
| | 131 | VERBOSE(VB_RECORD, LOC + "Close() -- begin"); |
| | 132 | Stop(); |
| | 133 | |
| | 134 | QMutexLocker locker(&_lock); |
| | 135 | |
| | 136 | if (_sink) |
| | 137 | { |
| | 138 | Medium::close(_sink); |
| | 139 | _sink = NULL; |
| | 140 | } |
| | 141 | |
| | 142 | if (_multiplexor) |
| | 143 | { |
| | 144 | Medium::close(_multiplexor); |
| | 145 | _multiplexor = NULL; |
| | 146 | } |
| | 147 | |
| | 148 | if (_source) |
| | 149 | { |
| | 150 | Medium::close(_source); |
| | 151 | _source = NULL; |
| | 152 | } |
| | 153 | |
| | 154 | FreeEnv(); |
| | 155 | |
| | 156 | VERBOSE(VB_RECORD, LOC + "Close() -- end"); |
| | 157 | } |
| | 158 | |
| | 159 | void IPTVFeederHTTP::AddListener(TSDataListener *item) |
| | 160 | { |
| | 161 | VERBOSE(VB_RECORD, LOC + "AddListener("<<item<<") -- begin"); |
| | 162 | if (!item) |
| | 163 | { |
| | 164 | VERBOSE(VB_RECORD, LOC + "AddListener("<<item<<") -- end"); |
| | 165 | return; |
| | 166 | } |
| | 167 | |
| | 168 | // avoid duplicates |
| | 169 | RemoveListener(item); |
| | 170 | |
| | 171 | // add to local list |
| | 172 | QMutexLocker locker(&_lock); |
| | 173 | _listeners.push_back(item); |
| | 174 | |
| | 175 | if (_sink) |
| | 176 | _sink->AddListener(item); |
| | 177 | |
| | 178 | VERBOSE(VB_RECORD, LOC + "AddListener("<<item<<") -- end"); |
| | 179 | } |
| | 180 | |
| | 181 | void IPTVFeederHTTP::RemoveListener(TSDataListener *item) |
| | 182 | { |
| | 183 | VERBOSE(VB_RECORD, LOC + "RemoveListener("<<item<<") -- begin"); |
| | 184 | QMutexLocker locker(&_lock); |
| | 185 | vector<TSDataListener*>::iterator it = |
| | 186 | find(_listeners.begin(), _listeners.end(), item); |
| | 187 | |
| | 188 | if (it == _listeners.end()) |
| | 189 | { |
| | 190 | VERBOSE(VB_RECORD, LOC + "RemoveListener("<<item<<") -- end 1"); |
| | 191 | return; |
| | 192 | } |
| | 193 | |
| | 194 | // remove from local list.. |
| | 195 | *it = *_listeners.rbegin(); |
| | 196 | _listeners.resize(_listeners.size() - 1); |
| | 197 | |
| | 198 | if (_sink) |
| | 199 | _sink->RemoveListener(item); |
| | 200 | |
| | 201 | VERBOSE(VB_RECORD, LOC + "RemoveListener("<<item<<") -- end 2"); |
| | 202 | } |