1 /* 2 * Copyright 2016 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 LEGACY_AUDIO_STREAM_RECORD_H 18 #define LEGACY_AUDIO_STREAM_RECORD_H 19 20 #include <media/AudioRecord.h> 21 #include <aaudio/AAudio.h> 22 23 #include "AudioStreamBuilder.h" 24 #include "AudioStream.h" 25 #include "AAudioLegacy.h" 26 #include "legacy/AudioStreamLegacy.h" 27 #include "utility/FixedBlockWriter.h" 28 #include <android/content/AttributionSourceState.h> 29 30 namespace aaudio { 31 32 /** 33 * Internal stream that uses the legacy AudioRecord path. 34 */ 35 class AudioStreamRecord : public AudioStreamLegacy { 36 public: 37 AudioStreamRecord(); 38 39 virtual ~AudioStreamRecord(); 40 41 aaudio_result_t open(const AudioStreamBuilder & builder) override; 42 aaudio_result_t release_l() override; 43 void close_l() override; 44 45 virtual aaudio_result_t getTimestamp(clockid_t clockId, 46 int64_t *framePosition, 47 int64_t *timeNanoseconds) override; 48 49 aaudio_result_t read(void *buffer, 50 int32_t numFrames, 51 int64_t timeoutNanoseconds) override; 52 53 aaudio_result_t setBufferSize(int32_t requestedFrames) override; 54 55 int32_t getBufferSize() const override; 56 57 int32_t getXRunCount() const override; 58 59 int64_t getFramesWritten() override; 60 61 aaudio_result_t updateStateMachine() override; 62 getDirection()63 aaudio_direction_t getDirection() const override { 64 return AAUDIO_DIRECTION_INPUT; 65 } 66 67 // This is public so it can be called from the C callback function. 68 void processCallback(int event, void *info); 69 70 void processCallbackRecord(aaudio_callback_operation_t opcode, void *info); 71 incrementClientFrameCounter(int32_t frames)72 int64_t incrementClientFrameCounter(int32_t frames) override { 73 return incrementFramesRead(frames); 74 } 75 76 const void * maybeConvertDeviceData(const void *audioData, int32_t numFrames) override; 77 78 protected: 79 80 aaudio_result_t requestStart_l() REQUIRES(mStreamLock) override; 81 aaudio_result_t requestStop_l() REQUIRES(mStreamLock) override; 82 83 int32_t getFramesPerBurstFromDevice() const override; 84 int32_t getBufferCapacityFromDevice() const override; 85 86 private: 87 android::sp<android::AudioRecord> mAudioRecord; 88 // adapts between variable sized blocks and fixed size blocks 89 FixedBlockWriter mFixedBlockWriter; 90 91 // TODO add 64-bit position reporting to AudioRecord and use it. 92 android::content::AttributionSourceState mAttributionSource; 93 94 // Only one type of conversion buffer is used. 95 std::unique_ptr<float[]> mFormatConversionBufferFloat; 96 std::unique_ptr<int16_t[]> mFormatConversionBufferI16; 97 int32_t mFormatConversionBufferSizeInFrames = 0; 98 }; 99 100 } /* namespace aaudio */ 101 102 #endif /* LEGACY_AUDIO_STREAM_RECORD_H */ 103