forked from rofafor/vdr-plugin-iptv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstreamer.cpp
193 lines (149 loc) · 4.61 KB
/
streamer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
* streamer.c: IPTV plugin for the Video Disk Recorder
*
* See the README file for copyright information and how to reach the author.
*
*/
#include <vdr/plugin.h>
#include "common.h"
#include "log.h"
#include "streamer.h"
cIptvStreamer::cIptvStreamer(cIptvDeviceIf &deviceP, unsigned int packetLenP)
: cThread("IPTV streamer"),
sleepM(),
deviceM(&deviceP),
packetBufferLenM(packetLenP),
protocolM(nullptr),
radioImage(nullptr) {
debug1("%s (, %d)", __PRETTY_FUNCTION__, packetBufferLenM);
// Allocate packet buffer
packetBufferM = MALLOC(unsigned char, packetBufferLenM);
if (packetBufferM) {
memset(packetBufferM, 0, packetBufferLenM);
} else {
error("MALLOC() failed for packet buffer");
}
}
cIptvStreamer::~cIptvStreamer() {
debug1("%s", __PRETTY_FUNCTION__);
// Close the protocol
Close();
protocolM = nullptr;
// Free allocated memory
free(packetBufferM);
}
void cIptvStreamer::Action() {
debug1("%s() Entering", __PRETTY_FUNCTION__);
// Increase priority
//SetPriority(-1);
// Do the thread loop
while (packetBufferM && Running()) {
int length = -1;
unsigned int size = min(deviceM->CheckData(), packetBufferLenM);
if (protocolM && (size > 0)) {
length = protocolM->Read(packetBufferM, size);
}
if (length > 0) {
AddStreamerStatistic(length);
deviceM->WriteData(packetBufferM, length);
} else {
sleepM.Wait(10); // to avoid busy loop and reduce cpu load
}
}
debug1("%s Exiting", __PRETTY_FUNCTION__);
}
bool cIptvStreamer::Open() {
// printf("Vor Open Mutex\n");
// std::lock_guard<std::mutex> guard(streamerMutex);
// printf("Nach Open Mutex\n");
debug1("%s", __PRETTY_FUNCTION__);
// Open the protocol
if (protocolM && !protocolM->Open())
return false;
// check if this is a radio channel (vpid == 0)
bool isRadio = false;
{
LOCK_CHANNELS_READ;
auto c = Channels->GetByNumber(channelNumber);
if (c && c->Vpid() == 0) {
isRadio = true;
}
}
// check if the radio plugin exists
bool hasRadioPlugin = false;
auto p = cPluginManager::GetPlugin("radio");
if (p) {
hasRadioPlugin = true;
}
// start RadioImage if this is a radio channel and radio plugin does not exists
if (isRadio && !hasRadioPlugin) {
if (radioImage != nullptr) {
radioImage->Exit();
}
radioImage = new cRadioImage();
cString img = cString::sprintf("%s/%s", IptvConfig.GetResourceDirectory(), "radio.mpg");
radioImage->SetBackgroundImage(*img);
radioImage->Start();
}
// Start thread
Start();
return true;
}
bool cIptvStreamer::Close() {
// printf("Vor Close Mutex\n");
// std::lock_guard<std::mutex> guard(streamerMutex);
// printf("Nach close Mutex\n");
debug1("%s", __PRETTY_FUNCTION__);
// Stop thread
sleepM.Signal();
if (Running()) {
Cancel(3);
}
// Close the protocol
if (protocolM) {
protocolM->Close();
}
return true;
}
bool cIptvStreamer::SetSource(cIptvProtocolIf *protocolP, SourceParameter parameter) {
// printf("Vor SetSource Mutex\n");
// std::lock_guard<std::mutex> guard(streamerMutex);
// printf("Nach SetSource Mutex\n");
debug1("%s (%s, %d, %d, ChannelNumber: %d)", __PRETTY_FUNCTION__, parameter.locationP, parameter.parameterP, parameter.indexP, parameter.channelNumber);
if (!isempty(parameter.locationP)) {
channelNumber = parameter.channelNumber;
// Update protocol and set location and parameter; Close the existing one if changed
if (protocolM!=protocolP) {
if (protocolM) {
protocolM->Close();
}
protocolM = protocolP;
if (protocolM) {
if (!protocolM->SetSource(parameter)) {
return false;
}
protocolM->Open();
}
} else if (protocolM) {
if (!protocolM->SetSource(parameter)) {
return false;
}
}
}
return true;
}
bool cIptvStreamer::SetPid(int pidP, int typeP, bool onP) {
debug9("%s (%d, %d, %d)", __PRETTY_FUNCTION__, pidP, typeP, onP);
if (protocolM) {
return protocolM->SetPid(pidP, typeP, onP);
}
return true;
}
cString cIptvStreamer::GetInformation() {
debug16("%s", __PRETTY_FUNCTION__);
cString s;
if (protocolM) {
s = protocolM->GetInformation();
}
return s;
}