1 | // Copyright (C) 2016 The Qt Company Ltd. |
---|---|
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
3 | |
4 | #include "qgstreameraudiodevice_p.h" |
5 | |
6 | #include <common/qgst_p.h> |
7 | #include <common/qgstutils_p.h> |
8 | #include <private/qplatformmediaintegration_p.h> |
9 | |
10 | QT_BEGIN_NAMESPACE |
11 | |
12 | QGStreamerAudioDeviceInfo::QGStreamerAudioDeviceInfo(GstDevice *d, const QByteArray &device, |
13 | QAudioDevice::Mode mode) |
14 | : QAudioDevicePrivate(device, mode), |
15 | gstDevice{ |
16 | d, |
17 | QGstDeviceHandle::NeedsRef, |
18 | } |
19 | { |
20 | QGString name{ |
21 | gst_device_get_display_name(device: gstDevice.get()), |
22 | }; |
23 | description = name.toQString(); |
24 | |
25 | auto caps = QGstCaps(gst_device_get_caps(device: gstDevice.get()), QGstCaps::HasRef); |
26 | int size = caps.size(); |
27 | for (int i = 0; i < size; ++i) { |
28 | auto c = caps.at(index: i); |
29 | if (c.name() == "audio/x-raw") { |
30 | auto rate = c["rate"].toIntRange(); |
31 | if (rate) { |
32 | minimumSampleRate = rate->min; |
33 | maximumSampleRate = rate->max; |
34 | } |
35 | auto channels = c["channels"].toIntRange(); |
36 | if (channels) { |
37 | minimumChannelCount = channels->min; |
38 | maximumChannelCount = channels->max; |
39 | } |
40 | supportedSampleFormats = c["format"].getSampleFormats(); |
41 | } |
42 | } |
43 | |
44 | preferredFormat.setChannelCount(qBound(min: minimumChannelCount, val: 2, max: maximumChannelCount)); |
45 | preferredFormat.setSampleRate(qBound(min: minimumSampleRate, val: 48000, max: maximumSampleRate)); |
46 | QAudioFormat::SampleFormat f = QAudioFormat::Int16; |
47 | if (!supportedSampleFormats.contains(t: f)) |
48 | f = supportedSampleFormats.value(i: 0, defaultValue: QAudioFormat::Unknown); |
49 | preferredFormat.setSampleFormat(f); |
50 | } |
51 | |
52 | QGStreamerCustomAudioDeviceInfo::QGStreamerCustomAudioDeviceInfo( |
53 | const QByteArray &gstreamerPipeline, QAudioDevice::Mode mode) |
54 | : QAudioDevicePrivate{ |
55 | gstreamerPipeline, |
56 | mode, |
57 | } |
58 | { |
59 | } |
60 | |
61 | QAudioDevice qMakeCustomGStreamerAudioInput(const QByteArray &gstreamerPipeline) |
62 | { |
63 | auto deviceInfo = std::make_unique<QGStreamerCustomAudioDeviceInfo>(args: gstreamerPipeline, |
64 | args: QAudioDevice::Mode::Input); |
65 | |
66 | return deviceInfo.release()->create(); |
67 | } |
68 | |
69 | QAudioDevice qMakeCustomGStreamerAudioOutput(const QByteArray &gstreamerPipeline) |
70 | { |
71 | auto deviceInfo = std::make_unique<QGStreamerCustomAudioDeviceInfo>(args: gstreamerPipeline, |
72 | args: QAudioDevice::Mode::Output); |
73 | |
74 | return deviceInfo.release()->create(); |
75 | } |
76 | |
77 | bool isCustomAudioDevice(const QAudioDevicePrivate *device) |
78 | { |
79 | return dynamic_cast<const QGStreamerCustomAudioDeviceInfo *>(device); |
80 | } |
81 | |
82 | bool isCustomAudioDevice(const QAudioDevice &device) |
83 | { |
84 | return isCustomAudioDevice(device: device.handle()); |
85 | } |
86 | |
87 | QT_END_NAMESPACE |
88 |