Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 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 | |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 17 | #define LOG_TAG "AudioEndpointParcelable" |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 18 | //#define LOG_NDEBUG 0 |
| 19 | #include <utils/Log.h> |
| 20 | |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 21 | #include <stdint.h> |
| 22 | |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 23 | #include <binder/Parcel.h> |
| 24 | #include <binder/Parcelable.h> |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 25 | #include <utility/AAudioUtilities.h> |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 26 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 27 | #include "binding/AAudioServiceDefinitions.h" |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 28 | #include "binding/RingBufferParcelable.h" |
| 29 | #include "binding/AudioEndpointParcelable.h" |
| 30 | |
Phil Burk | e72481c | 2017-08-08 13:20:45 -0700 | [diff] [blame] | 31 | using android::base::unique_fd; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 32 | using android::NO_ERROR; |
| 33 | using android::status_t; |
| 34 | using android::Parcel; |
| 35 | using android::Parcelable; |
| 36 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 37 | using namespace aaudio; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 38 | |
| 39 | /** |
| 40 | * Container for information about the message queues plus |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 41 | * general stream information needed by AAudio clients. |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 42 | * It contains no addresses, just sizes, offsets and file descriptors for |
| 43 | * shared memory that can be passed through Binder. |
| 44 | */ |
| 45 | AudioEndpointParcelable::AudioEndpointParcelable() {} |
| 46 | |
| 47 | AudioEndpointParcelable::~AudioEndpointParcelable() {} |
| 48 | |
| 49 | /** |
| 50 | * Add the file descriptor to the table. |
| 51 | * @return index in table or negative error |
| 52 | */ |
Phil Burk | e72481c | 2017-08-08 13:20:45 -0700 | [diff] [blame] | 53 | int32_t AudioEndpointParcelable::addFileDescriptor(const unique_fd& fd, |
| 54 | int32_t sizeInBytes) { |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 55 | if (mNumSharedMemories >= MAX_SHARED_MEMORIES) { |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 56 | return AAUDIO_ERROR_OUT_OF_RANGE; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 57 | } |
| 58 | int32_t index = mNumSharedMemories++; |
| 59 | mSharedMemories[index].setup(fd, sizeInBytes); |
| 60 | return index; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * The read and write must be symmetric. |
| 65 | */ |
| 66 | status_t AudioEndpointParcelable::writeToParcel(Parcel* parcel) const { |
Phil Burk | a5891f4 | 2018-03-12 17:21:11 -0700 | [diff] [blame] | 67 | status_t status = AAudioConvert_aaudioToAndroidStatus(validate()); |
| 68 | if (status != NO_ERROR) goto error; |
| 69 | |
| 70 | status = parcel->writeInt32(mNumSharedMemories); |
| 71 | if (status != NO_ERROR) goto error; |
| 72 | |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 73 | for (int i = 0; i < mNumSharedMemories; i++) { |
Phil Burk | a5891f4 | 2018-03-12 17:21:11 -0700 | [diff] [blame] | 74 | status = mSharedMemories[i].writeToParcel(parcel); |
| 75 | if (status != NO_ERROR) goto error; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 76 | } |
Phil Burk | a5891f4 | 2018-03-12 17:21:11 -0700 | [diff] [blame] | 77 | status = mUpMessageQueueParcelable.writeToParcel(parcel); |
| 78 | if (status != NO_ERROR) goto error; |
| 79 | status = mDownMessageQueueParcelable.writeToParcel(parcel); |
| 80 | if (status != NO_ERROR) goto error; |
| 81 | status = mUpDataQueueParcelable.writeToParcel(parcel); |
| 82 | if (status != NO_ERROR) goto error; |
| 83 | status = mDownDataQueueParcelable.writeToParcel(parcel); |
| 84 | if (status != NO_ERROR) goto error; |
| 85 | |
| 86 | return NO_ERROR; |
| 87 | |
| 88 | error: |
| 89 | ALOGE("%s returning %d", __func__, status); |
| 90 | return status; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | status_t AudioEndpointParcelable::readFromParcel(const Parcel* parcel) { |
Phil Burk | a5891f4 | 2018-03-12 17:21:11 -0700 | [diff] [blame] | 94 | status_t status = parcel->readInt32(&mNumSharedMemories); |
| 95 | if (status != NO_ERROR) goto error; |
| 96 | |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 97 | for (int i = 0; i < mNumSharedMemories; i++) { |
| 98 | mSharedMemories[i].readFromParcel(parcel); |
Phil Burk | a5891f4 | 2018-03-12 17:21:11 -0700 | [diff] [blame] | 99 | if (status != NO_ERROR) goto error; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 100 | } |
Phil Burk | a5891f4 | 2018-03-12 17:21:11 -0700 | [diff] [blame] | 101 | status = mUpMessageQueueParcelable.readFromParcel(parcel); |
| 102 | if (status != NO_ERROR) goto error; |
| 103 | status = mDownMessageQueueParcelable.readFromParcel(parcel); |
| 104 | if (status != NO_ERROR) goto error; |
| 105 | status = mUpDataQueueParcelable.readFromParcel(parcel); |
| 106 | if (status != NO_ERROR) goto error; |
| 107 | status = mDownDataQueueParcelable.readFromParcel(parcel); |
| 108 | if (status != NO_ERROR) goto error; |
| 109 | |
| 110 | return AAudioConvert_aaudioToAndroidStatus(validate()); |
| 111 | |
| 112 | error: |
| 113 | ALOGE("%s returning %d", __func__, status); |
| 114 | return status; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 115 | } |
| 116 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 117 | aaudio_result_t AudioEndpointParcelable::resolve(EndpointDescriptor *descriptor) { |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 118 | aaudio_result_t result = mUpMessageQueueParcelable.resolve(mSharedMemories, |
| 119 | &descriptor->upMessageQueueDescriptor); |
| 120 | if (result != AAUDIO_OK) return result; |
| 121 | result = mDownMessageQueueParcelable.resolve(mSharedMemories, |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 122 | &descriptor->downMessageQueueDescriptor); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 123 | if (result != AAUDIO_OK) return result; |
| 124 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 125 | result = mDownDataQueueParcelable.resolve(mSharedMemories, |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 126 | &descriptor->dataQueueDescriptor); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 127 | return result; |
| 128 | } |
| 129 | |
| 130 | aaudio_result_t AudioEndpointParcelable::close() { |
| 131 | int err = 0; |
| 132 | for (int i = 0; i < mNumSharedMemories; i++) { |
| 133 | int lastErr = mSharedMemories[i].close(); |
| 134 | if (lastErr < 0) err = lastErr; |
| 135 | } |
| 136 | return AAudioConvert_androidToAAudioResult(err); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 137 | } |
| 138 | |
Phil Burk | a5891f4 | 2018-03-12 17:21:11 -0700 | [diff] [blame] | 139 | aaudio_result_t AudioEndpointParcelable::validate() const { |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 140 | if (mNumSharedMemories < 0 || mNumSharedMemories >= MAX_SHARED_MEMORIES) { |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 141 | ALOGE("invalid mNumSharedMemories = %d", mNumSharedMemories); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 142 | return AAUDIO_ERROR_INTERNAL; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 143 | } |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 144 | return AAUDIO_OK; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | void AudioEndpointParcelable::dump() { |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 148 | ALOGD("======================================= BEGIN"); |
| 149 | ALOGD("mNumSharedMemories = %d", mNumSharedMemories); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 150 | for (int i = 0; i < mNumSharedMemories; i++) { |
| 151 | mSharedMemories[i].dump(); |
| 152 | } |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 153 | ALOGD("mUpMessageQueueParcelable ========="); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 154 | mUpMessageQueueParcelable.dump(); |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 155 | ALOGD("mDownMessageQueueParcelable ======="); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 156 | mDownMessageQueueParcelable.dump(); |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 157 | ALOGD("mUpDataQueueParcelable ============"); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 158 | mUpDataQueueParcelable.dump(); |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 159 | ALOGD("mDownDataQueueParcelable =========="); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 160 | mDownDataQueueParcelable.dump(); |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 161 | ALOGD("======================================= END"); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 162 | } |
| 163 | |