aboutsummaryrefslogtreecommitdiffstats
path: root/alc/backends/oboe.cpp
blob: c999df6fa6d76a55b571b10b24fbe8f6f4538ef7 (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
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209

#include "config.h"

#include "oboe.h"

#include <cassert>
#include <cstring>

#include "alu.h"
#include "logging.h"

#include "oboe/Oboe.h"


namespace {

constexpr char device_name[] = "Oboe Default";


struct OboePlayback final : public BackendBase, public oboe::AudioStreamCallback {
    OboePlayback(ALCdevice *device) : BackendBase{device} { }

    oboe::ManagedStream mStream;

    oboe::DataCallbackResult onAudioReady(oboe::AudioStream *oboeStream, void *audioData,
        int32_t numFrames) override;

    void open(const ALCchar *name) override;
    bool reset() override;
    bool start() override;
    void stop() override;
};


oboe::DataCallbackResult OboePlayback::onAudioReady(oboe::AudioStream *oboeStream, void *audioData,
    int32_t numFrames)
{
    assert(numFrames > 0);
    aluMixData(mDevice, audioData, static_cast<uint32_t>(numFrames),
        static_cast<uint32_t>(oboeStream->getChannelCount()));
    return oboe::DataCallbackResult::Continue;
}


void OboePlayback::open(const ALCchar *name)
{
    if(!name)
        name = device_name;
    else if(std::strcmp(name, device_name) != 0)
        throw al::backend_exception{ALC_INVALID_VALUE, "Device name \"%s\" not found", name};

    oboe::AudioStreamBuilder builder;
    builder.setDirection(oboe::Direction::Output);
    builder.setPerformanceMode(oboe::PerformanceMode::LowLatency);

    oboe::Result result{builder.openManagedStream(mStream)};
    if(result != oboe::Result::OK)
        throw al::backend_exception{ALC_INVALID_VALUE, "Failed to create stream. Error: %s",
            oboe::convertToText(result)};
}

bool OboePlayback::reset()
{
    mStream = nullptr;

    oboe::AudioStreamBuilder builder;
    builder.setDirection(oboe::Direction::Output);
    builder.setPerformanceMode(oboe::PerformanceMode::LowLatency);
    builder.setCallback(this);

    if(mDevice->Flags.get<FrequencyRequest>())
        builder.setSampleRate(static_cast<int32_t>(mDevice->Frequency));
    if(mDevice->Flags.get<ChannelsRequest>())
        builder.setChannelCount((mDevice->FmtChans==DevFmtMono) ? oboe::ChannelCount::Mono
            : oboe::ChannelCount::Stereo);
    if(mDevice->Flags.get<SampleTypeRequest>())
    {
        oboe::AudioFormat format{oboe::AudioFormat::Unspecified};
        switch(mDevice->FmtType)
        {
        case DevFmtByte:
        case DevFmtUByte:
        case DevFmtShort:
        case DevFmtUShort:
            format = oboe::AudioFormat::I16;
            break;
        case DevFmtInt:
        case DevFmtUInt:
        case DevFmtFloat:
            format = oboe::AudioFormat::Float;
            break;
        }
        builder.setFormat(format);
    }

    oboe::Result result{builder.openManagedStream(mStream)};
    if(result == oboe::Result::ErrorInvalidFormat)
    {
        builder.setSampleRate(oboe::kUnspecified);
        builder.setChannelCount(oboe::ChannelCount::Stereo);
        builder.setFormat(oboe::AudioFormat::Unspecified);
        result = builder.openManagedStream(mStream);
    }
    if(result != oboe::Result::OK)
        throw al::backend_exception{ALC_INVALID_DEVICE, "Failed to create stream: %s",
            oboe::convertToText(result)};

    switch(mStream->getChannelCount())
    {
    case oboe::ChannelCount::Mono:
        mDevice->FmtChans = DevFmtMono;
        break;
    case oboe::ChannelCount::Stereo:
        mDevice->FmtChans = DevFmtStereo;
        break;
    /* Other potential configurations. Assume WFX channel order. */
    case 4:
        mDevice->FmtChans = DevFmtQuad;
        break;
    case 6:
        mDevice->FmtChans = DevFmtX51Rear;
        break;
    case 7:
        mDevice->FmtChans = DevFmtX61;
        break;
    case 8:
        mDevice->FmtChans = DevFmtX71;
        break;
    default:
        throw al::backend_exception{ALC_INVALID_DEVICE, "Got unhandled channel count: %d",
            mStream->getChannelCount()};
    }
    SetDefaultWFXChannelOrder(mDevice);

    switch(mStream->getFormat())
    {
    case oboe::AudioFormat::I16:
        mDevice->FmtType = DevFmtShort;
        break;
    case oboe::AudioFormat::Float:
        mDevice->FmtType = DevFmtFloat;
        break;
    case oboe::AudioFormat::Unspecified:
    case oboe::AudioFormat::Invalid:
        throw al::backend_exception{ALC_INVALID_DEVICE, "Got unhandled sample type: %d",
            mStream->getFormat()};
    }
    mDevice->Frequency = static_cast<uint32_t>(mStream->getSampleRate());

    /* Ensure the period size is no less than 10ms. It's possible for FramesPerBurst to be 0
     * indicating variable updates, but OpenAL should have a reasonable minimum update size set.
     */
    mDevice->UpdateSize = maxu(mDevice->Frequency / 100,
        static_cast<uint32_t>(mStream->getFramesPerBurst()));
    mDevice->BufferSize = maxu(mDevice->UpdateSize * 2,
        static_cast<uint32_t>(mStream->getBufferSizeInFrames()));

    return true;
}

bool OboePlayback::start()
{
    oboe::Result result{mStream->start()};
    if(result != oboe::Result::OK)
        throw al::backend_exception{ALC_INVALID_DEVICE, "Failed to start stream: %s",
            oboe::convertToText(result)};
    return true;
}

void OboePlayback::stop()
{
    oboe::Result result{mStream->stop()};
    if(result != oboe::Result::OK)
        throw al::backend_exception{ALC_INVALID_DEVICE, "Failed to stop stream: %s",
            oboe::convertToText(result)};
}

} // namespace

bool OboeBackendFactory::init() { return true; }

bool OboeBackendFactory::querySupport(BackendType type)
{ return type == BackendType::Playback; }

std::string OboeBackendFactory::probe(BackendType type)
{
    switch(type)
    {
    case BackendType::Playback:
        /* Includes null char. */
        return std::string{device_name, sizeof(device_name)};
    case BackendType::Capture:
        break;
    }
    return std::string{};
}

BackendPtr OboeBackendFactory::createBackend(ALCdevice *device, BackendType type)
{
    if(type == BackendType::Playback)
        return BackendPtr{new OboePlayback{device}};
    return nullptr;
}

BackendFactory &OboeBackendFactory::getFactory()
{
    static OboeBackendFactory factory{};
    return factory;
}