Tuner HAL Demux Filter VTS

Test: atest VtsHalTvTunerV1_0TargetTest
Bug: 135708935
Change-Id: I2b6b7e904cb274e7ccd10e481b04dfd9394afe3e
Merged-In: I2b6b7e904cb274e7ccd10e481b04dfd9394afe3e
diff --git a/tv/tuner/1.0/vts/functional/Android.bp b/tv/tuner/1.0/vts/functional/Android.bp
index faf566c..7d6b990 100644
--- a/tv/tuner/1.0/vts/functional/Android.bp
+++ b/tv/tuner/1.0/vts/functional/Android.bp
@@ -24,6 +24,8 @@
         "android.hidl.memory@1.0",
         "libhidlallocatorutils",
         "libhidlmemory",
+        "libcutils",
+        "libfmq",
     ],
     shared_libs: [
         "libbinder",
diff --git a/tv/tuner/1.0/vts/functional/VtsHalTvTunerV1_0TargetTest.cpp b/tv/tuner/1.0/vts/functional/VtsHalTvTunerV1_0TargetTest.cpp
index c652944..66adb2a 100644
--- a/tv/tuner/1.0/vts/functional/VtsHalTvTunerV1_0TargetTest.cpp
+++ b/tv/tuner/1.0/vts/functional/VtsHalTvTunerV1_0TargetTest.cpp
@@ -27,6 +27,7 @@
 #include <android/hardware/tv/tuner/1.0/ITuner.h>
 #include <android/hardware/tv/tuner/1.0/types.h>
 #include <binder/MemoryDealer.h>
+#include <fmq/MessageQueue.h>
 #include <hidl/HidlSupport.h>
 #include <hidl/HidlTransportSupport.h>
 #include <hidl/Status.h>
@@ -42,12 +43,22 @@
 using android::MemoryDealer;
 using android::Mutex;
 using android::sp;
+using android::hardware::EventFlag;
 using android::hardware::fromHeap;
 using android::hardware::hidl_string;
 using android::hardware::hidl_vec;
 using android::hardware::HidlMemory;
+using android::hardware::kSynchronizedReadWrite;
+using android::hardware::MessageQueue;
+using android::hardware::MQDescriptorSync;
 using android::hardware::Return;
 using android::hardware::Void;
+using android::hardware::tv::tuner::V1_0::DemuxFilterEvent;
+using android::hardware::tv::tuner::V1_0::DemuxFilterPesEvent;
+using android::hardware::tv::tuner::V1_0::DemuxFilterSectionEvent;
+using android::hardware::tv::tuner::V1_0::DemuxFilterStatus;
+using android::hardware::tv::tuner::V1_0::DemuxFilterType;
+using android::hardware::tv::tuner::V1_0::DemuxQueueNotifyBits;
 using android::hardware::tv::tuner::V1_0::FrontendAtscModulation;
 using android::hardware::tv::tuner::V1_0::FrontendAtscSettings;
 using android::hardware::tv::tuner::V1_0::FrontendDvbtSettings;
@@ -65,6 +76,53 @@
 
 namespace {
 
+using FilterMQ = MessageQueue<uint8_t, kSynchronizedReadWrite>;
+using FilterMQDesc = MQDescriptorSync<uint8_t>;
+
+const std::vector<uint8_t> goldenDataInputBuffer{
+        0x00, 0x00, 0x00, 0x01, 0x09, 0xf0, 0x00, 0x00, 0x00, 0x01, 0x67, 0x42, 0xc0, 0x1e, 0xdb,
+        0x01, 0x40, 0x16, 0xec, 0x04, 0x40, 0x00, 0x00, 0x03, 0x00, 0x40, 0x00, 0x00, 0x0f, 0x03,
+        0xc5, 0x8b, 0xb8, 0x00, 0x00, 0x00, 0x01, 0x68, 0xca, 0x8c, 0xb2, 0x00, 0x00, 0x01, 0x06,
+        0x05, 0xff, 0xff, 0x70, 0xdc, 0x45, 0xe9, 0xbd, 0xe6, 0xd9, 0x48, 0xb7, 0x96, 0x2c, 0xd8,
+        0x20, 0xd9, 0x23, 0xee, 0xef, 0x78, 0x32, 0x36, 0x34, 0x20, 0x2d, 0x20, 0x63, 0x6f, 0x72,
+        0x65, 0x20, 0x31, 0x34, 0x32, 0x20, 0x2d, 0x20, 0x48, 0x2e, 0x32, 0x36, 0x34, 0x2f, 0x4d,
+        0x50, 0x45, 0x47, 0x2d, 0x34, 0x20, 0x41, 0x56, 0x43, 0x20, 0x63, 0x6f, 0x64, 0x65, 0x63,
+        0x20, 0x2d, 0x20, 0x43, 0x6f, 0x70, 0x79, 0x6c, 0x65, 0x66, 0x74, 0x20, 0x32, 0x30, 0x30,
+        0x33, 0x2d, 0x32, 0x30, 0x31, 0x34, 0x20, 0x2d, 0x20, 0x68, 0x74, 0x74, 0x70, 0x3a, 0x2f,
+        0x2f, 0x77, 0x77, 0x77, 0x2e, 0x76, 0x69, 0x64, 0x65, 0x6f, 0x6c, 0x61, 0x6e, 0x2e, 0x6f,
+        0x72, 0x67, 0x2f, 0x78, 0x32, 0x36, 0x34, 0x2e, 0x68, 0x74, 0x6d, 0x6c, 0x20, 0x2d, 0x20,
+        0x6f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x3a, 0x20, 0x63, 0x61, 0x62, 0x61, 0x63, 0x3d,
+        0x30, 0x20, 0x72, 0x65, 0x66, 0x3d, 0x32, 0x20, 0x64, 0x65, 0x62, 0x6c, 0x6f, 0x63, 0x6b,
+        0x3d, 0x31, 0x3a, 0x30, 0x3a, 0x30, 0x20, 0x61, 0x6e, 0x61, 0x6c, 0x79, 0x73, 0x65, 0x3d,
+        0x30, 0x78, 0x31, 0x3a, 0x30, 0x78, 0x31, 0x31, 0x31, 0x20, 0x6d, 0x65, 0x3d, 0x68, 0x65,
+        0x78, 0x20, 0x73, 0x75, 0x62, 0x6d, 0x65, 0x3d, 0x37, 0x20, 0x70, 0x73, 0x79, 0x3d, 0x31,
+        0x20, 0x70, 0x73, 0x79, 0x5f, 0x72, 0x64, 0x3d, 0x31, 0x2e, 0x30, 0x30, 0x3a, 0x30, 0x2e,
+        0x30, 0x30, 0x20, 0x6d, 0x69, 0x78, 0x65, 0x64, 0x5f, 0x72, 0x65, 0x66, 0x3d, 0x31, 0x20,
+        0x6d, 0x65, 0x5f, 0x72, 0x61, 0x6e, 0x67, 0x65, 0x3d, 0x31, 0x36, 0x20, 0x63, 0x68, 0x72,
+        0x6f, 0x6d, 0x61, 0x5f, 0x6d, 0x65, 0x3d, 0x31, 0x20, 0x74, 0x72, 0x65, 0x6c, 0x6c, 0x69,
+        0x73, 0x3d, 0x31, 0x20, 0x38, 0x78, 0x38, 0x64, 0x63, 0x74, 0x3d, 0x30, 0x20, 0x63, 0x71,
+        0x6d, 0x3d, 0x30, 0x20, 0x64, 0x65, 0x61, 0x64, 0x7a, 0x6f, 0x6e, 0x65, 0x3d, 0x32, 0x31,
+        0x2c, 0x31, 0x31, 0x20, 0x66, 0x61, 0x73, 0x74, 0x5f, 0x70, 0x73, 0x6b, 0x69, 0x70, 0x3d,
+        0x31, 0x20, 0x63, 0x68, 0x72, 0x6f, 0x6d, 0x61, 0x5f, 0x71, 0x70, 0x5f, 0x6f, 0x66, 0x66,
+        0x73, 0x65, 0x74, 0x3d, 0x2d, 0x32, 0x20, 0x74, 0x68, 0x72, 0x65, 0x61, 0x64, 0x73, 0x3d,
+        0x36, 0x30, 0x20, 0x6c, 0x6f, 0x6f, 0x6b, 0x61, 0x68, 0x65, 0x61, 0x64, 0x5f, 0x74, 0x68,
+        0x72, 0x65, 0x61, 0x64, 0x73, 0x3d, 0x35, 0x20, 0x73, 0x6c, 0x69, 0x63, 0x65, 0x64, 0x5f,
+        0x74, 0x68, 0x72, 0x65, 0x61, 0x64, 0x73, 0x3d, 0x30, 0x20, 0x6e, 0x72, 0x3d, 0x30, 0x20,
+        0x64, 0x65, 0x63, 0x69, 0x6d, 0x61, 0x74, 0x65, 0x3d, 0x31, 0x20, 0x69, 0x6e, 0x74, 0x65,
+        0x72, 0x6c, 0x61, 0x63, 0x65, 0x64, 0x3d, 0x30, 0x20, 0x62, 0x6c, 0x75, 0x72, 0x61, 0x79,
+        0x5f, 0x63, 0x6f, 0x6d, 0x70, 0x61, 0x74, 0x3d, 0x30, 0x20, 0x63, 0x6f, 0x6e, 0x73, 0x74,
+        0x72, 0x61, 0x69, 0x6e, 0x65, 0x64, 0x5f, 0x69, 0x6e, 0x74, 0x72, 0x61, 0x3d, 0x30, 0x20,
+        0x62, 0x66, 0x72, 0x61, 0x6d, 0x65, 0x73, 0x3d, 0x30, 0x20, 0x77, 0x65, 0x69, 0x67, 0x68,
+        0x74, 0x70, 0x3d, 0x30, 0x20, 0x6b, 0x65, 0x79, 0x69, 0x6e, 0x74, 0x3d, 0x32, 0x35, 0x30,
+        0x20, 0x6b, 0x65, 0x79, 0x69, 0x6e, 0x74, 0x5f, 0x6d, 0x69, 0x6e, 0x3d, 0x32, 0x35, 0x20,
+        0x73, 0x63, 0x65, 0x6e, 0x65,
+};
+
+const uint16_t FMQ_SIZE_4K = 0x1000;
+// Equal to SECTION_WRITE_COUNT on the HAL impl side
+// The HAL impl will repeatedly write to the FMQ the count times
+const uint16_t SECTION_READ_COUNT = 10;
+
 class FrontendCallback : public IFrontendCallback {
   public:
     virtual Return<void> onEvent(FrontendEventType frontendEventType) override {
@@ -123,6 +181,134 @@
     }
 }
 
+class DemuxCallback : public IDemuxCallback {
+  public:
+    virtual Return<void> onFilterEvent(const DemuxFilterEvent& filterEvent) override {
+        android::Mutex::Autolock autoLock(mMsgLock);
+        mFilterEventReceived = true;
+        mFilterEvent = filterEvent;
+        mMsgCondition.signal();
+        return Void();
+    }
+
+    virtual Return<void> onFilterStatus(uint32_t /*filterId*/,
+                                        const DemuxFilterStatus /*status*/) override {
+        return Void();
+    }
+
+    void testOnFilterEvent(uint32_t filterId);
+    void testOnSectionFilterEvent(sp<IDemux>& demux, uint32_t filterId,
+                                  FilterMQDesc& filterMQDescriptor);
+    void testOnPesFilterEvent(sp<IDemux>& demux, uint32_t filterId,
+                              FilterMQDesc& filterMQDescriptor);
+    void readAndCompareSectionEventData();
+    void readAndComparePesEventData();
+
+  private:
+    bool mFilterEventReceived = false;
+    std::vector<uint8_t> mDataOutputBuffer;
+    std::unique_ptr<FilterMQ> mFilterMQ;
+    uint16_t mDataLength = 0;
+    DemuxFilterEvent mFilterEvent;
+    android::Mutex mMsgLock;
+    android::Mutex mReadLock;
+    android::Condition mMsgCondition;
+    EventFlag* mFilterMQEventFlag;
+};
+
+void DemuxCallback::testOnFilterEvent(uint32_t filterId) {
+    android::Mutex::Autolock autoLock(mMsgLock);
+    while (!mFilterEventReceived) {
+        if (-ETIMEDOUT == mMsgCondition.waitRelative(mMsgLock, WAIT_TIMEOUT)) {
+            EXPECT_TRUE(false) << "filter event not received within timeout";
+            return;
+        }
+    }
+    // Reset the filter event recieved flag
+    mFilterEventReceived = false;
+    // Check if filter id match
+    EXPECT_TRUE(filterId == mFilterEvent.filterId) << "filter id match";
+}
+
+void DemuxCallback::testOnSectionFilterEvent(sp<IDemux>& demux, uint32_t filterId,
+                                             FilterMQDesc& filterMQDescriptor) {
+    Result status;
+    // Create MQ to read the output into the local buffer
+    mFilterMQ = std::make_unique<FilterMQ>(filterMQDescriptor, true /* resetPointers */);
+    EXPECT_TRUE(mFilterMQ);
+    // Create the EventFlag that is used to signal the HAL impl that data have been
+    // read the Filter FMQ
+    EXPECT_TRUE(EventFlag::createEventFlag(mFilterMQ->getEventFlagWord(), &mFilterMQEventFlag) ==
+                android::OK);
+    // Start filter
+    status = demux->startFilter(filterId);
+    EXPECT_EQ(status, Result::SUCCESS);
+    // Test start filter and receive callback event
+    for (int i = 0; i < SECTION_READ_COUNT; i++) {
+        testOnFilterEvent(filterId);
+        // checksum of mDataOutputBuffer and Input golden input
+        readAndCompareSectionEventData();
+    }
+}
+
+void DemuxCallback::testOnPesFilterEvent(sp<IDemux>& demux, uint32_t filterId,
+                                         FilterMQDesc& filterMQDescriptor) {
+    Result status;
+    // Create MQ to read the output into the local buffer
+    mFilterMQ = std::make_unique<FilterMQ>(filterMQDescriptor, true /* resetPointers */);
+    EXPECT_TRUE(mFilterMQ);
+    // Create the EventFlag that is used to signal the HAL impl that data have been
+    // read the Filter FMQ
+    EXPECT_TRUE(EventFlag::createEventFlag(mFilterMQ->getEventFlagWord(), &mFilterMQEventFlag) ==
+                android::OK);
+    // Start filter
+    status = demux->startFilter(filterId);
+    EXPECT_EQ(status, Result::SUCCESS);
+    // Test start filter and receive callback event
+    testOnFilterEvent(filterId);
+    // checksum of mDataOutputBuffer and Input golden input
+    readAndComparePesEventData();
+}
+
+void DemuxCallback::readAndCompareSectionEventData() {
+    bool result = false;
+    for (int i = 0; i < mFilterEvent.events.size(); i++) {
+        DemuxFilterSectionEvent event = mFilterEvent.events[i].section();
+        mDataLength = event.dataLength;
+        EXPECT_TRUE(mDataLength == goldenDataInputBuffer.size()) << "buffer size does not match";
+
+        mDataOutputBuffer.resize(mDataLength);
+        result = mFilterMQ->read(mDataOutputBuffer.data(), mDataLength);
+        EXPECT_TRUE(result) << "can't read from Filter MQ";
+
+        for (int i = 0; i < mDataLength; i++) {
+            EXPECT_TRUE(goldenDataInputBuffer[i] == mDataOutputBuffer[i]) << "data does not match";
+        }
+    }
+    if (result) {
+        mFilterMQEventFlag->wake(static_cast<uint32_t>(DemuxQueueNotifyBits::DATA_CONSUMED));
+    }
+}
+
+void DemuxCallback::readAndComparePesEventData() {
+    // TODO handle multiple events in one filter callback event
+    DemuxFilterPesEvent event = mFilterEvent.events[0].pes();
+    mDataLength = event.dataLength;
+    EXPECT_TRUE(mDataLength == goldenDataInputBuffer.size()) << "buffer size does not match";
+
+    mDataOutputBuffer.resize(mDataLength);
+    bool result = mFilterMQ->read(mDataOutputBuffer.data(), mDataLength);
+    EXPECT_TRUE(result) << "can't read from Filter MQ";
+
+    if (result) {
+        mFilterMQEventFlag->wake(static_cast<uint32_t>(DemuxQueueNotifyBits::DATA_CONSUMED));
+    }
+
+    for (int i = 0; i < mDataLength; i++) {
+        EXPECT_TRUE(goldenDataInputBuffer[i] == mDataOutputBuffer[i]) << "data does not match";
+    }
+}
+
 // Test environment for Tuner HIDL HAL.
 class TunerHidlEnvironment : public ::testing::VtsHalHidlTargetTestEnvBase {
   public:
@@ -154,7 +340,10 @@
     sp<FrontendCallback> mFrontendCallback;
     sp<IDescrambler> mDescrambler;
     sp<IDemux> mDemux;
+    sp<DemuxCallback> mDemuxCallback;
+    FilterMQDesc mFilterMQDescriptor;
     uint32_t mDemuxId;
+    uint32_t mFilterId;
 
     ::testing::AssertionResult createFrontend(int32_t frontendId);
     ::testing::AssertionResult tuneFrontend(int32_t frontendId);
@@ -162,6 +351,11 @@
     ::testing::AssertionResult closeFrontend(int32_t frontendId);
     ::testing::AssertionResult createDemux();
     ::testing::AssertionResult createDemuxWithFrontend(int32_t frontendId);
+    ::testing::AssertionResult addSectionFilterToDemux();
+    ::testing::AssertionResult addPesFilterToDemux();
+    ::testing::AssertionResult getFilterMQDescriptor(sp<IDemux>& demux, const uint32_t filterId);
+    ::testing::AssertionResult readSectionFilterDataOutput();
+    ::testing::AssertionResult readPesFilterDataOutput();
     ::testing::AssertionResult closeDemux();
     ::testing::AssertionResult createDescrambler();
     ::testing::AssertionResult closeDescrambler();
@@ -256,6 +450,104 @@
     return ::testing::AssertionResult(status == Result::SUCCESS);
 }
 
+::testing::AssertionResult TunerHidlTest::addSectionFilterToDemux() {
+    Result status;
+
+    if (createDemux() == ::testing::AssertionFailure()) {
+        return ::testing::AssertionFailure();
+    }
+
+    // Create demux callback
+    mDemuxCallback = new DemuxCallback();
+
+    // Add section filter to the local demux
+    mDemux->addFilter(DemuxFilterType::SECTION, FMQ_SIZE_4K, mDemuxCallback,
+                      [&](Result result, uint32_t filterId) {
+                          mFilterId = filterId;
+                          status = result;
+                      });
+
+    // Add another section filter to the local demux
+    mDemux->addFilter(DemuxFilterType::SECTION, FMQ_SIZE_4K, mDemuxCallback,
+                      [&](Result result, uint32_t filterId) {
+                          mFilterId = filterId;
+                          status = result;
+                      });
+
+    // TODO Test configure the filter
+
+    return ::testing::AssertionResult(status == Result::SUCCESS);
+}
+
+::testing::AssertionResult TunerHidlTest::addPesFilterToDemux() {
+    Result status;
+
+    if (createDemux() == ::testing::AssertionFailure()) {
+        return ::testing::AssertionFailure();
+    }
+
+    // Create demux callback
+    mDemuxCallback = new DemuxCallback();
+
+    // Add PES filter to the local demux
+    mDemux->addFilter(DemuxFilterType::PES, FMQ_SIZE_4K, mDemuxCallback,
+                      [&](Result result, uint32_t filterId) {
+                          mFilterId = filterId;
+                          status = result;
+                      });
+
+    // Add another PES filter to the local demux
+    mDemux->addFilter(DemuxFilterType::PES, FMQ_SIZE_4K, mDemuxCallback,
+                      [&](Result result, uint32_t filterId) {
+                          mFilterId = filterId;
+                          status = result;
+                      });
+
+    // TODO Test configure the filter
+
+    return ::testing::AssertionResult(status == Result::SUCCESS);
+}
+
+::testing::AssertionResult TunerHidlTest::getFilterMQDescriptor(sp<IDemux>& demux,
+                                                                const uint32_t filterId) {
+    Result status;
+
+    if (!demux) {
+        return ::testing::AssertionFailure();
+    }
+
+    mDemux->getFilterQueueDesc(filterId, [&](Result result, const FilterMQDesc& filterMQDesc) {
+        mFilterMQDescriptor = filterMQDesc;
+        status = result;
+    });
+
+    return ::testing::AssertionResult(status == Result::SUCCESS);
+}
+
+::testing::AssertionResult TunerHidlTest::readSectionFilterDataOutput() {
+    if (addSectionFilterToDemux() == ::testing::AssertionFailure() ||
+        getFilterMQDescriptor(mDemux, mFilterId) == ::testing::AssertionFailure()) {
+        return ::testing::AssertionFailure();
+    }
+
+    // Test start filter and read the output data
+    mDemuxCallback->testOnSectionFilterEvent(mDemux, mFilterId, mFilterMQDescriptor);
+
+    return ::testing::AssertionResult(true);
+}
+
+::testing::AssertionResult TunerHidlTest::readPesFilterDataOutput() {
+    if (addPesFilterToDemux() == ::testing::AssertionFailure() ||
+        getFilterMQDescriptor(mDemux, mFilterId) == ::testing::AssertionFailure()) {
+        return ::testing::AssertionFailure();
+    }
+
+    // Test start filter and read the output data
+    mDemuxCallback->testOnPesFilterEvent(mDemux, mFilterId, mFilterMQDescriptor);
+
+    return ::testing::AssertionResult(true);
+}
+
 ::testing::AssertionResult TunerHidlTest::closeDemux() {
     Result status;
     if (createDemux() == ::testing::AssertionFailure()) {
@@ -407,6 +699,32 @@
     }
 }
 
+TEST_F(TunerHidlTest, AddSectionFilterToDemux) {
+    description("Add a section filter to a created demux");
+    ASSERT_TRUE(addSectionFilterToDemux());
+}
+
+TEST_F(TunerHidlTest, AddPesFilterToDemux) {
+    description("Add a pes filter to a created demux");
+    ASSERT_TRUE(addPesFilterToDemux());
+}
+
+TEST_F(TunerHidlTest, GetFilterMQDescriptor) {
+    description("Get MQ Descriptor from a created filter");
+    ASSERT_TRUE(addSectionFilterToDemux());
+    ASSERT_TRUE(getFilterMQDescriptor(mDemux, mFilterId));
+}
+
+TEST_F(TunerHidlTest, ReadSectionFilterOutput) {
+    description("Read data output from FMQ of a Section Filter");
+    ASSERT_TRUE(readSectionFilterDataOutput());
+}
+
+TEST_F(TunerHidlTest, ReadPesFilterOutput) {
+    description("Read data output from FMQ of a PES Filter");
+    ASSERT_TRUE(readPesFilterDataOutput());
+}
+
 TEST_F(TunerHidlTest, CloseDemux) {
     description("Close Demux");