blob: 5b1f8da156da9c54cca26cb6f951abbfe2c3f99a [file] [log] [blame]
Phil Burk23296382017-11-20 15:45:11 -08001 /*
Phil Burkc0c70e32017-02-09 13:18:38 -08002 * 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:
Phil Burk19e990e2018-03-22 13:59:34 -070046 explicit 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
Phil Burk523b3042017-09-13 13:03:08 -070055 /**
Phil Burk8f4fe502020-07-15 23:54:50 +000056 * This must be locked when calling getAudioDataQueue_l() and while
57 * using the FifoBuffer it contains.
Phil Burk523b3042017-09-13 13:03:08 -070058 */
59 std::mutex &getAudioDataQueueLock() {
60 return mAudioDataQueueLock;
61 }
62
Phil Burk8f4fe502020-07-15 23:54:50 +000063 void writeDataIfRoom(int64_t mmapFramesRead, const void *buffer, int32_t numFrames);
64
Phil Burk523b3042017-09-13 13:03:08 -070065 /**
Phil Burk8f4fe502020-07-15 23:54:50 +000066 * This must only be called under getAudioDataQueueLock().
Phil Burk523b3042017-09-13 13:03:08 -070067 * @return
68 */
Phil Burk8f4fe502020-07-15 23:54:50 +000069 std::shared_ptr<SharedRingBuffer> getAudioDataQueue_l() {
70 return mAudioDataQueue;
71 }
Phil Burkc0c70e32017-02-09 13:18:38 -080072
Phil Burk71f35bb2017-04-13 16:05:07 -070073 /* Keep a record of when a buffer transfer completed.
74 * This allows for a more accurate timing model.
75 */
Phil Burk97350f92017-07-21 15:59:44 -070076 void markTransferTime(Timestamp &timestamp);
77
78 void setTimestampPositionOffset(int64_t deltaFrames) {
79 mTimestampPositionOffset.store(deltaFrames);
80 }
Phil Burk71f35bb2017-04-13 16:05:07 -070081
Phil Burka5222e22017-07-28 13:31:14 -070082 void incrementXRunCount() {
Phil Burk23296382017-11-20 15:45:11 -080083 sendXRunCount(++mXRunCount);
Phil Burka5222e22017-07-28 13:31:14 -070084 }
85
86 int32_t getXRunCount() const {
87 return mXRunCount.load();
88 }
89
Phil Burk19e990e2018-03-22 13:59:34 -070090 const char *getTypeText() const override { return "Shared"; }
91
Phil Burkc0c70e32017-02-09 13:18:38 -080092protected:
93
Phil Burk523b3042017-09-13 13:03:08 -070094 aaudio_result_t getAudioDataDescription(AudioEndpointParcelable &parcelable) override;
Phil Burkc0c70e32017-02-09 13:18:38 -080095
96 aaudio_result_t getFreeRunningPosition(int64_t *positionFrames, int64_t *timeNanos) override;
97
Phil Burk39f02dd2017-08-04 09:13:31 -070098 aaudio_result_t getHardwareTimestamp(int64_t *positionFrames, int64_t *timeNanos) override;
Phil Burk97350f92017-07-21 15:59:44 -070099
Phil Burkec89b2e2017-06-20 15:05:06 -0700100 /**
101 * @param requestedCapacityFrames
102 * @param framesPerBurst
103 * @return capacity or negative error
104 */
105 static int32_t calculateBufferCapacity(int32_t requestedCapacityFrames,
106 int32_t framesPerBurst);
107
Phil Burkc0c70e32017-02-09 13:18:38 -0800108private:
Phil Burk8f4fe502020-07-15 23:54:50 +0000109
110 std::shared_ptr<SharedRingBuffer> mAudioDataQueue; // protected by mAudioDataQueueLock
Phil Burk523b3042017-09-13 13:03:08 -0700111 std::mutex mAudioDataQueueLock;
Phil Burk71f35bb2017-04-13 16:05:07 -0700112
Phil Burk97350f92017-07-21 15:59:44 -0700113 std::atomic<int64_t> mTimestampPositionOffset;
Phil Burka5222e22017-07-28 13:31:14 -0700114 std::atomic<int32_t> mXRunCount;
Phil Burk39f02dd2017-08-04 09:13:31 -0700115
Phil Burkc0c70e32017-02-09 13:18:38 -0800116};
117
118} /* namespace aaudio */
119
120#endif //AAUDIO_AAUDIO_SERVICE_STREAM_SHARED_H