~aleteoryx/muditaos

ref: eeafb5f1be091a34446b842dbde1b7d54cc029a7 muditaos/module-audio/board/linux/PulseAudioWrapper.cpp -rw-r--r-- 5.2 KiB
eeafb5f1 — Lefucjusz [MOS-783] Moved battery config file to '/user/data' 3 years ago
                                                                                
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
// Copyright (c) 2017-2022, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "PulseAudioWrapper.hpp"
#include <pthread.h>
#include <iostream>
#include <utility>
#include <stdexcept>

namespace audio::pulse_audio
{
    Stream::Stream(AudioFormat audio_format_, pa_context *ctx) : audio_format{audio_format_}
    {
        /// PulseAudio requests at least SampleRate bytes of data to start playback
        /// Due to how module-audio's streams operate we can load a bit more data to the cache than needed.
        /// Doubling the buffer size is cheap and simple solution.
        cache.reserve(audio_format.getSampleRate() * 2);

        pa_sample_spec sample_spec;
        sample_spec.channels = audio_format.getChannels();
        sample_spec.rate     = audio_format.getSampleRate();
        sample_spec.format   = PA_SAMPLE_S16LE;

        if ((stream = pa_stream_new(ctx, "PureOSStream", &sample_spec, nullptr)) == nullptr) {
            throw std::runtime_error(pa_strerror(pa_context_errno(ctx)));
        }
    }

    Stream::~Stream()
    {
        pa_stream_disconnect(stream);
        pa_stream_unref(stream);
    }

    void Stream::insert(AbstractStream::Span span)
    {
        std::copy(span.data, span.dataEnd(), &cache[cache_pos]);
        cache_pos += span.dataSize;
    }

    void Stream::consume()
    {
        if (pa_stream_write(stream, cache.data(), cache_pos, NULL, 0, PA_SEEK_RELATIVE) < 0) {
            fprintf(stderr, "pa_stream_write() failed\n");
            return;
        }
        cache_pos = 0;
    }

    std::size_t Stream::bytes() const
    {
        return cache_pos;
    }

    pa_stream *Stream::raw()
    {
        return stream;
    }

    Context::Context()
    {
        pthread_create(
            &tid,
            nullptr,
            [](void *arg) -> void * {
                /// Any 'external' threads that are not part of the FreeRTOS simulator port must be treated as a
                /// interrupt source and have to have all the signals masked in order to not 'steal' work from the
                /// threads managed by the port.
                sigset_t set;
                sigfillset(&set);
                pthread_sigmask(SIG_SETMASK, &set, NULL);

                auto *inst = static_cast<Context *>(arg);
                return inst->worker();
            },
            this);
    }

    void *Context::worker()
    {
        mainloop     = pa_mainloop_new();
        mainloop_api = pa_mainloop_get_api(mainloop);
        context      = pa_context_new(mainloop_api, "PureOSContext");
        pa_context_set_state_callback(
            context,
            [](pa_context *, void *arg) {
                auto *inst = static_cast<Context *>(arg);
                inst->context_state_callback();
            },
            this);
        pa_context_connect(context, nullptr, {}, nullptr);

        int ret = 1;
        if (pa_mainloop_run(mainloop, &ret) < 0) {
            fprintf(stderr, "pa_mainloop_run() failed.\n");
            std::abort();
        }

        return nullptr;
    }

    void Context::context_state_callback()
    {
        assert(context);

        switch (pa_context_get_state(context)) {
        case PA_CONTEXT_CONNECTING:
        case PA_CONTEXT_AUTHORIZING:
        case PA_CONTEXT_SETTING_NAME:
        case PA_CONTEXT_READY:
            break;

        case PA_CONTEXT_TERMINATED:
            quit(0);
            fprintf(stderr, "PulseAudio connection terminated.\n");
            break;

        case PA_CONTEXT_FAILED:
        default:
            fprintf(stderr, "Connection failure: %s\n", pa_strerror(pa_context_errno(context)));
            quit(1);
        }
    }

    void Context::stream_write_cb(size_t length)
    {
        write_cb(length);
    }

    Context::~Context()
    {
        quit();
        pthread_join(tid, nullptr);

        if (context != nullptr) {
            pa_context_unref(context);
            context = nullptr;
        }

        if (mainloop != nullptr) {
            pa_mainloop_free(mainloop);
            mainloop     = nullptr;
            mainloop_api = nullptr;
        }
    }

    void Context::quit(int ret)
    {
        if (mainloop_api != nullptr) {
            mainloop_api->quit(mainloop_api, ret);
        }
    }

    Stream *Context::open_stream(AudioFormat audio_format_, WriteCallback write_cb_)
    {
        try {
            stream   = std::make_unique<Stream>(audio_format_, context);
            write_cb = write_cb_;

            pa_stream_set_write_callback(
                stream->raw(),
                [](pa_stream *, size_t length, void *arg) {
                    auto *inst = static_cast<Context *>(arg);
                    inst->stream_write_cb(length);
                },
                this);

            if (pa_stream_connect_playback(stream->raw(), nullptr, nullptr, {}, nullptr, nullptr) < 0) {
                throw std::runtime_error(pa_strerror(pa_context_errno(context)));
            }
        }
        catch (const std::runtime_error &e) {
            fprintf(stderr, e.what());
            return nullptr;
        }

        return stream.get();
    }
    void Context::close_stream()
    {
        stream = {};
    }
} // namespace audio::pulse_audio