blob: c6b74e1450139c06574ca1805a4f1bdbd315c778 [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);
jiabin613e6ae2022-12-21 20:20:11 +000047 ~AAudioServiceStreamShared() override = 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
jiabine1001322023-12-04 23:58:39 +000053 aaudio_result_t open(const aaudio::AAudioStreamRequest &request) override
54 EXCLUDES(mUpMessageQueueLock);
Phil Burkc0c70e32017-02-09 13:18:38 -080055
Phil Burk8f4fe502020-07-15 23:54:50 +000056 void writeDataIfRoom(int64_t mmapFramesRead, const void *buffer, int32_t numFrames);
57
Phil Burk523b3042017-09-13 13:03:08 -070058 /**
Phil Burk8f4fe502020-07-15 23:54:50 +000059 * This must only be called under getAudioDataQueueLock().
Phil Burk523b3042017-09-13 13:03:08 -070060 * @return
61 */
Phil Burk0bd745e2020-10-17 18:20:01 +000062 std::shared_ptr<SharedRingBuffer> getAudioDataQueue_l()
63 REQUIRES(audioDataQueueLock) {
64 return mAudioDataQueue;
Phil Burk8f4fe502020-07-15 23:54:50 +000065 }
Phil Burkc0c70e32017-02-09 13:18:38 -080066
Phil Burk71f35bb2017-04-13 16:05:07 -070067 /* Keep a record of when a buffer transfer completed.
68 * This allows for a more accurate timing model.
69 */
Phil Burk97350f92017-07-21 15:59:44 -070070 void markTransferTime(Timestamp &timestamp);
71
72 void setTimestampPositionOffset(int64_t deltaFrames) {
73 mTimestampPositionOffset.store(deltaFrames);
74 }
Phil Burk71f35bb2017-04-13 16:05:07 -070075
Phil Burka5222e22017-07-28 13:31:14 -070076 void incrementXRunCount() {
Phil Burk23296382017-11-20 15:45:11 -080077 sendXRunCount(++mXRunCount);
Phil Burka5222e22017-07-28 13:31:14 -070078 }
79
80 int32_t getXRunCount() const {
81 return mXRunCount.load();
82 }
83
Phil Burk19e990e2018-03-22 13:59:34 -070084 const char *getTypeText() const override { return "Shared"; }
85
Phil Burk0bd745e2020-10-17 18:20:01 +000086 // This is public so that the thread safety annotation, GUARDED_BY(),
87 // Can work when another object takes the lock.
88 mutable std::mutex audioDataQueueLock;
89
Phil Burkc0c70e32017-02-09 13:18:38 -080090protected:
91
jiabin2a594622021-10-14 00:32:25 +000092 aaudio_result_t getAudioDataDescription_l(
93 AudioEndpointParcelable* parcelable) REQUIRES(mLock) override;
Phil Burkc0c70e32017-02-09 13:18:38 -080094
jiabin2a594622021-10-14 00:32:25 +000095 aaudio_result_t getFreeRunningPosition_l(
96 int64_t *positionFrames, int64_t *timeNanos) REQUIRES(mLock) override;
Phil Burkc0c70e32017-02-09 13:18:38 -080097
jiabin2a594622021-10-14 00:32:25 +000098 aaudio_result_t getHardwareTimestamp_l(
99 int64_t *positionFrames, int64_t *timeNanos) REQUIRES(mLock) override;
Phil Burk97350f92017-07-21 15:59:44 -0700100
Phil Burkec89b2e2017-06-20 15:05:06 -0700101 /**
102 * @param requestedCapacityFrames
103 * @param framesPerBurst
104 * @return capacity or negative error
105 */
106 static int32_t calculateBufferCapacity(int32_t requestedCapacityFrames,
107 int32_t framesPerBurst);
108
Phil Burkc0c70e32017-02-09 13:18:38 -0800109private:
Phil Burk8f4fe502020-07-15 23:54:50 +0000110
jiabine1001322023-12-04 23:58:39 +0000111 std::shared_ptr<SharedRingBuffer> mAudioDataQueue PT_GUARDED_BY(audioDataQueueLock);
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