blob: 8499ea5d6c16f0e5b108ed1dd3d0761ec399e62b [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
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() {
83 mXRunCount++;
84 }
85
86 int32_t getXRunCount() const {
87 return mXRunCount.load();
88 }
89
Phil Burkc0c70e32017-02-09 13:18:38 -080090protected:
91
Phil Burk523b3042017-09-13 13:03:08 -070092 aaudio_result_t getAudioDataDescription(AudioEndpointParcelable &parcelable) override;
Phil Burkc0c70e32017-02-09 13:18:38 -080093
94 aaudio_result_t getFreeRunningPosition(int64_t *positionFrames, int64_t *timeNanos) override;
95
Phil Burk39f02dd2017-08-04 09:13:31 -070096 aaudio_result_t getHardwareTimestamp(int64_t *positionFrames, int64_t *timeNanos) override;
Phil Burk97350f92017-07-21 15:59:44 -070097
Phil Burkec89b2e2017-06-20 15:05:06 -070098 /**
99 * @param requestedCapacityFrames
100 * @param framesPerBurst
101 * @return capacity or negative error
102 */
103 static int32_t calculateBufferCapacity(int32_t requestedCapacityFrames,
104 int32_t framesPerBurst);
105
Phil Burkc0c70e32017-02-09 13:18:38 -0800106private:
Phil Burk523b3042017-09-13 13:03:08 -0700107 SharedRingBuffer *mAudioDataQueue = nullptr; // protected by mAudioDataQueueLock
108 std::mutex mAudioDataQueueLock;
Phil Burk71f35bb2017-04-13 16:05:07 -0700109
Phil Burk97350f92017-07-21 15:59:44 -0700110 std::atomic<int64_t> mTimestampPositionOffset;
Phil Burka5222e22017-07-28 13:31:14 -0700111 std::atomic<int32_t> mXRunCount;
Phil Burk39f02dd2017-08-04 09:13:31 -0700112
Phil Burkc0c70e32017-02-09 13:18:38 -0800113};
114
115} /* namespace aaudio */
116
117#endif //AAUDIO_AAUDIO_SERVICE_STREAM_SHARED_H