1 /*
2  * Copyright 2020 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 MEGA_COMMON_STREAMBASE_H
18 #define MEGA_COMMON_STREAMBASE_H
19 
20 #include <cstdint>
21 
22 class AudioSink;
23 
24 class StreamBase {
25 public:
26     //
27     // Error Codes
28     // These values must be kept in sync with the equivalent symbols in
29     // org.hyphonate.megaaudio.common.Streambase.java
30     //
31     enum Result {
32         OK = 0,
33         ERROR_UNKNOWN = -1,
34         ERROR_UNSUPPORTED = -2,
35         ERROR_INVALID_STATE = -3
36     };
37 
StreamBase()38     StreamBase() :
39         mChannelCount(0),
40         mSampleRate(0),
41         mRouteDeviceId(ROUTING_DEVICE_NONE),
42         mBufferSizeInFrames(-1) {}
~StreamBase()43     virtual ~StreamBase() {}
44 
45     //
46     // Attributes
47     //
48     static const int32_t ROUTING_DEVICE_NONE    = -1;
getRoutedDeviceId()49     int32_t getRoutedDeviceId() const { return mRouteDeviceId; }
50 
getNumBufferFrames()51     int32_t getNumBufferFrames() const { return mBufferSizeInFrames; }
52 
53     //
54     // State
55     //
56     /**
57      * Initializes the audio stream as specified, but does not start the stream. Specifically,
58      * concrete subclasses should do whatever initialization and resource allocation required
59      * such that the stream can be started (in startStream()) as quickly as possible.
60      *
61      * The expectation is that this method will be synchronous in concrete subclasses.
62      *
63      * @param channelCount  the number of channels in the audio data
64      * @param sampleRate the desired playback sample rate
65      * @param the device id of the device to route the audio to.
66      * @param a pointer to an AudioSource (subclass) object which will provide the audio data.
67      * @return ERROR_NONE if successful, otherwise an error code
68      */
69     virtual Result setupStream(int32_t channelCount, int32_t sampleRate, int32_t routeDeviceId) = 0;
70 
71     /**
72      * Deinitializes the stream.
73      * Concrete subclasses should stop the stream (is not already stopped) and deallocate any
74      * resources being used by the stream.
75      * The stream cannot be started again without another call to setupAudioStream().
76      *
77      * The expectation is that this method will be synchronous in concrete subclasses.
78      * @return ERROR_NONE if successful, otherwise an error code
79      */
80     virtual Result teardownStream() = 0;
81 
82     /**
83      * Begin the playback/recording process.
84      *
85      * In concrete subclasses, this may be either synchronous or asynchronous.
86      * @return ERROR_NONE if successful, otherwise an error code
87      */
88     virtual Result startStream() = 0;
89 
90     /**
91      * Stop the playback/recording process.
92      *
93      * In concrete subclasses, this may be either synchronous or asynchronous.
94      */
95     virtual Result stopStream() = 0;
96 
97 protected:
98     // Audio attributes
99     int32_t mChannelCount;
100     int32_t mSampleRate;
101     int32_t mRouteDeviceId;
102     int32_t mBufferSizeInFrames;
103 
104     // from org.hyphonate.megaaudio.common.BuilderBase
105     static const int32_t SUB_TYPE_OBOE_AAUDIO       = 0x0001;
106     static const int32_t SUB_TYPE_OBOE_OPENSL_ES    = 0x0002;
107 };
108 
109 #endif // MEGA_COMMON_STREAMBASE_H
110 
111