AudioStreamInternal.h revision 3316d5e6d375a4f09c681205e9094d30a0bfc4a2
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
29using android::sp;
30using android::IAAudioService;
31
32namespace aaudio {
33
34// A stream that talks to the AAudioService or directly to a HAL.
35class AudioStreamInternal : public AudioStream {
36
37public:
38    AudioStreamInternal();
39    virtual ~AudioStreamInternal();
40
41    // =========== Begin ABSTRACT methods ===========================
42    virtual aaudio_result_t requestStart() override;
43
44    virtual aaudio_result_t requestPause() override;
45
46    virtual aaudio_result_t requestFlush() override;
47
48    virtual aaudio_result_t requestStop() override;
49
50    // TODO use aaudio_clockid_t all the way down to AudioClock
51    virtual aaudio_result_t getTimestamp(clockid_t clockId,
52                                       int64_t *framePosition,
53                                       int64_t *timeNanoseconds) override;
54
55
56    virtual aaudio_result_t updateState() override;
57    // =========== End ABSTRACT methods ===========================
58
59    virtual aaudio_result_t open(const AudioStreamBuilder &builder) override;
60
61    virtual aaudio_result_t close() override;
62
63    virtual aaudio_result_t write(const void *buffer,
64                             int32_t numFrames,
65                             int64_t timeoutNanoseconds) override;
66
67    virtual aaudio_result_t waitForStateChange(aaudio_stream_state_t currentState,
68                                          aaudio_stream_state_t *nextState,
69                                          int64_t timeoutNanoseconds) override;
70
71    virtual aaudio_result_t setBufferSize(int32_t requestedFrames) override;
72
73    virtual int32_t getBufferSize() const override;
74
75    virtual int32_t getBufferCapacity() const override;
76
77    virtual int32_t getFramesPerBurst() const override;
78
79    virtual int64_t getFramesRead() override;
80
81    virtual int32_t getXRunCount() const override {
82        return mXRunCount;
83    }
84
85    virtual aaudio_result_t registerThread() override;
86
87    virtual aaudio_result_t unregisterThread() override;
88
89protected:
90
91    aaudio_result_t processCommands();
92
93/**
94 * Low level write that will not block. It will just write as much as it can.
95 *
96 * It passed back a recommended time to wake up if wakeTimePtr is not NULL.
97 *
98 * @return the number of frames written or a negative error code.
99 */
100    virtual aaudio_result_t writeNow(const void *buffer,
101                                int32_t numFrames,
102                                int64_t currentTimeNanos,
103                                int64_t *wakeTimePtr);
104
105    void onFlushFromServer();
106
107    aaudio_result_t onEventFromServer(AAudioServiceMessage *message);
108
109    aaudio_result_t onTimestampFromServer(AAudioServiceMessage *message);
110
111private:
112    IsochronousClockModel    mClockModel;
113    AudioEndpoint            mAudioEndpoint;
114    aaudio_handle_t          mServiceStreamHandle;
115    EndpointDescriptor       mEndpointDescriptor;
116    // Offset from underlying frame position.
117    int64_t                  mFramesOffsetFromService = 0;
118    int64_t                  mLastFramesRead = 0;
119    int32_t                  mFramesPerBurst;
120    int32_t                  mXRunCount = 0;
121
122    void processTimestamp(uint64_t position, int64_t time);
123};
124
125} /* namespace aaudio */
126
127#endif //AAUDIO_AUDIOSTREAMINTERNAL_H
128