aboutsummaryrefslogtreecommitdiffstats
path: root/src/GSTInput.cpp
blob: 6c437d72a5dd21969c3ac81d4e9c719f2f51f195 (plain)
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
314
315
316
317
318
319
320
321
322
/* ------------------------------------------------------------------
 * Copyright (C) 2019 Matthias P. Braendli
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
 * express or implied.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 * -------------------------------------------------------------------
 */

#include <string>
#include <chrono>
#include <algorithm>
#include <functional>
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <cstring>

#include "GSTInput.h"

#include "config.h"

#if HAVE_GST

#include <gst/audio/audio.h>
#include <gst/app/gstappsink.h>

using namespace std;

GSTData::GSTData(SampleQueue<uint8_t>& samplequeue) :
    samplequeue(samplequeue)
{ }

GSTInput::GSTInput(const std::string& uri,
        const std::string& pipeline,
        int rate,
        unsigned channels,
        SampleQueue<uint8_t>& queue) :
    m_uri(uri),
    m_pipeline(pipeline),
    m_channels(channels),
    m_rate(rate),
    m_gst_data(queue)
{ }

static void error_cb(GstBus *bus, GstMessage *msg, GSTData *data)
{
    GError *err;
    gchar *debug_info;

    /* Print error details on the screen */
    gst_message_parse_error(msg, &err, &debug_info);
    g_printerr("Error received from element %s: %s\n", GST_OBJECT_NAME (msg->src), err->message);
    g_printerr("Debugging information: %s\n", debug_info ? debug_info : "none");
    g_clear_error(&err);
    g_free(debug_info);
}

static void cb_newpad(GstElement *decodebin, GstPad *pad, GSTData *data)
{
    /* only link once */
    GstPad *audiopad = gst_element_get_static_pad(data->audio_convert, "sink");
    if (GST_PAD_IS_LINKED(audiopad)) {
        g_object_unref(audiopad);
        return;
    }

    /* check media type */
    GstCaps *caps = gst_pad_query_caps(pad, NULL);
    GstStructure *str = gst_caps_get_structure(caps, 0);
    if (!g_strrstr(gst_structure_get_name(str), "audio")) {
        gst_caps_unref(caps);
        gst_object_unref(audiopad);
        return;
    }
    gst_caps_unref(caps);

    gst_pad_link(pad, audiopad);

    g_object_unref(audiopad);
}

static GstFlowReturn new_sample(GstElement *sink, GSTData *data) {
    /* Retrieve the buffer */
    GstSample* sample = gst_app_sink_pull_sample(GST_APP_SINK(sink));
    if (sample) {
        GstBuffer* buffer = gst_sample_get_buffer(sample);

        GstMapInfo map;
        gst_buffer_map(buffer, &map, GST_MAP_READ);

        data->samplequeue.push(map.data, map.size);

        gst_buffer_unmap(buffer, &map);
        gst_sample_unref(sample);

        return GST_FLOW_OK;
    }
    return GST_FLOW_ERROR;
}

void GSTInput::prepare()
{
    gst_init(nullptr, nullptr);

    if (not m_uri.empty()) {
        m_gst_data.uridecodebin = gst_element_factory_make("uridecodebin", "uridecodebin");
        assert(m_gst_data.uridecodebin != nullptr);
        g_object_set(m_gst_data.uridecodebin, "uri", m_uri.c_str(), nullptr);
        g_signal_connect(m_gst_data.uridecodebin, "pad-added", G_CALLBACK(cb_newpad), &m_gst_data);

        m_gst_data.audio_convert = gst_element_factory_make("audioconvert", "audio_convert");
        assert(m_gst_data.audio_convert != nullptr);

        m_gst_data.audio_resample = gst_element_factory_make("audioresample", "audio_resample");
        assert(m_gst_data.audio_resample != nullptr);
        g_object_set(m_gst_data.audio_resample,
#if (GST_VERSION_MAJOR == 1 && GST_VERSION_MINOR >= 10) || GST_VERSION_MAJOR > 1
                "sinc-filter-mode", GST_AUDIO_RESAMPLER_FILTER_MODE_FULL,
#else
#warning "GStreamer version is too old to set GST_AUDIO_RESAMPLER_FILTER_MODE_FULL" GST_VERSION_MAJOR
#endif
                "quality", 6, // between 0 and 10, 10 being best
                /* default audio-resampler-method: GST_AUDIO_RESAMPLER_METHOD_KAISER */
                NULL);

    }
    else if (not m_pipeline.empty()) {
        m_gst_data.custom_bin = gst_parse_bin_from_description(m_pipeline.c_str(), true, nullptr);
        if (m_gst_data.custom_bin == nullptr) {
            throw runtime_error("Could not instantiate pipeline");
        }
    }

    m_gst_data.caps_filter = gst_element_factory_make("capsfilter", "caps_filter");
    assert(m_gst_data.caps_filter != nullptr);

    GstAudioInfo info;
    gst_audio_info_set_format(&info, GST_AUDIO_FORMAT_S16, m_rate, m_channels, NULL);
    GstCaps *audio_caps = gst_audio_info_to_caps(&info);
    g_object_set(m_gst_data.caps_filter, "caps", audio_caps, NULL);

    m_gst_data.app_sink = gst_element_factory_make("appsink", "app_sink");
    assert(m_gst_data.app_sink != nullptr);

    m_gst_data.pipeline = gst_pipeline_new("pipeline");
    assert(m_gst_data.pipeline != nullptr);

    // TODO also set max-buffers
    g_object_set(m_gst_data.app_sink, "emit-signals", TRUE, "caps", audio_caps, NULL);
    g_signal_connect(m_gst_data.app_sink, "new-sample", G_CALLBACK(new_sample), &m_gst_data);
    gst_caps_unref(audio_caps);

    if (not m_uri.empty()) {
        gst_bin_add_many(GST_BIN(m_gst_data.pipeline),
                m_gst_data.uridecodebin,
                m_gst_data.audio_convert,
                m_gst_data.audio_resample,
                m_gst_data.caps_filter,
                m_gst_data.app_sink, NULL);

        if (gst_element_link_many(
                    m_gst_data.audio_convert,
                    m_gst_data.audio_resample,
                    m_gst_data.caps_filter,
                    m_gst_data.app_sink, NULL) != true) {
            throw runtime_error("Could not link GST elements");
        }
    }
    else if (not m_pipeline.empty()) {
        gst_bin_add_many(GST_BIN(m_gst_data.pipeline),
                m_gst_data.custom_bin,
                m_gst_data.caps_filter,
                m_gst_data.app_sink, NULL);

        if (gst_element_link_many(
                    m_gst_data.custom_bin,
                    m_gst_data.caps_filter,
                    m_gst_data.app_sink, NULL) != true) {
            throw runtime_error("Could not link GST elements");
        }
    }

    m_gst_data.bus = gst_element_get_bus(m_gst_data.pipeline);
    gst_bus_add_signal_watch(m_gst_data.bus);
    g_signal_connect(G_OBJECT(m_gst_data.bus), "message::error", (GCallback)error_cb, &m_gst_data);

    gst_element_set_state(m_gst_data.pipeline, GST_STATE_PLAYING);

    m_running = true;
    m_thread = std::thread(&GSTInput::process, this);
}

