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
27 #include "../hvirtual_config.h"
28 #include "chantables.h"
29 #include "devicedvbinput.h"
30 #include "devicempeginput.h"
31 #include "recordconfig.h"
32 #include "signalstatus.h"
42 #include <linux/dvb/dmx.h>
43 #include <linux/dvb/frontend.h>
45 #include <sys/ioctl.h>
48 DVBInputStatus::DVBInputStatus(DeviceDVBInput *dvb_input)
51 this->dvb_input = dvb_input;
56 DVBInputStatus::~DVBInputStatus()
59 if( signal_status ) signal_status->disconnect();
62 void DVBInputStatus::stop()
70 void DVBInputStatus::start()
77 void DVBInputStatus::lock_dvb()
79 Thread::disable_cancel();
80 dvb_input->dvb_lock->lock("DVBInputStatus::lock_dvb");
83 void DVBInputStatus::unlock_dvb()
85 dvb_input->dvb_lock->unlock();
86 Thread::enable_cancel();
89 void DVBInputStatus::run()
94 dvb_input->status_dev();
104 void DVBInputStatus::update()
106 if( !signal_status ) return;
107 signal_status->lock_window("DVBInputStatus::update");
108 signal_status->update();
109 signal_status->unlock_window();
113 DeviceDVBInput::DeviceDVBInput(const char *name, int no)
114 : DeviceMPEGInput(name, no)
121 dvb_lock = new Mutex("DeviceDVBInput::dvb_lock", 0);
122 sprintf(frontend_path, "%s/frontend%d", name, no);
123 sprintf(demux_path, "%s/demux%d", name, no);
124 sprintf(dvb_path, "%s/dvr%d", name, no);
125 dvb_input_status = new DVBInputStatus(this);
128 DeviceMPEGInput *DeviceDVBInput::
129 NewDVBInput(const char *name, int no)
131 return (DeviceMPEGInput *) new DeviceDVBInput(name, no);
134 DeviceDVBInput::~DeviceDVBInput()
137 delete dvb_input_status;
143 DeviceMPEGInput* DeviceDVBInput::get_mpeg_input(VideoDevice *device)
145 DeviceMPEGInput* mpeg_device = get_mpeg_video(device, NewDVBInput,
146 device->in_config->dvb_in_adapter, device->in_config->dvb_in_device);
147 if( !mpeg_device ) return 0;
148 device->channel->has_subchan = 1;
149 device->channel->has_scanning = 1;
150 device->channel->use_frequency = 1;
151 mpeg_device->set_device_number(0);
152 Channel *input_src = device->new_input_source((char*)"input0");
153 input_src->device_index = 0;
154 input_src->tuner = 0;
158 DeviceMPEGInput* DeviceDVBInput::get_mpeg_input(AudioDevice *device)
160 return get_mpeg_audio(device, NewDVBInput,
161 device->in_config->dvb_in_adapter, device->in_config->dvb_in_device);
165 int DeviceDVBInput::wait_signal(int ms, int trys)
168 for( int retry=trys; --retry>=0; ) {
170 if( !dvb_status() && has_signal() ) break;
171 int dt = ms - timer.get_difference();
172 if( dt > 0 ) Timer::delay(dt);
174 return has_signal() ? 0 : 1;
178 int DeviceDVBBuffer::read(int retries, int usec, int bsz)
180 while( --retries >= 0 ) {
181 struct timeval tv; tv.tv_sec = 0; tv.tv_usec = usec;
182 fd_set rd_fd; FD_ZERO(&rd_fd); FD_SET(fd, &rd_fd);
183 fd_set er_fd; FD_ZERO(&er_fd); FD_SET(fd, &er_fd);
184 int ret = select(fd+1, &rd_fd, 0, &er_fd, &tv);
187 if( !FD_ISSET(fd, &er_fd) ) {
188 select(0, 0, 0, 0, &tv);
191 if( !FD_ISSET(fd, &rd_fd) ) continue;
192 if( (ret=::read(fd, bfr, bsz)) > 0 )
198 int DeviceDVBInput::dvb_sync()
200 const int SYNC = 0x47, TS_BLKSZ = 188;
201 const int BSZ = 100*TS_BLKSZ, XSZ = BSZ*5;
202 DeviceDVBBuffer data(dvb_fd, BSZ);
204 int nsync = 0, xfr = 0;
206 if( data.read(5, 200000, BSZ) < 0 ) break;
208 while( ofs < data.size() ) {
211 while( i<data.size() && data[i]!=SYNC ) ++i;
212 if( i >= data.size() ) break;
214 while( (ofs+=TS_BLKSZ)<data.size() && data[ofs]==SYNC ) {
218 if( nsync >= 32 && (ofs-=data.size()) >= 0 ) {
220 if( data.read(5, 200000, ofs) < 0 ) return 1;
230 int DeviceDVBInput::dvb_open()
235 if( frontend_fd < 0 &&
236 (frontend_fd = ::open(frontend_path, O_RDWR)) < 0 ) {
237 fprintf(stderr, "DeviceDVBInput::dvb_open %s: %s\n",
238 frontend_path, strerror(errno));
241 if( !ret && ioctl(frontend_fd, FE_GET_INFO, &fe_info) < 0 ) {
243 "DeviceDVBInput::dvb_open FE_GET_INFO: %s\n",
248 pwr_min = snr_min = 0;
249 pwr_max = snr_max = 65535;
252 int index = -1, table = -1;
253 if( !ret && (index = get_channel()) < 0 ) ret = 1;
254 if( !ret && (table = get_channel_table()) < 0 ) ret = 1;
256 uint32_t frequency = 0;
257 struct dvb_frontend_parameters frontend_param;
258 bzero(&frontend_param, sizeof(frontend_param));
262 if( index >= chanlists[NTSC_DVB].count ) break;
263 frequency = chanlists[NTSC_DVB].list[index].freq * 1000000;
264 if( frequency < fe_info.frequency_min ||
265 frequency > fe_info.frequency_max ) break;
266 frontend_param.frequency = frequency;
267 frontend_param.u.vsb.modulation = VSB_8;
271 if( index >= chanlists[CATV_DVB].count ) break;
272 frequency = chanlists[CATV_DVB].list[index].freq * 1000000;
273 if( frequency < fe_info.frequency_min ||
274 frequency > fe_info.frequency_max ) break;
275 frontend_param.frequency = frequency;
276 frontend_param.u.vsb.modulation = QAM_AUTO;
281 if( !ret && ioctl(frontend_fd, FE_SET_FRONTEND, &frontend_param) < 0 ) {
283 "DeviceDVBInput::dvb_open FE_SET_FRONTEND frequency=%d: %s\n",
284 frontend_param.frequency, strerror(errno));
288 if( !ret && wait_signal(333,3) ) {
290 "DeviceDVBInput::dvb_open: no signal, index=%d frequency=%d\n",
291 index, frontend_param.frequency);
295 if( !ret && ioctl(frontend_fd, FE_GET_FRONTEND, &frontend_param) ) {
297 "DeviceDVBInput::dvb_open FE_GET_FRONTEND: %s\n",
301 if( !ret ) { // goofy quirks
302 if( !strcmp("Samsung S5H1409 QAM/8VSB Frontend", fe_info.name) ) {
303 switch(frontend_param.u.vsb.modulation) {
304 case QAM_64: snr_min = 200; snr_max = 300; break;
305 case QAM_256: snr_min = 260; snr_max = 400; break;
306 case VSB_8: snr_min = 111; snr_max = 300; break;
309 pwr_min = snr_min; pwr_max = snr_max;
311 else if( !strcmp("Auvitek AU8522 QAM/8VSB Frontend", fe_info.name) ) {
312 switch(frontend_param.u.vsb.modulation) {
313 case QAM_64: snr_min = 190; snr_max = 290; break;
314 case QAM_256: snr_min = 280; snr_max = 400; break;
315 case VSB_8: snr_min = 115; snr_max = 270; break;
318 pwr_min = snr_min; pwr_max = snr_max;
323 if( !ret && (demux_fd = ::open(demux_path, O_RDWR)) < 0 ) {
325 "DeviceDVBInput::dvb_open %s for video: %s\n",
331 // Setting exactly one PES filter to 0x2000 dumps the entire
334 struct dmx_pes_filter_params pesfilter;
335 memset(&pesfilter,0,sizeof(pesfilter));
336 pesfilter.pid = 0x2000;
337 pesfilter.input = DMX_IN_FRONTEND;
338 pesfilter.output = DMX_OUT_TS_TAP;
339 pesfilter.pes_type = DMX_PES_OTHER;
340 pesfilter.flags = DMX_IMMEDIATE_START;
341 if( ioctl(demux_fd, DMX_SET_PES_FILTER, &pesfilter) < 0 ) {
343 "DeviceDVBInput::dvb_open DMX_SET_PES_FILTER for raw: %s\n",
348 // Open transport stream for reading
349 if( !ret && (dvb_fd = ::open(dvb_path, O_RDONLY+O_NONBLOCK)) < 0 ) {
350 fprintf(stderr, "DeviceDVBInput::dvb_open %s: %s\n",
351 dvb_path, strerror(errno));
359 dvb_fd = open(TEST_DATA,O_RDONLY);
360 if( dvb_fd < 0 ) ret = 1;
364 if( !ret ) ret = dvb_sync();
365 if( !ret ) ret = dvb_status();
366 if( ret ) dvb_close();
371 void DeviceDVBInput::dvb_close(int fe)
373 if( dvb_fd >= 0 ) { ::close(dvb_fd); dvb_fd = -1; }
374 if( demux_fd >= 0 ) { ::close(demux_fd); demux_fd = -1; }
375 if( fe && frontend_fd >= 0 ) { ::close(frontend_fd); frontend_fd = -1; }
379 int DeviceDVBInput::dvb_status()
381 //note: this function can take > 500ms to execute (slowww)
386 int &fe = frontend_fd;
387 if( fe < 0 ) return -1;
388 fe_status_t st; memset(&st, 0, sizeof(st));
389 if( ioctl(fe, FE_READ_STATUS, &st) ) return 1;
390 if( (st & FE_TIMEDOUT) != 0 ) return 1;
391 signal_lck = (st & FE_HAS_LOCK) != 0 ? 1 : 0;
392 signal_crr = (st & FE_HAS_CARRIER) != 0 ? 1 : 0;
394 signal_pwr = ioctl(fe,FE_READ_SIGNAL_STRENGTH,&power) ? -1 :
395 drange(power, pwr_min, pwr_max);
397 signal_snr = ioctl(fe, FE_READ_SNR, &ratio) ? -1 :
398 drange(ratio, snr_min, snr_max);
400 signal_ber = ioctl(fe, FE_READ_BER, &rate) ? -1 : rate;
402 signal_unc = ioctl(fe, FE_READ_UNCORRECTED_BLOCKS, &errs) ? -1 : errs;
403 if( signal_lck && signal_ber >= 0 && signal_ber < 255 ) locked = 1;
404 if( dvb_locked != locked ) {
405 printf(_("** %scarrier, dvb_locked %s\n"),
406 signal_crr ? "" : _("no "), locked ? _("lock") : _("lost") );
414 int DeviceDVBInput::drange(int v, int min, int max)
416 if( (v-=min) < 0 || (max-=min) <= 0 ) return 0;
417 return v>max ? 65535U : (65535U * v) / max;
420 void DeviceDVBInput::reset_signal()
422 signal_pwr = signal_crr = signal_lck =
423 signal_snr = signal_ber = signal_unc = -1;
426 int DeviceDVBInput::open_dev(int color_model)
429 dvb_lock->lock("DeviceDVBInput::open_dev");
434 printf("DeviceDVBInput::open_dev: adaptor %s open failed\n",
439 dvb_input_status->start();
446 void DeviceDVBInput::close_dev()
448 dvb_lock->lock("DeviceDVBInput::close_dev");
449 dvb_input_status->stop();
454 int DeviceDVBInput::status_dev()
458 result = frontend_fd >= 0 ? dvb_status() : 1;
463 void DeviceDVBInput::set_signal_status(SignalStatus *stat)
465 dvb_lock->lock("DeviceDVBInput::set_signal_status");
466 if( dvb_input_status->signal_status )
467 dvb_input_status->signal_status->disconnect();
468 dvb_input_status->signal_status = stat;
469 if( stat ) stat->dvb_input = this;