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 "qradiotuner.h" |
41 | #include "qmediaservice.h" |
42 | #include "qmediaobject_p.h" |
43 | #include "qradiotunercontrol.h" |
44 | #include "qradiodata.h" |
45 | #include "qmediaserviceprovider_p.h" |
46 | |
47 | #include <QPair> |
48 | |
49 | |
50 | QT_BEGIN_NAMESPACE |
51 | |
52 | |
53 | static void qRegisterRadioTunerMetaTypes() |
54 | { |
55 | qRegisterMetaType<QRadioTuner::Band>(); |
56 | qRegisterMetaType<QRadioTuner::Error>(); |
57 | qRegisterMetaType<QRadioTuner::SearchMode>(); |
58 | qRegisterMetaType<QRadioTuner::State>(); |
59 | qRegisterMetaType<QRadioTuner::StereoMode>(); |
60 | } |
61 | |
62 | Q_CONSTRUCTOR_FUNCTION(qRegisterRadioTunerMetaTypes) |
63 | |
64 | |
65 | /*! |
66 | \class QRadioTuner |
67 | \obsolete |
68 | \brief The QRadioTuner class provides an interface to the systems analog radio device. |
69 | |
70 | \inmodule QtMultimedia |
71 | \ingroup multimedia |
72 | \ingroup multimedia_radio |
73 | |
74 | You can control the systems analog radio device using this interface, for example: |
75 | |
76 | \snippet multimedia-snippets/media.cpp Radio tuner |
77 | |
78 | The radio object will emit signals for any changes in state such as: |
79 | bandChanged(), frequencyChanged(), stereoStatusChanged(), searchingChanged(), |
80 | signalStrengthChanged(), volumeChanged(), mutedChanged(). |
81 | |
82 | You can change between the frequency bands using setBand() however it is recommended |
83 | that you check to make sure the band is available first using isBandSupported(). |
84 | |
85 | \sa QRadioData, {Radio Overview} |
86 | */ |
87 | |
88 | |
89 | class QRadioTunerPrivate : public QMediaObjectPrivate |
90 | { |
91 | public: |
92 | QRadioTunerPrivate():provider(nullptr), control(nullptr), radioData(nullptr) {} |
93 | QMediaServiceProvider *provider; |
94 | QRadioTunerControl* control; |
95 | QRadioData *radioData; |
96 | }; |
97 | |
98 | |
99 | |
100 | /*! |
101 | Constructs a radio tuner based on a media service allocated by the default |
102 | media service provider. |
103 | |
104 | The \a parent is passed to QMediaObject. |
105 | */ |
106 | |
107 | QRadioTuner::QRadioTuner(QObject *parent): |
108 | QMediaObject(*new QRadioTunerPrivate, |
109 | parent, |
110 | QMediaServiceProvider::defaultServiceProvider()->requestService(Q_MEDIASERVICE_RADIO)) |
111 | { |
112 | Q_D(QRadioTuner); |
113 | |
114 | d->provider = QMediaServiceProvider::defaultServiceProvider(); |
115 | |
116 | if (d->service != nullptr) { |
117 | d->control = qobject_cast<QRadioTunerControl*>(object: d->service->requestControl(QRadioTunerControl_iid)); |
118 | if (d->control != nullptr) { |
119 | connect(asender: d->control, SIGNAL(stateChanged(QRadioTuner::State)), SIGNAL(stateChanged(QRadioTuner::State))); |
120 | connect(asender: d->control, SIGNAL(bandChanged(QRadioTuner::Band)), SIGNAL(bandChanged(QRadioTuner::Band))); |
121 | connect(asender: d->control, SIGNAL(frequencyChanged(int)), SIGNAL(frequencyChanged(int))); |
122 | connect(asender: d->control, SIGNAL(stereoStatusChanged(bool)), SIGNAL(stereoStatusChanged(bool))); |
123 | connect(asender: d->control, SIGNAL(searchingChanged(bool)), SIGNAL(searchingChanged(bool))); |
124 | connect(asender: d->control, SIGNAL(signalStrengthChanged(int)), SIGNAL(signalStrengthChanged(int))); |
125 | connect(asender: d->control, SIGNAL(volumeChanged(int)), SIGNAL(volumeChanged(int))); |
126 | connect(asender: d->control, SIGNAL(mutedChanged(bool)), SIGNAL(mutedChanged(bool))); |
127 | connect(asender: d->control, SIGNAL(stationFound(int,QString)), SIGNAL(stationFound(int,QString))); |
128 | connect(asender: d->control, SIGNAL(antennaConnectedChanged(bool)), SIGNAL(antennaConnectedChanged(bool))); |
129 | connect(asender: d->control, SIGNAL(error(QRadioTuner::Error)), SIGNAL(error(QRadioTuner::Error))); |
130 | } |
131 | |
132 | d->radioData = new QRadioData(this, this); |
133 | } |
134 | } |
135 | |
136 | /*! |
137 | Destroys a radio tuner. |
138 | */ |
139 | |
140 | QRadioTuner::~QRadioTuner() |
141 | { |
142 | Q_D(QRadioTuner); |
143 | |
144 | if (d->radioData) |
145 | delete d->radioData; |
146 | |
147 | if (d->service && d->control) |
148 | d->service->releaseControl(control: d->control); |
149 | |
150 | d->provider->releaseService(service: d->service); |
151 | } |
152 | |
153 | /*! |
154 | Returns the availability of the radio tuner. |
155 | */ |
156 | QMultimedia::AvailabilityStatus QRadioTuner::availability() const |
157 | { |
158 | if (d_func()->control == nullptr) |
159 | return QMultimedia::ServiceMissing; |
160 | |
161 | if (!d_func()->control->isAntennaConnected()) |
162 | return QMultimedia::ResourceError; |
163 | |
164 | return QMediaObject::availability(); |
165 | } |
166 | |
167 | /*! |
168 | \property QRadioTuner::state |
169 | Return the current radio tuner state. |
170 | |
171 | \sa QRadioTuner::State |
172 | */ |
173 | |
174 | QRadioTuner::State QRadioTuner::state() const |
175 | { |
176 | return d_func()->control ? |
177 | d_func()->control->state() : QRadioTuner::StoppedState; |
178 | } |
179 | |
180 | /*! |
181 | \property QRadioTuner::band |
182 | \brief the frequency band a radio tuner is tuned to. |
183 | |
184 | \sa QRadioTuner::Band |
185 | */ |
186 | |
187 | QRadioTuner::Band QRadioTuner::band() const |
188 | { |
189 | Q_D(const QRadioTuner); |
190 | |
191 | if (d->control != nullptr) |
192 | return d->control->band(); |
193 | |
194 | return QRadioTuner::FM; |
195 | } |
196 | |
197 | /*! |
198 | \property QRadioTuner::frequency |
199 | \brief the frequency in Hertz a radio tuner is tuned to. |
200 | */ |
201 | |
202 | int QRadioTuner::frequency() const |
203 | { |
204 | Q_D(const QRadioTuner); |
205 | |
206 | if (d->control != nullptr) |
207 | return d->control->frequency(); |
208 | |
209 | return 0; |
210 | } |
211 | |
212 | /*! |
213 | Returns the number of Hertz to increment the frequency by when stepping through frequencies |
214 | within a given \a band. |
215 | */ |
216 | |
217 | int QRadioTuner::frequencyStep(QRadioTuner::Band band) const |
218 | { |
219 | Q_D(const QRadioTuner); |
220 | |
221 | if (d->control != nullptr) |
222 | return d->control->frequencyStep(b: band); |
223 | |
224 | return 0; |
225 | } |
226 | |
227 | /*! |
228 | Returns a frequency \a band's minimum and maximum frequency. |
229 | */ |
230 | |
231 | QPair<int,int> QRadioTuner::frequencyRange(QRadioTuner::Band band) const |
232 | { |
233 | Q_D(const QRadioTuner); |
234 | |
235 | if (d->control != nullptr) |
236 | return d->control->frequencyRange(b: band); |
237 | |
238 | return qMakePair<int,int>(x: 0,y: 0); |
239 | } |
240 | |
241 | /*! |
242 | \property QRadioTuner::stereo |
243 | \brief whether a radio tuner is receiving a stereo signal. |
244 | */ |
245 | |
246 | bool QRadioTuner::isStereo() const |
247 | { |
248 | Q_D(const QRadioTuner); |
249 | |
250 | if (d->control != nullptr) |
251 | return d->control->isStereo(); |
252 | |
253 | return false; |
254 | } |
255 | |
256 | |
257 | /*! |
258 | \property QRadioTuner::stereoMode |
259 | \brief the stereo mode of a radio tuner. |
260 | */ |
261 | |
262 | QRadioTuner::StereoMode QRadioTuner::stereoMode() const |
263 | { |
264 | Q_D(const QRadioTuner); |
265 | |
266 | if (d->control != nullptr) |
267 | return d->control->stereoMode(); |
268 | |
269 | return QRadioTuner::Auto; |
270 | } |
271 | |
272 | void QRadioTuner::setStereoMode(QRadioTuner::StereoMode mode) |
273 | { |
274 | Q_D(QRadioTuner); |
275 | |
276 | if (d->control != nullptr) |
277 | return d->control->setStereoMode(mode); |
278 | } |
279 | |
280 | /*! |
281 | Identifies if a frequency \a band is supported by a radio tuner. |
282 | |
283 | Returns true if the band is supported, and false if it is not. |
284 | */ |
285 | |
286 | bool QRadioTuner::isBandSupported(QRadioTuner::Band band) const |
287 | { |
288 | Q_D(const QRadioTuner); |
289 | |
290 | if (d->control != nullptr) |
291 | return d->control->isBandSupported(b: band); |
292 | |
293 | return false; |
294 | } |
295 | |
296 | /*! |
297 | Activate the radio device. |
298 | */ |
299 | |
300 | void QRadioTuner::start() |
301 | { |
302 | Q_D(const QRadioTuner); |
303 | |
304 | if (d->control != nullptr) |
305 | d->control->start(); |
306 | } |
307 | |
308 | /*! |
309 | Deactivate the radio device. |
310 | */ |
311 | |
312 | void QRadioTuner::stop() |
313 | { |
314 | Q_D(const QRadioTuner); |
315 | |
316 | if (d->control != nullptr) |
317 | d->control->stop(); |
318 | } |
319 | |
320 | /*! |
321 | \property QRadioTuner::signalStrength |
322 | \brief the strength of the current radio signal as a percentage. |
323 | */ |
324 | |
325 | int QRadioTuner::signalStrength() const |
326 | { |
327 | Q_D(const QRadioTuner); |
328 | |
329 | if (d->control != nullptr) |
330 | return d->control->signalStrength(); |
331 | |
332 | return 0; |
333 | } |
334 | |
335 | /*! |
336 | \property QRadioTuner::volume |
337 | \brief the volume of a radio tuner's audio output as a percentage. |
338 | */ |
339 | |
340 | |
341 | int QRadioTuner::volume() const |
342 | { |
343 | Q_D(const QRadioTuner); |
344 | |
345 | if (d->control != nullptr) |
346 | return d->control->volume(); |
347 | |
348 | return 0; |
349 | } |
350 | |
351 | /*! |
352 | \property QRadioTuner::muted |
353 | \brief whether a radio tuner's audio output is muted. |
354 | */ |
355 | |
356 | bool QRadioTuner::isMuted() const |
357 | { |
358 | Q_D(const QRadioTuner); |
359 | |
360 | if (d->control != nullptr) |
361 | return d->control->isMuted(); |
362 | |
363 | return false; |
364 | } |
365 | |
366 | /*! |
367 | Sets a radio tuner's frequency \a band. |
368 | |
369 | Changing the band will reset the \l frequency to the new band's minimum frequency. |
370 | */ |
371 | |
372 | void QRadioTuner::setBand(QRadioTuner::Band band) |
373 | { |
374 | Q_D(QRadioTuner); |
375 | |
376 | if (d->control != nullptr) |
377 | d->control->setBand(band); |
378 | } |
379 | |
380 | /*! |
381 | Sets a radio tuner's \a frequency. |
382 | |
383 | If the tuner is set to a frequency outside the current \l band, the band will be changed to |
384 | one occupied by the new frequency. |
385 | */ |
386 | |
387 | void QRadioTuner::setFrequency(int frequency) |
388 | { |
389 | Q_D(QRadioTuner); |
390 | |
391 | if (d->control != nullptr) |
392 | d->control->setFrequency(frequency); |
393 | } |
394 | |
395 | void QRadioTuner::setVolume(int volume) |
396 | { |
397 | Q_D(QRadioTuner); |
398 | |
399 | if (d->control != nullptr) |
400 | d->control->setVolume(volume); |
401 | } |
402 | |
403 | void QRadioTuner::setMuted(bool muted) |
404 | { |
405 | Q_D(QRadioTuner); |
406 | |
407 | if (d->control != nullptr) |
408 | d->control->setMuted(muted); |
409 | } |
410 | |
411 | /*! |
412 | \property QRadioTuner::searching |
413 | \brief whether a radio tuner is currently scanning for a signal. |
414 | |
415 | \sa searchForward(), searchBackward(), cancelSearch() |
416 | */ |
417 | |
418 | bool QRadioTuner::isSearching() const |
419 | { |
420 | Q_D(const QRadioTuner); |
421 | |
422 | if (d->control != nullptr) |
423 | return d->control->isSearching(); |
424 | |
425 | return false; |
426 | } |
427 | |
428 | /*! |
429 | \property QRadioTuner::antennaConnected |
430 | \brief whether there is an antenna connected |
431 | */ |
432 | bool QRadioTuner::isAntennaConnected() const |
433 | { |
434 | Q_D(const QRadioTuner); |
435 | |
436 | if (d->control != nullptr) |
437 | return d->control->isAntennaConnected(); |
438 | |
439 | return false; |
440 | } |
441 | |
442 | /*! |
443 | Starts a forward scan for a signal, starting from the current \l frequency. |
444 | |
445 | \sa searchBackward(), cancelSearch(), searching |
446 | */ |
447 | |
448 | void QRadioTuner::searchForward() |
449 | { |
450 | Q_D(QRadioTuner); |
451 | |
452 | if (d->control != nullptr) |
453 | d->control->searchForward(); |
454 | } |
455 | |
456 | /*! |
457 | Starts a backwards scan for a signal, starting from the current \l frequency. |
458 | |
459 | \sa searchForward(), cancelSearch(), searching |
460 | */ |
461 | |
462 | void QRadioTuner::searchBackward() |
463 | { |
464 | Q_D(QRadioTuner); |
465 | |
466 | if (d->control != nullptr) |
467 | d->control->searchBackward(); |
468 | } |
469 | |
470 | /*! |
471 | \enum QRadioTuner::SearchMode |
472 | |
473 | Enumerates how the radio tuner should search for stations. |
474 | |
475 | \value SearchFast Use only signal strength when searching. |
476 | \value SearchGetStationId After finding a strong signal, wait for the RDS station id (PI) before continuing. |
477 | */ |
478 | |
479 | /*! |
480 | Search all stations in current band |
481 | |
482 | Emits QRadioTuner::stationFound(int, QString) for every found station. |
483 | After searching is completed, QRadioTuner::searchingChanged(bool) is |
484 | emitted (false). If \a searchMode is set to SearchGetStationId, searching |
485 | waits for station id (PI) on each frequency. |
486 | |
487 | \sa searchForward(), searchBackward(), searching |
488 | */ |
489 | |
490 | void QRadioTuner::searchAllStations(QRadioTuner::SearchMode searchMode) |
491 | { |
492 | Q_D(const QRadioTuner); |
493 | |
494 | if (d->control != nullptr) |
495 | d->control->searchAllStations(searchMode); |
496 | } |
497 | |
498 | /*! |
499 | Stops scanning for a signal. |
500 | |
501 | \sa searchForward(), searchBackward(), searching |
502 | */ |
503 | |
504 | void QRadioTuner::cancelSearch() |
505 | { |
506 | Q_D(QRadioTuner); |
507 | |
508 | if (d->control != nullptr) |
509 | d->control->cancelSearch(); |
510 | } |
511 | |
512 | /*! |
513 | Returns the error state of a radio tuner. |
514 | |
515 | \sa errorString() |
516 | */ |
517 | |
518 | QRadioTuner::Error QRadioTuner::error() const |
519 | { |
520 | Q_D(const QRadioTuner); |
521 | |
522 | if (d->control != nullptr) |
523 | return d->control->error(); |
524 | |
525 | return QRadioTuner::ResourceError; |
526 | } |
527 | |
528 | /*! |
529 | Returns a description of a radio tuner's error state. |
530 | |
531 | \sa error() |
532 | */ |
533 | |
534 | QString QRadioTuner::errorString() const |
535 | { |
536 | Q_D(const QRadioTuner); |
537 | |
538 | if (d->control != nullptr) |
539 | return d->control->errorString(); |
540 | |
541 | return QString(); |
542 | } |
543 | |
544 | /*! |
545 | \property QRadioTuner::radioData |
546 | \brief holds an instance of \l QRadioData |
547 | |
548 | The instance of QRadioData is already bound to this instance of QRadioTuner. |
549 | */ |
550 | QRadioData *QRadioTuner::radioData() const |
551 | { |
552 | return d_func()->radioData; |
553 | } |
554 | |
555 | |
556 | /*! |
557 | \fn void QRadioTuner::bandChanged(QRadioTuner::Band band) |
558 | |
559 | Signals a radio tuner's \a band has changed. |
560 | */ |
561 | |
562 | /*! |
563 | \fn void QRadioTuner::frequencyChanged(int frequency) |
564 | |
565 | Signals that the \a frequency a radio tuner is tuned to has changed. |
566 | */ |
567 | |
568 | /*! |
569 | \fn void QRadioTuner::mutedChanged(bool muted) |
570 | |
571 | Signals that the \a muted state of a radio tuner's audio output has changed. |
572 | */ |
573 | |
574 | /*! |
575 | \fn void QRadioTuner::volumeChanged(int volume) |
576 | |
577 | Signals that the \a volume of a radio tuner's audio output has changed. |
578 | */ |
579 | |
580 | /*! |
581 | \fn void QRadioTuner::searchingChanged(bool searching) |
582 | |
583 | Signals that the \a searching state of a radio tuner has changed. |
584 | */ |
585 | |
586 | /*! |
587 | \fn void QRadioTuner::stereoStatusChanged(bool stereo) |
588 | |
589 | Signals that the \a stereo state of a radio tuner has changed. |
590 | */ |
591 | |
592 | /*! |
593 | \fn void QRadioTuner::signalStrengthChanged(int strength) |
594 | |
595 | Signals that the \a strength of the signal received by a radio tuner has changed. |
596 | */ |
597 | |
598 | /*! |
599 | \fn void QRadioTuner::stationFound(int frequency, QString stationId) |
600 | |
601 | Signals that a station was found in \a frequency with \a stationId Program |
602 | Identification code. |
603 | */ |
604 | |
605 | /*! |
606 | \fn void QRadioTuner::error(QRadioTuner::Error error) |
607 | |
608 | Signals that an \a error occurred. |
609 | */ |
610 | |
611 | /*! |
612 | \enum QRadioTuner::State |
613 | |
614 | Enumerates radio tuner states. |
615 | |
616 | \value ActiveState The tuner is started and active. |
617 | \value StoppedState The tuner device is stopped. |
618 | */ |
619 | |
620 | |
621 | /*! |
622 | \enum QRadioTuner::Band |
623 | |
624 | Enumerates radio frequency bands. |
625 | |
626 | \value AM 520 to 1610 kHz, 9 or 10kHz channel spacing, extended 1610 to 1710 kHz |
627 | \value FM 87.5 to 108.0 MHz, except Japan 76-90 MHz |
628 | \value SW 1.711 to 30.0 MHz, divided into 15 bands. 5kHz channel spacing |
629 | \value LW 148.5 to 283.5 kHz, 9kHz channel spacing (Europe, Africa, Asia) |
630 | \value FM2 range not defined, used when area supports more than one FM range. |
631 | */ |
632 | |
633 | /*! |
634 | \enum QRadioTuner::Error |
635 | |
636 | Enumerates radio tuner error conditions. |
637 | |
638 | \value NoError No errors have occurred. |
639 | \value ResourceError There is no radio service available. |
640 | \value OpenError Unable to open radio device. |
641 | \value OutOfRangeError An attempt to set a frequency or band that is not supported by radio device. |
642 | */ |
643 | |
644 | /*! |
645 | \enum QRadioTuner::StereoMode |
646 | |
647 | Enumerates radio tuner policy for receiving stereo signals. |
648 | |
649 | \value ForceStereo Provide stereo mode, converting if required. |
650 | \value ForceMono Provide mono mode, converting if required. |
651 | \value Auto Uses the stereo mode matching the station. |
652 | */ |
653 | |
654 | /*! \fn void QRadioTuner::stateChanged(QRadioTuner::State state) |
655 | This signal is emitted when the state changes to \a state. |
656 | */ |
657 | |
658 | QT_END_NAMESPACE |
659 | |
660 | #include "moc_qradiotuner.cpp" |
661 | |