SDL.c revision 262059f71a68edc5e510427c63f5f1623d3672a8
1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/** \file SDL.c SDL platform implementation */
18
19#include "sles_allinclusive.h"
20
21
22/** \brief Called by SDL to fill the next audio output buffer */
23
24static void SDLCALL SDL_callback(void *context, Uint8 *stream, int len)
25{
26    assert(len > 0);
27    IEngine *thisEngine = (IEngine *) context;
28    // A peek lock would be risky if output mixes are dynamic, so we use SDL_PauseAudio to
29    // temporarily disable callbacks during any change to the current output mix, and use a
30    // shared lock here
31    interface_lock_shared(thisEngine);
32    COutputMix *outputMix = thisEngine->mOutputMix;
33    interface_unlock_shared(thisEngine);
34    if (NULL != outputMix) {
35        SLOutputMixExtItf OutputMixExt = &outputMix->mOutputMixExt.mItf;
36        IOutputMixExt_FillBuffer(OutputMixExt, stream, (SLuint32) len);
37    } else {
38        memset(stream, 0, (size_t) len);
39    }
40}
41
42
43/** \brief Called during slCreateEngine to initialize SDL */
44
45void SDL_open(IEngine *thisEngine)
46{
47    SDL_AudioSpec fmt;
48    fmt.freq = 44100;
49    fmt.format = AUDIO_S16;
50    fmt.channels = STEREO_CHANNELS;
51#ifdef _WIN32 // FIXME Either a bug or a serious misunderstanding
52    fmt.samples = SndFile_BUFSIZE;
53#else
54    fmt.samples = SndFile_BUFSIZE / sizeof(short);
55#endif
56    fmt.callback = SDL_callback;
57    fmt.userdata = (void *) thisEngine;
58
59    if (SDL_OpenAudio(&fmt, NULL) < 0) {
60        SL_LOGE("Unable to open audio: %s", SDL_GetError());
61        exit(EXIT_FAILURE);
62    }
63}
64
65
66/** \brief Called during Object::Destroy for engine to shutdown SDL */
67
68void SDL_close(void)
69{
70    SDL_CloseAudio();
71}
72