Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: HDCP/HDR settings cache updated #631

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
28 changes: 28 additions & 0 deletions core/main/src/service/context_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,34 @@ impl ContextManager {
warn!("No processor to set TimeZoneChanged status listener")
}

// Setup the DisplayConnectionChanged status listener
debug!("Subscribing for change in Display Connection");
if ps
.get_client()
.send_extn_request(DeviceEventRequest {
event: DeviceEvent::InputChanged,
bsenth200 marked this conversation as resolved.
Show resolved Hide resolved
subscribe: true,
callback_type: DeviceEventCallback::ExtnEvent,
})
.await
.is_err()
{
warn!("No processor to set InputChanged listener")
}

if ps
.get_client()
.send_extn_request(DeviceEventRequest {
event: DeviceEvent::HdrChanged,
subscribe: true,
callback_type: DeviceEventCallback::ExtnEvent,
})
.await
.is_err()
{
warn!("No processor to set HdrChanged listener")
}

let ps_c = ps.clone();

// Asynchronously get context and update the state
Expand Down
21 changes: 11 additions & 10 deletions device/thunder_ripple_sdk/src/events/thunder_event_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ use ripple_sdk::{
device_events::{DeviceEvent, DeviceEventCallback},
device_operator::DeviceSubscribeRequest,
device_request::{
AudioProfile, InternetConnectionStatus, NetworkResponse, NetworkState, NetworkType,
PowerState, SystemPowerState, VoiceGuidanceState,
AudioProfile, HDCPStatus, InternetConnectionStatus, NetworkResponse, NetworkState,
NetworkType, PowerState, SystemPowerState, VoiceGuidanceState,
},
},
},
Expand All @@ -43,12 +43,6 @@ use serde::{Deserialize, Serialize};

use crate::{thunder_state::ThunderState, utils::get_audio_profile_from_value};

#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ActiveInputThunderEvent {
pub active_input: bool,
}

#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct TimeZoneChangedThunderEvent {
Expand All @@ -67,9 +61,16 @@ pub struct ResolutionChangedEvent {
pub resolution: String,
}

#[derive(Debug, Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct DisplayConnectionChangedEvent {
#[serde(rename = "HDCPStatus")]
pub hdcp_status: HDCPStatus,
}

