pw_utils: support sending SHM sharing as fallback when first negotiating
Browse files at this commitbe6ec0a36696769bd9c0de9af4e1a291922516b6Parent: 4dc27501 changed file +132 -124
- src/screencasting/pw_utils.rs +132 -124
src/screencasting/pw_utils.rsmodified+132 -124
| @@ -35,7 +35,7 @@ use pipewire::sys::{pw_buffer, pw_check_library_version, pw_stream_queue_buffer} | |||
| 35 | use smithay::backend::allocator::dmabuf::{AsDmabuf, Dmabuf}; | 35 | use smithay::backend::allocator::dmabuf::{AsDmabuf, Dmabuf}; |
| 36 | use smithay::backend::allocator::format::FormatSet; | 36 | use smithay::backend::allocator::format::FormatSet; |
| 37 | use smithay::backend::allocator::gbm::{GbmBuffer, GbmBufferFlags, GbmDevice}; | 37 | use smithay::backend::allocator::gbm::{GbmBuffer, GbmBufferFlags, GbmDevice}; |
| 38 | -use smithay::backend::allocator::{Format, Fourcc}; | 38 | +use smithay::backend::allocator::Fourcc; |
| 39 | use smithay::backend::drm::DrmDeviceFd; | 39 | use smithay::backend::drm::DrmDeviceFd; |
| 40 | use smithay::backend::renderer::damage::OutputDamageTracker; | 40 | use smithay::backend::renderer::damage::OutputDamageTracker; |
| 41 | use smithay::backend::renderer::element::utils::{Relocate, RelocateRenderElement}; | 41 | use smithay::backend::renderer::element::utils::{Relocate, RelocateRenderElement}; |
| @@ -211,24 +211,131 @@ impl<'a, E: Element> CursorData<'a, E> { | |||
| 211 | } | 211 | } |
| 212 | } | 212 | } |
| 213 | 213 | ||
| 214 | -macro_rules! make_params { | 214 | +fn make_video_params( |
| 215 | - ($params:ident, $formats:expr, $size:expr, $refresh:expr, $alpha:expr) => { | 215 | + format: VideoFormat, |
| 216 | - let mut b1 = Vec::new(); | 216 | + modifiers: &[Modifier], |
| 217 | - let mut b2 = Vec::new(); | 217 | + size: Size<u32, Physical>, |
| 218 | - | 218 | + refresh: u32, |
| 219 | - let o1 = make_video_params($formats, $size, $refresh, false); | 219 | +) -> pod::Object { |
| 220 | - let pod1 = make_pod(&mut b1, o1); | 220 | + let mut properties = vec![ |
| 221 | - | 221 | + pod::property!(FormatProperties::MediaType, Id, MediaType::Video), |
| 222 | - let mut p1; | 222 | + pod::property!(FormatProperties::MediaSubtype, Id, MediaSubtype::Raw), |
| 223 | - let mut p2; | 223 | + pod::property!( |
| 224 | - $params = if $alpha { | 224 | + FormatProperties::VideoSize, |
| 225 | - let o2 = make_video_params($formats, $size, $refresh, true); | 225 | + Rectangle, |
| 226 | - p2 = [pod1, make_pod(&mut b2, o2)]; | 226 | + Rectangle { |
| 227 | - &mut p2[..] | 227 | + width: size.w, |
| 228 | + height: size.h, | ||
| 229 | + } | ||
| 230 | + ), | ||
| 231 | + pod::property!( | ||
| 232 | + FormatProperties::VideoFramerate, | ||
| 233 | + Fraction, | ||
| 234 | + Fraction { num: 0, denom: 1 } | ||
| 235 | + ), | ||
| 236 | + pod::property!( | ||
| 237 | + FormatProperties::VideoMaxFramerate, | ||
| 238 | + Choice, | ||
| 239 | + Range, | ||
| 240 | + Fraction, | ||
| 241 | + Fraction { | ||
| 242 | + num: refresh, | ||
| 243 | + denom: 1000 | ||
| 244 | + }, | ||
| 245 | + Fraction { num: 1, denom: 1 }, | ||
| 246 | + Fraction { | ||
| 247 | + num: refresh, | ||
| 248 | + denom: 1000 | ||
| 249 | + } | ||
| 250 | + ), | ||
| 251 | + pod::property!(FormatProperties::VideoFormat, Id, format), | ||
| 252 | + ]; | ||
| 253 | + | ||
| 254 | + if !modifiers.is_empty() { | ||
| 255 | + let dont_fixate = if modifiers.len() > 1 { | ||
| 256 | + PropertyFlags::DONT_FIXATE | ||
| 228 | } else { | 257 | } else { |
| 229 | - p1 = [pod1]; | 258 | + PropertyFlags::empty() |
| 230 | - &mut p1[..] | ||
| 231 | }; | 259 | }; |
| 260 | + let flags = PropertyFlags::MANDATORY | dont_fixate; | ||
| 261 | + let modifiers_i64 = modifiers | ||
| 262 | + .iter() | ||
| 263 | + .map(|m| u64::from(*m) as i64) | ||
| 264 | + .collect::<Vec<_>>(); | ||
| 265 | + | ||
| 266 | + let prop = Property { | ||
| 267 | + key: FormatProperties::VideoModifier.as_raw(), | ||
| 268 | + flags, | ||
| 269 | + value: pod::Value::Choice(ChoiceValue::Long(Choice( | ||
| 270 | + ChoiceFlags::empty(), | ||
| 271 | + ChoiceEnum::Enum { | ||
| 272 | + default: modifiers_i64[0], | ||
| 273 | + alternatives: modifiers_i64, | ||
| 274 | + }, | ||
| 275 | + ))), | ||
| 276 | + }; | ||
| 277 | + | ||
| 278 | + properties.push(prop); | ||
| 279 | + } | ||
| 280 | + | ||
| 281 | + pod::Object { | ||
| 282 | + type_: SpaTypes::ObjectParamFormat.as_raw(), | ||
| 283 | + id: ParamType::EnumFormat.as_raw(), | ||
| 284 | + properties, | ||
| 285 | + } | ||
| 286 | +} | ||
| 287 | + | ||
| 288 | +fn make_initial_video_params( | ||
| 289 | + possible_modifiers: &FormatSet, | ||
| 290 | + size: Size<u32, Physical>, | ||
| 291 | + refresh: u32, | ||
| 292 | + alpha: bool, | ||
| 293 | +) -> Vec<pod::Object> { | ||
| 294 | + let mut rv = Vec::new(); | ||
| 295 | + | ||
| 296 | + let mut push_alpha = |alpha| { | ||
| 297 | + let format = if alpha { | ||
| 298 | + VideoFormat::BGRA | ||
| 299 | + } else { | ||
| 300 | + VideoFormat::BGRx | ||
| 301 | + }; | ||
| 302 | + | ||
| 303 | + let fourcc = if alpha { | ||
| 304 | + Fourcc::Argb8888 | ||
| 305 | + } else { | ||
| 306 | + Fourcc::Xrgb8888 | ||
| 307 | + }; | ||
| 308 | + | ||
| 309 | + let modifiers: Vec<_> = possible_modifiers | ||
| 310 | + .iter() | ||
| 311 | + .filter_map(|f| (f.code == fourcc).then_some(f.modifier)) | ||
| 312 | + .collect(); | ||
| 313 | + | ||
| 314 | + trace!("offering: {modifiers:?}"); | ||
| 315 | + | ||
| 316 | + if !modifiers.is_empty() { | ||
| 317 | + rv.push(make_video_params(format, &modifiers, size, refresh)); | ||
| 318 | + } | ||
| 319 | + rv.push(make_video_params(format, &[], size, refresh)); | ||
| 320 | + }; | ||
| 321 | + | ||
| 322 | + if alpha { | ||
| 323 | + push_alpha(true); | ||
| 324 | + } | ||
| 325 | + push_alpha(false); | ||
| 326 | + | ||
| 327 | + rv | ||
| 328 | +} | ||
| 329 | + | ||
| 330 | +macro_rules! make_params { | ||
| 331 | + ($params:ident, $formats:expr, $size:expr, $refresh:expr, $alpha:expr) => { | ||
| 332 | + let $params = make_initial_video_params($formats, $size, $refresh, $alpha); | ||
| 333 | + let mut bufs = [const { Vec::new() }; 4]; // Maximum possible params len. | ||
| 334 | + let mut $params: Vec<_> = $params | ||
| 335 | + .into_iter() | ||
| 336 | + .zip(&mut bufs) | ||
| 337 | + .map(|(obj, buf)| make_pod(buf, obj)) | ||
| 338 | + .collect(); | ||
| 232 | }; | 339 | }; |
| 233 | } | 340 | } |
| 234 | 341 | ||
| @@ -517,29 +624,12 @@ impl PipeWire { | |||
| 517 | }), | 624 | }), |
| 518 | }; | 625 | }; |
| 519 | 626 | ||
| 520 | - let fixated_format = FormatSet::from_iter([Format { | 627 | + let o = make_video_params(format.format(), &[modifier], format_size, inner.refresh); |
| 521 | - code: fourcc, | 628 | + let mut b = Vec::new(); |
| 522 | - modifier, | 629 | + let pod = make_pod(&mut b, o); |
| 523 | - }]); | ||
| 524 | - | ||
| 525 | - let mut b1 = Vec::new(); | ||
| 526 | - let mut b2 = Vec::new(); | ||
| 527 | 630 | ||
| 528 | - let o1 = make_video_params( | 631 | + make_params!(params, &formats, format_size, inner.refresh, format_has_alpha); |
| 529 | - &fixated_format, | 632 | + params.insert(0, pod); |
| 530 | - format_size, | ||
| 531 | - inner.refresh, | ||
| 532 | - format_has_alpha, | ||
| 533 | - ); | ||
| 534 | - let pod1 = make_pod(&mut b1, o1); | ||
| 535 | - | ||
| 536 | - let o2 = make_video_params( | ||
| 537 | - &formats, | ||
| 538 | - format_size, | ||
| 539 | - inner.refresh, | ||
| 540 | - format_has_alpha, | ||
| 541 | - ); | ||
| 542 | - let mut params = [pod1, make_pod(&mut b2, o2)]; | ||
| 543 | 633 | ||
| 544 | if let Err(err) = stream.update_params(&mut params) { | 634 | if let Err(err) = stream.update_params(&mut params) { |
| 545 | warn!("error updating stream params: {err:?}"); | 635 | warn!("error updating stream params: {err:?}"); |
| @@ -821,14 +911,13 @@ impl PipeWire { | |||
| 821 | "starting pw stream with size={pending_size:?}, refresh={refresh:?}" | 911 | "starting pw stream with size={pending_size:?}, refresh={refresh:?}" |
| 822 | ); | 912 | ); |
| 823 | 913 | ||
| 824 | - let params; | ||
| 825 | make_params!(params, &formats, pending_size, refresh, alpha); | 914 | make_params!(params, &formats, pending_size, refresh, alpha); |
| 826 | stream | 915 | stream |
| 827 | .connect( | 916 | .connect( |
| 828 | Direction::Output, | 917 | Direction::Output, |
| 829 | None, | 918 | None, |
| 830 | StreamFlags::DRIVER | StreamFlags::ALLOC_BUFFERS, | 919 | StreamFlags::DRIVER | StreamFlags::ALLOC_BUFFERS, |
| 831 | - params, | 920 | + &mut params, |
| 832 | ) | 921 | ) |
| 833 | .context("error connecting stream")?; | 922 | .context("error connecting stream")?; |
| 834 | 923 | ||
| @@ -883,7 +972,6 @@ impl Cast { | |||
| 883 | pending_size: new_size, | 972 | pending_size: new_size, |
| 884 | }; | 973 | }; |
| 885 | 974 | ||
| 886 | - let params; | ||
| 887 | make_params!( | 975 | make_params!( |
| 888 | params, | 976 | params, |
| 889 | &self.formats, | 977 | &self.formats, |
| @@ -892,7 +980,7 @@ impl Cast { | |||
| 892 | self.offer_alpha | 980 | self.offer_alpha |
| 893 | ); | 981 | ); |
| 894 | self.stream | 982 | self.stream |
| 895 | - .update_params(params) | 983 | + .update_params(&mut params) |
| 896 | .context("error updating stream params")?; | 984 | .context("error updating stream params")?; |
| 897 | 985 | ||
| 898 | Ok(CastSizeChange::Pending) | 986 | Ok(CastSizeChange::Pending) |
| @@ -910,10 +998,9 @@ impl Cast { | |||
| 910 | inner.refresh = refresh; | 998 | inner.refresh = refresh; |
| 911 | 999 | ||
| 912 | let size = inner.state.expected_format_size(); | 1000 | let size = inner.state.expected_format_size(); |
| 913 | - let params; | ||
| 914 | make_params!(params, &self.formats, size, refresh, self.offer_alpha); | 1001 | make_params!(params, &self.formats, size, refresh, self.offer_alpha); |
| 915 | self.stream | 1002 | self.stream |
| 916 | - .update_params(params) | 1003 | + .update_params(&mut params) |
| 917 | .context("error updating stream params")?; | 1004 | .context("error updating stream params")?; |
| 918 | 1005 | ||
| 919 | Ok(()) | 1006 | Ok(()) |
| @@ -1273,85 +1360,6 @@ fn pw_version_supports_cursor_metadata() -> bool { | |||
| 1273 | unsafe { pw_check_library_version(1, 4, 8) } | 1360 | unsafe { pw_check_library_version(1, 4, 8) } |
| 1274 | } | 1361 | } |
| 1275 | 1362 | ||
| 1276 | -fn make_video_params( | ||
| 1277 | - formats: &FormatSet, | ||
| 1278 | - size: Size<u32, Physical>, | ||
| 1279 | - refresh: u32, | ||
| 1280 | - alpha: bool, | ||
| 1281 | -) -> pod::Object { | ||
| 1282 | - let format = if alpha { | ||
| 1283 | - VideoFormat::BGRA | ||
| 1284 | - } else { | ||
| 1285 | - VideoFormat::BGRx | ||
| 1286 | - }; | ||
| 1287 | - | ||
| 1288 | - let fourcc = if alpha { | ||
| 1289 | - Fourcc::Argb8888 | ||
| 1290 | - } else { | ||
| 1291 | - Fourcc::Xrgb8888 | ||
| 1292 | - }; | ||
| 1293 | - | ||
| 1294 | - let formats: Vec<_> = formats | ||
| 1295 | - .iter() | ||
| 1296 | - .filter_map(|f| (f.code == fourcc).then_some(u64::from(f.modifier) as i64)) | ||
| 1297 | - .collect(); | ||
| 1298 | - | ||
| 1299 | - trace!("offering: {formats:?}"); | ||
| 1300 | - | ||
| 1301 | - let dont_fixate = if formats.len() > 1 { | ||
| 1302 | - PropertyFlags::DONT_FIXATE | ||
| 1303 | - } else { | ||
| 1304 | - PropertyFlags::empty() | ||
| 1305 | - }; | ||
| 1306 | - | ||
| 1307 | - pod::object!( | ||
| 1308 | - SpaTypes::ObjectParamFormat, | ||
| 1309 | - ParamType::EnumFormat, | ||
| 1310 | - pod::property!(FormatProperties::MediaType, Id, MediaType::Video), | ||
| 1311 | - pod::property!(FormatProperties::MediaSubtype, Id, MediaSubtype::Raw), | ||
| 1312 | - pod::property!(FormatProperties::VideoFormat, Id, format), | ||
| 1313 | - Property { | ||
| 1314 | - key: FormatProperties::VideoModifier.as_raw(), | ||
| 1315 | - flags: PropertyFlags::MANDATORY | dont_fixate, | ||
| 1316 | - value: pod::Value::Choice(ChoiceValue::Long(Choice( | ||
| 1317 | - ChoiceFlags::empty(), | ||
| 1318 | - ChoiceEnum::Enum { | ||
| 1319 | - default: formats[0], | ||
| 1320 | - alternatives: formats, | ||
| 1321 | - } | ||
| 1322 | - ))) | ||
| 1323 | - }, | ||
| 1324 | - pod::property!( | ||
| 1325 | - FormatProperties::VideoSize, | ||
| 1326 | - Rectangle, | ||
| 1327 | - Rectangle { | ||
| 1328 | - width: size.w, | ||
| 1329 | - height: size.h, | ||
| 1330 | - } | ||
| 1331 | - ), | ||
| 1332 | - pod::property!( | ||
| 1333 | - FormatProperties::VideoFramerate, | ||
| 1334 | - Fraction, | ||
| 1335 | - Fraction { num: 0, denom: 1 } | ||
| 1336 | - ), | ||
| 1337 | - pod::property!( | ||
| 1338 | - FormatProperties::VideoMaxFramerate, | ||
| 1339 | - Choice, | ||
| 1340 | - Range, | ||
| 1341 | - Fraction, | ||
| 1342 | - Fraction { | ||
| 1343 | - num: refresh, | ||
| 1344 | - denom: 1000 | ||
| 1345 | - }, | ||
| 1346 | - Fraction { num: 1, denom: 1 }, | ||
| 1347 | - Fraction { | ||
| 1348 | - num: refresh, | ||
| 1349 | - denom: 1000 | ||
| 1350 | - } | ||
| 1351 | - ), | ||
| 1352 | - ) | ||
| 1353 | -} | ||
| 1354 | - | ||
| 1355 | fn make_pod(buffer: &mut Vec<u8>, object: pod::Object) -> &Pod { | 1363 | fn make_pod(buffer: &mut Vec<u8>, object: pod::Object) -> &Pod { |
| 1356 | PodSerializer::serialize(Cursor::new(&mut *buffer), &pod::Value::Object(object)).unwrap(); | 1364 | PodSerializer::serialize(Cursor::new(&mut *buffer), &pod::Value::Object(object)).unwrap(); |
| 1357 | Pod::from_bytes(buffer).unwrap() | 1365 | Pod::from_bytes(buffer).unwrap() |