1# Copyright 2018 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 5import logging 6import os 7import xml.etree.ElementTree as ET 8 9from autotest_lib.client.common_lib import error 10from autotest_lib.client.common_lib.cros import arc 11 12 13class ArcDeviceCapability(object): 14 """ 15 Generates capabilities status on DUT from media_codecs.xml in Android 16 container. Answers from the capabilities whether some capability is 17 satisfied on DUT. 18 """ 19 MEDIA_CODECS_XML_PATH = '/vendor/etc/media_codecs.xml' 20 MEDIA_CODEC_TO_CAPABILITY = { 21 'ARC.h264.decode': 'hw_dec_h264_arc', 22 'ARC.vp8.decode' : 'hw_dec_vp8_arc', 23 'ARC.vp9.decode' : 'hw_dec_vp9_arc', 24 'OMX.arc.h264.encode': 'hw_enc_h264_arc', 25 'OMX.arc.vp8.encode' : 'hw_enc_vp8_arc', 26 'OMX.arc.vp9.encode' : 'hw_enc_vp9_arc', 27 } 28 29 30 def __init__(self): 31 self.capabilities = self._get_arc_capability() 32 33 def _get_arc_capability(self): 34 """ 35 Reads media_codecs.xml in android container and returns available ARC++ 36 capability. 37 38 @returns list: Available ARC++ capabilities on DUT. 39 """ 40 if not arc.is_android_container_alive(): 41 raise error.TestFail('Android container is not alive.') 42 43 # Reads media_codecs.xml 44 media_codecs_data = arc.read_android_file(self.MEDIA_CODECS_XML_PATH) 45 if not media_codecs_data: 46 raise error.TestFail('media_codecs.xml is empty.') 47 48 logging.debug('%s', media_codecs_data) 49 50 # Parses media_codecs.xml 51 root = ET.fromstring(media_codecs_data) 52 arc_caps = dict.fromkeys(self.MEDIA_CODEC_TO_CAPABILITY.values(), 'no') 53 for mc in root.iter('MediaCodec'): 54 codec_name = mc.get('name') 55 if codec_name in self.MEDIA_CODEC_TO_CAPABILITY: 56 arc_caps[self.MEDIA_CODEC_TO_CAPABILITY[codec_name]] = 'yes' 57 logging.debug('%r', arc_caps) 58 return arc_caps 59 60 def get_capability(self, cap): 61 """ 62 Decides if a device satisfies a required capability for an autotest. 63 64 @param cap: string, denoting one capability. It must be one of 65 self.MEDIA_CODEC_TO_CAPABILITY. 66 @returns 'yes' or 'no'. 67 """ 68 try: 69 return self.capabilities[cap] 70 except KeyError: 71 raise error.TestFail('Unexpected capability: %s' % cap) 72 73 def ensure_capability(self, cap): 74 """ 75 Raises TestNAError if a device doesn't satisfy cap. 76 77 @param cap: string, denoting one capability. 78 """ 79 if not have_capability(cap): 80 raise error.TestNAError('Missing Capability: %s' % cap) 81 82 def have_capability(self, cap): 83 """ 84 Return whether cap is available. 85 """ 86 return self.get_capability(cap) == 'yes' 87