summaryrefslogtreecommitdiffstats
path: root/media/omx/omx_codec_unittest.cc
blob: adf81322c74aaa06d6b5c62487ac8b969932c096 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#define _CRT_SECURE_NO_WARNINGS

#include <deque>

#include "base/callback.h"
#include "base/message_loop.h"
#include "media/base/mock_filters.h"
#include "media/omx/mock_omx.h"
#include "media/omx/omx_codec.h"
#include "testing/gtest/include/gtest/gtest.h"

using ::testing::_;
using ::testing::DoAll;
using ::testing::InSequence;
using ::testing::IsNull;
using ::testing::NotNull;
using ::testing::Return;
using ::testing::SaveArg;
using ::testing::SetArgumentPointee;
using ::testing::StrEq;
using ::testing::StrictMock;

namespace {

const int kBufferCount = 3;
const int kBufferSize = 4096;
const char* kRoleName = "awsome";
const char* kComponentName = "OMX.google.mock.awsome";

}  // namespace

namespace media {

ACTION(ReturnComponentName) {
  strcpy(((char**)arg2)[0], kComponentName);
}

ACTION(GetHandle) {
  *arg0 = MockOmx::get()->component();
  MockOmx::get()->component()->pApplicationPrivate = arg2;
  memcpy(MockOmx::get()->callbacks(), arg3, sizeof(OMX_CALLBACKTYPE));
}

ACTION(GetParameterVideoInit) {
  ((OMX_PORT_PARAM_TYPE*)arg1)->nStartPortNumber = 0;
}

ACTION(GetParameterPortDefinition) {
  OMX_PARAM_PORTDEFINITIONTYPE* port_format =
      (OMX_PARAM_PORTDEFINITIONTYPE*)arg1;
  CHECK(port_format->nPortIndex == 0 || port_format->nPortIndex == 1);
  if (port_format->nPortIndex == 0)
    port_format->eDir = OMX_DirInput;
  else
    port_format->eDir = OMX_DirOutput;
  port_format->nBufferCountMin = kBufferCount;
  port_format->nBufferSize = kBufferSize;
}

ACTION(AllocateBuffer) {
  *arg0 = new OMX_BUFFERHEADERTYPE();
  memset(*arg0, 0, sizeof(OMX_BUFFERHEADERTYPE));
  (*arg0)->pBuffer = new uint8[kBufferSize];
}

ACTION(UseBuffer) {
  *arg0 = new OMX_BUFFERHEADERTYPE();
  memset(*arg0, 0, sizeof(OMX_BUFFERHEADERTYPE));
}

ACTION(FreeBuffer) {
  delete [] arg1->pBuffer;
  delete arg1;
}

ACTION_P2(SendEvent, event, data1) {
  // TODO(hclam): pass data2 and event data.
  (*MockOmx::get()->callbacks()->EventHandler)(
      MockOmx::get()->component(),
      MockOmx::get()->component()->pApplicationPrivate,
      event, static_cast<OMX_U32>(data1), 0, NULL);
}

ACTION(FillBuffer) {
  arg0->nFlags = 0;
  arg0->nFilledLen = kBufferSize;
  (*MockOmx::get()->callbacks()->FillBufferDone)(
      MockOmx::get()->component(),
      MockOmx::get()->component()->pApplicationPrivate,
      arg0);
}

ACTION(FillEosBuffer) {
  arg0->nFlags = OMX_BUFFERFLAG_EOS;
  arg0->nFilledLen = 0;
  (*MockOmx::get()->callbacks()->FillBufferDone)(
      MockOmx::get()->component(),
      MockOmx::get()->component()->pApplicationPrivate,
      arg0);
}

class MockOmxConfigurator : public OmxConfigurator {
 public:
  MockOmxConfigurator()
      : OmxConfigurator(MediaFormat(), MediaFormat()) {}

  MOCK_CONST_METHOD0(GetRoleName, std::string());
  MOCK_CONST_METHOD3(ConfigureIOPorts,
                     bool(OMX_COMPONENTTYPE* component,
                          OMX_PARAM_PORTDEFINITIONTYPE* input_fef,
                          OMX_PARAM_PORTDEFINITIONTYPE* output_def));
};

class OmxCodecTest : public testing::Test {
 public:
  OmxCodecTest ()
      : omx_codec_(new OmxCodec(&message_loop_)) {
    omx_codec_->Setup(&mock_configurator_);
  }

