blob: 469f0a850115549ffd4be9c1b8b9e1faa765e518 [file] [log] [blame]
Phil Burkc0c70e32017-02-09 13:18:38 -08001/*
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
Phil Burk5204d312017-05-04 17:16:13 -070017#ifndef ANDROID_AAUDIO_AAUDIO_BINDER_CLIENT_H
18#define ANDROID_AAUDIO_AAUDIO_BINDER_CLIENT_H
Phil Burkc0c70e32017-02-09 13:18:38 -080019
Phil Burk11e8d332017-05-24 09:59:02 -070020#include <utils/RefBase.h>
Phil Burk9b3f8ef2017-05-16 11:37:43 -070021#include <utils/Singleton.h>
22
Phil Burka4eb0d82017-04-12 15:44:06 -070023#include <aaudio/AAudio.h>
Phil Burkc0c70e32017-02-09 13:18:38 -080024#include "AAudioServiceDefinitions.h"
25#include "AAudioServiceInterface.h"
26#include "binding/AAudioStreamRequest.h"
27#include "binding/AAudioStreamConfiguration.h"
28#include "binding/AudioEndpointParcelable.h"
Phil Burk11e8d332017-05-24 09:59:02 -070029#include "binding/IAAudioService.h"
Phil Burkc0c70e32017-02-09 13:18:38 -080030
31/**
Phil Burk11e8d332017-05-24 09:59:02 -070032 * Implements the AAudioServiceInterface by talking to the service through Binder.
Phil Burkc0c70e32017-02-09 13:18:38 -080033 */
34
35namespace aaudio {
36
Phil Burk11e8d332017-05-24 09:59:02 -070037class AAudioBinderClient : public virtual android::RefBase
38 , public AAudioServiceInterface
Phil Burk9b3f8ef2017-05-16 11:37:43 -070039 , public android::Singleton<AAudioBinderClient> {
Phil Burkc0c70e32017-02-09 13:18:38 -080040
41public:
42
43 AAudioBinderClient();
44
45 virtual ~AAudioBinderClient();
46
Phil Burk11e8d332017-05-24 09:59:02 -070047 const android::sp<android::IAAudioService> getAAudioService();
48
49 void dropAAudioService();
50
51 void registerClient(const android::sp<android::IAAudioClient>& client __unused) override {}
52
Phil Burkc0c70e32017-02-09 13:18:38 -080053 /**
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
Phil Burk71f35bb2017-04-13 16:05:07 -070081 aaudio_result_t stopStream(aaudio_handle_t streamHandle) override;
82
Phil Burkc0c70e32017-02-09 13:18:38 -080083 /**
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,
Phil Burkc0c70e32017-02-09 13:18:38 -080094 pid_t clientThreadId,
95 int64_t periodNanoseconds) override;
96
97 aaudio_result_t unregisterAudioThread(aaudio_handle_t streamHandle,
Phil Burkc0c70e32017-02-09 13:18:38 -080098 pid_t clientThreadId) override;
Phil Burk11e8d332017-05-24 09:59:02 -070099
100 void onStreamChange(aaudio_handle_t handle, int32_t opcode, int32_t value) {
101 // TODO This is just a stub so we can have a client Binder to pass to the service.
102 // TODO Implemented in a later CL.
103 ALOGW("onStreamChange called!");
104 }
105
106 class AAudioClient : public android::IBinder::DeathRecipient , public android::BnAAudioClient
107 {
108 public:
109 AAudioClient(android::wp<AAudioBinderClient> aaudioBinderClient)
110 : mBinderClient(aaudioBinderClient) {
111 }
112
113 // implement DeathRecipient
114 virtual void binderDied(const android::wp<android::IBinder>& who __unused) {
115 android::sp<AAudioBinderClient> client = mBinderClient.promote();
116 if (client != 0) {
117 client->dropAAudioService();
118 }
119 ALOGW("AAudio service binderDied()!");
120 }
121
122 // implement BnAAudioClient
123 void onStreamChange(aaudio_handle_t handle, int32_t opcode, int32_t value) {
124 android::sp<AAudioBinderClient> client = mBinderClient.promote();
125 if (client != 0) {
126 client->onStreamChange(handle, opcode, value);
127 }
128 }
129 private:
130 android::wp<AAudioBinderClient> mBinderClient;
131 };
132
133
134 android::Mutex mServiceLock;
135 android::sp<android::IAAudioService> mAAudioService;
136 android::sp<AAudioClient> mAAudioClient;
137
Phil Burkc0c70e32017-02-09 13:18:38 -0800138};
139
140
141} /* namespace aaudio */
142
Phil Burk5204d312017-05-04 17:16:13 -0700143#endif //ANDROID_AAUDIO_AAUDIO_BINDER_CLIENT_H