| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 1 | /* | 
 | 2 |  * Copyright (C) 2016 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 | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 17 | #ifndef AAUDIO_AAUDIO_SERVICE_H | 
 | 18 | #define AAUDIO_AAUDIO_SERVICE_H | 
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 19 |  | 
 | 20 | #include <time.h> | 
 | 21 | #include <pthread.h> | 
 | 22 |  | 
 | 23 | #include <binder/BinderService.h> | 
| Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 24 | #include <media/AudioClient.h> | 
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 25 |  | 
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 26 | #include <aaudio/AAudio.h> | 
| Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 27 |  | 
| Phil Burk | 523b304 | 2017-09-13 13:03:08 -0700 | [diff] [blame] | 28 | #include "binding/AAudioCommon.h" | 
| Ytai Ben-Tsvi | 734e350 | 2020-08-24 14:57:36 -0700 | [diff] [blame^] | 29 | #include "binding/AAudioBinderAdapter.h" | 
| Phil Burk | 523b304 | 2017-09-13 13:03:08 -0700 | [diff] [blame] | 30 | #include "binding/AAudioServiceInterface.h" | 
 | 31 | #include "binding/IAAudioService.h" | 
 | 32 |  | 
 | 33 | #include "AAudioServiceStreamBase.h" | 
 | 34 | #include "AAudioStreamTracker.h" | 
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 35 |  | 
 | 36 | namespace android { | 
 | 37 |  | 
 | 38 | class AAudioService : | 
 | 39 |     public BinderService<AAudioService>, | 
| Ytai Ben-Tsvi | 734e350 | 2020-08-24 14:57:36 -0700 | [diff] [blame^] | 40 |     public BnAAudioService | 
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 41 | { | 
 | 42 |     friend class BinderService<AAudioService>; | 
 | 43 |  | 
 | 44 | public: | 
 | 45 |     AAudioService(); | 
| Ytai Ben-Tsvi | 734e350 | 2020-08-24 14:57:36 -0700 | [diff] [blame^] | 46 |     virtual ~AAudioService() = default; | 
 | 47 |  | 
 | 48 |     aaudio::AAudioServiceInterface& asAAudioServiceInterface() { | 
 | 49 |         return mAdapter; | 
 | 50 |     } | 
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 51 |  | 
| Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 52 |     static const char* getServiceName() { return AAUDIO_SERVICE_NAME; } | 
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 53 |  | 
| Andy Hung | 47c5e53 | 2017-06-26 18:28:00 -0700 | [diff] [blame] | 54 |     virtual status_t        dump(int fd, const Vector<String16>& args) override; | 
 | 55 |  | 
| Ytai Ben-Tsvi | 0412f73 | 2020-08-17 14:43:36 -0700 | [diff] [blame] | 56 |     virtual void            registerClient(const sp<aaudio::IAAudioClient>& client); | 
| Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 57 |  | 
| Phil Burk | 523b304 | 2017-09-13 13:03:08 -0700 | [diff] [blame] | 58 |     aaudio::aaudio_handle_t openStream(const aaudio::AAudioStreamRequest &request, | 
 | 59 |                                        aaudio::AAudioStreamConfiguration &configurationOutput) | 
 | 60 |                                        override; | 
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 61 |  | 
| Phil Burk | 6e463ce | 2020-04-13 10:20:20 -0700 | [diff] [blame] | 62 |     /* | 
 | 63 |      * This is called from Binder. It checks for permissions | 
 | 64 |      * and converts the handle passed through Binder to a stream pointer. | 
 | 65 |      */ | 
