/
asistreamhandler.cpp
406 lines (337 loc) · 9.7 KB
/
asistreamhandler.cpp
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
// -*- Mode: c++ -*-
// POSIX headers
#include <fcntl.h>
#include <unistd.h>
#ifndef USING_MINGW
#include <sys/select.h>
#include <sys/ioctl.h>
#endif
// Qt headers
#include <QString>
#include <QFile>
// MythTV headers
#include "asistreamhandler.h"
#include "asichannel.h"
#include "ThreadedFileWriter.h"
#include "dtvsignalmonitor.h"
#include "streamlisteners.h"
#include "mpegstreamdata.h"
#include "cardutil.h"
// DVEO ASI headers
#include <dveo/asi.h>
#include <dveo/master.h>
#define LOC QString("ASISH(%1): ").arg(_device)
#define LOC_WARN QString("ASISH(%1) Warning: ").arg(_device)
#define LOC_ERR QString("ASISH(%1) Error: ").arg(_device)
QMap<QString,ASIStreamHandler*> ASIStreamHandler::_handlers;
QMap<QString,uint> ASIStreamHandler::_handlers_refcnt;
QMutex ASIStreamHandler::_handlers_lock;
ASIStreamHandler *ASIStreamHandler::Get(const QString &devname)
{
QMutexLocker locker(&_handlers_lock);
QString devkey = devname;
QMap<QString,ASIStreamHandler*>::iterator it = _handlers.find(devkey);
if (it == _handlers.end())
{
ASIStreamHandler *newhandler = new ASIStreamHandler(devname);
newhandler->Open();
_handlers[devkey] = newhandler;
_handlers_refcnt[devkey] = 1;
LOG(VB_RECORD, LOG_INFO,
QString("ASISH: Creating new stream handler %1 for %2")
.arg(devkey).arg(devname));
}
else
{
_handlers_refcnt[devkey]++;
uint rcount = _handlers_refcnt[devkey];
LOG(VB_RECORD, LOG_INFO,
QString("ASISH: Using existing stream handler %1 for %2")
.arg(devkey)
.arg(devname) + QString(" (%1 in use)").arg(rcount));
}
return _handlers[devkey];
}
void ASIStreamHandler::Return(ASIStreamHandler * & ref)
{
QMutexLocker locker(&_handlers_lock);
QString devname = ref->_device;
QMap<QString,uint>::iterator rit = _handlers_refcnt.find(devname);
if (rit == _handlers_refcnt.end())
return;
if (*rit > 1)
{
ref = NULL;
(*rit)--;
return;
}
QMap<QString,ASIStreamHandler*>::iterator it = _handlers.find(devname);
if ((it != _handlers.end()) && (*it == ref))
{
LOG(VB_RECORD, LOG_INFO, QString("ASISH: Closing handler for %1")
.arg(devname));
ref->Close();
delete *it;
_handlers.erase(it);
}
else
{
LOG(VB_GENERAL, LOG_ERR,
QString("ASISH Error: Couldn't find handler for %1")
.arg(devname));
}
_handlers_refcnt.erase(rit);
ref = NULL;
}
ASIStreamHandler::ASIStreamHandler(const QString &device) :
StreamHandler(device),
_device_num(-1), _buf_size(-1), _fd(-1),
_packet_size(TSPacket::kSize), _clock_source(kASIInternalClock),
_rx_mode(kASIRXSyncOnActualConvertTo188), _drb(NULL), _mpts(NULL)
{
}
void ASIStreamHandler::SetClockSource(ASIClockSource cs)
{
_clock_source = cs;
// TODO we should make it possible to set this immediately
// not wait for the next open
}
void ASIStreamHandler::SetRXMode(ASIRXMode m)
{
_rx_mode = m;
// TODO we should make it possible to set this immediately
// not wait for the next open
}
void ASIStreamHandler::SetRunningDesired(bool desired)
{
if (_drb && _running_desired && !desired)
_drb->Stop();
StreamHandler::SetRunningDesired(desired);
}
void ASIStreamHandler::run(void)
{
LOG(VB_RECORD, LOG_INFO, LOC + "run(): begin");
if (!Open())
{
LOG(VB_GENERAL, LOG_ERR, LOC + QString("Failed to open device %1 : %2")
.arg(_device).arg(strerror(errno)));
_error = true;
return;
}
DeviceReadBuffer *drb = new DeviceReadBuffer(this);
bool ok = drb->Setup(_device, _fd, _packet_size, _buf_size);
if (!ok)
{
LOG(VB_GENERAL, LOG_ERR, LOC + "Failed to allocate DRB buffer");
delete drb;
drb = NULL;
Close();
_error = true;
return;
}
uint buffer_size = _packet_size * 15000;
unsigned char *buffer = new unsigned char[buffer_size];
if (!buffer)
{
LOG(VB_GENERAL, LOG_ERR, LOC + "Failed to allocate buffer");
delete drb;
drb = NULL;
Close();
_error = true;
return;
}
memset(buffer, 0, buffer_size);
SetRunning(true, true, false);
drb->Start();
{
QMutexLocker locker(&_start_stop_lock);
_drb = drb;
}
int remainder = 0;
while (_running_desired && !_error)
{
UpdateFiltersFromStreamData();
ssize_t len = 0;
len = drb->Read(
&(buffer[remainder]), buffer_size - remainder);
if (!_running_desired)
break;
// Check for DRB errors
if (drb->IsErrored())
{
LOG(VB_GENERAL, LOG_ERR, LOC + "Device error detected");
_error = true;
}
if (drb->IsEOF())
{
LOG(VB_GENERAL, LOG_ERR, LOC + "Device EOF detected");
_error = true;
}
if ((0 == len) || (-1 == len))
{
usleep(100);
continue;
}
len += remainder;
if (len < 10) // 10 bytes = 4 bytes TS header + 6 bytes PES header
{
remainder = len;
continue;
}
if (!_listener_lock.tryLock())
{
remainder = len;
continue;
}
if (_stream_data_list.empty())
{
_listener_lock.unlock();
continue;
}
StreamDataList::const_iterator sit = _stream_data_list.begin();
for (; sit != _stream_data_list.end(); ++sit)
remainder = sit.key()->ProcessData(buffer, len);
if (_mpts != NULL)
_mpts->Write(buffer, len - remainder);
_listener_lock.unlock();
if (remainder > 0 && (len > remainder)) // leftover bytes
memmove(buffer, &(buffer[len - remainder]), remainder);
}
LOG(VB_RECORD, LOG_INFO, LOC + "run(): " + "shutdown");
RemoveAllPIDFilters();
{
QMutexLocker locker(&_start_stop_lock);
_drb = NULL;
}
if (drb->IsRunning())
drb->Stop();
delete drb;
delete[] buffer;
Close();
LOG(VB_RECORD, LOG_INFO, LOC + "run(): " + "end");
SetRunning(false, true, false);
}
bool ASIStreamHandler::Open(void)
{
if (_fd >= 0)
return true;
QString error;
_device_num = CardUtil::GetASIDeviceNumber(_device, &error);
if (_device_num < 0)
{
LOG(VB_GENERAL, LOG_ERR, LOC + error);
return false;
}
_buf_size = CardUtil::GetASIBufferSize(_device_num, &error);
if (_buf_size <= 0)
{
LOG(VB_GENERAL, LOG_ERR, LOC + error);
return false;
}
if (!CardUtil::SetASIMode(_device_num, (uint)_rx_mode, &error))
{
LOG(VB_GENERAL, LOG_ERR, LOC + "Failed to set RX Mode: " + error);
return false;
}
// actually open the device
_fd = open(_device.toLocal8Bit().constData(), O_RDONLY, 0);
if (_fd < 0)
{
LOG(VB_GENERAL, LOG_ERR, LOC +
QString("Failed to open '%1'").arg(_device) + ENO);
return false;
}
// get the rx capabilities
unsigned int cap;
if (ioctl(_fd, ASI_IOC_RXGETCAP, &cap) < 0)
{
LOG(VB_GENERAL, LOG_ERR, LOC +
QString("Failed to query capabilities '%1'").arg(_device) + ENO);
Close();
return false;
}
// TODO? do stuff with capabilities..
// we need to handle 188 & 204 byte packets..
switch (_rx_mode)
{
case kASIRXRawMode:
case kASIRXSyncOnActualSize:
_packet_size = TSPacket::kDVBEmissionSize * TSPacket::kSize;
break;
case kASIRXSyncOn204:
_packet_size = TSPacket::kDVBEmissionSize;
break;
case kASIRXSyncOn188:
case kASIRXSyncOnActualConvertTo188:
case kASIRXSyncOn204ConvertTo188:
_packet_size = TSPacket::kSize;
break;
}
// pid counter?
return _fd >= 0;
}
void ASIStreamHandler::Close(void)
{
if (_fd >= 0)
{
close(_fd);
_fd = -1;
}
}
typedef ThreadedFileWriter* ThreadedFileWriterP;
static bool named_output_file_common(
const QString &_device, ThreadedFileWriterP &tfw, QMap<QString,int> &files)
{
if (tfw)
{
delete tfw;
tfw = NULL;
}
QMap<QString,int>::iterator it = files.begin();
if (it == files.end())
return true;
for (it = files.begin(); it != files.end(); ++it)
(*it)++;
QString fn = QString("%1.%2.raw")
.arg(files.begin().key()).arg(*files.begin());
tfw = new ThreadedFileWriter(
fn, O_WRONLY|O_TRUNC|O_CREAT|O_LARGEFILE, 0644);
if (!tfw->Open())
{
delete tfw;
tfw = NULL;
return false;
}
bool ok = true;
const QByteArray ba = fn.toLocal8Bit();
for (; ok && it != files.end(); ++it)
{
int ret = link(ba.constData(), it.key().toLocal8Bit().constData());
if (ret < 0)
{
LOG(VB_GENERAL, LOG_ERR, LOC +
QString("Failed to link '%1' to '%2'").arg(it.key()).arg(fn) +
ENO);
}
ok &= ret >= 0;
}
return ok;
}
void ASIStreamHandler::AddNamedOutputFile(const QString &file)
{
_mpts_files[file] = -1;
named_output_file_common(_device, _mpts, _mpts_files);
}
void ASIStreamHandler::RemoveNamedOutputFile(const QString &file)
{
QMap<QString,int>::iterator it = _mpts_files.find(file);
if (it != _mpts_files.end())
{
_mpts_files.erase(it);
named_output_file_common(_device, _mpts, _mpts_files);
}
}
void ASIStreamHandler::PriorityEvent(int fd)
{
// TODO report on buffer overruns, etc.
}