|
| 1 | +/* |
| 2 | + * device.c: IPTV plugin for the Video Disk Recorder |
| 3 | + * |
| 4 | + * See the README file for copyright information and how to reach the author. |
| 5 | + * |
| 6 | + * $Id: device.c,v 1.1 2007/09/12 17:28:59 rahrenbe Exp $ |
| 7 | + */ |
| 8 | + |
| 9 | +#include "common.h" |
| 10 | +#include "device.h" |
| 11 | + |
| 12 | +#define IPTV_MAX_DEVICES 8 |
| 13 | + |
| 14 | +cIptvDevice * IptvDevices[IPTV_MAX_DEVICES]; |
| 15 | + |
| 16 | +unsigned int cIptvDevice::deviceCount = 0; |
| 17 | + |
| 18 | +cIptvDevice::cIptvDevice(unsigned int Index) |
| 19 | +: deviceIndex(Index), |
| 20 | + isPacketDelivered(false), |
| 21 | + isOpenDvr(false), |
| 22 | + pIptvStreamer(NULL) |
| 23 | +{ |
| 24 | + debug("cIptvDevice::cIptvDevice()\n"); |
| 25 | + tsBuffer = new cRingBufferLinear(MEGABYTE(8), TS_SIZE, false, "IPTV"); |
| 26 | + tsBuffer->SetTimeouts(100, 100); |
| 27 | + pIptvStreamer = new cIptvStreamer(tsBuffer, &mutex); |
| 28 | +} |
| 29 | + |
| 30 | +cIptvDevice::~cIptvDevice() |
| 31 | +{ |
| 32 | + debug("cIptvDevice::~cIptvDevice()\n"); |
| 33 | + if (pIptvStreamer) |
| 34 | + delete pIptvStreamer; |
| 35 | + delete tsBuffer; |
| 36 | +} |
| 37 | + |
| 38 | +bool cIptvDevice::Initialize(unsigned int DeviceCount) |
| 39 | +{ |
| 40 | + debug("cIptvDevice::Initialize()\n"); |
| 41 | + if (DeviceCount > IPTV_MAX_DEVICES) |
| 42 | + DeviceCount = IPTV_MAX_DEVICES; |
| 43 | + for (unsigned int i = 0; i < DeviceCount; ++i) |
| 44 | + IptvDevices[i] = new cIptvDevice(i); |
| 45 | + return true; |
| 46 | +} |
| 47 | + |
| 48 | +unsigned int cIptvDevice::Count(void) |
| 49 | +{ |
| 50 | + unsigned int count = 0; |
| 51 | + debug("cIptvDevice::Count()\n"); |
| 52 | + for (unsigned int i = 0; i < IPTV_MAX_DEVICES; ++i) { |
| 53 | + if (IptvDevices[i]) |
| 54 | + count++; |
| 55 | + } |
| 56 | + return count; |
| 57 | +} |
| 58 | + |
| 59 | +cIptvDevice *cIptvDevice::Get(unsigned int DeviceIndex) |
| 60 | +{ |
| 61 | + debug("cIptvDevice::Get()\n"); |
| 62 | + if ((DeviceIndex > 0) && (DeviceIndex <= IPTV_MAX_DEVICES)) |
| 63 | + return IptvDevices[DeviceIndex - 1]; |
| 64 | + return NULL; |
| 65 | +} |
| 66 | + |
| 67 | +cString cIptvDevice::GetChannelSettings(const char *Param, int *IpPort, int *Protocol) |
| 68 | +{ |
| 69 | + unsigned int a, b, c, d; |
| 70 | + |
| 71 | + debug("cIptvDevice::GetChannelSettings()\n"); |
| 72 | + if (sscanf(Param, "IPTV-UDP-%u.%u.%u.%u-%u", &a, &b, &c, &d, IpPort) == 5) { |
| 73 | + debug("UDP channel detected\n"); |
| 74 | + Protocol = (int*)(cIptvStreamer::PROTOCOL_UDP); |
| 75 | + return cString::sprintf("%u.%u.%u.%u", a, b, c, d); |
| 76 | + } |
| 77 | + else if (sscanf(Param, "IPTV-RTSP-%u.%u.%u.%u-%u", &a, &b, &c, &d, IpPort) == 5) { |
| 78 | + debug("RTSP channel detected\n"); |
| 79 | + Protocol = (int*)(cIptvStreamer::PROTOCOL_RTSP); |
| 80 | + return cString::sprintf("%u.%u.%u.%u", a, b, c, d); |
| 81 | + } |
| 82 | + else if (sscanf(Param, "IPTV-HTTP-%u.%u.%u.%u-%u", &a, &b, &c, &d, IpPort) == 5) { |
| 83 | + debug("HTTP channel detected\n"); |
| 84 | + Protocol = (int*)cIptvStreamer::PROTOCOL_HTTP; |
| 85 | + return cString::sprintf("%u.%u.%u.%u", a, b, c, d); |
| 86 | + } |
| 87 | + return NULL; |
| 88 | +} |
| 89 | + |
| 90 | +bool cIptvDevice::ProvidesIptv(const char *Param) const |
| 91 | +{ |
| 92 | + debug("cIptvDevice::ProvidesIptv()\n"); |
| 93 | + return (strncmp(Param, "IPTV", 4) == 0); |
| 94 | +} |
| 95 | + |
| 96 | +bool cIptvDevice::ProvidesSource(int Source) const |
| 97 | +{ |
| 98 | + debug("cIptvDevice::ProvidesSource()\n"); |
| 99 | + return ((Source & cSource::st_Mask) == cSource::stPlug); |
| 100 | +} |
| 101 | + |
| 102 | +bool cIptvDevice::ProvidesTransponder(const cChannel *Channel) const |
| 103 | +{ |
| 104 | + debug("cIptvDevice::ProvidesTransponder()\n"); |
| 105 | + return (ProvidesSource(Channel->Source()) && ProvidesIptv(Channel->Param())); |
| 106 | +} |
| 107 | + |
| 108 | +bool cIptvDevice::ProvidesChannel(const cChannel *Channel, int Priority, bool *NeedsDetachReceivers) const |
| 109 | +{ |
| 110 | + bool result = false; |
| 111 | + bool needsDetachReceivers = false; |
| 112 | + |
| 113 | + debug("cIptvDevice::ProvidesChannel()\n"); |
| 114 | + if (ProvidesTransponder(Channel)) |
| 115 | + result = true; |
| 116 | + if (NeedsDetachReceivers) |
| 117 | + *NeedsDetachReceivers = needsDetachReceivers; |
| 118 | + return result; |
| 119 | +} |
| 120 | + |
| 121 | +bool cIptvDevice::SetChannelDevice(const cChannel *Channel, bool LiveView) |
| 122 | +{ |
| 123 | + int port, protocol; |
| 124 | + cString addr; |
| 125 | + |
| 126 | + debug("cIptvDevice::SetChannelDevice()\n"); |
| 127 | + addr = GetChannelSettings(Channel->Param(), &port, &protocol); |
| 128 | + if (addr) |
| 129 | + pIptvStreamer->SetStream(addr, port, protocol); |
| 130 | + return true; |
| 131 | +} |
| 132 | + |
| 133 | +bool cIptvDevice::SetPid(cPidHandle *Handle, int Type, bool On) |
| 134 | +{ |
| 135 | + debug("cIptvDevice::SetPid()\n"); |
| 136 | + return true; |
| 137 | +} |
| 138 | + |
| 139 | +bool cIptvDevice::OpenDvr(void) |
| 140 | +{ |
| 141 | + debug("cIptvDevice::OpenDvr()\n"); |
| 142 | + mutex.Lock(); |
| 143 | + isPacketDelivered = false; |
| 144 | + tsBuffer->Clear(); |
| 145 | + mutex.Unlock(); |
| 146 | + pIptvStreamer->Activate(); |
| 147 | + isOpenDvr = true; |
| 148 | + return true; |
| 149 | +} |
| 150 | + |
| 151 | +void cIptvDevice::CloseDvr(void) |
| 152 | +{ |
| 153 | + debug("cIptvDevice::CloseDvr()\n"); |
| 154 | + pIptvStreamer->Deactivate(); |
| 155 | + isOpenDvr = false; |
| 156 | +} |
| 157 | + |
| 158 | +bool cIptvDevice::GetTSPacket(uchar *&Data) |
| 159 | +{ |
| 160 | + int Count = 0; |
| 161 | + //debug("cIptvDevice::GetTSPacket()\n"); |
| 162 | + if (isPacketDelivered) { |
| 163 | + tsBuffer->Del(TS_SIZE); |
| 164 | + isPacketDelivered = false; |
| 165 | + } |
| 166 | + uchar *p = tsBuffer->Get(Count); |
| 167 | + if (p && Count >= TS_SIZE) { |
| 168 | + if (*p != TS_SYNC_BYTE) { |
| 169 | + for (int i = 1; i < Count; i++) { |
| 170 | + if (p[i] == TS_SYNC_BYTE) { |
| 171 | + Count = i; |
| 172 | + break; |
| 173 | + } |
| 174 | + } |
| 175 | + tsBuffer->Del(Count); |
| 176 | + error("ERROR: skipped %d bytes to sync on TS packet\n", Count); |
| 177 | + return false; |
| 178 | + } |
| 179 | + isPacketDelivered = true; |
| 180 | + Data = p; |
| 181 | + return true; |
| 182 | + } |
| 183 | + Data = NULL; |
| 184 | + return true; |
| 185 | +} |
| 186 | + |
0 commit comments