diff --git a/src/screencasting/pw_utils.rs b/src/screencasting/pw_utils.rs index 7914af20..80ab5115 100644 --- a/src/screencasting/pw_utils.rs +++ b/src/screencasting/pw_utils.rs @@ -448,150 +448,53 @@ impl PipeWire { ); inner.min_time_between_frames = min_frame_time; + // We have following cases when param_changed: + // + // 1. Modifier exists and its flags contain DONT_FIXATE + // + // Do test allocation, set CastState to ConfirmationPending and send param + // again. + // + // 2. Modifier exists and it doesn't need fixation + // + // Do test allocation to ensure the modifier work, then set CastState to + // Ready. Then set buffer to DMA. + // + // 3. Modifier doesn't exist + // + // TODO: set CastState to Ready and set buffer to SHM. + let object = pod.as_object().unwrap(); - let Some(prop_modifier) = - object.find_prop(spa::utils::Id(FormatProperties::VideoModifier.0)) - else { - warn!("modifier prop missing"); - stop_cast(); - return; - }; + let maybe_prop_modifier = object.find_prop(spa::utils::Id(FormatProperties::VideoModifier.0)); - if prop_modifier.flags().contains(PodPropFlags::DONT_FIXATE) { - debug!("fixating the modifier"); + match maybe_prop_modifier { + Some(prop_modifier) if prop_modifier.flags().contains(PodPropFlags::DONT_FIXATE) => { + debug!("fixating the modifier"); - let pod_modifier = prop_modifier.value(); - let Ok((_, modifiers)) = PodDeserializer::deserialize_from::>( - pod_modifier.as_bytes(), - ) else { - warn!("wrong modifier property type"); - stop_cast(); - return; - }; - - let ChoiceEnum::Enum { alternatives, .. } = modifiers.1 else { - warn!("wrong modifier choice type"); - stop_cast(); - return; - }; - - let (modifier, plane_count) = match find_preferred_modifier( - &gbm, - format_size, - fourcc, - alternatives, - ) { - Ok(x) => x, - Err(err) => { - warn!("couldn't find preferred modifier: {err:?}"); + let pod_modifier = prop_modifier.value(); + let Ok((_, modifiers)) = PodDeserializer::deserialize_from::>( + pod_modifier.as_bytes(), + ) else { + warn!("wrong modifier property type"); stop_cast(); return; - } - }; - - debug!( - "allocation successful \ - (modifier={modifier:?}, plane_count={plane_count}), \ - moving to confirmation pending" - ); - - *state = CastState::ConfirmationPending { - size: format_size, - alpha: format_has_alpha, - dma_negotiation: DmaNegotiation { - modifier, - plane_count: plane_count as i32, - }, - }; - - let fixated_format = FormatSet::from_iter([Format { - code: fourcc, - modifier, - }]); - - let mut b1 = Vec::new(); - let mut b2 = Vec::new(); - - let o1 = make_video_params( - &fixated_format, - format_size, - inner.refresh, - format_has_alpha, - ); - let pod1 = make_pod(&mut b1, o1); - - let o2 = make_video_params( - &formats, - format_size, - inner.refresh, - format_has_alpha, - ); - let mut params = [pod1, make_pod(&mut b2, o2)]; - - if let Err(err) = stream.update_params(&mut params) { - warn!("error updating stream params: {err:?}"); - stop_cast(); - } - - return; - } + }; - // Verify that alpha and modifier didn't change. - let plane_count = match &*state { - CastState::ConfirmationPending { - size, - alpha, - dma_negotiation, - } - | CastState::Ready { - size, - alpha, - dma_negotiation, - .. - } if *alpha == format_has_alpha - && dma_negotiation.modifier == Modifier::from(format.modifier()) => - { - let size = *size; - let alpha = *alpha; - let dma_negotiation = *dma_negotiation; - - let (damage_tracker, cursor_damage_tracker) = - if let CastState::Ready { - damage_tracker, - cursor_damage_tracker, - .. - } = &mut *state - { - (damage_tracker.take(), cursor_damage_tracker.take()) - } else { - (None, None) - }; - - debug!("moving to ready state"); - - *state = CastState::Ready { - size, - alpha, - dma_negotiation, - damage_tracker, - cursor_damage_tracker, - last_cursor_location: None, + let ChoiceEnum::Enum { alternatives, .. } = modifiers.1 else { + warn!("wrong modifier choice type"); + stop_cast(); + return; }; - dma_negotiation.plane_count - } - _ => { - // We're negotiating a single modifier, or alpha or modifier changed, - // so we need to do a test allocation. let (modifier, plane_count) = match find_preferred_modifier( &gbm, format_size, fourcc, - vec![format.modifier() as i64], + alternatives, ) { Ok(x) => x, Err(err) => { - warn!("test allocation failed: {err:?}"); + warn!("couldn't find preferred modifier: {err:?}"); stop_cast(); return; } @@ -600,93 +503,206 @@ impl PipeWire { debug!( "allocation successful \ (modifier={modifier:?}, plane_count={plane_count}), \ - moving to ready" + moving to confirmation pending" ); - *state = CastState::Ready { + *state = CastState::ConfirmationPending { size: format_size, alpha: format_has_alpha, dma_negotiation: DmaNegotiation { modifier, plane_count: plane_count as i32, }, - damage_tracker: None, - cursor_damage_tracker: None, - last_cursor_location: None, }; - plane_count as i32 - } - }; + let fixated_format = FormatSet::from_iter([Format { + code: fourcc, + modifier, + }]); + + let mut b1 = Vec::new(); + let mut b2 = Vec::new(); + + let o1 = make_video_params( + &fixated_format, + format_size, + inner.refresh, + format_has_alpha, + ); + let pod1 = make_pod(&mut b1, o1); + + let o2 = make_video_params( + &formats, + format_size, + inner.refresh, + format_has_alpha, + ); + let mut params = [pod1, make_pod(&mut b2, o2)]; - // const BPP: u32 = 4; - // let stride = format.size().width * BPP; - // let size = stride * format.size().height; - - let o1 = pod::object!( - SpaTypes::ObjectParamBuffers, - ParamType::Buffers, - Property::new( - SPA_PARAM_BUFFERS_buffers, - pod::Value::Choice(ChoiceValue::Int(Choice( - ChoiceFlags::empty(), - ChoiceEnum::Range { - default: 8, - min: 2, - max: 16 + if let Err(err) = stream.update_params(&mut params) { + warn!("error updating stream params: {err:?}"); + stop_cast(); + } + } + Some(_) => { + // Verify that alpha and modifier didn't change. + let plane_count = match &*state { + CastState::ConfirmationPending { + size, + alpha, + dma_negotiation, } - ))), - ), - Property::new(SPA_PARAM_BUFFERS_blocks, pod::Value::Int(plane_count)), - Property::new( - SPA_PARAM_BUFFERS_dataType, - pod::Value::Choice(ChoiceValue::Int(Choice( - ChoiceFlags::empty(), - ChoiceEnum::Flags { - default: 1 << DataType::DmaBuf.as_raw(), - flags: vec![1 << DataType::DmaBuf.as_raw()], - }, - ))), - ), - ); + | CastState::Ready { + size, + alpha, + dma_negotiation, + .. + } if *alpha == format_has_alpha + && dma_negotiation.modifier + == Modifier::from(format.modifier()) => + { + let size = *size; + let alpha = *alpha; + let dma_negotiation = *dma_negotiation; + + let (damage_tracker, cursor_damage_tracker) = + if let CastState::Ready { + damage_tracker, + cursor_damage_tracker, + .. + } = &mut *state + { + (damage_tracker.take(), cursor_damage_tracker.take()) + } else { + (None, None) + }; + + debug!("moving to ready state"); + + *state = CastState::Ready { + size, + alpha, + dma_negotiation, + damage_tracker, + cursor_damage_tracker, + last_cursor_location: None, + }; + + dma_negotiation.plane_count + } + _ => { + // We're negotiating a single modifier, or alpha or modifier changed, + // so we need to do a test allocation. + let (modifier, plane_count) = match find_preferred_modifier( + &gbm, + format_size, + fourcc, + vec![format.modifier() as i64], + ) { + Ok(x) => x, + Err(err) => { + warn!("test allocation failed: {err:?}"); + stop_cast(); + return; + } + }; + + debug!( + "allocation successful \ + (modifier={modifier:?}, plane_count={plane_count}), \ + moving to ready" + ); + + *state = CastState::Ready { + size: format_size, + alpha: format_has_alpha, + dma_negotiation: DmaNegotiation { + modifier, + plane_count: plane_count as i32, + }, + damage_tracker: None, + cursor_damage_tracker: None, + last_cursor_location: None, + }; + + plane_count as i32 + } + }; - let o2 = pod::object!( - SpaTypes::ObjectParamMeta, - ParamType::Meta, - Property::new( - SPA_PARAM_META_type, - pod::Value::Id(spa::utils::Id(SPA_META_Header)) - ), - Property::new( - SPA_PARAM_META_size, - pod::Value::Int(size_of::() as i32) - ), - ); - let mut b1 = vec![]; - let mut b2 = vec![]; - let mut params = vec![make_pod(&mut b1, o1), make_pod(&mut b2, o2)]; - - let mut b_cursor = vec![]; - if cursor_mode == CursorMode::Metadata { - let o_cursor = pod::object!( - SpaTypes::ObjectParamMeta, - ParamType::Meta, - Property::new( - SPA_PARAM_META_type, - pod::Value::Id(spa::utils::Id(SPA_META_Cursor)) - ), - Property::new( - SPA_PARAM_META_size, - pod::Value::Int(CURSOR_META_SIZE as i32) - ), - ); - params.push(make_pod(&mut b_cursor, o_cursor)); - } + // const BPP: u32 = 4; + // let stride = format.size().width * BPP; + // let size = stride * format.size().height; + + let o1 = pod::object!( + SpaTypes::ObjectParamBuffers, + ParamType::Buffers, + Property::new( + SPA_PARAM_BUFFERS_buffers, + pod::Value::Choice(ChoiceValue::Int(Choice( + ChoiceFlags::empty(), + ChoiceEnum::Range { + default: 8, + min: 2, + max: 16 + } + ))), + ), + Property::new(SPA_PARAM_BUFFERS_blocks, pod::Value::Int(plane_count)), + Property::new( + SPA_PARAM_BUFFERS_dataType, + pod::Value::Choice(ChoiceValue::Int(Choice( + ChoiceFlags::empty(), + ChoiceEnum::Flags { + default: 1 << DataType::DmaBuf.as_raw(), + flags: vec![1 << DataType::DmaBuf.as_raw()], + }, + ))), + ), + ); - if let Err(err) = stream.update_params(&mut params) { - warn!("error updating stream params: {err:?}"); - stop_cast(); - } + let o2 = pod::object!( + SpaTypes::ObjectParamMeta, + ParamType::Meta, + Property::new( + SPA_PARAM_META_type, + pod::Value::Id(spa::utils::Id(SPA_META_Header)) + ), + Property::new( + SPA_PARAM_META_size, + pod::Value::Int(size_of::() as i32) + ), + ); + let mut b1 = vec![]; + let mut b2 = vec![]; + let mut params = vec![make_pod(&mut b1, o1), make_pod(&mut b2, o2)]; + + let mut b_cursor = vec![]; + if cursor_mode == CursorMode::Metadata { + let o_cursor = pod::object!( + SpaTypes::ObjectParamMeta, + ParamType::Meta, + Property::new( + SPA_PARAM_META_type, + pod::Value::Id(spa::utils::Id(SPA_META_Cursor)) + ), + Property::new( + SPA_PARAM_META_size, + pod::Value::Int(CURSOR_META_SIZE as i32) + ), + ); + params.push(make_pod(&mut b_cursor, o_cursor)); + } + + if let Err(err) = stream.update_params(&mut params) { + warn!("error updating stream params: {err:?}"); + stop_cast(); + } + } + None => { + warn!("pw stream: modifier prop missing and we don't support shared memory sharing currently"); + stop_cast(); + } + }; } }) .add_buffer({