pw_utils: Extract on_{add,remove}_buffer() allowing rustfmt to run
Browse files at this commit94b7dcddac09737cf92ce6b2135f066fb7874268Parent: f6f82901 changed file +534 -496
- src/screencasting/pw_utils.rs +534 -496
src/screencasting/pw_utils.rsmodified+534 -496
| @@ -456,550 +456,458 @@ impl PipeWire { | |||
| 456 | rendering_buffers: Vec::new(), | 456 | rendering_buffers: Vec::new(), |
| 457 | })); | 457 | })); |
| 458 | 458 | ||
| 459 | - let listener = stream | 459 | + let listener = |
| 460 | - .add_local_listener_with_user_data(()) | 460 | + stream |
| 461 | - .state_changed({ | 461 | + .add_local_listener_with_user_data(()) |
| 462 | - let inner = inner.clone(); | 462 | + .state_changed({ |
| 463 | - let stop_cast = stop_cast.clone(); | 463 | + let inner = inner.clone(); |
| 464 | - move |stream, (), old, new| { | 464 | + let stop_cast = stop_cast.clone(); |
| 465 | - let _span = debug_span!("state_changed", %stream_id).entered(); | 465 | + move |stream, (), old, new| { |
| 466 | - debug!("{old:?} -> {new:?}"); | 466 | + let _span = debug_span!("state_changed", %stream_id).entered(); |
| 467 | - let mut inner = inner.borrow_mut(); | 467 | + debug!("{old:?} -> {new:?}"); |
| 468 | - | 468 | + let mut inner = inner.borrow_mut(); |
| 469 | - match new { | 469 | + |
| 470 | - StreamState::Paused => { | 470 | + match new { |
| 471 | - if inner.node_id.is_none() { | 471 | + StreamState::Paused => { |
| 472 | - let id = stream.node_id(); | 472 | + if inner.node_id.is_none() { |
| 473 | - inner.node_id = Some(id); | 473 | + let id = stream.node_id(); |
| 474 | - debug!("sending signal with {id}"); | 474 | + inner.node_id = Some(id); |
| 475 | - | 475 | + debug!("sending signal with {id}"); |
| 476 | - let _span = tracy_client::span!("sending PipeWireStreamAdded"); | 476 | + |
| 477 | - async_io::block_on(async { | 477 | + let _span = tracy_client::span!("sending PipeWireStreamAdded"); |
| 478 | - let res = mutter_screen_cast::Stream::pipe_wire_stream_added( | 478 | + async_io::block_on(async { |
| 479 | - &signal_ctx, | 479 | + let res = |
| 480 | - id, | 480 | + mutter_screen_cast::Stream::pipe_wire_stream_added( |
| 481 | - ) | 481 | + &signal_ctx, |
| 482 | - .await; | 482 | + id, |
| 483 | - | 483 | + ) |
| 484 | - if let Err(err) = res { | 484 | + .await; |
| 485 | - warn!("error sending PipeWireStreamAdded: {err:?}"); | 485 | + |
| 486 | - stop_cast(); | 486 | + if let Err(err) = res { |
| 487 | - } | 487 | + warn!("error sending PipeWireStreamAdded: {err:?}"); |
| 488 | - }); | 488 | + stop_cast(); |
| 489 | - } | 489 | + } |
| 490 | + }); | ||
| 491 | + } | ||
| 490 | 492 | ||
| 491 | - inner.is_active = false; | ||
| 492 | - } | ||
| 493 | - StreamState::Error(_) => { | ||
| 494 | - if inner.is_active { | ||
| 495 | inner.is_active = false; | 493 | inner.is_active = false; |
| 496 | - stop_cast(); | 494 | + } |
| 495 | + StreamState::Error(_) => { | ||
| 496 | + if inner.is_active { | ||
| 497 | + inner.is_active = false; | ||
| 498 | + stop_cast(); | ||
| 499 | + } | ||
| 500 | + } | ||
| 501 | + StreamState::Unconnected => (), | ||
| 502 | + StreamState::Connecting => (), | ||
| 503 | + StreamState::Streaming => { | ||
| 504 | + inner.is_active = true; | ||
| 505 | + redraw(); | ||
| 497 | } | 506 | } |
| 498 | } | 507 | } |
| 499 | - StreamState::Unconnected => (), | ||
| 500 | - StreamState::Connecting => (), | ||
| 501 | - StreamState::Streaming => { | ||
| 502 | - inner.is_active = true; | ||
| 503 | - redraw(); | ||
| 504 | - } | ||
| 505 | - } | ||
| 506 | - } | ||
| 507 | - }) | ||
| 508 | - .param_changed({ | ||
| 509 | - let inner = inner.clone(); | ||
| 510 | - let stop_cast = stop_cast.clone(); | ||
| 511 | - let gbm = gbm.clone(); | ||
| 512 | - let formats = formats.clone(); | ||
| 513 | - move |stream, (), id, pod| { | ||
| 514 | - let id = ParamType::from_raw(id); | ||
| 515 | - trace!(%stream_id, ?id, "param_changed"); | ||
| 516 | - let mut inner = inner.borrow_mut(); | ||
| 517 | - let inner = &mut *inner; | ||
| 518 | - | ||
| 519 | - if id != ParamType::Format { | ||
| 520 | - return; | ||
| 521 | } | 508 | } |
| 522 | - | 509 | + }) |
| 523 | - let _span = debug_span!("param_changed", %stream_id).entered(); | 510 | + .param_changed({ |
| 524 | - | 511 | + let inner = inner.clone(); |
| 525 | - let Some(pod) = pod else { return }; | 512 | + let stop_cast = stop_cast.clone(); |
| 526 | - | 513 | + let gbm = gbm.clone(); |
| 527 | - let (m_type, m_subtype) = match parse_format(pod) { | 514 | + let formats = formats.clone(); |
| 528 | - Ok(x) => x, | 515 | + move |stream, (), id, pod| { |
| 529 | - Err(err) => { | 516 | + let id = ParamType::from_raw(id); |
| 530 | - warn!("error parsing format: {err:?}"); | 517 | + trace!(%stream_id, ?id, "param_changed"); |
| 518 | + let mut inner = inner.borrow_mut(); | ||
| 519 | + let inner = &mut *inner; | ||
| 520 | + | ||
| 521 | + if id != ParamType::Format { | ||
| 531 | return; | 522 | return; |
| 532 | } | 523 | } |
| 533 | - }; | ||
| 534 | 524 | ||
| 535 | - if m_type != MediaType::Video || m_subtype != MediaSubtype::Raw { | 525 | + let _span = debug_span!("param_changed", %stream_id).entered(); |
| 536 | - return; | ||
| 537 | - } | ||
| 538 | 526 | ||
| 539 | - let mut format = VideoInfoRaw::new(); | 527 | + let Some(pod) = pod else { return }; |
| 540 | - format.parse(pod).unwrap(); | ||
| 541 | - debug!("got format = {format:?}"); | ||
| 542 | 528 | ||
| 543 | - let format_size = Size::from((format.size().width, format.size().height)); | 529 | + let (m_type, m_subtype) = match parse_format(pod) { |
| 530 | + Ok(x) => x, | ||
| 531 | + Err(err) => { | ||
| 532 | + warn!("error parsing format: {err:?}"); | ||
| 533 | + return; | ||
| 534 | + } | ||
| 535 | + }; | ||
| 544 | 536 | ||
| 545 | - let state = &mut inner.state; | 537 | + if m_type != MediaType::Video || m_subtype != MediaSubtype::Raw { |
| 546 | - if format_size != state.expected_format_size() { | ||
| 547 | - if !matches!(&*state, CastState::ResizePending { .. }) { | ||
| 548 | - warn!("wrong size, but we're not resizing"); | ||
| 549 | - stop_cast(); | ||
| 550 | return; | 538 | return; |
| 551 | } | 539 | } |
| 552 | 540 | ||
| 553 | - debug!("wrong size, waiting"); | 541 | + let mut format = VideoInfoRaw::new(); |
| 554 | - return; | 542 | + format.parse(pod).unwrap(); |
| 555 | - } | 543 | + debug!("got format = {format:?}"); |
| 556 | 544 | ||
| 557 | - let format_has_alpha = format.format() == VideoFormat::BGRA; | 545 | + let format_size = Size::from((format.size().width, format.size().height)); |
| 558 | - let fourcc = if format_has_alpha { | ||
| 559 | - Fourcc::Argb8888 | ||
| 560 | - } else { | ||
| 561 | - Fourcc::Xrgb8888 | ||
| 562 | - }; | ||
| 563 | 546 | ||
| 564 | - let max_frame_rate = format.max_framerate(); | 547 | + let state = &mut inner.state; |
| 565 | - let min_frame_time = Duration::from_micros( | 548 | + if format_size != state.expected_format_size() { |
| 566 | - 1_000_000 * u64::from(max_frame_rate.denom) / u64::from(max_frame_rate.num), | 549 | + if !matches!(&*state, CastState::ResizePending { .. }) { |
| 567 | - ); | 550 | + warn!("wrong size, but we're not resizing"); |
| 568 | - inner.min_time_between_frames = min_frame_time; | ||
| 569 | - | ||
| 570 | - // We have following cases when param_changed: | ||
| 571 | - // | ||
| 572 | - // 1. Modifier exists and its flags contain DONT_FIXATE | ||
| 573 | - // | ||
| 574 | - // Do test allocation, set CastState to ConfirmationPending and send param | ||
| 575 | - // again. | ||
| 576 | - // | ||
| 577 | - // 2. Modifier exists and it doesn't need fixation | ||
| 578 | - // | ||
| 579 | - // Do test allocation to ensure the modifier work, then set CastState to | ||
| 580 | - // Ready. Then set buffer to DMA. | ||
| 581 | - // | ||
| 582 | - // 3. Modifier doesn't exist | ||
| 583 | - // | ||
| 584 | - // Set CastState to Ready and set buffer to SHM. | ||
| 585 | - | ||
| 586 | - let object = pod.as_object().unwrap(); | ||
| 587 | - let maybe_prop_modifier = object.find_prop(spa::utils::Id(FormatProperties::VideoModifier.0)); | ||
| 588 | - | ||
| 589 | - match maybe_prop_modifier { | ||
| 590 | - Some(prop_modifier) if prop_modifier.flags().contains(PodPropFlags::DONT_FIXATE) => { | ||
| 591 | - debug!("fixating the modifier"); | ||
| 592 | - | ||
| 593 | - let pod_modifier = prop_modifier.value(); | ||
| 594 | - let Ok((_, modifiers)) = PodDeserializer::deserialize_from::<Choice<i64>>( | ||
| 595 | - pod_modifier.as_bytes(), | ||
| 596 | - ) else { | ||
| 597 | - warn!("wrong modifier property type"); | ||
| 598 | stop_cast(); | 551 | stop_cast(); |
| 599 | return; | 552 | return; |
| 600 | - }; | 553 | + } |
| 601 | 554 | ||
| 602 | - let ChoiceEnum::Enum { alternatives, .. } = modifiers.1 else { | 555 | + debug!("wrong size, waiting"); |
| 603 | - warn!("wrong modifier choice type"); | 556 | + return; |
| 604 | - stop_cast(); | 557 | + } |
| 605 | - return; | 558 | + |
| 606 | - }; | 559 | + let format_has_alpha = format.format() == VideoFormat::BGRA; |
| 607 | - | 560 | + let fourcc = if format_has_alpha { |
| 608 | - let (modifier, plane_count) = match find_preferred_modifier( | 561 | + Fourcc::Argb8888 |
| 609 | - &gbm, | 562 | + } else { |
| 610 | - format_size, | 563 | + Fourcc::Xrgb8888 |
| 611 | - fourcc, | 564 | + }; |
| 612 | - alternatives, | 565 | + |
| 613 | - ) { | 566 | + let max_frame_rate = format.max_framerate(); |
| 614 | - Ok(x) => x, | 567 | + let min_frame_time = Duration::from_micros( |
| 615 | - Err(err) => { | 568 | + 1_000_000 * u64::from(max_frame_rate.denom) |
| 616 | - warn!("couldn't find preferred modifier: {err:?}"); | 569 | + / u64::from(max_frame_rate.num), |
| 570 | + ); | ||
| 571 | + inner.min_time_between_frames = min_frame_time; | ||
| 572 | + | ||
| 573 | + // We have following cases when param_changed: | ||
| 574 | + // | ||
| 575 | + // 1. Modifier exists and its flags contain DONT_FIXATE | ||
| 576 | + // | ||
| 577 | + // Do test allocation, set CastState to ConfirmationPending and send | ||
| 578 | + // param again. | ||
| 579 | + // | ||
| 580 | + // 2. Modifier exists and it doesn't need fixation | ||
| 581 | + // | ||
| 582 | + // Do test allocation to ensure the modifier work, then set CastState to | ||
| 583 | + // Ready. Then set buffer to DMA. | ||
| 584 | + // | ||
| 585 | + // 3. Modifier doesn't exist | ||
| 586 | + // | ||
| 587 | + // Set CastState to Ready and set buffer to SHM. | ||
| 588 | + | ||
| 589 | + let object = pod.as_object().unwrap(); | ||
| 590 | + let maybe_prop_modifier = | ||
| 591 | + object.find_prop(spa::utils::Id(FormatProperties::VideoModifier.0)); | ||
| 592 | + | ||
| 593 | + match maybe_prop_modifier { | ||
| 594 | + Some(prop_modifier) | ||
| 595 | + if prop_modifier.flags().contains(PodPropFlags::DONT_FIXATE) => | ||
| 596 | + { | ||
| 597 | + debug!("fixating the modifier"); | ||
| 598 | + | ||
| 599 | + let pod_modifier = prop_modifier.value(); | ||
| 600 | + let Ok((_, modifiers)) = | ||
| 601 | + PodDeserializer::deserialize_from::<Choice<i64>>( | ||
| 602 | + pod_modifier.as_bytes(), | ||
| 603 | + ) | ||
| 604 | + else { | ||
| 605 | + warn!("wrong modifier property type"); | ||
| 617 | stop_cast(); | 606 | stop_cast(); |
| 618 | return; | 607 | return; |
| 608 | + }; | ||
| 609 | + | ||
| 610 | + let ChoiceEnum::Enum { alternatives, .. } = modifiers.1 else { | ||
| 611 | + warn!("wrong modifier choice type"); | ||
| 612 | + stop_cast(); | ||
| 613 | + return; | ||
| 614 | + }; | ||
| 615 | + | ||
| 616 | + let (modifier, plane_count) = match find_preferred_modifier( | ||
| 617 | + &gbm, | ||
| 618 | + format_size, | ||
| 619 | + fourcc, | ||
| 620 | + alternatives, | ||
| 621 | + ) { | ||
| 622 | + Ok(x) => x, | ||
| 623 | + Err(err) => { | ||
| 624 | + warn!("couldn't find preferred modifier: {err:?}"); | ||
| 625 | + stop_cast(); | ||
| 626 | + return; | ||
| 627 | + } | ||
| 628 | + }; | ||
| 629 | + | ||
| 630 | + debug!( | ||
| 631 | + "allocation successful \ | ||
| 632 | + (modifier={modifier:?}, plane_count={plane_count}), \ | ||
| 633 | + moving to confirmation pending" | ||
| 634 | + ); | ||
| 635 | + | ||
| 636 | + *state = CastState::ConfirmationPending { | ||
| 637 | + size: format_size, | ||
| 638 | + alpha: format_has_alpha, | ||
| 639 | + dma_negotiation: Some(DmaNegotiation { | ||
| 640 | + modifier, | ||
| 641 | + plane_count: plane_count as i32, | ||
| 642 | + }), | ||
| 643 | + }; | ||
| 644 | + | ||
| 645 | + let o = make_video_params( | ||
| 646 | + format.format(), | ||
| 647 | + &[modifier], | ||
| 648 | + format_size, | ||
| 649 | + inner.refresh, | ||
| 650 | + ); | ||
| 651 | + let mut b = Vec::new(); | ||
| 652 | + let pod = make_pod(&mut b, o); | ||
| 653 | + | ||
| 654 | + make_params!( | ||
| 655 | + params, | ||
| 656 | + &formats, | ||
| 657 | + format_size, | ||
| 658 | + inner.refresh, | ||
| 659 | + format_has_alpha | ||
| 660 | + ); | ||
| 661 | + params.insert(0, pod); | ||
| 662 | + | ||
| 663 | + if let Err(err) = stream.update_params(&mut params) { | ||
| 664 | + warn!("error updating stream params: {err:?}"); | ||
| 665 | + stop_cast(); | ||
| 619 | } | 666 | } |
| 620 | - }; | ||
| 621 | - | ||
| 622 | - debug!( | ||
| 623 | - "allocation successful \ | ||
| 624 | - (modifier={modifier:?}, plane_count={plane_count}), \ | ||
| 625 | - moving to confirmation pending" | ||
| 626 | - ); | ||
| 627 | - | ||
| 628 | - *state = CastState::ConfirmationPending { | ||
| 629 | - size: format_size, | ||
| 630 | - alpha: format_has_alpha, | ||
| 631 | - dma_negotiation: Some(DmaNegotiation { | ||
| 632 | - modifier, | ||
| 633 | - plane_count: plane_count as i32, | ||
| 634 | - }), | ||
| 635 | - }; | ||
| 636 | - | ||
| 637 | - let o = make_video_params(format.format(), &[modifier], format_size, inner.refresh); | ||
| 638 | - let mut b = Vec::new(); | ||
| 639 | - let pod = make_pod(&mut b, o); | ||
| 640 | - | ||
| 641 | - make_params!(params, &formats, format_size, inner.refresh, format_has_alpha); | ||
| 642 | - params.insert(0, pod); | ||
| 643 | - | ||
| 644 | - if let Err(err) = stream.update_params(&mut params) { | ||
| 645 | - warn!("error updating stream params: {err:?}"); | ||
| 646 | - stop_cast(); | ||
| 647 | } | 667 | } |
| 648 | - } | 668 | + _ => { |
| 649 | - _ => { | 669 | + let o1 = match maybe_prop_modifier { |
| 650 | - let o1 = match maybe_prop_modifier { | 670 | + Some(_) => { |
| 651 | - Some(_) => { | 671 | + // Verify that alpha and modifier didn't change. |
| 652 | - // Verify that alpha and modifier didn't change. | 672 | + let plane_count = match &*state { |
| 653 | - let plane_count = match &*state { | 673 | + CastState::ConfirmationPending { |
| 654 | - CastState::ConfirmationPending { | 674 | + size, |
| 655 | - size, | 675 | + alpha, |
| 656 | - alpha, | 676 | + dma_negotiation: Some(dma_negotiation), |
| 657 | - dma_negotiation: Some(dma_negotiation), | 677 | + } |
| 658 | - } | 678 | + | CastState::Ready { |
| 659 | - | CastState::Ready { | 679 | + size, |
| 660 | - size, | 680 | + alpha, |
| 661 | - alpha, | 681 | + dma_negotiation: Some(dma_negotiation), |
| 662 | - dma_negotiation: Some(dma_negotiation), | 682 | + .. |
| 663 | - .. | 683 | + } if *alpha == format_has_alpha |
| 664 | - } if *alpha == format_has_alpha | 684 | + && dma_negotiation.modifier |
| 665 | - && dma_negotiation.modifier == Modifier::from(format.modifier()) => | 685 | + == Modifier::from(format.modifier()) => |
| 666 | - { | 686 | + { |
| 667 | - let size = *size; | 687 | + let size = *size; |
| 668 | - let alpha = *alpha; | 688 | + let alpha = *alpha; |
| 669 | - let dma_negotiation = *dma_negotiation; | 689 | + let dma_negotiation = *dma_negotiation; |
| 670 | - | 690 | + |
| 671 | - let (damage_tracker, cursor_damage_tracker) = | 691 | + let (damage_tracker, cursor_damage_tracker) = |
| 672 | - if let CastState::Ready { | 692 | + if let CastState::Ready { |
| 693 | + damage_tracker, | ||
| 694 | + cursor_damage_tracker, | ||
| 695 | + .. | ||
| 696 | + } = &mut *state | ||
| 697 | + { | ||
| 698 | + ( | ||
| 699 | + damage_tracker.take(), | ||
| 700 | + cursor_damage_tracker.take(), | ||
| 701 | + ) | ||
| 702 | + } else { | ||
| 703 | + (None, None) | ||
| 704 | + }; | ||
| 705 | + | ||
| 706 | + debug!("moving to ready state"); | ||
| 707 | + | ||
| 708 | + *state = CastState::Ready { | ||
| 709 | + size, | ||
| 710 | + alpha, | ||
| 711 | + dma_negotiation: Some(dma_negotiation), | ||
| 673 | damage_tracker, | 712 | damage_tracker, |
| 674 | cursor_damage_tracker, | 713 | cursor_damage_tracker, |
| 675 | - .. | 714 | + last_cursor_location: None, |
| 676 | - } = &mut *state | ||
| 677 | - { | ||
| 678 | - (damage_tracker.take(), cursor_damage_tracker.take()) | ||
| 679 | - } else { | ||
| 680 | - (None, None) | ||
| 681 | }; | 715 | }; |
| 682 | 716 | ||
| 683 | - debug!("moving to ready state"); | 717 | + dma_negotiation.plane_count |
| 684 | - | 718 | + } |
| 685 | - *state = CastState::Ready { | 719 | + _ => { |
| 686 | - size, | 720 | + // We're negotiating a single modifier, or alpha or |
| 687 | - alpha, | 721 | + // modifier changed, so we need to do a test |
| 688 | - dma_negotiation: Some(dma_negotiation), | 722 | + // allocation. |
| 689 | - damage_tracker, | 723 | + let (modifier, plane_count) = |
| 690 | - cursor_damage_tracker, | 724 | + match find_preferred_modifier( |
| 691 | - last_cursor_location: None, | 725 | + &gbm, |
| 692 | - }; | 726 | + format_size, |
| 727 | + fourcc, | ||
| 728 | + vec![format.modifier() as i64], | ||
| 729 | + ) { | ||
| 730 | + Ok(x) => x, | ||
| 731 | + Err(err) => { | ||
| 732 | + warn!( | ||
| 733 | + "test allocation failed: {err:?}" | ||
| 734 | + ); | ||
| 735 | + stop_cast(); | ||
| 736 | + return; | ||
| 737 | + } | ||
| 738 | + }; | ||
| 739 | + | ||
| 740 | + debug!( | ||
| 741 | + "allocation successful \ | ||
| 742 | + (modifier={modifier:?}, \ | ||
| 743 | + plane_count={plane_count}), \ | ||
| 744 | + moving to ready" | ||
| 745 | + ); | ||
| 746 | + | ||
| 747 | + *state = CastState::Ready { | ||
| 748 | + size: format_size, | ||
| 749 | + alpha: format_has_alpha, | ||
| 750 | + dma_negotiation: Some(DmaNegotiation { | ||
| 751 | + modifier, | ||
| 752 | + plane_count: plane_count as i32, | ||
| 753 | + }), | ||
| 754 | + damage_tracker: None, | ||
| 755 | + cursor_damage_tracker: None, | ||
| 756 | + last_cursor_location: None, | ||
| 757 | + }; | ||
| 693 | 758 | ||
| 694 | - dma_negotiation.plane_count | 759 | + plane_count as i32 |
| 695 | - } | 760 | + } |
| 696 | - _ => { | 761 | + }; |
| 697 | - // We're negotiating a single modifier, or alpha or modifier changed, | ||
| 698 | - // so we need to do a test allocation. | ||
| 699 | - let (modifier, plane_count) = match find_preferred_modifier( | ||
| 700 | - &gbm, | ||
| 701 | - format_size, | ||
| 702 | - fourcc, | ||
| 703 | - vec![format.modifier() as i64], | ||
| 704 | - ) { | ||
| 705 | - Ok(x) => x, | ||
| 706 | - Err(err) => { | ||
| 707 | - warn!("test allocation failed: {err:?}"); | ||
| 708 | - stop_cast(); | ||
| 709 | - return; | ||
| 710 | - } | ||
| 711 | - }; | ||
| 712 | - | ||
| 713 | - debug!( | ||
| 714 | - "allocation successful \ | ||
| 715 | - (modifier={modifier:?}, plane_count={plane_count}), \ | ||
| 716 | - moving to ready" | ||
| 717 | - ); | ||
| 718 | - | ||
| 719 | - *state = CastState::Ready { | ||
| 720 | - size: format_size, | ||
| 721 | - alpha: format_has_alpha, | ||
| 722 | - dma_negotiation: Some(DmaNegotiation { | ||
| 723 | - modifier, | ||
| 724 | - plane_count: plane_count as i32, | ||
| 725 | - }), | ||
| 726 | - damage_tracker: None, | ||
| 727 | - cursor_damage_tracker: None, | ||
| 728 | - last_cursor_location: None, | ||
| 729 | - }; | ||
| 730 | - | ||
| 731 | - plane_count as i32 | ||
| 732 | - } | ||
| 733 | - }; | ||
| 734 | 762 | ||
| 735 | - // const BPP: u32 = 4; | 763 | + pod::object!( |
| 736 | - // let stride = format.size().width * BPP; | 764 | + SpaTypes::ObjectParamBuffers, |
| 737 | - // let size = stride * format.size().height; | 765 | + ParamType::Buffers, |
| 766 | + Property::new( | ||
| 767 | + SPA_PARAM_BUFFERS_buffers, | ||
| 768 | + pod::Value::Choice(ChoiceValue::Int(Choice( | ||
| 769 | + ChoiceFlags::empty(), | ||
| 770 | + ChoiceEnum::Range { | ||
| 771 | + default: 8, | ||
| 772 | + min: 2, | ||
| 773 | + max: 16 | ||
| 774 | + } | ||
| 775 | + ))), | ||
| 776 | + ), | ||
| 777 | + Property::new( | ||
| 778 | + SPA_PARAM_BUFFERS_blocks, | ||
| 779 | + pod::Value::Int(plane_count) | ||
| 780 | + ), | ||
| 781 | + Property::new( | ||
| 782 | + SPA_PARAM_BUFFERS_dataType, | ||
| 783 | + pod::Value::Choice(ChoiceValue::Int(Choice( | ||
| 784 | + ChoiceFlags::empty(), | ||
| 785 | + ChoiceEnum::Flags { | ||
| 786 | + default: 1 << DataType::DmaBuf.as_raw(), | ||
| 787 | + flags: vec![1 << DataType::DmaBuf.as_raw()], | ||
| 788 | + }, | ||
| 789 | + ))), | ||
| 790 | + ), | ||
| 791 | + ) | ||
| 792 | + } | ||
| 793 | + None => { | ||
| 794 | + *state = CastState::Ready { | ||
| 795 | + size: format_size, | ||
| 796 | + alpha: format_has_alpha, | ||
| 797 | + dma_negotiation: None, | ||
| 798 | + damage_tracker: None, | ||
| 799 | + cursor_damage_tracker: None, | ||
| 800 | + last_cursor_location: None, | ||
| 801 | + }; | ||
| 802 | + pod::object!( | ||
| 803 | + SpaTypes::ObjectParamBuffers, | ||
| 804 | + ParamType::Buffers, | ||
| 805 | + Property::new( | ||
| 806 | + SPA_PARAM_BUFFERS_buffers, | ||
| 807 | + pod::Value::Choice(ChoiceValue::Int(Choice( | ||
| 808 | + ChoiceFlags::empty(), | ||
| 809 | + ChoiceEnum::Range { | ||
| 810 | + default: 8, | ||
| 811 | + min: 2, | ||
| 812 | + max: 16 | ||
| 813 | + } | ||
| 814 | + ))), | ||
| 815 | + ), | ||
| 816 | + Property::new( | ||
| 817 | + SPA_PARAM_BUFFERS_blocks, | ||
| 818 | + pod::Value::Int(SHM_BLOCKS as i32), | ||
| 819 | + ), | ||
| 820 | + Property::new( | ||
| 821 | + SPA_PARAM_BUFFERS_dataType, | ||
| 822 | + pod::Value::Choice(ChoiceValue::Int(Choice( | ||
| 823 | + ChoiceFlags::empty(), | ||
| 824 | + ChoiceEnum::Flags { | ||
| 825 | + default: 1 << DataType::MemFd.as_raw(), | ||
| 826 | + flags: vec![1 << DataType::MemFd.as_raw()], | ||
| 827 | + }, | ||
| 828 | + ))), | ||
| 829 | + ), | ||
| 830 | + ) | ||
| 831 | + } | ||
| 832 | + }; | ||
| 738 | 833 | ||
| 739 | - pod::object!( | 834 | + let o2 = pod::object!( |
| 740 | - SpaTypes::ObjectParamBuffers, | ||
| 741 | - ParamType::Buffers, | ||
| 742 | - Property::new( | ||
| 743 | - SPA_PARAM_BUFFERS_buffers, | ||
| 744 | - pod::Value::Choice(ChoiceValue::Int(Choice( | ||
| 745 | - ChoiceFlags::empty(), | ||
| 746 | - ChoiceEnum::Range { | ||
| 747 | - default: 8, | ||
| 748 | - min: 2, | ||
| 749 | - max: 16 | ||
| 750 | - } | ||
| 751 | - ))), | ||
| 752 | - ), | ||
| 753 | - Property::new(SPA_PARAM_BUFFERS_blocks, pod::Value::Int(plane_count)), | ||
| 754 | - Property::new( | ||
| 755 | - SPA_PARAM_BUFFERS_dataType, | ||
| 756 | - pod::Value::Choice(ChoiceValue::Int(Choice( | ||
| 757 | - ChoiceFlags::empty(), | ||
| 758 | - ChoiceEnum::Flags { | ||
| 759 | - default: 1 << DataType::DmaBuf.as_raw(), | ||
| 760 | - flags: vec![1 << DataType::DmaBuf.as_raw()], | ||
| 761 | - }, | ||
| 762 | - ))), | ||
| 763 | - ), | ||
| 764 | - ) | ||
| 765 | - }, | ||
| 766 | - None => { | ||
| 767 | - *state = CastState::Ready { | ||
| 768 | - size: format_size, | ||
| 769 | - alpha: format_has_alpha, | ||
| 770 | - dma_negotiation: None, | ||
| 771 | - damage_tracker: None, | ||
| 772 | - cursor_damage_tracker: None, | ||
| 773 | - last_cursor_location: None, | ||
| 774 | - }; | ||
| 775 | - pod::object!( | ||
| 776 | - SpaTypes::ObjectParamBuffers, | ||
| 777 | - ParamType::Buffers, | ||
| 778 | - Property::new( | ||
| 779 | - SPA_PARAM_BUFFERS_buffers, | ||
| 780 | - pod::Value::Choice(ChoiceValue::Int(Choice( | ||
| 781 | - ChoiceFlags::empty(), | ||
| 782 | - ChoiceEnum::Range { | ||
| 783 | - default: 8, | ||
| 784 | - min: 2, | ||
| 785 | - max: 16 | ||
| 786 | - } | ||
| 787 | - ))), | ||
| 788 | - ), | ||
| 789 | - Property::new( | ||
| 790 | - SPA_PARAM_BUFFERS_blocks, | ||
| 791 | - pod::Value::Int(SHM_BLOCKS as i32), | ||
| 792 | - ), | ||
| 793 | - Property::new( | ||
| 794 | - SPA_PARAM_BUFFERS_dataType, | ||
| 795 | - pod::Value::Choice(ChoiceValue::Int(Choice( | ||
| 796 | - ChoiceFlags::empty(), | ||
| 797 | - ChoiceEnum::Flags { | ||
| 798 | - default: 1 << DataType::MemFd.as_raw(), | ||
| 799 | - flags: vec![1 << DataType::MemFd.as_raw()], | ||
| 800 | - }, | ||
| 801 | - ))), | ||
| 802 | - ), | ||
| 803 | - ) | ||
| 804 | - } | ||
| 805 | - }; | ||
| 806 | - | ||
| 807 | - let o2 = pod::object!( | ||
| 808 | - SpaTypes::ObjectParamMeta, | ||
| 809 | - ParamType::Meta, | ||
| 810 | - Property::new( | ||
| 811 | - SPA_PARAM_META_type, | ||
| 812 | - pod::Value::Id(spa::utils::Id(SPA_META_Header)) | ||
| 813 | - ), | ||
| 814 | - Property::new( | ||
| 815 | - SPA_PARAM_META_size, | ||
| 816 | - pod::Value::Int(size_of::<spa_meta_header>() as i32) | ||
| 817 | - ), | ||
| 818 | - ); | ||
| 819 | - | ||
| 820 | - let mut b1 = vec![]; | ||
| 821 | - let mut b2 = vec![]; | ||
| 822 | - | ||
| 823 | - let mut params = vec![make_pod(&mut b1, o1), make_pod(&mut b2, o2)]; | ||
| 824 | - | ||
| 825 | - let mut b_cursor = vec![]; | ||
| 826 | - if cursor_mode == CursorMode::Metadata { | ||
| 827 | - let o_cursor = pod::object!( | ||
| 828 | SpaTypes::ObjectParamMeta, | 835 | SpaTypes::ObjectParamMeta, |
| 829 | ParamType::Meta, | 836 | ParamType::Meta, |
| 830 | Property::new( | 837 | Property::new( |
| 831 | SPA_PARAM_META_type, | 838 | SPA_PARAM_META_type, |
| 832 | - pod::Value::Id(spa::utils::Id(SPA_META_Cursor)) | 839 | + pod::Value::Id(spa::utils::Id(SPA_META_Header)) |
| 833 | ), | 840 | ), |
| 834 | Property::new( | 841 | Property::new( |
| 835 | SPA_PARAM_META_size, | 842 | SPA_PARAM_META_size, |
| 836 | - pod::Value::Int(CURSOR_META_SIZE as i32) | 843 | + pod::Value::Int(size_of::<spa_meta_header>() as i32) |
| 837 | ), | 844 | ), |
| 838 | ); | 845 | ); |
| 839 | - params.push(make_pod(&mut b_cursor, o_cursor)); | ||
| 840 | - } | ||
| 841 | 846 | ||
| 842 | - if let Err(err) = stream.update_params(&mut params) { | 847 | + let mut b1 = vec![]; |
| 843 | - warn!("error updating stream params: {err:?}"); | 848 | + let mut b2 = vec![]; |
| 844 | - stop_cast(); | ||
| 845 | - } | ||
| 846 | - } | ||
| 847 | - }; | ||
| 848 | - } | ||
| 849 | - }) | ||
| 850 | - .add_buffer({ | ||
| 851 | - let inner = inner.clone(); | ||
| 852 | - let stop_cast = stop_cast.clone(); | ||
| 853 | - move |stream, (), buffer| { | ||
| 854 | - let _span = debug_span!("add_buffer", %stream_id).entered(); | ||
| 855 | - let mut inner = inner.borrow_mut(); | ||
| 856 | - | ||
| 857 | - match inner.state { | ||
| 858 | - CastState::Ready { | ||
| 859 | - size, | ||
| 860 | - alpha, | ||
| 861 | - dma_negotiation, | ||
| 862 | - .. | ||
| 863 | - } => { | ||
| 864 | - match dma_negotiation { | ||
| 865 | - Some(DmaNegotiation { modifier, .. }) => { | ||
| 866 | - trace!("pw stream: add_buffer (dma), size={size:?}, alpha={alpha}, modifier={modifier:?}"); | ||
| 867 | - unsafe { | ||
| 868 | - let spa_buffer = (*buffer).buffer; | ||
| 869 | - | ||
| 870 | - let fourcc = if alpha { | ||
| 871 | - Fourcc::Argb8888 | ||
| 872 | - } else { | ||
| 873 | - Fourcc::Xrgb8888 | ||
| 874 | - }; | ||
| 875 | - | ||
| 876 | - let dmabuf = match allocate_dmabuf(&gbm, size, fourcc, modifier) { | ||
| 877 | - Ok(dmabuf) => dmabuf, | ||
| 878 | - Err(err) => { | ||
| 879 | - warn!("error allocating dmabuf: {err:?}"); | ||
| 880 | - stop_cast(); | ||
| 881 | - return; | ||
| 882 | - } | ||
| 883 | - }; | ||
| 884 | - | ||
| 885 | - let plane_count = dmabuf.num_planes(); | ||
| 886 | - assert_eq!((*spa_buffer).n_datas as usize, plane_count); | ||
| 887 | - | ||
| 888 | - for (i, (fd, (stride, offset))) in | ||
| 889 | - zip(dmabuf.handles(), zip(dmabuf.strides(), dmabuf.offsets())) | ||
| 890 | - .enumerate() | ||
| 891 | - { | ||
| 892 | - let spa_data = (*spa_buffer).datas.add(i); | ||
| 893 | - assert!((*spa_data).type_ & (1 << DataType::DmaBuf.as_raw()) > 0); | ||
| 894 | - | ||
| 895 | - (*spa_data).type_ = DataType::DmaBuf.as_raw(); | ||
| 896 | - | ||
| 897 | - // With DMA-BUFs, consumers should ignore the maxsize field, and | ||
| 898 | - // producers are allowed to set it to 0. | ||
| 899 | - // | ||
| 900 | - // https://docs.pipewire.org/page_dma_buf.html | ||
| 901 | - (*spa_data).maxsize = 1; | ||
| 902 | - (*spa_data).fd = fd.as_raw_fd() as i64; | ||
| 903 | - (*spa_data).flags = SPA_DATA_FLAG_READWRITE; | ||
| 904 | - | ||
| 905 | - let chunk = (*spa_data).chunk; | ||
| 906 | - (*chunk).stride = stride as i32; | ||
| 907 | - (*chunk).offset = offset; | ||
| 908 | - | ||
| 909 | - trace!( | ||
| 910 | - "pw buffer plane: fd={}, stride={stride}, offset={offset}", | ||
| 911 | - (*spa_data).fd | ||
| 912 | - ); | ||
| 913 | - } | ||
| 914 | - | ||
| 915 | - let fd = (*(*spa_buffer).datas).fd; | ||
| 916 | - assert!(inner.dmabufs.insert(fd, dmabuf).is_none()); | ||
| 917 | - } | ||
| 918 | - | ||
| 919 | - // During size re-negotiation, the stream sometimes just keeps running, in | ||
| 920 | - // which case we may need to force a redraw once we got a newly sized buffer. | ||
| 921 | - if inner.dmabufs.len() == 1 && stream.state() == StreamState::Streaming { | ||
| 922 | - redraw_(); | ||
| 923 | - } | ||
| 924 | - }, | ||
| 925 | - None => { | ||
| 926 | - trace!("pw stream: add_buffer (shm), size={size:?}, alpha={alpha}"); | ||
| 927 | - unsafe { | ||
| 928 | - let spa_buffer = (*buffer).buffer; | ||
| 929 | - | ||
| 930 | - let shmbuf = match allocate_shmbuf(size) { | ||
| 931 | - Ok(x) => x, | ||
| 932 | - Err(err) => { | ||
| 933 | - warn!("error allocating shmbuf: {err:?}"); | ||
| 934 | - stop_cast(); | ||
| 935 | - return; | ||
| 936 | - } | ||
| 937 | - }; | ||
| 938 | - | ||
| 939 | - assert_eq!((*spa_buffer).n_datas as usize, SHM_BLOCKS); | ||
| 940 | 849 | ||
| 941 | - let spa_data = (*spa_buffer).datas; | 850 | + let mut params = vec![make_pod(&mut b1, o1), make_pod(&mut b2, o2)]; |
| 942 | - assert!((*spa_data).type_ & (1 << DataType::MemFd.as_raw()) > 0); | ||
| 943 | 851 | ||
| 944 | - (*spa_data).type_ = DataType::MemFd.as_raw(); | 852 | + let mut b_cursor = vec![]; |
| 945 | - (*spa_data).maxsize = shmbuf.layout.size; | 853 | + if cursor_mode == CursorMode::Metadata { |
| 946 | - (*spa_data).fd = shmbuf.fd.as_raw_fd() as i64; | 854 | + let o_cursor = pod::object!( |
| 947 | - (*spa_data).flags = SPA_DATA_FLAG_READWRITE; | 855 | + SpaTypes::ObjectParamMeta, |
| 948 | - | 856 | + ParamType::Meta, |
| 949 | - let chunk = (*spa_data).chunk; | 857 | + Property::new( |
| 950 | - (*chunk).stride = shmbuf.layout.stride; | 858 | + SPA_PARAM_META_type, |
| 951 | - (*chunk).offset = 0; | 859 | + pod::Value::Id(spa::utils::Id(SPA_META_Cursor)) |
| 952 | - | 860 | + ), |
| 953 | - let fd = (*(*spa_buffer).datas).fd; | 861 | + Property::new( |
| 954 | - assert!(inner.shmbufs.insert(fd, shmbuf).is_none()); | 862 | + SPA_PARAM_META_size, |
| 955 | - } | 863 | + pod::Value::Int(CURSOR_META_SIZE as i32) |
| 864 | + ), | ||
| 865 | + ); | ||
| 866 | + params.push(make_pod(&mut b_cursor, o_cursor)); | ||
| 867 | + } | ||
| 956 | 868 | ||
| 957 | - // During size re-negotiation, the stream sometimes just keeps running, in | 869 | + if let Err(err) = stream.update_params(&mut params) { |
| 958 | - // which case we may need to force a redraw once we got a newly sized buffer. | 870 | + warn!("error updating stream params: {err:?}"); |
| 959 | - if inner.shmbufs.len() == 1 && stream.state() == StreamState::Streaming { | 871 | + stop_cast(); |
| 960 | - redraw_(); | ||
| 961 | - } | ||
| 962 | } | 872 | } |
| 963 | } | 873 | } |
| 964 | - }, | 874 | + }; |
| 965 | - _ => { | ||
| 966 | - trace!("pw stream: add buffer, but not ready yet"); | ||
| 967 | - } | ||
| 968 | } | 875 | } |
| 969 | - } | 876 | + }) |
| 970 | - }) | 877 | + .add_buffer({ |
| 971 | - .remove_buffer({ | 878 | + let inner = inner.clone(); |
| 972 | - let inner = inner.clone(); | 879 | + let stop_cast = stop_cast.clone(); |
| 973 | - move |_stream, (), buffer| { | 880 | + move |stream, (), buffer| { |
| 974 | - let mut inner = inner.borrow_mut(); | 881 | + let _span = debug_span!("add_buffer", %stream_id).entered(); |
| 975 | - | 882 | + |
| 976 | - inner | 883 | + match unsafe { inner.borrow_mut().on_add_buffer(&gbm, buffer) } { |
| 977 | - .rendering_buffers | 884 | + Ok(redraw) => { |
| 978 | - .retain(|(buf, _)| buf.as_ptr() != buffer); | 885 | + // During size re-negotiation, the stream sometimes just keeps |
| 979 | - | 886 | + // running, in which case we may need to force a redraw once we got |
| 980 | - unsafe { | 887 | + // a newly sized buffer. |
| 981 | - let spa_buffer = (*buffer).buffer; | 888 | + if redraw && stream.state() == StreamState::Streaming { |
| 982 | - let spa_data = (*spa_buffer).datas; | 889 | + redraw_(); |
| 983 | - | 890 | + } |
| 984 | - if (*spa_data).type_ == DataType::DmaBuf.as_raw() { | 891 | + } |
| 985 | - trace!(%stream_id, "pw stream: remove_buffer (dma)"); | 892 | + Err(err) => { |
| 986 | - assert!((*spa_buffer).n_datas > 0); | 893 | + warn!("error adding pw buffer: {err:?}"); |
| 987 | - | 894 | + stop_cast(); |
| 988 | - let fd = (*spa_data).fd; | 895 | + } |
| 989 | - inner.dmabufs.remove(&fd); | 896 | + }; |
| 990 | - } else if (*spa_data).type_ == DataType::MemFd.as_raw() { | 897 | + } |
| 991 | - trace!(%stream_id, "pw stream: remove_buffer (shm)"); | 898 | + }) |
| 992 | - assert_eq!((*spa_buffer).n_datas, SHM_BLOCKS as u32); | 899 | + .remove_buffer({ |
| 993 | - let fd = (*spa_data).fd; | 900 | + let inner = inner.clone(); |
| 994 | - inner.shmbufs.remove(&fd); | 901 | + move |_stream, (), buffer| { |
| 995 | - } else { | 902 | + let _span = debug_span!("remove_buffer", %stream_id).entered(); |
| 996 | - error!(%stream_id, "pw stream: remove_buffer (unknown): {:?}", (*spa_data).type_); | 903 | + |
| 904 | + unsafe { | ||
| 905 | + inner.borrow_mut().on_remove_buffer(buffer); | ||
| 997 | } | 906 | } |
| 998 | } | 907 | } |
| 999 | - } | 908 | + }) |
| 1000 | - }) | 909 | + .register() |
| 1001 | - .register() | 910 | + .unwrap(); |
| 1002 | - .unwrap(); | ||
| 1003 | 911 | ||
| 1004 | trace!( | 912 | trace!( |
| 1005 | %stream_id, | 913 | %stream_id, |
| @@ -1470,6 +1378,136 @@ impl Cast { | |||
| 1470 | } | 1378 | } |
| 1471 | } | 1379 | } |
| 1472 | 1380 | ||
| 1381 | +impl CastInner { | ||
| 1382 | + unsafe fn on_add_buffer( | ||
| 1383 | + &mut self, | ||
| 1384 | + gbm: &GbmDevice<DrmDeviceFd>, | ||
| 1385 | + buffer: *mut pw_buffer, | ||
| 1386 | + ) -> anyhow::Result<bool> { | ||
| 1387 | + let CastState::Ready { | ||
| 1388 | + size, | ||
| 1389 | + alpha, | ||
| 1390 | + dma_negotiation, | ||
| 1391 | + .. | ||
| 1392 | + } = self.state | ||
| 1393 | + else { | ||
| 1394 | + trace!("pw stream: add_buffer, but not ready yet"); | ||
| 1395 | + return Ok(false); | ||
| 1396 | + }; | ||
| 1397 | + | ||
| 1398 | + match dma_negotiation { | ||
| 1399 | + Some(DmaNegotiation { modifier, .. }) => { | ||
| 1400 | + trace!( | ||
| 1401 | + "pw stream: add_buffer (dma), size={size:?}, \ | ||
| 1402 | + alpha={alpha}, modifier={modifier:?}" | ||
| 1403 | + ); | ||
| 1404 | + unsafe { | ||
| 1405 | + let spa_buffer = (*buffer).buffer; | ||
| 1406 | + | ||
| 1407 | + let fourcc = if alpha { | ||
| 1408 | + Fourcc::Argb8888 | ||
| 1409 | + } else { | ||
| 1410 | + Fourcc::Xrgb8888 | ||
| 1411 | + }; | ||
| 1412 | + | ||
| 1413 | + let dmabuf = allocate_dmabuf(gbm, size, fourcc, modifier) | ||
| 1414 | + .context("error allocating dmabuf")?; | ||
| 1415 | + | ||
| 1416 | + let plane_count = dmabuf.num_planes(); | ||
| 1417 | + assert_eq!((*spa_buffer).n_datas as usize, plane_count); | ||
| 1418 | + | ||
| 1419 | + for (i, (fd, (stride, offset))) in | ||
| 1420 | + zip(dmabuf.handles(), zip(dmabuf.strides(), dmabuf.offsets())).enumerate() | ||
| 1421 | + { | ||
| 1422 | + let spa_data = (*spa_buffer).datas.add(i); | ||
| 1423 | + assert!((*spa_data).type_ & (1 << DataType::DmaBuf.as_raw()) > 0); | ||
| 1424 | + | ||
| 1425 | + (*spa_data).type_ = DataType::DmaBuf.as_raw(); | ||
| 1426 | + | ||
| 1427 | + // With DMA-BUFs, consumers should ignore the maxsize field, and | ||
| 1428 | + // producers are allowed to set it to 0. | ||
| 1429 | + // | ||
| 1430 | + // https://docs.pipewire.org/page_dma_buf.html | ||
| 1431 | + (*spa_data).maxsize = 1; | ||
| 1432 | + (*spa_data).fd = fd.as_raw_fd() as i64; | ||
| 1433 | + (*spa_data).flags = SPA_DATA_FLAG_READWRITE; | ||
| 1434 | + | ||
| 1435 | + let chunk = (*spa_data).chunk; | ||
| 1436 | + (*chunk).stride = stride as i32; | ||
| 1437 | + (*chunk).offset = offset; | ||
| 1438 | + | ||
| 1439 | + trace!( | ||
| 1440 | + "pw buffer plane: fd={}, stride={stride}, offset={offset}", | ||
| 1441 | + (*spa_data).fd | ||
| 1442 | + ); | ||
| 1443 | + } | ||
| 1444 | + | ||
| 1445 | + let fd = (*(*spa_buffer).datas).fd; | ||
| 1446 | + assert!(self.dmabufs.insert(fd, dmabuf).is_none()); | ||
| 1447 | + } | ||
| 1448 | + | ||
| 1449 | + Ok(self.dmabufs.len() == 1) | ||
| 1450 | + } | ||
| 1451 | + None => { | ||
| 1452 | + trace!("pw stream: add_buffer (shm), size={size:?}, alpha={alpha}"); | ||
| 1453 | + unsafe { | ||
| 1454 | + let spa_buffer = (*buffer).buffer; | ||
| 1455 | + | ||
| 1456 | + let shmbuf = allocate_shmbuf(size).context("error allocating shmbuf")?; | ||
| 1457 | + | ||
| 1458 | + assert_eq!((*spa_buffer).n_datas as usize, SHM_BLOCKS); | ||
| 1459 | + | ||
| 1460 | + let spa_data = (*spa_buffer).datas; | ||
| 1461 | + assert!((*spa_data).type_ & (1 << DataType::MemFd.as_raw()) > 0); | ||
| 1462 | + | ||
| 1463 | + (*spa_data).type_ = DataType::MemFd.as_raw(); | ||
| 1464 | + (*spa_data).maxsize = shmbuf.layout.size; | ||
| 1465 | + (*spa_data).fd = shmbuf.fd.as_raw_fd() as i64; | ||
| 1466 | + (*spa_data).flags = SPA_DATA_FLAG_READWRITE; | ||
| 1467 | + | ||
| 1468 | + let chunk = (*spa_data).chunk; | ||
| 1469 | + (*chunk).stride = shmbuf.layout.stride; | ||
| 1470 | + (*chunk).offset = 0; | ||
| 1471 | + | ||
| 1472 | + let fd = (*(*spa_buffer).datas).fd; | ||
| 1473 | + assert!(self.shmbufs.insert(fd, shmbuf).is_none()); | ||
| 1474 | + } | ||
| 1475 | + | ||
| 1476 | + Ok(self.shmbufs.len() == 1) | ||
| 1477 | + } | ||
| 1478 | + } | ||
| 1479 | + } | ||
| 1480 | + | ||
| 1481 | + unsafe fn on_remove_buffer(&mut self, buffer: *mut pw_buffer) { | ||
| 1482 | + self.rendering_buffers | ||
| 1483 | + .retain(|(buf, _)| buf.as_ptr() != buffer); | ||
| 1484 | + | ||
| 1485 | + unsafe { | ||
| 1486 | + let spa_buffer = (*buffer).buffer; | ||
| 1487 | + let spa_data = (*spa_buffer).datas; | ||
| 1488 | + | ||
| 1489 | + if (*spa_data).type_ == DataType::DmaBuf.as_raw() { | ||
| 1490 | + trace!("pw stream: remove_buffer (dma)"); | ||
| 1491 | + assert!((*spa_buffer).n_datas > 0); | ||
| 1492 | + | ||
| 1493 | + let fd = (*spa_data).fd; | ||
| 1494 | + self.dmabufs.remove(&fd); | ||
| 1495 | + } else if (*spa_data).type_ == DataType::MemFd.as_raw() { | ||
| 1496 | + trace!("pw stream: remove_buffer (shm)"); | ||
| 1497 | + assert_eq!((*spa_buffer).n_datas, SHM_BLOCKS as u32); | ||
| 1498 | + | ||
| 1499 | + let fd = (*spa_data).fd; | ||
| 1500 | + self.shmbufs.remove(&fd); | ||
| 1501 | + } else { | ||
| 1502 | + error!( | ||
| 1503 | + "pw stream: remove_buffer (unknown type): {:?}", | ||
| 1504 | + (*spa_data).type_ | ||
| 1505 | + ); | ||
| 1506 | + } | ||
| 1507 | + } | ||
| 1508 | + } | ||
| 1509 | +} | ||
| 1510 | + | ||
| 1473 | impl CastState { | 1511 | impl CastState { |
| 1474 | fn pending_size(&self) -> Option<Size<u32, Physical>> { | 1512 | fn pending_size(&self) -> Option<Size<u32, Physical>> { |
| 1475 | match self { | 1513 | match self { |