Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
Phil Burk | 5204d31 | 2017-05-04 17:16:13 -0700 | [diff] [blame] | 17 | #ifndef ANDROID_AAUDIO_AAUDIO_BINDER_CLIENT_H |
| 18 | #define ANDROID_AAUDIO_AAUDIO_BINDER_CLIENT_H |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 19 | |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 20 | #include <utils/RefBase.h> |
Phil Burk | 9b3f8ef | 2017-05-16 11:37:43 -0700 | [diff] [blame] | 21 | #include <utils/Singleton.h> |
| 22 | |
Phil Burk | a4eb0d8 | 2017-04-12 15:44:06 -0700 | [diff] [blame] | 23 | #include <aaudio/AAudio.h> |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 24 | #include "AAudioServiceDefinitions.h" |
| 25 | #include "AAudioServiceInterface.h" |
| 26 | #include "binding/AAudioStreamRequest.h" |
| 27 | #include "binding/AAudioStreamConfiguration.h" |
| 28 | #include "binding/AudioEndpointParcelable.h" |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 29 | #include "binding/IAAudioService.h" |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 30 | |
| 31 | /** |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 32 | * Implements the AAudioServiceInterface by talking to the service through Binder. |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 33 | */ |
| 34 | |
| 35 | namespace aaudio { |
| 36 | |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 37 | class AAudioBinderClient : public virtual android::RefBase |
| 38 | , public AAudioServiceInterface |
Phil Burk | 9b3f8ef | 2017-05-16 11:37:43 -0700 | [diff] [blame] | 39 | , public android::Singleton<AAudioBinderClient> { |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 40 | |
| 41 | public: |
| 42 | |
| 43 | AAudioBinderClient(); |
| 44 | |
| 45 | virtual ~AAudioBinderClient(); |
| 46 | |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 47 | const android::sp<android::IAAudioService> getAAudioService(); |
| 48 | |
| 49 | void dropAAudioService(); |
| 50 | |
| 51 | void registerClient(const android::sp<android::IAAudioClient>& client __unused) override {} |
| 52 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 53 | /** |
| 54 | * @param request info needed to create the stream |
| 55 | * @param configuration contains resulting information about the created stream |
| 56 | * @return handle to the stream or a negative error |
| 57 | */ |
| 58 | aaudio_handle_t openStream(const AAudioStreamRequest &request, |
| 59 | AAudioStreamConfiguration &configurationOutput) override; |
| 60 | |
| 61 | aaudio_result_t closeStream(aaudio_handle_t streamHandle) override; |
| 62 | |
| 63 | /* Get an immutable description of the in-memory queues |
| 64 | * used to communicate with the underlying HAL or Service. |
| 65 | */ |
| 66 | aaudio_result_t getStreamDescription(aaudio_handle_t streamHandle, |
| 67 | AudioEndpointParcelable &parcelable) override; |
| 68 | |
| 69 | /** |
| 70 | * Start the flow of data. |
| 71 | * This is asynchronous. When complete, the service will send a STARTED event. |
| 72 | */ |
| 73 | aaudio_result_t startStream(aaudio_handle_t streamHandle) override; |
| 74 | |
| 75 | /** |
| 76 | * Stop the flow of data such that start() can resume without loss of data. |
| 77 | * This is asynchronous. When complete, the service will send a PAUSED event. |
| 78 | */ |
| 79 | aaudio_result_t pauseStream(aaudio_handle_t streamHandle) override; |
| 80 | |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 81 | aaudio_result_t stopStream(aaudio_handle_t streamHandle) override; |
| 82 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 83 | /** |
| 84 | * Discard any data held by the underlying HAL or Service. |
| 85 | * This is asynchronous. When complete, the service will send a FLUSHED event. |
| 86 | */ |
| 87 | aaudio_result_t flushStream(aaudio_handle_t streamHandle) override; |
| 88 | |
| 89 | /** |
| 90 | * Manage the specified thread as a low latency audio thread. |
| 91 | * TODO Consider passing this information as part of the startStream() call. |
| 92 | */ |
| 93 | aaudio_result_t registerAudioThread(aaudio_handle_t streamHandle, |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 94 | pid_t clientThreadId, |
| 95 | int64_t periodNanoseconds) override; |
| 96 | |
| 97 | aaudio_result_t unregisterAudioThread(aaudio_handle_t streamHandle, |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 98 | pid_t clientThreadId) override; |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 99 | |
Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 100 | aaudio_result_t startClient(aaudio_handle_t streamHandle __unused, |
jiabin | d1f1cb6 | 2020-03-24 11:57:57 -0700 | [diff] [blame] | 101 | const android::AudioClient& client __unused, |
| 102 | const audio_attributes_t *attr __unused, |
| 103 | audio_port_handle_t *clientHandle __unused) override { |
Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 104 | return AAUDIO_ERROR_UNAVAILABLE; |
| 105 | } |
| 106 | |
| 107 | aaudio_result_t stopClient(aaudio_handle_t streamHandle __unused, |
| 108 | audio_port_handle_t clientHandle __unused) override { |
| 109 | return AAUDIO_ERROR_UNAVAILABLE; |
| 110 | } |
| 111 | |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 112 | void onStreamChange(aaudio_handle_t handle, int32_t opcode, int32_t value) { |
| 113 | // TODO This is just a stub so we can have a client Binder to pass to the service. |
| 114 | // TODO Implemented in a later CL. |
| 115 | ALOGW("onStreamChange called!"); |
| 116 | } |
| 117 | |
| 118 | class AAudioClient : public android::IBinder::DeathRecipient , public android::BnAAudioClient |
| 119 | { |
| 120 | public: |
| 121 | AAudioClient(android::wp<AAudioBinderClient> aaudioBinderClient) |
Phil Burk | 2bc7c18 | 2017-08-28 11:45:01 -0700 | [diff] [blame] | 122 | : mBinderClient(aaudioBinderClient) { |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | // implement DeathRecipient |
| 126 | virtual void binderDied(const android::wp<android::IBinder>& who __unused) { |
| 127 | android::sp<AAudioBinderClient> client = mBinderClient.promote(); |
Phil Burk | 2bc7c18 | 2017-08-28 11:45:01 -0700 | [diff] [blame] | 128 | if (client.get() != nullptr) { |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 129 | client->dropAAudioService(); |
| 130 | } |
| 131 | ALOGW("AAudio service binderDied()!"); |
| 132 | } |
| 133 | |
| 134 | // implement BnAAudioClient |
| 135 | void onStreamChange(aaudio_handle_t handle, int32_t opcode, int32_t value) { |
| 136 | android::sp<AAudioBinderClient> client = mBinderClient.promote(); |
Phil Burk | 2bc7c18 | 2017-08-28 11:45:01 -0700 | [diff] [blame] | 137 | if (client.get() != nullptr) { |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 138 | client->onStreamChange(handle, opcode, value); |
| 139 | } |
| 140 | } |
| 141 | private: |
| 142 | android::wp<AAudioBinderClient> mBinderClient; |
| 143 | }; |
| 144 | |
Phil Burk | 2bc7c18 | 2017-08-28 11:45:01 -0700 | [diff] [blame] | 145 | private: |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 146 | |
Phil Burk | 2bc7c18 | 2017-08-28 11:45:01 -0700 | [diff] [blame] | 147 | android::Mutex mServiceLock; |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 148 | android::sp<android::IAAudioService> mAAudioService; |
Phil Burk | 2bc7c18 | 2017-08-28 11:45:01 -0700 | [diff] [blame] | 149 | android::sp<AAudioClient> mAAudioClient; |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 150 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 151 | }; |
| 152 | |
| 153 | |
| 154 | } /* namespace aaudio */ |
| 155 | |
Phil Burk | 5204d31 | 2017-05-04 17:16:13 -0700 | [diff] [blame] | 156 | #endif //ANDROID_AAUDIO_AAUDIO_BINDER_CLIENT_H |