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