AudioStreamBuilder.cpp revision 5bab576c2f03c02e8ae6020127ba962d96172e8a
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 stream to an ALSA MMAP buffer.
34#define MMAP_SHARED_ENABLED      0
35// Enable AAUDIO_SHARING_MODE_EXCLUSIVE that uses an ALSA MMAP buffer.
36#define MMAP_EXCLUSIVE_ENABLED   0
37
38using namespace aaudio;
39
40/*
41 * AudioStreamBuilder
42 */
43AudioStreamBuilder::AudioStreamBuilder() {
44}
45
46AudioStreamBuilder::~AudioStreamBuilder() {
47}
48
49aaudio_result_t AudioStreamBuilder::build(AudioStream** streamPtr) {
50    AudioStream* audioStream = nullptr;
51    AAudioBinderClient *aaudioClient = nullptr;
52    const aaudio_sharing_mode_t sharingMode = getSharingMode();
53    ALOGD("AudioStreamBuilder.build() sharingMode = %d", sharingMode);
54    switch (getDirection()) {
55
56    case AAUDIO_DIRECTION_INPUT:
57        switch (sharingMode) {
58            case AAUDIO_SHARING_MODE_SHARED:
59                audioStream = new(std::nothrow) AudioStreamRecord();
60                break;
61            default:
62                ALOGE("AudioStreamBuilder(): bad sharing mode = %d", sharingMode);
63                return AAUDIO_ERROR_ILLEGAL_ARGUMENT;
64                break;
65        }
66        break;
67
68    case AAUDIO_DIRECTION_OUTPUT:
69        switch (sharingMode) {
70            case AAUDIO_SHARING_MODE_SHARED:
71#if MMAP_SHARED_ENABLED
72                aaudioClient = new AAudioBinderClient();
73                audioStream = new(std::nothrow) AudioStreamInternal(*aaudioClient, false);
74#else
75                audioStream = new(std::nothrow) AudioStreamTrack();
76#endif
77                break;
78#if MMAP_EXCLUSIVE_ENABLED
79            case AAUDIO_SHARING_MODE_EXCLUSIVE:
80                aaudioClient = new AAudioBinderClient();
81                audioStream = new(std::nothrow) AudioStreamInternal(*aaudioClient, false);
82                break;
83#endif
84            default:
85                ALOGE("AudioStreamBuilder(): bad sharing mode = %d", sharingMode);
86                return AAUDIO_ERROR_ILLEGAL_ARGUMENT;
87                break;
88        }
89        break;
90
91    default:
92        ALOGE("AudioStreamBuilder(): bad direction = %d", getDirection());
93        return AAUDIO_ERROR_ILLEGAL_ARGUMENT;
94        break;
95    }
96    if (audioStream == nullptr) {
97        delete aaudioClient;
98        return AAUDIO_ERROR_NO_MEMORY;
99    }
100    ALOGD("AudioStreamBuilder(): created audioStream = %p", audioStream);
101
102    // TODO maybe move this out of build and pass the builder to the constructors
103    // Open the stream using the parameters from the builder.
104    const aaudio_result_t result = audioStream->open(*this);
105    if (result != AAUDIO_OK) {
106        delete audioStream;
107    } else {
108        *streamPtr = audioStream;
109    }
110    return result;
111}
112