1 #include "mvpreceiver.h"
3 int MVPReceiver::numMVPReceivers = 0;
5 MVPReceiver* MVPReceiver::create(const cChannel* channel, int priority)
8 bool NeedsDetachReceivers;
9 cDevice* device = cDevice::GetDevice(channel, priority, &NeedsDetachReceivers);
11 cDevice* device = cDevice::GetDevice(channel, priority, true); // last param is live-view
16 Log::getInstance()->log("MVPReceiver", Log::INFO, "No device found to receive this channel at this priority");
20 #if VDRVERSNUM < 10500
21 if (NeedsDetachReceivers)
23 Log::getInstance()->log("MVPReceiver", Log::WARN, "Needs detach receivers");
25 // Need to detach other receivers or VDR will shut down??
29 MVPReceiver* m = new MVPReceiver(channel, device);
32 Log::getInstance()->log("MVPReceiver", Log::DEBUG, "num mvp receivers now up to %i", numMVPReceivers);
37 MVPReceiver::MVPReceiver(const cChannel* channel, cDevice* device)
38 #if VDRVERSNUM < 10300
39 : cReceiver(channel->Ca(), 0, 7, channel->Vpid(), channel->Ppid(), channel->Apid1(), channel->Apid2(), channel->Dpid1(), channel->Dpid2(), channel->Tpid())
40 #elif VDRVERSNUM < 10500
41 : cReceiver(channel->Ca(), 0, channel->Vpid(), channel->Apids(), channel->Dpids(), mergeSpidsTpid(channel->Spids(),channel->Tpid()))
42 #elif VDRVERSNUM < 10712
43 : cReceiver(channel->GetChannelID(), 0, channel->Vpid(), channel->Apids(), channel->Dpids(), mergeSpidsTpid(channel->Spids(),channel->Tpid()))
45 : cReceiver(channel, 0)
48 logger = Log::getInstance();
54 #if VDRVERSNUM >= 10712
55 AddPid(channel->Tpid());
58 // logger->log("MVPReceiver", Log::DEBUG, "Channel has VPID %i APID %i", channel->Vpid(), channel->Apid(0));
60 if (!processed.init(6000000)) return; // Ringbuffer increased for better performance
61 pthread_mutex_init(&processedRingLock, NULL);
65 // Detect whether this is video or radio and set an appropriate stream chunk size
66 // 50k for video, 5k for radio
67 // Perhaps move this client side?
68 if (channel->Vpid()) streamChunkSize = 50000;
69 else streamChunkSize = 5000;
72 device->SwitchChannel(channel, false);
73 device->AttachReceiver(this);
76 int MVPReceiver::init(TCP* ttcp, ULONG tstreamID)
83 MVPReceiver::~MVPReceiver()
86 Log::getInstance()->log("MVPReceiver", Log::DEBUG, "num mvp receivers now down to %i", numMVPReceivers);
89 void MVPReceiver::Activate(bool on)
94 logger->log("MVPReceiver", Log::DEBUG, "VDR active");
99 logger->log("MVPReceiver", Log::DEBUG, "VDR inactive, sending stream end message");
105 bool MVPReceiver::isVdrActivated()
110 void MVPReceiver::detachMVPReceiver()
116 void MVPReceiver::Receive(UCHAR* data, int length)
118 pthread_mutex_lock(&processedRingLock);
119 processed.put(data, length);
120 if (processed.getContent() > streamChunkSize) threadSignal();
121 pthread_mutex_unlock(&processedRingLock);
124 void MVPReceiver::threadMethod()
127 ULONG headerLength = sizeof(ULONG) * 4;
128 UCHAR buffer[streamChunkSize + headerLength];
131 // threadSetKillable(); ??
136 threadWaitForSignal();
142 pthread_mutex_lock(&processedRingLock);
143 amountReceived = processed.get(buffer+headerLength, streamChunkSize);
144 pthread_mutex_unlock(&processedRingLock);
146 p = (ULONG*)&buffer[0]; *p = htonl(2); // stream channel
147 p = (ULONG*)&buffer[4]; *p = htonl(streamID);
148 p = (ULONG*)&buffer[8]; *p = htonl(0); // here insert flag: 0 = ok, data follows
149 p = (ULONG*)&buffer[12]; *p = htonl(amountReceived);
151 tcp->sendPacket(buffer, amountReceived + headerLength);
152 } while(processed.getContent() >= streamChunkSize);
156 void MVPReceiver::sendStreamEnd()
159 ULONG bufferLength = sizeof(ULONG) * 4;
160 UCHAR buffer[bufferLength];
161 p = (ULONG*)&buffer[0]; *p = htonl(2); // stream channel
162 p = (ULONG*)&buffer[4]; *p = htonl(streamID);
163 p = (ULONG*)&buffer[8]; *p = htonl(1); // stream end
164 p = (ULONG*)&buffer[12]; *p = htonl(0); // zero length, no more data
165 tcp->sendPacket(buffer, bufferLength);
169 int *MVPReceiver::mergeSpidsTpid(const int *spids,int tpid)
172 const int *runspid=spids;
173 for (runspid=spids,destpids=mergedSpidsTpid;*runspid;runspid++,destpids++) {
179 return mergedSpidsTpid;