OSDN Git Service

supplicant(vts): Remove start/stop framework
[android-x86/hardware-interfaces.git] / media / omx / 1.0 / vts / functional / video / VtsHalMediaOmxV1_0TargetVideoDecTest.cpp
1 /*
2  * Copyright (C) 2017 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 #define LOG_TAG "media_omx_hidl_video_dec_test"
18 #include <android-base/logging.h>
19
20 #include <android/hardware/graphics/allocator/2.0/IAllocator.h>
21 #include <android/hardware/graphics/mapper/2.0/IMapper.h>
22 #include <android/hardware/graphics/mapper/2.0/types.h>
23 #include <android/hardware/media/omx/1.0/IOmx.h>
24 #include <android/hardware/media/omx/1.0/IOmxNode.h>
25 #include <android/hardware/media/omx/1.0/IOmxObserver.h>
26 #include <android/hardware/media/omx/1.0/types.h>
27 #include <android/hidl/allocator/1.0/IAllocator.h>
28 #include <android/hidl/memory/1.0/IMapper.h>
29 #include <android/hidl/memory/1.0/IMemory.h>
30
31 using ::android::hardware::graphics::common::V1_0::BufferUsage;
32 using ::android::hardware::graphics::common::V1_0::PixelFormat;
33 using ::android::hardware::media::omx::V1_0::IOmx;
34 using ::android::hardware::media::omx::V1_0::IOmxObserver;
35 using ::android::hardware::media::omx::V1_0::IOmxNode;
36 using ::android::hardware::media::omx::V1_0::Message;
37 using ::android::hardware::media::omx::V1_0::CodecBuffer;
38 using ::android::hardware::media::omx::V1_0::PortMode;
39 using ::android::hidl::allocator::V1_0::IAllocator;
40 using ::android::hidl::memory::V1_0::IMemory;
41 using ::android::hidl::memory::V1_0::IMapper;
42 using ::android::hardware::Return;
43 using ::android::hardware::Void;
44 using ::android::hardware::hidl_vec;
45 using ::android::hardware::hidl_string;
46 using ::android::sp;
47
48 #include <VtsHalHidlTargetTestBase.h>
49 #include <getopt.h>
50 #include <media_hidl_test_common.h>
51 #include <media_video_hidl_test_common.h>
52 #include <fstream>
53
54 // A class for test environment setup
55 class ComponentTestEnvironment : public ::testing::Environment {
56    public:
57     virtual void SetUp() {}
58     virtual void TearDown() {}
59
60     ComponentTestEnvironment() : instance("default"), res("/sdcard/media/") {}
61
62     void setInstance(const char* _instance) { instance = _instance; }
63
64     void setComponent(const char* _component) { component = _component; }
65
66     void setRole(const char* _role) { role = _role; }
67
68     void setRes(const char* _res) { res = _res; }
69
70     const hidl_string getInstance() const { return instance; }
71
72     const hidl_string getComponent() const { return component; }
73
74     const hidl_string getRole() const { return role; }
75
76     const hidl_string getRes() const { return res; }
77
78     int initFromOptions(int argc, char** argv) {
79         static struct option options[] = {
80             {"instance", required_argument, 0, 'I'},
81             {"component", required_argument, 0, 'C'},
82             {"role", required_argument, 0, 'R'},
83             {"res", required_argument, 0, 'P'},
84             {0, 0, 0, 0}};
85
86         while (true) {
87             int index = 0;
88             int c = getopt_long(argc, argv, "I:C:R:P:", options, &index);
89             if (c == -1) {
90                 break;
91             }
92
93             switch (c) {
94                 case 'I':
95                     setInstance(optarg);
96                     break;
97                 case 'C':
98                     setComponent(optarg);
99                     break;
100                 case 'R':
101                     setRole(optarg);
102                     break;
103                 case 'P':
104                     setRes(optarg);
105                     break;
106                 case '?':
107                     break;
108             }
109         }
110
111         if (optind < argc) {
112             fprintf(stderr,
113                     "unrecognized option: %s\n\n"
114                     "usage: %s <gtest options> <test options>\n\n"
115                     "test options are:\n\n"
116                     "-I, --instance: HAL instance to test\n"
117                     "-C, --component: OMX component to test\n"
118                     "-R, --role: OMX component Role\n"
119                     "-P, --res: Resource files directory location\n",
120                     argv[optind ?: 1], argv[0]);
121             return 2;
122         }
123         return 0;
124     }
125
126    private:
127     hidl_string instance;
128     hidl_string component;
129     hidl_string role;
130     hidl_string res;
131 };
132
133 static ComponentTestEnvironment* gEnv = nullptr;
134
135 // video decoder test fixture class
136 class VideoDecHidlTest : public ::testing::VtsHalHidlTargetTestBase {
137    private:
138     typedef ::testing::VtsHalHidlTargetTestBase Super;
139    public:
140     ::std::string getTestCaseInfo() const override {
141         return ::std::string() +
142                 "Component: " + gEnv->getComponent().c_str() + " | " +
143                 "Role: " + gEnv->getRole().c_str() + " | " +
144                 "Instance: " + gEnv->getInstance().c_str() + " | " +
145                 "Res: " + gEnv->getRes().c_str();
146     }
147
148     virtual void SetUp() override {
149         Super::SetUp();
150         disableTest = false;
151         android::hardware::media::omx::V1_0::Status status;
152         omx = Super::getService<IOmx>(gEnv->getInstance());
153         ASSERT_NE(omx, nullptr);
154         observer =
155             new CodecObserver([this](Message msg, const BufferInfo* buffer) {
156                 handleMessage(msg, buffer);
157             });
158         ASSERT_NE(observer, nullptr);
159         if (strncmp(gEnv->getComponent().c_str(), "OMX.", 4) != 0)
160             disableTest = true;
161         EXPECT_TRUE(omx->allocateNode(
162                            gEnv->getComponent(), observer,
163                            [&](android::hardware::media::omx::V1_0::Status _s,
164                                sp<IOmxNode> const& _nl) {
165                                status = _s;
166                                this->omxNode = _nl;
167                            })
168                         .isOk());
169         ASSERT_NE(omxNode, nullptr);
170         ASSERT_NE(gEnv->getRole().empty(), true) << "Invalid Component Role";
171         struct StringToName {
172             const char* Name;
173             standardComp CompName;
174         };
175         const StringToName kStringToName[] = {
176             {"h263", h263}, {"avc", avc}, {"mpeg2", mpeg2}, {"mpeg4", mpeg4},
177             {"hevc", hevc}, {"vp8", vp8}, {"vp9", vp9},
178         };
179         const size_t kNumStringToName =
180             sizeof(kStringToName) / sizeof(kStringToName[0]);
181         const char* pch;
182         char substring[OMX_MAX_STRINGNAME_SIZE];
183         strcpy(substring, gEnv->getRole().c_str());
184         pch = strchr(substring, '.');
185         ASSERT_NE(pch, nullptr);
186         compName = unknown_comp;
187         for (size_t i = 0; i < kNumStringToName; ++i) {
188             if (!strcasecmp(pch + 1, kStringToName[i].Name)) {
189                 compName = kStringToName[i].CompName;
190                 break;
191             }
192         }
193         if (compName == unknown_comp) disableTest = true;
194         struct CompToCompression {
195             standardComp CompName;
196             OMX_VIDEO_CODINGTYPE eCompressionFormat;
197         };
198         static const CompToCompression kCompToCompression[] = {
199             {h263, OMX_VIDEO_CodingH263},   {avc, OMX_VIDEO_CodingAVC},
200             {mpeg2, OMX_VIDEO_CodingMPEG2}, {mpeg4, OMX_VIDEO_CodingMPEG4},
201             {hevc, OMX_VIDEO_CodingHEVC},   {vp8, OMX_VIDEO_CodingVP8},
202             {vp9, OMX_VIDEO_CodingVP9},
203         };
204         static const size_t kNumCompToCompression =
205             sizeof(kCompToCompression) / sizeof(kCompToCompression[0]);
206         size_t i;
207         for (i = 0; i < kNumCompToCompression; ++i) {
208             if (kCompToCompression[i].CompName == compName) {
209                 eCompressionFormat = kCompToCompression[i].eCompressionFormat;
210                 break;
211             }
212         }
213         if (i == kNumCompToCompression) disableTest = true;
214         portMode[0] = portMode[1] = PortMode::PRESET_BYTE_BUFFER;
215         eosFlag = false;
216         framesReceived = 0;
217         timestampUs = 0;
218         timestampDevTest = false;
219         isSecure = false;
220         size_t suffixLen = strlen(".secure");
221         if (strlen(gEnv->getComponent().c_str()) >= suffixLen) {
222             isSecure =
223                 !strcmp(gEnv->getComponent().c_str() +
224                             strlen(gEnv->getComponent().c_str()) - suffixLen,
225                         ".secure");
226         }
227         if (isSecure) disableTest = true;
228         if (disableTest) std::cout << "[          ] Warning !  Test Disabled\n";
229     }
230
231     virtual void TearDown() override {
232         if (omxNode != nullptr) {
233             EXPECT_TRUE((omxNode->freeNode()).isOk());
234             omxNode = nullptr;
235         }
236         Super::TearDown();
237     }
238
239     // callback function to process messages received by onMessages() from IL
240     // client.
241     void handleMessage(Message msg, const BufferInfo* buffer) {
242         (void)buffer;
243         if (msg.type == Message::Type::FILL_BUFFER_DONE) {
244             if (msg.data.extendedBufferData.flags & OMX_BUFFERFLAG_EOS) {
245                 eosFlag = true;
246             }
247             if (msg.data.extendedBufferData.rangeLength != 0) {
248                 framesReceived += 1;
249                 // For decoder components current timestamp always exceeds
250                 // previous timestamp
251                 EXPECT_GE(msg.data.extendedBufferData.timestampUs, timestampUs);
252                 timestampUs = msg.data.extendedBufferData.timestampUs;
253                 // Test if current timestamp is among the list of queued
254                 // timestamps
255                 if (timestampDevTest) {
256                     bool tsHit = false;
257                     android::List<uint64_t>::iterator it =
258                         timestampUslist.begin();
259                     while (it != timestampUslist.end()) {
260                         if (*it == timestampUs) {
261                             timestampUslist.erase(it);
262                             tsHit = true;
263                             break;
264                         }
265                         it++;
266                     }
267                     if (tsHit == false) {
268                         if (timestampUslist.empty() == false) {
269                             EXPECT_EQ(tsHit, true)
270                                 << "TimeStamp not recognized";
271                         } else {
272                             std::cout
273                                 << "[          ] Warning ! Received non-zero "
274                                    "output / TimeStamp not recognized \n";
275                         }
276                     }
277                 }
278 #define WRITE_OUTPUT 0
279 #if WRITE_OUTPUT
280                 static int count = 0;
281                 FILE* ofp = nullptr;
282                 if (count)
283                     ofp = fopen("out.bin", "ab");
284                 else
285                     ofp = fopen("out.bin", "wb");
286                 if (ofp != nullptr &&
287                     portMode[1] == PortMode::PRESET_BYTE_BUFFER) {
288                     fwrite(static_cast<void*>(buffer->mMemory->getPointer()),
289                            sizeof(char),
290                            msg.data.extendedBufferData.rangeLength, ofp);
291                     fclose(ofp);
292                     count++;
293                 }
294 #endif
295             }
296         }
297     }
298
299     enum standardComp {
300         h263,
301         avc,
302         mpeg2,
303         mpeg4,
304         hevc,
305         vp8,
306         vp9,
307         unknown_comp,
308     };
309
310     sp<IOmx> omx;
311     sp<CodecObserver> observer;
312     sp<IOmxNode> omxNode;
313     standardComp compName;
314     OMX_VIDEO_CODINGTYPE eCompressionFormat;
315     bool disableTest;
316     PortMode portMode[2];
317     bool eosFlag;
318     uint32_t framesReceived;
319     uint64_t timestampUs;
320     ::android::List<uint64_t> timestampUslist;
321     bool timestampDevTest;
322     bool isSecure;
323
324    protected:
325     static void description(const std::string& description) {
326         RecordProperty("description", description);
327     }
328 };
329
330 // Set Default port param.
331 void setDefaultPortParam(sp<IOmxNode> omxNode, OMX_U32 portIndex,
332                          OMX_VIDEO_CODINGTYPE eCompressionFormat,
333                          OMX_COLOR_FORMATTYPE eColorFormat,
334                          OMX_U32 nFrameWidth = 352, OMX_U32 nFrameHeight = 288,
335                          OMX_U32 nBitrate = 0,
336                          OMX_U32 xFramerate = (24U << 16)) {
337     switch ((int)eCompressionFormat) {
338         case OMX_VIDEO_CodingUnused:
339             setupRAWPort(omxNode, portIndex, nFrameWidth, nFrameHeight,
340                          nBitrate, xFramerate, eColorFormat);
341             break;
342         default:
343             break;
344     }
345 }
346
347 // In decoder components, often the input port parameters get updated upon
348 // parsing the header of elementary stream. Client needs to collect this
349 // information to reconfigure other ports that share data with this input
350 // port.
351 void getInputChannelInfo(sp<IOmxNode> omxNode, OMX_U32 kPortIndexInput,
352                          uint32_t* nFrameWidth, uint32_t* nFrameHeight,
353                          uint32_t* xFramerate) {
354     android::hardware::media::omx::V1_0::Status status;
355     *nFrameWidth = 352;
356     *nFrameHeight = 288;
357     *xFramerate = (24U << 16);
358
359     OMX_PARAM_PORTDEFINITIONTYPE portDef;
360     status = getPortParam(omxNode, OMX_IndexParamPortDefinition,
361                           kPortIndexInput, &portDef);
362     EXPECT_EQ(status, ::android::hardware::media::omx::V1_0::Status::OK);
363     if (status == ::android::hardware::media::omx::V1_0::Status::OK) {
364         *nFrameWidth = portDef.format.video.nFrameWidth;
365         *nFrameHeight = portDef.format.video.nFrameHeight;
366         *xFramerate = portDef.format.video.xFramerate;
367     }
368 }
369
370 // LookUpTable of clips and metadata for component testing
371 void GetURLForComponent(VideoDecHidlTest::standardComp comp, char* mURL,
372                         char* info) {
373     struct CompToURL {
374         VideoDecHidlTest::standardComp comp;
375         const char* mURL;
376         const char* info;
377     };
378     static const CompToURL kCompToURL[] = {
379         {VideoDecHidlTest::standardComp::avc,
380          "bbb_avc_1920x1080_5000kbps_30fps.h264",
381          "bbb_avc_1920x1080_5000kbps_30fps.info"},
382         {VideoDecHidlTest::standardComp::hevc,
383          "bbb_hevc_640x360_1600kbps_30fps.hevc",
384          "bbb_hevc_640x360_1600kbps_30fps.info"},
385         {VideoDecHidlTest::standardComp::mpeg2,
386          "bbb_mpeg2_176x144_105kbps_25fps.m2v",
387          "bbb_mpeg2_176x144_105kbps_25fps.info"},
388         {VideoDecHidlTest::standardComp::h263,
389          "bbb_h263_352x288_300kbps_12fps.h263",
390          "bbb_h263_352x288_300kbps_12fps.info"},
391         {VideoDecHidlTest::standardComp::mpeg4,
392          "bbb_mpeg4_1280x720_1000kbps_25fps.m4v",
393          "bbb_mpeg4_1280x720_1000kbps_25fps.info"},
394         {VideoDecHidlTest::standardComp::vp8, "bbb_vp8_640x360_2mbps_30fps.vp8",
395          "bbb_vp8_640x360_2mbps_30fps.info"},
396         {VideoDecHidlTest::standardComp::vp9,
397          "bbb_vp9_640x360_1600kbps_30fps.vp9",
398          "bbb_vp9_640x360_1600kbps_30fps.info"},
399     };
400
401     for (size_t i = 0; i < sizeof(kCompToURL) / sizeof(kCompToURL[0]); ++i) {
402         if (kCompToURL[i].comp == comp) {
403             strcat(mURL, kCompToURL[i].mURL);
404             strcat(info, kCompToURL[i].info);
405             return;
406         }
407     }
408 }
409
410 void allocateGraphicBuffers(sp<IOmxNode> omxNode, OMX_U32 portIndex,
411                             android::Vector<BufferInfo>* buffArray,
412                             uint32_t nFrameWidth, uint32_t nFrameHeight,
413                             int32_t* nStride, uint32_t count) {
414     android::hardware::media::omx::V1_0::Status status;
415     sp<android::hardware::graphics::allocator::V2_0::IAllocator> allocator =
416         android::hardware::graphics::allocator::V2_0::IAllocator::getService();
417     ASSERT_NE(nullptr, allocator.get());
418
419     sp<android::hardware::graphics::mapper::V2_0::IMapper> mapper =
420         android::hardware::graphics::mapper::V2_0::IMapper::getService();
421     ASSERT_NE(mapper.get(), nullptr);
422
423     android::hardware::graphics::mapper::V2_0::IMapper::BufferDescriptorInfo
424         descriptorInfo;
425     uint32_t usage;
426
427     descriptorInfo.width = nFrameWidth;
428     descriptorInfo.height = nFrameHeight;
429     descriptorInfo.layerCount = 1;
430     descriptorInfo.format = PixelFormat::RGBA_8888;
431     descriptorInfo.usage = static_cast<uint64_t>(BufferUsage::CPU_READ_OFTEN);
432     omxNode->getGraphicBufferUsage(
433         portIndex,
434         [&status, &usage](android::hardware::media::omx::V1_0::Status _s,
435                           uint32_t _n1) {
436             status = _s;
437             usage = _n1;
438         });
439     if (status == android::hardware::media::omx::V1_0::Status::OK) {
440         descriptorInfo.usage |= usage;
441     }
442
443     ::android::hardware::hidl_vec<uint32_t> descriptor;
444     android::hardware::graphics::mapper::V2_0::Error error;
445     mapper->createDescriptor(
446         descriptorInfo, [&error, &descriptor](
447                             android::hardware::graphics::mapper::V2_0::Error _s,
448                             ::android::hardware::hidl_vec<uint32_t> _n1) {
449             error = _s;
450             descriptor = _n1;
451         });
452     EXPECT_EQ(error, android::hardware::graphics::mapper::V2_0::Error::NONE);
453
454     EXPECT_EQ(buffArray->size(), count);
455     allocator->allocate(
456         descriptor, count,
457         [&](android::hardware::graphics::mapper::V2_0::Error _s, uint32_t _n1,
458             const ::android::hardware::hidl_vec<
459                 ::android::hardware::hidl_handle>& _n2) {
460             ASSERT_EQ(android::hardware::graphics::mapper::V2_0::Error::NONE,
461                       _s);
462             *nStride = _n1;
463             ASSERT_EQ(count, _n2.size());
464             for (uint32_t i = 0; i < count; i++) {
465                 buffArray->editItemAt(i).omxBuffer.nativeHandle = _n2[i];
466                 buffArray->editItemAt(i).omxBuffer.attr.anwBuffer.width =
467                     nFrameWidth;
468                 buffArray->editItemAt(i).omxBuffer.attr.anwBuffer.height =
469                     nFrameHeight;
470                 buffArray->editItemAt(i).omxBuffer.attr.anwBuffer.stride = _n1;
471                 buffArray->editItemAt(i).omxBuffer.attr.anwBuffer.format =
472                     descriptorInfo.format;
473                 buffArray->editItemAt(i).omxBuffer.attr.anwBuffer.usage =
474                     descriptorInfo.usage;
475                 buffArray->editItemAt(i).omxBuffer.attr.anwBuffer.layerCount =
476                     descriptorInfo.layerCount;
477                 buffArray->editItemAt(i).omxBuffer.attr.anwBuffer.id =
478                     (*buffArray)[i].id;
479             }
480         });
481 }
482
483 // port settings reconfiguration during runtime. reconfigures frame dimensions
484 void portReconfiguration(sp<IOmxNode> omxNode, sp<CodecObserver> observer,
485                          android::Vector<BufferInfo>* iBuffer,
486                          android::Vector<BufferInfo>* oBuffer,
487                          OMX_U32 kPortIndexInput, OMX_U32 kPortIndexOutput,
488                          Message msg, PortMode oPortMode) {
489     android::hardware::media::omx::V1_0::Status status;
490
491     if (msg.data.eventData.event == OMX_EventPortSettingsChanged) {
492         ASSERT_EQ(msg.data.eventData.data1, kPortIndexOutput);
493         if (msg.data.eventData.data2 == OMX_IndexParamPortDefinition ||
494             msg.data.eventData.data2 == 0) {
495             status = omxNode->sendCommand(
496                 toRawCommandType(OMX_CommandPortDisable), kPortIndexOutput);
497             ASSERT_EQ(status, android::hardware::media::omx::V1_0::Status::OK);
498
499             status = observer->dequeueMessage(&msg, DEFAULT_TIMEOUT, iBuffer,
500                                               oBuffer);
501             if (status ==
502                 android::hardware::media::omx::V1_0::Status::TIMED_OUT) {
503                 for (size_t i = 0; i < oBuffer->size(); ++i) {
504                     // test if client got all its buffers back
505                     EXPECT_EQ((*oBuffer)[i].owner, client);
506                     // free the buffers
507                     status =
508                         omxNode->freeBuffer(kPortIndexOutput, (*oBuffer)[i].id);
509                     ASSERT_EQ(status,
510                               android::hardware::media::omx::V1_0::Status::OK);
511                 }
512                 status = observer->dequeueMessage(&msg, DEFAULT_TIMEOUT,
513                                                   iBuffer, oBuffer);
514                 ASSERT_EQ(status,
515                           android::hardware::media::omx::V1_0::Status::OK);
516                 ASSERT_EQ(msg.type, Message::Type::EVENT);
517                 ASSERT_EQ(msg.data.eventData.event, OMX_EventCmdComplete);
518                 ASSERT_EQ(msg.data.eventData.data1, OMX_CommandPortDisable);
519                 ASSERT_EQ(msg.data.eventData.data2, kPortIndexOutput);
520
521                 // set Port Params
522                 uint32_t nFrameWidth, nFrameHeight, xFramerate;
523                 OMX_COLOR_FORMATTYPE eColorFormat =
524                     OMX_COLOR_FormatYUV420Planar;
525                 getInputChannelInfo(omxNode, kPortIndexInput, &nFrameWidth,
526                                     &nFrameHeight, &xFramerate);
527                 setDefaultPortParam(omxNode, kPortIndexOutput,
528                                     OMX_VIDEO_CodingUnused, eColorFormat,
529                                     nFrameWidth, nFrameHeight, 0, xFramerate);
530
531                 // If you can disable a port, then you should be able to
532                 // enable
533                 // it as well
534                 status = omxNode->sendCommand(
535                     toRawCommandType(OMX_CommandPortEnable), kPortIndexOutput);
536                 ASSERT_EQ(status,
537                           android::hardware::media::omx::V1_0::Status::OK);
538
539                 // do not enable the port until all the buffers are supplied
540                 status = observer->dequeueMessage(&msg, DEFAULT_TIMEOUT,
541                                                   iBuffer, oBuffer);
542                 ASSERT_EQ(
543                     status,
544                     android::hardware::media::omx::V1_0::Status::TIMED_OUT);
545
546                 allocatePortBuffers(omxNode, oBuffer, kPortIndexOutput,
547                                     oPortMode);
548                 if (oPortMode != PortMode::PRESET_BYTE_BUFFER) {
549                     OMX_PARAM_PORTDEFINITIONTYPE portDef;
550
551                     status = getPortParam(omxNode, OMX_IndexParamPortDefinition,
552                                           kPortIndexOutput, &portDef);
553                     ASSERT_EQ(
554                         status,
555                         ::android::hardware::media::omx::V1_0::Status::OK);
556                     allocateGraphicBuffers(omxNode, kPortIndexOutput, oBuffer,
557                                            portDef.format.video.nFrameWidth,
558                                            portDef.format.video.nFrameHeight,
559                                            &portDef.format.video.nStride,
560                                            portDef.nBufferCountActual);
561                 }
562                 status = observer->dequeueMessage(&msg, DEFAULT_TIMEOUT,
563                                                   iBuffer, oBuffer);
564                 ASSERT_EQ(status,
565                           android::hardware::media::omx::V1_0::Status::OK);
566                 ASSERT_EQ(msg.type, Message::Type::EVENT);
567                 ASSERT_EQ(msg.data.eventData.data1, OMX_CommandPortEnable);
568                 ASSERT_EQ(msg.data.eventData.data2, kPortIndexOutput);
569
570                 // dispatch output buffers
571                 for (size_t i = 0; i < oBuffer->size(); i++) {
572                     dispatchOutputBuffer(omxNode, oBuffer, i, oPortMode);
573                 }
574             } else {
575                 ASSERT_TRUE(false);
576             }
577         } else if (msg.data.eventData.data2 ==
578                    OMX_IndexConfigCommonOutputCrop) {
579             std::cout << "[          ] Warning ! OMX_EventPortSettingsChanged/ "
580                          "OMX_IndexConfigCommonOutputCrop not handled \n";
581         } else if (msg.data.eventData.data2 == OMX_IndexVendorStartUnused + 3) {
582             std::cout << "[          ] Warning ! OMX_EventPortSettingsChanged/ "
583                          "kDescribeColorAspectsIndex not handled \n";
584         }
585     } else if (msg.data.eventData.event == OMX_EventError) {
586         std::cout << "[          ] Warning ! OMX_EventError/ "
587                      "Decode Frame Call might be failed \n";
588         return;
589     } else if (msg.data.eventData.event == OMX_EventBufferFlag) {
590         // soft omx components donot send this, we will just ignore it
591         // for now
592     } else {
593         // something unexpected happened
594         ASSERT_TRUE(false);
595     }
596 }
597
598 // blocking call to ensures application to Wait till all the inputs are consumed
599 void waitOnInputConsumption(sp<IOmxNode> omxNode, sp<CodecObserver> observer,
600                             android::Vector<BufferInfo>* iBuffer,
601                             android::Vector<BufferInfo>* oBuffer,
602                             OMX_U32 kPortIndexInput, OMX_U32 kPortIndexOutput,
603                             PortMode oPortMode) {
604     android::hardware::media::omx::V1_0::Status status;
605     Message msg;
606     int timeOut = TIMEOUT_COUNTER;
607
608     while (timeOut--) {
609         size_t i = 0;
610         status =
611             observer->dequeueMessage(&msg, DEFAULT_TIMEOUT, iBuffer, oBuffer);
612         if (status == android::hardware::media::omx::V1_0::Status::OK) {
613             EXPECT_EQ(msg.type, Message::Type::EVENT);
614             portReconfiguration(omxNode, observer, iBuffer, oBuffer,
615                                 kPortIndexInput, kPortIndexOutput, msg,
616                                 oPortMode);
617         }
618         // status == TIMED_OUT, it could be due to process time being large
619         // than DEFAULT_TIMEOUT or component needs output buffers to start
620         // processing.
621         for (; i < iBuffer->size(); i++) {
622             if ((*iBuffer)[i].owner != client) break;
623         }
624         if (i == iBuffer->size()) break;
625
626         // Dispatch an output buffer assuming outQueue.empty() is true
627         size_t index;
628         if ((index = getEmptyBufferID(oBuffer)) < oBuffer->size()) {
629             dispatchOutputBuffer(omxNode, oBuffer, index, oPortMode);
630         }
631         timeOut--;
632     }
633 }
634
635 // Decode N Frames
636 void decodeNFrames(sp<IOmxNode> omxNode, sp<CodecObserver> observer,
637                    android::Vector<BufferInfo>* iBuffer,
638                    android::Vector<BufferInfo>* oBuffer,
639                    OMX_U32 kPortIndexInput, OMX_U32 kPortIndexOutput,
640                    std::ifstream& eleStream, android::Vector<FrameData>* Info,
641                    int offset, int range, PortMode oPortMode,
642                    bool signalEOS = true) {
643     android::hardware::media::omx::V1_0::Status status;
644     Message msg;
645
646     // dispatch output buffers
647     for (size_t i = 0; i < oBuffer->size(); i++) {
648         dispatchOutputBuffer(omxNode, oBuffer, i, oPortMode);
649     }
650     // dispatch input buffers
651     uint32_t flags = 0;
652     int frameID = offset;
653     for (size_t i = 0; (i < iBuffer->size()) && (frameID < (int)Info->size()) &&
654                        (frameID < (offset + range));
655          i++) {
656         char* ipBuffer = static_cast<char*>(
657             static_cast<void*>((*iBuffer)[i].mMemory->getPointer()));
658         ASSERT_LE((*Info)[frameID].bytesCount,
659                   static_cast<int>((*iBuffer)[i].mMemory->getSize()));
660         eleStream.read(ipBuffer, (*Info)[frameID].bytesCount);
661         ASSERT_EQ(eleStream.gcount(), (*Info)[frameID].bytesCount);
662         flags = (*Info)[frameID].flags;
663         if (signalEOS && ((frameID == (int)Info->size() - 1) ||
664                           (frameID == (offset + range - 1))))
665             flags |= OMX_BUFFERFLAG_EOS;
666         dispatchInputBuffer(omxNode, iBuffer, i, (*Info)[frameID].bytesCount,
667                             flags, (*Info)[frameID].timestamp);
668         frameID++;
669     }
670
671     int timeOut = TIMEOUT_COUNTER;
672     bool stall = false;
673     while (1) {
674         status =
675             observer->dequeueMessage(&msg, DEFAULT_TIMEOUT, iBuffer, oBuffer);
676
677         // Port Reconfiguration
678         if (status == android::hardware::media::omx::V1_0::Status::OK &&
679             msg.type == Message::Type::EVENT) {
680             portReconfiguration(omxNode, observer, iBuffer, oBuffer,
681                                 kPortIndexInput, kPortIndexOutput, msg,
682                                 oPortMode);
683         }
684
685         if (frameID == (int)Info->size() || frameID == (offset + range)) break;
686
687         // Dispatch input buffer
688         size_t index = 0;
689         if ((index = getEmptyBufferID(iBuffer)) < iBuffer->size()) {
690             char* ipBuffer = static_cast<char*>(
691                 static_cast<void*>((*iBuffer)[index].mMemory->getPointer()));
692             ASSERT_LE((*Info)[frameID].bytesCount,
693                       static_cast<int>((*iBuffer)[index].mMemory->getSize()));
694             eleStream.read(ipBuffer, (*Info)[frameID].bytesCount);
695             ASSERT_EQ(eleStream.gcount(), (*Info)[frameID].bytesCount);
696             flags = (*Info)[frameID].flags;
697             if (signalEOS && ((frameID == (int)Info->size() - 1) ||
698                               (frameID == (offset + range - 1))))
699                 flags |= OMX_BUFFERFLAG_EOS;
700             dispatchInputBuffer(omxNode, iBuffer, index,
701                                 (*Info)[frameID].bytesCount, flags,
702                                 (*Info)[frameID].timestamp);
703             frameID++;
704             stall = false;
705         } else
706             stall = true;
707         if ((index = getEmptyBufferID(oBuffer)) < oBuffer->size()) {
708             dispatchOutputBuffer(omxNode, oBuffer, index, oPortMode);
709             stall = false;
710         } else
711             stall = true;
712         if (stall)
713             timeOut--;
714         else
715             timeOut = TIMEOUT_COUNTER;
716         if (timeOut == 0) {
717             EXPECT_TRUE(false) << "Wait on Input/Output is found indefinite";
718             break;
719         }
720     }
721 }
722
723 // set component role
724 TEST_F(VideoDecHidlTest, SetRole) {
725     description("Test Set Component Role");
726     if (disableTest) return;
727     android::hardware::media::omx::V1_0::Status status;
728     status = setRole(omxNode, gEnv->getRole().c_str());
729     ASSERT_EQ(status, ::android::hardware::media::omx::V1_0::Status::OK);
730 }
731
732 // port format enumeration
733 TEST_F(VideoDecHidlTest, EnumeratePortFormat) {
734     description("Test Component on Mandatory Port Parameters (Port Format)");
735     if (disableTest) return;
736     android::hardware::media::omx::V1_0::Status status;
737     uint32_t kPortIndexInput = 0, kPortIndexOutput = 1;
738     OMX_COLOR_FORMATTYPE eColorFormat = OMX_COLOR_FormatYUV420Planar;
739     OMX_U32 xFramerate = (24U << 16);
740     status = setRole(omxNode, gEnv->getRole().c_str());
741     ASSERT_EQ(status, ::android::hardware::media::omx::V1_0::Status::OK);
742     OMX_PORT_PARAM_TYPE params;
743     status = getParam(omxNode, OMX_IndexParamVideoInit, &params);
744     if (status == ::android::hardware::media::omx::V1_0::Status::OK) {
745         ASSERT_EQ(params.nPorts, 2U);
746         kPortIndexInput = params.nStartPortNumber;
747         kPortIndexOutput = kPortIndexInput + 1;
748     }
749     status = setVideoPortFormat(omxNode, kPortIndexInput, eCompressionFormat,
750                                 OMX_COLOR_FormatUnused, 0U);
751     EXPECT_EQ(status, ::android::hardware::media::omx::V1_0::Status::OK);
752     status =
753         setVideoPortFormat(omxNode, kPortIndexOutput, OMX_VIDEO_CodingUnused,
754                            eColorFormat, xFramerate);
755     EXPECT_EQ(status, ::android::hardware::media::omx::V1_0::Status::OK);
756 }
757
758 // test port settings reconfiguration, elementary stream decode and timestamp
759 // deviation
760 TEST_F(VideoDecHidlTest, DecodeTest) {
761     description("Tests Port Reconfiguration, Decode and timestamp deviation");
762     if (disableTest) return;
763     android::hardware::media::omx::V1_0::Status status;
764     uint32_t kPortIndexInput = 0, kPortIndexOutput = 1;
765     status = setRole(omxNode, gEnv->getRole().c_str());
766     ASSERT_EQ(status, ::android::hardware::media::omx::V1_0::Status::OK);
767     OMX_PORT_PARAM_TYPE params;
768     status = getParam(omxNode, OMX_IndexParamVideoInit, &params);
769     if (status == ::android::hardware::media::omx::V1_0::Status::OK) {
770         ASSERT_EQ(params.nPorts, 2U);
771         kPortIndexInput = params.nStartPortNumber;
772         kPortIndexOutput = kPortIndexInput + 1;
773     }
774     char mURL[512], info[512];
775     strcpy(mURL, gEnv->getRes().c_str());
776     strcpy(info, gEnv->getRes().c_str());
777     GetURLForComponent(compName, mURL, info);
778
779     std::ifstream eleStream, eleInfo;
780
781     eleInfo.open(info);
782     ASSERT_EQ(eleInfo.is_open(), true);
783     android::Vector<FrameData> Info;
784     int bytesCount = 0;
785     uint32_t flags = 0;
786     uint32_t timestamp = 0;
787     timestampDevTest = true;
788     while (1) {
789         if (!(eleInfo >> bytesCount)) break;
790         eleInfo >> flags;
791         eleInfo >> timestamp;
792         Info.push_back({bytesCount, flags, timestamp});
793         if (flags != OMX_BUFFERFLAG_CODECCONFIG)
794             timestampUslist.push_back(timestamp);
795     }
796     eleInfo.close();
797
798     // set port mode
799     portMode[0] = PortMode::PRESET_BYTE_BUFFER;
800     portMode[1] = PortMode::DYNAMIC_ANW_BUFFER;
801     status = omxNode->setPortMode(kPortIndexInput, portMode[0]);
802     ASSERT_EQ(status, ::android::hardware::media::omx::V1_0::Status::OK);
803     status = omxNode->setPortMode(kPortIndexOutput, portMode[1]);
804     if (status != ::android::hardware::media::omx::V1_0::Status::OK) {
805         portMode[1] = PortMode::PRESET_BYTE_BUFFER;
806         status = omxNode->setPortMode(kPortIndexOutput, portMode[1]);
807         ASSERT_EQ(status, ::android::hardware::media::omx::V1_0::Status::OK);
808     }
809
810     // set Port Params
811     uint32_t nFrameWidth, nFrameHeight, xFramerate;
812     OMX_COLOR_FORMATTYPE eColorFormat = OMX_COLOR_FormatYUV420Planar;
813     getInputChannelInfo(omxNode, kPortIndexInput, &nFrameWidth, &nFrameHeight,
814                         &xFramerate);
815     setDefaultPortParam(omxNode, kPortIndexOutput, OMX_VIDEO_CodingUnused,
816                         eColorFormat, nFrameWidth, nFrameHeight, 0, xFramerate);
817     omxNode->prepareForAdaptivePlayback(kPortIndexOutput, false, 1920, 1080);
818
819     android::Vector<BufferInfo> iBuffer, oBuffer;
820
821     // set state to idle
822     changeStateLoadedtoIdle(omxNode, observer, &iBuffer, &oBuffer,
823                             kPortIndexInput, kPortIndexOutput, portMode);
824     // set state to executing
825     changeStateIdletoExecute(omxNode, observer);
826
827     if (portMode[1] != PortMode::PRESET_BYTE_BUFFER) {
828         OMX_PARAM_PORTDEFINITIONTYPE portDef;
829
830         status = getPortParam(omxNode, OMX_IndexParamPortDefinition,
831                               kPortIndexOutput, &portDef);
832         ASSERT_EQ(status, ::android::hardware::media::omx::V1_0::Status::OK);
833         allocateGraphicBuffers(
834             omxNode, kPortIndexOutput, &oBuffer,
835             portDef.format.video.nFrameWidth, portDef.format.video.nFrameHeight,
836             &portDef.format.video.nStride, portDef.nBufferCountActual);
837     }
838
839     // Port Reconfiguration
840     eleStream.open(mURL, std::ifstream::binary);
841     ASSERT_EQ(eleStream.is_open(), true);
842     decodeNFrames(omxNode, observer, &iBuffer, &oBuffer, kPortIndexInput,
843                   kPortIndexOutput, eleStream, &Info, 0, (int)Info.size(),
844                   portMode[1]);
845     eleStream.close();
846     waitOnInputConsumption(omxNode, observer, &iBuffer, &oBuffer,
847                            kPortIndexInput, kPortIndexOutput, portMode[1]);
848     testEOS(omxNode, observer, &iBuffer, &oBuffer, false, eosFlag, portMode);
849     EXPECT_EQ(timestampUslist.empty(), true);
850     // set state to idle
851     changeStateExecutetoIdle(omxNode, observer, &iBuffer, &oBuffer);
852     // set state to executing
853     changeStateIdletoLoaded(omxNode, observer, &iBuffer, &oBuffer,
854                             kPortIndexInput, kPortIndexOutput);
855 }
856
857 // end of sequence test
858 TEST_F(VideoDecHidlTest, EOSTest_M) {
859     description("Test End of stream monkeying");
860     if (disableTest) return;
861     android::hardware::media::omx::V1_0::Status status;
862     uint32_t kPortIndexInput = 0, kPortIndexOutput = 1;
863     status = setRole(omxNode, gEnv->getRole().c_str());
864     ASSERT_EQ(status, ::android::hardware::media::omx::V1_0::Status::OK);
865     OMX_PORT_PARAM_TYPE params;
866     status = getParam(omxNode, OMX_IndexParamVideoInit, &params);
867     if (status == ::android::hardware::media::omx::V1_0::Status::OK) {
868         ASSERT_EQ(params.nPorts, 2U);
869         kPortIndexInput = params.nStartPortNumber;
870         kPortIndexOutput = kPortIndexInput + 1;
871     }
872
873     // set Port Params
874     uint32_t nFrameWidth, nFrameHeight, xFramerate;
875     OMX_COLOR_FORMATTYPE eColorFormat = OMX_COLOR_FormatYUV420Planar;
876     getInputChannelInfo(omxNode, kPortIndexInput, &nFrameWidth, &nFrameHeight,
877                         &xFramerate);
878     setDefaultPortParam(omxNode, kPortIndexOutput, OMX_VIDEO_CodingUnused,
879                         eColorFormat, nFrameWidth, nFrameHeight, 0, xFramerate);
880
881     // set port mode
882     PortMode portMode[2];
883     portMode[0] = portMode[1] = PortMode::PRESET_BYTE_BUFFER;
884     status = omxNode->setPortMode(kPortIndexInput, portMode[0]);
885     ASSERT_EQ(status, ::android::hardware::media::omx::V1_0::Status::OK);
886     status = omxNode->setPortMode(kPortIndexOutput, portMode[1]);
887     ASSERT_EQ(status, ::android::hardware::media::omx::V1_0::Status::OK);
888
889     android::Vector<BufferInfo> iBuffer, oBuffer;
890
891     // set state to idle
892     changeStateLoadedtoIdle(omxNode, observer, &iBuffer, &oBuffer,
893                             kPortIndexInput, kPortIndexOutput, portMode);
894     // set state to executing
895     changeStateIdletoExecute(omxNode, observer);
896
897     // request EOS at the start
898     testEOS(omxNode, observer, &iBuffer, &oBuffer, true, eosFlag, portMode);
899     flushPorts(omxNode, observer, &iBuffer, &oBuffer, kPortIndexInput,
900                kPortIndexOutput);
901     EXPECT_GE(framesReceived, 0U);
902     framesReceived = 0;
903     timestampUs = 0;
904
905     // set state to idle
906     changeStateExecutetoIdle(omxNode, observer, &iBuffer, &oBuffer);
907     // set state to executing
908     changeStateIdletoLoaded(omxNode, observer, &iBuffer, &oBuffer,
909                             kPortIndexInput, kPortIndexOutput);
910 }
911
912 // end of sequence test
913 TEST_F(VideoDecHidlTest, ThumbnailTest) {
914     description("Test Request for thumbnail");
915     if (disableTest) return;
916     android::hardware::media::omx::V1_0::Status status;
917     uint32_t kPortIndexInput = 0, kPortIndexOutput = 1;
918     status = setRole(omxNode, gEnv->getRole().c_str());
919     ASSERT_EQ(status, ::android::hardware::media::omx::V1_0::Status::OK);
920     OMX_PORT_PARAM_TYPE params;
921     status = getParam(omxNode, OMX_IndexParamVideoInit, &params);
922     if (status == ::android::hardware::media::omx::V1_0::Status::OK) {
923         ASSERT_EQ(params.nPorts, 2U);
924         kPortIndexInput = params.nStartPortNumber;
925         kPortIndexOutput = kPortIndexInput + 1;
926     }
927     char mURL[512], info[512];
928     strcpy(mURL, gEnv->getRes().c_str());
929     strcpy(info, gEnv->getRes().c_str());
930     GetURLForComponent(compName, mURL, info);
931
932     std::ifstream eleStream, eleInfo;
933
934     eleInfo.open(info);
935     ASSERT_EQ(eleInfo.is_open(), true);
936     android::Vector<FrameData> Info;
937     int bytesCount = 0;
938     uint32_t flags = 0;
939     uint32_t timestamp = 0;
940     while (1) {
941         if (!(eleInfo >> bytesCount)) break;
942         eleInfo >> flags;
943         eleInfo >> timestamp;
944         Info.push_back({bytesCount, flags, timestamp});
945     }
946     eleInfo.close();
947
948     // set Port Params
949     uint32_t nFrameWidth, nFrameHeight, xFramerate;
950     OMX_COLOR_FORMATTYPE eColorFormat = OMX_COLOR_FormatYUV420Planar;
951     getInputChannelInfo(omxNode, kPortIndexInput, &nFrameWidth, &nFrameHeight,
952                         &xFramerate);
953     setDefaultPortParam(omxNode, kPortIndexOutput, OMX_VIDEO_CodingUnused,
954                         eColorFormat, nFrameWidth, nFrameHeight, 0, xFramerate);
955
956     // set port mode
957     PortMode portMode[2];
958     portMode[0] = portMode[1] = PortMode::PRESET_BYTE_BUFFER;
959     status = omxNode->setPortMode(kPortIndexInput, portMode[0]);
960     ASSERT_EQ(status, ::android::hardware::media::omx::V1_0::Status::OK);
961     status = omxNode->setPortMode(kPortIndexOutput, portMode[1]);
962     ASSERT_EQ(status, ::android::hardware::media::omx::V1_0::Status::OK);
963
964     android::Vector<BufferInfo> iBuffer, oBuffer;
965
966     // set state to idle
967     changeStateLoadedtoIdle(omxNode, observer, &iBuffer, &oBuffer,
968                             kPortIndexInput, kPortIndexOutput, portMode);
969     // set state to executing
970     changeStateIdletoExecute(omxNode, observer);
971
972     // request EOS for thumbnail
973     size_t i = 0;
974     while (!(Info[i].flags & OMX_BUFFERFLAG_SYNCFRAME)) i++;
975     eleStream.open(mURL, std::ifstream::binary);
976     ASSERT_EQ(eleStream.is_open(), true);
977     decodeNFrames(omxNode, observer, &iBuffer, &oBuffer, kPortIndexInput,
978                   kPortIndexOutput, eleStream, &Info, 0, i + 1, portMode[1]);
979     eleStream.close();
980     waitOnInputConsumption(omxNode, observer, &iBuffer, &oBuffer,
981                            kPortIndexInput, kPortIndexOutput, portMode[1]);
982     testEOS(omxNode, observer, &iBuffer, &oBuffer, false, eosFlag, portMode);
983     flushPorts(omxNode, observer, &iBuffer, &oBuffer, kPortIndexInput,
984                kPortIndexOutput);
985     EXPECT_GE(framesReceived, 1U);
986     framesReceived = 0;
987     timestampUs = 0;
988
989     eleStream.open(mURL, std::ifstream::binary);
990     ASSERT_EQ(eleStream.is_open(), true);
991     decodeNFrames(omxNode, observer, &iBuffer, &oBuffer, kPortIndexInput,
992                   kPortIndexOutput, eleStream, &Info, 0, i + 1, portMode[1],
993                   false);
994     eleStream.close();
995     waitOnInputConsumption(omxNode, observer, &iBuffer, &oBuffer,
996                            kPortIndexInput, kPortIndexOutput, portMode[1]);
997     testEOS(omxNode, observer, &iBuffer, &oBuffer, true, eosFlag, portMode);
998     flushPorts(omxNode, observer, &iBuffer, &oBuffer, kPortIndexInput,
999                kPortIndexOutput);
1000     EXPECT_GE(framesReceived, 1U);
1001     framesReceived = 0;
1002     timestampUs = 0;
1003
1004     // set state to idle
1005     changeStateExecutetoIdle(omxNode, observer, &iBuffer, &oBuffer);
1006     // set state to executing
1007     changeStateIdletoLoaded(omxNode, observer, &iBuffer, &oBuffer,
1008                             kPortIndexInput, kPortIndexOutput);
1009 }
1010
1011 // end of sequence test
1012 TEST_F(VideoDecHidlTest, SimpleEOSTest) {
1013     description("Test End of stream");
1014     if (disableTest) return;
1015     android::hardware::media::omx::V1_0::Status status;
1016     uint32_t kPortIndexInput = 0, kPortIndexOutput = 1;
1017     status = setRole(omxNode, gEnv->getRole().c_str());
1018     ASSERT_EQ(status, ::android::hardware::media::omx::V1_0::Status::OK);
1019     OMX_PORT_PARAM_TYPE params;
1020     status = getParam(omxNode, OMX_IndexParamVideoInit, &params);
1021     if (status == ::android::hardware::media::omx::V1_0::Status::OK) {
1022         ASSERT_EQ(params.nPorts, 2U);
1023         kPortIndexInput = params.nStartPortNumber;
1024         kPortIndexOutput = kPortIndexInput + 1;
1025     }
1026     char mURL[512], info[512];
1027     strcpy(mURL, gEnv->getRes().c_str());
1028     strcpy(info, gEnv->getRes().c_str());
1029     GetURLForComponent(compName, mURL, info);
1030
1031     std::ifstream eleStream, eleInfo;
1032
1033     eleInfo.open(info);
1034     ASSERT_EQ(eleInfo.is_open(), true);
1035     android::Vector<FrameData> Info;
1036     int bytesCount = 0;
1037     uint32_t flags = 0;
1038     uint32_t timestamp = 0;
1039     while (1) {
1040         if (!(eleInfo >> bytesCount)) break;
1041         eleInfo >> flags;
1042         eleInfo >> timestamp;
1043         Info.push_back({bytesCount, flags, timestamp});
1044     }
1045     eleInfo.close();
1046
1047     // set Port Params
1048     uint32_t nFrameWidth, nFrameHeight, xFramerate;
1049     OMX_COLOR_FORMATTYPE eColorFormat = OMX_COLOR_FormatYUV420Planar;
1050     getInputChannelInfo(omxNode, kPortIndexInput, &nFrameWidth, &nFrameHeight,
1051                         &xFramerate);
1052     setDefaultPortParam(omxNode, kPortIndexOutput, OMX_VIDEO_CodingUnused,
1053                         eColorFormat, nFrameWidth, nFrameHeight, 0, xFramerate);
1054
1055     // set port mode
1056     PortMode portMode[2];
1057     portMode[0] = portMode[1] = PortMode::PRESET_BYTE_BUFFER;
1058     status = omxNode->setPortMode(kPortIndexInput, portMode[0]);
1059     ASSERT_EQ(status, ::android::hardware::media::omx::V1_0::Status::OK);
1060     status = omxNode->setPortMode(kPortIndexOutput, portMode[1]);
1061     ASSERT_EQ(status, ::android::hardware::media::omx::V1_0::Status::OK);
1062
1063     android::Vector<BufferInfo> iBuffer, oBuffer;
1064
1065     // set state to idle
1066     changeStateLoadedtoIdle(omxNode, observer, &iBuffer, &oBuffer,
1067                             kPortIndexInput, kPortIndexOutput, portMode);
1068     // set state to executing
1069     changeStateIdletoExecute(omxNode, observer);
1070
1071     // request EOS at the end
1072     eleStream.open(mURL, std::ifstream::binary);
1073     ASSERT_EQ(eleStream.is_open(), true);
1074     decodeNFrames(omxNode, observer, &iBuffer, &oBuffer, kPortIndexInput,
1075                   kPortIndexOutput, eleStream, &Info, 0, (int)Info.size(),
1076                   portMode[1], false);
1077     eleStream.close();
1078     waitOnInputConsumption(omxNode, observer, &iBuffer, &oBuffer,
1079                            kPortIndexInput, kPortIndexOutput, portMode[1]);
1080     testEOS(omxNode, observer, &iBuffer, &oBuffer, true, eosFlag, portMode);
1081     flushPorts(omxNode, observer, &iBuffer, &oBuffer, kPortIndexInput,
1082                kPortIndexOutput);
1083     framesReceived = 0;
1084     timestampUs = 0;
1085
1086     // set state to idle
1087     changeStateExecutetoIdle(omxNode, observer, &iBuffer, &oBuffer);
1088     // set state to executing
1089     changeStateIdletoLoaded(omxNode, observer, &iBuffer, &oBuffer,
1090                             kPortIndexInput, kPortIndexOutput);
1091 }
1092
1093 // test input/output port flush
1094 TEST_F(VideoDecHidlTest, FlushTest) {
1095     description("Test Flush");
1096     if (disableTest) return;
1097     android::hardware::media::omx::V1_0::Status status;
1098     uint32_t kPortIndexInput = 0, kPortIndexOutput = 1;
1099     status = setRole(omxNode, gEnv->getRole().c_str());
1100     ASSERT_EQ(status, ::android::hardware::media::omx::V1_0::Status::OK);
1101     OMX_PORT_PARAM_TYPE params;
1102     status = getParam(omxNode, OMX_IndexParamVideoInit, &params);
1103     if (status == ::android::hardware::media::omx::V1_0::Status::OK) {
1104         ASSERT_EQ(params.nPorts, 2U);
1105         kPortIndexInput = params.nStartPortNumber;
1106         kPortIndexOutput = kPortIndexInput + 1;
1107     }
1108     char mURL[512], info[512];
1109     strcpy(mURL, gEnv->getRes().c_str());
1110     strcpy(info, gEnv->getRes().c_str());
1111     GetURLForComponent(compName, mURL, info);
1112
1113     std::ifstream eleStream, eleInfo;
1114
1115     eleInfo.open(info);
1116     ASSERT_EQ(eleInfo.is_open(), true);
1117     android::Vector<FrameData> Info;
1118     int bytesCount = 0;
1119     uint32_t flags = 0;
1120     uint32_t timestamp = 0;
1121     while (1) {
1122         if (!(eleInfo >> bytesCount)) break;
1123         eleInfo >> flags;
1124         eleInfo >> timestamp;
1125         Info.push_back({bytesCount, flags, timestamp});
1126     }
1127     eleInfo.close();
1128
1129     // set Port Params
1130     uint32_t nFrameWidth, nFrameHeight, xFramerate;
1131     OMX_COLOR_FORMATTYPE eColorFormat = OMX_COLOR_FormatYUV420Planar;
1132     getInputChannelInfo(omxNode, kPortIndexInput, &nFrameWidth, &nFrameHeight,
1133                         &xFramerate);
1134     setDefaultPortParam(omxNode, kPortIndexOutput, OMX_VIDEO_CodingUnused,
1135                         eColorFormat, nFrameWidth, nFrameHeight, 0, xFramerate);
1136
1137     // set port mode
1138     PortMode portMode[2];
1139     portMode[0] = portMode[1] = PortMode::PRESET_BYTE_BUFFER;
1140     status = omxNode->setPortMode(kPortIndexInput, portMode[0]);
1141     ASSERT_EQ(status, ::android::hardware::media::omx::V1_0::Status::OK);
1142     status = omxNode->setPortMode(kPortIndexOutput, portMode[1]);
1143     ASSERT_EQ(status, ::android::hardware::media::omx::V1_0::Status::OK);
1144
1145     android::Vector<BufferInfo> iBuffer, oBuffer;
1146
1147     // set state to idle
1148     changeStateLoadedtoIdle(omxNode, observer, &iBuffer, &oBuffer,
1149                             kPortIndexInput, kPortIndexOutput, portMode);
1150     // set state to executing
1151     changeStateIdletoExecute(omxNode, observer);
1152
1153     // Decode 128 frames and flush. here 128 is chosen to ensure there is a key
1154     // frame after this so that the below section can be convered for all
1155     // components
1156     int nFrames = 128;
1157     eleStream.open(mURL, std::ifstream::binary);
1158     ASSERT_EQ(eleStream.is_open(), true);
1159     decodeNFrames(omxNode, observer, &iBuffer, &oBuffer, kPortIndexInput,
1160                   kPortIndexOutput, eleStream, &Info, 0, nFrames, portMode[1],
1161                   false);
1162     // Note: Assumes 200 ms is enough to end any decode call that started
1163     flushPorts(omxNode, observer, &iBuffer, &oBuffer, kPortIndexInput,
1164                kPortIndexOutput, 200000);
1165     framesReceived = 0;
1166
1167     // Seek to next key frame and start decoding till the end
1168     int index = nFrames;
1169     bool keyFrame = false;
1170     while (index < (int)Info.size()) {
1171         if ((Info[index].flags & OMX_BUFFERFLAG_SYNCFRAME) ==
1172             OMX_BUFFERFLAG_SYNCFRAME) {
1173             timestampUs = Info[index - 1].timestamp;
1174             keyFrame = true;
1175             break;
1176         }
1177         eleStream.ignore(Info[index].bytesCount);
1178         index++;
1179     }
1180     if (keyFrame) {
1181         decodeNFrames(omxNode, observer, &iBuffer, &oBuffer, kPortIndexInput,
1182                       kPortIndexOutput, eleStream, &Info, index,
1183                       Info.size() - index, portMode[1], false);
1184     }
1185     // Note: Assumes 200 ms is enough to end any decode call that started
1186     flushPorts(omxNode, observer, &iBuffer, &oBuffer, kPortIndexInput,
1187                kPortIndexOutput, 200000);
1188     framesReceived = 0;
1189
1190     // set state to idle
1191     changeStateExecutetoIdle(omxNode, observer, &iBuffer, &oBuffer);
1192     // set state to executing
1193     changeStateIdletoLoaded(omxNode, observer, &iBuffer, &oBuffer,
1194                             kPortIndexInput, kPortIndexOutput);
1195 }
1196
1197 int main(int argc, char** argv) {
1198     gEnv = new ComponentTestEnvironment();
1199     ::testing::AddGlobalTestEnvironment(gEnv);
1200     ::testing::InitGoogleTest(&argc, argv);
1201     int status = gEnv->initFromOptions(argc, argv);
1202     if (status == 0) {
1203         status = RUN_ALL_TESTS();
1204         ALOGI("Test result = %d", status);
1205     }
1206     return status;
1207 }