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     aaudio_result_t closeStream(aaudio::aaudio_handle_t streamHandle) override;
59 
60     aaudio_result_t getStreamDescription(
61                 aaudio::aaudio_handle_t streamHandle,
62                 aaudio::AudioEndpointParcelable &parcelable) override;
63 
64     aaudio_result_t startStream(aaudio::aaudio_handle_t streamHandle) override;
65 
66     aaudio_result_t pauseStream(aaudio::aaudio_handle_t streamHandle) override;
67 
68     aaudio_result_t stopStream(aaudio::aaudio_handle_t streamHandle) override;
69 
70     aaudio_result_t flushStream(aaudio::aaudio_handle_t streamHandle) override;
71 
72     aaudio_result_t registerAudioThread(aaudio::aaudio_handle_t streamHandle,
73                                                 pid_t tid,
74                                                 int64_t periodNanoseconds) override;
75 
76     aaudio_result_t unregisterAudioThread(aaudio::aaudio_handle_t streamHandle,
77                                                   pid_t tid) override;
78 
79     aaudio_result_t startClient(aaudio::aaudio_handle_t streamHandle,
80                                       const android::AudioClient& client,
81                                       audio_port_handle_t *clientHandle) override;
82 
83     aaudio_result_t stopClient(aaudio::aaudio_handle_t streamHandle,
84                                        audio_port_handle_t clientHandle) override;
85 
86     aaudio_result_t disconnectStreamByPortHandle(audio_port_handle_t portHandle);
87 
88 private:
89 
90     /**
91      * Lookup stream and then validate access to the stream.
92      * @param streamHandle
93      * @return
94      */
95     sp<aaudio::AAudioServiceStreamBase> convertHandleToServiceStream(
96             aaudio::aaudio_handle_t streamHandle);
97 
98 
99 
100     bool releaseStream(const sp<aaudio::AAudioServiceStreamBase> &serviceStream);
101 
102     aaudio_result_t checkForPendingClose(const sp<aaudio::AAudioServiceStreamBase> &serviceStream,
103                                          aaudio_result_t defaultResult);
104 
105     android::AudioClient            mAudioClient;
106 
107     aaudio::AAudioStreamTracker     mStreamTracker;
108 
109     enum constants {
110         DEFAULT_AUDIO_PRIORITY = 2
111     };
112 };
113 
114 } /* namespace android */
115 
116 #endif //AAUDIO_AAUDIO_SERVICE_H
117