stbt: Refactor wait_for_match and wait_for_motion

This is to make it easier for a new feature that gives access to the list
of results that stbt-templatematch and stbt-motiondetect return. It also
cleans up the code and could make it possible to have the main loop running
in the background.

We would benefit from having a motion api that can return a list of
[timestamp, motion or not].
We would probably also benefit from having an api that can return a list of
[timestamp, match or not, position, name of template]. This would allow
to measure the smoothness of animation, wait for a match in a specific
position.

wait_for_match and wait_for_motion are simplified and the logic is clearer.

If necessary, with a minimum refactoring, it will also allow more easily to
have the main loop running in the background due to the use of the
synchronized queue. (see http://docs.python.org/library/queue.html)
wait_for_match, wait_for_motion: Reset gstreamer elements in all cases

Previously, when a timeout occured when calling wait_for_match or
wait_for_motion, the gstreamer elements were not properly disabled.

This was causing the templatematch to be active when searching for motion
for instance. Note that there was a typo when disabling motiondetect.
stbt: Add a new iterator method `detect_match`

The iterator method `detect_match` gives access to everything returned by
stbt-templatematch.

The method wait_for_match is extracted from Display and implements its
logic using a generic method `detect_match`. This cleans up considerably
the code and makes it easier to understand what wait_for_match does.
stbt: Add a new iterator method `detect_motion`

The iterator method `detect_motion` gives access to everything returned by
stbt-motiondetect.

The method wait_for_motion is extracted from Display and implements its
logic using a generic method `detect_motion`. This cleans up considerably
the code and makes it easier to understand what wait_for_motion does.
stbt: Expose detect_motion and detect_match to the tests scripts

This makes it possible for the tests scripts to call the two iterator
methods detect_motion and detect_match.

This gives access to everything returned by the gstreamer elements
stbt-templatematch and stbt-motiondetect. These two methods are factored
out of Display and a single method detect is implemented. This cleans up
considerably the code and avoids having a class Display responsible for
doing everything.
detect_match, detect_motion: Results are returned using namedtuples

This allow users to access result.position instead of result['position'].
detect_match, detect_motion: Make optional the argument `directory`

Like wait_for_match and wait_for_motion: detect_match and detect_motion
have the directory parameter that defaults to the caller script's
directory.
Cleanup comments
Add tests for detect_match, detect_motion and wait_for_motion
detect_match, detect_motion: Add two regression tests that expose a race

When calling detect_match, detect_motion, the timeout has no effect if
it occurs while the result is processed by the user code during the yield.

Note that this also affects wait_for_match and wait_for_motion as they are
implemented in terms of detect_match and detect_motion.
`stbt run` times out using video stream time to fix the race condition

This fixes the race condition exposed by f289d24.

The timeout implementation is now based on the video stream time instead of
the real time. It will make it possible to implement process_all_frames by
slowing down the pipeline without having to change the timeout values in
the test scripts.

The gtk timeout added in 58285fb is kept but times out after 5 seconds. It
is now explicitly cancelled after the first message is received on the bus.
This is because this timeout is only used to prevent stbt run from hanging
when the gtk main loop is started but nothing is received on the bus.
Note that there are no tests that prove that this timeout is useful.

Note also that now, stbt explicitly fails after 3 consecutive underruns
whereas it used to rely on the gtk timeout to occurs after the third
underrun.
stbt: Expose `detect_motion` and `detect_match` to the test scripts

These two methods give access to everything returned by the gstreamer
element stbt-templatematch and stbt-motiondetect compiled as part of stbt.

This patch also fixes a issue where under specific circumstances, it was
possible that the templatematch element was active while the motiondetect
element was running. (see 22c737a)

The timeout specified in wait_for_match and in wait_for_motion is now using
video stream time. The gtk timeout added in 58285fb is kept but now times
out after 5 seconds. Also, stbt now explicitly fails after 3 consecutive
underruns whereas it used to rely on the gtk timeout to occurs after the
third underrun. (see 411712f)

The two methods `detect_motion` and `detect_match` are designed in a way
that will make it easy to solve several challenges:

- this will be easier to implement the feature that allows to measure non-
functional requirements (performance, smoothness).
The test scripts have now access to more informations (list of results,
positions of the matches, result of the first pass, ...) and have more
control on the results (possible to detect when no match / no motion is
detected without exception being generated).

- as stated by David Röthlisberger, the commit b14db20 will make it easier
to have the "main application loop run[ning] continuously in a separate
thread". This will enable to handle errors as soon as they happen (e.g. in
case of video loss, stbt currently only recovers on the next call to
detect_match / detect_motion). This will be possible thanks to the
introduction of "concurrency to the stbt implementation, which will make it
trivial to add actual parallelism later".

- this will make it easy to implement new methods available for the test
scripts. As an example, this could include (but is not limited to):
`matches` would tell whether a match is actually present on the screen,
`wait_for_no_match`, `wait_for_no_motion`, `press_and_wait_for_match`,
`press_and_wait_for_motion`.

- this will also make it possible to improve the current methods provided
to the test scripts.
The first timeout of press_until_match is currently not necessary and slows
down the test. It could be replaced by using the method `matches` described
above.
wait_for_match could return a MatchResult, giving the position of the match
and enabling to develop complex test scripts. (e.g. navigating to a specific
menu on the screen without hard coding the navigation).
wait_for_match and wait_for_motion are now respectively implemented in
terms of detect_match and detect_motion. This cleans up considerably the
code and make it easier to understand. (see 4cd5c01 and 1c1d320)

Note also that as part of 98305d7, the element named "templatematch" is
renamed to "template_match" to make it possible to have a common detect
method. This is because the detect method requires the messages emitted by
the elements to have the same name as the element name. For consistency,
the messages emitted by stbt-templatematch should be renamed to
"templatematch" and stbt.py should be updated accordingly.