blob: 4fae5b4af68fedfe6df7cd62d2b3cd4cb521dc63 [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 Burk8f4fe502020-07-15 23:54:50 +000055 void writeDataIfRoom(int64_t mmapFramesRead, const void *buffer, int32_t numFrames);
56
Phil Burk523b3042017-09-13 13:03:08 -070057 /**
Phil Burk8f4fe502020-07-15 23:54:50 +000058 * This must only be called under getAudioDataQueueLock().
Phil Burk523b3042017-09-13 13:03:08 -070059 * @return
60 */
Phil Burk0bd745e2020-10-17 18:20:01 +000061 std::shared_ptr<SharedRingBuffer> getAudioDataQueue_l()
62 REQUIRES(audioDataQueueLock) {
63 return mAudioDataQueue;
Phil Burk8f4fe502020-07-15 23:54:50 +000064 }
Phil Burkc0c70e32017-02-09 13:18:38 -080065
Phil Burk71f35bb2017-04-13 16:05:07 -070066 /* Keep a record of when a buffer transfer completed.
67 * This allows for a more accurate timing model.
68 */
Phil Burk97350f92017-07-21 15:59:44 -070069 void markTransferTime(Timestamp &timestamp);
70
71 void setTimestampPositionOffset(int64_t deltaFrames) {
72 mTimestampPositionOffset.store(deltaFrames);
73 }
Phil Burk71f35bb2017-04-13 16:05:07 -070074
Phil Burka5222e22017-07-28 13:31:14 -070075 void incrementXRunCount() {
Phil Burk23296382017-11-20 15:45:11 -080076 sendXRunCount(++mXRunCount);
Phil Burka5222e22017-07-28 13:31:14 -070077 }
78
79 int32_t getXRunCount() const {
80 return mXRunCount.load();
81 }
82
Phil Burk19e990e2018-03-22 13:59:34 -070083 const char *getTypeText() const override { return "Shared"; }
84
Phil Burk0bd745e2020-10-17 18:20:01 +000085 // This is public so that the thread safety annotation, GUARDED_BY(),
86 // Can work when another object takes the lock.
87 mutable std::mutex audioDataQueueLock;
88
Phil Burkc0c70e32017-02-09 13:18:38 -080089protected:
90
Phil Burk523b3042017-09-13 13:03:08 -070091 aaudio_result_t getAudioDataDescription(AudioEndpointParcelable &parcelable) override;
Phil Burkc0c70e32017-02-09 13:18:38 -080092
93 aaudio_result_t getFreeRunningPosition(int64_t *positionFrames, int64_t *timeNanos) override;
94
Phil Burk39f02dd2017-08-04 09:13:31 -070095 aaudio_result_t getHardwareTimestamp(int64_t *positionFrames, int64_t *timeNanos) override;
Phil Burk97350f92017-07-21 15:59:44 -070096
Phil Burkec89b2e2017-06-20 15:05:06 -070097 /**
98 * @param requestedCapacityFrames
99 * @param framesPerBurst
100 * @return capacity or negative error
101 */
102 static int32_t calculateBufferCapacity(int32_t requestedCapacityFrames,
103 int32_t framesPerBurst);
104
Phil Burkc0c70e32017-02-09 13:18:38 -0800105private:
Phil Burk8f4fe502020-07-15 23:54:50 +0000106
Phil Burk0bd745e2020-10-17 18:20:01 +0000107 std::shared_ptr<SharedRingBuffer> mAudioDataQueue GUARDED_BY(audioDataQueueLock);
Phil Burk71f35bb2017-04-13 16:05:07 -0700108
Phil Burk97350f92017-07-21 15:59:44 -0700109 std::atomic<int64_t> mTimestampPositionOffset;
Phil Burka5222e22017-07-28 13:31:14 -0700110 std::atomic<int32_t> mXRunCount;
Phil Burk39f02dd2017-08-04 09:13:31 -0700111
Phil Burkc0c70e32017-02-09 13:18:38 -0800112};
113
114} /* namespace aaudio */
115
116#endif //AAUDIO_AAUDIO_SERVICE_STREAM_SHARED_H