12 #include <sys/ioctl.h>
64 #define MIN_PRE_1_3_19_PRIVATESTREAM 10
113 esyslog(
"ERROR: too many devices!");
128 for (time_t t0 = time(NULL); time(NULL) - t0 < Timeout; ) {
156 esyslog(
"ERROR: invalid value in nextCardIndex(%d)", n);
191 isyslog(
"setting primary device to %d", n + 1);
200 esyslog(
"ERROR: invalid primary device number: %d", n + 1);
229 int MaxNumProvidedSystems = (1 << AvailableBits) - 1;
231 if (NumProvidedSystems > MaxNumProvidedSystems) {
232 esyslog(
"ERROR: device %d supports %d modulation systems but cDevice::GetDevice() currently only supports %d delivery systems which should be fixed", Device->
CardIndex() + 1, NumProvidedSystems, MaxNumProvidedSystems);
233 NumProvidedSystems = MaxNumProvidedSystems;
235 else if (NumProvidedSystems <= 0) {
236 esyslog(
"ERROR: device %d reported an invalid number (%d) of supported delivery systems - assuming 1", Device->
CardIndex() + 1, NumProvidedSystems);
237 NumProvidedSystems = 1;
239 return NumProvidedSystems;
246 int SlotPriority[NumCamSlots];
247 int NumUsableSlots = 0;
248 bool InternalCamNeeded =
false;
262 InternalCamNeeded =
true;
265 bool NeedsDetachReceivers =
false;
269 uint32_t Impact = 0xFFFFFFFF;
270 for (
int j = 0; j < NumCamSlots || !NumUsableSlots; j++) {
271 if (NumUsableSlots && SlotPriority[j] >
MAXPRIORITY)
277 if (InternalCamNeeded && !HasInternalCam)
299 imp <<= 1; imp |= ndr;
300 imp <<= 1; imp |= (NumUsableSlots || InternalCamNeeded) ? 0 :
device[i]->
HasCi();
308 NeedsDetachReceivers = ndr;
309 if (NumUsableSlots && !HasInternalCam)
318 if (NeedsDetachReceivers)
340 if (d->IsTunedToTransponder(Channel))
342 if (d->ProvidesTransponder(Channel)) {
343 if (d->MaySwitchTransponder(Channel))
345 else if (!d->Occupied() && d->MaySwitchTransponder(Channel)) {
346 if (d->Priority() < Priority && (!Device || d->
Priority() < Device->
Priority()))
382 int fd = open(FileName, O_WRONLY | O_CREAT | O_NOFOLLOW | O_TRUNC, DEFFILEMODE);
387 if (
safe_write(fd, Image, ImageSize) == ImageSize)
388 isyslog(
"grabbed image to %s", FileName);
413 switch (VideoDisplayFormat) {
423 default:
esyslog(
"ERROR: invalid value for VideoDisplayFormat '%d'", VideoDisplayFormat);
466 if (Pid || PidType ==
ptPcr) {
469 if (PidType !=
ptPcr) {
506 esyslog(
"ERROR: no free slot for PID %d on device %d", Pid,
CardIndex() + 1);
530 if (Pid || PidType ==
ptPcr) {
532 if (PidType ==
ptPcr)
605 return safe_read(Handle, Buffer, Length);
701 for (
int i = 3; i--;) {
703 case scrOk:
return true;
709 default:
esyslog(
"ERROR: invalid return value from SetChannel");
719 Direction =
sgn(Direction);
729 n = channel->
Number() + Direction;
734 dsyslog(
"skipped channel %d", first);
736 dsyslog(
"skipped channels %d..%d", first, n -
sgn(d));
758 bool NeedsTransferMode = Device !=
this;
765 if (NeedsTransferMode) {
801 if (Result ==
scrOk) {
814 if (!NeedsTransferMode)
836 return Seconds > 0 ? Seconds : 0;
905 return (0 <= c && c <= 2) ? c : 0;
910 if (0 <= AudioChannel && AudioChannel <= 2)
931 if (DescriptionsOnly) {
975 esyslog(
"ERROR: SetAvailableTrack called with invalid Type/Index (%d/%d)", Type, Index);
987 for (
int i = FirstTrack; i <= LastTrack; i++) {
1040 if (TrackId && TrackId->
id) {
1056 int PreferredAudioChannel = 0;
1057 int LanguagePreference = -1;
1060 for (
int i = StartCheck; i <= EndCheck; i++) {
1065 PreferredAudioChannel = pos;
1076 dsyslog(
"setting audio track to %d (%d)", PreferredTrack, PreferredAudioChannel);
1089 int LanguagePreference = INT_MAX;
1152 if (Data[0] == 0x47) {
1158 int Pid =
TsPid(Data);
1168 int NewSize = Size + l;
1169 if (
uchar *NewBuffer = (
uchar *)realloc(buf, NewSize)) {
1172 memcpy(buf + Offset, p, l);
1190 int NewSize = Size + l;
1191 if (
uchar *NewBuffer = (
uchar *)realloc(buf, NewSize)) {
1194 memcpy(buf + Offset, p, l);
1197 esyslog(
"ERROR: out of memory");
1240 if (Player &&
player == Player) {
1295 bool FirstLoop =
true;
1298 const uchar *End = Start + Length;
1299 while (Start < End) {
1300 int d = End -
Start;
1319 if (Data[8] == 0x24 && Data[45] >= 0x10 && Data[45] < 0x20) {
1324 int PayloadOffset = Data[8] + 9;
1327 if ((Data[7] & 0x01) && (Data[PayloadOffset - 3] & 0x81) == 0x01 && Data[PayloadOffset - 2] == 0x81)
1330 uchar SubStreamId = Data[PayloadOffset];
1331 uchar SubStreamType = SubStreamId & 0xF0;
1332 uchar SubStreamIndex = SubStreamId & 0x1F;
1335 pre_1_3_19_PrivateStreamDetected:
1338 SubStreamType = 0x80;
1343 switch (SubStreamType) {
1374 dsyslog(
"switching to pre 1.3.19 Dolby Digital compatibility mode - substream id = %02X", SubStreamId);
1377 goto pre_1_3_19_PrivateStreamDetected;
1390 esyslog(
"ERROR: incomplete PES packet write!");
1391 return Start == Data ? w : Start - Data;
1406 while (i <= Length - 6) {
1407 if (Data[i] == 0x00 && Data[i + 1] == 0x00 && Data[i + 2] == 0x01) {
1409 if (i + l > Length) {
1410 esyslog(
"ERROR: incomplete PES packet!");
1417 return i == 0 ? w : i;
1423 esyslog(
"ERROR: leftover PES data!");
1486 esyslog(
"ERROR: skipped %d bytes of TS fragment", Length);
1495 esyslog(
"ERROR: skipped %d bytes to sync on start of TS packet", Skipped);
1496 return Played + Skipped;
1498 int Pid =
TsPid(Data);
1501 if (PayloadOffset <
TS_SIZE) {
1510 return Played ? Played : w;
1518 return Played ? Played : w;
1533 if ((l > 45) && (p[0] == 0x00) && (p[1] == 0x00) && (p[2] == 0x01) && (p[3] == 0xbd) && (p[8] == 0x24) && (p[45] >= 0x10) && (p[45] < 0x20))
1544 return Played ? Played : w;
1584 #define TS_SCRAMBLING_TIMEOUT 3 // seconds to wait until a TS becomes unscrambled
1585 #define TS_SCRAMBLING_TIME_OK 10 // seconds before a Channel/CAM combination is marked as known to decrypt
1597 bool DetachReceivers =
false;
1598 bool DescramblingOk =
false;
1599 int CamSlotNumber = 0;
1603 if (CamSlotNumber) {
1608 DetachReceivers =
true;
1611 DescramblingOk =
true;
1620 if (DetachReceivers) {
1658 if (Receiver->
device ==
this)
1662 #ifdef WAIT_FOR_TUNER_LOCK
1663 #define TUNER_LOCK_TIMEOUT 5000 // ms
1664 if (!
HasLock(TUNER_LOCK_TIMEOUT)) {
1665 esyslog(
"ERROR: device %d has no lock, can't attach receiver!",
CardIndex() + 1);
1672 for (
int n = 0; n < Receiver->
numPids; n++) {
1692 esyslog(
"ERROR: no free receiver slot!");
1698 if (!Receiver || Receiver->
device !=
this)
1700 bool receiversLeft =
false;
1709 for (
int n = 0; n < Receiver->
numPids; n++)
1713 receiversLeft =
true;
1727 if (Receiver && Receiver->
WantsPid(Pid))
1763 bool firstRead =
true;
1766 if (firstRead || Poller.
Poll(100)) {
1770 if (errno == EOVERFLOW)
1792 for (
int i = 1; i < Count; i++) {
1799 esyslog(
"ERROR: skipped %d bytes to sync on TS packet on device %d", Count,
cardIndex);