]> git.vomp.tv Git - vompserver.git/blob - mvpreceiver.c
Compatibility for VDR 1.7.30, thanks to hondansx, Uwe and MartenR.
[vompserver.git] / mvpreceiver.c
1 #include "mvpreceiver.h"
2
3 int MVPReceiver::numMVPReceivers = 0;
4
5 MVPReceiver* MVPReceiver::create(cChannel* channel, int priority)
6 {
7 #if VDRVERSNUM < 10500
8   bool NeedsDetachReceivers;
9   cDevice* device = cDevice::GetDevice(channel, priority, &NeedsDetachReceivers);
10 #else
11   cDevice* device = cDevice::GetDevice(channel, priority, true); // last param is live-view
12 #endif
13
14   if (!device)
15   {
16     Log::getInstance()->log("MVPReceiver", Log::INFO, "No device found to receive this channel at this priority");
17     return NULL;
18   }
19
20 #if VDRVERSNUM < 10500
21   if (NeedsDetachReceivers)
22   {
23     Log::getInstance()->log("MVPReceiver", Log::WARN, "Needs detach receivers");
24
25     // Need to detach other receivers or VDR will shut down??
26   }
27 #endif
28
29   MVPReceiver* m = new MVPReceiver(channel, device);
30
31   numMVPReceivers++;
32   Log::getInstance()->log("MVPReceiver", Log::DEBUG, "num mvp receivers now up to %i", numMVPReceivers);
33   
34   return m;
35 }
36
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(), 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()))
44 #else
45 : cReceiver(channel, 0)
46 #endif
47 {
48   logger = Log::getInstance();
49   vdrActivated = false;
50   inittedOK = 0;
51   streamID = 0;
52   tcp = NULL;
53
54 //  logger->log("MVPReceiver", Log::DEBUG, "Channel has VPID %i APID %i", channel->Vpid(), channel->Apid(0));
55
56   if (!processed.init(1000000)) return;
57   pthread_mutex_init(&processedRingLock, NULL);
58
59   // OK
60   
61   // Detect whether this is video or radio and set an appropriate stream chunk size
62   // 50k for video, 5k for radio
63   // Perhaps move this client side?
64   if (channel->Vpid()) streamChunkSize = 50000;
65   else streamChunkSize = 5000;
66   
67   inittedOK = 1;
68   device->SwitchChannel(channel, false);
69   device->AttachReceiver(this);
70 }
71
72 int MVPReceiver::init(TCP* ttcp, ULONG tstreamID)
73 {
74   tcp = ttcp;
75   streamID = tstreamID;
76   return inittedOK;
77 }
78
79 MVPReceiver::~MVPReceiver()
80 {
81   numMVPReceivers--;
82   Log::getInstance()->log("MVPReceiver", Log::DEBUG, "num mvp receivers now down to %i", numMVPReceivers);
83 }
84
85 void MVPReceiver::Activate(bool on)
86 {
87   vdrActivated = on;
88   if (on) 
89   {
90     logger->log("MVPReceiver", Log::DEBUG, "VDR active");
91     threadStart();
92   }
93   else
94   {
95     logger->log("MVPReceiver", Log::DEBUG, "VDR inactive, sending stream end message");
96     threadStop();
97     sendStreamEnd();
98   }
99 }
100
101 bool MVPReceiver::isVdrActivated()
102 {
103   return vdrActivated;
104 }
105
106 void MVPReceiver::detachMVPReceiver()
107 {
108   threadStop();
109   Detach();
110 }
111
112 void MVPReceiver::Receive(UCHAR* data, int length)
113 {
114   pthread_mutex_lock(&processedRingLock);
115   processed.put(data, length);
116   if (processed.getContent() > streamChunkSize) threadSignal();
117   pthread_mutex_unlock(&processedRingLock);
118 }
119
120 void MVPReceiver::threadMethod()
121 {
122   ULONG *p;
123   ULONG headerLength = sizeof(ULONG) * 4;
124   UCHAR buffer[streamChunkSize + headerLength];
125   int amountReceived;
126
127 //   threadSetKillable(); ??
128
129   while(1)
130   {
131     threadLock();
132     threadWaitForSignal();
133     threadUnlock();
134     threadCheckExit();
135     
136     do
137     {
138       pthread_mutex_lock(&processedRingLock);
139       amountReceived = processed.get(buffer+headerLength, streamChunkSize);
140       pthread_mutex_unlock(&processedRingLock);
141     
142       p = (ULONG*)&buffer[0]; *p = htonl(2); // stream channel
143       p = (ULONG*)&buffer[4]; *p = htonl(streamID);
144       p = (ULONG*)&buffer[8]; *p = htonl(0); // here insert flag: 0 = ok, data follows
145       p = (ULONG*)&buffer[12]; *p = htonl(amountReceived);
146
147       tcp->sendPacket(buffer, amountReceived + headerLength);
148     } while(processed.getContent() >= streamChunkSize);
149   }  
150 }
151
152 void MVPReceiver::sendStreamEnd()
153 {
154   ULONG *p;
155   ULONG bufferLength = sizeof(ULONG) * 4;
156   UCHAR buffer[bufferLength];
157   p = (ULONG*)&buffer[0]; *p = htonl(2); // stream channel
158   p = (ULONG*)&buffer[4]; *p = htonl(streamID);
159   p = (ULONG*)&buffer[8]; *p = htonl(1); // stream end
160   p = (ULONG*)&buffer[12]; *p = htonl(0); // zero length, no more data
161   tcp->sendPacket(buffer, bufferLength);
162 }
163
164
165 int *MVPReceiver::mergeSpidsTpid(const int *spids,int tpid)
166 {
167   int *destpids;
168   const int *runspid=spids;
169   for (runspid=spids,destpids=mergedSpidsTpid;*runspid;runspid++,destpids++) {
170        *destpids=*runspid;
171   }
172   *destpids=tpid;
173   destpids++;
174   *destpids=0;
175   return mergedSpidsTpid;
176 }
177