[GStreamer][WebRTC] Add API to enable/disable device mocks
[WebKit-https.git] / Source / WebCore / platform / mediastream / gstreamer / GStreamerAudioCapturer.cpp
1 /*
2  * Copyright (C) 2018 Metrological Group B.V.
3  * Author: Thibault Saunier <tsaunier@igalia.com>
4  * Author: Alejandro G. Castro  <alex@igalia.com>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public License
17  * aint with this library; see the file COPYING.LIB.  If not, write to
18  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  */
21
22 #include "config.h"
23
24 #if ENABLE(MEDIA_STREAM) && USE(LIBWEBRTC) && USE(GSTREAMER)
25 #include "GStreamerAudioCapturer.h"
26
27 #include "LibWebRTCAudioFormat.h"
28
29 #include <gst/app/gstappsink.h>
30
31 namespace WebCore {
32
33 GStreamerAudioCapturer::GStreamerAudioCapturer(GStreamerCaptureDevice device)
34     : GStreamerCapturer(device, adoptGRef(gst_caps_new_simple("audio/x-raw", "rate", G_TYPE_INT, LibWebRTCAudioFormat::sampleRate, nullptr)))
35 {
36 }
37
38 GStreamerAudioCapturer::GStreamerAudioCapturer()
39     : GStreamerCapturer("audiotestsrc", adoptGRef(gst_caps_new_simple("audio/x-raw", "rate", G_TYPE_INT, LibWebRTCAudioFormat::sampleRate, nullptr)))
40 {
41 }
42
43 GstElement* GStreamerAudioCapturer::createSource()
44 {
45     GstElement* source = GStreamerCapturer::createSource();
46
47     if (!m_device)
48         gst_util_set_object_arg(G_OBJECT(m_src.get()), "wave", "ticks");
49
50     return source;
51 }
52
53 GstElement* GStreamerAudioCapturer::createConverter()
54 {
55     auto converter = gst_parse_bin_from_description("audioconvert ! audioresample", TRUE, nullptr);
56
57     ASSERT(converter);
58
59     return converter;
60 }
61
62 bool GStreamerAudioCapturer::setSampleRate(int sampleRate)
63 {
64
65     if (sampleRate > 0) {
66         GST_INFO_OBJECT(m_pipeline.get(), "Setting SampleRate %d", sampleRate);
67         m_caps = adoptGRef(gst_caps_new_simple("audio/x-raw", "rate", G_TYPE_INT, sampleRate, nullptr));
68     } else {
69         GST_INFO_OBJECT(m_pipeline.get(), "Not forcing sample rate");
70         m_caps = adoptGRef(gst_caps_new_empty_simple("audio/x-raw"));
71     }
72
73     if (!m_capsfilter.get())
74         return false;
75
76     g_object_set(m_capsfilter.get(), "caps", m_caps.get(), nullptr);
77
78     return true;
79 }
80
81 } // namespace WebCore
82
83 #endif // ENABLE(MEDIA_STREAM) && USE(LIBWEBRTC) && USE(GSTREAMER)