blob: df66f1b9006948e77ff2c04c06033f8f77a96b40 [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"
39
Phil Burk5ed503c2017-02-01 09:38:15 -080040class AAudioService :
41 public BinderService<AAudioService>,
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -070042 public aaudio::BnAAudioService
Phil Burk5ed503c2017-02-01 09:38:15 -080043{
44 friend class BinderService<AAudioService>;
45
46public:
47 AAudioService();
jiabin613e6ae2022-12-21 20:20:11 +000048 ~AAudioService() override = default;
Ytai Ben-Tsvi734e3502020-08-24 14:57:36 -070049
50 aaudio::AAudioServiceInterface& asAAudioServiceInterface() {
51 return mAdapter;
52 }
Phil Burk5ed503c2017-02-01 09:38:15 -080053
Phil Burkc0c70e32017-02-09 13:18:38 -080054 static const char* getServiceName() { return AAUDIO_SERVICE_NAME; }
Phil Burk5ed503c2017-02-01 09:38:15 -080055
jiabin613e6ae2022-12-21 20:20:11 +000056 status_t dump(int fd, const Vector<String16>& args) override;
Andy Hung47c5e532017-06-26 18:28:00 -070057
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -070058 binder::Status registerClient(const ::android::sp<::aaudio::IAAudioClient>& client) override;
Phil Burk11e8d332017-05-24 09:59:02 -070059
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -070060 binder::Status openStream(const ::aaudio::StreamRequest& request,
61 ::aaudio::StreamParameters* paramsOut,
62 int32_t* _aidl_return) override;
Phil Burk5ed503c2017-02-01 09:38:15 -080063
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -070064 binder::Status closeStream(int32_t streamHandle, int32_t* _aidl_return) override;
Phil Burk5ed503c2017-02-01 09:38:15 -080065
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -070066 binder::Status
67 getStreamDescription(int32_t streamHandle, ::aaudio::Endpoint* endpoint,
68 int32_t* _aidl_return) override;
Phil Burk5ed503c2017-02-01 09:38:15 -080069
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -070070 binder::Status startStream(int32_t streamHandle, int32_t* _aidl_return) override;
Phil Burk5ed503c2017-02-01 09:38:15 -080071
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -070072 binder::Status pauseStream(int32_t streamHandle, int32_t* _aidl_return) override;
Phil Burk5ed503c2017-02-01 09:38:15 -080073
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -070074 binder::Status stopStream(int32_t streamHandle, int32_t* _aidl_return) override;
Phil Burk71f35bb2017-04-13 16:05:07 -070075
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -070076 binder::Status flushStream(int32_t streamHandle, int32_t* _aidl_return) override;
Phil Burk5ed503c2017-02-01 09:38:15 -080077
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -070078 binder::Status
79 registerAudioThread(int32_t streamHandle, int32_t clientThreadId, int64_t periodNanoseconds,
80 int32_t* _aidl_return) override;
Phil Burk5ed503c2017-02-01 09:38:15 -080081
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -070082 binder::Status unregisterAudioThread(int32_t streamHandle, int32_t clientThreadId,
83 int32_t* _aidl_return) override;
Phil Burk5ed503c2017-02-01 09:38:15 -080084
jiabinf7f06152021-11-22 18:10:14 +000085 binder::Status exitStandby(int32_t streamHandle, ::aaudio::Endpoint* endpoint,
86 int32_t* _aidl_return) override;
87
Phil Burk523b3042017-09-13 13:03:08 -070088 aaudio_result_t startClient(aaudio::aaudio_handle_t streamHandle,
jiabind1f1cb62020-03-24 11:57:57 -070089 const android::AudioClient& client,
90 const audio_attributes_t *attr,
Ytai Ben-Tsvi734e3502020-08-24 14:57:36 -070091 audio_port_handle_t *clientHandle);
Eric Laurenta54f1282017-07-01 19:39:32 -070092
Phil Burk523b3042017-09-13 13:03:08 -070093 aaudio_result_t stopClient(aaudio::aaudio_handle_t streamHandle,
Ytai Ben-Tsvi734e3502020-08-24 14:57:36 -070094 audio_port_handle_t clientHandle);
Eric Laurenta54f1282017-07-01 19:39:32 -070095
Phil Burk6e463ce2020-04-13 10:20:20 -070096 // ===============================================================================
97 // The following public methods are only called from the service and NOT by Binder.
98 // ===============================================================================
99
Phil Burkbbd52862018-04-13 11:37:42 -0700100 aaudio_result_t disconnectStreamByPortHandle(audio_port_handle_t portHandle);
101
Phil Burk6e463ce2020-04-13 10:20:20 -0700102 /*
103 * This is only called from within the Service.
104 * It bypasses the permission checks in closeStream(handle).
105 */
jiabin613e6ae2022-12-21 20:20:11 +0000106 aaudio_result_t closeStream(const sp<aaudio::AAudioServiceStreamBase>& serviceStream);
Phil Burk6e463ce2020-04-13 10:20:20 -0700107
Phil Burk5ed503c2017-02-01 09:38:15 -0800108private:
Ytai Ben-Tsvi734e3502020-08-24 14:57:36 -0700109 class Adapter : public aaudio::AAudioBinderAdapter {
110 public:
111 explicit Adapter(AAudioService *service)
112 : aaudio::AAudioBinderAdapter(service),
113 mService(service) {}
114
115 aaudio_result_t startClient(aaudio::aaudio_handle_t streamHandle,
116 const android::AudioClient &client,
117 const audio_attributes_t *attr,
118 audio_port_handle_t *clientHandle) override {
119 return mService->startClient(streamHandle, client, attr, clientHandle);
120 }
121
122 aaudio_result_t stopClient(aaudio::aaudio_handle_t streamHandle,
123 audio_port_handle_t clientHandle) override {
124 return mService->stopClient(streamHandle, clientHandle);
125 }
126
127 private:
128 AAudioService* const mService;
129 };
130
131 Adapter mAdapter;
Phil Burk5ed503c2017-02-01 09:38:15 -0800132
Phil Burk2ebf6622019-04-17 11:10:25 -0700133 /** @return true if the client is the audioserver
134 */
135 bool isCallerInService();
136
Phil Burk523b3042017-09-13 13:03:08 -0700137 /**
138 * Lookup stream and then validate access to the stream.
139 * @param streamHandle
140 * @return
141 */
142 sp<aaudio::AAudioServiceStreamBase> convertHandleToServiceStream(
143 aaudio::aaudio_handle_t streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800144
Phil Burk94862522017-09-13 21:31:36 -0700145 android::AudioClient mAudioClient;
146
147 aaudio::AAudioStreamTracker mStreamTracker;
Phil Burk523b3042017-09-13 13:03:08 -0700148
Phil Burk6e463ce2020-04-13 10:20:20 -0700149 // We use a lock to prevent thread A from reopening an exclusive stream
150 // after thread B steals thread A's exclusive MMAP resource stream.
151 std::recursive_mutex mOpenLock;
152
Phil Burk2ebf6622019-04-17 11:10:25 -0700153 // TODO Extract the priority constants from services/audioflinger/Threads.cpp
154 // and share them with this code. Look for "kPriorityFastMixer".
155 static constexpr int32_t kRealTimeAudioPriorityClient = 2;
156 static constexpr int32_t kRealTimeAudioPriorityService = 3;
Phil Burka9876702020-04-20 18:16:15 -0700157
Phil Burk5ed503c2017-02-01 09:38:15 -0800158};
159
160} /* namespace android */
161
Phil Burk3316d5e2017-02-15 11:23:01 -0800162#endif //AAUDIO_AAUDIO_SERVICE_H