AudioStreamBuilder.cpp revision f53e613b3dedab3ecada2c93d8846233c442d129
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/AAudioDefinitions.h>
25#include <aaudio/AAudio.h>
26
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
44aaudio_result_t AudioStreamBuilder::build(AudioStream** streamPtr) {
45    // TODO Is there a better place to put the code that decides which class to use?
46    AudioStream* audioStream = nullptr;
47    const aaudio_sharing_mode_t sharingMode = getSharingMode();
48    switch (getDirection()) {
49    case AAUDIO_DIRECTION_INPUT:
50        switch (sharingMode) {
51            case AAUDIO_SHARING_MODE_LEGACY:
52                audioStream = new(std::nothrow) AudioStreamRecord();
53                break;
54            default:
55                ALOGE("AudioStreamBuilder(): bad sharing mode = %d", sharingMode);
56                return AAUDIO_ERROR_ILLEGAL_ARGUMENT;
57                break;
58        }
59        break;
60    case AAUDIO_DIRECTION_OUTPUT:
61        switch (sharingMode) {
62            case AAUDIO_SHARING_MODE_LEGACY:
63                audioStream = new(std::nothrow) AudioStreamTrack();
64                break;
65            case AAUDIO_SHARING_MODE_EXCLUSIVE:
66                audioStream = new(std::nothrow) AudioStreamInternal();
67                break;
68            default:
69                ALOGE("AudioStreamBuilder(): bad sharing mode = %d", sharingMode);
70                return AAUDIO_ERROR_ILLEGAL_ARGUMENT;
71                break;
72        }
73        break;
74    default:
75        ALOGE("AudioStreamBuilder(): bad direction = %d", getDirection());
76        return AAUDIO_ERROR_ILLEGAL_ARGUMENT;
77        break;
78    }
79    if (audioStream == nullptr) {
80        return AAUDIO_ERROR_NO_MEMORY;
81    }
82    ALOGD("AudioStreamBuilder(): created audioStream = %p", audioStream);
83
84    // TODO maybe move this out of build and pass the builder to the constructors
85    // Open the stream using the parameters from the builder.
86    const aaudio_result_t result = audioStream->open(*this);
87    if (result != AAUDIO_OK) {
88        delete audioStream;
89    } else {
90        *streamPtr = audioStream;
91    }
92    return result;
93}
94