• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "AudioStreamBuilder"
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 #include <aaudio/AAudioTesting.h>
26 
27 #include "binding/AAudioBinderClient.h"
28 #include "client/AudioStreamInternalCapture.h"
29 #include "client/AudioStreamInternalPlay.h"
30 #include "core/AudioStream.h"
31 #include "core/AudioStreamBuilder.h"
32 #include "legacy/AudioStreamRecord.h"
33 #include "legacy/AudioStreamTrack.h"
34 
35 using namespace aaudio;
36 
37 #define AAUDIO_MMAP_POLICY_DEFAULT             AAUDIO_POLICY_NEVER
38 #define AAUDIO_MMAP_EXCLUSIVE_POLICY_DEFAULT   AAUDIO_POLICY_NEVER
39 
40 // These values are for a pre-check before we ask the lower level service to open a stream.
41 // So they are just outside the maximum conceivable range of value,
42 // on the edge of being ridiculous.
43 // TODO These defines should be moved to a central place in audio.
44 #define SAMPLES_PER_FRAME_MIN        1
45 // TODO Remove 8 channel limitation.
46 #define SAMPLES_PER_FRAME_MAX        FCC_8
47 #define SAMPLE_RATE_HZ_MIN           8000
48 // HDMI supports up to 32 channels at 1536000 Hz.
49 #define SAMPLE_RATE_HZ_MAX           1600000
50 #define FRAMES_PER_DATA_CALLBACK_MIN 1
51 #define FRAMES_PER_DATA_CALLBACK_MAX (1024 * 1024)
52 
53 /*
54  * AudioStreamBuilder
55  */
AudioStreamBuilder()56 AudioStreamBuilder::AudioStreamBuilder() {
57 }
58 
~AudioStreamBuilder()59 AudioStreamBuilder::~AudioStreamBuilder() {
60 }
61 
builder_createStream(aaudio_direction_t direction,aaudio_sharing_mode_t sharingMode,bool tryMMap,AudioStream ** audioStreamPtr)62 static aaudio_result_t builder_createStream(aaudio_direction_t direction,
63                                          aaudio_sharing_mode_t sharingMode,
64                                          bool tryMMap,
65                                          AudioStream **audioStreamPtr) {
66     *audioStreamPtr = nullptr;
67     aaudio_result_t result = AAUDIO_OK;
68 
69     switch (direction) {
70 
71         case AAUDIO_DIRECTION_INPUT:
72             if (tryMMap) {
73                 *audioStreamPtr = new AudioStreamInternalCapture(AAudioBinderClient::getInstance(),
74                                                                  false);
75             } else {
76                 *audioStreamPtr = new AudioStreamRecord();
77             }
78             break;
79 
80         case AAUDIO_DIRECTION_OUTPUT:
81             if (tryMMap) {
82                 *audioStreamPtr = new AudioStreamInternalPlay(AAudioBinderClient::getInstance(),
83                                                               false);
84             } else {
85                 *audioStreamPtr = new AudioStreamTrack();
86             }
87             break;
88 
89         default:
90             ALOGE("%s() bad direction = %d", __func__, direction);
91             result = AAUDIO_ERROR_ILLEGAL_ARGUMENT;
92     }
93     return result;
94 }
95 
96 // Try to open using MMAP path if that is allowed.
97 // Fall back to Legacy path if MMAP not available.
98 // Exact behavior is controlled by MMapPolicy.
build(AudioStream ** streamPtr)99 aaudio_result_t AudioStreamBuilder::build(AudioStream** streamPtr) {
100     AudioStream *audioStream = nullptr;
101     if (streamPtr == nullptr) {
102         ALOGE("%s() streamPtr is null", __func__);
103         return AAUDIO_ERROR_NULL;
104     }
105     *streamPtr = nullptr;
106 
107     logParameters();
108 
109     aaudio_result_t result = validate();
110     if (result != AAUDIO_OK) {
111         return result;
112     }
113 
114     // The API setting is the highest priority.
115     aaudio_policy_t mmapPolicy = AAudio_getMMapPolicy();
116     // If not specified then get from a system property.
117     if (mmapPolicy == AAUDIO_UNSPECIFIED) {
118         mmapPolicy = AAudioProperty_getMMapPolicy();
119     }
120     // If still not specified then use the default.
121     if (mmapPolicy == AAUDIO_UNSPECIFIED) {
122         mmapPolicy = AAUDIO_MMAP_POLICY_DEFAULT;
123     }
124 
125     int32_t mapExclusivePolicy = AAudioProperty_getMMapExclusivePolicy();
126     if (mapExclusivePolicy == AAUDIO_UNSPECIFIED) {
127         mapExclusivePolicy = AAUDIO_MMAP_EXCLUSIVE_POLICY_DEFAULT;
128     }
129 
130     aaudio_sharing_mode_t sharingMode = getSharingMode();
131     if ((sharingMode == AAUDIO_SHARING_MODE_EXCLUSIVE)
132         && (mapExclusivePolicy == AAUDIO_POLICY_NEVER)) {
133         ALOGD("%s() EXCLUSIVE sharing mode not supported. Use SHARED.", __func__);
134         sharingMode = AAUDIO_SHARING_MODE_SHARED;
135         setSharingMode(sharingMode);
136     }
137 
138     bool allowMMap = mmapPolicy != AAUDIO_POLICY_NEVER;
139     bool allowLegacy = mmapPolicy != AAUDIO_POLICY_ALWAYS;
140 
141     // TODO Support other performance settings in MMAP mode.
142     // Disable MMAP if low latency not requested.
143     if (getPerformanceMode() != AAUDIO_PERFORMANCE_MODE_LOW_LATENCY) {
144         ALOGD("%s() MMAP not available because AAUDIO_PERFORMANCE_MODE_LOW_LATENCY not used.",
145               __func__);
146         allowMMap = false;
147     }
148 
149     // SessionID and Effects are only supported in Legacy mode.
150     if (getSessionId() != AAUDIO_SESSION_ID_NONE) {
151         ALOGD("%s() MMAP not available because sessionId used.", __func__);
152         allowMMap = false;
153     }
154 
155     if (!allowMMap && !allowLegacy) {
156         ALOGE("%s() no backend available: neither MMAP nor legacy path are allowed", __func__);
157         return AAUDIO_ERROR_ILLEGAL_ARGUMENT;
158     }
159 
160     result = builder_createStream(getDirection(), sharingMode, allowMMap, &audioStream);
161     if (result == AAUDIO_OK) {
162         // Open the stream using the parameters from the builder.
163         result = audioStream->open(*this);
164         if (result == AAUDIO_OK) {
165             *streamPtr = audioStream;
166         } else {
167             bool isMMap = audioStream->isMMap();
168             delete audioStream;
169             audioStream = nullptr;
170 
171             if (isMMap && allowLegacy) {
172                 ALOGV("%s() MMAP stream did not open so try Legacy path", __func__);
173                 // If MMAP stream failed to open then TRY using a legacy stream.
174                 result = builder_createStream(getDirection(), sharingMode,
175                                               false, &audioStream);
176                 if (result == AAUDIO_OK) {
177                     result = audioStream->open(*this);
178                     if (result == AAUDIO_OK) {
179                         *streamPtr = audioStream;
180                     } else {
181                         delete audioStream;
182                     }
183                 }
184             }
185         }
186     }
187 
188     return result;
189 }
190 
validate() const191 aaudio_result_t AudioStreamBuilder::validate() const {
192 
193     // Check for values that are ridiculously out of range to prevent math overflow exploits.
194     // The service will do a better check.
195     aaudio_result_t result = AAudioStreamParameters::validate();
196     if (result != AAUDIO_OK) {
197         return result;
198     }
199 
200     switch (mPerformanceMode) {
201         case AAUDIO_PERFORMANCE_MODE_NONE:
202         case AAUDIO_PERFORMANCE_MODE_POWER_SAVING:
203         case AAUDIO_PERFORMANCE_MODE_LOW_LATENCY:
204             break;
205         default:
206             ALOGE("illegal performanceMode = %d", mPerformanceMode);
207             return AAUDIO_ERROR_ILLEGAL_ARGUMENT;
208             // break;
209     }
210 
211     // Prevent ridiculous values from causing problems.
212     if (mFramesPerDataCallback != AAUDIO_UNSPECIFIED
213         && (mFramesPerDataCallback < FRAMES_PER_DATA_CALLBACK_MIN
214             || mFramesPerDataCallback > FRAMES_PER_DATA_CALLBACK_MAX)) {
215         ALOGE("framesPerDataCallback out of range = %d",
216               mFramesPerDataCallback);
217         return AAUDIO_ERROR_OUT_OF_RANGE;
218     }
219 
220     return AAUDIO_OK;
221 }
222 
AAudio_convertSharingModeToShortText(aaudio_sharing_mode_t sharingMode)223 static const char *AAudio_convertSharingModeToShortText(aaudio_sharing_mode_t sharingMode) {
224     switch (sharingMode) {
225         case AAUDIO_SHARING_MODE_EXCLUSIVE:
226             return "EX";
227         case AAUDIO_SHARING_MODE_SHARED:
228             return "SH";
229         default:
230             return "?!";
231     }
232 }
233 
AAudio_convertDirectionToText(aaudio_direction_t direction)234 static const char *AAudio_convertDirectionToText(aaudio_direction_t direction) {
235     switch (direction) {
236         case AAUDIO_DIRECTION_OUTPUT:
237             return "OUTPUT";
238         case AAUDIO_DIRECTION_INPUT:
239             return "INPUT";
240         default:
241             return "?!";
242     }
243 }
244 
logParameters() const245 void AudioStreamBuilder::logParameters() const {
246     // This is very helpful for debugging in the future. Please leave it in.
247     ALOGI("rate   = %6d, channels  = %d, format   = %d, sharing = %s, dir = %s",
248           getSampleRate(), getSamplesPerFrame(), getFormat(),
249           AAudio_convertSharingModeToShortText(getSharingMode()),
250           AAudio_convertDirectionToText(getDirection()));
251     ALOGI("device = %6d, sessionId = %d, perfMode = %d, callback: %s with frames = %d",
252           getDeviceId(),
253           getSessionId(),
254           getPerformanceMode(),
255           ((getDataCallbackProc() != nullptr) ? "ON" : "OFF"),
256           mFramesPerDataCallback);
257     ALOGI("usage  = %6d, contentType = %d, inputPreset = %d, allowedCapturePolicy = %d",
258           getUsage(), getContentType(), getInputPreset(), getAllowedCapturePolicy());
259 }
260