1 // jam job dvb file testing
2 // 1) define TEST_DATA as mpeg ts file "name.ts"
3 // 2) mod record.C Record::resync(), comment out: record_channel->drain_audio()
4 // 3) enable audio delay in audioidevice.C AudioDevice::run_input, #if 0 <-> #if 1
5 //#define TEST_DATA "/tmp/xx.ts"
9 * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 #include "chantables.h"
29 #include "devicedvbinput.h"
30 #include "devicempeginput.h"
31 #include "recordconfig.h"
32 #include "signalstatus.h"
41 #include <linux/dvb/dmx.h>
42 #include <linux/dvb/frontend.h>
43 #include <sys/ioctl.h>
46 DVBInputStatus::DVBInputStatus(DeviceDVBInput *dvb_input)
49 this->dvb_input = dvb_input;
54 DVBInputStatus::~DVBInputStatus()
57 if( signal_status ) signal_status->disconnect();
60 void DVBInputStatus::stop()
68 void DVBInputStatus::start()
75 void DVBInputStatus::lock_dvb()
77 Thread::disable_cancel();
78 dvb_input->dvb_lock->lock("DVBInputStatus::lock_dvb");
81 void DVBInputStatus::unlock_dvb()
83 dvb_input->dvb_lock->unlock();
84 Thread::enable_cancel();
87 void DVBInputStatus::run()
92 dvb_input->status_dev();
102 void DVBInputStatus::update()
104 if( !signal_status ) return;
105 signal_status->lock_window("DVBInputStatus::update");
106 signal_status->update();
107 signal_status->unlock_window();
111 DeviceDVBInput::DeviceDVBInput(const char *name, int no)
112 : DeviceMPEGInput(name, no)
119 dvb_lock = new Mutex("DeviceDVBInput::dvb_lock", 0);
120 sprintf(frontend_path, "%s/frontend%d", name, no);
121 sprintf(demux_path, "%s/demux%d", name, no);
122 sprintf(dvb_path, "%s/dvr%d", name, no);
123 dvb_input_status = new DVBInputStatus(this);
126 DeviceMPEGInput *DeviceDVBInput::
127 NewDVBInput(const char *name, int no)
129 return (DeviceMPEGInput *) new DeviceDVBInput(name, no);
132 DeviceDVBInput::~DeviceDVBInput()
135 delete dvb_input_status;
141 DeviceMPEGInput* DeviceDVBInput::get_mpeg_input(VideoDevice *device)
143 DeviceMPEGInput* mpeg_device = get_mpeg_video(device, NewDVBInput,
144 device->in_config->dvb_in_adapter, device->in_config->dvb_in_device);
145 if( !mpeg_device ) return 0;
146 device->channel->has_subchan = 1;
147 device->channel->has_scanning = 1;
148 device->channel->use_frequency = 1;
149 mpeg_device->set_device_number(0);
150 Channel *input_src = device->new_input_source((char*)"input0");
151 input_src->device_index = 0;
152 input_src->tuner = 0;
156 DeviceMPEGInput* DeviceDVBInput::get_mpeg_input(AudioDevice *device)
158 return get_mpeg_audio(device, NewDVBInput,
159 device->in_config->dvb_in_adapter, device->in_config->dvb_in_device);
163 int DeviceDVBInput::wait_signal(int ms, int trys)
166 for( int retry=trys; --retry>=0; ) {
168 if( !dvb_status() && has_signal() ) break;
169 int dt = ms - timer.get_difference();
170 if( dt > 0 ) Timer::delay(dt);
172 return has_signal() ? 0 : 1;
176 int DeviceDVBBuffer::read(int retries, int usec, int bsz)
178 while( --retries >= 0 ) {
179 struct timeval tv; tv.tv_sec = 0; tv.tv_usec = usec;
180 fd_set rd_fd; FD_ZERO(&rd_fd); FD_SET(fd, &rd_fd);
181 fd_set er_fd; FD_ZERO(&er_fd); FD_SET(fd, &er_fd);
182 int ret = select(fd+1, &rd_fd, 0, &er_fd, &tv);
185 if( !FD_ISSET(fd, &er_fd) ) {
186 select(0, 0, 0, 0, &tv);
189 if( !FD_ISSET(fd, &rd_fd) ) continue;
190 if( (ret=::read(fd, bfr, bsz)) > 0 )
196 int DeviceDVBInput::dvb_sync()
198 const int SYNC = 0x47, TS_BLKSZ = 188;
199 const int BSZ = 100*TS_BLKSZ, XSZ = BSZ*5;
200 DeviceDVBBuffer data(dvb_fd, BSZ);
202 int nsync = 0, xfr = 0;
204 if( data.read(5, 200000, BSZ) < 0 ) break;
206 while( ofs < data.size() ) {
209 while( i<data.size() && data[i]!=SYNC ) ++i;
210 if( i >= data.size() ) break;
212 while( (ofs+=TS_BLKSZ)<data.size() && data[ofs]==SYNC ) {
216 if( nsync >= 32 && (ofs-=data.size()) >= 0 ) {
218 if( data.read(5, 200000, ofs) < 0 ) return 1;
228 // Return values: 0 = OK, 1 = unreported error, 2 = error reported to stderr
229 int DeviceDVBInput::dvb_open()
234 if( frontend_fd < 0 &&
235 (frontend_fd = ::open(frontend_path, O_RDWR)) < 0 ) {
236 fprintf(stderr, "DeviceDVBInput::dvb_open %s: %s\n",
237 frontend_path, strerror(errno));
240 if( !ret && ioctl(frontend_fd, FE_GET_INFO, &fe_info) < 0 ) {
242 "DeviceDVBInput::dvb_open FE_GET_INFO: %s\n",
247 pwr_min = snr_min = 0;
248 pwr_max = snr_max = 65535;
251 int index = -1, table = -1;
252 if( !ret && (index = get_channel()) < 0 ) ret = 1;
253 if( !ret && (table = get_channel_table()) < 0 ) ret = 1;
254 if( !ret && table >= CHANLIST_SIZE ) ret = 1;
256 struct dvb_frontend_parameters frontend_param;
258 uint32_t frequency = chanlists[table].list[index].freq * 1000;
259 if( frequency < fe_info.frequency_min ||
260 frequency > fe_info.frequency_max ) {
262 "DeviceDVBInput::dvb_open channel %s frequency %d out of range %d-%d\n",
263 chanlists[table].list[index].name, frequency, fe_info.frequency_min, fe_info.frequency_max);
266 memset(&frontend_param, 0, sizeof(frontend_param));
267 frontend_param.frequency = frequency;
275 frontend_param.u.vsb.modulation = VSB_8;
286 frontend_param.u.vsb.modulation = QAM_AUTO;
290 "DeviceDVBInput::dvb_open bad table index=%d\n", table);
296 if( !ret && ioctl(frontend_fd, FE_SET_FRONTEND, &frontend_param) < 0 ) {
298 "DeviceDVBInput::dvb_open FE_SET_FRONTEND frequency=%d: %s\n",
299 frontend_param.frequency, strerror(errno));
303 if( !ret && wait_signal(333,3) ) {
305 "DeviceDVBInput::dvb_open: no signal, index=%d, channel %s, frequency=%d\n",
306 index, chanlists[table].list[index].name, frontend_param.frequency);
310 if( !ret && ioctl(frontend_fd, FE_GET_FRONTEND, &frontend_param) ) {
312 "DeviceDVBInput::dvb_open FE_GET_FRONTEND: %s\n",
316 if( !ret ) { // goofy quirks
317 if( !strcmp("Samsung S5H1409 QAM/8VSB Frontend", fe_info.name) ) {
318 switch(frontend_param.u.vsb.modulation) {
319 case QAM_64: snr_min = 200; snr_max = 300; break;
320 case QAM_256: snr_min = 260; snr_max = 400; break;
321 case VSB_8: snr_min = 111; snr_max = 300; break;
324 pwr_min = snr_min; pwr_max = snr_max;
326 else if( !strcmp("Auvitek AU8522 QAM/8VSB Frontend", fe_info.name) ) {
327 switch(frontend_param.u.vsb.modulation) {
328 case QAM_64: snr_min = 190; snr_max = 290; break;
329 case QAM_256: snr_min = 280; snr_max = 400; break;
330 case VSB_8: snr_min = 115; snr_max = 270; break;
333 pwr_min = snr_min; pwr_max = snr_max;
337 if( !ret && (demux_fd = ::open(demux_path, O_RDWR)) < 0 ) {
339 "DeviceDVBInput::dvb_open %s for video: %s\n",
345 // Setting exactly one PES filter to 0x2000 dumps the entire
348 struct dmx_pes_filter_params pesfilter;
349 memset(&pesfilter,0,sizeof(pesfilter));
350 pesfilter.pid = 0x2000;
351 pesfilter.input = DMX_IN_FRONTEND;
352 pesfilter.output = DMX_OUT_TS_TAP;
353 pesfilter.pes_type = DMX_PES_OTHER;
354 pesfilter.flags = DMX_IMMEDIATE_START;
355 if( ioctl(demux_fd, DMX_SET_PES_FILTER, &pesfilter) < 0 ) {
357 "DeviceDVBInput::dvb_open DMX_SET_PES_FILTER for raw: %s\n",
362 // Open transport stream for reading
363 if( !ret && (dvb_fd = ::open(dvb_path, O_RDONLY+O_NONBLOCK)) < 0 ) {
364 fprintf(stderr, "DeviceDVBInput::dvb_open %s: %s\n",
365 dvb_path, strerror(errno));
373 dvb_fd = open(TEST_DATA,O_RDONLY);
374 if( dvb_fd < 0 ) ret = 1;
378 if( !ret ) ret = dvb_sync();
379 if( !ret ) ret = dvb_status();
380 if( ret ) dvb_close();
385 void DeviceDVBInput::dvb_close(int fe)
387 if( dvb_fd >= 0 ) { ::close(dvb_fd); dvb_fd = -1; }
388 if( demux_fd >= 0 ) { ::close(demux_fd); demux_fd = -1; }
389 if( fe && frontend_fd >= 0 ) { ::close(frontend_fd); frontend_fd = -1; }
393 int DeviceDVBInput::dvb_status()
395 //note: this function can take > 500ms to execute (slowww)
400 int &fe = frontend_fd;
401 if( fe < 0 ) return -1;
402 fe_status_t st; memset(&st, 0, sizeof(st));
403 if( ioctl(fe, FE_READ_STATUS, &st) ) return 1;
404 if( (st & FE_TIMEDOUT) != 0 ) return 1;
405 signal_lck = (st & FE_HAS_LOCK) != 0 ? 1 : 0;
406 signal_crr = (st & FE_HAS_CARRIER) != 0 ? 1 : 0;
408 signal_pwr = ioctl(fe,FE_READ_SIGNAL_STRENGTH,&power) ? -1 :
409 drange(power, pwr_min, pwr_max);
411 signal_snr = ioctl(fe, FE_READ_SNR, &ratio) ? -1 :
412 drange(ratio, snr_min, snr_max);
414 signal_ber = ioctl(fe, FE_READ_BER, &rate) ? -1 : rate;
416 signal_unc = ioctl(fe, FE_READ_UNCORRECTED_BLOCKS, &errs) ? -1 : errs;
417 if( signal_lck && signal_ber >= 0 && signal_ber < 255 ) locked = 1;
418 if( dvb_locked != locked ) {
419 printf(_("** %scarrier, dvb_locked %s\n"),
420 signal_crr ? "" : _("no "), locked ? _("lock") : _("lost") );
428 int DeviceDVBInput::drange(int v, int min, int max)
430 if( (v-=min) < 0 || (max-=min) <= 0 ) return 0;
431 return v>max ? 65535U : (65535U * v) / max;
434 void DeviceDVBInput::reset_signal()
436 signal_pwr = signal_crr = signal_lck =
437 signal_snr = signal_ber = signal_unc = -1;
440 int DeviceDVBInput::open_dev(int color_model)
443 dvb_lock->lock("DeviceDVBInput::open_dev");
447 printf("DeviceDVBInput::open_dev: adaptor %s open failed\n",
452 dvb_input_status->start();
459 void DeviceDVBInput::close_dev()
461 dvb_lock->lock("DeviceDVBInput::close_dev");
462 dvb_input_status->stop();
467 int DeviceDVBInput::status_dev()
471 result = frontend_fd >= 0 ? dvb_status() : 1;
476 void DeviceDVBInput::set_signal_status(SignalStatus *stat)
478 dvb_lock->lock("DeviceDVBInput::set_signal_status");
479 if( dvb_input_status->signal_status )
480 dvb_input_status->signal_status->disconnect();
481 dvb_input_status->signal_status = stat;
482 if( stat ) stat->dvb_input = this;