]> git.vomp.tv Git - vompserver.git/blob - mvpreceiver.c
Updates for media player after protocol change
[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 #elif VDRVERSNUM < 10500
29 : cReceiver(channel->Ca(), 0, channel->Vpid(), channel->Apids(), channel->Dpids(), channel->Spids())
30 #else
31 : cReceiver(channel->GetChannelID(), 0, channel->Vpid(), channel->Apids(), channel->Dpids(), channel->Spids())
32 #endif
33 {
34   logger = Log::getInstance();
35   vdrActivated = false;
36   inittedOK = 0;
37
38 //  logger->log("MVPReceiver", Log::DEBUG, "Channel has VPID %i APID %i", channel->Vpid(), channel->Apid(0));
39
40   if (!processed.init(1000000)) return;
41   pthread_mutex_init(&processedRingLock, NULL);
42
43   // OK
44
45   inittedOK = 1;
46   device->SwitchChannel(channel, false);
47   device->AttachReceiver(this);
48 }
49
50 int MVPReceiver::init()
51 {
52   return inittedOK;
53 }
54
55 MVPReceiver::~MVPReceiver()
56 {
57   Detach();
58 }
59
60 void MVPReceiver::Activate(bool on)
61 {
62   vdrActivated = on;
63   if (on) logger->log("MVPReceiver", Log::DEBUG, "VDR active");
64   else logger->log("MVPReceiver", Log::DEBUG, "VDR inactive");
65 }
66
67 bool MVPReceiver::isVdrActivated()
68 {
69   return vdrActivated;
70 }
71
72 void MVPReceiver::Receive(UCHAR* data, int length)
73 {
74   pthread_mutex_lock(&processedRingLock);
75   processed.put(data, length);
76   pthread_mutex_unlock(&processedRingLock);
77 }
78
79 unsigned long MVPReceiver::getBlock(unsigned char* buffer, unsigned long amount)
80 {
81   pthread_mutex_lock(&processedRingLock);
82
83   int numTries = 0;
84
85   while ((unsigned long)processed.getContent() < amount)
86   {
87     pthread_mutex_unlock(&processedRingLock);
88     if (++numTries == 30) // 15s
89     {
90       logger->log("MVPReceiver", Log::DEBUG, "getBlock timeout");
91       return 0;
92     }
93     usleep(500000);
94     pthread_mutex_lock(&processedRingLock);
95   }
96
97   unsigned long amountReceived = processed.get(buffer, amount);
98   pthread_mutex_unlock(&processedRingLock);
99   return amountReceived;
100 }