  ~OmxCodecTest() {
    CHECK(output_units_.size() == 0);
  }

 protected:
  void ExpectSettings() {
    // Return the component name.
    EXPECT_CALL(*MockOmx::get(), GetComponentsOfRole(_, _, IsNull()))
        .WillOnce(DoAll(SetArgumentPointee<1>(1),
                        Return(OMX_ErrorNone)));
    EXPECT_CALL(*MockOmx::get(), GetComponentsOfRole(_, _, NotNull()))
        .WillOnce(DoAll(SetArgumentPointee<1>(1),
                        ReturnComponentName(),
                        Return(OMX_ErrorNone)));

    // Handle get parameter calls.
    EXPECT_CALL(*MockOmx::get(),
                GetParameter(OMX_IndexParamVideoInit, NotNull()))
        .WillRepeatedly(DoAll(GetParameterVideoInit(), Return(OMX_ErrorNone)));
    EXPECT_CALL(*MockOmx::get(),
                GetParameter(OMX_IndexParamPortDefinition, NotNull()))
        .WillRepeatedly(DoAll(GetParameterPortDefinition(),
                              Return(OMX_ErrorNone)));

    // Ignore all set parameter calls.
    EXPECT_CALL(*MockOmx::get(), SetParameter(_, _))
        .WillRepeatedly(Return(OMX_ErrorNone));

    // Expect calling to configurator once.
    EXPECT_CALL(mock_configurator_,
                ConfigureIOPorts(MockOmx::get()->component(), _, _))
        .WillOnce(Return(true));

    EXPECT_CALL(mock_configurator_, GetRoleName())
        .WillRepeatedly(Return(kRoleName));
  }

  void ExpectToLoaded() {
    InSequence s;

    // Expect initialization.
    EXPECT_CALL(*MockOmx::get(), Init())
        .WillOnce(Return(OMX_ErrorNone));

    // Return the handle.
    EXPECT_CALL(*MockOmx::get(),
                GetHandle(NotNull(), StrEq(kComponentName),
                          NotNull(), NotNull()))
        .WillOnce(DoAll(GetHandle(),
                        Return(OMX_ErrorNone)));
  }

  void ExpectLoadedToIdle() {
    InSequence s;

    // Expect transition to idle.
    EXPECT_CALL(*MockOmx::get(),
                SendCommand(OMX_CommandStateSet, OMX_StateIdle, _))
        .WillOnce(
            DoAll(
                SendEvent(OMX_EventCmdComplete, OMX_CommandStateSet),
                Return(OMX_ErrorNone)));

    // Expect allocation of buffers.
    EXPECT_CALL(*MockOmx::get(),
                UseBuffer(NotNull(), 0, IsNull(), kBufferSize, _))
        .Times(kBufferCount)
        .WillRepeatedly(DoAll(UseBuffer(), Return(OMX_ErrorNone)));

    // Expect allocation of output buffers and send command complete.
    EXPECT_CALL(*MockOmx::get(),
                AllocateBuffer(NotNull(), 1, IsNull(), kBufferSize))
        .Times(kBufferCount)
        .WillRepeatedly(DoAll(AllocateBuffer(), Return(OMX_ErrorNone)));
  }

  void ExpectToExecuting() {
    InSequence s;

    // Expect transition to executing.
    EXPECT_CALL(*MockOmx::get(),
                SendCommand(OMX_CommandStateSet, OMX_StateExecuting, _))
        .WillOnce(DoAll(
            SendEvent(OMX_EventCmdComplete, OMX_CommandStateSet),
            Return(OMX_ErrorNone)));

    // Expect initial FillThisBuffer() calls.
    EXPECT_CALL(*MockOmx::get(), FillThisBuffer(NotNull()))
        .Times(kBufferCount)
        .WillRepeatedly(DoAll(FillBuffer(), Return(OMX_ErrorNone)));
  }

  void ExpectToIdle() {
    // Expect going to idle
    EXPECT_CALL(*MockOmx::get(),
                SendCommand(OMX_CommandStateSet, OMX_StateIdle, _))
        .WillOnce(DoAll(
            SendEvent(OMX_EventCmdComplete, OMX_CommandStateSet),
            Return(OMX_ErrorNone)));
  }

