1# Copyright 2014 The Chromium Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5from telemetry.internal.image_processing import frame_generator
6from telemetry.internal.util import external_modules
7
8np = external_modules.ImportRequiredModule('numpy')
9
10
11class FakeFrameGenerator(frame_generator.FrameGenerator):
12  """ Fakes a Frame Generator, for testing.
13
14  Attributes:
15    _frame_index: A frame read counter.
16    _timestamps: A generator of timestamps to return, or None.
17    _timestamp: The current timestamp.
18    _dimensions: The dimensions to return.
19    _channels: The number of color channels to return in the generated frames.
20    _frames: The number of frames to return before fake EOF."""
21  def __init__(self, frames=1e16, dimensions=(320, 240), channels=3,
22               timestamps=(x for x in iter(int, 1))):
23    """ Initializes the FakeFrameGenerator object.
24
25    Args:
26      frames: int, The number of frames to return before fake EOF.
27      dimensions: (int, int), The dimensions to return.
28      timestamps: generator, A generator of timestamps to return. The default
29          value is an infinite 0 generator.
30      channels: int, The number of color channels to return in the generated
31          frames, 1 for greyscale, 3 for RGB."""
32    self._dimensions = dimensions
33    self._timestamps = timestamps
34    self._timestamp = 0
35    self._frame_index = -1
36    self._channels = channels
37    self._frames = frames
38
39    super(FakeFrameGenerator, self).__init__()
40
41  # OVERRIDE
42  def _CreateGenerator(self):
43    while self._frame_index < self._frames - 1:
44      self._frame_index += 1
45      self._timestamp = next(self._timestamps)
46      yield np.zeros((self._dimensions[0], self._dimensions[1],
47                      self._channels), np.uint8)
48
49  # OVERRIDE
50  @property
51  def CurrentTimestamp(self):
52    return self._timestamp
53
54  # OVERRIDE
55  @property
56  def CurrentFrameNumber(self):
57    return self._frame_index
58
59  # OVERRIDE
60  @property
61  def Dimensions(self):
62    return self._dimensions
63