blob: e8c91fc935566d58b13df15f91d494df46db9dde [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
Eric Laurenta54f1282017-07-01 19:39:32 -0700100 aaudio_result_t startClient(aaudio_handle_t streamHandle __unused,
jiabind1f1cb62020-03-24 11:57:57 -0700101 const android::AudioClient& client __unused,
102 const audio_attributes_t *attr __unused,
103 audio_port_handle_t *clientHandle __unused) override {
Eric Laurenta54f1282017-07-01 19:39:32 -0700104 return AAUDIO_ERROR_UNAVAILABLE;
105 }
106
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
Phil Burk11e8d332017-05-24 09:59:02 -0700112 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:
121 AAudioClient(android::wp<AAudioBinderClient> aaudioBinderClient)
Phil Burk2bc7c182017-08-28 11:45:01 -0700122 : mBinderClient(aaudioBinderClient) {
Phil Burk11e8d332017-05-24 09:59:02 -0700123 }
124
125 // implement DeathRecipient
126 virtual void binderDied(const android::wp<android::IBinder>& who __unused) {
127 android::sp<AAudioBinderClient> client = mBinderClient.promote();
Phil Burk2bc7c182017-08-28 11:45:01 -0700128 if (client.get() != nullptr) {
Phil Burk11e8d332017-05-24 09:59:02 -0700129 client->dropAAudioService();
130 }
131 ALOGW("AAudio service binderDied()!");
132 }
133
134 // implement BnAAudioClient
135 void onStreamChange(aaudio_handle_t handle, int32_t opcode, int32_t value) {
136 android::sp<AAudioBinderClient> client = mBinderClient.promote();
Phil Burk2bc7c182017-08-28 11:45:01 -0700137 if (client.get() != nullptr) {
Phil Burk11e8d332017-05-24 09:59:02 -0700138 client->onStreamChange(handle, opcode, value);
139 }
140 }
141 private:
142 android::wp<AAudioBinderClient> mBinderClient;
143 };
144
Phil Burk2bc7c182017-08-28 11:45:01 -0700145private:
Phil Burk11e8d332017-05-24 09:59:02 -0700146
Phil Burk2bc7c182017-08-28 11:45:01 -0700147 android::Mutex mServiceLock;
Phil Burk11e8d332017-05-24 09:59:02 -0700148 android::sp<android::IAAudioService> mAAudioService;
Phil Burk2bc7c182017-08-28 11:45:01 -0700149 android::sp<AAudioClient> mAAudioClient;
Phil Burk11e8d332017-05-24 09:59:02 -0700150
Phil Burkc0c70e32017-02-09 13:18:38 -0800151};
152
153
154} /* namespace aaudio */
155
Phil Burk5204d312017-05-04 17:16:13 -0700156#endif //ANDROID_AAUDIO_AAUDIO_BINDER_CLIENT_H