File: GStreamerDTMFSenderBackend.cpp

package info (click to toggle)
webkit2gtk 2.49.90-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 445,664 kB
  • sloc: cpp: 3,797,881; javascript: 197,914; ansic: 161,337; python: 49,141; asm: 21,979; ruby: 18,539; perl: 16,723; xml: 4,623; yacc: 2,360; sh: 2,246; java: 2,019; lex: 1,327; pascal: 366; makefile: 295
file content (202 lines) | stat: -rw-r--r-- 6,586 bytes parent folder | download | duplicates (2)
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
/*
 *  Copyright (C) 2019-2025 Igalia S.L.
 *  Copyright (C) 2025 Comcast Inc.
 *
 *  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"
#include "GStreamerDTMFSenderBackend.h"

#if USE(GSTREAMER_WEBRTC)

#include "GStreamerCommon.h"
#include <wtf/HashFunctions.h>
#include <wtf/HashTraits.h>
#include <wtf/TZoneMallocInlines.h>
#include <wtf/WorkQueue.h>

namespace WebCore {

WTF_MAKE_TZONE_ALLOCATED_IMPL(GStreamerDTMFSenderBackend);

GST_DEBUG_CATEGORY(webkit_webrtc_dtmf_sender_debug);
#define GST_CAT_DEFAULT webkit_webrtc_dtmf_sender_debug

RefPtr<GStreamerDTMFSenderPrivate> GStreamerDTMFSenderPrivate::create()
{
    auto dtmfSrc = makeGStreamerElement("dtmfsrc"_s);
    if (!dtmfSrc)
        return nullptr;

    return adoptRef(*new GStreamerDTMFSenderPrivate(WTFMove(dtmfSrc)));
}

GStreamerDTMFSenderPrivate::GStreamerDTMFSenderPrivate(GRefPtr<GstElement>&& dtmfSrc)
    : m_dtmfSrc(WTFMove(dtmfSrc))
{
    static std::once_flag debugRegisteredFlag;
    std::call_once(debugRegisteredFlag, [] {
        GST_DEBUG_CATEGORY_INIT(webkit_webrtc_dtmf_sender_debug, "webkitwebrtcdtmfsender", 0, "WebKit WebRTC DTMF Sender");
    });

    static uint32_t nPipeline = 0;
    auto pipelineName = makeString("webkit-dtmf-playout-pipeline-"_s, nPipeline);
    m_pipeline = gst_pipeline_new(pipelineName.ascii().data());
    registerActivePipeline(m_pipeline);
    connectSimpleBusMessageCallback(m_pipeline.get());
    GST_DEBUG_OBJECT(m_pipeline.get(), "DTMF sender backend created");

    auto audioconvert = makeGStreamerElement("audioconvert"_s);
    auto audioresample = makeGStreamerElement("audioresample"_s);
    auto queue = gst_element_factory_make("queue", nullptr);
    auto sink = createPlatformAudioSink("dtmf"_s);
    gst_bin_add_many(GST_BIN_CAST(m_pipeline.get()), m_dtmfSrc.get(), audioconvert, audioresample, queue, sink, nullptr);
    gst_element_link_many(m_dtmfSrc.get(), audioconvert, audioresample, queue, sink, nullptr);
    gst_element_set_state(m_pipeline.get(), GST_STATE_PLAYING);
}

GStreamerDTMFSenderPrivate::~GStreamerDTMFSenderPrivate()
{
    if (!m_pipeline)
        return;

    unregisterPipeline(m_pipeline);
    disconnectSimpleBusMessageCallback(m_pipeline.get());
    gst_element_set_state(m_pipeline.get(), GST_STATE_NULL);
}

void GStreamerDTMFSenderPrivate::playTone(const RefPtr<RealtimeOutgoingAudioSourceGStreamer>& source, const char tone, size_t duration)
{
    static HashMap<char, int, WTF::IntHash<char>, WTF::UnsignedWithZeroKeyHashTraits<char>> tones = {
        { '0', 0 },
        { '1', 1 },
        { '2', 2 },
        { '3', 3 },
        { '4', 4 },
        { '5', 5 },
        { '6', 6 },
        { '7', 7 },
        { '8', 8 },
        { '9', 9 },
        { '*', 10 },
        { '#', 11 },
        { 'A', 12 },
        { 'B', 13 },
        { 'C', 14 },
        { 'D', 15 }
    };

    auto element = adoptGRef(gst_bin_get_by_name(GST_BIN_CAST(source->bin().get()), "dtmfSource"));
    auto pad = adoptGRef(gst_element_get_static_pad(element.get(), "src"));

    if (tone == ',') {
        GST_DEBUG_OBJECT(element.get(), "Playing silence for 2 seconds");
        sleep(2_s);
        m_onTonePlayed();
        GST_DEBUG_OBJECT(element.get(), "Playing tone %c DONE", tone);
        return;
    }

    auto toneNumber = tones.get(tone);
    GST_DEBUG_OBJECT(pad.get(), "Playing tone %c for %zu milliseconds", tone, duration);
    sendEvent(pad, toneNumber, 25, true);

    RunLoop::mainSingleton().dispatchAfter(Seconds::fromMilliseconds(duration), [toneNumber, pad = WTFMove(pad), weakThis = ThreadSafeWeakPtr { *this }] {
        RefPtr self = weakThis.get();
        if (!self)
            return;
        self->stopTone(pad, toneNumber);
    });
}

void GStreamerDTMFSenderPrivate::sendEvent(const GRefPtr<GstPad>& pad, int number, int volume, bool start)
{
    auto event = adoptGRef(gst_event_new_custom(GST_EVENT_CUSTOM_UPSTREAM, gst_structure_new("dtmf-event", "type", G_TYPE_INT, 1, "number", G_TYPE_INT, number, "volume", G_TYPE_INT, volume, "start", G_TYPE_BOOLEAN, start, nullptr)));
    gst_pad_send_event(pad.get(), event.ref());
    gst_element_send_event(m_dtmfSrc.get(), event.leakRef());
}

void GStreamerDTMFSenderPrivate::stopTone(const GRefPtr<GstPad>& pad, int tone)
{
    sendEvent(pad, tone, 0, false);
    GST_DEBUG_OBJECT(pad.get(), "Playing tone %c DONE", tone);
    m_onTonePlayed();
}

GStreamerDTMFSenderBackend::GStreamerDTMFSenderBackend(ThreadSafeWeakPtr<RealtimeOutgoingAudioSourceGStreamer>&& source)
    : m_source(WTFMove(source))
{
    RefPtr strongSource = m_source.get();
    if (!strongSource) {
        m_canInsertDTMF = false;
        return;
    }

    m_senderPrivate = GStreamerDTMFSenderPrivate::create();
    if (!m_senderPrivate) {
        m_canInsertDTMF = false;
        return;
    }

    m_canInsertDTMF = true;
}

bool GStreamerDTMFSenderBackend::canInsertDTMF()
{
    RefPtr source = m_source.get();
    if (!source)
        return false;
    return m_canInsertDTMF;
}

void GStreamerDTMFSenderBackend::playTone(const char tone, size_t duration, size_t interToneGap)
{
    RefPtr source = m_source.get();
    if (!source)
        return;

    if (!m_senderPrivate) [[unlikely]]
        return;

    m_duration = duration;
    m_interToneGap = interToneGap;

    if (m_isFirstTone)
        m_isFirstTone = false;
    else
        sleep(Seconds::fromMilliseconds(interToneGap));

    m_senderPrivate->playTone(source, tone, duration);
    m_tones.append(tone);
}

String GStreamerDTMFSenderBackend::tones() const
{
    return m_tones.toStringPreserveCapacity();
}

void GStreamerDTMFSenderBackend::onTonePlayed(Function<void()>&& onTonePlayed)
{
    if (!m_senderPrivate) [[unlikely]]
        return;
    m_senderPrivate->setOnTonePlayedCallback(WTFMove(onTonePlayed));
}

#undef GST_CAT_DEFAULT

} // namespace WebCore

#endif // USE(GSTREAMER_WEBRTC)