• 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 #ifndef ANDROID_AAUDIO_SHARED_MEMORY_PARCELABLE_H
18 #define ANDROID_AAUDIO_SHARED_MEMORY_PARCELABLE_H
19 
20 #include <stdint.h>
21 #include <sys/mman.h>
22 
23 #include <android-base/unique_fd.h>
24 #include <android/media/SharedFileRegion.h>
25 
26 namespace aaudio {
27 
28 // Arbitrary limits for range checks.
29 #define MAX_SHARED_MEMORIES (32)
30 #define MAX_MMAP_OFFSET_BYTES (32 * 1024 * 8)
31 #define MAX_MMAP_SIZE_BYTES (32 * 1024 * 8)
32 
33 /**
34  * This is a parcelable description of a shared memory referenced by a file descriptor.
35  * It may be divided into several regions.
36  * The memory can be shared using Binder or simply shared between threads.
37  */
38 class SharedMemoryParcelable {
39 public:
40     SharedMemoryParcelable() = default;
41 
42     // Ctor from a parcelable representation.
43     // Since the parcelable object owns a unique FD, move semantics are provided to avoid the need
44     // to dupe.
45     explicit SharedMemoryParcelable(android::media::SharedFileRegion&& parcelable);
46 
47     /**
48      * Make a dup() of the fd and store it for later use.
49      *
50      * @param fd
51      * @param sizeInBytes
52      */
53     void setup(const android::base::unique_fd& fd, int32_t sizeInBytes);
54 
55     // mmap() shared memory
56     aaudio_result_t resolve(int32_t offsetInBytes, int32_t sizeInBytes, void **regionAddressPtr);
57 
58     // munmap() any mapped memory
59     aaudio_result_t close();
60 
61     int32_t getSizeInBytes();
62 
63     void dump();
64 
65     // Extract a parcelable representation of this object.
66     // Since we own a unique FD, move semantics are provided to avoid the need to dupe.
67     android::media::SharedFileRegion parcelable() &&;
68 
69     // Copy this instance. Duplicates the underlying FD.
70     SharedMemoryParcelable dup() const;
71 
72 private:
73 #define MMAP_UNRESOLVED_ADDRESS    reinterpret_cast<uint8_t*>(MAP_FAILED)
74 
75     android::base::unique_fd   mFd;
76     int64_t                    mSizeInBytes = 0;
77     int64_t                    mOffsetInBytes = 0;
78     uint8_t                   *mResolvedAddress = MMAP_UNRESOLVED_ADDRESS;
79 
80     aaudio_result_t resolveSharedMemory(const android::base::unique_fd& fd);
81     aaudio_result_t validate() const;
82 };
83 
84 } /* namespace aaudio */
85 
86 #endif //ANDROID_AAUDIO_SHARED_MEMORY_PARCELABLE_H
87