summaryrefslogtreecommitdiffstats
path: root/media/omx/omx_unittest.cc
blob: aca3647427ff111f31b1fd7617b1ddacc5f66ff7 (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
// Copyright (c) 2009-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.

#include "base/command_line.h"
#include "base/condition_variable.h"
#include "base/logging.h"
#include "base/waitable_event.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/openmax/il/OMX_Component.h"
#include "third_party/openmax/il/OMX_Core.h"

namespace {

// Defines the maximum number of buffers created for I/O ports.
const int kMaxBufferNum = 256;

template <typename T>
static void ResetHeader(T* param) {
  memset(param, 0, sizeof(T));
  // TODO(hclam): Make this version number configurable.
  param->nVersion.nVersion = 0x00000101;
  param->nSize = sizeof(T);
}

}  // namespace

namespace media {

class OmxTest : public testing::Test {
 public:
  OmxTest()
      : handle_(NULL),
        event_(false, false),
        empty_buffer_(false, false),
        fill_buffer_(false, false),
        last_event_type_(OMX_EventMax),
        last_event_data1_(0),
        last_event_data2_(0) {
    memset(input_buffers_, 0, sizeof(input_buffers_));
    memset(output_buffers_, 0, sizeof(output_buffers_));
  }

 protected:
  virtual void SetUp() {
    // Initialize OpenMAX.
    EXPECT_EQ(OMX_ErrorNone, OMX_Init());
  }

  virtual void TearDown() {
    EXPECT_EQ(OMX_ErrorNone, OMX_Deinit());
  }

  void InitComponent(std::string component_name) {
    // TODO(hclam): Remove static when bug in driver is fixed.
    static OMX_CALLBACKTYPE callback = { &EventHandler,
                                         &EmptyBufferCallback,
                                         &FillBufferCallback };

    OMX_ERRORTYPE omxresult = OMX_GetHandle(
        (void**)&handle_,
        const_cast<OMX_STRING>(component_name.c_str()),
        this, &callback);
    EXPECT_EQ(OMX_ErrorNone, omxresult);
    CHECK(handle_);
  }

  void DeinitComponent() {
    if (handle_)
      OMX_FreeHandle(handle_);
  }

  void AllocateBuffers(int port) {
    int count = 0;
    int size = 0;
    OMX_BUFFERHEADERTYPE** buffers = NULL;
    if (port == input_port_) {
      count = input_buffer_count_;
      size = input_buffer_size_;
      buffers = input_buffers_;
    } else if (port == output_port_) {
      count = output_buffer_count_;
      size = output_buffer_size_;
      buffers = output_buffers_;
    } else {
      NOTREACHED() << "Not a valid port";
    }
    for (int i = 0; i < count; ++i) {
      EXPECT_EQ(OMX_ErrorNone,
                OMX_AllocateBuffer(handle_, buffers + i,
                                   port, NULL, size));
    }
  }

  void ReleaseBuffers(int port) {
    int count = 0;
    OMX_BUFFERHEADERTYPE** buffers = NULL;
    if (port == input_port_) {
      count = input_buffer_count_;
      buffers = input_buffers_;
    } else if (port == output_port_) {
      count = output_buffer_count_;
      buffers = output_buffers_;
    } else {
      NOTREACHED() << "Not a valid port";
    }
    for (int i = 0; i < count; ++i) {
      CHECK(buffers[i]);
      EXPECT_EQ(OMX_ErrorNone,
                OMX_FreeBuffer(handle_, port, buffers[i]));
      buffers[i] = NULL;
    }
  }

  void TransitionLoadedToIdle() {
    EXPECT_EQ(OMX_ErrorNone,
              OMX_SendCommand(handle_, OMX_CommandStateSet,
                              OMX_StateIdle, 0));
    AllocateBuffers(input_port_);
    AllocateBuffers(output_port_);
    event_.Wait();
    EXPECT_EQ(OMX_EventCmdComplete, last_event_type_);
    EXPECT_EQ(OMX_CommandStateSet, last_event_data1_);
    EXPECT_EQ(OMX_StateIdle, last_event_data2_);
  }

  void TransitionIdleToLoaded() {
    EXPECT_EQ(OMX_ErrorNone,
              OMX_SendCommand(handle_, OMX_CommandStateSet,
                              OMX_StateLoaded, 0));
    ReleaseBuffers(input_port_);
    ReleaseBuffers(output_port_);
    event_.Wait();
    EXPECT_EQ(OMX_EventCmdComplete, last_event_type_);
    EXPECT_EQ(OMX_CommandStateSet, last_event_data1_);
    EXPECT_EQ(OMX_StateLoaded, last_event_data2_);
  }

  void TransitionIdleToExecuting() {
    EXPECT_EQ(OMX_ErrorNone,
              OMX_SendCommand(handle_, OMX_CommandStateSet,
                              OMX_StateExecuting, 0));
    event_.Wait();
    EXPECT_EQ(OMX_EventCmdComplete, last_event_type_);
    EXPECT_EQ(OMX_CommandStateSet, last_event_data1_);
    EXPECT_EQ(OMX_StateExecuting, last_event_data2_);
  }

  void TransitionExecutingToIdle() {
    EXPECT_EQ(OMX_ErrorNone,
              OMX_SendCommand(handle_, OMX_CommandStateSet,
                              OMX_StateIdle, 0));
    event_.Wait();
    EXPECT_EQ(OMX_EventCmdComplete, last_event_type_);
    EXPECT_EQ(OMX_CommandStateSet, last_event_data1_);
    EXPECT_EQ(OMX_StateIdle, last_event_data2_);
  }

  void GetComponentsOfRole(std::string role) {
    OMX_U32 roles = 0;
    OMX_U8** component_names = NULL;
    const int kSize = 256;

    LOG(INFO) << "GetComponentsOfRole: " << role;
    EXPECT_EQ(OMX_ErrorNone, OMX_GetComponentsOfRole(
        const_cast<OMX_STRING>(role.c_str()), &roles, 0));

    // TODO(hclam): Should assert the component number.
    LOG(INFO) << "Components: " << roles;

    if (roles) {
      component_names = new OMX_U8*[roles];
      for (size_t i = 0; i < roles; ++i)
        component_names[i] = new OMX_U8[kSize];

      OMX_U32 roles_backup = roles;
      EXPECT_EQ(OMX_ErrorNone,
                OMX_GetComponentsOfRole(
                    const_cast<OMX_STRING>(role.c_str()),
                    &roles, component_names));
      ASSERT_EQ(roles_backup, roles);

      for (size_t i = 0; i < roles; ++i) {
        LOG(INFO) << "Component name: " << component_names[i];
        delete [] component_names[i];
      }
      delete [] component_names;
    }
  }

  OMX_ERRORTYPE EventHandlerInternal(
      OMX_HANDLETYPE component, OMX_EVENTTYPE event,
      OMX_U32 data1, OMX_U32 data2, OMX_PTR event_data) {
    last_event_type_ = event;
    last_event_data1_ = static_cast<int>(data1);
    last_event_data2_ = static_cast<int>(data2);
    // TODO(hclam): Save |event_data|.
    event_.Signal();
    return OMX_ErrorNone;
  }

  OMX_ERRORTYPE EmptyBufferCallbackInternal(
      OMX_HANDLETYPE component, OMX_BUFFERHEADERTYPE* buffer) {
    // TODO(hclam): Add code here.
    empty_buffer_.Signal();
    return OMX_ErrorNone;
  }

  OMX_ERRORTYPE FillBufferCallbackInternal(
      OMX_HANDLETYPE component, OMX_BUFFERHEADERTYPE* buffer) {
    // TODO(hclam): Add code here.
    fill_buffer_.Signal();
    return OMX_ErrorNone;
  }

  // Static callback methods.
  static OMX_ERRORTYPE EventHandler(
      OMX_HANDLETYPE component, OMX_PTR priv_data,
      OMX_EVENTTYPE event, OMX_U32 data1, OMX_U32 data2,
      OMX_PTR event_data) {
    return static_cast<OmxTest*>(priv_data)
        ->EventHandlerInternal(component,
                               event, data1, data2, event_data);
  }

  static OMX_ERRORTYPE EmptyBufferCallback(
      OMX_HANDLETYPE component, OMX_PTR priv_data,
      OMX_BUFFERHEADERTYPE* buffer) {
    return static_cast<OmxTest*>(priv_data)
        ->EmptyBufferCallbackInternal(component, buffer);
  }

  static OMX_ERRORTYPE FillBufferCallback(
      OMX_HANDLETYPE component, OMX_PTR priv_data,
      OMX_BUFFERHEADERTYPE* buffer) {
    return static_cast<OmxTest*>(priv_data)
        ->FillBufferCallbackInternal(component, buffer);
  }

  OMX_COMPONENTTYPE* handle_;
  int input_port_;
  int output_port_;
  int input_buffer_count_;
  int input_buffer_size_;
  int output_buffer_count_;
  int output_buffer_size_;
  OMX_BUFFERHEADERTYPE* input_buffers_[kMaxBufferNum];
  OMX_BUFFERHEADERTYPE* output_buffers_[kMaxBufferNum];

  base::WaitableEvent event_;
  base::WaitableEvent empty_buffer_;
  base::WaitableEvent fill_buffer_;

  OMX_EVENTTYPE last_event_type_;
  int last_event_data1_;
  int last_event_data2_;
};

class OmxVideoDecoderTest : public OmxTest {
 protected:
  void Configure(OMX_VIDEO_CODINGTYPE codec,
                 int width, int height) {
    // Obtain port IDs.
    OMX_PORT_PARAM_TYPE port_param;
    ResetHeader(&port_param);
    EXPECT_EQ(OMX_ErrorNone,
              OMX_GetParameter(handle_,
                               OMX_IndexParamVideoInit,
                               &port_param));

    input_port_ = port_param.nStartPortNumber;
    output_port_ = port_param.nStartPortNumber + 1;
    LOG(INFO) << "Input port number: " << input_port_;
    LOG(INFO) << "Output port number: " << output_port_;

    // Get and set parameters for input port.
    LOG(INFO) << "Input port width: " << width;
    LOG(INFO) << "Input port height: " << height;
    LOG(INFO) << "Input port codec: " << codec;
    OMX_PARAM_PORTDEFINITIONTYPE port_format;
    ResetHeader(&port_format);
    port_format.nPortIndex = input_port_;
    EXPECT_EQ(OMX_ErrorNone,
              OMX_GetParameter(handle_,
                               OMX_IndexParamPortDefinition,
                               &port_format));
    EXPECT_EQ(OMX_DirInput, port_format.eDir);
    port_format.format.video.eCompressionFormat = codec;
    port_format.format.video.nFrameWidth  = width;
    port_format.format.video.nFrameHeight = height;
    EXPECT_EQ(OMX_ErrorNone,
              OMX_SetParameter(handle_,
                               OMX_IndexParamPortDefinition,
                               &port_format));

    // TODO(hclam): Add configurations to output port.

    // Get Parameters for input port.
    ResetHeader(&port_format);
    port_format.nPortIndex = input_port_;
    EXPECT_EQ(OMX_ErrorNone,
              OMX_GetParameter(handle_,
                               OMX_IndexParamPortDefinition,
                               &port_format));
    EXPECT_EQ(OMX_DirInput, port_format.eDir);
    input_buffer_count_ = port_format.nBufferCountMin;
    input_buffer_size_ = port_format.nBufferSize;
    CHECK(input_buffer_count_ < kMaxBufferNum);

    // Get parameters for output port.
    ResetHeader(&port_format);
    port_format.nPortIndex = output_port_;
    EXPECT_EQ(OMX_ErrorNone,
              OMX_GetParameter(handle_,
                               OMX_IndexParamPortDefinition,
                               &port_format));
    EXPECT_EQ(OMX_DirOutput, port_format.eDir);
    output_buffer_count_ = port_format.nBufferCountMin;
    output_buffer_size_ = port_format.nBufferSize;
    CHECK(output_buffer_count_ < kMaxBufferNum);

    LOG(INFO) << "Input buffer count: " << input_buffer_count_;
    LOG(INFO) << "Input buffer size: " << input_buffer_size_;
    LOG(INFO) << "Output buffer count: " << output_buffer_count_;
    LOG(INFO) << "Output buffer size: " << output_buffer_size_;
  }

  std::string component() {
    return CommandLine::ForCurrentProcess()
        ->GetSwitchValueASCII("video-decoder-component");
  }

  OMX_VIDEO_CODINGTYPE codec() {
    std::string codec = CommandLine::ForCurrentProcess()
        ->GetSwitchValueASCII("video-decoder-codec");
    if (codec == "h264")
      return OMX_VIDEO_CodingAVC;
    return OMX_VIDEO_CodingAutoDetect;
  }
};

TEST_F(OmxTest, SimpleInit) {
  // A empty test case will test basic init/deinit of OpenMAX library.
}

TEST_F(OmxTest, GetComponentsOfRole) {
  // Roles video decoders.
  GetComponentsOfRole("video_decoder.avc");
  GetComponentsOfRole("video_decoder.mpeg4");
  GetComponentsOfRole("video_decoder.vc1");

  // TODO(hclam): Add roles of encoders.
}

TEST_F(OmxVideoDecoderTest, GetHandle) {
  // TODO(hclam): Should use GetComponentsOfRole instead.
  InitComponent(component());
  DeinitComponent();
}

TEST_F(OmxVideoDecoderTest, Configuration) {
  InitComponent(component());
  // TODO(hclam): Make resolution configurable.
  Configure(codec(), 1024, 768);
  DeinitComponent();
}

TEST_F(OmxVideoDecoderTest, TransitionToIdle) {
  InitComponent(component());
  Configure(codec(), 1024, 768);
  TransitionLoadedToIdle();
  TransitionIdleToLoaded();
  DeinitComponent();
}

TEST_F(OmxVideoDecoderTest, FreeHandleWhenIdle) {
  InitComponent(component());
  Configure(codec(), 1024, 768);
  TransitionLoadedToIdle();
  DeinitComponent();
}

TEST_F(OmxVideoDecoderTest, TransitionToExecuting) {
  InitComponent(component());
  Configure(codec(), 1024, 768);
  TransitionLoadedToIdle();
  TransitionIdleToExecuting();
  TransitionExecutingToIdle();
  TransitionIdleToLoaded();
  DeinitComponent();
}

TEST_F(OmxVideoDecoderTest, FreeHandleWhenExecuting) {
  InitComponent(component());
  Configure(codec(), 1024, 768);
  TransitionLoadedToIdle();
  TransitionIdleToExecuting();
  DeinitComponent();
}

TEST_F(OmxVideoDecoderTest, CallbacksAreCopied) {
  // Allocate a callback struct on stack and clear it with zero.
  // This make sure OpenMAX library will copy the content of the
  // struct.
  OMX_CALLBACKTYPE callback = { &EventHandler,
                                &EmptyBufferCallback,
                                &FillBufferCallback };

  OMX_ERRORTYPE omxresult = OMX_GetHandle(
      (void**)&handle_,
      const_cast<OMX_STRING>(component().c_str()),
      this, &callback);
  EXPECT_EQ(OMX_ErrorNone, omxresult);
  CHECK(handle_);
  memset(&callback, 0, sizeof(callback));

  // Then configure the component as usual.
  Configure(codec(), 1024, 768);
  TransitionLoadedToIdle();
  TransitionIdleToExecuting();
  TransitionExecutingToIdle();
  TransitionIdleToLoaded();
  DeinitComponent();
}

}  // namespace media