1 | /* |
2 | * Copyright (C) 2011, 2012 Igalia S.L |
3 | * Copyright (C) 2014 Sebastian Dröge <sebastian@centricular.com> |
4 | * |
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. |
9 | * |
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. |
14 | * |
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 |
18 | */ |
19 | |
20 | #include "config.h" |
21 | |
22 | #if ENABLE(WEB_AUDIO) |
23 | |
24 | #include "AudioDestinationGStreamer.h" |
25 | |
26 | #include "AudioChannel.h" |
27 | #include "AudioSourceProvider.h" |
28 | #include "GRefPtrGStreamer.h" |
29 | #include "Logging.h" |
30 | #include "WebKitWebAudioSourceGStreamer.h" |
31 | #include <gst/audio/gstaudiobasesink.h> |
32 | #include <gst/gst.h> |
33 | #include <wtf/glib/GUniquePtr.h> |
34 | #include <wtf/glib/RunLoopSourcePriority.h> |
35 | |
36 | namespace WebCore { |
37 | |
38 | // Size of the AudioBus for playback. The webkitwebaudiosrc element |
39 | // needs to handle this number of frames per cycle as well. |
40 | const unsigned framesToPull = 128; |
41 | |
42 | gboolean messageCallback(GstBus*, GstMessage* message, AudioDestinationGStreamer* destination) |
43 | { |
44 | return destination->handleMessage(message); |
45 | } |
46 | |
47 | static void autoAudioSinkChildAddedCallback(GstChildProxy*, GObject* object, gchar*, gpointer) |
48 | { |
49 | if (GST_IS_AUDIO_BASE_SINK(object)) |
50 | g_object_set(GST_AUDIO_BASE_SINK(object), "buffer-time" , static_cast<gint64>(100000), nullptr); |
51 | } |
52 | |
53 | std::unique_ptr<AudioDestination> AudioDestination::create(AudioIOCallback& callback, const String&, unsigned numberOfInputChannels, unsigned numberOfOutputChannels, float sampleRate) |
54 | { |
55 | // FIXME: make use of inputDeviceId as appropriate. |
56 | |
57 | // FIXME: Add support for local/live audio input. |
58 | if (numberOfInputChannels) |
59 | LOG(Media, "AudioDestination::create(%u, %u, %f) - unhandled input channels" , numberOfInputChannels, numberOfOutputChannels, sampleRate); |
60 | |
61 | // FIXME: Add support for multi-channel (> stereo) output. |
62 | if (numberOfOutputChannels != 2) |
63 | LOG(Media, "AudioDestination::create(%u, %u, %f) - unhandled output channels" , numberOfInputChannels, numberOfOutputChannels, sampleRate); |
64 | |
65 | return std::make_unique<AudioDestinationGStreamer>(callback, sampleRate); |
66 | } |
67 | |
68 | float AudioDestination::hardwareSampleRate() |
69 | { |
70 | return 44100; |
71 | } |
72 | |
73 | unsigned long AudioDestination::maxChannelCount() |
74 | { |
75 | // FIXME: query the default audio hardware device to return the actual number |
76 | // of channels of the device. Also see corresponding FIXME in create(). |
77 | return 0; |
78 | } |
79 | |
80 | AudioDestinationGStreamer::AudioDestinationGStreamer(AudioIOCallback& callback, float sampleRate) |
81 | : m_callback(callback) |
82 | , m_renderBus(AudioBus::create(2, framesToPull, false)) |
83 | , m_sampleRate(sampleRate) |
84 | , m_isPlaying(false) |
85 | { |
86 | m_pipeline = gst_pipeline_new("play" ); |
87 | GRefPtr<GstBus> bus = adoptGRef(gst_pipeline_get_bus(GST_PIPELINE(m_pipeline))); |
88 | ASSERT(bus); |
89 | gst_bus_add_signal_watch_full(bus.get(), RunLoopSourcePriority::RunLoopDispatcher); |
90 | g_signal_connect(bus.get(), "message" , G_CALLBACK(messageCallback), this); |
91 | |
92 | GstElement* webkitAudioSrc = reinterpret_cast<GstElement*>(g_object_new(WEBKIT_TYPE_WEB_AUDIO_SRC, |
93 | "rate" , sampleRate, |
94 | "bus" , m_renderBus.get(), |
95 | "provider" , &m_callback, |
96 | "frames" , framesToPull, nullptr)); |
97 | |
98 | GRefPtr<GstElement> audioSink = gst_element_factory_make("autoaudiosink" , nullptr); |
99 | m_audioSinkAvailable = audioSink; |
100 | if (!audioSink) { |
101 | LOG_ERROR("Failed to create GStreamer autoaudiosink element" ); |
102 | return; |
103 | } |
104 | |
105 | g_signal_connect(audioSink.get(), "child-added" , G_CALLBACK(autoAudioSinkChildAddedCallback), nullptr); |
106 | |
107 | // Autoaudiosink does the real sink detection in the GST_STATE_NULL->READY transition |
108 | // so it's best to roll it to READY as soon as possible to ensure the underlying platform |
109 | // audiosink was loaded correctly. |
110 | GstStateChangeReturn stateChangeReturn = gst_element_set_state(audioSink.get(), GST_STATE_READY); |
111 | if (stateChangeReturn == GST_STATE_CHANGE_FAILURE) { |
112 | LOG_ERROR("Failed to change autoaudiosink element state" ); |
113 | gst_element_set_state(audioSink.get(), GST_STATE_NULL); |
114 | m_audioSinkAvailable = false; |
115 | return; |
116 | } |
117 | |
118 | GstElement* audioConvert = gst_element_factory_make("audioconvert" , nullptr); |
119 | GstElement* audioResample = gst_element_factory_make("audioresample" , nullptr); |
120 | gst_bin_add_many(GST_BIN(m_pipeline), webkitAudioSrc, audioConvert, audioResample, audioSink.get(), nullptr); |
121 | |
122 | // Link src pads from webkitAudioSrc to audioConvert ! audioResample ! autoaudiosink. |
123 | gst_element_link_pads_full(webkitAudioSrc, "src" , audioConvert, "sink" , GST_PAD_LINK_CHECK_NOTHING); |
124 | gst_element_link_pads_full(audioConvert, "src" , audioResample, "sink" , GST_PAD_LINK_CHECK_NOTHING); |
125 | gst_element_link_pads_full(audioResample, "src" , audioSink.get(), "sink" , GST_PAD_LINK_CHECK_NOTHING); |
126 | } |
127 | |
128 | AudioDestinationGStreamer::~AudioDestinationGStreamer() |
129 | { |
130 | GRefPtr<GstBus> bus = adoptGRef(gst_pipeline_get_bus(GST_PIPELINE(m_pipeline))); |
131 | ASSERT(bus); |
132 | g_signal_handlers_disconnect_by_func(bus.get(), reinterpret_cast<gpointer>(messageCallback), this); |
133 | gst_bus_remove_signal_watch(bus.get()); |
134 | |
135 | gst_element_set_state(m_pipeline, GST_STATE_NULL); |
136 | gst_object_unref(m_pipeline); |
137 | } |
138 | |
139 | gboolean AudioDestinationGStreamer::handleMessage(GstMessage* message) |
140 | { |
141 | GUniqueOutPtr<GError> error; |
142 | GUniqueOutPtr<gchar> debug; |
143 | |
144 | switch (GST_MESSAGE_TYPE(message)) { |
145 | case GST_MESSAGE_WARNING: |
146 | gst_message_parse_warning(message, &error.outPtr(), &debug.outPtr()); |
147 | g_warning("Warning: %d, %s. Debug output: %s" , error->code, error->message, debug.get()); |
148 | break; |
149 | case GST_MESSAGE_ERROR: |
150 | gst_message_parse_error(message, &error.outPtr(), &debug.outPtr()); |
151 | g_warning("Error: %d, %s. Debug output: %s" , error->code, error->message, debug.get()); |
152 | gst_element_set_state(m_pipeline, GST_STATE_NULL); |
153 | m_isPlaying = false; |
154 | break; |
155 | default: |
156 | break; |
157 | } |
158 | return TRUE; |
159 | } |
160 | |
161 | void AudioDestinationGStreamer::start() |
162 | { |
163 | ASSERT(m_audioSinkAvailable); |
164 | if (!m_audioSinkAvailable) |
165 | return; |
166 | |
167 | if (gst_element_set_state(m_pipeline, GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE) { |
168 | g_warning("Error: Failed to set pipeline to playing" ); |
169 | m_isPlaying = false; |
170 | return; |
171 | } |
172 | |
173 | m_isPlaying = true; |
174 | } |
175 | |
176 | void AudioDestinationGStreamer::stop() |
177 | { |
178 | ASSERT(m_audioSinkAvailable); |
179 | if (!m_audioSinkAvailable) |
180 | return; |
181 | |
182 | gst_element_set_state(m_pipeline, GST_STATE_PAUSED); |
183 | m_isPlaying = false; |
184 | } |
185 | |
186 | } // namespace WebCore |
187 | |
188 | #endif // ENABLE(WEB_AUDIO) |
189 | |