1828bea5f61f3c40d24759cd8739dd95570883b56Phil Burk/*
2828bea5f61f3c40d24759cd8739dd95570883b56Phil Burk * Copyright 2016 The Android Open Source Project
3828bea5f61f3c40d24759cd8739dd95570883b56Phil Burk *
4828bea5f61f3c40d24759cd8739dd95570883b56Phil Burk * Licensed under the Apache License, Version 2.0 (the "License");
5828bea5f61f3c40d24759cd8739dd95570883b56Phil Burk * you may not use this file except in compliance with the License.
6828bea5f61f3c40d24759cd8739dd95570883b56Phil Burk * You may obtain a copy of the License at
7828bea5f61f3c40d24759cd8739dd95570883b56Phil Burk *
8828bea5f61f3c40d24759cd8739dd95570883b56Phil Burk *      http://www.apache.org/licenses/LICENSE-2.0
9828bea5f61f3c40d24759cd8739dd95570883b56Phil Burk *
10828bea5f61f3c40d24759cd8739dd95570883b56Phil Burk * Unless required by applicable law or agreed to in writing, software
11828bea5f61f3c40d24759cd8739dd95570883b56Phil Burk * distributed under the License is distributed on an "AS IS" BASIS,
12828bea5f61f3c40d24759cd8739dd95570883b56Phil Burk * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13828bea5f61f3c40d24759cd8739dd95570883b56Phil Burk * See the License for the specific language governing permissions and
14828bea5f61f3c40d24759cd8739dd95570883b56Phil Burk * limitations under the License.
15828bea5f61f3c40d24759cd8739dd95570883b56Phil Burk */
16828bea5f61f3c40d24759cd8739dd95570883b56Phil Burk
175204d315c6c6f53188f8d1414dd1b55b6c90142bPhil Burk#ifndef ANDROID_AAUDIO_SHARED_MEMORY_PARCELABLE_H
185204d315c6c6f53188f8d1414dd1b55b6c90142bPhil Burk#define ANDROID_AAUDIO_SHARED_MEMORY_PARCELABLE_H
19828bea5f61f3c40d24759cd8739dd95570883b56Phil Burk
20828bea5f61f3c40d24759cd8739dd95570883b56Phil Burk#include <stdint.h>
21828bea5f61f3c40d24759cd8739dd95570883b56Phil Burk#include <sys/mman.h>
22e72481c5c16d401c776e0ba49b84b80223769b91Phil Burk
23e72481c5c16d401c776e0ba49b84b80223769b91Phil Burk#include <android-base/unique_fd.h>
24828bea5f61f3c40d24759cd8739dd95570883b56Phil Burk#include <binder/Parcel.h>
25828bea5f61f3c40d24759cd8739dd95570883b56Phil Burk#include <binder/Parcelable.h>
26828bea5f61f3c40d24759cd8739dd95570883b56Phil Burk
275ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burknamespace aaudio {
28828bea5f61f3c40d24759cd8739dd95570883b56Phil Burk
29828bea5f61f3c40d24759cd8739dd95570883b56Phil Burk// Arbitrary limits for sanity checks. TODO remove after debugging.
30828bea5f61f3c40d24759cd8739dd95570883b56Phil Burk#define MAX_SHARED_MEMORIES (32)
313df348fbaca567ca891503213ff8c344a1ea2e05Phil Burk#define MAX_MMAP_OFFSET_BYTES (32 * 1024 * 8)
323df348fbaca567ca891503213ff8c344a1ea2e05Phil Burk#define MAX_MMAP_SIZE_BYTES (32 * 1024 * 8)
33828bea5f61f3c40d24759cd8739dd95570883b56Phil Burk
34828bea5f61f3c40d24759cd8739dd95570883b56Phil Burk/**
35828bea5f61f3c40d24759cd8739dd95570883b56Phil Burk * This is a parcelable description of a shared memory referenced by a file descriptor.
36828bea5f61f3c40d24759cd8739dd95570883b56Phil Burk * It may be divided into several regions.
37e72481c5c16d401c776e0ba49b84b80223769b91Phil Burk * The memory can be shared using Binder or simply shared between threads.
38828bea5f61f3c40d24759cd8739dd95570883b56Phil Burk */
39e72481c5c16d401c776e0ba49b84b80223769b91Phil Burkclass SharedMemoryParcelable : public android::Parcelable {
40828bea5f61f3c40d24759cd8739dd95570883b56Phil Burkpublic:
41828bea5f61f3c40d24759cd8739dd95570883b56Phil Burk    SharedMemoryParcelable();
42828bea5f61f3c40d24759cd8739dd95570883b56Phil Burk    virtual ~SharedMemoryParcelable();
43828bea5f61f3c40d24759cd8739dd95570883b56Phil Burk
44e72481c5c16d401c776e0ba49b84b80223769b91Phil Burk    /**
45e72481c5c16d401c776e0ba49b84b80223769b91Phil Burk     * Make a dup() of the fd and store it for later use.
46e72481c5c16d401c776e0ba49b84b80223769b91Phil Burk     *
47e72481c5c16d401c776e0ba49b84b80223769b91Phil Burk     * @param fd
48e72481c5c16d401c776e0ba49b84b80223769b91Phil Burk     * @param sizeInBytes
49e72481c5c16d401c776e0ba49b84b80223769b91Phil Burk     */
50e72481c5c16d401c776e0ba49b84b80223769b91Phil Burk    void setup(const android::base::unique_fd& fd, int32_t sizeInBytes);
51828bea5f61f3c40d24759cd8739dd95570883b56Phil Burk
52e72481c5c16d401c776e0ba49b84b80223769b91Phil Burk    virtual android::status_t writeToParcel(android::Parcel* parcel) const override;
53828bea5f61f3c40d24759cd8739dd95570883b56Phil Burk
54e72481c5c16d401c776e0ba49b84b80223769b91Phil Burk    virtual android::status_t readFromParcel(const android::Parcel* parcel) override;
55828bea5f61f3c40d24759cd8739dd95570883b56Phil Burk
56c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk    // mmap() shared memory
575ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    aaudio_result_t resolve(int32_t offsetInBytes, int32_t sizeInBytes, void **regionAddressPtr);
58828bea5f61f3c40d24759cd8739dd95570883b56Phil Burk
59c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk    // munmap() any mapped memory
60c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk    aaudio_result_t close();
61c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk
62828bea5f61f3c40d24759cd8739dd95570883b56Phil Burk    int32_t getSizeInBytes();
63828bea5f61f3c40d24759cd8739dd95570883b56Phil Burk
64828bea5f61f3c40d24759cd8739dd95570883b56Phil Burk    void dump();
65828bea5f61f3c40d24759cd8739dd95570883b56Phil Burk
66828bea5f61f3c40d24759cd8739dd95570883b56Phil Burkprotected:
67478d5df3a0e1693669f5809176d5c7f6e8c9aa88Phil Burk
68478d5df3a0e1693669f5809176d5c7f6e8c9aa88Phil Burk#define MMAP_UNRESOLVED_ADDRESS    reinterpret_cast<uint8_t*>(MAP_FAILED)
69478d5df3a0e1693669f5809176d5c7f6e8c9aa88Phil Burk
70e72481c5c16d401c776e0ba49b84b80223769b91Phil Burk    aaudio_result_t resolveSharedMemory(const android::base::unique_fd& fd);
71e72481c5c16d401c776e0ba49b84b80223769b91Phil Burk
72e72481c5c16d401c776e0ba49b84b80223769b91Phil Burk    android::base::unique_fd   mFd;
73e72481c5c16d401c776e0ba49b84b80223769b91Phil Burk    int32_t     mSizeInBytes = 0;
74e72481c5c16d401c776e0ba49b84b80223769b91Phil Burk    uint8_t    *mResolvedAddress = MMAP_UNRESOLVED_ADDRESS;
75a5891f427bbd57914090f3260430b82f6879ec8dPhil Burk
76a5891f427bbd57914090f3260430b82f6879ec8dPhil Burkprivate:
77a5891f427bbd57914090f3260430b82f6879ec8dPhil Burk
78a5891f427bbd57914090f3260430b82f6879ec8dPhil Burk    aaudio_result_t validate() const;
79a5891f427bbd57914090f3260430b82f6879ec8dPhil Burk
80828bea5f61f3c40d24759cd8739dd95570883b56Phil Burk};
81828bea5f61f3c40d24759cd8739dd95570883b56Phil Burk
825ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk} /* namespace aaudio */
83828bea5f61f3c40d24759cd8739dd95570883b56Phil Burk
845204d315c6c6f53188f8d1414dd1b55b6c90142bPhil Burk#endif //ANDROID_AAUDIO_SHARED_MEMORY_PARCELABLE_H
85