]> git.vomp.tv Git - vompserver.git/blob - mvpreceiver.c
Fix for moving recordings
[vompserver.git] / mvpreceiver.c
1 #include "mvpreceiver.h"
2
3 MVPReceiver* MVPReceiver::create(cChannel* channel, int priority)
4 {
5 #if VDRVERSNUM < 10500
6    bool NeedsDetachReceivers;
7    cDevice* device = cDevice::GetDevice(channel, priority, &NeedsDetachReceivers);
8 #else
9   cDevice* device = cDevice::GetDevice(channel, priority, true); // last param is live-view
10 #endif
11
12   if (!device)
13   {
14     Log::getInstance()->log("MVPReceiver", Log::DEBUG, "No device found to receive this channel at this priority");
15     return NULL;
16   }
17
18 #if VDRVERSNUM < 10500
19   if (NeedsDetachReceivers)
20   {
21     Log::getInstance()->log("MVPReceiver", Log::DEBUG, "Needs detach receivers");
22
23     // Need to detach other receivers or VDR will shut down??
24   }
25 #endif
26
27   MVPReceiver* m = new MVPReceiver(channel, device);
28   return m;
29 }
30
31 MVPReceiver::MVPReceiver(cChannel* channel, cDevice* device)
32 #if VDRVERSNUM < 10300
33 : cReceiver(channel->Ca(), 0, 7, channel->Vpid(), channel->Ppid(), channel->Apid1(), channel->Apid2(), channel->Dpid1(), channel->Dpid2(), channel->Tpid())
34 #elif VDRVERSNUM < 10500
35 : cReceiver(channel->Ca(), 0, channel->Vpid(), channel->Apids(), channel->Dpids(), channel->Spids())
36 #else
37 : cReceiver(channel->GetChannelID(), 0, channel->Vpid(), channel->Apids(), channel->Dpids(), channel->Spids())
38 #endif
39 {
40   logger = Log::getInstance();
41   vdrActivated = false;
42   inittedOK = 0;
43   streamID = 0;
44   tcp = NULL;
45
46 //  logger->log("MVPReceiver", Log::DEBUG, "Channel has VPID %i APID %i", channel->Vpid(), channel->Apid(0));
47
48   if (!processed.init(1000000)) return;
49   pthread_mutex_init(&processedRingLock, NULL);
50
51   // OK
52
53   inittedOK = 1;
54   device->SwitchChannel(channel, false);
55   device->AttachReceiver(this);
56 }
57
58 int MVPReceiver::init(TCP* ttcp, ULONG tstreamID)
59 {
60   tcp = ttcp;
61   streamID = tstreamID;
62   return inittedOK;
63 }
64
65 MVPReceiver::~MVPReceiver()
66 {
67   Detach();
68   threadStop();
69 }
70
71 void MVPReceiver::Activate(bool on)
72 {
73   vdrActivated = on;
74   if (on) 
75   {
76     logger->log("MVPReceiver", Log::DEBUG, "VDR active");
77     threadStart();
78   }
79   else
80   {
81     logger->log("MVPReceiver", Log::DEBUG, "VDR inactive");
82     threadStop();
83   }
84 }
85
86 bool MVPReceiver::isVdrActivated()
87 {
88   return vdrActivated;
89 }
90
91 void MVPReceiver::Receive(UCHAR* data, int length)
92 {
93   pthread_mutex_lock(&processedRingLock);
94   processed.put(data, length);
95   if (processed.getContent() > streamChunkSize) threadSignal();
96   pthread_mutex_unlock(&processedRingLock);
97 }
98
99 void MVPReceiver::threadMethod()
100 {
101   UCHAR buffer[streamChunkSize + 12];
102   int amountReceived;
103
104 //   threadSetKillable(); ??
105
106   while(1)
107   {
108     threadWaitForSignal();
109     threadCheckExit();
110     
111     do
112     {
113       pthread_mutex_lock(&processedRingLock);
114       amountReceived = processed.get(buffer+12, streamChunkSize);
115       pthread_mutex_unlock(&processedRingLock);
116     
117       *(ULONG*)&buffer[0] = htonl(2); // stream channel
118       *(ULONG*)&buffer[4] = htonl(streamID);
119       *(ULONG*)&buffer[8] = htonl(amountReceived);
120       tcp->sendPacket(buffer, amountReceived + 12);
121     } while(processed.getContent() >= streamChunkSize);
122   }  
123 }
124
125 ULONG MVPReceiver::getBlock(unsigned char* buffer, unsigned long amount)
126 {
127 /*
128   pthread_mutex_lock(&processedRingLock);
129
130   int numTries = 0;
131
132   while ((unsigned long)processed.getContent() < amount)
133   {
134     pthread_mutex_unlock(&processedRingLock);
135     if (++numTries == 30) // 15s
136     {
137       logger->log("MVPReceiver", Log::DEBUG, "getBlock timeout");
138       return 0;
139     }
140     usleep(500000);
141     pthread_mutex_lock(&processedRingLock);
142   }
143
144   unsigned long amountReceived = processed.get(buffer, amount);
145   pthread_mutex_unlock(&processedRingLock);
146   return amountReceived;
147   */
148   sleep(10);
149   return 0;
150 }