1import logging
2import re
3import threading
4from utilities import constants
5import os
6
7
8
9class VideoRecording:
10    """Video recording and saving functions for reporting"""
11
12    def __init__(self, device, class_name):
13        self._device = device
14        self._class_name = class_name.lower()
15        self.thread = None
16
17    # Enable screen recording for device
18    def enable_screen_recording(self):
19        ENABLE_SCREEN_RECORDING_COMMAND=(
20            f'{constants.SCREEN_RECORDING_COMMAND} {self.get_screen_recording_path()}')
21        logging.info("Enable screen recording with command  %s: ", ENABLE_SCREEN_RECORDING_COMMAND)
22
23        def spin():
24            self._device.adb.shell(ENABLE_SCREEN_RECORDING_COMMAND)
25
26        self.thread = threading.Thread(target=spin)
27        self.thread.start()
28
29    # Stop screen recording for device
30    def stop_screen_recording(self):
31        logging.info("Stop screen recording on %s", self._device)
32        self._device.adb.shell(constants.STOP_VIDEO_RECORDING)
33        if self.thread is not None:
34            self.thread.join()
35
36    # Move recorded video file to logs
37    def pull_recording_file(self, log_path):
38        logging.info("Move recorded video file from %s to <%s>", self._device,
39                     log_path)
40        self._device.adb.pull([self.get_screen_recording_path(), log_path])
41
42    # Delete video file from device
43    def delete_screen_recording_from_device(self):
44        DELETE_SCREEN_RECORDING_FILE = self.get_screen_recording_path()
45        logging.info("Deleting file <%s> on device %s",
46                     DELETE_SCREEN_RECORDING_FILE, self._device)
47        DELETE_SCREEN_RECORDING_COMMAND = f'{constants.DELETE_SCREEN_RECORDING}{DELETE_SCREEN_RECORDING_FILE}'
48        self._device.adb.shell(DELETE_SCREEN_RECORDING_COMMAND)
49
50    # Generate screen recording path
51    def get_screen_recording_path(self):
52        logging.info("Generating Screen Recording Path for %s", self._device)
53        m = f'{self._device}'.split('|')[1]
54        device = m.split('>')[0]
55
56        RECORDED_VIDEO_FILE_PATH = f'{constants.RECORDED_VIDEO_FILE_LOCATION}{device}{constants.RECORDED_VIDEO_FILE_OUTPUT_FILE}{self._class_name}'
57        logging.info("Screen recording for %s is %s", self._device, RECORDED_VIDEO_FILE_PATH)
58        return RECORDED_VIDEO_FILE_PATH