summaryrefslogtreecommitdiffstats
path: root/Alc/null.c
blob: ad3d3f6aacf8e46a6ef5b4c572579ff363e0cc87 (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
/**
 * OpenAL cross platform audio library
 * Copyright (C) 2010 by Chris Robinson
 * This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Library General Public
 *  License as published by the Free Software Foundation; either
 *  version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 *  License along with this library; if not, write to the
 *  Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 *  Boston, MA  02111-1307, USA.
 * Or go to http://www.gnu.org/copyleft/lgpl.html
 */

#include "config.h"

#include <stdlib.h>
#include "alMain.h"
#include "AL/al.h"
#include "AL/alc.h"


typedef struct {
    ALvoid *buffer;
    ALuint size;

    ALuint startTime;
    ALuint64 baseTime;

    volatile int killNow;
    ALvoid *thread;
} null_data;


static const ALCchar nullDevice[] = "No Output";

static ALuint NullProc(ALvoid *ptr)
{
    ALCdevice *Device = (ALCdevice*)ptr;
    null_data *data = (null_data*)Device->ExtraData;
    ALuint now, start;
    ALuint64 avail, done;
    const ALuint restTime = (ALuint64)Device->UpdateSize * 1000 /
                            Device->Frequency / 2;

    done = 0;
    start = data->startTime;
    while(!data->killNow && Device->Connected)
    {
        now = timeGetTime();

        avail = (ALuint64)(now-start) * Device->Frequency / 1000;
        if(avail < done)
        {
            AL_PRINT("Timer wrapped\n");
            aluHandleDisconnect(Device);
            break;
        }
        if(avail-done < Device->UpdateSize)
        {
            Sleep(restTime);
            continue;
        }

        while(avail-done >= Device->UpdateSize)
        {
            aluMixData(Device, data->buffer, Device->UpdateSize);
            done += Device->UpdateSize;
        }
    }

    return 0;
}

static ALCboolean null_open_playback(ALCdevice *device, const ALCchar *deviceName)
{
    null_data *data;

    if(!deviceName)
        deviceName = nullDevice;
    else if(strcmp(deviceName, nullDevice) != 0)
        return ALC_FALSE;

    data = (null_data*)calloc(1, sizeof(*data));

    device->szDeviceName = strdup(deviceName);
    device->ExtraData = data;
    return ALC_TRUE;
}

static void null_close_playback(ALCdevice *device)
{
    null_data *data = (null_data*)device->ExtraData;

    free(data);
    device->ExtraData = NULL;
}

static ALCboolean null_reset_playback(ALCdevice *device)
{
    null_data *data = (null_data*)device->ExtraData;

    data->size = device->UpdateSize * aluFrameSizeFromFormat(device->Format);
    data->buffer = malloc(data->size);
    if(!data->buffer)
    {
        AL_PRINT("buffer malloc failed\n");
        return ALC_FALSE;
    }
    SetDefaultWFXChannelOrder(device);

    device->TimeRes = 1000000;

    data->startTime = timeGetTime();
    data->thread = StartThread(NullProc, device);
    if(data->thread == NULL)
    {
        free(data->buffer);
        data->buffer = NULL;
        return ALC_FALSE;
    }

    return ALC_TRUE;
}

static void null_stop_playback(ALCdevice *device)
{
    null_data *data = (null_data*)device->ExtraData;
    ALuint ext;

    if(!data->thread)
        return;

    data->killNow = 1;
    StopThread(data->thread);
    data->thread = NULL;

    data->killNow = 0;

    ext = timeGetTime() - data->startTime;
    data->baseTime += (ALuint64)ext * 1000000;

    free(data->buffer);
    data->buffer = NULL;
}


static ALCboolean null_open_capture(ALCdevice *device, const ALCchar *deviceName)
{
    (void)device;
    (void)deviceName;
    return ALC_FALSE;
}

static ALuint64 null_get_time(ALCdevice *Device)
{
    null_data *data = (null_data*)Device->ExtraData;
    ALuint ext = 0;
    if(data->thread)
        ext = timeGetTime() - data->startTime;
    return data->baseTime + ((ALuint64)ext * 1000000);
}


BackendFuncs null_funcs = {
    null_open_playback,
    null_close_playback,
    null_reset_playback,
    null_stop_playback,
    null_open_capture,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL,
    null_get_time
};

void alc_null_init(BackendFuncs *func_list)
{
    *func_list = null_funcs;
}

void alc_null_deinit(void)
{
}

void alc_null_probe(int type)
{
    if(type == DEVICE_PROBE)
        AppendDeviceList(nullDevice);
    else if(type == ALL_DEVICE_PROBE)
        AppendAllDeviceList(nullDevice);
}