summaryrefslogtreecommitdiffstats
path: root/src/dabInputRawFifo.cpp
blob: 0da9727216877574548347f051a458e07cca374c (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
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
/*
   Copyright (C) 2009 Her Majesty the Queen in Right of Canada (Communications
   Research Center Canada)
   */
/*
   This file is part of ODR-DabMux.

   ODR-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.

   ODR-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 ODR-DabMux.  If not, see <http://www.gnu.org/licenses/>.
   */

#include "dabInputRawFifo.h"

#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
#include <string.h>

#ifndef _WIN32
#   define O_BINARY 0
#endif


#ifdef HAVE_FORMAT_RAW
#   ifdef HAVE_INPUT_FILE


struct dabInputOperations dabInputRawFifoOperations = {
    dabInputRawFifoInit,
    dabInputRawFifoOpen,
    dabInputRawFifoSetbuf,
    dabInputRawFifoRead,
    NULL,
    NULL,
    dabInputRawFifoReadFrame,
    dabInputSetbitrate,
    dabInputRawFifoClose,
    dabInputRawFifoClean,
    dabInputRawFifoRewind
};


int dabInputRawFifoInit(void** args)
{
    dabInputRawFifoData* data = new dabInputRawFifoData;
    data->file = -1;
    data->buffer = NULL;
    data->bufferSize = 0;
    data->bufferOffset = 0;

    *args = data;
    return 0;
}


int dabInputRawFifoOpen(void* args, const char* filename)
{
    dabInputRawFifoData* data = (dabInputRawFifoData*)args;
    data->file = open(filename, O_RDONLY | O_BINARY | O_NONBLOCK);
    if (data->file == -1) {
        perror(filename);
        return -1;
    }
#ifdef _WIN32
#else
    int flags = fcntl(data->file, F_GETFL);
    if (flags == -1) {
        perror(filename);
        return -1;
    }
    if (fcntl(data->file, F_SETFL, flags & ~O_NONBLOCK) == -1) {
        perror(filename);
        return -1;
    }
#endif

    return 0;
}


int dabInputRawFifoSetbuf(void* args, int size)
{
    dabInputRawFifoData* data = (dabInputRawFifoData*)args;

    if (size <= 0) {
        return size;
    }

    if (data->bufferSize != (size_t)size) {
        if (data->buffer != NULL) {
            delete[] data->buffer;
        }
        data->buffer = new uint8_t[size];
        data->bufferSize = size;
        data->bufferOffset = 0;
    }

    return 0;
}


int dabInputRawFifoRead(void* args, void* buffer, int size)
{
    dabInputRawFifoData* data = (dabInputRawFifoData*)args;

    return read(data->file, buffer, size);
}


int dabInputRawFifoReadFrame(dabInputOperations* ops, void* args,
        void* buffer, int size)
{
    dabInputRawFifoData* data = (dabInputRawFifoData*)args;
    int result;

    result = ops->read(args, data->buffer + data->bufferOffset, size - data->bufferOffset);
    if (result == -1) {
        etiLog.log(alert, "ERROR: Can't read fifo\n");
        perror("");
        return -1;
    }

    if (result + data->bufferOffset < (size_t)size) {
        data->bufferOffset += result;

        etiLog.log(info, "reach end of fifo -> rewinding\n");
        if (ops->rewind(args) == -1) {
            etiLog.log(alert, "ERROR: Can't rewind fifo\n");
            return -1;
        }

        result = ops->read(args, data->buffer + data->bufferOffset, size - data->bufferOffset);
        if (result == -1) {
            etiLog.log(alert, "ERROR: Can't read fifo\n");
            perror("");
            return -1;
        }

        if (result < size) {
            etiLog.log(alert, "ERROR: Not enought data in fifo\n");
            return 0;
        }
    }

    memcpy(buffer, data->buffer, size);
    data->bufferOffset = 0;

    return size;
}


int dabInputRawFifoClose(void* args)
{
    dabInputRawFifoData* data = (dabInputRawFifoData*)args;
    
    if (data->file != -1) {
        close(data->file);
        data->file = -1;
    }
    return 0;
}


int dabInputRawFifoClean(void** args)
{
    dabInputRawFifoData* data = (dabInputRawFifoData*)*args;
    if (data->buffer != NULL) {
        delete[] data->buffer;
    }
    delete data;
    return 0;
}


int dabInputRawFifoRewind(void* args)
{
    dabInputRawFifoData* data = (dabInputRawFifoData*)args;
    return lseek(data->file, 0, SEEK_SET);
}


#   endif
#endif