AudioStreamBuilder.cpp revision 9b3f8ef290bd5ad392f5eba8a0f0a8ddd331b54f
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
33using namespace aaudio;
34
35/*
36 * AudioStreamBuilder
37 */
38AudioStreamBuilder::AudioStreamBuilder() {
39}
40
41AudioStreamBuilder::~AudioStreamBuilder() {
42}
43
44static aaudio_result_t builder_createStream(aaudio_direction_t direction,
45                                         aaudio_sharing_mode_t sharingMode,
46                                         bool tryMMap,
47                                         AudioStream **audioStreamPtr) {
48    *audioStreamPtr = nullptr;
49    aaudio_result_t result = AAUDIO_OK;
50
51    switch (direction) {
52
53        case AAUDIO_DIRECTION_INPUT:
54            if (sharingMode == AAUDIO_SHARING_MODE_SHARED) {
55                *audioStreamPtr = new AudioStreamRecord();
56            } else {
57                ALOGE("AudioStreamBuilder(): bad sharing mode = %d for input", sharingMode);
58                result = AAUDIO_ERROR_ILLEGAL_ARGUMENT;
59            }
60            break;
61
62        case AAUDIO_DIRECTION_OUTPUT:
63            if (tryMMap) {
64                *audioStreamPtr = new AudioStreamInternal(AAudioBinderClient::getInstance(), false);
65            } else {
66                *audioStreamPtr = new AudioStreamTrack();
67            }
68            break;
69
70        default:
71            ALOGE("AudioStreamBuilder(): bad direction = %d", direction);
72            result = AAUDIO_ERROR_ILLEGAL_ARGUMENT;
73    }
74    return result;
75}
76
77// Try to open using MMAP path if that is enabled.
78// Fall back to Legacy path is MMAP not available.
79aaudio_result_t AudioStreamBuilder::build(AudioStream** streamPtr) {
80    AudioStream *audioStream = nullptr;
81    *streamPtr = nullptr;
82
83    int32_t mmapEnabled = AAudioProperty_getMMapEnabled();
84    int32_t mmapExclusiveEnabled = AAudioProperty_getMMapExclusiveEnabled();
85    ALOGD("AudioStreamBuilder(): mmapEnabled = %d, mmapExclusiveEnabled = %d",
86          mmapEnabled, mmapExclusiveEnabled);
87
88    aaudio_sharing_mode_t sharingMode = getSharingMode();
89    if ((sharingMode == AAUDIO_SHARING_MODE_EXCLUSIVE)
90        && (mmapExclusiveEnabled == AAUDIO_USE_NEVER)) {
91        ALOGW("AudioStreamBuilder(): EXCLUSIVE sharing mode not supported. Use SHARED.");
92        sharingMode = AAUDIO_SHARING_MODE_SHARED;
93        setSharingMode(sharingMode);
94    }
95
96    bool allowMMap = mmapEnabled != AAUDIO_USE_NEVER;
97    bool allowLegacy = mmapEnabled != AAUDIO_USE_ALWAYS;
98
99    aaudio_result_t result = builder_createStream(getDirection(), sharingMode,
100                                                  allowMMap, &audioStream);
101    if (result == AAUDIO_OK) {
102        // Open the stream using the parameters from the builder.
103        result = audioStream->open(*this);
104        if (result == AAUDIO_OK) {
105            *streamPtr = audioStream;
106        } else {
107            bool isMMap = audioStream->isMMap();
108            delete audioStream;
109            audioStream = nullptr;
110
111            if (isMMap && allowLegacy) {
112                ALOGD("AudioStreamBuilder.build() MMAP stream did not open so try Legacy path");
113                // If MMAP stream failed to open then TRY using a legacy stream.
114                result = builder_createStream(getDirection(), sharingMode,
115                                              false, &audioStream);
116                if (result == AAUDIO_OK) {
117                    result = audioStream->open(*this);
118                    if (result == AAUDIO_OK) {
119                        *streamPtr = audioStream;
120                    } else {
121                        delete audioStream;
122                    }
123                }
124            }
125        }
126    }
127
128    ALOGD("AudioStreamBuilder(): returned %d", result);
129    return result;
130}
131