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 
28 #include "binding/AAudioCommon.h"
29 #include "binding/AAudioServiceInterface.h"
30 #include "binding/IAAudioService.h"
31 
32 #include "AAudioServiceStreamBase.h"
33 #include "AAudioStreamTracker.h"
34 
35 namespace android {
36 
37 class AAudioService :
38     public BinderService<AAudioService>,
39     public BnAAudioService,
40     public aaudio::AAudioServiceInterface
41 {
42     friend class BinderService<AAudioService>;
43 
44 public:
45     AAudioService();
46     virtual ~AAudioService();
47 
getServiceName()48     static const char* getServiceName() { return AAUDIO_SERVICE_NAME; }
49 
50     virtual status_t        dump(int fd, const Vector<String16>& args) override;
51 
52     virtual void            registerClient(const sp<IAAudioClient>& client);
53 
54     aaudio::aaudio_handle_t openStream(const aaudio::AAudioStreamRequest &request,
55                                        aaudio::AAudioStreamConfiguration &configurationOutput)
56                                        override;
57 
58     /*
59      * This is called from Binder. It checks for permissions
60      * and converts the handle passed through Binder to a stream pointer.
61      */
62     aaudio_result_t closeStream(aaudio::aaudio_handle_t streamHandle) override;
63 
64     aaudio_result_t getStreamDescription(
65                 aaudio::aaudio_handle_t streamHandle,
66                 aaudio::AudioEndpointParcelable &parcelable) override;
67 
68     aaudio_result_t startStream(aaudio::aaudio_handle_t streamHandle) override;
69 
70     aaudio_result_t pauseStream(aaudio::aaudio_handle_t streamHandle) override;
71 
72     aaudio_result_t stopStream(aaudio::aaudio_handle_t streamHandle) override;
73 
74     aaudio_result_t flushStream(aaudio::aaudio_handle_t streamHandle) override;
75 
76     aaudio_result_t registerAudioThread(aaudio::aaudio_handle_t streamHandle,
77                                                 pid_t tid,
78                                                 int64_t periodNanoseconds) override;
79 
80     aaudio_result_t unregisterAudioThread(aaudio::aaudio_handle_t streamHandle,
81                                                   pid_t tid) override;
82 
83     aaudio_result_t startClient(aaudio::aaudio_handle_t streamHandle,
84                                 const android::AudioClient& client,
85                                 const audio_attributes_t *attr,
86                                 audio_port_handle_t *clientHandle) override;
87 
88     aaudio_result_t stopClient(aaudio::aaudio_handle_t streamHandle,
89                                        audio_port_handle_t clientHandle) override;
90 
91  // ===============================================================================
92  // The following public methods are only called from the service and NOT by Binder.
93  // ===============================================================================
94 
95     aaudio_result_t disconnectStreamByPortHandle(audio_port_handle_t portHandle);
96 
97     /*
98      * This is only called from within the Service.
99      * It bypasses the permission checks in closeStream(handle).
100      */
101     aaudio_result_t closeStream(sp<aaudio::AAudioServiceStreamBase> serviceStream);
102 
103 private:
104 
105     /** @return true if the client is the audioserver
106      */
107     bool isCallerInService();
108 
109     /**
110      * Lookup stream and then validate access to the stream.
111      * @param streamHandle
112      * @return
113      */
114     sp<aaudio::AAudioServiceStreamBase> convertHandleToServiceStream(
115             aaudio::aaudio_handle_t streamHandle);
116 
117     android::AudioClient            mAudioClient;
118 
119     aaudio::AAudioStreamTracker     mStreamTracker;
120 
121     // We use a lock to prevent thread A from reopening an exclusive stream
122     // after thread B steals thread A's exclusive MMAP resource stream.
123     std::recursive_mutex            mOpenLock;
124 
125     // TODO  Extract the priority constants from services/audioflinger/Threads.cpp
126     // and share them with this code. Look for "kPriorityFastMixer".
127     static constexpr int32_t        kRealTimeAudioPriorityClient = 2;
128     static constexpr int32_t        kRealTimeAudioPriorityService = 3;
129 
130 };
131 
132 } /* namespace android */
133 
134 #endif //AAUDIO_AAUDIO_SERVICE_H
135