  void ExpectIdleToLoaded() {
    InSequence s;

    // Expect transition to loaded.
    EXPECT_CALL(*MockOmx::get(),
                SendCommand(OMX_CommandStateSet, OMX_StateLoaded, _))
        .WillOnce(DoAll(
            SendEvent(OMX_EventCmdComplete, OMX_CommandStateSet),
            Return(OMX_ErrorNone)));

    // Expect free buffer for input port.
    EXPECT_CALL(*MockOmx::get(), FreeBuffer(0, NotNull()))
        .Times(kBufferCount)
        .WillRepeatedly(DoAll(FreeBuffer(), Return(OMX_ErrorNone)));

    EXPECT_CALL(*MockOmx::get(), FreeBuffer(1, NotNull()))
        .Times(kBufferCount)
        .WillRepeatedly(DoAll(FreeBuffer(), Return(OMX_ErrorNone)));
  }

  void ExpectToEmpty() {
    InSequence s;

    EXPECT_CALL(*MockOmx::get(), FreeHandle(MockOmx::get()->component()))
        .WillOnce(Return(OMX_ErrorNone));
    EXPECT_CALL(*MockOmx::get(), Deinit())
        .WillOnce(Return(OMX_ErrorNone));
  }

  // TODO(hclam): Make a more generic about when to stop.
  void ExpectStart() {
    ExpectToLoaded();
    ExpectLoadedToIdle();
    ExpectToExecuting();
  }

  void ExpectStop() {
    ExpectToIdle();
    ExpectIdleToLoaded();
    ExpectToEmpty();
  }

  void ReadCallback(OMX_BUFFERHEADERTYPE* buffer) {
    output_units_.push_back(buffer);
  }

  void MakeReadRequest() {
    omx_codec_->Read(NewCallback(this, &OmxCodecTest::ReadCallback));
  }

  void SaveFillThisBuffer(OMX_BUFFERHEADERTYPE* buffer) {
    fill_this_buffer_received_.push_back(buffer);
  }

  void ExpectAndSaveFillThisBuffer() {
    EXPECT_CALL(*MockOmx::get(), FillThisBuffer(NotNull()))
        .WillOnce(DoAll(Invoke(this, &OmxCodecTest::SaveFillThisBuffer),
                        Return(OMX_ErrorNone)))
        .RetiresOnSaturation();
  }

  std::deque<OMX_BUFFERHEADERTYPE*> output_units_;
  std::deque<OMX_BUFFERHEADERTYPE*> fill_this_buffer_received_;

  MockOmx mock_omx_;
  MockOmxConfigurator mock_configurator_;

  MessageLoop message_loop_;
  scoped_refptr<OmxCodec> omx_codec_;

  MockFilterCallback stop_callback_;

