1# Copyright 2017 The Chromium OS 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
5"""Get v4l2 interface, and chrome processes which access the video interface"""
6
7from __future__ import print_function
8
9import logging
10
11
12LSOF_CHROME_VIDEO = {
13                    '2bd9:0011': 3,
14                    '046d:0843': 2,
15                    '046d:082d': 2,
16                    '046d:0853': 2,
17                    '064e:9405': 2,
18                    '046d:0853': 2
19                    }
20
21
22def get_video_by_name(dut, name):
23    """
24    Get v4l2 interface based on WebCamera
25    @param dut: The handle of the device under test. Should be initialized in
26                 autotest.
27    @param name: The name of web camera
28                 For example: 'Huddly GO'
29                              'Logitech Webcam C930e'
30    @returns:  if video device v4l2 found, return True,
31               else return False.
32    """
33    cmd = 'ls /sys/class/video4linux/'
34    video4linux_list = dut.run(cmd, ignore_status=True).stdout.split()
35    for video_dev in video4linux_list:
36        cmd = 'cat /sys/class/video4linux/{}/name'.format(video_dev)
37        video_dev_name = dut.run(cmd, ignore_status=True).stdout.strip()
38        logging.info('---%s', cmd)
39        logging.info('---%s', video_dev_name)
40        if name in video_dev_name and not 'overview' in video_dev_name:
41            logging.info('---found interface for %s', name)
42            return video_dev
43    return None
44
45
46def get_lsof4_video(dut, video):
47    """
48    Get output of chrome processes which attach to video device.
49    @param dut: The handle of the device under test. Should be initialized in
50                autotest.
51    @param video: video device name for camera.
52    @returns: output of lsof /dev/videox.
53    """
54    cmd = 'lsof /dev/{} | grep chrome'.format(video)
55    lsof_output = dut.run(cmd, ignore_status=True).stdout.strip().split('\n')
56    logging.info('---%s', cmd)
57    logging.info('---%s', lsof_output)
58    return lsof_output
59
60
61def get_video_streams(dut, name):
62    """
63    Get output of chrome processes which attach to video device.
64    @param dut: The handle of the device under test.
65    @param name: name of camera.
66    @returns: output of lsof for v4l2 device.
67    """
68    video_dev = get_video_by_name(dut, name)
69    lsof_output = get_lsof4_video(dut, video_dev)
70    return lsof_output
71
72
73def check_v4l2_interface(dut, vidpid, camera):
74    """
75    Check v4l2 interface exists for camera.
76    @param dut: The handle of the device under test.
77    @param vidpid: vidpid of camera.
78    @param camera: name of camera
79    @returns: True if v4l2 interface found for camera,
80              False if not found.
81    """
82    logging.info('---check v4l2 interface for %s', camera)
83    if get_video_by_name(dut, camera):
84        return True, None
85    return False, '{} have no v4l2 interface.'.format(camera)
86
87
88def check_video_stream(dut, is_muted, vidpid, camera):
89    """
90    Check camera is streaming as expected.
91    @param dut: The handle of the device under test.
92    @is_streaming: True if camera is expected to be streaming,
93                   False if not.
94    @param vidpid: vidpid of camera
95    @param camera: name of camera.
96    @returns: True if camera is streaming or not based on
97              expectation,
98              False, errMsg if not found.
99    """
100    process_camera = get_video_streams(dut, camera)
101    if is_muted:
102        if len(process_camera) >= LSOF_CHROME_VIDEO[vidpid]:
103            return False, '{} fails to stop video streaming.'.format(camera)
104    else:
105        if not len(process_camera) >= LSOF_CHROME_VIDEO[vidpid]:
106            return False, '{} fails to start video streaming.'.format(camera)
107    return True, None
108