aboutsummaryrefslogtreecommitdiffstats
path: root/Alc/backends/base.h
blob: 7a9232a6148558e200a8e93e96c2d4bc4ea709b5 (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
#ifndef ALC_BACKENDS_BASE_H
#define ALC_BACKENDS_BASE_H

#include <memory>
#include <chrono>
#include <string>
#include <mutex>

#include "alMain.h"


struct ClockLatency {
    std::chrono::nanoseconds ClockTime;
    std::chrono::nanoseconds Latency;
};

/* Helper to get the current clock time from the device's ClockBase, and
 * SamplesDone converted from the sample rate.
 */
inline std::chrono::nanoseconds GetDeviceClockTime(ALCdevice *device)
{
    using std::chrono::seconds;
    using std::chrono::nanoseconds;

    auto ns = nanoseconds{seconds{device->SamplesDone}} / device->Frequency;
    return device->ClockBase + ns;
}

ClockLatency GetClockLatency(ALCdevice *device);

struct BackendBase {
    virtual ALCenum open(const ALCchar *name) = 0;

    virtual ALCboolean reset();
    virtual ALCboolean start() = 0;
    virtual void stop() = 0;

    virtual ALCenum captureSamples(void *buffer, ALCuint samples);
    virtual ALCuint availableSamples();

    virtual ClockLatency getClockLatency();

    virtual void lock() { mMutex.lock(); }
    virtual void unlock() { mMutex.unlock(); }

    ALCdevice *mDevice;

    std::recursive_mutex mMutex;

    BackendBase(ALCdevice *device) noexcept;
    virtual ~BackendBase();
};
using BackendPtr = std::unique_ptr<BackendBase>;
using BackendUniqueLock = std::unique_lock<BackendBase>;
using BackendLockGuard = std::lock_guard<BackendBase>;

enum class BackendType {
    Playback,
    Capture
};

enum class DevProbe {
    Playback,
    Capture
};


struct BackendFactory {
    virtual bool init() = 0;

    virtual bool querySupport(BackendType type) = 0;

    virtual void probe(DevProbe type, std::string *outnames) = 0;

    virtual BackendPtr createBackend(ALCdevice *device, BackendType type) = 0;
};

#endif /* ALC_BACKENDS_BASE_H */