summaryrefslogtreecommitdiffstats
path: root/src/audio_core/audio_event.h
blob: 012d2ed70e2c0d25692d9bdfd9606b9c94d77cf3 (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 2022 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#pragma once

#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <mutex>

namespace AudioCore {
/**
 * Responsible for the input/output events, set by the stream backend when buffers are consumed, and
 * waited on by the audio manager. These callbacks signal the game's events to keep the audio buffer
 * recycling going.
 * In a real Switch this is not a separate class, and exists entirely within the audio manager.
 * On the Switch it's implemented more simply through a MultiWaitEventHolder, where it can
 * wait on multiple events at once, and the events are not needed by the backend.
 */
class Event {
public:
    enum class Type {
        AudioInManager,
        AudioOutManager,
        FinalOutputRecorderManager,
        Max,
    };

    /**
     * Convert a manager type to an index.
     *
     * @param type - The manager type to convert
     * @return The index of the type.
     */
    size_t GetManagerIndex(Type type) const;

    /**
     * Set an audio event to true or false.
     *
     * @param type      - The manager type to signal.
     * @param signalled - Its signal state.
     */
    void SetAudioEvent(Type type, bool signalled);

    /**
     * Check if the given manager type is signalled.
     *
     * @param type - The manager type to check.
     * @return True if the event is signalled, otherwise false.
     */
    bool CheckAudioEventSet(Type type) const;

    /**
     * Get the lock for audio events.
     *
     * @return Reference to the lock.
     */
    std::mutex& GetAudioEventLock();

    /**
     * Get the manager event, this signals the audio manager to release buffers and signal the game
     * for more.
     *
     * @return Reference to the condition variable.
     */
    std::condition_variable_any& GetAudioEvent();

    /**
     * Wait on the manager_event.
     *
     * @param l       - Lock held by the wait.
     * @param timeout - Timeout for the wait. This is 2 seconds by default.
     * @return True if the wait timed out, otherwise false if signalled.
     */
    bool Wait(std::unique_lock<std::mutex>& l, std::chrono::seconds timeout);

    /**
     * Reset all manager events.
     */
    void ClearEvents();

private:
    /// Lock, used by the audio manager
    std::mutex event_lock;
    /// Array of events, one per system type (see Type), last event is used to terminate
    std::array<std::atomic<bool>, 4> events_signalled;
    /// Event to signal the audio manager
    std::condition_variable_any manager_event;
};

} // namespace AudioCore