~aleteoryx/muditaos

ref: b890bcd6e32223e624d0d63af50bcd2219469d32 muditaos/module-audio/Audio/decoder/DecoderWorker.hpp -rw-r--r-- 1.8 KiB
b890bcd6 — Marcin Smoczyński [EGD-5260] Add A2DP playback to audio 5 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
// Copyright (c) 2017-2020, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#pragma once

#include "Audio/StreamQueuedEventsListener.hpp"

#include <Service/Worker.hpp>
#include <semaphore.hpp>

namespace audio
{
    class Decoder;
    class DecoderWorker : public sys::Worker
    {
      public:
        using EndOfFileCallback = std::function<void()>;
        enum class Command
        {
            EnablePlayback,
            DisablePlayback,
        };

        DecoderWorker(Stream *audioStreamOut, Decoder *decoder, EndOfFileCallback endOfFileCallback);
        ~DecoderWorker() override;

        virtual auto init(std::list<sys::WorkerQueueInfo> queues = std::list<sys::WorkerQueueInfo>()) -> bool override;

        auto enablePlayback() -> bool;
        auto disablePlayback() -> bool;

      private:
        static constexpr std::size_t stackDepth = 2 * 1024;

        virtual auto handleMessage(uint32_t queueID) -> bool override;
        void pushAudioData();
        bool stateChangeWait();

        using BufferInternalType = int16_t;

        static constexpr auto workerName            = "DecoderWorker";
        static constexpr auto workerPriority        = static_cast<UBaseType_t>(sys::ServicePriority::Idle);
        static constexpr auto listenerQueueName     = "DecoderWorkerQueue";
        static constexpr auto listenerQueueCapacity = 1024;

        Stream *audioStreamOut = nullptr;
        Decoder *decoder       = nullptr;
        EndOfFileCallback endOfFileCallback;
        std::unique_ptr<StreamQueuedEventsListener> queueListener;
        bool playbackEnabled = false;
        cpp_freertos::BinarySemaphore stateSemaphore;

        const int bufferSize;
        std::unique_ptr<BufferInternalType[]> decoderBuffer;
    };
} // namespace audio