1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
|
/*
* Copyright (C) 2018 Metrological Group B.V.
* Copyright (C) 2020 Igalia S.L.
* Author: Thibault Saunier <tsaunier@igalia.com>
* Author: Alejandro G. Castro <alex@igalia.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* aint with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "config.h"
#if ENABLE(MEDIA_STREAM) && USE(GSTREAMER)
#include "GStreamerAudioCaptureSource.h"
#include "GStreamerAudioData.h"
#include "GStreamerAudioStreamDescription.h"
#include "GStreamerCaptureDeviceManager.h"
#include <gst/app/gstappsink.h>
#include <gst/gst.h>
#include <wtf/NeverDestroyed.h>
#include <wtf/text/MakeString.h>
namespace WebCore {
static DoubleCapabilityRange defaultVolumeCapability()
{
return { 0.0, 1.0 };
}
const static RealtimeMediaSourceCapabilities::EchoCancellation defaultEchoCancellationCapability = RealtimeMediaSourceCapabilities::EchoCancellation::OnOrOff;
GST_DEBUG_CATEGORY(webkit_audio_capture_source_debug);
#define GST_CAT_DEFAULT webkit_audio_capture_source_debug
class GStreamerAudioCaptureSourceFactory : public AudioCaptureFactory {
public:
CaptureSourceOrError createAudioCaptureSource(const CaptureDevice& device, MediaDeviceHashSalts&& hashSalts, const MediaConstraints* constraints, std::optional<PageIdentifier>) final
{
return GStreamerAudioCaptureSource::create(String { device.persistentId() }, WTFMove(hashSalts), constraints);
}
private:
CaptureDeviceManager& audioCaptureDeviceManager() final { return GStreamerAudioCaptureDeviceManager::singleton(); }
const Vector<CaptureDevice>& speakerDevices() const final { return GStreamerAudioCaptureDeviceManager::singleton().speakerDevices(); }
};
static GStreamerAudioCaptureSourceFactory& libWebRTCAudioCaptureSourceFactory()
{
static NeverDestroyed<GStreamerAudioCaptureSourceFactory> factory;
return factory.get();
}
CaptureSourceOrError GStreamerAudioCaptureSource::create(String&& deviceID, MediaDeviceHashSalts&& hashSalts, const MediaConstraints* constraints)
{
auto device = GStreamerAudioCaptureDeviceManager::singleton().gstreamerDeviceWithUID(deviceID);
if (!device) {
auto errorMessage = makeString("GStreamerAudioCaptureSource::create(): GStreamer did not find the device: "_s, deviceID, '.');
return CaptureSourceOrError({ WTFMove(errorMessage), MediaAccessDenialReason::PermissionDenied });
}
auto source = adoptRef(*new GStreamerAudioCaptureSource(WTFMove(*device), WTFMove(hashSalts)));
if (constraints) {
if (auto result = source->applyConstraints(*constraints))
return CaptureSourceOrError(CaptureSourceError { result->invalidConstraint });
}
return CaptureSourceOrError(WTFMove(source));
}
AudioCaptureFactory& GStreamerAudioCaptureSource::factory()
{
return libWebRTCAudioCaptureSourceFactory();
}
GStreamerAudioCaptureSource::GStreamerAudioCaptureSource(GStreamerCaptureDevice&& device, MediaDeviceHashSalts&& hashSalts)
: RealtimeMediaSource(device, WTFMove(hashSalts))
, m_capturer(adoptRef(*new GStreamerAudioCapturer(WTFMove(device))))
{
ensureGStreamerInitialized();
static std::once_flag debugRegisteredFlag;
std::call_once(debugRegisteredFlag, [] {
GST_DEBUG_CATEGORY_INIT(webkit_audio_capture_source_debug, "webkitaudiocapturesource", 0, "WebKit Audio Capture Source.");
});
auto& singleton = GStreamerAudioCaptureDeviceManager::singleton();
singleton.registerCapturer(m_capturer.copyRef());
}
GStreamerAudioCaptureSource::~GStreamerAudioCaptureSource()
{
auto& singleton = GStreamerAudioCaptureDeviceManager::singleton();
singleton.unregisterCapturer(*m_capturer);
}
void GStreamerAudioCaptureSource::captureEnded()
{
m_capturer->stop();
captureFailed();
}
std::pair<GstClockTime, GstClockTime> GStreamerAudioCaptureSource::queryCaptureLatency() const
{
if (!m_capturer)
return { GST_CLOCK_TIME_NONE, GST_CLOCK_TIME_NONE };
return m_capturer->queryLatency();
}
void GStreamerAudioCaptureSource::startProducingData()
{
m_capturer->setupPipeline();
m_capturer->setSampleRate(sampleRate());
m_capturer->setSinkAudioCallback([this](auto&& sample, auto&& presentationTime) {
auto bufferSize = gst_buffer_get_size(gst_sample_get_buffer(sample.get()));
GStreamerAudioData frames(WTFMove(sample));
GStreamerAudioStreamDescription description(frames.getAudioInfo());
audioSamplesAvailable(presentationTime, frames, description, bufferSize / description.getInfo().bpf);
});
m_capturer->start();
}
void GStreamerAudioCaptureSource::stopProducingData()
{
m_capturer->stop();
}
const RealtimeMediaSourceCapabilities& GStreamerAudioCaptureSource::capabilities()
{
if (m_capabilities)
return m_capabilities.value();
uint i;
auto caps = m_capturer->caps();
int minSampleRate = 0, maxSampleRate = 0;
for (i = 0; i < gst_caps_get_size(caps.get()); i++) {
int capabilityMinSampleRate = 0, capabilityMaxSampleRate = 0;
GstStructure* str = gst_caps_get_structure(caps.get(), i);
// Only accept raw audio for now.
if (!gst_structure_has_name(str, "audio/x-raw"))
continue;
gst_structure_get(str, "rate", GST_TYPE_INT_RANGE, &capabilityMinSampleRate, &capabilityMaxSampleRate, nullptr);
if (i > 0) {
minSampleRate = std::min(minSampleRate, capabilityMinSampleRate);
maxSampleRate = std::max(maxSampleRate, capabilityMaxSampleRate);
} else {
minSampleRate = capabilityMinSampleRate;
maxSampleRate = capabilityMaxSampleRate;
}
}
RealtimeMediaSourceCapabilities capabilities(settings().supportedConstraints());
capabilities.setDeviceId(hashedId());
capabilities.setEchoCancellation(defaultEchoCancellationCapability);
capabilities.setVolume(defaultVolumeCapability());
capabilities.setSampleRate({ minSampleRate, maxSampleRate });
m_capabilities = WTFMove(capabilities);
return m_capabilities.value();
}
void GStreamerAudioCaptureSource::settingsDidChange(OptionSet<RealtimeMediaSourceSettings::Flag> settings)
{
if (settings.contains(RealtimeMediaSourceSettings::Flag::SampleRate))
m_capturer->setSampleRate(sampleRate());
}
const RealtimeMediaSourceSettings& GStreamerAudioCaptureSource::settings()
{
if (!m_currentSettings) {
RealtimeMediaSourceSettings settings;
settings.setDeviceId(hashedId());
RealtimeMediaSourceSupportedConstraints supportedConstraints;
supportedConstraints.setSupportsDeviceId(true);
supportedConstraints.setSupportsEchoCancellation(true);
supportedConstraints.setSupportsVolume(true);
supportedConstraints.setSupportsSampleRate(true);
settings.setSupportedConstraints(supportedConstraints);
m_currentSettings = WTFMove(settings);
}
m_currentSettings->setVolume(volume());
m_currentSettings->setSampleRate(sampleRate());
m_currentSettings->setEchoCancellation(echoCancellation());
return m_currentSettings.value();
}
bool GStreamerAudioCaptureSource::interrupted() const
{
if (m_capturer->pipeline())
return m_capturer->isInterrupted() || RealtimeMediaSource::interrupted();
return RealtimeMediaSource::interrupted();
}
void GStreamerAudioCaptureSource::setInterruptedForTesting(bool isInterrupted)
{
m_capturer->setInterrupted(isInterrupted);
RealtimeMediaSource::setInterruptedForTesting(isInterrupted);
}
#undef GST_CAT_DEFAULT
} // namespace WebCore
#endif // ENABLE(MEDIA_STREAM) && USE(GSTREAMER)
|