audio_common.h 1.98 KB
Newer Older
Adrien Béraud's avatar
Adrien Béraud committed
1 2
/*
 * Copyright 2015 The Android Open Source Project
Sébastien Blin's avatar
Sébastien Blin committed
3
 * Copyright 2015-2019 Savoir-faire Linux Inc.
Adrien Béraud's avatar
Adrien Béraud committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#pragma once
19
#include <SLES/OpenSLES.h>
Adrien Béraud's avatar
Adrien Béraud committed
20 21 22 23
#include <SLES/OpenSLES_Android.h>

#include "buf_manager.h"

Adrien Béraud's avatar
Adrien Béraud committed
24
namespace jami {
Adrien Béraud's avatar
Adrien Béraud committed
25 26 27 28 29
namespace opensl {

/*
 * Sample Buffer Controls...
 */
30
#define RECORD_DEVICE_KICKSTART_BUF_COUNT 4
Sébastien Blin's avatar
Sébastien Blin committed
31 32 33
#define PLAY_KICKSTART_BUFFER_COUNT       8
#define DEVICE_SHADOW_BUFFER_QUEUE_LEN    4
#define BUF_COUNT                         16
Adrien Béraud's avatar
Adrien Béraud committed
34

Sébastien Blin's avatar
Sébastien Blin committed
35 36
inline SLDataFormat_PCM
convertToSLSampleFormat(const jami::AudioFormat& infos)
Adrien Béraud's avatar
Adrien Béraud committed
37 38
{
    return SLDataFormat_PCM {
39 40
        .formatType = SL_DATAFORMAT_PCM,
        .numChannels = infos.nb_channels <= 1 ? 1u : 2u,
Sébastien Blin's avatar
Sébastien Blin committed
41 42 43
        .samplesPerSec = infos.sample_rate * 1000,
        .bitsPerSample = SL_PCMSAMPLEFORMAT_FIXED_16,
        .containerSize = SL_PCMSAMPLEFORMAT_FIXED_16,
44
        .channelMask = infos.nb_channels <= 1 ? SL_SPEAKER_FRONT_CENTER
Sébastien Blin's avatar
Sébastien Blin committed
45 46
                                              : SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT,
        .endianness = SL_BYTEORDER_LITTLEENDIAN,
Adrien Béraud's avatar
Adrien Béraud committed
47 48 49
    };
}

Sébastien Blin's avatar
Sébastien Blin committed
50 51 52 53
#define SLASSERT(x) \
    { \
        if (SL_RESULT_SUCCESS != (x)) \
            throw std::runtime_error("OpenSLES error " + std::to_string(x)); \
Adrien Béraud's avatar
Adrien Béraud committed
54 55 56 57 58
    }

/*
 * Interface for player and recorder to communicate with engine
 */
Sébastien Blin's avatar
Sébastien Blin committed
59 60
#define ENGINE_SERVICE_MSG_KICKSTART_PLAYER   1
#define ENGINE_SERVICE_MSG_RETRIEVE_DUMP_BUFS 2
Adrien Béraud's avatar
Adrien Béraud committed
61

Adrien Béraud's avatar
Adrien Béraud committed
62
using EngineCallback = std::function<void()>;
Adrien Béraud's avatar
Adrien Béraud committed
63

Sébastien Blin's avatar
Sébastien Blin committed
64 65
} // namespace opensl
} // namespace jami