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 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
|
/*
* Copyright (C) 2011, 2012 Igalia S.L
* Copyright (C) 2014 Sebastian Dröge <sebastian@centricular.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "config.h"
#if ENABLE(WEB_AUDIO)
#include "AudioDestinationGStreamer.h"
#include "AudioSourceProvider.h"
#include "AudioUtilities.h"
#include "GStreamerCommon.h"
#include "GStreamerQuirks.h"
#include "WebKitWebAudioSourceGStreamer.h"
#include <gst/audio/gstaudiobasesink.h>
#include <gst/gst.h>
#include <wtf/PrintStream.h>
#include <wtf/glib/GUniquePtr.h>
#include <wtf/glib/RunLoopSourcePriority.h>
#include <wtf/text/MakeString.h>
namespace WebCore {
GST_DEBUG_CATEGORY(webkit_audio_destination_debug);
#define GST_CAT_DEFAULT webkit_audio_destination_debug
static void initializeAudioDestinationDebugCategory()
{
ensureGStreamerInitialized();
registerWebKitGStreamerElements();
static std::once_flag onceFlag;
std::call_once(onceFlag, [] {
GST_DEBUG_CATEGORY_INIT(webkit_audio_destination_debug, "webkitaudiodestination", 0, "WebKit WebAudio Destination");
});
}
static unsigned long maximumNumberOfOutputChannels()
{
initializeAudioDestinationDebugCategory();
static int count = 0;
static std::once_flag onceFlag;
std::call_once(onceFlag, [] {
auto monitor = adoptGRef(gst_device_monitor_new());
auto caps = adoptGRef(gst_caps_new_empty_simple("audio/x-raw"));
gst_device_monitor_add_filter(monitor.get(), "Audio/Sink", caps.get());
bool started = gst_device_monitor_start(monitor.get());
auto* devices = gst_device_monitor_get_devices(monitor.get());
while (devices) {
auto device = adoptGRef(GST_DEVICE_CAST(devices->data));
auto caps = adoptGRef(gst_device_get_caps(device.get()));
unsigned size = gst_caps_get_size(caps.get());
for (unsigned i = 0; i < size; i++) {
auto* structure = gst_caps_get_structure(caps.get(), i);
if (gstStructureGetName(structure) != "audio/x-raw"_s)
continue;
if (auto value = gstStructureGet<int>(structure, "channels"_s))
count = std::max(count, *value);
}
devices = g_list_delete_link(devices, devices);
}
GST_DEBUG("maximumNumberOfOutputChannels: %d", count);
if (started)
gst_device_monitor_stop(monitor.get());
});
return count;
}
Ref<AudioDestination> AudioDestination::create(AudioIOCallback& callback, const String&, unsigned numberOfInputChannels, unsigned numberOfOutputChannels, float sampleRate)
{
initializeAudioDestinationDebugCategory();
// FIXME: make use of inputDeviceId as appropriate.
// FIXME: Add support for local/live audio input.
if (numberOfInputChannels)
WTFLogAlways("AudioDestination::create(%u, %u, %f) - unhandled input channels", numberOfInputChannels, numberOfOutputChannels, sampleRate);
return adoptRef(*new AudioDestinationGStreamer(callback, numberOfOutputChannels, sampleRate));
}
float AudioDestination::hardwareSampleRate()
{
return 44100;
}
unsigned long AudioDestination::maxChannelCount()
{
return maximumNumberOfOutputChannels();
}
AudioDestinationGStreamer::AudioDestinationGStreamer(AudioIOCallback& callback, unsigned long numberOfOutputChannels, float sampleRate)
: AudioDestination(callback, sampleRate)
, m_renderBus(AudioBus::create(numberOfOutputChannels, AudioUtilities::renderQuantumSize, false))
{
static Atomic<uint32_t> pipelineId;
m_pipeline = gst_pipeline_new(makeString("audio-destination-"_s, pipelineId.exchangeAdd(1)).ascii().data());
registerActivePipeline(m_pipeline);
connectSimpleBusMessageCallback(m_pipeline.get(), [this](GstMessage* message) {
this->handleMessage(message);
});
m_src = GST_ELEMENT_CAST(g_object_new(WEBKIT_TYPE_WEB_AUDIO_SRC, "rate", sampleRate,
"destination", this, "frames", AudioUtilities::renderQuantumSize, nullptr));
webkitWebAudioSourceSetBus(WEBKIT_WEB_AUDIO_SRC(m_src.get()), m_renderBus);
auto& quirksManager = GStreamerQuirksManager::singleton();
GRefPtr<GstElement> audioSink = quirksManager.createWebAudioSink();
m_audioSinkAvailable = audioSink;
if (!audioSink) {
GST_ERROR("Failed to create GStreamer audio sink element");
return;
}
// Probe platform early on for a working audio output device in autoaudiosink.
auto nameView = StringView::fromLatin1(GST_OBJECT_NAME(audioSink.get()));
if (nameView.startsWith("autoaudiosink"_s)) {
g_signal_connect(audioSink.get(), "child-added", G_CALLBACK(+[](GstChildProxy*, GObject* object, gchar*, gpointer) {
if (GST_IS_AUDIO_BASE_SINK(object))
g_object_set(GST_AUDIO_BASE_SINK(object), "buffer-time", static_cast<gint64>(100000), nullptr);
}), nullptr);
// Autoaudiosink does the real sink detection in the GST_STATE_NULL->READY transition
// so it's best to roll it to READY as soon as possible to ensure the underlying platform
// audiosink was loaded correctly.
GstStateChangeReturn stateChangeReturn = gst_element_set_state(audioSink.get(), GST_STATE_READY);
if (stateChangeReturn == GST_STATE_CHANGE_FAILURE) {
GST_ERROR("Failed to change autoaudiosink element state");
gst_element_set_state(audioSink.get(), GST_STATE_NULL);
m_audioSinkAvailable = false;
return;
}
}
GstElement* audioConvert = makeGStreamerElement("audioconvert", nullptr);
GstElement* audioResample = makeGStreamerElement("audioresample", nullptr);
auto queue = gst_element_factory_make("queue", nullptr);
g_object_set(queue, "max-size-buffers", 2, "max-size-bytes", 0, "max-size-time", static_cast<guint64>(0), nullptr);
gst_bin_add_many(GST_BIN_CAST(m_pipeline.get()), m_src.get(), audioConvert, audioResample, queue, audioSink.get(), nullptr);
// Link src pads from webkitAudioSrc to audioConvert ! audioResample ! [capsfilter !] queue ! autoaudiosink.
gst_element_link_pads_full(m_src.get(), "src", audioConvert, "sink", GST_PAD_LINK_CHECK_NOTHING);
gst_element_link_pads_full(audioConvert, "src", audioResample, "sink", GST_PAD_LINK_CHECK_NOTHING);
if (!webkitGstCheckVersion(1, 20, 4)) {
// Force audio conversion to 'interleaved' format (by audioconvert element).
// 1) Some platform sinks don't support non-interleaved audio without special caps (rialtowebaudiosink).
// 2) Interaudio sink/src doesn't fully support non-interleaved audio (webkit audio sink)
// 3) audiomixer doesn't support non-interleaved audio in output pipeline (webkit audio sink)
GstElement* capsFilter = makeGStreamerElement("capsfilter", nullptr);
GRefPtr<GstCaps> caps = adoptGRef(gst_caps_new_simple("audio/x-raw", "layout", G_TYPE_STRING, "interleaved", nullptr));
g_object_set(capsFilter, "caps", caps.get(), nullptr);
gst_bin_add(GST_BIN_CAST(m_pipeline.get()), capsFilter);
gst_element_link_pads_full(audioResample, "src", capsFilter, "sink", GST_PAD_LINK_CHECK_NOTHING);
gst_element_link_pads_full(capsFilter, "src", queue, "sink", GST_PAD_LINK_CHECK_NOTHING);
} else
gst_element_link_pads_full(audioResample, "src", queue, "sink", GST_PAD_LINK_CHECK_NOTHING);
gst_element_link_pads_full(queue, "src", audioSink.get(), "sink", GST_PAD_LINK_CHECK_NOTHING);
}
AudioDestinationGStreamer::~AudioDestinationGStreamer()
{
GST_DEBUG_OBJECT(m_pipeline.get(), "Disposing");
if (LIKELY(m_src))
g_object_set(m_src.get(), "destination", nullptr, nullptr);
unregisterPipeline(m_pipeline);
disconnectSimpleBusMessageCallback(m_pipeline.get());
gst_element_set_state(m_pipeline.get(), GST_STATE_NULL);
notifyStopResult(true);
}
unsigned AudioDestinationGStreamer::framesPerBuffer() const
{
return AudioUtilities::renderQuantumSize;
}
bool AudioDestinationGStreamer::handleMessage(GstMessage* message)
{
switch (GST_MESSAGE_TYPE(message)) {
case GST_MESSAGE_ERROR:
notifyIsPlaying(false);
break;
case GST_MESSAGE_LATENCY:
gst_bin_recalculate_latency(GST_BIN_CAST(m_pipeline.get()));
break;
default:
break;
}
return true;
}
void AudioDestinationGStreamer::start(Function<void(Function<void()>&&)>&& dispatchToRenderThread, CompletionHandler<void(bool)>&& completionHandler)
{
webkitWebAudioSourceSetDispatchToRenderThreadFunction(WEBKIT_WEB_AUDIO_SRC(m_src.get()), WTFMove(dispatchToRenderThread));
startRendering(WTFMove(completionHandler));
}
void AudioDestinationGStreamer::startRendering(CompletionHandler<void(bool)>&& completionHandler)
{
ASSERT(m_audioSinkAvailable);
m_startupCompletionHandler = WTFMove(completionHandler);
GST_DEBUG_OBJECT(m_pipeline.get(), "Starting audio rendering, sink %s", m_audioSinkAvailable ? "available" : "not available");
if (m_isPlaying) {
notifyStartupResult(true);
return;
}
if (!m_audioSinkAvailable) {
notifyStartupResult(false);
return;
}
notifyStartupResult(webkitGstSetElementStateSynchronously(m_pipeline.get(), GST_STATE_PLAYING, [this](GstMessage* message) -> bool {
return handleMessage(message);
}));
}
void AudioDestinationGStreamer::stop(CompletionHandler<void(bool)>&& completionHandler)
{
stopRendering(WTFMove(completionHandler));
webkitWebAudioSourceSetDispatchToRenderThreadFunction(WEBKIT_WEB_AUDIO_SRC(m_src.get()), nullptr);
}
void AudioDestinationGStreamer::stopRendering(CompletionHandler<void(bool)>&& completionHandler)
{
ASSERT(m_audioSinkAvailable);
m_stopCompletionHandler = WTFMove(completionHandler);
GST_DEBUG_OBJECT(m_pipeline.get(), "Stopping audio rendering, sink %s", m_audioSinkAvailable ? "available" : "not available");
if (!m_isPlaying) {
GST_DEBUG_OBJECT(m_pipeline.get(), "Already stopped");
notifyStopResult(true);
return;
}
if (!m_audioSinkAvailable) {
notifyStopResult(false);
return;
}
notifyStopResult(webkitGstSetElementStateSynchronously(m_pipeline.get(), GST_STATE_READY, [this](GstMessage* message) -> bool {
return handleMessage(message);
}));
}
void AudioDestinationGStreamer::notifyStartupResult(bool success)
{
if (success)
notifyIsPlaying(true);
callOnMainThreadAndWait([this, completionHandler = WTFMove(m_startupCompletionHandler), success]() mutable {
#ifdef GST_DISABLE_GST_DEBUG
UNUSED_VARIABLE(this);
#endif
GST_DEBUG_OBJECT(m_pipeline.get(), "Has start completion handler: %s", boolForPrinting(!!completionHandler));
if (completionHandler)
completionHandler(success);
});
}
void AudioDestinationGStreamer::notifyStopResult(bool success)
{
if (success)
notifyIsPlaying(false);
callOnMainThreadAndWait([this, completionHandler = WTFMove(m_stopCompletionHandler), success]() mutable {
#ifdef GST_DISABLE_GST_DEBUG
UNUSED_VARIABLE(this);
#endif
GST_DEBUG_OBJECT(m_pipeline.get(), "Has stop completion handler: %s", boolForPrinting(!!completionHandler));
if (completionHandler)
completionHandler(success);
});
}
void AudioDestinationGStreamer::notifyIsPlaying(bool isPlaying)
{
if (m_isPlaying == isPlaying)
return;
GST_DEBUG("Is playing: %s", boolForPrinting(isPlaying));
m_isPlaying = isPlaying;
if (m_callback)
m_callback->isPlayingDidChange();
}
#undef GST_CAT_DEFAULT
} // namespace WebCore
#endif // ENABLE(WEB_AUDIO)
|