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(); ??
125 threadWaitForSignal();
130 pthread_mutex_lock(&processedRingLock);
131 amountReceived = processed.get(buffer+headerLength, streamChunkSize);
132 pthread_mutex_unlock(&processedRingLock);
134 *(ULONG*)&buffer[0] = htonl(2); // stream channel
135 *(ULONG*)&buffer[4] = htonl(streamID);
136 *(ULONG*)&buffer[8] = htonl(0); // here insert flag: 0 = ok, data follows
137 *(ULONG*)&buffer[12] = htonl(amountReceived);
139 tcp->sendPacket(buffer, amountReceived + headerLength);
140 } while(processed.getContent() >= streamChunkSize);
144 void MVPReceiver::sendStreamEnd()
146 ULONG bufferLength = sizeof(ULONG) * 4;
147 UCHAR buffer[bufferLength];
148 *(ULONG*)&buffer[0] = htonl(2); // stream channel
149 *(ULONG*)&buffer[4] = htonl(streamID);
150 *(ULONG*)&buffer[8] = htonl(1); // stream end
151 *(ULONG*)&buffer[12] = htonl(0); // zero length, no more data
152 tcp->sendPacket(buffer, bufferLength);
155 ULONG MVPReceiver::getBlock(unsigned char* buffer, unsigned long amount)
158 pthread_mutex_lock(&processedRingLock);
162 while ((unsigned long)processed.getContent() < amount)
164 pthread_mutex_unlock(&processedRingLock);
165 if (++numTries == 30) // 15s
167 logger->log("MVPReceiver", Log::DEBUG, "getBlock timeout");
171 pthread_mutex_lock(&processedRingLock);
174 unsigned long amountReceived = processed.get(buffer, amount);
175 pthread_mutex_unlock(&processedRingLock);
176 return amountReceived;