summaryrefslogtreecommitdiffstats
path: root/src/audio_core/adsp/apps/opus/opus_decoder.h
blob: fcb89bb40e986c80ea4d389706a644982ff8b05f (plain) (blame)
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
// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#pragma once

#include <memory>
#include <thread>

#include "audio_core/adsp/apps/opus/shared_memory.h"
#include "audio_core/adsp/mailbox.h"
#include "common/common_types.h"

namespace Core {
class System;
} // namespace Core

namespace AudioCore::ADSP::OpusDecoder {

enum Message : u32 {
    Invalid = 0,
    Start = 1,
    Shutdown = 2,
    StartOK = 11,
    ShutdownOK = 12,
    GetWorkBufferSize = 21,
    InitializeDecodeObject = 22,
    ShutdownDecodeObject = 23,
    DecodeInterleaved = 24,
    MapMemory = 25,
    UnmapMemory = 26,
    GetWorkBufferSizeForMultiStream = 27,
    InitializeMultiStreamDecodeObject = 28,
    ShutdownMultiStreamDecodeObject = 29,
    DecodeInterleavedForMultiStream = 30,

    GetWorkBufferSizeOK = 41,
    InitializeDecodeObjectOK = 42,
    ShutdownDecodeObjectOK = 43,
    DecodeInterleavedOK = 44,
    MapMemoryOK = 45,
    UnmapMemoryOK = 46,
    GetWorkBufferSizeForMultiStreamOK = 47,
    InitializeMultiStreamDecodeObjectOK = 48,
    ShutdownMultiStreamDecodeObjectOK = 49,
    DecodeInterleavedForMultiStreamOK = 50,
};

/**
 * The AudioRenderer application running on the ADSP.
 */
class OpusDecoder {
public:
    explicit OpusDecoder(Core::System& system);
    ~OpusDecoder();

    bool IsRunning() const noexcept {
        return running;
    }

    void Send(Direction dir, u32 message);
    u32 Receive(Direction dir, std::stop_token stop_token = {});

    void SetSharedMemory(SharedMemory& shared_memory_) {
        shared_memory = &shared_memory_;
    }

private:
    /**
     * Initializing thread, launched at audio_core boot to avoid blocking the main emu boot thread.
     */
    void Init(std::stop_token stop_token);
    /**
     * Main OpusDecoder thread, responsible for processing the incoming Opus packets.
     */
    void Main(std::stop_token stop_token);

    /// Core system
    Core::System& system;
    /// Mailbox to communicate messages with the host, drives the main thread
    Mailbox mailbox;
    /// Init thread
    std::jthread init_thread{};
    /// Main thread
    std::jthread main_thread{};
    /// The current state
    bool running{};
    /// Structure shared with the host, input data set by the host before sending a mailbox message,
    /// and the responses are written back by the OpusDecoder.
    SharedMemory* shared_memory{};
};

} // namespace AudioCore::ADSP::OpusDecoder