1 /*
2  * Copyright (C) 2017 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 ANDROID_AAUDIO_AAUDIO_BINDER_CLIENT_H
18 #define ANDROID_AAUDIO_AAUDIO_BINDER_CLIENT_H
19 
20 #include <utils/RefBase.h>
21 #include <utils/Singleton.h>
22 
23 #include <aaudio/AAudio.h>
24 #include "AAudioServiceDefinitions.h"
25 #include "AAudioServiceInterface.h"
26 #include "binding/AAudioStreamRequest.h"
27 #include "binding/AAudioStreamConfiguration.h"
28 #include "binding/AudioEndpointParcelable.h"
29 #include "binding/IAAudioService.h"
30 
31 /**
32  * Implements the AAudioServiceInterface by talking to the service through Binder.
33  */
34 
35 namespace aaudio {
36 
37 class AAudioBinderClient : public virtual android::RefBase
38         , public AAudioServiceInterface
39         , public android::Singleton<AAudioBinderClient> {
40 
41 public:
42 
43     AAudioBinderClient();
44 
45     virtual ~AAudioBinderClient();
46 
47     const android::sp<android::IAAudioService> getAAudioService();
48 
49     void dropAAudioService();
50 
registerClient(const android::sp<android::IAAudioClient> & client __unused)51     void registerClient(const android::sp<android::IAAudioClient>& client __unused) override {}
52 
53     /**
54      * @param request info needed to create the stream
55      * @param configuration contains resulting information about the created stream
56      * @return handle to the stream or a negative error
57      */
58     aaudio_handle_t openStream(const AAudioStreamRequest &request,
59                                AAudioStreamConfiguration &configurationOutput) override;
60 
61     aaudio_result_t closeStream(aaudio_handle_t streamHandle) override;
62 
63     /* Get an immutable description of the in-memory queues
64     * used to communicate with the underlying HAL or Service.
65     */
66     aaudio_result_t getStreamDescription(aaudio_handle_t streamHandle,
67                                                  AudioEndpointParcelable &parcelable) override;
68 
69     /**
70      * Start the flow of data.
71      * This is asynchronous. When complete, the service will send a STARTED event.
72      */
73     aaudio_result_t startStream(aaudio_handle_t streamHandle) override;
74 
75     /**
76      * Stop the flow of data such that start() can resume without loss of data.
77      * This is asynchronous. When complete, the service will send a PAUSED event.
78      */
79     aaudio_result_t pauseStream(aaudio_handle_t streamHandle) override;
80 
81     aaudio_result_t stopStream(aaudio_handle_t streamHandle) override;
82 
83     /**
84      *  Discard any data held by the underlying HAL or Service.
85      * This is asynchronous. When complete, the service will send a FLUSHED event.
86      */
87     aaudio_result_t flushStream(aaudio_handle_t streamHandle) override;
88 
89     /**
90      * Manage the specified thread as a low latency audio thread.
91      * TODO Consider passing this information as part of the startStream() call.
92      */
93     aaudio_result_t registerAudioThread(aaudio_handle_t streamHandle,
94                                                 pid_t clientThreadId,
95                                                 int64_t periodNanoseconds) override;
96 
97     aaudio_result_t unregisterAudioThread(aaudio_handle_t streamHandle,
98                                                   pid_t clientThreadId) override;
99 
startClient(aaudio_handle_t streamHandle __unused,const android::AudioClient & client __unused,const audio_attributes_t * attr __unused,audio_port_handle_t * clientHandle __unused)100     aaudio_result_t startClient(aaudio_handle_t streamHandle __unused,
101                                 const android::AudioClient& client __unused,
102                                 const audio_attributes_t *attr __unused,
103                                 audio_port_handle_t *clientHandle __unused) override {
104         return AAUDIO_ERROR_UNAVAILABLE;
105     }
106 
stopClient(aaudio_handle_t streamHandle __unused,audio_port_handle_t clientHandle __unused)107     aaudio_result_t stopClient(aaudio_handle_t streamHandle __unused,
108                                audio_port_handle_t clientHandle __unused)  override {
109         return AAUDIO_ERROR_UNAVAILABLE;
110     }
111 
onStreamChange(aaudio_handle_t handle,int32_t opcode,int32_t value)112     void onStreamChange(aaudio_handle_t handle, int32_t opcode, int32_t value) {
113         // TODO This is just a stub so we can have a client Binder to pass to the service.
114         // TODO Implemented in a later CL.
115         ALOGW("onStreamChange called!");
116     }
117 
118     class AAudioClient : public android::IBinder::DeathRecipient , public android::BnAAudioClient
119     {
120     public:
AAudioClient(android::wp<AAudioBinderClient> aaudioBinderClient)121         AAudioClient(android::wp<AAudioBinderClient> aaudioBinderClient)
122                 : mBinderClient(aaudioBinderClient) {
123         }
124 
125         // implement DeathRecipient
binderDied(const android::wp<android::IBinder> & who __unused)126         virtual void binderDied(const android::wp<android::IBinder>& who __unused) {
127             android::sp<AAudioBinderClient> client = mBinderClient.promote();
128             if (client.get() != nullptr) {
129                 client->dropAAudioService();
130             }
131             ALOGW("AAudio service binderDied()!");
132         }
133 
134         // implement BnAAudioClient
onStreamChange(aaudio_handle_t handle,int32_t opcode,int32_t value)135         void onStreamChange(aaudio_handle_t handle, int32_t opcode, int32_t value) {
136             android::sp<AAudioBinderClient> client = mBinderClient.promote();
137             if (client.get() != nullptr) {
138                 client->onStreamChange(handle, opcode, value);
139             }
140         }
141     private:
142         android::wp<AAudioBinderClient> mBinderClient;
143     };
144 
145 private:
146 
147     android::Mutex                  mServiceLock;
148     android::sp<android::IAAudioService>  mAAudioService;
149     android::sp<AAudioClient>       mAAudioClient;
150 
151 };
152 
153 
154 } /* namespace aaudio */
155 
156 #endif //ANDROID_AAUDIO_AAUDIO_BINDER_CLIENT_H
157