blob: a171cb0b0c3ae38140247b67af482488fba3ee41 [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_SERVICE_ENDPOINT_H
18#define AAUDIO_SERVICE_ENDPOINT_H
19
20#include <atomic>
21#include <functional>
22#include <mutex>
23#include <vector>
24
25#include "client/AudioStreamInternal.h"
Phil Burk87c9f642017-05-17 07:22:39 -070026#include "client/AudioStreamInternalPlay.h"
Phil Burk15f7cab2017-08-04 09:13:31 -070027#include "core/AAudioStreamParameters.h"
Phil Burkc0c70e32017-02-09 13:18:38 -080028#include "binding/AAudioServiceMessage.h"
Phil Burk15f7cab2017-08-04 09:13:31 -070029#include "binding/AAudioStreamConfiguration.h"
30
31#include "AAudioServiceStreamBase.h"
Phil Burkc0c70e32017-02-09 13:18:38 -080032
33namespace aaudio {
34
Phil Burk15f7cab2017-08-04 09:13:31 -070035/**
36 * AAudioServiceEndpoint is used by a subclass of AAudioServiceStreamBase
37 * to communicate with the underlying audio device or port.
38 */
39class AAudioServiceEndpoint
40 : public virtual android::RefBase
41 , public AAudioStreamParameters {
Phil Burkc0c70e32017-02-09 13:18:38 -080042public:
Phil Burkc0c70e32017-02-09 13:18:38 -080043
Phil Burka3901e92018-10-08 13:54:38 -070044 virtual ~AAudioServiceEndpoint() = default;
Phil Burk4501b352017-06-29 18:12:36 -070045
Phil Burk15f7cab2017-08-04 09:13:31 -070046 virtual std::string dump() const;
Phil Burkc0c70e32017-02-09 13:18:38 -080047
Phil Burk15f7cab2017-08-04 09:13:31 -070048 virtual aaudio_result_t open(const aaudio::AAudioStreamRequest &request) = 0;
Phil Burkc0c70e32017-02-09 13:18:38 -080049
Phil Burk15f7cab2017-08-04 09:13:31 -070050 virtual aaudio_result_t close() = 0;
51
Phil Burk6e2770e2018-05-01 13:03:52 -070052 aaudio_result_t registerStream(android::sp<AAudioServiceStreamBase> stream);
Phil Burk15f7cab2017-08-04 09:13:31 -070053
Phil Burk6e2770e2018-05-01 13:03:52 -070054 aaudio_result_t unregisterStream(android::sp<AAudioServiceStreamBase> stream);
Phil Burk15f7cab2017-08-04 09:13:31 -070055
56 virtual aaudio_result_t startStream(android::sp<AAudioServiceStreamBase> stream,
57 audio_port_handle_t *clientHandle) = 0;
58
59 virtual aaudio_result_t stopStream(android::sp<AAudioServiceStreamBase> stream,
60 audio_port_handle_t clientHandle) = 0;
61
62 virtual aaudio_result_t startClient(const android::AudioClient& client,
jiabind1f1cb62020-03-24 11:57:57 -070063 const audio_attributes_t *attr,
Phil Burk15f7cab2017-08-04 09:13:31 -070064 audio_port_handle_t *clientHandle) {
Phil Burk7ba46552019-04-15 08:58:08 -070065 ALOGD("AAudioServiceEndpoint::startClient(...) AAUDIO_ERROR_UNAVAILABLE");
Phil Burk15f7cab2017-08-04 09:13:31 -070066 return AAUDIO_ERROR_UNAVAILABLE;
67 }
68
69 virtual aaudio_result_t stopClient(audio_port_handle_t clientHandle) {
70 ALOGD("AAudioServiceEndpoint::stopClient(...) AAUDIO_ERROR_UNAVAILABLE");
71 return AAUDIO_ERROR_UNAVAILABLE;
72 }
73
74 /**
75 * @param positionFrames
76 * @param timeNanos
77 * @return AAUDIO_OK or AAUDIO_ERROR_UNAVAILABLE or other negative error
78 */
79 virtual aaudio_result_t getFreeRunningPosition(int64_t *positionFrames, int64_t *timeNanos) = 0;
80
Phil Burk56d34f22018-10-11 13:30:56 -070081 /**
82 * Set time that the associated frame was presented to the hardware.
83 *
84 * @param positionFrames receive position, input value is ignored
85 * @param timeNanos receive time, input value is ignored
86 * @return AAUDIO_OK or AAUDIO_ERROR_UNAVAILABLE or other negative error
87 */
Phil Burk15f7cab2017-08-04 09:13:31 -070088 virtual aaudio_result_t getTimestamp(int64_t *positionFrames, int64_t *timeNanos) = 0;
89
90 int32_t getFramesPerBurst() const {
91 return mFramesPerBurst;
92 }
Phil Burkc0c70e32017-02-09 13:18:38 -080093
Phil Burkec89b2e2017-06-20 15:05:06 -070094 int32_t getRequestedDeviceId() const { return mRequestedDeviceId; }
Phil Burk97350f92017-07-21 15:59:44 -070095
Eric Laurenta17ae742017-06-29 15:43:55 -070096 bool matches(const AAudioStreamConfiguration& configuration);
97
Phil Burk15f7cab2017-08-04 09:13:31 -070098 // This should only be called from the AAudioEndpointManager under a mutex.
99 int32_t getOpenCount() const {
100 return mOpenCount;
101 }
Phil Burkc0c70e32017-02-09 13:18:38 -0800102
Phil Burk15f7cab2017-08-04 09:13:31 -0700103 // This should only be called from the AAudioEndpointManager under a mutex.
104 void setOpenCount(int32_t count) {
105 mOpenCount = count;
106 }
107
Phil Burkbe0a5b62017-10-12 15:56:00 -0700108 bool isConnected() const {
109 return mConnected;
110 }
111
Phil Burka9876702020-04-20 18:16:15 -0700112 static audio_attributes_t getAudioAttributesFrom(const AAudioStreamParameters *params);
113
Phil Burk6e463ce2020-04-13 10:20:20 -0700114 // Stop, disconnect and release any streams registered on this endpoint.
115 void releaseRegisteredStreams();
116
117 bool isForSharing() const {
118 return mForSharing;
119 }
120
121 /**
122 *
123 * @param flag true if this endpoint is to be shared between multiple streams
124 */
125 void setForSharing(bool flag) {
126 mForSharing = flag;
127 }
128
Phil Burk15f7cab2017-08-04 09:13:31 -0700129protected:
Phil Burkbbd52862018-04-13 11:37:42 -0700130
131 /**
132 * @param portHandle
133 * @return return true if a stream with the given portHandle is registered
134 */
135 bool isStreamRegistered(audio_port_handle_t portHandle);
136
Phil Burk6e463ce2020-04-13 10:20:20 -0700137 std::vector<android::sp<AAudioServiceStreamBase>> disconnectRegisteredStreams();
Phil Burkc0c70e32017-02-09 13:18:38 -0800138
Phil Burk4501b352017-06-29 18:12:36 -0700139 mutable std::mutex mLockStreams;
Phil Burk15f7cab2017-08-04 09:13:31 -0700140 std::vector<android::sp<AAudioServiceStreamBase>> mRegisteredStreams;
Phil Burk87c9f642017-05-17 07:22:39 -0700141
Phil Burka53ffa62018-10-10 16:21:37 -0700142 SimpleDoubleBuffer<Timestamp> mAtomicEndpointTimestamp;
Phil Burk11e8d332017-05-24 09:59:02 -0700143
Phil Burk15f7cab2017-08-04 09:13:31 -0700144 android::AudioClient mMmapClient; // set in open, used in open and startStream
Phil Burk71f35bb2017-04-13 16:05:07 -0700145
Phil Burk15f7cab2017-08-04 09:13:31 -0700146 int32_t mFramesPerBurst = 0;
147 int32_t mOpenCount = 0;
Phil Burkec89b2e2017-06-20 15:05:06 -0700148 int32_t mRequestedDeviceId = 0;
Phil Burk15f7cab2017-08-04 09:13:31 -0700149
Phil Burk6e463ce2020-04-13 10:20:20 -0700150 // True if this will be shared by one or more other streams.
151 bool mForSharing = false;
152
Phil Burkbe0a5b62017-10-12 15:56:00 -0700153 std::atomic<bool> mConnected{true};
Phil Burk6e463ce2020-04-13 10:20:20 -0700154
Phil Burkc0c70e32017-02-09 13:18:38 -0800155};
156
157} /* namespace aaudio */
158
159
160#endif //AAUDIO_SERVICE_ENDPOINT_H