]> git.vomp.tv Git - vompserver.git/blob - mvpreceiver.c
Removal of remuxer
[vompserver.git] / mvpreceiver.c
1 #include "mvpreceiver.h"
2
3 MVPReceiver* MVPReceiver::create(cChannel* channel, int priority)
4 {
5   bool NeedsDetachReceivers;
6   cDevice* device = cDevice::GetDevice(channel, priority, &NeedsDetachReceivers);
7
8   if (!device)
9   {
10     Log::getInstance()->log("MVPReceiver", Log::DEBUG, "No device found to receive this channel at this priority");
11     return NULL;
12   }
13
14   if (NeedsDetachReceivers)
15   {
16     Log::getInstance()->log("MVPReceiver", Log::DEBUG, "Needs detach receivers");
17
18     // Need to detach other receivers or VDR will shut down
19   }
20
21   MVPReceiver* m = new MVPReceiver(channel, device);
22   return m;
23 }
24
25 MVPReceiver::MVPReceiver(cChannel* channel, cDevice* device)
26 #if VDRVERSNUM < 10300
27 : cReceiver(channel->Ca(), 0, 7, channel->Vpid(), channel->Ppid(), channel->Apid1(), channel->Apid2(), channel->Dpid1(), channel->Dpid2(), channel->Tpid())
28 #else
29 : cReceiver(channel->Ca(), 0, channel->Vpid(), channel->Apids(), channel->Dpids(), channel->Spids())
30 #endif
31 {
32   logger = Log::getInstance();
33   vdrActivated = false;
34   inittedOK = 0;
35
36   logger->log("MVPReceiver", Log::DEBUG, "Channel has VPID %i APID %i", channel->Vpid(), channel->Apid(0));
37
38   if (!processed.init(1000000)) return;
39   pthread_mutex_init(&processedRingLock, NULL);
40
41   // OK
42
43   inittedOK = 1;
44   device->SwitchChannel(channel, false);
45   device->AttachReceiver(this);
46 }
47
48 int MVPReceiver::init()
49 {
50   return inittedOK;
51 }
52
53 MVPReceiver::~MVPReceiver()
54 {
55   Detach();
56 }
57
58 void MVPReceiver::Activate(bool on)
59 {
60   vdrActivated = on;
61   if (on) logger->log("MVPReceiver", Log::DEBUG, "VDR active");
62   else logger->log("MVPReceiver", Log::DEBUG, "VDR inactive");
63 }
64
65 bool MVPReceiver::isVdrActivated()
66 {
67   return vdrActivated;
68 }
69
70 void MVPReceiver::Receive(UCHAR* data, int length)
71 {
72   pthread_mutex_lock(&processedRingLock);
73   processed.put(data, length);
74   pthread_mutex_unlock(&processedRingLock);
75 }
76
77 unsigned long MVPReceiver::getBlock(unsigned char* buffer, unsigned long amount)
78 {
79   pthread_mutex_lock(&processedRingLock);
80
81   int numTries = 0;
82
83   while ((unsigned long)processed.getContent() < amount)
84   {
85     pthread_mutex_unlock(&processedRingLock);
86     if (++numTries == 30) // 15s
87     {
88       logger->log("MVPReceiver", Log::DEBUG, "getBlock timeout");
89       return 0;
90     }
91     usleep(500000);
92     pthread_mutex_lock(&processedRingLock);
93   }
94
95   unsigned long amountReceived = processed.get(buffer, amount);
96   pthread_mutex_unlock(&processedRingLock);
97   return amountReceived;
98 }