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