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 #ifndef AAUDIO_AUDIO_STREAM_BUILDER_H
18 #define AAUDIO_AUDIO_STREAM_BUILDER_H
19 
20 #include <stdint.h>
21 
22 #include <aaudio/AAudio.h>
23 
24 #include "AAudioStreamParameters.h"
25 #include "AudioStream.h"
26 
27 namespace aaudio {
28 
29 /**
30  * Factory class for an AudioStream.
31  */
32 class AudioStreamBuilder : public AAudioStreamParameters {
33 public:
34     AudioStreamBuilder();
35 
36     ~AudioStreamBuilder();
37 
isSharingModeMatchRequired()38     bool isSharingModeMatchRequired() const {
39         return mSharingModeMatchRequired;
40     }
41 
setSharingModeMatchRequired(bool required)42     AudioStreamBuilder* setSharingModeMatchRequired(bool required) {
43         mSharingModeMatchRequired = required;
44         return this;
45     }
46 
getPerformanceMode()47     int32_t getPerformanceMode() const {
48         return mPerformanceMode;
49     }
50 
setPerformanceMode(aaudio_performance_mode_t performanceMode)51     AudioStreamBuilder* setPerformanceMode(aaudio_performance_mode_t performanceMode) {
52         mPerformanceMode = performanceMode;
53         return this;
54     }
55 
getDataCallbackProc()56     AAudioStream_dataCallback getDataCallbackProc() const {
57         return mDataCallbackProc;
58     }
59 
setDataCallbackProc(AAudioStream_dataCallback proc)60     AudioStreamBuilder* setDataCallbackProc(AAudioStream_dataCallback proc) {
61         mDataCallbackProc = proc;
62         return this;
63     }
64 
getDataCallbackUserData()65     void *getDataCallbackUserData() const {
66         return mDataCallbackUserData;
67     }
68 
setDataCallbackUserData(void * userData)69     AudioStreamBuilder* setDataCallbackUserData(void *userData) {
70         mDataCallbackUserData = userData;
71         return this;
72     }
73 
getErrorCallbackProc()74     AAudioStream_errorCallback getErrorCallbackProc() const {
75         return mErrorCallbackProc;
76     }
77 
setErrorCallbackProc(AAudioStream_errorCallback proc)78     AudioStreamBuilder* setErrorCallbackProc(AAudioStream_errorCallback proc) {
79         mErrorCallbackProc = proc;
80         return this;
81     }
82 
setErrorCallbackUserData(void * userData)83     AudioStreamBuilder* setErrorCallbackUserData(void *userData) {
84         mErrorCallbackUserData = userData;
85         return this;
86     }
87 
getErrorCallbackUserData()88     void *getErrorCallbackUserData() const {
89         return mErrorCallbackUserData;
90     }
91 
getFramesPerDataCallback()92     int32_t getFramesPerDataCallback() const {
93         return mFramesPerDataCallback;
94     }
95 
setFramesPerDataCallback(int32_t sizeInFrames)96     AudioStreamBuilder* setFramesPerDataCallback(int32_t sizeInFrames) {
97         mFramesPerDataCallback = sizeInFrames;
98         return this;
99     }
100 
101     aaudio_result_t build(AudioStream **streamPtr);
102 
103     virtual aaudio_result_t validate() const override;
104 
105     void logParameters() const;
106 
107 private:
108     bool                       mSharingModeMatchRequired = false; // must match sharing mode requested
109     aaudio_performance_mode_t  mPerformanceMode = AAUDIO_PERFORMANCE_MODE_NONE;
110 
111     AAudioStream_dataCallback  mDataCallbackProc = nullptr;  // external callback functions
112     void                      *mDataCallbackUserData = nullptr;
113     int32_t                    mFramesPerDataCallback = AAUDIO_UNSPECIFIED; // frames
114 
115     AAudioStream_errorCallback mErrorCallbackProc = nullptr;
116     void                      *mErrorCallbackUserData = nullptr;
117 };
118 
119 } /* namespace aaudio */
120 
121 #endif //AAUDIO_AUDIO_STREAM_BUILDER_H
122