2 * Copyright (C) 2011, 2012 Igalia S.L
3 * Copyright (C) 2014 Sebastian Dröge <sebastian@centricular.com>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 #include "AudioDestinationGStreamer.h"
26 #include "AudioChannel.h"
27 #include "AudioSourceProvider.h"
28 #include "GRefPtrGStreamer.h"
30 #include "WebKitWebAudioSourceGStreamer.h"
32 #include <wtf/gobject/GUniquePtr.h>
36 // Size of the AudioBus for playback. The webkitwebaudiosrc element
37 // needs to handle this number of frames per cycle as well.
38 const unsigned framesToPull = 128;
40 gboolean messageCallback(GstBus*, GstMessage* message, AudioDestinationGStreamer* destination)
42 return destination->handleMessage(message);
45 std::unique_ptr<AudioDestination> AudioDestination::create(AudioIOCallback& callback, const String&, unsigned numberOfInputChannels, unsigned numberOfOutputChannels, float sampleRate)
47 // FIXME: make use of inputDeviceId as appropriate.
49 // FIXME: Add support for local/live audio input.
50 if (numberOfInputChannels)
51 LOG(Media, "AudioDestination::create(%u, %u, %f) - unhandled input channels", numberOfInputChannels, numberOfOutputChannels, sampleRate);
53 // FIXME: Add support for multi-channel (> stereo) output.
54 if (numberOfOutputChannels != 2)
55 LOG(Media, "AudioDestination::create(%u, %u, %f) - unhandled output channels", numberOfInputChannels, numberOfOutputChannels, sampleRate);
57 return std::make_unique<AudioDestinationGStreamer>(callback, sampleRate);
60 float AudioDestination::hardwareSampleRate()
65 unsigned long AudioDestination::maxChannelCount()
67 // FIXME: query the default audio hardware device to return the actual number
68 // of channels of the device. Also see corresponding FIXME in create().
72 AudioDestinationGStreamer::AudioDestinationGStreamer(AudioIOCallback& callback, float sampleRate)
73 : m_callback(callback)
74 , m_renderBus(AudioBus::create(2, framesToPull, false))
75 , m_sampleRate(sampleRate)
78 m_pipeline = gst_pipeline_new("play");
79 GRefPtr<GstBus> bus = adoptGRef(gst_pipeline_get_bus(GST_PIPELINE(m_pipeline)));
81 gst_bus_add_signal_watch(bus.get());
82 g_signal_connect(bus.get(), "message", G_CALLBACK(messageCallback), this);
84 GstElement* webkitAudioSrc = reinterpret_cast<GstElement*>(g_object_new(WEBKIT_TYPE_WEB_AUDIO_SRC,
86 "bus", m_renderBus.get(),
87 "provider", &m_callback,
88 "frames", framesToPull, NULL));
90 GRefPtr<GstPad> srcPad = adoptGRef(gst_element_get_static_pad(webkitAudioSrc, "src"));
92 GRefPtr<GstElement> audioSink = gst_element_factory_make("autoaudiosink", 0);
93 m_audioSinkAvailable = audioSink;
95 LOG_ERROR("Failed to create GStreamer autoaudiosink element");
99 // Autoaudiosink does the real sink detection in the GST_STATE_NULL->READY transition
100 // so it's best to roll it to READY as soon as possible to ensure the underlying platform
101 // audiosink was loaded correctly.
102 GstStateChangeReturn stateChangeReturn = gst_element_set_state(audioSink.get(), GST_STATE_READY);
103 if (stateChangeReturn == GST_STATE_CHANGE_FAILURE) {
104 LOG_ERROR("Failed to change autoaudiosink element state");
105 gst_element_set_state(audioSink.get(), GST_STATE_NULL);
106 m_audioSinkAvailable = false;
110 GstElement* audioConvert = gst_element_factory_make("audioconvert", 0);
111 GstElement* audioResample = gst_element_factory_make("audioresample", 0);
112 gst_bin_add_many(GST_BIN(m_pipeline), webkitAudioSrc, audioConvert, audioResample, audioSink.get(), NULL);
114 // Link wavparse's src pad to audioconvert sink pad.
115 GRefPtr<GstPad> sinkPad = adoptGRef(gst_element_get_static_pad(audioConvert, "sink"));
116 gst_pad_link_full(srcPad.get(), sinkPad.get(), GST_PAD_LINK_CHECK_NOTHING);
118 // Link audioconvert to audiosink and roll states.
119 gst_element_link_pads_full(audioConvert, "src", audioResample, "sink", GST_PAD_LINK_CHECK_NOTHING);
120 gst_element_link_pads_full(audioResample, "src", audioSink.get(), "sink", GST_PAD_LINK_CHECK_NOTHING);
123 AudioDestinationGStreamer::~AudioDestinationGStreamer()
125 GRefPtr<GstBus> bus = adoptGRef(gst_pipeline_get_bus(GST_PIPELINE(m_pipeline)));
127 g_signal_handlers_disconnect_by_func(bus.get(), reinterpret_cast<gpointer>(messageCallback), this);
128 gst_bus_remove_signal_watch(bus.get());
130 gst_element_set_state(m_pipeline, GST_STATE_NULL);
131 gst_object_unref(m_pipeline);
134 gboolean AudioDestinationGStreamer::handleMessage(GstMessage* message)
136 GUniqueOutPtr<GError> error;
137 GUniqueOutPtr<gchar> debug;
139 switch (GST_MESSAGE_TYPE(message)) {
140 case GST_MESSAGE_WARNING:
141 gst_message_parse_warning(message, &error.outPtr(), &debug.outPtr());
142 g_warning("Warning: %d, %s. Debug output: %s", error->code, error->message, debug.get());
144 case GST_MESSAGE_ERROR:
145 gst_message_parse_error(message, &error.outPtr(), &debug.outPtr());
146 g_warning("Error: %d, %s. Debug output: %s", error->code, error->message, debug.get());
147 gst_element_set_state(m_pipeline, GST_STATE_NULL);
156 void AudioDestinationGStreamer::start()
158 ASSERT(m_audioSinkAvailable);
159 if (!m_audioSinkAvailable)
162 if (gst_element_set_state(m_pipeline, GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE) {
163 g_warning("Error: Failed to set pipeline to playing");
171 void AudioDestinationGStreamer::stop()
173 ASSERT(m_audioSinkAvailable);
174 if (!m_audioSinkAvailable)
177 gst_element_set_state(m_pipeline, GST_STATE_PAUSED);
181 } // namespace WebCore
183 #endif // ENABLE(WEB_AUDIO)