#[derive(Debug, Clone, Deserialize)]
pub enum ThunderEventMessage {
ActiveInput(ActiveInputThunderEvent),
DisplayConnection(DisplayConnectionChangedEvent),
Resolution(ResolutionChangedEvent),
Network(NetworkResponse),
Internet(InternetConnectionStatus),
Expand All @@ -85,7 +86,7 @@ impl ThunderEventMessage {
match device_event {
DeviceEvent::InputChanged | DeviceEvent::HdrChanged => {
if let Ok(v) = serde_json::from_value(value.clone()) {
return Some(ThunderEventMessage::ActiveInput(v));
return Some(ThunderEventMessage::DisplayConnection(v));
}
}
DeviceEvent::ScreenResolutionChanged | DeviceEvent::VideoResolutionChanged => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ use super::super::thunder_device_info::{
get_dimension_from_resolution, ThunderDeviceInfoRequestProcessor,
};

pub fn is_active_input(value: ThunderEventMessage) -> bool {
if let ThunderEventMessage::ActiveInput(_) = value {
pub fn is_display_connection_changed(value: ThunderEventMessage) -> bool {
if let ThunderEventMessage::DisplayConnection(_) = value {
return true;
}
false
Expand All @@ -74,7 +74,7 @@ pub fn is_resolution(value: ThunderEventMessage) -> bool {
}

// -----------------------
// Active Input Changed
// Display Connection Changed
pub struct HDCPEventHandler;

impl HDCPEventHandler {
Expand All @@ -83,14 +83,15 @@ impl HDCPEventHandler {
value: ThunderEventMessage,
callback_type: DeviceEventCallback,
) {
if let ThunderEventMessage::ActiveInput(input) = value {
if input.active_input {
debug!("activeInput changed");
}
if let ThunderEventMessage::DisplayConnection(_connection) = value {
debug!("HDCPEventHandler: display connection changed");
}

let state_c = state.clone();
let cached_state = CachedState::new(state.clone());

tokio::spawn(async move {
let map = ThunderDeviceInfoRequestProcessor::get_hdcp_support(state).await;
let map = ThunderDeviceInfoRequestProcessor::update_hdcp_cache(cached_state).await;
if let Ok(v) = Self::get_extn_event(map, callback_type) {
ThunderEventHandler::callback_device_event(state_c, Self::get_mapped_event(), v)
}
Expand All @@ -104,7 +105,7 @@ impl ThunderEventHandlerProvider for HDCPEventHandler {
ThunderEventHandler {
request: Self::get_device_request(),
handle: Self::handle,
is_valid: is_active_input,
is_valid: is_display_connection_changed,
listeners: vec![id],
id: Self::get_mapped_event(),
callback_type,
Expand All @@ -116,7 +117,7 @@ impl ThunderEventHandlerProvider for HDCPEventHandler {
}

fn event_name() -> String {
"activeInputChanged".into()
"onDisplayConnectionChanged".into()
}

fn module() -> String {
Expand All @@ -135,14 +136,15 @@ impl HDREventHandler {
value: ThunderEventMessage,
callback_type: DeviceEventCallback,
) {
if let ThunderEventMessage::ActiveInput(input) = value {
if input.active_input {
debug!("activeInput changed");
}
if let ThunderEventMessage::DisplayConnection(_connection) = value {
debug!("HDREventHandler: display connection changed");
}

let state_c = state.clone();
let cached_state = CachedState::new(state.clone());

tokio::spawn(async move {
let map = ThunderDeviceInfoRequestProcessor::get_hdr(state).await;
let map = ThunderDeviceInfoRequestProcessor::get_hdr(cached_state).await;
if let Ok(v) = Self::get_extn_event(map, callback_type) {
ThunderEventHandler::callback_device_event(state_c, Self::get_mapped_event(), v)
}
Expand All @@ -157,7 +159,7 @@ impl ThunderEventHandlerProvider for HDREventHandler {
ThunderEventHandler {
request: Self::get_device_request(),
handle: Self::handle,
is_valid: is_active_input,
is_valid: is_display_connection_changed,
listeners: vec![id],
id: Self::get_mapped_event(),
callback_type,
Expand All @@ -169,7 +171,7 @@ impl ThunderEventHandlerProvider for HDREventHandler {
}

fn event_name() -> String {
"activeInputChanged".into()
"onDisplayConnectionChanged".into()
}

fn module() -> String {
Expand Down
48 changes: 40 additions & 8 deletions device/thunder_ripple_sdk/src/processors/thunder_device_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ pub struct CachedDeviceInfo {
make: Option<String>,
hdcp_support: Option<HashMap<HdcpProfile, bool>>,
hdr_profile: Option<HashMap<HdrProfile, bool>>,
hdcp_status: Option<HDCPStatus>,
version: Option<FireboltSemanticVersion>,
}

Expand Down Expand Up @@ -183,6 +184,15 @@ impl CachedState {
let _ = hdcp.hdcp_support.insert(value);
}

fn get_hdcp_status(&self) -> Option<HDCPStatus> {
self.cached.read().unwrap().hdcp_status.clone()
}

fn update_hdcp_status(&self, value: HDCPStatus) {
let mut hdcp = self.cached.write().unwrap();
let _ = hdcp.hdcp_status.insert(value);
}

fn get_hdr(&self) -> Option<HashMap<HdrProfile, bool>> {
self.cached.read().unwrap().hdr_profile.clone()
}
Expand Down Expand Up @@ -644,7 +654,12 @@ impl ThunderDeviceInfoRequestProcessor {
.is_ok()
}

pub async fn get_hdcp_support(state: ThunderState) -> HashMap<HdcpProfile, bool> {
pub async fn update_hdcp_cache(state: CachedState) -> HashMap<HdcpProfile, bool> {
Self::get_hdcp_status(&state).await;
Self::get_hdcp_support(state.clone()).await
}

pub async fn get_hdcp_support(state: CachedState) -> HashMap<HdcpProfile, bool> {
let response = state
.get_thunder_client()
.call(DeviceCallRequest {
Expand Down Expand Up @@ -679,16 +694,17 @@ impl ThunderDeviceInfoRequestProcessor {
hdcp_response.insert(HdcpProfile::Hdcp2_2, is_hdcp_supported);
}

// update cache
state.update_hdcp_support(hdcp_response.clone());

hdcp_response
}

async fn get_cached_hdcp_support(state: CachedState) -> HashMap<HdcpProfile, bool> {
if let Some(v) = state.get_hdcp_support() {
v
} else {
let v = Self::get_hdcp_support(state.clone().state).await;
state.update_hdcp_support(v.clone());
v
Self::get_hdcp_support(state.clone()).await
}
}

Expand Down Expand Up @@ -722,11 +738,23 @@ impl ThunderDeviceInfoRequestProcessor {
if let Ok(thdcp) = serde_json::from_value::<ThunderHDCPStatus>(resp.message) {
response = thdcp.hdcp_status;
}

// update cache
state.update_hdcp_status(response.clone());

response
}

async fn get_cached_hdcp_status(state: &CachedState) -> HDCPStatus {
if let Some(v) = state.get_hdcp_status() {
v
} else {
Self::get_hdcp_status(state).await
}
}

async fn hdcp_status(state: CachedState, req: ExtnMessage) -> bool {
let response = Self::get_hdcp_status(&state).await;
let response = Self::get_cached_hdcp_status(&state).await;

Self::respond(
state.get_client(),
Expand All @@ -747,13 +775,13 @@ impl ThunderDeviceInfoRequestProcessor {
if let Some(v) = state.get_hdr() {
v
} else {
let v = Self::get_hdr(state.clone().state).await;
let v = Self::get_hdr(state.clone()).await;
state.update_hdr_support(v.clone());
v
}
}

pub async fn get_hdr(state: ThunderState) -> HashMap<HdrProfile, bool> {
pub async fn get_hdr(state: CachedState) -> HashMap<HdrProfile, bool> {
let response = state
.get_thunder_client()
.call(DeviceCallRequest {
Expand Down Expand Up @@ -787,6 +815,10 @@ impl ThunderDeviceInfoRequestProcessor {
HdrProfile::Hdr10plus,
0 != (supported_cap & hdr_flags::HDRSTANDARD_HDR10PLUS),
);

// update cache
state.update_hdr_support(hm.clone());

hm
}

Expand Down Expand Up @@ -1392,7 +1424,7 @@ impl ThunderDeviceInfoRequestProcessor {
},
async {
if device_info_authorized {
Some(Self::get_hdcp_status(&state).await)
Some(Self::get_cached_hdcp_status(&state).await)
} else {
None
}
Expand Down
Loading