blob: 8caccda29e1516a7d96cc4b356f53dcd20c5e3bd [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
49 aaudio_result_t open(const aaudio::AAudioStreamRequest &request,
50 aaudio::AAudioStreamConfiguration &configurationOutput) override;
51
52 /**
53 * Start the flow of audio data.
54 *
55 * This is not guaranteed to be synchronous but it currently is.
56 * An AAUDIO_SERVICE_EVENT_STARTED will be sent to the client when complete.
57 */
58 aaudio_result_t start() override;
59
60 /**
61 * Stop the flow of data so that start() can resume without loss of data.
62 *
63 * This is not guaranteed to be synchronous but it currently is.
64 * An AAUDIO_SERVICE_EVENT_PAUSED will be sent to the client when complete.
65 */
66 aaudio_result_t pause() override;
67
68 /**
Phil Burk71f35bb2017-04-13 16:05:07 -070069 * Stop the flow of data after data in buffer has played.
70 */
71 aaudio_result_t stop() override;
72
73 /**
Phil Burkc0c70e32017-02-09 13:18:38 -080074 * Discard any data held by the underlying HAL or Service.
75 *
76 * This is not guaranteed to be synchronous but it currently is.
77 * An AAUDIO_SERVICE_EVENT_FLUSHED will be sent to the client when complete.
78 */
79 aaudio_result_t flush() override;
80
81 aaudio_result_t close() override;
82
83 android::FifoBuffer *getDataFifoBuffer() { return mAudioDataQueue->getFifoBuffer(); }
84
Phil Burk71f35bb2017-04-13 16:05:07 -070085 /* Keep a record of when a buffer transfer completed.
86 * This allows for a more accurate timing model.
87 */
Phil Burk97350f92017-07-21 15:59:44 -070088 void markTransferTime(Timestamp &timestamp);
89
90 void setTimestampPositionOffset(int64_t deltaFrames) {
91 mTimestampPositionOffset.store(deltaFrames);
92 }
Phil Burk71f35bb2017-04-13 16:05:07 -070093
Phil Burkc0c70e32017-02-09 13:18:38 -080094protected:
95
96 aaudio_result_t getDownDataDescription(AudioEndpointParcelable &parcelable) override;
97
98 aaudio_result_t getFreeRunningPosition(int64_t *positionFrames, int64_t *timeNanos) override;
99
Phil Burk97350f92017-07-21 15:59:44 -0700100 virtual aaudio_result_t getHardwareTimestamp(int64_t *positionFrames,
101 int64_t *timeNanos) override;
102
Phil Burkec89b2e2017-06-20 15:05:06 -0700103 /**
104 * @param requestedCapacityFrames
105 * @param framesPerBurst
106 * @return capacity or negative error
107 */
108 static int32_t calculateBufferCapacity(int32_t requestedCapacityFrames,
109 int32_t framesPerBurst);
110
Phil Burkc0c70e32017-02-09 13:18:38 -0800111private:
112 android::AAudioService &mAudioService;
113 AAudioServiceEndpoint *mServiceEndpoint = nullptr;
Phil Burk87c9f642017-05-17 07:22:39 -0700114 SharedRingBuffer *mAudioDataQueue = nullptr;
Phil Burk71f35bb2017-04-13 16:05:07 -0700115
Phil Burk97350f92017-07-21 15:59:44 -0700116 std::atomic<int64_t> mTimestampPositionOffset;
Phil Burkc0c70e32017-02-09 13:18:38 -0800117};
118
119} /* namespace aaudio */
120
121#endif //AAUDIO_AAUDIO_SERVICE_STREAM_SHARED_H