• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 #ifndef AAUDIO_AAUDIO_SERVICE_H
18 #define AAUDIO_AAUDIO_SERVICE_H
19 
20 #include <time.h>
21 #include <pthread.h>
22 
23 #include <binder/BinderService.h>
24 #include <media/AudioClient.h>
25 
26 #include <aaudio/AAudio.h>
27 #include <aaudio/BnAAudioService.h>
28 
29 #include "binding/AAudioCommon.h"
30 #include "binding/AAudioBinderAdapter.h"
31 #include "binding/AAudioServiceInterface.h"
32 
33 #include "AAudioServiceStreamBase.h"
34 #include "AAudioStreamTracker.h"
35 
36 namespace android {
37 
38 #define AAUDIO_SERVICE_NAME  "media.aaudio"
39 #define DEFAULT_AAUDIO_SERVICE_ID 0
40 
41 class AAudioService :
42     public BinderService<AAudioService>,
43     public aaudio::BnAAudioService
44 {
45     friend class BinderService<AAudioService>;
46 
47 public:
48     AAudioService();
49     ~AAudioService() override = default;
50 
asAAudioServiceInterface()51     aaudio::AAudioServiceInterface& asAAudioServiceInterface() {
52         return mAdapter;
53     }
54 
getServiceName()55     static const char* getServiceName() { return AAUDIO_SERVICE_NAME; }
56 
57     status_t dump(int fd, const Vector<String16>& args) override;
58 
59     binder::Status registerClient(const ::android::sp<::aaudio::IAAudioClient>& client) override;
60 
61     binder::Status openStream(const ::aaudio::StreamRequest& request,
62                               ::aaudio::StreamParameters* paramsOut,
63                               int32_t* _aidl_return) override;
64 
65     binder::Status closeStream(int32_t streamHandle, int32_t* _aidl_return) override;
66 
67     binder::Status
68     getStreamDescription(int32_t streamHandle, ::aaudio::Endpoint* endpoint,
69                          int32_t* _aidl_return) override;
70 
71     binder::Status startStream(int32_t streamHandle, int32_t* _aidl_return) override;
72 
73     binder::Status pauseStream(int32_t streamHandle, int32_t* _aidl_return) override;
74 
75     binder::Status stopStream(int32_t streamHandle, int32_t* _aidl_return) override;
76 
77     binder::Status flushStream(int32_t streamHandle, int32_t* _aidl_return) override;
78 
79     binder::Status
80     registerAudioThread(int32_t streamHandle, int32_t clientThreadId, int64_t periodNanoseconds,
81                         int32_t* _aidl_return) override;
82 
83     binder::Status unregisterAudioThread(int32_t streamHandle, int32_t clientThreadId,
84                                          int32_t* _aidl_return) override;
85 
86     binder::Status exitStandby(int32_t streamHandle, ::aaudio::Endpoint* endpoint,
87                                int32_t* _aidl_return) override;
88 
89     aaudio_result_t startClient(aaudio::aaudio_handle_t streamHandle,
90                                 const android::AudioClient& client,
91                                 const audio_attributes_t *attr,
92                                 audio_port_handle_t *clientHandle);
93 
94     aaudio_result_t stopClient(aaudio::aaudio_handle_t streamHandle,
95                                        audio_port_handle_t clientHandle);
96 
97  // ===============================================================================
98  // The following public methods are only called from the service and NOT by Binder.
99  // ===============================================================================
100 
101     aaudio_result_t disconnectStreamByPortHandle(audio_port_handle_t portHandle);
102 
103     /*
104      * This is only called from within the Service.
105      * It bypasses the permission checks in closeStream(handle).
106      */
107     aaudio_result_t closeStream(const sp<aaudio::AAudioServiceStreamBase>& serviceStream);
108 
109 private:
110     class Adapter : public aaudio::AAudioBinderAdapter {
111     public:
112         // Always use default service id in server side since when crash happens,
113         // the aaudio service will restart.
Adapter(AAudioService * service)114         explicit Adapter(AAudioService *service)
115                 : aaudio::AAudioBinderAdapter(service, DEFAULT_AAUDIO_SERVICE_ID),
116                   mService(service) {}
117 
startClient(const aaudio::AAudioHandleInfo & streamHandleInfo,const android::AudioClient & client,const audio_attributes_t * attr,audio_port_handle_t * clientHandle)118         aaudio_result_t startClient(const aaudio::AAudioHandleInfo& streamHandleInfo,
119                                     const android::AudioClient &client,
120                                     const audio_attributes_t *attr,
121                                     audio_port_handle_t *clientHandle) override {
122             return mService->startClient(streamHandleInfo.getHandle(), client, attr, clientHandle);
123         }
124 
stopClient(const aaudio::AAudioHandleInfo & streamHandleInfo,audio_port_handle_t clientHandle)125         aaudio_result_t stopClient(const aaudio::AAudioHandleInfo& streamHandleInfo,
126                                    audio_port_handle_t clientHandle) override {
127             return mService->stopClient(streamHandleInfo.getHandle(), clientHandle);
128         }
129 
130     private:
131         AAudioService* const mService;
132     };
133 
134     Adapter mAdapter;
135 
136     /** @return true if the client is the audioserver
137      */
138     bool isCallerInService();
139 
140     /**
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);
147 
148     android::AudioClient            mAudioClient;
149 
150     aaudio::AAudioStreamTracker     mStreamTracker;
151 
152     // 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 
156     // 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;
160 
161 };
162 
163 } /* namespace android */
164 
165 #endif //AAUDIO_AAUDIO_SERVICE_H
166