blob: 666df3a385d7e355637af2def8ee4b207d26d833 [file] [log] [blame]
Phil Burk204a1632017-01-03 17:23:43 -08001/*
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
Phil Burk5ed503c2017-02-01 09:38:15 -080017#ifndef AAUDIO_AUDIOSTREAMINTERNAL_H
18#define AAUDIO_AUDIOSTREAMINTERNAL_H
Phil Burk204a1632017-01-03 17:23:43 -080019
20#include <stdint.h>
Phil Burk5ed503c2017-02-01 09:38:15 -080021#include <aaudio/AAudio.h>
Phil Burk204a1632017-01-03 17:23:43 -080022
Phil Burk5ed503c2017-02-01 09:38:15 -080023#include "binding/IAAudioService.h"
Phil Burk204a1632017-01-03 17:23:43 -080024#include "binding/AudioEndpointParcelable.h"
25#include "client/IsochronousClockModel.h"
26#include "client/AudioEndpoint.h"
27#include "core/AudioStream.h"
28
29using android::sp;
Phil Burk5ed503c2017-02-01 09:38:15 -080030using android::IAAudioService;
Phil Burk204a1632017-01-03 17:23:43 -080031
Phil Burk5ed503c2017-02-01 09:38:15 -080032namespace aaudio {
Phil Burk204a1632017-01-03 17:23:43 -080033
Phil Burk5ed503c2017-02-01 09:38:15 -080034// A stream that talks to the AAudioService or directly to a HAL.
Phil Burk204a1632017-01-03 17:23:43 -080035class AudioStreamInternal : public AudioStream {
36
37public:
38 AudioStreamInternal();
39 virtual ~AudioStreamInternal();
40
41 // =========== Begin ABSTRACT methods ===========================
Phil Burk5ed503c2017-02-01 09:38:15 -080042 virtual aaudio_result_t requestStart() override;
Phil Burk204a1632017-01-03 17:23:43 -080043
Phil Burk5ed503c2017-02-01 09:38:15 -080044 virtual aaudio_result_t requestPause() override;
Phil Burk204a1632017-01-03 17:23:43 -080045
Phil Burk5ed503c2017-02-01 09:38:15 -080046 virtual aaudio_result_t requestFlush() override;
Phil Burk204a1632017-01-03 17:23:43 -080047
Phil Burk5ed503c2017-02-01 09:38:15 -080048 virtual aaudio_result_t requestStop() override;
Phil Burk204a1632017-01-03 17:23:43 -080049
Phil Burk5ed503c2017-02-01 09:38:15 -080050 // TODO use aaudio_clockid_t all the way down to AudioClock
51 virtual aaudio_result_t getTimestamp(clockid_t clockId,
52 aaudio_position_frames_t *framePosition,
53 aaudio_nanoseconds_t *timeNanoseconds) override;
Phil Burk204a1632017-01-03 17:23:43 -080054
55
Phil Burk5ed503c2017-02-01 09:38:15 -080056 virtual aaudio_result_t updateState() override;
Phil Burk204a1632017-01-03 17:23:43 -080057 // =========== End ABSTRACT methods ===========================
58
Phil Burk5ed503c2017-02-01 09:38:15 -080059 virtual aaudio_result_t open(const AudioStreamBuilder &builder) override;
Phil Burk204a1632017-01-03 17:23:43 -080060
Phil Burk5ed503c2017-02-01 09:38:15 -080061 virtual aaudio_result_t close() override;
Phil Burk204a1632017-01-03 17:23:43 -080062
Phil Burk5ed503c2017-02-01 09:38:15 -080063 virtual aaudio_result_t write(const void *buffer,
Phil Burk204a1632017-01-03 17:23:43 -080064 int32_t numFrames,
Phil Burk5ed503c2017-02-01 09:38:15 -080065 aaudio_nanoseconds_t timeoutNanoseconds) override;
Phil Burk204a1632017-01-03 17:23:43 -080066
Phil Burk5ed503c2017-02-01 09:38:15 -080067 virtual aaudio_result_t waitForStateChange(aaudio_stream_state_t currentState,
68 aaudio_stream_state_t *nextState,
69 aaudio_nanoseconds_t timeoutNanoseconds) override;
Phil Burk204a1632017-01-03 17:23:43 -080070
Phil Burk5ed503c2017-02-01 09:38:15 -080071 virtual aaudio_result_t setBufferSize(aaudio_size_frames_t requestedFrames,
72 aaudio_size_frames_t *actualFrames) override;
Phil Burk204a1632017-01-03 17:23:43 -080073
Phil Burk5ed503c2017-02-01 09:38:15 -080074 virtual aaudio_size_frames_t getBufferSize() const override;
Phil Burk204a1632017-01-03 17:23:43 -080075
Phil Burk5ed503c2017-02-01 09:38:15 -080076 virtual aaudio_size_frames_t getBufferCapacity() const override;
Phil Burk204a1632017-01-03 17:23:43 -080077
Phil Burk5ed503c2017-02-01 09:38:15 -080078 virtual aaudio_size_frames_t getFramesPerBurst() const override;
Phil Burk204a1632017-01-03 17:23:43 -080079
Phil Burk5ed503c2017-02-01 09:38:15 -080080 virtual aaudio_position_frames_t getFramesRead() override;
Phil Burk204a1632017-01-03 17:23:43 -080081
82 virtual int32_t getXRunCount() const override {
83 return mXRunCount;
84 }
85
Phil Burk5ed503c2017-02-01 09:38:15 -080086 virtual aaudio_result_t registerThread() override;
Phil Burk204a1632017-01-03 17:23:43 -080087
Phil Burk5ed503c2017-02-01 09:38:15 -080088 virtual aaudio_result_t unregisterThread() override;
Phil Burk204a1632017-01-03 17:23:43 -080089
90protected:
91
Phil Burk5ed503c2017-02-01 09:38:15 -080092 aaudio_result_t processCommands();
Phil Burk204a1632017-01-03 17:23:43 -080093
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 */
Phil Burk5ed503c2017-02-01 09:38:15 -0800101 virtual aaudio_result_t writeNow(const void *buffer,
Phil Burk204a1632017-01-03 17:23:43 -0800102 int32_t numFrames,
Phil Burk5ed503c2017-02-01 09:38:15 -0800103 aaudio_nanoseconds_t currentTimeNanos,
104 aaudio_nanoseconds_t *wakeTimePtr);
Phil Burk204a1632017-01-03 17:23:43 -0800105
106 void onFlushFromServer();
107
Phil Burk5ed503c2017-02-01 09:38:15 -0800108 aaudio_result_t onEventFromServer(AAudioServiceMessage *message);
Phil Burk204a1632017-01-03 17:23:43 -0800109
Phil Burk5ed503c2017-02-01 09:38:15 -0800110 aaudio_result_t onTimestampFromServer(AAudioServiceMessage *message);
Phil Burk204a1632017-01-03 17:23:43 -0800111
112private:
113 IsochronousClockModel mClockModel;
114 AudioEndpoint mAudioEndpoint;
Phil Burk5ed503c2017-02-01 09:38:15 -0800115 aaudio_handle_t mServiceStreamHandle;
Phil Burk204a1632017-01-03 17:23:43 -0800116 EndpointDescriptor mEndpointDescriptor;
Phil Burk204a1632017-01-03 17:23:43 -0800117 // Offset from underlying frame position.
Phil Burk5ed503c2017-02-01 09:38:15 -0800118 aaudio_position_frames_t mFramesOffsetFromService = 0;
119 aaudio_position_frames_t mLastFramesRead = 0;
120 aaudio_size_frames_t mFramesPerBurst;
Phil Burk204a1632017-01-03 17:23:43 -0800121 int32_t mXRunCount = 0;
122
Phil Burk5ed503c2017-02-01 09:38:15 -0800123 void processTimestamp(uint64_t position, aaudio_nanoseconds_t time);
Phil Burk204a1632017-01-03 17:23:43 -0800124};
125
Phil Burk5ed503c2017-02-01 09:38:15 -0800126} /* namespace aaudio */
Phil Burk204a1632017-01-03 17:23:43 -0800127
Phil Burk5ed503c2017-02-01 09:38:15 -0800128#endif //AAUDIO_AUDIOSTREAMINTERNAL_H