• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
2#
3#   Copyright 2020 - The Android Open Source Project
4#
5#   Licensed under the Apache License, Version 2.0 (the "License");
6#   you may not use this file except in compliance with the License.
7#   You may obtain a copy of the License at
8#
9#       http://www.apache.org/licenses/LICENSE-2.0
10#
11#   Unless required by applicable law or agreed to in writing, software
12#   distributed under the License is distributed on an "AS IS" BASIS,
13#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14#   See the License for the specific language governing permissions and
15#   limitations under the License.
16
17from acts import logger
18from acts.controllers.fuchsia_lib.base_lib import BaseLib
19
20import base64
21
22
23class FuchsiaAudioLib(BaseLib):
24
25    def __init__(self, addr: str) -> None:
26        super().__init__(addr, "audio")
27
28    def startOutputSave(self):
29        """Starts saving audio output on the device
30
31        Returns:
32            Dictionary is success, error if error.
33        """
34        test_cmd = "audio_facade.StartOutputSave"
35        test_args = {}
36
37        return self.send_command(test_cmd, test_args)
38
39    def stopOutputSave(self):
40        """Stops saving audio output on the device
41
42        Returns:
43            Dictionary is success, error if error.
44        """
45        test_cmd = "audio_facade.StopOutputSave"
46        test_args = {}
47
48        return self.send_command(test_cmd, test_args)
49
50    def getOutputAudio(self, save_path):
51        """Gets the saved audio in base64 encoding. Use base64.b64decode.
52
53        Args:
54            save_path: The path to save the raw audio
55
56        Returns:
57            True if success, False if error.
58        """
59        test_cmd = "audio_facade.GetOutputAudio"
60        test_args = {}
61
62        result = self.send_command(test_cmd, test_args)
63        if result.get("error") is not None:
64            self.log.error("Failed to get recorded audio.")
65            return False
66
67        f = open(save_path, "wb")
68        f.write(base64.b64decode(result.get('result')))
69        f.close()
70        self.log.info("Raw audio file captured at {}".format(save_path))
71        return True
72