Hongguang | 600a6ae | 2021-07-08 18:51:51 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2021 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 | #pragma once |
| 18 | |
| 19 | #include <fcntl.h> |
| 20 | #include <fmq/AidlMessageQueue.h> |
| 21 | #include <gtest/gtest.h> |
| 22 | #include <log/log.h> |
| 23 | #include <utils/Condition.h> |
| 24 | #include <utils/Mutex.h> |
| 25 | #include <fstream> |
| 26 | #include <iostream> |
| 27 | #include <map> |
| 28 | |
| 29 | #include <aidl/android/hardware/tv/tuner/BnDvrCallback.h> |
| 30 | #include <aidl/android/hardware/tv/tuner/IDvr.h> |
| 31 | #include <aidl/android/hardware/tv/tuner/ITuner.h> |
| 32 | |
| 33 | #include "FilterTests.h" |
| 34 | |
| 35 | using ::aidl::android::hardware::common::fmq::MQDescriptor; |
| 36 | using ::aidl::android::hardware::common::fmq::SynchronizedReadWrite; |
| 37 | using ::android::AidlMessageQueue; |
| 38 | using ::android::Condition; |
| 39 | using ::android::Mutex; |
| 40 | using ::android::hardware::EventFlag; |
| 41 | |
| 42 | using namespace aidl::android::hardware::tv::tuner; |
| 43 | using namespace std; |
| 44 | |
| 45 | #define WAIT_TIMEOUT 3000000000 |
| 46 | |
| 47 | class DvrCallback : public BnDvrCallback { |
| 48 | public: |
| 49 | virtual ::ndk::ScopedAStatus onRecordStatus(RecordStatus status) override { |
| 50 | ALOGD("[vts] record status %hhu", status); |
| 51 | switch (status) { |
| 52 | case RecordStatus::DATA_READY: |
| 53 | break; |
| 54 | case RecordStatus::LOW_WATER: |
| 55 | break; |
| 56 | case RecordStatus::HIGH_WATER: |
| 57 | case RecordStatus::OVERFLOW: |
| 58 | ALOGD("[vts] record overflow. Flushing."); |
| 59 | EXPECT_TRUE(mDvr) << "Dvr callback is not set with an IDvr"; |
| 60 | if (mDvr) { |
| 61 | ndk::ScopedAStatus result = mDvr->flush(); |
| 62 | ALOGD("[vts] Flushing result %s.", result.getMessage()); |
| 63 | } |
| 64 | break; |
| 65 | } |
| 66 | return ndk::ScopedAStatus::ok(); |
| 67 | } |
| 68 | |
| 69 | virtual ::ndk::ScopedAStatus onPlaybackStatus(PlaybackStatus status) override { |
| 70 | // android::Mutex::Autolock autoLock(mMsgLock); |
| 71 | ALOGD("[vts] playback status %d", status); |
| 72 | switch (status) { |
| 73 | case PlaybackStatus::SPACE_EMPTY: |
| 74 | case PlaybackStatus::SPACE_ALMOST_EMPTY: |
| 75 | ALOGD("[vts] keep playback inputing %d", status); |
| 76 | mKeepWritingPlaybackFMQ = true; |
| 77 | break; |
| 78 | case PlaybackStatus::SPACE_ALMOST_FULL: |
| 79 | case PlaybackStatus::SPACE_FULL: |
| 80 | ALOGD("[vts] stop playback inputing %d", status); |
| 81 | mKeepWritingPlaybackFMQ = false; |
| 82 | break; |
| 83 | } |
| 84 | return ndk::ScopedAStatus::ok(); |
| 85 | } |
| 86 | |
| 87 | void stopPlaybackThread(); |
| 88 | void testRecordOutput(); |
| 89 | void stopRecordThread(); |
| 90 | |
| 91 | void startPlaybackInputThread(string& dataInputFile, PlaybackSettings& settings, |
| 92 | MQDesc& playbackMQDescriptor); |
| 93 | void startRecordOutputThread(RecordSettings recordSettings, MQDesc& recordMQDescriptor); |
| 94 | static void* __threadLoopPlayback(void* user); |
| 95 | static void* __threadLoopRecord(void* threadArgs); |
| 96 | void playbackThreadLoop(); |
| 97 | void recordThreadLoop(RecordSettings* recordSetting, bool* keepWritingPlaybackFMQ); |
| 98 | |
| 99 | bool readRecordFMQ(); |
| 100 | |
| 101 | void setDvr(std::shared_ptr<IDvr> dvr) { mDvr = dvr; } |
| 102 | |
| 103 | private: |
| 104 | struct RecordThreadArgs { |
| 105 | DvrCallback* user; |
| 106 | RecordSettings* recordSettings; |
| 107 | bool* keepReadingRecordFMQ; |
| 108 | }; |
| 109 | // uint16_t mDataLength = 0; |
| 110 | std::vector<int8_t> mDataOutputBuffer; |
| 111 | |
| 112 | std::map<uint32_t, std::unique_ptr<FilterMQ>> mFilterMQ; |
| 113 | std::unique_ptr<FilterMQ> mPlaybackMQ; |
| 114 | std::unique_ptr<FilterMQ> mRecordMQ; |
| 115 | std::map<uint32_t, EventFlag*> mFilterMQEventFlag; |
| 116 | |
| 117 | android::Mutex mMsgLock; |
| 118 | android::Mutex mPlaybackThreadLock; |
| 119 | android::Mutex mRecordThreadLock; |
| 120 | android::Condition mMsgCondition; |
| 121 | |
| 122 | bool mKeepWritingPlaybackFMQ = true; |
| 123 | bool mKeepReadingRecordFMQ = true; |
| 124 | bool mPlaybackThreadRunning; |
| 125 | bool mRecordThreadRunning; |
| 126 | pthread_t mPlaybackThread; |
| 127 | pthread_t mRecordThread; |
| 128 | string mInputDataFile; |
| 129 | PlaybackSettings mPlaybackSettings; |
| 130 | |
| 131 | std::shared_ptr<IDvr> mDvr = nullptr; |
| 132 | |
| 133 | // int mPidFilterOutputCount = 0; |
| 134 | }; |
| 135 | |
| 136 | class DvrTests { |
| 137 | public: |
| 138 | void setService(std::shared_ptr<ITuner> tuner) { mService = tuner; } |
| 139 | void setDemux(std::shared_ptr<IDemux> demux) { mDemux = demux; } |
| 140 | |
| 141 | void startPlaybackInputThread(string& dataInputFile, PlaybackSettings& settings) { |
| 142 | mDvrPlaybackCallback->startPlaybackInputThread(dataInputFile, settings, |
| 143 | mDvrPlaybackMQDescriptor); |
| 144 | }; |
| 145 | |
| 146 | void startRecordOutputThread(RecordSettings settings) { |
| 147 | mDvrRecordCallback->startRecordOutputThread(settings, mDvrRecordMQDescriptor); |
| 148 | }; |
| 149 | |
| 150 | void stopPlaybackThread() { mDvrPlaybackCallback->stopPlaybackThread(); } |
| 151 | void testRecordOutput() { mDvrRecordCallback->testRecordOutput(); } |
| 152 | void stopRecordThread() { mDvrRecordCallback->stopRecordThread(); } |
| 153 | |
| 154 | AssertionResult openDvrInDemux(DvrType type, int32_t bufferSize); |
| 155 | AssertionResult configDvrPlayback(DvrSettings setting); |
| 156 | AssertionResult configDvrRecord(DvrSettings setting); |
| 157 | AssertionResult getDvrPlaybackMQDescriptor(); |
| 158 | AssertionResult getDvrRecordMQDescriptor(); |
| 159 | AssertionResult attachFilterToDvr(std::shared_ptr<IFilter> filter); |
| 160 | AssertionResult detachFilterToDvr(std::shared_ptr<IFilter> filter); |
| 161 | AssertionResult stopDvrPlayback(); |
| 162 | AssertionResult startDvrPlayback(); |
| 163 | AssertionResult stopDvrRecord(); |
| 164 | AssertionResult startDvrRecord(); |
| 165 | void closeDvrPlayback(); |
| 166 | void closeDvrRecord(); |
| 167 | |
| 168 | protected: |
| 169 | static AssertionResult failure() { return ::testing::AssertionFailure(); } |
| 170 | |
| 171 | static AssertionResult success() { return ::testing::AssertionSuccess(); } |
| 172 | |
| 173 | std::shared_ptr<ITuner> mService; |
| 174 | std::shared_ptr<IDvr> mDvrPlayback; |
| 175 | std::shared_ptr<IDvr> mDvrRecord; |
| 176 | std::shared_ptr<IDemux> mDemux; |
| 177 | std::shared_ptr<DvrCallback> mDvrPlaybackCallback; |
| 178 | std::shared_ptr<DvrCallback> mDvrRecordCallback; |
| 179 | MQDesc mDvrPlaybackMQDescriptor; |
| 180 | MQDesc mDvrRecordMQDescriptor; |
| 181 | }; |