AudioStreamInternal.h revision c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fb
1/*
2 * Copyright (C) 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 AAUDIO_AUDIOSTREAMINTERNAL_H
18#define AAUDIO_AUDIOSTREAMINTERNAL_H
19
20#include <stdint.h>
21#include <aaudio/AAudio.h>
22
23#include "binding/IAAudioService.h"
24#include "binding/AudioEndpointParcelable.h"
25#include "client/IsochronousClockModel.h"
26#include "client/AudioEndpoint.h"
27#include "core/AudioStream.h"
28
29#include "binding/AAudioServiceInterface.h"
30
31using android::sp;
32using android::IAAudioService;
33
34namespace aaudio {
35
36// A stream that talks to the AAudioService or directly to a HAL.
37class AudioStreamInternal : public AudioStream {
38
39public:
40    AudioStreamInternal(AAudioServiceInterface  &serviceInterface, bool inService = false);
41    virtual ~AudioStreamInternal();
42
43    // =========== Begin ABSTRACT methods ===========================
44    aaudio_result_t requestStart() override;
45
46    aaudio_result_t requestPause() override;
47
48    aaudio_result_t requestFlush() override;
49
50    aaudio_result_t requestStop() override;
51
52    // TODO use aaudio_clockid_t all the way down to AudioClock
53    aaudio_result_t getTimestamp(clockid_t clockId,
54                                       int64_t *framePosition,
55                                       int64_t *timeNanoseconds) override;
56
57
58
59    virtual aaudio_result_t updateStateWhileWaiting() override;
60
61    // =========== End ABSTRACT methods ===========================
62
63    aaudio_result_t open(const AudioStreamBuilder &builder) override;
64
65    aaudio_result_t close() override;
66
67    aaudio_result_t write(const void *buffer,
68                             int32_t numFrames,
69                             int64_t timeoutNanoseconds) override;
70
71    aaudio_result_t setBufferSize(int32_t requestedFrames) override;
72
73    int32_t getBufferSize() const override;
74
75    int32_t getBufferCapacity() const override;
76
77    int32_t getFramesPerBurst() const override;
78
79    int64_t getFramesRead() override;
80
81    int32_t getXRunCount() const override {
82        return mXRunCount;
83    }
84
85    aaudio_result_t registerThread() override;
86
87    aaudio_result_t unregisterThread() override;
88
89    // Called internally from 'C'
90    void *callbackLoop();
91
92protected:
93
94    aaudio_result_t processCommands();
95
96    aaudio_result_t requestPauseInternal();
97
98    aaudio_result_t stopCallback();
99
100/**
101 * Low level write that will not block. It will just write as much as it can.
102 *
103 * It passed back a recommended time to wake up if wakeTimePtr is not NULL.
104 *
105 * @return the number of frames written or a negative error code.
106 */
107    aaudio_result_t writeNow(const void *buffer,
108                                     int32_t numFrames,
109                                     int64_t currentTimeNanos,
110                                     int64_t *wakeTimePtr);
111
112    void onFlushFromServer();
113
114    aaudio_result_t onEventFromServer(AAudioServiceMessage *message);
115
116    aaudio_result_t onTimestampFromServer(AAudioServiceMessage *message);
117
118    // Calculate timeout for an operation involving framesPerOperation.
119    int64_t calculateReasonableTimeout(int32_t framesPerOperation);
120
121private:
122    /*
123     * Asynchronous write with data conversion.
124     * @param buffer
125     * @param numFrames
126     * @return fdrames written or negative error
127     */
128    aaudio_result_t writeNowWithConversion(const void *buffer,
129                                     int32_t numFrames);
130    void processTimestamp(uint64_t position, int64_t time);
131
132    // Adjust timing model based on timestamp from service.
133
134    IsochronousClockModel    mClockModel;      // timing model for chasing the HAL
135    AudioEndpoint            mAudioEndpoint;   // sink for writes
136    aaudio_handle_t          mServiceStreamHandle; // opaque handle returned from service
137
138    AudioEndpointParcelable  mEndPointParcelable; // description of the buffers filled by service
139    EndpointDescriptor       mEndpointDescriptor; // buffer description with resolved addresses
140
141    aaudio_audio_format_t    mDeviceFormat = AAUDIO_FORMAT_UNSPECIFIED;
142
143    uint8_t                 *mCallbackBuffer = nullptr;
144    int32_t                  mCallbackFrames = 0;
145
146    // Offset from underlying frame position.
147    int64_t                  mFramesOffsetFromService = 0; // offset for timestamps
148    int64_t                  mLastFramesRead = 0; // used to prevent retrograde motion
149    int32_t                  mFramesPerBurst;     // frames per HAL transfer
150    int32_t                  mXRunCount = 0;      // how many underrun events?
151    float                    mVolume = 1.0;       // volume that the server told us to use
152
153    AAudioServiceInterface  &mServiceInterface;   // abstract interface to the service
154
155    // The service uses this for SHARED mode.
156    bool                     mInService = false;  // Are running in the client or the service?
157};
158
159} /* namespace aaudio */
160
161#endif //AAUDIO_AUDIOSTREAMINTERNAL_H
162