blob: 61769b56cdc07df475fccb4a17a537c28366b6be [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
55 aaudio_result_t close() override;
56
Phil Burk523b3042017-09-13 13:03:08 -070057 /**
58 * This must be locked when calling getAudioDataFifoBuffer_l() and while
59 * using the FifoBuffer it returns.
60 */
61 std::mutex &getAudioDataQueueLock() {
62 return mAudioDataQueueLock;
63 }
64
65 /**
66 * This must only be call under getAudioDataQueueLock().
67 * @return
68 */
69 android::FifoBuffer *getAudioDataFifoBuffer_l() { return (mAudioDataQueue == nullptr)
70 ? nullptr
71 : mAudioDataQueue->getFifoBuffer(); }
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 Burk523b3042017-09-13 13:03:08 -0700109 SharedRingBuffer *mAudioDataQueue = nullptr; // protected by mAudioDataQueueLock
110 std::mutex mAudioDataQueueLock;
Phil Burk71f35bb2017-04-13 16:05:07 -0700111
Phil Burk97350f92017-07-21 15:59:44 -0700112 std::atomic<int64_t> mTimestampPositionOffset;
Phil Burka5222e22017-07-28 13:31:14 -0700113 std::atomic<int32_t> mXRunCount;
Phil Burk39f02dd2017-08-04 09:13:31 -0700114
Phil Burkc0c70e32017-02-09 13:18:38 -0800115};
116
117} /* namespace aaudio */
118
119#endif //AAUDIO_AAUDIO_SERVICE_STREAM_SHARED_H