1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39 
40 #include "v4lradiocontrol.h"
41 #include "v4lradioservice.h"
42 
43 #include <QtCore/qdebug.h>
44 
45 #include <fcntl.h>
46 
47 #include <sys/ioctl.h>
48 #include "linux/videodev2.h"
49 
50 #include <sys/soundcard.h>
51 #include <stdio.h>
52 #include <sys/types.h>
53 #include <sys/ioctl.h>
54 #include <fcntl.h>
55 #include <unistd.h>
56 
V4LRadioControl(QObject * parent)57 V4LRadioControl::V4LRadioControl(QObject *parent)
58     :QRadioTunerControl(parent)
59 {
60     fd = -1;
61     initRadio();
62     muted = false;
63     stereo = false;
64     m_error = false;
65     sig = 0;
66     currentBand = QRadioTuner::FM;
67     step = 100000;
68     scanning = false;
69     playTime.restart();
70     timer = new QTimer(this);
71     timer->setInterval(200);
72     connect(timer,SIGNAL(timeout()),this,SLOT(search()));
73     timer->start();
74 }
75 
~V4LRadioControl()76 V4LRadioControl::~V4LRadioControl()
77 {
78     timer->stop();
79 
80     if(fd > 0)
81         ::close(fd);
82 }
83 
isAvailable() const84 bool V4LRadioControl::isAvailable() const
85 {
86     return available;
87 }
88 
availability() const89 QMultimedia::AvailabilityStatus V4LRadioControl::availability() const
90 {
91     if (fd > 0)
92         return QMultimedia::Available;
93     else
94         return QMultimedia::ResourceError;
95 }
96 
state() const97 QRadioTuner::State V4LRadioControl::state() const
98 {
99     return fd > 0 ? QRadioTuner::ActiveState : QRadioTuner::StoppedState;
100 }
101 
band() const102 QRadioTuner::Band V4LRadioControl::band() const
103 {
104     return currentBand;
105 }
106 
isBandSupported(QRadioTuner::Band b) const107 bool V4LRadioControl::isBandSupported(QRadioTuner::Band b) const
108 {
109     QRadioTuner::Band bnd = (QRadioTuner::Band)b;
110     switch(bnd) {
111         case QRadioTuner::FM:
112             if(freqMin <= 87500000 && freqMax >= 108000000)
113                 return true;
114             break;
115         case QRadioTuner::LW:
116             if(freqMin <= 148500 && freqMax >= 283500)
117                 return true;
118         case QRadioTuner::AM:
119             if(freqMin <= 520000 && freqMax >= 1610000)
120                 return true;
121         default:
122             if(freqMin <= 1711000 && freqMax >= 30000000)
123                 return true;
124     }
125 
126     return false;
127 }
128 
setBand(QRadioTuner::Band b)129 void V4LRadioControl::setBand(QRadioTuner::Band b)
130 {
131     if(freqMin <= 87500000 && freqMax >= 108000000 && b == QRadioTuner::FM) {
132         // FM 87.5 to 108.0 MHz, except Japan 76-90 MHz
133         currentBand =  (QRadioTuner::Band)b;
134         step = 100000; // 100kHz steps
135         emit bandChanged(currentBand);
136 
137     } else if(freqMin <= 148500 && freqMax >= 283500 && b == QRadioTuner::LW) {
138         // LW 148.5 to 283.5 kHz, 9kHz channel spacing (Europe, Africa, Asia)
139         currentBand =  (QRadioTuner::Band)b;
140         step = 1000; // 1kHz steps
141         emit bandChanged(currentBand);
142 
143     } else if(freqMin <= 520000 && freqMax >= 1610000 && b == QRadioTuner::AM) {
144         // AM 520 to 1610 kHz, 9 or 10kHz channel spacing, extended 1610 to 1710 kHz
145         currentBand =  (QRadioTuner::Band)b;
146         step = 1000; // 1kHz steps
147         emit bandChanged(currentBand);
148 
149     } else if(freqMin <= 1711000 && freqMax >= 30000000 && b == QRadioTuner::SW) {
150         // SW 1.711 to 30.0 MHz, divided into 15 bands. 5kHz channel spacing
151         currentBand =  (QRadioTuner::Band)b;
152         step = 500; // 500Hz steps
153         emit bandChanged(currentBand);
154     }
155     playTime.restart();
156 }
157 
frequency() const158 int V4LRadioControl::frequency() const
159 {
160     return currentFreq;
161 }
162 
frequencyStep(QRadioTuner::Band b) const163 int V4LRadioControl::frequencyStep(QRadioTuner::Band b) const
164 {
165     int step = 0;
166 
167     if(b == QRadioTuner::FM)
168         step = 100000; // 100kHz steps
169     else if(b == QRadioTuner::LW)
170         step = 1000; // 1kHz steps
171     else if(b == QRadioTuner::AM)
172         step = 1000; // 1kHz steps
173     else if(b == QRadioTuner::SW)
174         step = 500; // 500Hz steps
175 
176     return step;
177 }
178 
frequencyRange(QRadioTuner::Band b) const179 QPair<int,int> V4LRadioControl::frequencyRange(QRadioTuner::Band b) const
180 {
181     if(b == QRadioTuner::AM)
182         return qMakePair<int,int>(520000,1710000);
183     else if(b == QRadioTuner::FM)
184         return qMakePair<int,int>(87500000,108000000);
185     else if(b == QRadioTuner::SW)
186         return qMakePair<int,int>(1711111,30000000);
187     else if(b == QRadioTuner::LW)
188         return qMakePair<int,int>(148500,283500);
189 
190     return qMakePair<int,int>(0,0);
191 }
192 
setFrequency(int frequency)193 void V4LRadioControl::setFrequency(int frequency)
194 {
195     qint64 f = frequency;
196 
197     v4l2_frequency freq;
198 
199     if(frequency < freqMin)
200         f = freqMax;
201     if(frequency > freqMax)
202         f = freqMin;
203 
204     if(fd > 0) {
205         memset( &freq, 0, sizeof( freq ) );
206         // Use the first tuner
207         freq.tuner = 0;
208         if ( ioctl( fd, VIDIOC_G_FREQUENCY, &freq ) >= 0 ) {
209             if(low) {
210                 // For low, freq in units of 62.5Hz, so convert from Hz to units.
211                 freq.frequency = (int)(f/62.5);
212             } else {
213                 // For high, freq in units of 62.5kHz, so convert from Hz to units.
214                 freq.frequency = (int)(f/62500);
215             }
216             ioctl( fd, VIDIOC_S_FREQUENCY, &freq );
217             currentFreq = f;
218             playTime.restart();
219             emit frequencyChanged(currentFreq);
220         }
221     }
222     playTime.restart();
223 }
224 
isStereo() const225 bool V4LRadioControl::isStereo() const
226 {
227     return stereo;
228 }
229 
stereoMode() const230 QRadioTuner::StereoMode V4LRadioControl::stereoMode() const
231 {
232     return QRadioTuner::Auto;
233 }
234 
setStereoMode(QRadioTuner::StereoMode mode)235 void V4LRadioControl::setStereoMode(QRadioTuner::StereoMode mode)
236 {
237     bool stereo = true;
238 
239     if(mode == QRadioTuner::ForceMono)
240         stereo = false;
241 
242     v4l2_tuner tuner;
243 
244     memset( &tuner, 0, sizeof( tuner ) );
245 
246     if ( ioctl( fd, VIDIOC_G_TUNER, &tuner ) >= 0 ) {
247         if(stereo)
248             tuner.audmode = V4L2_TUNER_MODE_STEREO;
249         else
250             tuner.audmode = V4L2_TUNER_MODE_MONO;
251 
252         if ( ioctl( fd, VIDIOC_S_TUNER, &tuner ) >= 0 ) {
253             emit stereoStatusChanged(stereo);
254         }
255     }
256 }
257 
signalStrength() const258 int V4LRadioControl::signalStrength() const
259 {
260     v4l2_tuner tuner;
261 
262     // Return the first tuner founds signal strength.
263     for ( int index = 0; index < tuners; ++index ) {
264         memset( &tuner, 0, sizeof( tuner ) );
265         tuner.index = index;
266         if ( ioctl( fd, VIDIOC_G_TUNER, &tuner ) < 0 )
267             continue;
268         if ( tuner.type != V4L2_TUNER_RADIO )
269             continue;
270         // percentage signal strength
271         return tuner.signal*100/65535;
272     }
273 
274     return 0;
275 }
276 
volume() const277 int V4LRadioControl::volume() const
278 {
279     v4l2_queryctrl queryctrl;
280 
281     if(fd > 0) {
282         memset( &queryctrl, 0, sizeof( queryctrl ) );
283         queryctrl.id = V4L2_CID_AUDIO_VOLUME;
284         if ( ioctl( fd, VIDIOC_QUERYCTRL, &queryctrl ) >= 0 ) {
285             if(queryctrl.maximum == 0) {
286                 return vol;
287             } else {
288                 // percentage volume returned
289                 return queryctrl.default_value*100/queryctrl.maximum;
290             }
291         }
292     }
293     return 0;
294 }
295 
setVolume(int volume)296 void V4LRadioControl::setVolume(int volume)
297 {
298     v4l2_queryctrl queryctrl;
299 
300     if(fd > 0) {
301         memset( &queryctrl, 0, sizeof( queryctrl ) );
302         queryctrl.id = V4L2_CID_AUDIO_VOLUME;
303         if ( ioctl( fd, VIDIOC_QUERYCTRL, &queryctrl ) >= 0 ) {
304             v4l2_control control;
305 
306             if(queryctrl.maximum > 0) {
307                 memset( &control, 0, sizeof( control ) );
308                 control.id = V4L2_CID_AUDIO_VOLUME;
309                 control.value = volume*queryctrl.maximum/100;
310                 ioctl( fd, VIDIOC_S_CTRL, &control );
311             } else {
312                 setVol(volume);
313             }
314             emit volumeChanged(volume);
315         }
316     }
317 }
318 
isMuted() const319 bool V4LRadioControl::isMuted() const
320 {
321     return muted;
322 }
323 
setMuted(bool muted)324 void V4LRadioControl::setMuted(bool muted)
325 {
326     v4l2_queryctrl queryctrl;
327 
328     if(fd > 0) {
329         memset( &queryctrl, 0, sizeof( queryctrl ) );
330         queryctrl.id = V4L2_CID_AUDIO_MUTE;
331         if ( ioctl( fd, VIDIOC_QUERYCTRL, &queryctrl ) >= 0 ) {
332             v4l2_control control;
333             memset( &control, 0, sizeof( control ) );
334             control.id = V4L2_CID_AUDIO_MUTE;
335             control.value = (muted ? queryctrl.maximum : queryctrl.minimum );
336             ioctl( fd, VIDIOC_S_CTRL, &control );
337             this->muted = muted;
338             emit mutedChanged(muted);
339         }
340     }
341 }
342 
isSearching() const343 bool V4LRadioControl::isSearching() const
344 {
345     return scanning;
346 }
347 
cancelSearch()348 void V4LRadioControl::cancelSearch()
349 {
350     scanning = false;
351     timer->stop();
352 }
353 
searchForward()354 void V4LRadioControl::searchForward()
355 {
356     // Scan up
357     if(scanning) {
358         cancelSearch();
359         return;
360     }
361     scanning = true;
362     forward  = true;
363     timer->start();
364 }
365 
searchBackward()366 void V4LRadioControl::searchBackward()
367 {
368     // Scan down
369     if(scanning) {
370         cancelSearch();
371         return;
372     }
373     scanning = true;
374     forward  = false;
375     timer->start();
376 }
377 
start()378 void V4LRadioControl::start()
379 {
380 }
381 
stop()382 void V4LRadioControl::stop()
383 {
384 }
385 
error() const386 QRadioTuner::Error V4LRadioControl::error() const
387 {
388     if(m_error)
389         return QRadioTuner::OpenError;
390 
391     return QRadioTuner::NoError;
392 }
393 
errorString() const394 QString V4LRadioControl::errorString() const
395 {
396     return QString();
397 }
398 
search()399 void V4LRadioControl::search()
400 {
401     int signal = signalStrength();
402     if(sig != signal) {
403         sig = signal;
404         emit signalStrengthChanged(sig);
405     }
406 
407     if(!scanning) return;
408 
409     if (signal > 25) {
410         cancelSearch();
411         return;
412     }
413 
414     if(forward) {
415         setFrequency(currentFreq+step);
416     } else {
417         setFrequency(currentFreq-step);
418     }
419 }
420 
initRadio()421 bool V4LRadioControl::initRadio()
422 {
423     v4l2_tuner tuner;
424     v4l2_input input;
425     v4l2_frequency freq;
426     v4l2_capability cap;
427 
428     low = false;
429     available = false;
430     freqMin = freqMax = currentFreq = 0;
431 
432     fd = ::open("/dev/radio0", O_RDWR);
433 
434     if(fd != -1) {
435         // Capabilities
436         memset( &cap, 0, sizeof( cap ) );
437         if(::ioctl(fd, VIDIOC_QUERYCAP, &cap ) >= 0) {
438             if(((cap.capabilities & V4L2_CAP_RADIO) == 0) && ((cap.capabilities & V4L2_CAP_AUDIO) == 0))
439                 available = true;
440         }
441 
442         // Tuners
443         memset( &input, 0, sizeof( input ) );
444         tuners = 0;
445         for ( ;; ) {
446             memset( &input, 0, sizeof( input ) );
447             input.index = tuners;
448             if ( ioctl( fd, VIDIOC_ENUMINPUT, &input ) < 0 )
449                 break;
450             ++tuners;
451         }
452 
453         // Freq bands
454         for ( int index = 0; index < tuners; ++index ) {
455             memset( &tuner, 0, sizeof( tuner ) );
456             tuner.index = index;
457             if ( ioctl( fd, VIDIOC_G_TUNER, &tuner ) < 0 )
458                 continue;
459             if ( tuner.type != V4L2_TUNER_RADIO )
460                 continue;
461             if ( ( tuner.capability & V4L2_TUNER_CAP_LOW ) != 0 ) {
462                 // Units are 1/16th of a kHz.
463                 low = true;
464             }
465             if(low) {
466                 freqMin = tuner.rangelow * 62.5;
467                 freqMax = tuner.rangehigh * 62.5;
468             } else {
469                 freqMin = tuner.rangelow * 62500;
470                 freqMax = tuner.rangehigh * 62500;
471             }
472         }
473 
474         // frequency
475         memset( &freq, 0, sizeof( freq ) );
476         if(::ioctl(fd, VIDIOC_G_FREQUENCY, &freq ) >= 0) {
477             if ( ((int)freq.frequency) != -1 ) {    // -1 means not set.
478                 if(low)
479                     currentFreq = freq.frequency * 62.5;
480                 else
481                     currentFreq = freq.frequency * 62500;
482             }
483         }
484 
485         // stereo
486         bool stereo = false;
487         memset( &tuner, 0, sizeof( tuner ) );
488         if ( ioctl( fd, VIDIOC_G_TUNER, &tuner ) >= 0 ) {
489             if((tuner.rxsubchans & V4L2_TUNER_SUB_STEREO) != 0)
490                 stereo = true;
491         }
492 
493         vol = getVol();
494 
495         return true;
496     }
497     m_error = true;
498     emit error();
499 
500     return false;
501 }
502 
setVol(int v)503 void V4LRadioControl::setVol(int v)
504 {
505     int fd = ::open( "/dev/mixer", O_RDWR, 0 );
506     if ( fd < 0 )
507         return;
508     int volume = v;
509     if ( volume < 0 )
510         volume = 0;
511     else if ( volume > 100 )
512         volume = 100;
513     vol = volume;
514     volume += volume << 8;
515     ::ioctl( fd, MIXER_WRITE(SOUND_MIXER_VOLUME), &volume );
516     ::close( fd );
517 }
518 
getVol()519 int V4LRadioControl::getVol()
520 {
521     int fd = ::open( "/dev/mixer", O_RDWR, 0 );
522     if ( fd >= 0 ) {
523         int volume = 0;
524         ::ioctl( fd, MIXER_READ(SOUND_MIXER_VOLUME), &volume );
525         int left = ( volume & 0xFF );
526         int right = ( ( volume >> 8 ) & 0xFF );
527         if ( left > right )
528             vol = left;
529         else
530             vol = right;
531         ::close( fd );
532         return vol;
533     }
534     return 0;
535 }
536 
537