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
35namespace aaudio {
36
37class AAudioBinderClient : public virtual android::RefBase
38        , public AAudioServiceInterface
39        , public android::Singleton<AAudioBinderClient> {
40
41public:
42
43    AAudioBinderClient();
44
45    virtual ~AAudioBinderClient();
46
47    const android::sp<android::IAAudioService> getAAudioService();
48
49    void dropAAudioService();
50
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
100    aaudio_result_t startClient(aaudio_handle_t streamHandle __unused,
101                                      const android::AudioClient& client __unused,
102                                      audio_port_handle_t *clientHandle) override {
103        return AAUDIO_ERROR_UNAVAILABLE;
104    }
105
106    aaudio_result_t stopClient(aaudio_handle_t streamHandle __unused,
107                               audio_port_handle_t clientHandle __unused)  override {
108        return AAUDIO_ERROR_UNAVAILABLE;
109    }
110
111    void onStreamChange(aaudio_handle_t handle, int32_t opcode, int32_t value) {
112        // TODO This is just a stub so we can have a client Binder to pass to the service.
113        // TODO Implemented in a later CL.
114        ALOGW("onStreamChange called!");
115    }
116
117    class AAudioClient : public android::IBinder::DeathRecipient , public android::BnAAudioClient
118    {
119    public:
120        AAudioClient(android::wp<AAudioBinderClient> aaudioBinderClient)
121                : mBinderClient(aaudioBinderClient) {
122        }
123
124        // implement DeathRecipient
125        virtual void binderDied(const android::wp<android::IBinder>& who __unused) {
126            android::sp<AAudioBinderClient> client = mBinderClient.promote();
127            if (client.get() != nullptr) {
128                client->dropAAudioService();
129            }
130            ALOGW("AAudio service binderDied()!");
131        }
132
133        // implement BnAAudioClient
134        void onStreamChange(aaudio_handle_t handle, int32_t opcode, int32_t value) {
135            android::sp<AAudioBinderClient> client = mBinderClient.promote();
136            if (client.get() != nullptr) {
137                client->onStreamChange(handle, opcode, value);
138            }
139        }
140    private:
141        android::wp<AAudioBinderClient> mBinderClient;
142    };
143
144private:
145
146    android::Mutex                  mServiceLock;
147    android::sp<android::IAAudioService>  mAAudioService;
148    android::sp<AAudioClient>       mAAudioClient;
149
150};
151
152
153} /* namespace aaudio */
154
155#endif //ANDROID_AAUDIO_AAUDIO_BINDER_CLIENT_H
156