 private:
  DISALLOW_COPY_AND_ASSIGN(OmxCodecTest);
};

TEST_F(OmxCodecTest, SimpleStartAndStop) {
  ExpectSettings();
  ExpectStart();
  omx_codec_->Start();
  message_loop_.RunAllPending();

  EXPECT_CALL(stop_callback_, OnFilterCallback());
  EXPECT_CALL(stop_callback_, OnCallbackDestroyed());
  ExpectStop();
  omx_codec_->Stop(stop_callback_.NewCallback());
  message_loop_.RunAllPending();
}

TEST_F(OmxCodecTest, EndOfStream) {
  ExpectSettings();
  ExpectStart();
  omx_codec_->Start();
  message_loop_.RunAllPending();

  // Make read requests, OmxCodec should have gotten kBufferCount
  // buffers already.
  EXPECT_EQ(0u, output_units_.size());
  for (int i = 0; i < kBufferCount; ++i) {
    // Give buffers back to OmxCodec. OmxCodec will make a new
    // FillThisBuffer() call for each read.
    EXPECT_CALL(*MockOmx::get(), FillThisBuffer(NotNull()))
        .WillOnce(DoAll(FillEosBuffer(), Return(OMX_ErrorNone)))
        .RetiresOnSaturation();
    MakeReadRequest();
  }
  message_loop_.RunAllPending();
  CHECK(kBufferCount == static_cast<int>(output_units_.size()));

  // Make sure buffers received are in order.
  for (int i = 0; i < kBufferCount; ++i) {
    // TODO(jiesun): How to verify this now?
    // EXPECT_EQ(i, output_units_[i].first);
    EXPECT_TRUE(output_units_[i] != NULL);
  }

  output_units_.clear();

  // Make some read requests and make sure end-of-stream buffer id
  // are received.
  EXPECT_EQ(0u, output_units_.size());
  for (int i = 0; i < 2 * kBufferCount; ++i) {
    MakeReadRequest();
  }
  message_loop_.RunAllPending();
  EXPECT_EQ(2 * kBufferCount, static_cast<int>(output_units_.size()));

  for (size_t i = 0; i <output_units_.size(); ++i) {
    EXPECT_EQ(NULL, output_units_[i]);
  }
  output_units_.clear();

  // Stop OmxCodec.
  EXPECT_CALL(stop_callback_, OnFilterCallback());
  EXPECT_CALL(stop_callback_, OnCallbackDestroyed());
  ExpectStop();
  omx_codec_->Stop(stop_callback_.NewCallback());
  message_loop_.RunAllPending();
}

TEST_F(OmxCodecTest, OutputFlowControl) {
  ExpectSettings();
  ExpectStart();
  omx_codec_->Start();
  message_loop_.RunAllPending();

  // Since initial FillThisBuffer() calls are made and fulfilled during
  // start. Reads issued to OmxCodec will be fulfilled now.
  EXPECT_EQ(0u, output_units_.size());
  for (int i = 0; i < kBufferCount; ++i) {
    ExpectAndSaveFillThisBuffer();
    MakeReadRequest();
  }
  message_loop_.RunAllPending();
  CHECK(kBufferCount == static_cast<int>(output_units_.size()));

  // Make sure buffers received are in order.
  for (int i = 0; i < kBufferCount; ++i) {
    EXPECT_TRUE(output_units_[i] != NULL);
  }

  output_units_.clear();

  // In each loop, perform the following actions:
  // 1. Make a read request to OmxCodec.
  // 2. Fake a response for FillBufferDone().
  // 3. Expect read response received.
  for (int i = 0; i < kBufferCount; ++i) {
    // 1. First make a read request.
    // Since a buffer is given back to OmxCodec. A FillThisBuffer() is called
    // to OmxCodec.
    EXPECT_CALL(*MockOmx::get(), FillThisBuffer(NotNull()))
         .WillOnce(Return(OMX_ErrorNone))
         .RetiresOnSaturation();
    MakeReadRequest();

    // 2. Then fake a response from OpenMAX.
    OMX_BUFFERHEADERTYPE* buffer = fill_this_buffer_received_.front();
    fill_this_buffer_received_.pop_front();
    buffer->nFlags = 0;
    buffer->nFilledLen = kBufferSize;
    (*MockOmx::get()->callbacks()->FillBufferDone)(
        MockOmx::get()->component(),
        MockOmx::get()->component()->pApplicationPrivate,
        buffer);

    // Make sure actions are completed.
    message_loop_.RunAllPending();

    // 3. Expect read response received.
    // The above action will cause a read callback be called and we should
    // receive one buffer now. Also expect the buffer id be received in
    // reverse order.
    EXPECT_EQ(1u, output_units_.size());
    //EXPECT_EQ(kBufferCount - i - 1, output_units_.front().first);

    output_units_.pop_front();

    // Make sure actions are completed.
    message_loop_.RunAllPending();
  }

  // Now issue kBufferCount reads to OmxCodec.
  EXPECT_EQ(0u, output_units_.size());

  // Stop OmxCodec.
  EXPECT_CALL(stop_callback_, OnFilterCallback());
  EXPECT_CALL(stop_callback_, OnCallbackDestroyed());
  ExpectStop();
  omx_codec_->Stop(stop_callback_.NewCallback());
  message_loop_.RunAllPending();
}

// TODO(hclam): Add test case for dynamic port config.
// TODO(hclam): Create a more complicated test case so that read
// requests and reply from FillThisBuffer() arrives out of order.
// TODO(hclam): Add test case for Feed().

}  // namespace media