• 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 
40 class AAudioService :
41     public BinderService<AAudioService>,
42     public aaudio::BnAAudioService
43 {
44     friend class BinderService<AAudioService>;
45 
46 public:
47     AAudioService();
48     virtual ~AAudioService() = default;
49 
asAAudioServiceInterface()50     aaudio::AAudioServiceInterface& asAAudioServiceInterface() {
51         return mAdapter;
52     }
53 
getServiceName()54     static const char* getServiceName() { return AAUDIO_SERVICE_NAME; }
55 
56     virtual status_t        dump(int fd, const Vector<String16>& args) override;
57 
58     binder::Status registerClient(const ::android::sp<::aaudio::IAAudioClient>& client) override;
59 
60     binder::Status openStream(const ::aaudio::StreamRequest& request,
61                               ::aaudio::StreamParameters* paramsOut,
62                               int32_t* _aidl_return) override;
63 
64     binder::Status closeStream(int32_t streamHandle, int32_t* _aidl_return) override;
65 
66     binder::Status
67     getStreamDescription(int32_t streamHandle, ::aaudio::Endpoint* endpoint,
68                          int32_t* _aidl_return) override;
69 
70     binder::Status startStream(int32_t streamHandle, int32_t* _aidl_return) override;
71 
72     binder::Status pauseStream(int32_t streamHandle, int32_t* _aidl_return) override;
73 
74     binder::Status stopStream(int32_t streamHandle, int32_t* _aidl_return) override;
75 
76     binder::Status flushStream(int32_t streamHandle, int32_t* _aidl_return) override;
77 
78     binder::Status
79     registerAudioThread(int32_t streamHandle, int32_t clientThreadId, int64_t periodNanoseconds,
80                         int32_t* _aidl_return) override;
81 
82     binder::Status unregisterAudioThread(int32_t streamHandle, int32_t clientThreadId,
83                                          int32_t* _aidl_return) override;
84 
85     binder::Status exitStandby(int32_t streamHandle, ::aaudio::Endpoint* endpoint,
86                                int32_t* _aidl_return) override;
87 
88     aaudio_result_t startClient(aaudio::aaudio_handle_t streamHandle,
89                                 const android::AudioClient& client,
90                                 const audio_attributes_t *attr,
91                                 audio_port_handle_t *clientHandle);
92 
93     aaudio_result_t stopClient(aaudio::aaudio_handle_t streamHandle,
94                                        audio_port_handle_t clientHandle);
95 
96  // ===============================================================================
97  // The following public methods are only called from the service and NOT by Binder.
98  // ===============================================================================
99 
100     aaudio_result_t disconnectStreamByPortHandle(audio_port_handle_t portHandle);
101 
102     /*
103      * This is only called from within the Service.
104      * It bypasses the permission checks in closeStream(handle).
105      */
106     aaudio_result_t closeStream(sp<aaudio::AAudioServiceStreamBase> serviceStream);
107 
108 private:
109     class Adapter : public aaudio::AAudioBinderAdapter {
110     public:
Adapter(AAudioService * service)111         explicit Adapter(AAudioService *service)
112                 : aaudio::AAudioBinderAdapter(service),
113                   mService(service) {}
114 
startClient(aaudio::aaudio_handle_t streamHandle,const android::AudioClient & client,const audio_attributes_t * attr,audio_port_handle_t * clientHandle)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 
stopClient(aaudio::aaudio_handle_t streamHandle,audio_port_handle_t clientHandle)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;
132 
133     /** @return true if the client is the audioserver
134      */
135     bool isCallerInService();
136 
137     /**
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);
144 
145     android::AudioClient            mAudioClient;
146 
147     aaudio::AAudioStreamTracker     mStreamTracker;
148 
149     // 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 
153     // 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;
157 
158 };
159 
160 } /* namespace android */
161 
162 #endif //AAUDIO_AAUDIO_SERVICE_H
163