aboutsummaryrefslogtreecommitdiffstats
path: root/src/inputs/InputDabplusFile.cpp
blob: b55b3d9ccfa5b40494eee7c7761d06a66b1cf3f7 (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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
   Copyright (C) 2009 Her Majesty the Queen in Right of Canada (Communications
   Research Center Canada)

   Copyright (C) 2013 Matthias P. Braendli
   http://mpb.li

   Base class for all file and fifo-based inputs.

   */
/*
   This file is part of CRC-DabMux.

   CRC-DabMux is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as
   published by the Free Software Foundation, either version 3 of the
   License, or (at your option) any later version.

   CRC-DabMux is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with CRC-DabMux.  If not, see <http://www.gnu.org/licenses/>.
   */

#include "inputs/Input.h"

#ifdef HAVE_FORMAT_DABPLUS
#ifdef HAVE_INPUT_FILE

#include "mpeg.h"
#include "TcpLog.h"

#include <string>
#include <vector>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <stdint.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>

#ifndef _WIN32
#   define O_BINARY 0
#endif

extern TcpLog etiLog;

int InputDabplusFile::Read(void* buffer, int size)
{
    if (this->buffer.size() != size * 5) {
        this->buffer.resize(size * 5);
        this->bufferIndex = 0;
    }

    if (this->bufferIndex + size > this->buffer.size()) {
        int ret = read(this->file, &this->buffer[0], this->buffer.size());
        if (ret != this->buffer.size()) {
            if (ret != 0) {
                etiLog.print(TcpLog::CRIT, "ERROR: Incomplete DAB+ frame!\n");
            }
            return 0;
        }
        this->bufferIndex = 0;
    }

    memcpy(buffer, &this->buffer[this->bufferIndex], size);
    this->bufferIndex += size;

    return size;
}

int InputDabplusFile::ReadFrame(void* buffer, int size)
{
    int result;
    uint8_t* dataOut = reinterpret_cast<uint8_t*>(buffer);

    result = Read(dataOut, size);
    if (result == -1) {
        etiLog.print(TcpLog::CRIT, "ERROR: Can't read file\n");
        perror("");
        return -1;
    }
    if (result < size) {
        int sizeOut = result;
        etiLog.print(TcpLog::NOTICE, "reach end of file -> rewinding\n");
        if (Rewind() == -1) {
            etiLog.print(TcpLog::CRIT, "ERROR: Can't rewind file\n");
            return -1;
        }

        result = Read(dataOut + sizeOut, size - sizeOut);
        if (result == -1) {
            etiLog.print(TcpLog::CRIT, "ERROR: Can't read file\n");
            perror("");
            return -1;
        }

        if (result < size) {
            etiLog.print(TcpLog::CRIT, "ERROR: Not enought data in file\n");
            return -1;
        }
    }

    return size;
}

#endif
#endif