1/*
2 * Copyright (C) 2017 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#ifndef ANDROID_AAUDIO_AUDIO_STREAM_INTERNAL_CAPTURE_H
17#define ANDROID_AAUDIO_AUDIO_STREAM_INTERNAL_CAPTURE_H
18
19#include <stdint.h>
20#include <aaudio/AAudio.h>
21
22#include "binding/AAudioServiceInterface.h"
23#include "client/AudioStreamInternal.h"
24
25using android::sp;
26using android::IAAudioService;
27
28namespace aaudio {
29
30class AudioStreamInternalCapture : public AudioStreamInternal {
31public:
32    AudioStreamInternalCapture(AAudioServiceInterface  &serviceInterface, bool inService = false);
33    virtual ~AudioStreamInternalCapture();
34
35    aaudio_result_t read(void *buffer,
36                         int32_t numFrames,
37                         int64_t timeoutNanoseconds) override;
38
39    int64_t getFramesRead() override;
40    int64_t getFramesWritten() override;
41
42    void *callbackLoop() override;
43
44    aaudio_direction_t getDirection() const override {
45        return AAUDIO_DIRECTION_INPUT;
46    }
47protected:
48
49/**
50 * Low level data processing that will not block. It will just read or write as much as it can.
51 *
52 * It passes back a recommended time to wake up if wakeTimePtr is not NULL.
53 *
54 * @return the number of frames processed or a negative error code.
55 */
56    aaudio_result_t processDataNow(void *buffer,
57                                   int32_t numFrames,
58                                   int64_t currentTimeNanos,
59                                   int64_t *wakeTimePtr) override;
60
61private:
62    /*
63     * Asynchronous read with data conversion.
64     * @param buffer
65     * @param numFrames
66     * @return frames written or negative error
67     */
68    aaudio_result_t readNowWithConversion(void *buffer, int32_t numFrames);
69
70    int64_t       mLastFramesWritten = 0; // used to prevent retrograde motion
71};
72
73} /* namespace aaudio */
74
75#endif //ANDROID_AAUDIO_AUDIO_STREAM_INTERNAL_CAPTURE_H
76