Skip to content

Commit a13fe40

Browse files
committed
fix: multithreading issue.
Remove parameter ""-mpegts_flags nit" in ffmpeghandler
1 parent 8a737be commit a13fe40

File tree

7 files changed

+30
-10
lines changed

7 files changed

+30
-10
lines changed

Diff for: device.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ cIptvDevice::cIptvDevice(unsigned int indexP)
2121
sidScanEnabledM(false),
2222
pidScanEnabledM(false),
2323
channelM() {
24+
2425
unsigned int bufsize = (unsigned int) IPTV_BUFFER_SIZE;
2526
bufsize -= (bufsize % TS_SIZE);
2627
info("Creating IPTV device %d (CardIndex=%d)", deviceIndexM, CardIndex());
@@ -46,6 +47,8 @@ cIptvDevice::cIptvDevice(unsigned int indexP)
4647
AttachFilter(pSidScannerM = new cSidScanner());
4748
// Check if dvr fifo exists
4849
struct stat sb;
50+
memset(&sb, 0, sizeof(struct stat));
51+
4952
cString filename = cString::sprintf(IPTV_DVR_FILENAME, deviceIndexM);
5053
stat(filename, &sb);
5154
if (S_ISFIFO(sb.st_mode)) {

Diff for: ffmpeghandler.cpp

+12-8
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,8 @@ std::vector<std::string> FFmpegHandler::prepareStreamCmdVideo(const m3u_stream&
138138
callStr.emplace_back("-mpegts_flags");
139139
callStr.emplace_back("system_b");
140140

141-
callStr.emplace_back("-mpegts_flags");
142-
callStr.emplace_back("nit");
141+
// callStr.emplace_back("-mpegts_flags");
142+
// callStr.emplace_back("nit");
143143

144144
callStr.emplace_back("-metadata");
145145
callStr.emplace_back("service_name=" + stream.channelName);
@@ -216,8 +216,8 @@ std::vector<std::string> FFmpegHandler::prepareStreamCmdAudio(const m3u_stream&
216216
callStr.emplace_back("-mpegts_flags");
217217
callStr.emplace_back("system_b");
218218

219-
callStr.emplace_back("-mpegts_flags");
220-
callStr.emplace_back("nit");
219+
// callStr.emplace_back("-mpegts_flags");
220+
// callStr.emplace_back("nit");
221221

222222
callStr.emplace_back("-metadata");
223223
callStr.emplace_back("service_name=" + stream.channelName);
@@ -277,7 +277,7 @@ bool FFmpegHandler::streamAudio(const m3u_stream& stream) {
277277

278278
streamHandler = new TinyProcessLib::Process(callStr, "",
279279
[this](const char *bytes, size_t n) {
280-
debug9("Queue size %ld\n", tsPackets.size());
280+
debug9("Add new packets. Current queue size %ld\n", tsPackets.size());
281281

282282
std::lock_guard<std::mutex> guard(queueMutex);
283283
tsPackets.emplace(bytes, n);
@@ -305,6 +305,7 @@ void FFmpegHandler::stop() {
305305
streamHandler = nullptr;
306306
}
307307

308+
std::lock_guard<std::mutex> guard(queueMutex);
308309
std::queue<std::string> empty;
309310
std::swap(tsPackets, empty);
310311
}
@@ -315,17 +316,20 @@ int FFmpegHandler::popPackets(unsigned char* bufferAddrP, unsigned int bufferLen
315316
std::string front = tsPackets.front();
316317

317318
if (bufferLenP < front.size()) {
319+
// this shall never happen. A possible solution is to use a deque instead
320+
// and modify the front element. Unsure, if this is worth the effort, because
321+
// a full buffer points to another problem.
318322
error("WARNING: BufferLen %u < Size %ld\n", bufferLenP, front.size());
319-
return -1;
323+
return 0;
320324
}
321325

322-
debug9("Read from queue, size %ld\n", front.size());
326+
debug9("Read from queue: len %ld, size %ld bytes\n", tsPackets.size(), front.size());
323327

324328
memcpy(bufferAddrP, front.data(), front.size());
325329

326330
tsPackets.pop();
327331
return front.size();
328332
}
329333

330-
return -1;
334+
return 0;
331335
}

Diff for: iptv.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
#endif
2323

2424
const char VERSION[] = "2.4.0" GITVERSION;
25+
static const char DESCRIPTION[] = trNOOP("Experience the IPTV");
26+
27+
const char *cPluginIptv::Description() { return tr(DESCRIPTION); }
2528

2629
cPluginIptv::cPluginIptv() : deviceCountM(1) {
2730
debug16("%s", __PRETTY_FUNCTION__);

Diff for: iptv.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#include "device.h"
1414
#include "iptvservice.h"
1515

16-
static const char DESCRIPTION[] = trNOOP("Experience the IPTV");
16+
1717

1818
class cPluginIptv : public cPlugin {
1919
private:
@@ -25,7 +25,7 @@ class cPluginIptv : public cPlugin {
2525
~cPluginIptv() override;
2626

2727
const char *Version() override { return VERSION; }
28-
const char *Description() override { return tr(DESCRIPTION); }
28+
const char *Description() override;
2929
const char *CommandLineHelp() override;
3030

3131
bool ProcessArgs(int argc, char *argv[]) override;

Diff for: protocolm3u.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ bool cIptvProtocolM3U::Open() {
3030

3131
auto streams = m3u8Handler.parseM3u(url);
3232
if (streams.width == 0 || streams.height == 0) {
33+
debug1("Unable to read URL '%s'\n", url.c_str());
34+
3335
isActiveM = false;
3436
return false;
3537
}
@@ -68,6 +70,7 @@ bool cIptvProtocolM3U::Close() {
6870

6971
isActiveM = false;
7072
handler.stop();
73+
7174
return true;
7275
}
7376

Diff for: streamer.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ void cIptvStreamer::Action() {
6666
}
6767

6868
bool cIptvStreamer::Open() {
69+
std::lock_guard<std::mutex> guard(streamerMutex);
6970
debug1("%s", __PRETTY_FUNCTION__);
7071

7172
// Open the protocol
@@ -74,10 +75,12 @@ bool cIptvStreamer::Open() {
7475

7576
// Start thread
7677
Start();
78+
7779
return true;
7880
}
7981

8082
bool cIptvStreamer::Close() {
83+
std::lock_guard<std::mutex> guard(streamerMutex);
8184
debug1("%s", __PRETTY_FUNCTION__);
8285

8386
// Stop thread
@@ -96,6 +99,7 @@ bool cIptvStreamer::Close() {
9699
}
97100

98101
bool cIptvStreamer::SetSource(const char *locationP, const int parameterP, const int indexP, cIptvProtocolIf *protocolP, int channelNumber) {
102+
std::lock_guard<std::mutex> guard(streamerMutex);
99103
debug1("%s (%s, %d, %d, ChannelNumber: %d)", __PRETTY_FUNCTION__, locationP, parameterP, indexP, channelNumber);
100104

101105
if (!isempty(locationP)) {

Diff for: streamer.h

+3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#pragma once
99

10+
#include <mutex>
1011
#include <arpa/inet.h>
1112

1213
#include <vdr/thread.h>
@@ -23,6 +24,8 @@ class cIptvStreamer : public cThread, public cIptvStreamerStatistics {
2324
unsigned int packetBufferLenM;
2425
cIptvProtocolIf *protocolM;
2526

27+
std::mutex streamerMutex;
28+
2629
protected:
2730
void Action() override;
2831

0 commit comments

Comments
 (0)