1 /* 2 * Copyright (C) 2024 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 //! This module implements the IMediaQuality AIDL interface. 17 18 use android_hardware_tv_mediaquality::aidl::android::hardware::tv::mediaquality::{ 19 IMediaQuality::IMediaQuality, 20 IMediaQualityCallback::IMediaQualityCallback, 21 AmbientBacklightEvent::AmbientBacklightEvent, 22 AmbientBacklightSettings::AmbientBacklightSettings, 23 IPictureProfileAdjustmentListener::IPictureProfileAdjustmentListener, 24 IPictureProfileChangedListener::IPictureProfileChangedListener, 25 ParamCapability::ParamCapability, 26 ParameterName::ParameterName, 27 PictureParameters::PictureParameters, 28 ISoundProfileAdjustmentListener::ISoundProfileAdjustmentListener, 29 ISoundProfileChangedListener::ISoundProfileChangedListener, 30 SoundParameters::SoundParameters, 31 VendorParamCapability::VendorParamCapability, 32 VendorParameterIdentifier::VendorParameterIdentifier, 33 }; 34 use binder::{Interface, Strong}; 35 use std::sync::{Arc, Mutex}; 36 use std::thread; 37 38 /// Defined so we can implement the IMediaQuality AIDL interface. 39 pub struct MediaQualityService { 40 callback: Arc<Mutex<Option<Strong<dyn IMediaQualityCallback>>>>, 41 ambient_backlight_enabled: Arc<Mutex<bool>>, 42 ambient_backlight_detector_settings: Arc<Mutex<AmbientBacklightSettings>>, 43 auto_pq_supported: Arc<Mutex<bool>>, 44 auto_pq_enabled: Arc<Mutex<bool>>, 45 auto_sr_supported: Arc<Mutex<bool>>, 46 auto_sr_enabled: Arc<Mutex<bool>>, 47 auto_aq_supported: Arc<Mutex<bool>>, 48 auto_aq_enabled: Arc<Mutex<bool>>, 49 picture_profile_adjustment_listener: 50 Arc<Mutex<Option<Strong<dyn IPictureProfileAdjustmentListener>>>>, 51 sound_profile_adjustment_listener: 52 Arc<Mutex<Option<Strong<dyn ISoundProfileAdjustmentListener>>>>, 53 picture_profile_changed_listener: Arc<Mutex<Option<Strong<dyn IPictureProfileChangedListener>>>>, 54 sound_profile_changed_listener: Arc<Mutex<Option<Strong<dyn ISoundProfileChangedListener>>>>, 55 } 56 57 impl MediaQualityService { 58 59 /// Create a new instance of the MediaQualityService. new() -> Self60 pub fn new() -> Self { 61 Self { 62 callback: Arc::new(Mutex::new(None)), 63 ambient_backlight_enabled: Arc::new(Mutex::new(true)), 64 ambient_backlight_detector_settings: 65 Arc::new(Mutex::new(AmbientBacklightSettings::default())), 66 auto_pq_supported: Arc::new(Mutex::new(false)), 67 auto_pq_enabled: Arc::new(Mutex::new(false)), 68 auto_sr_supported: Arc::new(Mutex::new(false)), 69 auto_sr_enabled: Arc::new(Mutex::new(false)), 70 auto_aq_supported: Arc::new(Mutex::new(false)), 71 auto_aq_enabled: Arc::new(Mutex::new(false)), 72 picture_profile_adjustment_listener: Arc::new(Mutex::new(None)), 73 sound_profile_adjustment_listener: Arc::new(Mutex::new(None)), 74 picture_profile_changed_listener: Arc::new(Mutex::new(None)), 75 sound_profile_changed_listener: Arc::new(Mutex::new(None)), 76 } 77 } 78 } 79 80 impl Interface for MediaQualityService {} 81 82 impl IMediaQuality for MediaQualityService { 83 setCallback( &self, callback: &Strong<dyn IMediaQualityCallback> ) -> binder::Result<()>84 fn setCallback( 85 &self, 86 callback: &Strong<dyn IMediaQualityCallback> 87 ) -> binder::Result<()> { 88 println!("Received callback: {:?}", callback); 89 let mut cb = self.callback.lock().unwrap(); 90 *cb = Some(callback.clone()); 91 Ok(()) 92 } 93 setAmbientBacklightDetector( &self, settings: &AmbientBacklightSettings ) -> binder::Result<()>94 fn setAmbientBacklightDetector( 95 &self, 96 settings: &AmbientBacklightSettings 97 ) -> binder::Result<()> { 98 println!("Received settings: {:?}", settings); 99 let mut ambient_backlight_detector_settings = self.ambient_backlight_detector_settings.lock().unwrap(); 100 ambient_backlight_detector_settings.packageName = settings.packageName.clone(); 101 ambient_backlight_detector_settings.source = settings.source; 102 ambient_backlight_detector_settings.maxFramerate = settings.maxFramerate; 103 ambient_backlight_detector_settings.colorFormat = settings.colorFormat; 104 ambient_backlight_detector_settings.hZonesNumber = settings.hZonesNumber; 105 ambient_backlight_detector_settings.vZonesNumber = settings.vZonesNumber; 106 ambient_backlight_detector_settings.hasLetterbox = settings.hasLetterbox; 107 ambient_backlight_detector_settings.threshold = settings.threshold; 108 Ok(()) 109 } 110 setAmbientBacklightDetectionEnabled(&self, enabled: bool) -> binder::Result<()>111 fn setAmbientBacklightDetectionEnabled(&self, enabled: bool) -> binder::Result<()> { 112 println!("Received enabled: {}", enabled); 113 let mut ambient_backlight_enabled = self.ambient_backlight_enabled.lock().unwrap(); 114 *ambient_backlight_enabled = enabled; 115 if enabled { 116 println!("Enable Ambient Backlight detection"); 117 thread::scope(|s| { 118 s.spawn(|| { 119 let cb = self.callback.lock().unwrap(); 120 if let Some(cb) = &*cb { 121 let enabled_event = AmbientBacklightEvent::Enabled(true); 122 cb.notifyAmbientBacklightEvent(&enabled_event).unwrap(); 123 } 124 }); 125 }); 126 } else { 127 println!("Disable Ambient Backlight detection"); 128 thread::scope(|s| { 129 s.spawn(|| { 130 let cb = self.callback.lock().unwrap(); 131 if let Some(cb) = &*cb { 132 let disabled_event = AmbientBacklightEvent::Enabled(false); 133 cb.notifyAmbientBacklightEvent(&disabled_event).unwrap(); 134 } 135 }); 136 }); 137 } 138 Ok(()) 139 } 140 getAmbientBacklightDetectionEnabled(&self) -> binder::Result<bool>141 fn getAmbientBacklightDetectionEnabled(&self) -> binder::Result<bool> { 142 let ambient_backlight_enabled = self.ambient_backlight_enabled.lock().unwrap(); 143 Ok(*ambient_backlight_enabled) 144 } 145 isAutoPqSupported(&self) -> binder::Result<bool>146 fn isAutoPqSupported(&self) -> binder::Result<bool> { 147 let auto_pq_supported = self.auto_pq_supported.lock().unwrap(); 148 Ok(*auto_pq_supported) 149 } 150 getAutoPqEnabled(&self) -> binder::Result<bool>151 fn getAutoPqEnabled(&self) -> binder::Result<bool> { 152 let auto_pq_enabled = self.auto_pq_enabled.lock().unwrap(); 153 Ok(*auto_pq_enabled) 154 } 155 setAutoPqEnabled(&self, enabled: bool) -> binder::Result<()>156 fn setAutoPqEnabled(&self, enabled: bool) -> binder::Result<()> { 157 let mut auto_pq_enabled = self.auto_pq_enabled.lock().unwrap(); 158 *auto_pq_enabled = enabled; 159 if enabled { 160 println!("Enable auto picture quality"); 161 } else { 162 println!("Disable auto picture quality"); 163 } 164 Ok(()) 165 } 166 isAutoSrSupported(&self) -> binder::Result<bool>167 fn isAutoSrSupported(&self) -> binder::Result<bool> { 168 let auto_sr_supported = self.auto_sr_supported.lock().unwrap(); 169 Ok(*auto_sr_supported) 170 } 171 getAutoSrEnabled(&self) -> binder::Result<bool>172 fn getAutoSrEnabled(&self) -> binder::Result<bool> { 173 let auto_sr_enabled = self.auto_sr_enabled.lock().unwrap(); 174 Ok(*auto_sr_enabled) 175 } 176 setAutoSrEnabled(&self, enabled: bool) -> binder::Result<()>177 fn setAutoSrEnabled(&self, enabled: bool) -> binder::Result<()> { 178 let mut auto_sr_enabled = self.auto_sr_enabled.lock().unwrap(); 179 *auto_sr_enabled = enabled; 180 if enabled { 181 println!("Enable auto super resolution"); 182 } else { 183 println!("Disable auto super resolution"); 184 } 185 Ok(()) 186 } 187 isAutoAqSupported(&self) -> binder::Result<bool>188 fn isAutoAqSupported(&self) -> binder::Result<bool> { 189 let auto_aq_supported = self.auto_aq_supported.lock().unwrap(); 190 Ok(*auto_aq_supported) 191 } 192 getAutoAqEnabled(&self) -> binder::Result<bool>193 fn getAutoAqEnabled(&self) -> binder::Result<bool> { 194 let auto_aq_enabled = self.auto_aq_enabled.lock().unwrap(); 195 Ok(*auto_aq_enabled) 196 } 197 setAutoAqEnabled(&self, enabled: bool) -> binder::Result<()>198 fn setAutoAqEnabled(&self, enabled: bool) -> binder::Result<()> { 199 let mut auto_aq_enabled = self.auto_aq_enabled.lock().unwrap(); 200 *auto_aq_enabled = enabled; 201 if enabled { 202 println!("Enable auto audio quality"); 203 } else { 204 println!("Disable auto audio quality"); 205 } 206 Ok(()) 207 } 208 getPictureProfileListener(&self) -> binder::Result<binder::Strong<dyn IPictureProfileChangedListener>>209 fn getPictureProfileListener(&self) -> binder::Result<binder::Strong<dyn IPictureProfileChangedListener>> { 210 println!("getPictureProfileListener"); 211 let listener = self.picture_profile_changed_listener.lock().unwrap(); 212 listener.clone().ok_or(binder::StatusCode::UNKNOWN_ERROR.into()) 213 } 214 setPictureProfileAdjustmentListener( &self, picture_profile_adjustment_listener: &Strong<dyn IPictureProfileAdjustmentListener> ) -> binder::Result<()>215 fn setPictureProfileAdjustmentListener( 216 &self, 217 picture_profile_adjustment_listener: &Strong<dyn IPictureProfileAdjustmentListener> 218 ) -> binder::Result<()> { 219 println!("Received picture profile adjustment"); 220 let mut listener = self.picture_profile_adjustment_listener.lock().unwrap(); 221 *listener = Some(picture_profile_adjustment_listener.clone()); 222 Ok(()) 223 } 224 sendDefaultPictureParameters(&self, _picture_parameters: &PictureParameters) -> binder::Result<()>225 fn sendDefaultPictureParameters(&self, _picture_parameters: &PictureParameters) -> binder::Result<()>{ 226 println!("Received picture parameters"); 227 Ok(()) 228 } 229 getSoundProfileListener(&self) -> binder::Result<binder::Strong<dyn ISoundProfileChangedListener>>230 fn getSoundProfileListener(&self) -> binder::Result<binder::Strong<dyn ISoundProfileChangedListener>> { 231 println!("getSoundProfileListener"); 232 let listener = self.sound_profile_changed_listener.lock().unwrap(); 233 listener.clone().ok_or(binder::StatusCode::UNKNOWN_ERROR.into()) 234 } 235 setSoundProfileAdjustmentListener( &self, sound_profile_adjustment_listener: &Strong<dyn ISoundProfileAdjustmentListener> ) -> binder::Result<()>236 fn setSoundProfileAdjustmentListener( 237 &self, 238 sound_profile_adjustment_listener: &Strong<dyn ISoundProfileAdjustmentListener> 239 ) -> binder::Result<()> { 240 println!("Received sound profile adjustment"); 241 let mut listener = self.sound_profile_adjustment_listener.lock().unwrap(); 242 *listener = Some(sound_profile_adjustment_listener.clone()); 243 Ok(()) 244 } 245 sendDefaultSoundParameters(&self, _sound_parameters: &SoundParameters) -> binder::Result<()>246 fn sendDefaultSoundParameters(&self, _sound_parameters: &SoundParameters) -> binder::Result<()>{ 247 println!("Received sound parameters"); 248 Ok(()) 249 } 250 getParamCaps( &self, param_names: &[ParameterName], _caps: &mut Vec<ParamCapability> ) -> binder::Result<()>251 fn getParamCaps( 252 &self, 253 param_names: &[ParameterName], 254 _caps: &mut Vec<ParamCapability> 255 ) -> binder::Result<()> { 256 println!("getParamCaps. len= {}", param_names.len()); 257 Ok(()) 258 } 259 getVendorParamCaps( &self, param_names: &[VendorParameterIdentifier], _caps: &mut Vec<VendorParamCapability> ) -> binder::Result<()>260 fn getVendorParamCaps( 261 &self, 262 param_names: &[VendorParameterIdentifier], 263 _caps: &mut Vec<VendorParamCapability> 264 ) -> binder::Result<()> { 265 println!("getVendorParamCaps. len= {}", param_names.len()); 266 Ok(()) 267 } 268 sendPictureParameters(&self, _picture_parameters: &PictureParameters) -> binder::Result<()>269 fn sendPictureParameters(&self, _picture_parameters: &PictureParameters) -> binder::Result<()>{ 270 println!("Received picture parameters"); 271 Ok(()) 272 } 273 sendSoundParameters(&self, _sound_parameters: &SoundParameters) -> binder::Result<()>274 fn sendSoundParameters(&self, _sound_parameters: &SoundParameters) -> binder::Result<()>{ 275 println!("Received sound parameters"); 276 Ok(()) 277 } 278 } 279