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