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 ERROR_BAD_START = 4, 37 ERROR_BAD_OPEN = 5, 38 ERROR_INVALID_ARGUMENT = 6 39 }; 40 StreamBase()41 StreamBase() : 42 mChannelCount(0), 43 mChannelMask(0), 44 mNumExchangeChannels(0), 45 mSampleRate(0), 46 mRouteDeviceId(ROUTING_DEVICE_NONE), 47 mBufferSizeInFrames(-1) {} ~StreamBase()48 virtual ~StreamBase() {} 49 50 // 51 // Attributes 52 // 53 static const int32_t ROUTING_DEVICE_NONE = -1; 54 55 static const int32_t SHARING_MODE_INVALID = -1; 56 getNumBufferFrames()57 int32_t getNumBufferFrames() const { return mBufferSizeInFrames; } 58 59 // 60 // State 61 // 62 63 /** 64 * Build the stream. 65 * 66 * @param channelCount The number of channels in the stream. 67 * @param channelMask The Channel mask for the stream. 68 * @param sampleRate The sample rate. 69 * @param performanceMode A performance mode constant. 70 * @param sharingMode A sharing mode constant. 71 * @param routeDeviceId The ID of the device to route the stream to/from. 72 * @return A Result code indicating the outcome of the call. 73 */ 74 virtual Result buildStream(int32_t channelCount, int32_t channelMask, int32_t sampleRate, 75 int32_t performanceMode, int32_t sharingMode, int32_t routeDeviceId) = 0; 76 77 /** 78 * Opens the stream. I.e. puts it in a state ready to be started. 79 * @return A Result code indicating the outcome of the call. 80 */ 81 virtual Result openStream() = 0; 82 83 /** 84 * Begin the playback/recording process. 85 * 86 * In concrete subclasses, this may be either synchronous or asynchronous. 87 * @return ERROR_NONE if successful, otherwise an error code 88 */ 89 virtual Result startStream() = 0; 90 91 /** 92 * Stop the playback/recording process. 93 * 94 * In concrete subclasses, this may be either synchronous or asynchronous. 95 */ 96 virtual Result stopStream() = 0; 97 98 /** 99 * Closes the stream and releases resources needed for a started stream. 100 * The Stream cannot be started again without another call to openStream(). 101 * @return 102 */ 103 virtual Result closeStream() = 0; 104 105 /** 106 * Deinitializes the stream. 107 * Concrete subclasses should stop the stream (is not already stopped) and deallocate any 108 * resources being used by the stream. 109 * The stream cannot be started again without another call to buildStream(). 110 * 111 * The expectation is that this method will be synchronous in concrete subclasses. 112 * @return ERROR_NONE if successful, otherwise an error code 113 */ 114 virtual Result teardownStream() = 0; 115 116 protected: 117 // Audio attributes 118 int32_t mChannelCount; 119 int32_t mChannelMask; 120 int32_t mNumExchangeChannels; // this may be mChannelCount or the number of channels 121 // associated with mChannelMask 122 int32_t mSampleRate; 123 int32_t mRouteDeviceId; 124 int32_t mBufferSizeInFrames; 125 126 // from org.hyphonate.megaaudio.common.BuilderBase 127 static const int32_t SUB_TYPE_OBOE_AAUDIO = 0x0001; 128 static const int32_t SUB_TYPE_OBOE_OPENSL_ES = 0x0002; 129 }; 130 131 #endif // MEGA_COMMON_STREAMBASE_H 132 133