1f004c14a79136a52421cae74fec416fa1c18fabemussa# Copyright (c) 2014 The Chromium OS Authors. All rights reserved.
2f004c14a79136a52421cae74fec416fa1c18fabemussa# Use of this source code is governed by a BSD-style license that can be
3f004c14a79136a52421cae74fec416fa1c18fabemussa# found in the LICENSE file.
4f004c14a79136a52421cae74fec416fa1c18fabemussa
5f004c14a79136a52421cae74fec416fa1c18fabemussaimport argparse, common, datetime
6f004c14a79136a52421cae74fec416fa1c18fabemussa
7f004c14a79136a52421cae74fec416fa1c18fabemussafrom autotest_lib.client.common_lib import file_utils
8f004c14a79136a52421cae74fec416fa1c18fabemussafrom autotest_lib.client.common_lib.cros import chrome
9f004c14a79136a52421cae74fec416fa1c18fabemussafrom autotest_lib.client.cros.video import media_test_factory, \
10f004c14a79136a52421cae74fec416fa1c18fabemussa    sequence_generator
11f004c14a79136a52421cae74fec416fa1c18fabemussa
12f004c14a79136a52421cae74fec416fa1c18fabemussa
13f004c14a79136a52421cae74fec416fa1c18fabemussadef main():
14f004c14a79136a52421cae74fec416fa1c18fabemussa    """
15f004c14a79136a52421cae74fec416fa1c18fabemussa    Loads specified video in a chrome HTML5 player and collects its screenshots
16f004c14a79136a52421cae74fec416fa1c18fabemussa    at specified time instances.
17f004c14a79136a52421cae74fec416fa1c18fabemussa    """
18f004c14a79136a52421cae74fec416fa1c18fabemussa
19f004c14a79136a52421cae74fec416fa1c18fabemussa    parser = argparse.ArgumentParser(
20f004c14a79136a52421cae74fec416fa1c18fabemussa        formatter_class=argparse.RawDescriptionHelpFormatter,
21f004c14a79136a52421cae74fec416fa1c18fabemussa
22f004c14a79136a52421cae74fec416fa1c18fabemussa        description='Capture images of a video.',
23f004c14a79136a52421cae74fec416fa1c18fabemussa
24f004c14a79136a52421cae74fec416fa1c18fabemussa        epilog='''
25f004c14a79136a52421cae74fec416fa1c18fabemussa        This tool supports image-comparison-based video glitch detection tests.
26f004c14a79136a52421cae74fec416fa1c18fabemussa
27f004c14a79136a52421cae74fec416fa1c18fabemussa        Use this tool to capture golden/reference images that will be used to
28f004c14a79136a52421cae74fec416fa1c18fabemussa        verify test images captured during a video playback.
29f004c14a79136a52421cae74fec416fa1c18fabemussa
30f004c14a79136a52421cae74fec416fa1c18fabemussa        Run this tool directly on the device.
31f004c14a79136a52421cae74fec416fa1c18fabemussa
32f004c14a79136a52421cae74fec416fa1c18fabemussa        Copy tool first into /usr/local/autotest/cros so that autotest's
33f004c14a79136a52421cae74fec416fa1c18fabemussa        common.py is imported so as to resolve autotest references correctly.
34f004c14a79136a52421cae74fec416fa1c18fabemussa
35f004c14a79136a52421cae74fec416fa1c18fabemussa        Output images will be placed under /tmp/test. Images will be saved as
36f004c14a79136a52421cae74fec416fa1c18fabemussa        hh_mm_ss_mss.png denoting the moment in time it was captured.
37f004c14a79136a52421cae74fec416fa1c18fabemussa
38f004c14a79136a52421cae74fec416fa1c18fabemussa        ''')
39f004c14a79136a52421cae74fec416fa1c18fabemussa
400967f8cbfa2e3ada3cca4a946cca582eae130df4Mussa    parser.add_argument("name",
410967f8cbfa2e3ada3cca4a946cca582eae130df4Mussa                        help="Name of video to use.")
420967f8cbfa2e3ada3cca4a946cca582eae130df4Mussa
43f004c14a79136a52421cae74fec416fa1c18fabemussa    parser.add_argument("format",
44f004c14a79136a52421cae74fec416fa1c18fabemussa                        choices=['mp4', 'webm'],
45f004c14a79136a52421cae74fec416fa1c18fabemussa                        help="Video format to use.")
46f004c14a79136a52421cae74fec416fa1c18fabemussa
47f004c14a79136a52421cae74fec416fa1c18fabemussa    parser.add_argument("definition",
480967f8cbfa2e3ada3cca4a946cca582eae130df4Mussa                        choices=['480p', '720p', '1080p', '720p_1080p'],
49f004c14a79136a52421cae74fec416fa1c18fabemussa                        help="Video definition to use.")
50f004c14a79136a52421cae74fec416fa1c18fabemussa
514c961233f9379390fa2e535ab5b638f92c0b8871mussa    parser.add_argument("--start",
524c961233f9379390fa2e535ab5b638f92c0b8871mussa                        default="00:01",
53f004c14a79136a52421cae74fec416fa1c18fabemussa                        help="Time to start taking screenshots. (mm:ss)")
54f004c14a79136a52421cae74fec416fa1c18fabemussa
554c961233f9379390fa2e535ab5b638f92c0b8871mussa    parser.add_argument("--stop",
56f004c14a79136a52421cae74fec416fa1c18fabemussa                        help="Time to stop taking screenshots. (mm:ss).")
57f004c14a79136a52421cae74fec416fa1c18fabemussa
58f004c14a79136a52421cae74fec416fa1c18fabemussa    parser.add_argument("interval",
59f004c14a79136a52421cae74fec416fa1c18fabemussa                        type=int,
60f004c14a79136a52421cae74fec416fa1c18fabemussa                        help="Seconds between two successive captures.")
61f004c14a79136a52421cae74fec416fa1c18fabemussa
62f004c14a79136a52421cae74fec416fa1c18fabemussa    args = parser.parse_args()
63f004c14a79136a52421cae74fec416fa1c18fabemussa
64f004c14a79136a52421cae74fec416fa1c18fabemussa    time_format = '%M:%S'
65f004c14a79136a52421cae74fec416fa1c18fabemussa
66f004c14a79136a52421cae74fec416fa1c18fabemussa    # Parse time arguments from user
674c961233f9379390fa2e535ab5b638f92c0b8871mussa    # Start time has a default argument of 01:00, parse right away
684c961233f9379390fa2e535ab5b638f92c0b8871mussa    # Parse stop time later as we don't know the length of the video,
694c961233f9379390fa2e535ab5b638f92c0b8871mussa    # the factory does
70f004c14a79136a52421cae74fec416fa1c18fabemussa    tmp = datetime.datetime.strptime(args.start, time_format)
71f004c14a79136a52421cae74fec416fa1c18fabemussa    start = datetime.timedelta(minutes=tmp.minute, seconds=tmp.second)
72f004c14a79136a52421cae74fec416fa1c18fabemussa
73f004c14a79136a52421cae74fec416fa1c18fabemussa    with chrome.Chrome() as cr:
74f004c14a79136a52421cae74fec416fa1c18fabemussa        bindir = '/usr/local/autotest/cros/video'
75f004c14a79136a52421cae74fec416fa1c18fabemussa
7655e4a38297449fcf3dbd693e2e6afe72fa84b519Ahmed Fakhry        cr.browser.platform.SetHTTPServerDirectories(bindir)
7755e4a38297449fcf3dbd693e2e6afe72fa84b519Ahmed Fakhry
7855e4a38297449fcf3dbd693e2e6afe72fa84b519Ahmed Fakhry        factory = media_test_factory.MediaTestFactory(
7955e4a38297449fcf3dbd693e2e6afe72fa84b519Ahmed Fakhry                      cr.browser.tabs[0],
8055e4a38297449fcf3dbd693e2e6afe72fa84b519Ahmed Fakhry                      cr.browser.platform.http_server,
8155e4a38297449fcf3dbd693e2e6afe72fa84b519Ahmed Fakhry                      bindir,
8255e4a38297449fcf3dbd693e2e6afe72fa84b519Ahmed Fakhry                      'dev',
8355e4a38297449fcf3dbd693e2e6afe72fa84b519Ahmed Fakhry                      args.name,
8455e4a38297449fcf3dbd693e2e6afe72fa84b519Ahmed Fakhry                      args.format,
8555e4a38297449fcf3dbd693e2e6afe72fa84b519Ahmed Fakhry                      args.definition)
86f004c14a79136a52421cae74fec416fa1c18fabemussa
874c961233f9379390fa2e535ab5b638f92c0b8871mussa        # if stop time is not specified, use the length of the video
884c961233f9379390fa2e535ab5b638f92c0b8871mussa        if args.stop is None:
894c961233f9379390fa2e535ab5b638f92c0b8871mussa            stop = factory.media_length
904c961233f9379390fa2e535ab5b638f92c0b8871mussa        else:
914c961233f9379390fa2e535ab5b638f92c0b8871mussa            tmp = datetime.datetime.strptime(args.stop, time_format)
924c961233f9379390fa2e535ab5b638f92c0b8871mussa            stop = datetime.timedelta(minutes=tmp.minute, seconds=tmp.second)
934c961233f9379390fa2e535ab5b638f92c0b8871mussa
94f004c14a79136a52421cae74fec416fa1c18fabemussa        file_utils.rm_dir_if_exists(factory.test_working_dir)
95f004c14a79136a52421cae74fec416fa1c18fabemussa
96f004c14a79136a52421cae74fec416fa1c18fabemussa        file_utils.make_leaf_dir(factory.test_working_dir)
97f004c14a79136a52421cae74fec416fa1c18fabemussa
98f004c14a79136a52421cae74fec416fa1c18fabemussa        seq = sequence_generator.generate_interval_sequence(start,
99f004c14a79136a52421cae74fec416fa1c18fabemussa                                                            stop,
100f004c14a79136a52421cae74fec416fa1c18fabemussa                                                            args.interval)
101f004c14a79136a52421cae74fec416fa1c18fabemussa
102f004c14a79136a52421cae74fec416fa1c18fabemussa        collector = factory.make_video_screenshot_collector()
103f004c14a79136a52421cae74fec416fa1c18fabemussa
104f004c14a79136a52421cae74fec416fa1c18fabemussa        collector.ensure_player_is_ready()
105f004c14a79136a52421cae74fec416fa1c18fabemussa
106f004c14a79136a52421cae74fec416fa1c18fabemussa        collector.collect_multiple_screenshots(seq)
107f004c14a79136a52421cae74fec416fa1c18fabemussa
108f004c14a79136a52421cae74fec416fa1c18fabemussa
109f004c14a79136a52421cae74fec416fa1c18fabemussaif __name__ == '__main__':
110f004c14a79136a52421cae74fec416fa1c18fabemussa    main()