summaryrefslogtreecommitdiff
path: root/vdr-checkts.cc
blob: 45ca51c21b9add585bff73ea354975ff4fa86acc (plain)
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
#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <limits.h>
#include "tsstreamerrorcounter.h"

#define TS_READ_BUFFER_SIZE 50

int maxErrors = 0;
const char* recordingDir;

void displayHelp(void)
{
  printf("Usage: vdr-checkts [OPTIONS] RECORDING_DIR\n\n"
         "  -m errors, --max-errors=ERROR Stop scanning if error count reaches ERROR\n"
         "  -h,        --help             print this help and exit\n"
         "\n"
         );
}

int processCliArguments(int argc, char *argv[])
{
  static struct option long_options[] = {
      { "max-errors", required_argument, NULL, 'm' },
      { "help",       no_argument,       NULL, 'h' },
      { NULL,         no_argument,       NULL,  0  }
    };

  int c;
  while ((c = getopt_long(argc, argv, "m:h", long_options, NULL)) != -1) {
        switch (c) {
          case 'm': maxErrors = strtol(optarg, (char**) NULL, 10);
                    if (errno != 0 || maxErrors <= 0) {
                       fprintf(stderr, "Invalid max error argument: %s\n", optarg);
                       return -2;
                       }
                    break;
          case 'h': displayHelp();
                    return -1;
          }
        }
  if (optind != argc - 1)
  {
    displayHelp();
    return -1;
  }
  recordingDir = argv[optind];
  return 0;
}

int checkRecording(void)
{
  int fileNumber = 1;
  cTsStreamErrorCounter tsStreamErrorCounter;
  while(!maxErrors || tsStreamErrorCounter.ErrorCount() < maxErrors) {
    char fileName[PATH_MAX];

    sprintf(fileName, "%s/%.5d.ts", recordingDir, fileNumber);

    FILE *file = fopen64(fileName, "rb");
    if (file) {
      unsigned char buffer[TS_READ_BUFFER_SIZE * TS_SIZE];
      int read;
      while (true) {
        read = fread(buffer, TS_SIZE, TS_READ_BUFFER_SIZE, file);
        if (read <= 0) break;
        tsStreamErrorCounter.CheckTsPackets(buffer, read * TS_SIZE);
        if (maxErrors && tsStreamErrorCounter.ErrorCount() >= maxErrors)
          break;
        }
      }
    else
      break;
    fclose(file);
    fileNumber++;
    }

  printf("Errors: %d\n", tsStreamErrorCounter.ErrorCount());
  return tsStreamErrorCounter.ErrorCount();
}

int main(int argc, char *argv[])
{
  if (int res = processCliArguments(argc, argv) != 0) return res;

  if (checkRecording() == 0)
    return 0;
  else
    return 1;
}