1 /* 2 * Copyright (C) 2018 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_MEDIA_NBLOG_READER_H 18 #define ANDROID_MEDIA_NBLOG_READER_H 19 20 #include <memory> 21 #include <stddef.h> 22 #include <string> 23 #include <unordered_set> 24 25 #include <media/nblog/Entry.h> 26 #include <media/nblog/Events.h> 27 #include <utils/RefBase.h> 28 29 class audio_utils_fifo; 30 class audio_utils_fifo_reader; 31 32 namespace android { 33 34 class IMemory; 35 class String8; 36 37 namespace NBLog { 38 39 struct Shared; 40 41 // NBLog Reader API 42 43 class Snapshot; // Forward declaration needed for Reader::getSnapshot() 44 45 class Reader : public RefBase { 46 public: 47 // Input parameter 'size' is the desired size of the timeline in byte units. 48 // The size of the shared memory must be at least Timeline::sharedSize(size). 49 Reader(const void *shared, size_t size, const std::string &name); 50 Reader(const sp<IMemory>& iMemory, size_t size, const std::string &name); 51 ~Reader() override; 52 53 // get snapshot of readers fifo buffer, effectively consuming the buffer 54 std::unique_ptr<Snapshot> getSnapshot(bool flush = true); 55 bool isIMemory(const sp<IMemory>& iMemory) const; name()56 const std::string &name() const { return mName; } 57 58 private: 59 // Amount of tries for reader to catch up with writer in getSnapshot(). 60 static constexpr int kMaxObtainTries = 3; 61 62 // invalidBeginTypes and invalidEndTypes are used to align the Snapshot::begin() and 63 // Snapshot::end() EntryIterators to valid entries. 64 static const std::unordered_set<Event> invalidBeginTypes; 65 static const std::unordered_set<Event> invalidEndTypes; 66 67 // declared as const because audio_utils_fifo() constructor 68 sp<IMemory> mIMemory; // ref-counted version, assigned only in constructor 69 70 const std::string mName; // name of reader (actually name of writer) 71 /*const*/ Shared* const mShared; // raw pointer to shared memory, actually const but not 72 audio_utils_fifo * const mFifo; // FIFO itself, 73 // non-NULL unless constructor fails 74 audio_utils_fifo_reader * const mFifoReader; // used to read from FIFO, 75 // non-NULL unless constructor fails 76 77 // Searches for the last valid entry in the range [front, back) 78 // back has to be entry-aligned. Returns nullptr if none enconuntered. 79 static const uint8_t *findLastValidEntry(const uint8_t *front, const uint8_t *back, 80 const std::unordered_set<Event> &invalidTypes); 81 }; 82 83 // A snapshot of a readers buffer 84 // This is raw data. No analysis has been done on it 85 class Snapshot { 86 public: ~Snapshot()87 ~Snapshot() { delete[] mData; } 88 89 // amount of data lost (given by audio_utils_fifo_reader) lost()90 size_t lost() const { return mLost; } 91 92 // iterator to beginning of readable segment of snapshot 93 // data between begin and end has valid entries begin()94 EntryIterator begin() const { return mBegin; } 95 96 // iterator to end of readable segment of snapshot end()97 EntryIterator end() const { return mEnd; } 98 99 private: 100 Snapshot() = default; Snapshot(size_t bufferSize)101 explicit Snapshot(size_t bufferSize) : mData(new uint8_t[bufferSize]) {} 102 friend std::unique_ptr<Snapshot> Reader::getSnapshot(bool flush); 103 104 uint8_t * const mData = nullptr; 105 size_t mLost = 0; 106 EntryIterator mBegin; 107 EntryIterator mEnd; 108 }; 109 110 // TODO move this to MediaLogService? 111 class DumpReader : public NBLog::Reader { 112 public: DumpReader(const void * shared,size_t size,const std::string & name)113 DumpReader(const void *shared, size_t size, const std::string &name) 114 : Reader(shared, size, name) {} DumpReader(const sp<IMemory> & iMemory,size_t size,const std::string & name)115 DumpReader(const sp<IMemory>& iMemory, size_t size, const std::string &name) 116 : Reader(iMemory, size, name) {} 117 void dump(int fd, size_t indent = 0); 118 private: handleAuthor(const AbstractEntry & fmtEntry __unused,String8 * body __unused)119 void handleAuthor(const AbstractEntry& fmtEntry __unused, String8* body __unused) {} 120 EntryIterator handleFormat(const FormatEntry &fmtEntry, String8 *timestamp, String8 *body); 121 122 static void appendInt(String8 *body, const void *data); 123 static void appendFloat(String8 *body, const void *data); 124 static void appendPID(String8 *body, const void *data, size_t length); 125 static void appendTimestamp(String8 *body, const void *data); 126 127 // The bufferDump functions are used for debugging only. 128 static String8 bufferDump(const uint8_t *buffer, size_t size); 129 static String8 bufferDump(const EntryIterator &it); 130 }; 131 132 } // namespace NBLog 133 } // namespace android 134 135 #endif // ANDROID_MEDIA_NBLOG_READER_H 136