pw_utils: support negotiating to SHM sharing after connect
Browse files at this commit1cf98949af46eac04d764af110d8aa8750e74ef8Parent: be6ec0a1 changed file +146 -105
- src/screencasting/pw_utils.rs +146 -105
src/screencasting/pw_utils.rsmodified+146 -105
| @@ -60,6 +60,7 @@ use crate::utils::{get_monotonic_time, CastSessionId, CastStreamId}; | ||
| 60 | 60 | |
| 61 | 61 | // Give a 0.1 ms allowance for presentation time errors. |
| 62 | 62 | const CAST_DELAY_ALLOWANCE: Duration = Duration::from_micros(100); |
| 63 | +const SHM_BLOCKS: usize = 1; | |
| 63 | 64 | |
| 64 | 65 | const CURSOR_FORMAT: spa_video_format = SPA_VIDEO_FORMAT_BGRA; |
| 65 | 66 | const CURSOR_BPP: u32 = 4; |
| @@ -571,7 +572,7 @@ impl PipeWire { | ||
| 571 | 572 | // |
| 572 | 573 | // 3. Modifier doesn't exist |
| 573 | 574 | // |
| 574 | - // TODO: set CastState to Ready and set buffer to SHM. | |
| 575 | + // Set CastState to Ready and set buffer to SHM. | |
| 575 | 576 | |
| 576 | 577 | let object = pod.as_object().unwrap(); |
| 577 | 578 | let maybe_prop_modifier = object.find_prop(spa::utils::Id(FormatProperties::VideoModifier.0)); |
| @@ -636,122 +637,164 @@ impl PipeWire { | ||
| 636 | 637 | stop_cast(); |
| 637 | 638 | } |
| 638 | 639 | } |
| 639 | - Some(_) => { | |
| 640 | - // Verify that alpha and modifier didn't change. | |
| 641 | - let plane_count = match &*state { | |
| 642 | - CastState::ConfirmationPending { | |
| 643 | - size, | |
| 644 | - alpha, | |
| 645 | - dma_negotiation: Some(dma_negotiation), | |
| 646 | - } | |
| 647 | - | CastState::Ready { | |
| 648 | - size, | |
| 649 | - alpha, | |
| 650 | - dma_negotiation: Some(dma_negotiation), | |
| 651 | - .. | |
| 652 | - } if *alpha == format_has_alpha | |
| 653 | - && dma_negotiation.modifier | |
| 654 | - == Modifier::from(format.modifier()) => | |
| 655 | - { | |
| 656 | - let size = *size; | |
| 657 | - let alpha = *alpha; | |
| 658 | - let dma_negotiation = *dma_negotiation; | |
| 659 | - | |
| 660 | - let (damage_tracker, cursor_damage_tracker) = | |
| 661 | - if let CastState::Ready { | |
| 662 | - damage_tracker, | |
| 663 | - cursor_damage_tracker, | |
| 640 | + _ => { | |
| 641 | + let o1 = match maybe_prop_modifier { | |
| 642 | + Some(_) => { | |
| 643 | + // Verify that alpha and modifier didn't change. | |
| 644 | + let plane_count = match &*state { | |
| 645 | + CastState::ConfirmationPending { | |
| 646 | + size, | |
| 647 | + alpha, | |
| 648 | + dma_negotiation: Some(dma_negotiation), | |
| 649 | + } | |
| 650 | + | CastState::Ready { | |
| 651 | + size, | |
| 652 | + alpha, | |
| 653 | + dma_negotiation: Some(dma_negotiation), | |
| 664 | 654 | .. |
| 665 | - } = &mut *state | |
| 655 | + } if *alpha == format_has_alpha | |
| 656 | + && dma_negotiation.modifier == Modifier::from(format.modifier()) => | |
| 666 | 657 | { |
| 667 | - (damage_tracker.take(), cursor_damage_tracker.take()) | |
| 668 | - } else { | |
| 669 | - (None, None) | |
| 670 | - }; | |
| 671 | - | |
| 672 | - debug!("moving to ready state"); | |
| 673 | - | |
| 674 | - *state = CastState::Ready { | |
| 675 | - size, | |
| 676 | - alpha, | |
| 677 | - dma_negotiation: Some(dma_negotiation), | |
| 678 | - damage_tracker, | |
| 679 | - cursor_damage_tracker, | |
| 680 | - last_cursor_location: None, | |
| 681 | - }; | |
| 658 | + let size = *size; | |
| 659 | + let alpha = *alpha; | |
| 660 | + let dma_negotiation = *dma_negotiation; | |
| 661 | + | |
| 662 | + let (damage_tracker, cursor_damage_tracker) = | |
| 663 | + if let CastState::Ready { | |
| 664 | + damage_tracker, | |
| 665 | + cursor_damage_tracker, | |
| 666 | + .. | |
| 667 | + } = &mut *state | |
| 668 | + { | |
| 669 | + (damage_tracker.take(), cursor_damage_tracker.take()) | |
| 670 | + } else { | |
| 671 | + (None, None) | |
| 672 | + }; | |
| 673 | + | |
| 674 | + debug!("moving to ready state"); | |
| 675 | + | |
| 676 | + *state = CastState::Ready { | |
| 677 | + size, | |
| 678 | + alpha, | |
| 679 | + dma_negotiation: Some(dma_negotiation), | |
| 680 | + damage_tracker, | |
| 681 | + cursor_damage_tracker, | |
| 682 | + last_cursor_location: None, | |
| 683 | + }; | |
| 684 | + | |
| 685 | + dma_negotiation.plane_count | |
| 686 | + } | |
| 687 | + _ => { | |
| 688 | + // We're negotiating a single modifier, or alpha or modifier changed, | |
| 689 | + // so we need to do a test allocation. | |
| 690 | + let (modifier, plane_count) = match find_preferred_modifier( | |
| 691 | + &gbm, | |
| 692 | + format_size, | |
| 693 | + fourcc, | |
| 694 | + vec![format.modifier() as i64], | |
| 695 | + ) { | |
| 696 | + Ok(x) => x, | |
| 697 | + Err(err) => { | |
| 698 | + warn!("test allocation failed: {err:?}"); | |
| 699 | + stop_cast(); | |
| 700 | + return; | |
| 701 | + } | |
| 702 | + }; | |
| 703 | + | |
| 704 | + debug!( | |
| 705 | + "allocation successful \ | |
| 706 | + (modifier={modifier:?}, plane_count={plane_count}), \ | |
| 707 | + moving to ready" | |
| 708 | + ); | |
| 682 | 709 | |
| 683 | - dma_negotiation.plane_count | |
| 684 | - } | |
| 685 | - _ => { | |
| 686 | - // We're negotiating a single modifier, or alpha or modifier changed, | |
| 687 | - // so we need to do a test allocation. | |
| 688 | - let (modifier, plane_count) = match find_preferred_modifier( | |
| 689 | - &gbm, | |
| 690 | - format_size, | |
| 691 | - fourcc, | |
| 692 | - vec![format.modifier() as i64], | |
| 693 | - ) { | |
| 694 | - Ok(x) => x, | |
| 695 | - Err(err) => { | |
| 696 | - warn!("test allocation failed: {err:?}"); | |
| 697 | - stop_cast(); | |
| 698 | - return; | |
| 710 | + *state = CastState::Ready { | |
| 711 | + size: format_size, | |
| 712 | + alpha: format_has_alpha, | |
| 713 | + dma_negotiation: Some(DmaNegotiation { | |
| 714 | + modifier, | |
| 715 | + plane_count: plane_count as i32, | |
| 716 | + }), | |
| 717 | + damage_tracker: None, | |
| 718 | + cursor_damage_tracker: None, | |
| 719 | + last_cursor_location: None, | |
| 720 | + }; | |
| 721 | + | |
| 722 | + plane_count as i32 | |
| 699 | 723 | } |
| 700 | 724 | }; |
| 701 | 725 | |
| 702 | - debug!( | |
| 703 | - "allocation successful \ | |
| 704 | - (modifier={modifier:?}, plane_count={plane_count}), \ | |
| 705 | - moving to ready" | |
| 706 | - ); | |
| 707 | - | |
| 726 | + // const BPP: u32 = 4; | |
| 727 | + // let stride = format.size().width * BPP; | |
| 728 | + // let size = stride * format.size().height; | |
| 729 | + | |
| 730 | + pod::object!( | |
| 731 | + SpaTypes::ObjectParamBuffers, | |
| 732 | + ParamType::Buffers, | |
| 733 | + Property::new( | |
| 734 | + SPA_PARAM_BUFFERS_buffers, | |
| 735 | + pod::Value::Choice(ChoiceValue::Int(Choice( | |
| 736 | + ChoiceFlags::empty(), | |
| 737 | + ChoiceEnum::Range { | |
| 738 | + default: 8, | |
| 739 | + min: 2, | |
| 740 | + max: 16 | |
| 741 | + } | |
| 742 | + ))), | |
| 743 | + ), | |
| 744 | + Property::new(SPA_PARAM_BUFFERS_blocks, pod::Value::Int(plane_count)), | |
| 745 | + Property::new( | |
| 746 | + SPA_PARAM_BUFFERS_dataType, | |
| 747 | + pod::Value::Choice(ChoiceValue::Int(Choice( | |
| 748 | + ChoiceFlags::empty(), | |
| 749 | + ChoiceEnum::Flags { | |
| 750 | + default: 1 << DataType::DmaBuf.as_raw(), | |
| 751 | + flags: vec![1 << DataType::DmaBuf.as_raw()], | |
| 752 | + }, | |
| 753 | + ))), | |
| 754 | + ), | |
| 755 | + ) | |
| 756 | + }, | |
| 757 | + None => { | |
| 708 | 758 | *state = CastState::Ready { |
| 709 | 759 | size: format_size, |
| 710 | 760 | alpha: format_has_alpha, |
| 711 | - dma_negotiation: Some(DmaNegotiation { | |
| 712 | - modifier, | |
| 713 | - plane_count: plane_count as i32, | |
| 714 | - }), | |
| 761 | + dma_negotiation: None, | |
| 715 | 762 | damage_tracker: None, |
| 716 | 763 | cursor_damage_tracker: None, |
| 717 | 764 | last_cursor_location: None, |
| 718 | 765 | }; |
| 719 | - | |
| 720 | - plane_count as i32 | |
| 766 | + pod::object!( | |
| 767 | + SpaTypes::ObjectParamBuffers, | |
| 768 | + ParamType::Buffers, | |
| 769 | + Property::new( | |
| 770 | + SPA_PARAM_BUFFERS_buffers, | |
| 771 | + pod::Value::Choice(ChoiceValue::Int(Choice( | |
| 772 | + ChoiceFlags::empty(), | |
| 773 | + ChoiceEnum::Range { | |
| 774 | + default: 8, | |
| 775 | + min: 2, | |
| 776 | + max: 16 | |
| 777 | + } | |
| 778 | + ))), | |
| 779 | + ), | |
| 780 | + Property::new( | |
| 781 | + SPA_PARAM_BUFFERS_blocks, | |
| 782 | + pod::Value::Int(SHM_BLOCKS as i32), | |
| 783 | + ), | |
| 784 | + Property::new( | |
| 785 | + SPA_PARAM_BUFFERS_dataType, | |
| 786 | + pod::Value::Choice(ChoiceValue::Int(Choice( | |
| 787 | + ChoiceFlags::empty(), | |
| 788 | + ChoiceEnum::Flags { | |
| 789 | + default: 1 << DataType::MemFd.as_raw(), | |
| 790 | + flags: vec![1 << DataType::MemFd.as_raw()], | |
| 791 | + }, | |
| 792 | + ))), | |
| 793 | + ), | |
| 794 | + ) | |
| 721 | 795 | } |
| 722 | 796 | }; |
| 723 | 797 | |
| 724 | - // const BPP: u32 = 4; | |
| 725 | - // let stride = format.size().width * BPP; | |
| 726 | - // let size = stride * format.size().height; | |
| 727 | - | |
| 728 | - let o1 = pod::object!( | |
| 729 | - SpaTypes::ObjectParamBuffers, | |
| 730 | - ParamType::Buffers, | |
| 731 | - Property::new( | |
| 732 | - SPA_PARAM_BUFFERS_buffers, | |
| 733 | - pod::Value::Choice(ChoiceValue::Int(Choice( | |
| 734 | - ChoiceFlags::empty(), | |
| 735 | - ChoiceEnum::Range { | |
| 736 | - default: 8, | |
| 737 | - min: 2, | |
| 738 | - max: 16 | |
| 739 | - } | |
| 740 | - ))), | |
| 741 | - ), | |
| 742 | - Property::new(SPA_PARAM_BUFFERS_blocks, pod::Value::Int(plane_count)), | |
| 743 | - Property::new( | |
| 744 | - SPA_PARAM_BUFFERS_dataType, | |
| 745 | - pod::Value::Choice(ChoiceValue::Int(Choice( | |
| 746 | - ChoiceFlags::empty(), | |
| 747 | - ChoiceEnum::Flags { | |
| 748 | - default: 1 << DataType::DmaBuf.as_raw(), | |
| 749 | - flags: vec![1 << DataType::DmaBuf.as_raw()], | |
| 750 | - }, | |
| 751 | - ))), | |
| 752 | - ), | |
| 753 | - ); | |
| 754 | - | |
| 755 | 798 | let o2 = pod::object!( |
| 756 | 799 | SpaTypes::ObjectParamMeta, |
| 757 | 800 | ParamType::Meta, |
| @@ -764,8 +807,10 @@ impl PipeWire { | ||
| 764 | 807 | pod::Value::Int(size_of::<spa_meta_header>() as i32) |
| 765 | 808 | ), |
| 766 | 809 | ); |
| 810 | + | |
| 767 | 811 | let mut b1 = vec![]; |
| 768 | 812 | let mut b2 = vec![]; |
| 813 | + | |
| 769 | 814 | let mut params = vec![make_pod(&mut b1, o1), make_pod(&mut b2, o2)]; |
| 770 | 815 | |
| 771 | 816 | let mut b_cursor = vec![]; |
| @@ -790,10 +835,6 @@ impl PipeWire { | ||
| 790 | 835 | stop_cast(); |
| 791 | 836 | } |
| 792 | 837 | } |
| 793 | - None => { | |
| 794 | - warn!("pw stream: modifier prop missing and we don't support shared memory sharing currently"); | |
| 795 | - stop_cast(); | |
| 796 | - } | |
| 797 | 838 | }; |
| 798 | 839 | } |
| 799 | 840 | }) |