blob: bc86dccf148dc5985f7e41e107d32c2ce40df95b [file] [log] [blame]
Phil Burkc0c70e32017-02-09 13:18:38 -08001/*
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
17#ifndef AAUDIO_AAUDIO_SERVICE_STREAM_SHARED_H
18#define AAUDIO_AAUDIO_SERVICE_STREAM_SHARED_H
19
20#include "fifo/FifoBuffer.h"
21#include "binding/AAudioServiceMessage.h"
22#include "binding/AAudioStreamRequest.h"
23#include "binding/AAudioStreamConfiguration.h"
24
25#include "AAudioService.h"
26#include "AAudioServiceStreamBase.h"
27
28namespace aaudio {
29
30// We expect the queue to only have a few commands.
31// This should be way more than we need.
32#define QUEUE_UP_CAPACITY_COMMANDS (128)
33
34class AAudioEndpointManager;
35class AAudioServiceEndpoint;
36class SharedRingBuffer;
37
38/**
39 * One of these is created for every MODE_SHARED stream in the AAudioService.
40 *
41 * Each Shared stream will register itself with an AAudioServiceEndpoint when it is opened.
42 */
43class AAudioServiceStreamShared : public AAudioServiceStreamBase {
44
45public:
46 AAudioServiceStreamShared(android::AAudioService &aAudioService);
Phil Burk98d6d922017-07-06 11:52:45 -070047 virtual ~AAudioServiceStreamShared() = default;
Phil Burkc0c70e32017-02-09 13:18:38 -080048
Phil Burka5222e22017-07-28 13:31:14 -070049 static std::string dumpHeader();
50
51 std::string dump() const override;
52
Phil Burk39f02dd2017-08-04 09:13:31 -070053 aaudio_result_t open(const aaudio::AAudioStreamRequest &request) override;
Phil Burkc0c70e32017-02-09 13:18:38 -080054
55 aaudio_result_t close() override;
56
57 android::FifoBuffer *getDataFifoBuffer() { return mAudioDataQueue->getFifoBuffer(); }
58
Phil Burk71f35bb2017-04-13 16:05:07 -070059 /* Keep a record of when a buffer transfer completed.
60 * This allows for a more accurate timing model.
61 */
Phil Burk97350f92017-07-21 15:59:44 -070062 void markTransferTime(Timestamp &timestamp);
63
64 void setTimestampPositionOffset(int64_t deltaFrames) {
65 mTimestampPositionOffset.store(deltaFrames);
66 }
Phil Burk71f35bb2017-04-13 16:05:07 -070067
Phil Burka5222e22017-07-28 13:31:14 -070068 void incrementXRunCount() {
69 mXRunCount++;
70 }
71
72 int32_t getXRunCount() const {
73 return mXRunCount.load();
74 }
75
Phil Burkc0c70e32017-02-09 13:18:38 -080076protected:
77
78 aaudio_result_t getDownDataDescription(AudioEndpointParcelable &parcelable) override;
79
80 aaudio_result_t getFreeRunningPosition(int64_t *positionFrames, int64_t *timeNanos) override;
81
Phil Burk39f02dd2017-08-04 09:13:31 -070082 aaudio_result_t getHardwareTimestamp(int64_t *positionFrames, int64_t *timeNanos) override;
Phil Burk97350f92017-07-21 15:59:44 -070083
Phil Burkec89b2e2017-06-20 15:05:06 -070084 /**
85 * @param requestedCapacityFrames
86 * @param framesPerBurst
87 * @return capacity or negative error
88 */
89 static int32_t calculateBufferCapacity(int32_t requestedCapacityFrames,
90 int32_t framesPerBurst);
91
Phil Burkc0c70e32017-02-09 13:18:38 -080092private:
Phil Burk87c9f642017-05-17 07:22:39 -070093 SharedRingBuffer *mAudioDataQueue = nullptr;
Phil Burk71f35bb2017-04-13 16:05:07 -070094
Phil Burk97350f92017-07-21 15:59:44 -070095 std::atomic<int64_t> mTimestampPositionOffset;
Phil Burka5222e22017-07-28 13:31:14 -070096 std::atomic<int32_t> mXRunCount;
Phil Burk39f02dd2017-08-04 09:13:31 -070097
Phil Burkc0c70e32017-02-09 13:18:38 -080098};
99
100} /* namespace aaudio */
101
102#endif //AAUDIO_AAUDIO_SERVICE_STREAM_SHARED_H