bool GSTInput::read_source(size_t num_bytes)
{
    return m_running;
}

ICY_TEXT_t GSTInput::get_icy_text() const
{
    ICY_TEXT_t now_playing;
    {
        std::lock_guard<std::mutex> lock(m_nowplaying_mutex);
        now_playing = m_nowplaying;
    }

    return now_playing;
}

void GSTInput::process()
{
    while (m_running) {
        GstMessage *msg = gst_bus_timed_pop(m_gst_data.bus, 100000);

        if (not msg) {
            continue;
        }

        switch (GST_MESSAGE_TYPE(msg)) {
            case GST_MESSAGE_BUFFERING:
                {
                    gint percent = 0;
                    gst_message_parse_buffering(msg, &percent);
                    //fprintf(stderr, "GST buffering %d\n", percent);
                    break;
                }
            case GST_MESSAGE_TAG:
                {
                    GstTagList *tags = nullptr;
                    gst_message_parse_tag(msg, &tags);
                    //fprintf(stderr, "Got tags from element %s\n", GST_OBJECT_NAME(msg->src));

                    struct user_data_t {
                        string new_artist;
                        string new_title;
                    } user_data;

                    auto extract_title = [](const GstTagList *list, const gchar *tag, void *user_data) {
                        GValue val = { 0, };

                        auto data = (user_data_t*)user_data;

                        gst_tag_list_copy_value(&val, list, tag);

                        if (G_VALUE_HOLDS_STRING(&val)) {
                            if (strcmp(tag, "title") == 0) {
                                data->new_title = g_value_get_string(&val);
                            }
                            else if (strcmp(tag, "artist") == 0) {
                                data->new_artist = g_value_get_string(&val);
                            }
                        }

                        g_value_unset(&val);
                    };

                    gst_tag_list_foreach(tags, extract_title, &user_data);

                    gst_tag_list_unref(tags);

                    {
                        std::lock_guard<std::mutex> lock(m_nowplaying_mutex);
                        if (user_data.new_artist.empty()) {
                            m_nowplaying.useNowPlaying(user_data.new_title);
                        }
                        else {
                            m_nowplaying.useArtistTitle(user_data.new_artist, user_data.new_title);
                        }
                    }
                    break;
                }
            case GST_MESSAGE_ERROR:
                {
                    GError *err = nullptr;
                    gst_message_parse_error(msg, &err, nullptr);
                    fprintf(stderr, "GST error: %s\n", err->message);
                    g_error_free(err);
                    m_fault = true;
                    break;
                }
            case GST_MESSAGE_EOS:
                m_fault = true;
                break;
            default:
                //fprintf(stderr, "GST message %s\n", gst_message_type_get_name(GST_MESSAGE_TYPE(msg)));
                break;
        }
        gst_message_unref(msg);
    }
}

GSTInput::~GSTInput()
{
    m_running = false;

    // Ensures push() doesn't get blocked
    m_gst_data.samplequeue.clear();

    if (m_thread.joinable()) {
        m_thread.join();
    }

    if (m_gst_data.bus) {
        gst_object_unref(m_gst_data.bus);
    }

    if (m_gst_data.pipeline) {
        gst_element_set_state(m_gst_data.pipeline, GST_STATE_NULL);
        gst_object_unref(m_gst_data.pipeline);
    }
}

#endif // HAVE_GST