| Phil Burk | 523b304 | 2017-09-13 13:03:08 -0700 | [diff] [blame] | 66 |     aaudio_result_t closeStream(aaudio::aaudio_handle_t streamHandle) override; | 
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 67 |  | 
| Phil Burk | 523b304 | 2017-09-13 13:03:08 -0700 | [diff] [blame] | 68 |     aaudio_result_t getStreamDescription( | 
 | 69 |                 aaudio::aaudio_handle_t streamHandle, | 
 | 70 |                 aaudio::AudioEndpointParcelable &parcelable) override; | 
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 71 |  | 
| Phil Burk | 523b304 | 2017-09-13 13:03:08 -0700 | [diff] [blame] | 72 |     aaudio_result_t startStream(aaudio::aaudio_handle_t streamHandle) override; | 
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 73 |  | 
| Phil Burk | 523b304 | 2017-09-13 13:03:08 -0700 | [diff] [blame] | 74 |     aaudio_result_t pauseStream(aaudio::aaudio_handle_t streamHandle) override; | 
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 75 |  | 
| Phil Burk | 523b304 | 2017-09-13 13:03:08 -0700 | [diff] [blame] | 76 |     aaudio_result_t stopStream(aaudio::aaudio_handle_t streamHandle) override; | 
| Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 77 |  | 
| Phil Burk | 523b304 | 2017-09-13 13:03:08 -0700 | [diff] [blame] | 78 |     aaudio_result_t flushStream(aaudio::aaudio_handle_t streamHandle) override; | 
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 79 |  | 
| Phil Burk | 523b304 | 2017-09-13 13:03:08 -0700 | [diff] [blame] | 80 |     aaudio_result_t registerAudioThread(aaudio::aaudio_handle_t streamHandle, | 
| Phil Burk | 2ac035f | 2017-06-23 14:51:14 -0700 | [diff] [blame] | 81 |                                                 pid_t tid, | 
| Phil Burk | 523b304 | 2017-09-13 13:03:08 -0700 | [diff] [blame] | 82 |                                                 int64_t periodNanoseconds) override; | 
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 83 |  | 
| Phil Burk | 523b304 | 2017-09-13 13:03:08 -0700 | [diff] [blame] | 84 |     aaudio_result_t unregisterAudioThread(aaudio::aaudio_handle_t streamHandle, | 
 | 85 |                                                   pid_t tid) override; | 
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 86 |  | 
| Phil Burk | 523b304 | 2017-09-13 13:03:08 -0700 | [diff] [blame] | 87 |     aaudio_result_t startClient(aaudio::aaudio_handle_t streamHandle, | 
| jiabin | d1f1cb6 | 2020-03-24 11:57:57 -0700 | [diff] [blame] | 88 |                                 const android::AudioClient& client, | 
 | 89 |                                 const audio_attributes_t *attr, | 
| Ytai Ben-Tsvi | 734e350 | 2020-08-24 14:57:36 -0700 | [diff] [blame^] | 90 |                                 audio_port_handle_t *clientHandle); | 
| Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 91 |  | 
| Phil Burk | 523b304 | 2017-09-13 13:03:08 -0700 | [diff] [blame] | 92 |     aaudio_result_t stopClient(aaudio::aaudio_handle_t streamHandle, | 
| Ytai Ben-Tsvi | 734e350 | 2020-08-24 14:57:36 -0700 | [diff] [blame^] | 93 |                                        audio_port_handle_t clientHandle); | 
| Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 94 |  | 
| Phil Burk | 6e463ce | 2020-04-13 10:20:20 -0700 | [diff] [blame] | 95 |  // =============================================================================== | 
 | 96 |  // The following public methods are only called from the service and NOT by Binder. | 
 | 97 |  // =============================================================================== | 
 | 98 |  | 
| Phil Burk | bbd5286 | 2018-04-13 11:37:42 -0700 | [diff] [blame] | 99 |     aaudio_result_t disconnectStreamByPortHandle(audio_port_handle_t portHandle); | 
 | 100 |  | 
