• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <atomic>
26 #include <fstream>
27 #include <iostream>
28 #include <map>
29 #include <thread>
30 
31 #include <aidl/android/hardware/tv/tuner/BnDvrCallback.h>
32 #include <aidl/android/hardware/tv/tuner/IDvr.h>
33 #include <aidl/android/hardware/tv/tuner/ITuner.h>
34 
35 #include "FilterTests.h"
36 
37 using ::aidl::android::hardware::common::fmq::MQDescriptor;
38 using ::aidl::android::hardware::common::fmq::SynchronizedReadWrite;
39 using ::android::AidlMessageQueue;
40 using ::android::Condition;
41 using ::android::Mutex;
42 using ::android::hardware::EventFlag;
43 
44 using namespace aidl::android::hardware::tv::tuner;
45 using namespace std;
46 
47 #define WAIT_TIMEOUT 3000000000
48 
49 class DvrCallback : public BnDvrCallback {
50   public:
onRecordStatus(RecordStatus status)51     virtual ::ndk::ScopedAStatus onRecordStatus(RecordStatus status) override {
52         ALOGD("[vts] record status %hhu", status);
53         switch (status) {
54             case RecordStatus::DATA_READY:
55                 break;
56             case RecordStatus::LOW_WATER:
57                 break;
58             case RecordStatus::HIGH_WATER:
59             case RecordStatus::OVERFLOW:
60                 ALOGD("[vts] record overflow. Flushing.");
61                 EXPECT_TRUE(mDvr) << "Dvr callback is not set with an IDvr";
62                 if (mDvr) {
63                     ndk::ScopedAStatus result = mDvr->flush();
64                     ALOGD("[vts] Flushing result %s.", result.getMessage());
65                 }
66                 break;
67         }
68         return ndk::ScopedAStatus::ok();
69     }
70 
onPlaybackStatus(PlaybackStatus status)71     virtual ::ndk::ScopedAStatus onPlaybackStatus(PlaybackStatus status) override {
72         // android::Mutex::Autolock autoLock(mMsgLock);
73         ALOGD("[vts] playback status %d", status);
74         switch (status) {
75             case PlaybackStatus::SPACE_EMPTY:
76             case PlaybackStatus::SPACE_ALMOST_EMPTY:
77                 ALOGD("[vts] keep playback inputing %d", status);
78                 mKeepWritingPlaybackFMQ = true;
79                 break;
80             case PlaybackStatus::SPACE_ALMOST_FULL:
81             case PlaybackStatus::SPACE_FULL:
82                 ALOGD("[vts] stop playback inputing %d", status);
83                 mKeepWritingPlaybackFMQ = false;
84                 break;
85         }
86         return ndk::ScopedAStatus::ok();
87     }
88 
89     void stopPlaybackThread();
90     void testRecordOutput();
91     void stopRecordThread();
92 
93     void startPlaybackInputThread(string& dataInputFile, PlaybackSettings& settings,
94                                   MQDesc& playbackMQDescriptor);
95     void startRecordOutputThread(RecordSettings recordSettings, MQDesc& recordMQDescriptor);
96     static void* __threadLoopPlayback(void* user);
97     static void* __threadLoopRecord(void* threadArgs);
98     void playbackThreadLoop();
99     void recordThreadLoop();
100 
101     bool readRecordFMQ();
102 
setDvr(std::shared_ptr<IDvr> dvr)103     void setDvr(std::shared_ptr<IDvr> dvr) { mDvr = dvr; }
104 
105   private:
106     struct RecordThreadArgs {
107         DvrCallback* user;
108         RecordSettings* recordSettings;
109         bool* keepReadingRecordFMQ;
110     };
111     // uint16_t mDataLength = 0;
112     std::vector<int8_t> mDataOutputBuffer;
113 
114     std::map<uint32_t, std::unique_ptr<FilterMQ>> mFilterMQ;
115     std::unique_ptr<FilterMQ> mPlaybackMQ;
116     std::unique_ptr<FilterMQ> mRecordMQ;
117     std::map<uint32_t, EventFlag*> mFilterMQEventFlag;
118 
119     android::Mutex mMsgLock;
120     android::Condition mMsgCondition;
121 
122     std::atomic<bool> mKeepWritingPlaybackFMQ = true;
123     std::atomic<bool> mKeepReadingRecordFMQ = true;
124     std::atomic<bool> mPlaybackThreadRunning;
125     std::atomic<bool> mRecordThreadRunning;
126     std::thread mPlaybackThread;
127     std::thread 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:
setService(std::shared_ptr<ITuner> tuner)138     void setService(std::shared_ptr<ITuner> tuner) { mService = tuner; }
setDemux(std::shared_ptr<IDemux> demux)139     void setDemux(std::shared_ptr<IDemux> demux) { mDemux = demux; }
140 
startPlaybackInputThread(string & dataInputFile,PlaybackSettings & settings)141     void startPlaybackInputThread(string& dataInputFile, PlaybackSettings& settings) {
142         mDvrPlaybackCallback->startPlaybackInputThread(dataInputFile, settings,
143                                                        mDvrPlaybackMQDescriptor);
144     };
145 
startRecordOutputThread(RecordSettings settings)146     void startRecordOutputThread(RecordSettings settings) {
147         mDvrRecordCallback->startRecordOutputThread(settings, mDvrRecordMQDescriptor);
148     };
149 
stopPlaybackThread()150     void stopPlaybackThread() { mDvrPlaybackCallback->stopPlaybackThread(); }
testRecordOutput()151     void testRecordOutput() { mDvrRecordCallback->testRecordOutput(); }
stopRecordThread()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     AssertionResult setPlaybackStatusCheckIntervalHint(int64_t milliseconds);
166     AssertionResult setRecordStatusCheckIntervalHint(int64_t milliseconds);
167     void closeDvrPlayback();
168     void closeDvrRecord();
169     int32_t getDvrPlaybackInterfaceVersion();
170     int32_t getDvrRecordInterfaceVersion();
171 
172   protected:
failure()173     static AssertionResult failure() { return ::testing::AssertionFailure(); }
174 
success()175     static AssertionResult success() { return ::testing::AssertionSuccess(); }
176 
177     std::shared_ptr<ITuner> mService;
178     std::shared_ptr<IDvr> mDvrPlayback;
179     std::shared_ptr<IDvr> mDvrRecord;
180     std::shared_ptr<IDemux> mDemux;
181     std::shared_ptr<DvrCallback> mDvrPlaybackCallback;
182     std::shared_ptr<DvrCallback> mDvrRecordCallback;
183     MQDesc mDvrPlaybackMQDescriptor;
184     MQDesc mDvrRecordMQDescriptor;
185 };
186