centos build workarounds
[goodguy/history.git] / cinelerra-5.0 / cinelerra / devicedvbinput.C
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"
6
7 /*
8  * CINELERRA
9  * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
10  * 
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.
15  * 
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.
20  * 
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
24  * 
25  */
26
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"
33
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <stdint.h>
37 #include <unistd.h>
38 #include <string.h>
39 #include <errno.h>
40 #include <fcntl.h>
41 #ifdef HAVE_DVB
42 #include <linux/dvb/dmx.h>
43 #include <linux/dvb/frontend.h>
44 #endif
45 #include <sys/ioctl.h>
46
47
48 DVBInputStatus::DVBInputStatus(DeviceDVBInput *dvb_input)
49  : Thread(1, 0, 0)
50 {
51         this->dvb_input = dvb_input;
52         signal_status = 0;
53         done = 1;
54 }
55
56 DVBInputStatus::~DVBInputStatus()
57 {
58         stop();
59         if( signal_status ) signal_status->disconnect();
60 }
61
62 void DVBInputStatus::stop()
63 {
64         if( done ) return;
65         done = 1;
66         Thread::cancel();
67         Thread::join();
68 }
69
70 void DVBInputStatus::start()
71 {
72         if( !done ) return;
73         done = 0;
74         Thread::start();
75 }
76
77 void DVBInputStatus::lock_dvb()
78 {
79         Thread::disable_cancel();
80         dvb_input->dvb_lock->lock("DVBInputStatus::lock_dvb");
81 }
82
83 void DVBInputStatus::unlock_dvb()
84 {
85         dvb_input->dvb_lock->unlock();
86         Thread::enable_cancel();
87 }
88
89 void DVBInputStatus::run()
90 {
91         lock_dvb();
92
93         while( !done ) {
94                 dvb_input->status_dev();
95                 update();
96                 unlock_dvb();
97                 usleep(250000);
98                 lock_dvb();
99         }
100
101         unlock_dvb();
102 }
103
104 void DVBInputStatus::update()
105 {
106         if( !signal_status ) return;
107         signal_status->lock_window("DVBInputStatus::update");
108         signal_status->update();
109         signal_status->unlock_window();
110 }
111
112
113 DeviceDVBInput::DeviceDVBInput(const char *name, int no)
114  : DeviceMPEGInput(name, no)
115 {
116         frontend_fd = -1;
117         demux_fd = -1;
118         dvb_fd = -1;
119         reset_signal();
120         dvb_locked = 0;
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);
126 }
127
128 DeviceMPEGInput *DeviceDVBInput::
129 NewDVBInput(const char *name, int no)
130 {
131         return (DeviceMPEGInput *) new DeviceDVBInput(name, no);
132 }
133
134 DeviceDVBInput::~DeviceDVBInput()
135 {
136         close_dev();
137         delete dvb_input_status;
138         delete dvb_lock;
139         dvb_close(1);
140 }
141
142
143 DeviceMPEGInput* DeviceDVBInput::get_mpeg_input(VideoDevice *device)
144 {
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;
155         return mpeg_device;
156 }
157
158 DeviceMPEGInput* DeviceDVBInput::get_mpeg_input(AudioDevice *device)
159 {
160         return get_mpeg_audio(device, NewDVBInput,
161                 device->in_config->dvb_in_adapter, device->in_config->dvb_in_device);
162 }
163
164
165 int DeviceDVBInput::wait_signal(int ms, int trys)
166 {
167         Timer timer;
168         for( int retry=trys; --retry>=0; ) {
169                 timer.update();
170                 if( !dvb_status() && has_signal() ) break;
171                 int dt = ms - timer.get_difference();
172                 if( dt > 0 ) Timer::delay(dt);
173         }
174         return has_signal() ? 0 : 1;
175 }
176
177
178 int DeviceDVBBuffer::read(int retries, int usec, int bsz)
179 {
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);
185                 if( ret < 0 ) break;
186                 if( !ret ) continue;
187                 if( !FD_ISSET(fd, &er_fd) ) {
188                         select(0, 0, 0, 0, &tv);
189                         continue;
190                 }
191                 if( !FD_ISSET(fd, &rd_fd) ) continue;
192                 if( (ret=::read(fd, bfr, bsz)) > 0 )
193                         return sz = ret;
194         }
195         return -1;
196 }
197
198 int DeviceDVBInput::dvb_sync()
199 {
200         const int SYNC = 0x47, TS_BLKSZ = 188;
201         const int BSZ = 100*TS_BLKSZ, XSZ = BSZ*5;
202         DeviceDVBBuffer data(dvb_fd, BSZ);
203
204         int nsync = 0, xfr = 0;
205         while( xfr < XSZ ) {
206                 if( data.read(5, 200000, BSZ) < 0 ) break;
207                 int ofs = 0;
208                 while( ofs < data.size() ) {
209                         nsync = 0;
210                         int i = ofs;
211                         while( i<data.size() && data[i]!=SYNC ) ++i;
212                         if( i >= data.size() ) break;
213                         ofs = i;
214                         while( (ofs+=TS_BLKSZ)<data.size() && data[ofs]==SYNC ) {
215                                 ++nsync;  i = ofs;
216                         }
217                 }
218                 if( nsync >= 32 && (ofs-=data.size()) >= 0 ) {
219                         while( ofs > 0 ) {
220                                 if( data.read(5, 200000, ofs) < 0 ) return 1;
221                                 ofs -= data.size();
222                         }
223                         return 0;
224                 }
225                 xfr += data.size();
226         }
227         return 1;
228 }
229
230 int DeviceDVBInput::dvb_open()
231 {
232         int ret = 0;
233 #ifndef TEST_DATA
234 #ifdef HAVE_DVB
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));
239                 ret = 1;
240         }
241         if( !ret && ioctl(frontend_fd, FE_GET_INFO, &fe_info) < 0 ) {
242                 fprintf(stderr, 
243                         "DeviceDVBInput::dvb_open FE_GET_INFO: %s\n",
244                         strerror(errno));
245                 ret = 1;
246         }
247
248         pwr_min = snr_min = 0;
249         pwr_max = snr_max = 65535;
250
251 // Set frequency
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;
255         if( !ret ) {
256                 uint32_t frequency = 0;
257                 struct dvb_frontend_parameters frontend_param;
258                 bzero(&frontend_param, sizeof(frontend_param));
259                 ret = 1;
260                 switch(table) {
261                 case NETTUNE_AIR:
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;
268                         ret = 0;
269                         break;
270                 case NETTUNE_CABLE:
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;
277                         ret = 0;
278                         break;
279                 }
280
281                 if( !ret && ioctl(frontend_fd, FE_SET_FRONTEND, &frontend_param) < 0 ) {
282                         fprintf(stderr, 
283                                 "DeviceDVBInput::dvb_open FE_SET_FRONTEND frequency=%d: %s\n",
284                                 frontend_param.frequency, strerror(errno));
285                         ret = 1;
286                 }
287
288                 if( !ret && wait_signal(333,3) ) {
289                         fprintf(stderr,
290                                  "DeviceDVBInput::dvb_open: no signal, index=%d frequency=%d\n",
291                                 index, frontend_param.frequency);
292                         ret = 1;
293                 }
294
295                 if( !ret && ioctl(frontend_fd, FE_GET_FRONTEND, &frontend_param) ) {
296                         fprintf(stderr, 
297                                 "DeviceDVBInput::dvb_open FE_GET_FRONTEND: %s\n",
298                                 strerror(errno));
299                         ret = 1;
300                 }
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;
307                                 default: break;
308                                 }
309                                 pwr_min = snr_min;  pwr_max = snr_max;
310                         }
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;
316                                 default: break;
317                                 }
318                                 pwr_min = snr_min;  pwr_max = snr_max;
319                         }
320                 }
321         }
322
323         if( !ret && (demux_fd = ::open(demux_path, O_RDWR)) < 0 ) {
324                 fprintf(stderr,
325                         "DeviceDVBInput::dvb_open %s for video: %s\n",
326                         demux_path,
327                         strerror(errno));
328                 ret = 1;
329         }
330
331 // Setting exactly one PES filter to 0x2000 dumps the entire
332 // transport stream.
333         if( !ret ) {
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 ) {
342                         fprintf(stderr, 
343                 "DeviceDVBInput::dvb_open DMX_SET_PES_FILTER for raw: %s\n",
344                                 strerror(errno));
345                         ret = 1;
346                 }
347         }
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));
352                 ret = 1;
353         }
354 #else
355         dvb_fd = -1;
356         ret = 1;
357 #endif
358 #else
359         dvb_fd = open(TEST_DATA,O_RDONLY);
360         if( dvb_fd < 0 ) ret = 1;
361 #endif
362         reset_signal();
363         dvb_locked = 0;
364         if( !ret ) ret = dvb_sync();
365         if( !ret ) ret = dvb_status();
366         if( ret ) dvb_close();
367         return ret;
368 }
369
370
371 void DeviceDVBInput::dvb_close(int fe)
372 {
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; }
376 }
377
378
379 int DeviceDVBInput::dvb_status()
380 {
381 //note: this function can take > 500ms to execute  (slowww)
382 #ifndef TEST_DATA
383         int locked = 0;
384         reset_signal();
385 #ifdef HAVE_DVB
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;
393         uint16_t power = 0;
394         signal_pwr = ioctl(fe,FE_READ_SIGNAL_STRENGTH,&power) ? -1 :
395                 drange(power, pwr_min, pwr_max);
396         uint16_t ratio = 0;
397         signal_snr = ioctl(fe, FE_READ_SNR, &ratio) ? -1 :
398                 drange(ratio, snr_min, snr_max);
399         uint32_t rate = 0;
400         signal_ber = ioctl(fe, FE_READ_BER, &rate) ? -1 : rate;
401         uint32_t errs = 0;
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") );
407         }
408 #endif
409         dvb_locked = locked;
410 #endif
411         return 0;
412 }
413
414 int DeviceDVBInput::drange(int v, int min, int max)
415 {
416         if( (v-=min) < 0 || (max-=min) <= 0 ) return 0;
417         return v>max ? 65535U : (65535U * v) / max;
418 }
419
420 void DeviceDVBInput::reset_signal()
421 {
422         signal_pwr = signal_crr = signal_lck =
423         signal_snr = signal_ber = signal_unc = -1;
424 }
425
426 int DeviceDVBInput::open_dev(int color_model)
427 {
428         int ret = 0;
429         dvb_lock->lock("DeviceDVBInput::open_dev");
430         if( dvb_fd < 0 )
431         {
432                 ret = dvb_open();
433                 if( ret ) {
434                         printf("DeviceDVBInput::open_dev: adaptor %s open failed\n", 
435                                 dev_name);
436                 }
437         }
438         if( !ret )
439                 dvb_input_status->start();
440         else
441                 dvb_close();
442         dvb_lock->unlock();
443         return ret;
444 }
445
446 void DeviceDVBInput::close_dev()
447 {
448         dvb_lock->lock("DeviceDVBInput::close_dev");
449         dvb_input_status->stop();
450         dvb_close();
451         dvb_lock->unlock();
452 }
453
454 int DeviceDVBInput::status_dev()
455 {
456         int result = 0;
457 #ifndef TEST_DATA
458         result = frontend_fd >= 0 ? dvb_status() : 1;
459 #endif
460         return result;
461 }
462
463 void DeviceDVBInput::set_signal_status(SignalStatus *stat)
464 {
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;
470         dvb_lock->unlock();
471 }
472