| Phil Burk | 6e463ce | 2020-04-13 10:20:20 -0700 | [diff] [blame] | 101 |     /* | 
 | 102 |      * This is only called from within the Service. | 
 | 103 |      * It bypasses the permission checks in closeStream(handle). | 
 | 104 |      */ | 
 | 105 |     aaudio_result_t closeStream(sp<aaudio::AAudioServiceStreamBase> serviceStream); | 
 | 106 |  | 
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 107 | private: | 
| Ytai Ben-Tsvi | 734e350 | 2020-08-24 14:57:36 -0700 | [diff] [blame^] | 108 |     class Adapter : public aaudio::AAudioBinderAdapter { | 
 | 109 |     public: | 
 | 110 |         explicit Adapter(AAudioService *service) | 
 | 111 |                 : aaudio::AAudioBinderAdapter(service), | 
 | 112 |                   mService(service) {} | 
 | 113 |  | 
 | 114 |         aaudio_result_t startClient(aaudio::aaudio_handle_t streamHandle, | 
 | 115 |                                     const android::AudioClient &client, | 
 | 116 |                                     const audio_attributes_t *attr, | 
 | 117 |                                     audio_port_handle_t *clientHandle) override { | 
 | 118 |             return mService->startClient(streamHandle, client, attr, clientHandle); | 
 | 119 |         } | 
 | 120 |  | 
 | 121 |         aaudio_result_t stopClient(aaudio::aaudio_handle_t streamHandle, | 
 | 122 |                                    audio_port_handle_t clientHandle) override { | 
 | 123 |             return mService->stopClient(streamHandle, clientHandle); | 
 | 124 |         } | 
 | 125 |  | 
 | 126 |     private: | 
 | 127 |         AAudioService* const mService; | 
 | 128 |     }; | 
 | 129 |  | 
 | 130 |     Adapter mAdapter; | 
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 131 |  | 
| Phil Burk | 2ebf662 | 2019-04-17 11:10:25 -0700 | [diff] [blame] | 132 |     /** @return true if the client is the audioserver | 
 | 133 |      */ | 
 | 134 |     bool isCallerInService(); | 
 | 135 |  | 
| Phil Burk | 523b304 | 2017-09-13 13:03:08 -0700 | [diff] [blame] | 136 |     /** | 
 | 137 |      * Lookup stream and then validate access to the stream. | 
 | 138 |      * @param streamHandle | 
 | 139 |      * @return | 
 | 140 |      */ | 
 | 141 |     sp<aaudio::AAudioServiceStreamBase> convertHandleToServiceStream( | 
 | 142 |             aaudio::aaudio_handle_t streamHandle); | 
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 143 |  | 
| Phil Burk | 9486252 | 2017-09-13 21:31:36 -0700 | [diff] [blame] | 144 |     android::AudioClient            mAudioClient; | 
 | 145 |  | 
 | 146 |     aaudio::AAudioStreamTracker     mStreamTracker; | 
| Phil Burk | 523b304 | 2017-09-13 13:03:08 -0700 | [diff] [blame] | 147 |  | 
| Phil Burk | 6e463ce | 2020-04-13 10:20:20 -0700 | [diff] [blame] | 148 |     // We use a lock to prevent thread A from reopening an exclusive stream | 
 | 149 |     // after thread B steals thread A's exclusive MMAP resource stream. | 
 | 150 |     std::recursive_mutex            mOpenLock; | 
 | 151 |  | 
| Phil Burk | 2ebf662 | 2019-04-17 11:10:25 -0700 | [diff] [blame] | 152 |     // TODO  Extract the priority constants from services/audioflinger/Threads.cpp | 
 | 153 |     // and share them with this code. Look for "kPriorityFastMixer". | 
 | 154 |     static constexpr int32_t        kRealTimeAudioPriorityClient = 2; | 
 | 155 |     static constexpr int32_t        kRealTimeAudioPriorityService = 3; | 
| Phil Burk | a987670 | 2020-04-20 18:16:15 -0700 | [diff] [blame] | 156 |  | 
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 157 | }; | 
 | 158 |  | 
 | 159 | } /* namespace android */ | 
 | 160 |  | 
| Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 161 | #endif //AAUDIO_AAUDIO_SERVICE_H |