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 def __init__(self, addr, tc, client_id): 25 self.address = addr 26 self.test_counter = tc 27 self.client_id = client_id 28 self.log = logger.create_tagged_trace_logger('FuchsiaAudioLib') 29 30 def startOutputSave(self): 31 """Starts saving audio output on the device 32 33 Returns: 34 Dictionary is success, error if error. 35 """ 36 test_cmd = "audio_facade.StartOutputSave" 37 test_args = {} 38 test_id = self.build_id(self.test_counter) 39 self.test_counter += 1 40 41 return self.send_command(test_id, test_cmd, test_args) 42 43 def stopOutputSave(self): 44 """Stops saving audio output on the device 45 46 Returns: 47 Dictionary is success, error if error. 48 """ 49 test_cmd = "audio_facade.StopOutputSave" 50 test_args = {} 51 test_id = self.build_id(self.test_counter) 52 self.test_counter += 1 53 54 return self.send_command(test_id, test_cmd, test_args) 55 56 def getOutputAudio(self, save_path): 57 """Gets the saved audio in base64 encoding. Use base64.b64decode. 58 59 Args: 60 save_path: The path to save the raw audio 61 62 Returns: 63 True if success, False if error. 64 """ 65 test_cmd = "audio_facade.GetOutputAudio" 66 test_args = {} 67 test_id = self.build_id(self.test_counter) 68 self.test_counter += 1 69 70 result = self.send_command(test_id, test_cmd, test_args) 71 if result.get("error") is not None: 72 self.log.error("Failed to get recorded audio.") 73 return False 74 75 f = open(save_path, "wb") 76 f.write(base64.b64decode(result.get('result'))) 77 f.close() 78 self.log.info("Raw audio file captured at {}".format(save_path)) 79 return True 80