blob: ada3d5342c254f642c8924ae0be8d2b18b9c19d2 [file] [log] [blame]
Phil Burk5ed503c2017-02-01 09:38:15 -08001/*
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 Burk3316d5e2017-02-15 11:23:01 -080017#ifndef AAUDIO_AAUDIO_SERVICE_H
18#define AAUDIO_AAUDIO_SERVICE_H
Phil Burk5ed503c2017-02-01 09:38:15 -080019
20#include <time.h>
21#include <pthread.h>
22
23#include <binder/BinderService.h>
Eric Laurenta54f1282017-07-01 19:39:32 -070024#include <media/AudioClient.h>
Phil Burk5ed503c2017-02-01 09:38:15 -080025
Phil Burk5ed503c2017-02-01 09:38:15 -080026#include <aaudio/AAudio.h>
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -070027#include <aaudio/BnAAudioService.h>
Phil Burkc0c70e32017-02-09 13:18:38 -080028
Phil Burk523b3042017-09-13 13:03:08 -070029#include "binding/AAudioCommon.h"
Ytai Ben-Tsvi734e3502020-08-24 14:57:36 -070030#include "binding/AAudioBinderAdapter.h"
Phil Burk523b3042017-09-13 13:03:08 -070031#include "binding/AAudioServiceInterface.h"
Phil Burk523b3042017-09-13 13:03:08 -070032
33#include "AAudioServiceStreamBase.h"
34#include "AAudioStreamTracker.h"
Phil Burk5ed503c2017-02-01 09:38:15 -080035
36namespace android {
37
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -070038#define AAUDIO_SERVICE_NAME "media.aaudio"
jiabin5f787812023-03-02 20:42:43 +000039#define DEFAULT_AAUDIO_SERVICE_ID 0
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -070040
Phil Burk5ed503c2017-02-01 09:38:15 -080041class AAudioService :
42 public BinderService<AAudioService>,
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -070043 public aaudio::BnAAudioService
Phil Burk5ed503c2017-02-01 09:38:15 -080044{
45 friend class BinderService<AAudioService>;
46
47public:
48 AAudioService();
jiabin613e6ae2022-12-21 20:20:11 +000049 ~AAudioService() override = default;
Ytai Ben-Tsvi734e3502020-08-24 14:57:36 -070050
51 aaudio::AAudioServiceInterface& asAAudioServiceInterface() {
52 return mAdapter;
53 }
Phil Burk5ed503c2017-02-01 09:38:15 -080054
Phil Burkc0c70e32017-02-09 13:18:38 -080055 static const char* getServiceName() { return AAUDIO_SERVICE_NAME; }
Phil Burk5ed503c2017-02-01 09:38:15 -080056
jiabin613e6ae2022-12-21 20:20:11 +000057 status_t dump(int fd, const Vector<String16>& args) override;
Andy Hung47c5e532017-06-26 18:28:00 -070058
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -070059 binder::Status registerClient(const ::android::sp<::aaudio::IAAudioClient>& client) override;
Phil Burk11e8d332017-05-24 09:59:02 -070060
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -070061 binder::Status openStream(const ::aaudio::StreamRequest& request,
62 ::aaudio::StreamParameters* paramsOut,
63 int32_t* _aidl_return) override;
Phil Burk5ed503c2017-02-01 09:38:15 -080064
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -070065 binder::Status closeStream(int32_t streamHandle, int32_t* _aidl_return) override;
Phil Burk5ed503c2017-02-01 09:38:15 -080066
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -070067 binder::Status
68 getStreamDescription(int32_t streamHandle, ::aaudio::Endpoint* endpoint,
69 int32_t* _aidl_return) override;
Phil Burk5ed503c2017-02-01 09:38:15 -080070
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -070071 binder::Status startStream(int32_t streamHandle, int32_t* _aidl_return) override;
Phil Burk5ed503c2017-02-01 09:38:15 -080072
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -070073 binder::Status pauseStream(int32_t streamHandle, int32_t* _aidl_return) override;
Phil Burk5ed503c2017-02-01 09:38:15 -080074
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -070075 binder::Status stopStream(int32_t streamHandle, int32_t* _aidl_return) override;
Phil Burk71f35bb2017-04-13 16:05:07 -070076
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -070077 binder::Status flushStream(int32_t streamHandle, int32_t* _aidl_return) override;
Phil Burk5ed503c2017-02-01 09:38:15 -080078
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -070079 binder::Status
80 registerAudioThread(int32_t streamHandle, int32_t clientThreadId, int64_t periodNanoseconds,
81 int32_t* _aidl_return) override;
Phil Burk5ed503c2017-02-01 09:38:15 -080082
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -070083 binder::Status unregisterAudioThread(int32_t streamHandle, int32_t clientThreadId,
84 int32_t* _aidl_return) override;
Phil Burk5ed503c2017-02-01 09:38:15 -080085
jiabinf7f06152021-11-22 18:10:14 +000086 binder::Status exitStandby(int32_t streamHandle, ::aaudio::Endpoint* endpoint,
87 int32_t* _aidl_return) override;
88
Phil Burk523b3042017-09-13 13:03:08 -070089 aaudio_result_t startClient(aaudio::aaudio_handle_t streamHandle,
jiabind1f1cb62020-03-24 11:57:57 -070090 const android::AudioClient& client,
91 const audio_attributes_t *attr,
Ytai Ben-Tsvi734e3502020-08-24 14:57:36 -070092 audio_port_handle_t *clientHandle);
Eric Laurenta54f1282017-07-01 19:39:32 -070093
Phil Burk523b3042017-09-13 13:03:08 -070094 aaudio_result_t stopClient(aaudio::aaudio_handle_t streamHandle,
Ytai Ben-Tsvi734e3502020-08-24 14:57:36 -070095 audio_port_handle_t clientHandle);
Eric Laurenta54f1282017-07-01 19:39:32 -070096
Phil Burk6e463ce2020-04-13 10:20:20 -070097 // ===============================================================================
98 // The following public methods are only called from the service and NOT by Binder.
99 // ===============================================================================
100
Phil Burkbbd52862018-04-13 11:37:42 -0700101 aaudio_result_t disconnectStreamByPortHandle(audio_port_handle_t portHandle);
102
Phil Burk6e463ce2020-04-13 10:20:20 -0700103 /*
104 * This is only called from within the Service.
105 * It bypasses the permission checks in closeStream(handle).
106 */
jiabin613e6ae2022-12-21 20:20:11 +0000107 aaudio_result_t closeStream(const sp<aaudio::AAudioServiceStreamBase>& serviceStream);
Phil Burk6e463ce2020-04-13 10:20:20 -0700108
Phil Burk5ed503c2017-02-01 09:38:15 -0800109private:
Ytai Ben-Tsvi734e3502020-08-24 14:57:36 -0700110 class Adapter : public aaudio::AAudioBinderAdapter {
111 public:
jiabin5f787812023-03-02 20:42:43 +0000112 // Always use default service id in server side since when crash happens,
113 // the aaudio service will restart.
Ytai Ben-Tsvi734e3502020-08-24 14:57:36 -0700114 explicit Adapter(AAudioService *service)
jiabin5f787812023-03-02 20:42:43 +0000115 : aaudio::AAudioBinderAdapter(service, DEFAULT_AAUDIO_SERVICE_ID),
Ytai Ben-Tsvi734e3502020-08-24 14:57:36 -0700116 mService(service) {}
117
jiabin5f787812023-03-02 20:42:43 +0000118 aaudio_result_t startClient(const aaudio::AAudioHandleInfo& streamHandleInfo,
Ytai Ben-Tsvi734e3502020-08-24 14:57:36 -0700119 const android::AudioClient &client,
120 const audio_attributes_t *attr,
121 audio_port_handle_t *clientHandle) override {
jiabin5f787812023-03-02 20:42:43 +0000122 return mService->startClient(streamHandleInfo.getHandle(), client, attr, clientHandle);
Ytai Ben-Tsvi734e3502020-08-24 14:57:36 -0700123 }
124
jiabin5f787812023-03-02 20:42:43 +0000125 aaudio_result_t stopClient(const aaudio::AAudioHandleInfo& streamHandleInfo,
Ytai Ben-Tsvi734e3502020-08-24 14:57:36 -0700126 audio_port_handle_t clientHandle) override {
jiabin5f787812023-03-02 20:42:43 +0000127 return mService->stopClient(streamHandleInfo.getHandle(), clientHandle);
Ytai Ben-Tsvi734e3502020-08-24 14:57:36 -0700128 }
129
130 private:
131 AAudioService* const mService;
132 };
133
134 Adapter mAdapter;
Phil Burk5ed503c2017-02-01 09:38:15 -0800135
Phil Burk2ebf6622019-04-17 11:10:25 -0700136 /** @return true if the client is the audioserver
137 */
138 bool isCallerInService();
139
Phil Burk523b3042017-09-13 13:03:08 -0700140 /**
141 * Lookup stream and then validate access to the stream.
142 * @param streamHandle
143 * @return
144 */
145 sp<aaudio::AAudioServiceStreamBase> convertHandleToServiceStream(
146 aaudio::aaudio_handle_t streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800147
Phil Burk94862522017-09-13 21:31:36 -0700148 android::AudioClient mAudioClient;
149
150 aaudio::AAudioStreamTracker mStreamTracker;
Phil Burk523b3042017-09-13 13:03:08 -0700151
Phil Burk6e463ce2020-04-13 10:20:20 -0700152 // We use a lock to prevent thread A from reopening an exclusive stream
153 // after thread B steals thread A's exclusive MMAP resource stream.
154 std::recursive_mutex mOpenLock;
155
Phil Burk2ebf6622019-04-17 11:10:25 -0700156 // TODO Extract the priority constants from services/audioflinger/Threads.cpp
157 // and share them with this code. Look for "kPriorityFastMixer".
158 static constexpr int32_t kRealTimeAudioPriorityClient = 2;
159 static constexpr int32_t kRealTimeAudioPriorityService = 3;
Phil Burka9876702020-04-20 18:16:15 -0700160
Phil Burk5ed503c2017-02-01 09:38:15 -0800161};
162
163} /* namespace android */
164
Phil Burk3316d5e2017-02-15 11:23:01 -0800165#endif //AAUDIO_AAUDIO_SERVICE_H