blob: 36a56b81d54674ffaa19c4bbdef18db1272da806 [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 Burkc0c70e32017-02-09 13:18:38 -080053 aaudio_result_t open(const aaudio::AAudioStreamRequest &request,
54 aaudio::AAudioStreamConfiguration &configurationOutput) override;
55
56 /**
57 * Start the flow of audio data.
58 *
59 * This is not guaranteed to be synchronous but it currently is.
60 * An AAUDIO_SERVICE_EVENT_STARTED will be sent to the client when complete.
61 */
62 aaudio_result_t start() override;
63
64 /**
65 * Stop the flow of data so that start() can resume without loss of data.
66 *
67 * This is not guaranteed to be synchronous but it currently is.
68 * An AAUDIO_SERVICE_EVENT_PAUSED will be sent to the client when complete.
69 */
70 aaudio_result_t pause() override;
71
72 /**
Phil Burk71f35bb2017-04-13 16:05:07 -070073 * Stop the flow of data after data in buffer has played.
74 */
75 aaudio_result_t stop() override;
76
77 /**
Phil Burkc0c70e32017-02-09 13:18:38 -080078 * Discard any data held by the underlying HAL or Service.
79 *
80 * This is not guaranteed to be synchronous but it currently is.
81 * An AAUDIO_SERVICE_EVENT_FLUSHED will be sent to the client when complete.
82 */
83 aaudio_result_t flush() override;
84
85 aaudio_result_t close() override;
86
87 android::FifoBuffer *getDataFifoBuffer() { return mAudioDataQueue->getFifoBuffer(); }
88
Phil Burk71f35bb2017-04-13 16:05:07 -070089 /* Keep a record of when a buffer transfer completed.
90 * This allows for a more accurate timing model.
91 */
Phil Burk97350f92017-07-21 15:59:44 -070092 void markTransferTime(Timestamp &timestamp);
93
94 void setTimestampPositionOffset(int64_t deltaFrames) {
95 mTimestampPositionOffset.store(deltaFrames);
96 }
Phil Burk71f35bb2017-04-13 16:05:07 -070097
Phil Burka5222e22017-07-28 13:31:14 -070098 void incrementXRunCount() {
99 mXRunCount++;
100 }
101
102 int32_t getXRunCount() const {
103 return mXRunCount.load();
104 }
105
Phil Burkc0c70e32017-02-09 13:18:38 -0800106protected:
107
108 aaudio_result_t getDownDataDescription(AudioEndpointParcelable &parcelable) override;
109
110 aaudio_result_t getFreeRunningPosition(int64_t *positionFrames, int64_t *timeNanos) override;
111
Phil Burk97350f92017-07-21 15:59:44 -0700112 virtual aaudio_result_t getHardwareTimestamp(int64_t *positionFrames,
113 int64_t *timeNanos) override;
114
Phil Burkec89b2e2017-06-20 15:05:06 -0700115 /**
116 * @param requestedCapacityFrames
117 * @param framesPerBurst
118 * @return capacity or negative error
119 */
120 static int32_t calculateBufferCapacity(int32_t requestedCapacityFrames,
121 int32_t framesPerBurst);
122
Phil Burkc0c70e32017-02-09 13:18:38 -0800123private:
124 android::AAudioService &mAudioService;
125 AAudioServiceEndpoint *mServiceEndpoint = nullptr;
Phil Burk87c9f642017-05-17 07:22:39 -0700126 SharedRingBuffer *mAudioDataQueue = nullptr;
Phil Burk71f35bb2017-04-13 16:05:07 -0700127
Phil Burk97350f92017-07-21 15:59:44 -0700128 std::atomic<int64_t> mTimestampPositionOffset;
Phil Burka5222e22017-07-28 13:31:14 -0700129 std::atomic<int32_t> mXRunCount;
Phil Burkc0c70e32017-02-09 13:18:38 -0800130};
131
132} /* namespace aaudio */
133
134#endif //AAUDIO_AAUDIO_SERVICE_STREAM_SHARED_H