1 #include "mvpreceiver.h"
3 int MVPReceiver::numMVPReceivers = 0;
5 MVPReceiver* MVPReceiver::create(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(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(), channel->Spids())
43 : cReceiver(channel->GetChannelID(), 0, channel->Vpid(), channel->Apids(), channel->Dpids(), channel->Spids())
46 logger = Log::getInstance();
52 // logger->log("MVPReceiver", Log::DEBUG, "Channel has VPID %i APID %i", channel->Vpid(), channel->Apid(0));
54 if (!processed.init(1000000)) return;
55 pthread_mutex_init(&processedRingLock, NULL);
59 // Detect whether this is video or radio and set an appropriate stream chunk size
60 // 50k for video, 5k for radio
61 // Perhaps move this client side?
62 if (channel->Vpid()) streamChunkSize = 50000;
63 else streamChunkSize = 5000;
66 device->SwitchChannel(channel, false);
67 device->AttachReceiver(this);
70 int MVPReceiver::init(TCP* ttcp, ULONG tstreamID)
77 MVPReceiver::~MVPReceiver()
83 Log::getInstance()->log("MVPReceiver", Log::DEBUG, "num mvp receivers now down to %i", numMVPReceivers);
86 void MVPReceiver::Activate(bool on)
91 logger->log("MVPReceiver", Log::DEBUG, "VDR active");
96 logger->log("MVPReceiver", Log::DEBUG, "VDR inactive, sending stream end message");
102 bool MVPReceiver::isVdrActivated()
107 void MVPReceiver::Receive(UCHAR* data, int length)
109 pthread_mutex_lock(&processedRingLock);
110 processed.put(data, length);
111 if (processed.getContent() > streamChunkSize) threadSignal();
112 pthread_mutex_unlock(&processedRingLock);
115 void MVPReceiver::threadMethod()
117 ULONG headerLength = sizeof(ULONG) * 4;
118 UCHAR buffer[streamChunkSize + headerLength];
121 // threadSetKillable(); ??
126 threadWaitForSignal();
132 pthread_mutex_lock(&processedRingLock);
133 amountReceived = processed.get(buffer+headerLength, streamChunkSize);
134 pthread_mutex_unlock(&processedRingLock);
136 *(ULONG*)&buffer[0] = htonl(2); // stream channel
137 *(ULONG*)&buffer[4] = htonl(streamID);
138 *(ULONG*)&buffer[8] = htonl(0); // here insert flag: 0 = ok, data follows
139 *(ULONG*)&buffer[12] = htonl(amountReceived);
141 tcp->sendPacket(buffer, amountReceived + headerLength);
142 } while(processed.getContent() >= streamChunkSize);
146 void MVPReceiver::sendStreamEnd()
148 ULONG bufferLength = sizeof(ULONG) * 4;
149 UCHAR buffer[bufferLength];
150 *(ULONG*)&buffer[0] = htonl(2); // stream channel
151 *(ULONG*)&buffer[4] = htonl(streamID);
152 *(ULONG*)&buffer[8] = htonl(1); // stream end
153 *(ULONG*)&buffer[12] = htonl(0); // zero length, no more data
154 tcp->sendPacket(buffer, bufferLength);
157 ULONG MVPReceiver::getBlock(unsigned char* buffer, unsigned long amount)
160 pthread_mutex_lock(&processedRingLock);
164 while ((unsigned long)processed.getContent() < amount)
166 pthread_mutex_unlock(&processedRingLock);
167 if (++numTries == 30) // 15s
169 logger->log("MVPReceiver", Log::DEBUG, "getBlock timeout");
173 pthread_mutex_lock(&processedRingLock);
176 unsigned long amountReceived = processed.get(buffer, amount);
177 pthread_mutex_unlock(&processedRingLock);
178 return amountReceived;