• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 #define LOG_TAG "SharedMemoryParcelable"
18 //#define LOG_NDEBUG 0
19 #include <utils/Log.h>
20 
21 #include <inttypes.h>
22 #include <stdint.h>
23 #include <stdio.h>
24 
25 #include <sys/mman.h>
26 #include <aaudio/AAudio.h>
27 
28 #include <android-base/unique_fd.h>
29 #include <binder/Parcelable.h>
30 #include <utility/AAudioUtilities.h>
31 
32 #include "binding/SharedMemoryParcelable.h"
33 
34 using android::base::unique_fd;
35 using android::status_t;
36 using android::media::SharedFileRegion;
37 
38 using namespace aaudio;
39 
SharedMemoryParcelable(SharedFileRegion && parcelable)40 SharedMemoryParcelable::SharedMemoryParcelable(SharedFileRegion&& parcelable) {
41     mFd = parcelable.fd.release();
42     mSizeInBytes = parcelable.size;
43     mOffsetInBytes = parcelable.offset;
44 }
45 
parcelable()46 SharedFileRegion SharedMemoryParcelable::parcelable() && {
47     SharedFileRegion result;
48     result.fd.reset(std::move(mFd));
49     result.size = mSizeInBytes;
50     result.offset = mOffsetInBytes;
51     return result;
52 }
53 
dup() const54 SharedMemoryParcelable SharedMemoryParcelable::dup() const {
55     SharedMemoryParcelable result;
56     result.setup(mFd, static_cast<int32_t>(mSizeInBytes));
57     return result;
58 }
59 
setup(const unique_fd & fd,int32_t sizeInBytes)60 void SharedMemoryParcelable::setup(const unique_fd& fd, int32_t sizeInBytes) {
61     constexpr int minFd = 3; // skip over stdout, stdin and stderr
62     mFd.reset(fcntl(fd.get(), F_DUPFD_CLOEXEC, minFd)); // store a duplicate FD
63     ALOGV("setup(fd = %d -> %d, size = %d) this = %p\n", fd.get(), mFd.get(), sizeInBytes, this);
64     mSizeInBytes = sizeInBytes;
65 }
66 
setup(const SharedMemoryParcelable & sharedMemoryParcelable)67 void SharedMemoryParcelable::setup(const SharedMemoryParcelable &sharedMemoryParcelable) {
68     setup(sharedMemoryParcelable.mFd, sharedMemoryParcelable.mSizeInBytes);
69 }
70 
close()71 aaudio_result_t SharedMemoryParcelable::close() {
72     if (mResolvedAddress != MMAP_UNRESOLVED_ADDRESS) {
73         int err = munmap(mResolvedAddress, mSizeInBytes);
74         if (err < 0) {
75             ALOGE("close() munmap() failed %d", err);
76             return AAudioConvert_androidToAAudioResult(err);
77         }
78         mResolvedAddress = MMAP_UNRESOLVED_ADDRESS;
79     }
80     return AAUDIO_OK;
81 }
82 
closeAndReleaseFd()83 aaudio_result_t SharedMemoryParcelable::closeAndReleaseFd() {
84     aaudio_result_t result = close();
85     if (result == AAUDIO_OK) {
86         mFd.reset();
87     }
88     return result;
89 }
90 
resolveSharedMemory(const unique_fd & fd)91 aaudio_result_t SharedMemoryParcelable::resolveSharedMemory(const unique_fd& fd) {
92     mResolvedAddress = (uint8_t *) mmap(nullptr, mSizeInBytes, PROT_READ | PROT_WRITE,
93                                         MAP_SHARED, fd.get(), 0);
94     if (mResolvedAddress == MMAP_UNRESOLVED_ADDRESS) {
95         ALOGE("mmap() failed for fd = %d, nBytes = %" PRId64 ", errno = %s",
96               fd.get(), mSizeInBytes, strerror(errno));
97         return AAUDIO_ERROR_INTERNAL;
98     }
99     return AAUDIO_OK;
100 }
101 
resolve(int32_t offsetInBytes,int32_t sizeInBytes,void ** regionAddressPtr)102 aaudio_result_t SharedMemoryParcelable::resolve(int32_t offsetInBytes, int32_t sizeInBytes,
103                                               void **regionAddressPtr) {
104     if (offsetInBytes < 0) {
105         ALOGE("illegal offsetInBytes = %d", offsetInBytes);
106         return AAUDIO_ERROR_OUT_OF_RANGE;
107     } else if ((offsetInBytes + sizeInBytes) > mSizeInBytes) {
108         ALOGE("out of range, offsetInBytes = %d, "
109                       "sizeInBytes = %d, mSizeInBytes = %" PRId64,
110               offsetInBytes, sizeInBytes, mSizeInBytes);
111         return AAUDIO_ERROR_OUT_OF_RANGE;
112     }
113 
114     aaudio_result_t result = AAUDIO_OK;
115 
116     if (mResolvedAddress == MMAP_UNRESOLVED_ADDRESS) {
117         if (mFd.get() != -1) {
118             result = resolveSharedMemory(mFd);
119         } else {
120             ALOGE("has no file descriptor for shared memory.");
121             result = AAUDIO_ERROR_INTERNAL;
122         }
123     }
124 
125     if (result == AAUDIO_OK && mResolvedAddress != MMAP_UNRESOLVED_ADDRESS) {
126         *regionAddressPtr = mResolvedAddress + offsetInBytes;
127         ALOGV("mResolvedAddress = %p", mResolvedAddress);
128         ALOGV("offset by %d, *regionAddressPtr = %p", offsetInBytes, *regionAddressPtr);
129     }
130     return result;
131 }
132 
getSizeInBytes()133 int32_t SharedMemoryParcelable::getSizeInBytes() {
134     return mSizeInBytes;
135 }
136 
validate() const137 aaudio_result_t SharedMemoryParcelable::validate() const {
138     if (mSizeInBytes < 0 || mSizeInBytes >= MAX_MMAP_SIZE_BYTES) {
139         ALOGE("invalid mSizeInBytes = %" PRId64, mSizeInBytes);
140         return AAUDIO_ERROR_OUT_OF_RANGE;
141     }
142     if (mOffsetInBytes != 0) {
143         ALOGE("invalid mOffsetInBytes = %" PRId64, mOffsetInBytes);
144         return AAUDIO_ERROR_OUT_OF_RANGE;
145     }
146     return AAUDIO_OK;
147 }
148 
dump()149 void SharedMemoryParcelable::dump() {
150     ALOGD("mFd = %d", mFd.get());
151     ALOGD("mSizeInBytes = %" PRId64, mSizeInBytes);
152 }
153