AudioStreamBuilder.cpp revision 4c5129b410884ec0400cbe65fce56d0ade12d11b
1/*
2 * Copyright 2015 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#define LOG_TAG "AAudio"
18//#define LOG_NDEBUG 0
19#include <utils/Log.h>
20
21#include <new>
22#include <stdint.h>
23
24#include <aaudio/AAudio.h>
25
26#include "binding/AAudioBinderClient.h"
27#include "client/AudioStreamInternal.h"
28#include "core/AudioStream.h"
29#include "core/AudioStreamBuilder.h"
30#include "legacy/AudioStreamRecord.h"
31#include "legacy/AudioStreamTrack.h"
32
33// Enable a mixer in AAudio service that will mix streams to an ALSA MMAP buffer.
34#define MMAP_SHARED_ENABLED      0
35
36// Enable AAUDIO_SHARING_MODE_EXCLUSIVE that uses an ALSA MMAP buffer directly.
37#define MMAP_EXCLUSIVE_ENABLED   0
38
39using namespace aaudio;
40
41/*
42 * AudioStreamBuilder
43 */
44AudioStreamBuilder::AudioStreamBuilder() {
45}
46
47AudioStreamBuilder::~AudioStreamBuilder() {
48}
49
50aaudio_result_t AudioStreamBuilder::build(AudioStream** streamPtr) {
51    AudioStream* audioStream = nullptr;
52    AAudioBinderClient *aaudioClient = nullptr;
53    const aaudio_sharing_mode_t sharingMode = getSharingMode();
54
55    switch (getDirection()) {
56
57    case AAUDIO_DIRECTION_INPUT:
58        switch (sharingMode) {
59            case AAUDIO_SHARING_MODE_SHARED:
60                audioStream = new(std::nothrow) AudioStreamRecord();
61                break;
62            default:
63                ALOGE("AudioStreamBuilder(): bad sharing mode = %d", sharingMode);
64                return AAUDIO_ERROR_ILLEGAL_ARGUMENT;
65                break;
66        }
67        break;
68
69    case AAUDIO_DIRECTION_OUTPUT:
70        switch (sharingMode) {
71            case AAUDIO_SHARING_MODE_SHARED:
72#if MMAP_SHARED_ENABLED
73                aaudioClient = new AAudioBinderClient();
74                audioStream = new(std::nothrow) AudioStreamInternal(*aaudioClient, false);
75#else
76                audioStream = new(std::nothrow) AudioStreamTrack();
77#endif
78                break;
79#if MMAP_EXCLUSIVE_ENABLED
80            case AAUDIO_SHARING_MODE_EXCLUSIVE:
81                aaudioClient = new AAudioBinderClient();
82                audioStream = new(std::nothrow) AudioStreamInternal(*aaudioClient, false);
83                break;
84#endif
85            default:
86                ALOGE("AudioStreamBuilder(): bad sharing mode = %d", sharingMode);
87                return AAUDIO_ERROR_ILLEGAL_ARGUMENT;
88                break;
89        }
90        break;
91
92    default:
93        ALOGE("AudioStreamBuilder(): bad direction = %d", getDirection());
94        return AAUDIO_ERROR_ILLEGAL_ARGUMENT;
95        break;
96    }
97    if (audioStream == nullptr) {
98        delete aaudioClient;
99        return AAUDIO_ERROR_NO_MEMORY;
100    }
101    ALOGD("AudioStreamBuilder(): created audioStream = %p", audioStream);
102
103    // TODO maybe move this out of build and pass the builder to the constructors
104    // Open the stream using the parameters from the builder.
105    const aaudio_result_t result = audioStream->open(*this);
106    if (result != AAUDIO_OK) {
107        delete audioStream;
108    } else {
109        *streamPtr = audioStream;
110    }
111    ALOGD("AudioStreamBuilder(): return %d", result);
112    return result;
113}
114