AudioStreamBuilder.cpp revision e2fbb59e729f6c3cade3b531f6f6411417ccbf40
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
50static aaudio_result_t builder_createStream(aaudio_direction_t direction,
51                                         aaudio_sharing_mode_t sharingMode,
52                                         bool tryMMap,
53                                         AudioStream **audioStreamPtr) {
54    *audioStreamPtr = nullptr;
55    aaudio_result_t result = AAUDIO_OK;
56    switch (direction) {
57
58        case AAUDIO_DIRECTION_INPUT:
59            if (sharingMode == AAUDIO_SHARING_MODE_SHARED) {
60                *audioStreamPtr = new AudioStreamRecord();
61            } else {
62                ALOGE("AudioStreamBuilder(): bad sharing mode = %d for input", sharingMode);
63                result = AAUDIO_ERROR_ILLEGAL_ARGUMENT;
64            }
65            break;
66
67        case AAUDIO_DIRECTION_OUTPUT:
68            if (tryMMap) {
69                // TODO use a singleton for the AAudioBinderClient
70                AAudioBinderClient *aaudioClient = new AAudioBinderClient();
71                *audioStreamPtr = new AudioStreamInternal(*aaudioClient, false);
72            } else {
73                *audioStreamPtr = new AudioStreamTrack();
74            }
75            break;
76
77        default:
78            ALOGE("AudioStreamBuilder(): bad direction = %d", direction);
79            result = AAUDIO_ERROR_ILLEGAL_ARGUMENT;
80    }
81    return result;
82}
83
84aaudio_result_t AudioStreamBuilder::build(AudioStream** streamPtr) {
85    aaudio_sharing_mode_t sharingMode = getSharingMode();
86    if ((sharingMode == AAUDIO_SHARING_MODE_EXCLUSIVE) && (MMAP_EXCLUSIVE_ENABLED == 0)) {
87        ALOGE("AudioStreamBuilder(): EXCLUSIVE sharing mode not supported");
88        return AAUDIO_ERROR_UNAVAILABLE;
89    }
90
91    AudioStream *audioStream = nullptr;
92    *streamPtr = nullptr;
93
94    bool tryMMap = (sharingMode == AAUDIO_SHARING_MODE_SHARED) && MMAP_SHARED_ENABLED;
95    aaudio_result_t result = builder_createStream(getDirection(), sharingMode,
96                                                  tryMMap, &audioStream);
97    if (result == AAUDIO_OK) {
98        // Open the stream using the parameters from the builder.
99        result = audioStream->open(*this);
100        if (result == AAUDIO_OK) {
101            *streamPtr = audioStream;
102        } else {
103            bool isMMap = audioStream->isMMap();
104            delete audioStream;
105            audioStream = nullptr;
106
107            if (isMMap) {
108                ALOGD("AudioStreamBuilder.build() MMAP stream did not open so try Legacy path");
109                // If MMAP stream failed to open then TRY using a legacy stream.
110                result = builder_createStream(getDirection(), sharingMode,
111                                              false, &audioStream);
112                if (result == AAUDIO_OK) {
113                    result = audioStream->open(*this);
114                    if (result == AAUDIO_OK) {
115                        *streamPtr = audioStream;
116                    } else {
117                        delete audioStream;
118                    }
119                }
120            }
121        }
122    }
123
124    ALOGD("AudioStreamBuilder(): returned %d", result);
125    return result;
126}
127