1

I'm developing a GStreamer plugin using Rust. I want to send back an array of objects from rust to c++. Each object has two fields. word and confidence. I tried this code. but it did not compile. I can send the structure whenever it just has text. But when I want to add words, I couldn't make it compile at all.

let v = vec![];
for word in transcript.words {
    v.push(gst::Structure::new("word-info",
    &[("word", &word.word), ("confidence", &word.confidence)]));
}

let structure = gst::Structure::new(
    "result",
    &[
        ("text", &transcript.text),
        ("words", &gst::Array::from(&v[..]))
    ],
);

element
    .emit_by_name("new-transcript", &[&structure])
    .unwrap();

So I want to send back structure. It has two fields. text and words. words is a list of (word, confidence) pairs.

s4eed
  • 7,173
  • 9
  • 67
  • 104
  • Where is the code that sends data between C++? This just looks like you are creating data in preparation to send. Or is there more going on in `Structure::new`? – Locke Feb 10 '22 at 21:57
  • "It didn't work" → what didn't work? Did it fail to compile? With what error message? Did it compile but crash when run? With what error message? Did it compile and run but not do what you expected? What did you expect and what did it do instead? – Jmb Feb 11 '22 at 07:59
  • @Jmb you're right. I'll update the question. but it doesn't compile. I'm completely wrong. – s4eed Feb 11 '22 at 10:13
  • @Locke Yes. the other parts of the code work expectedly. I'll update the question tho. but this part of the code doesn't compile at all – s4eed Feb 11 '22 at 10:14

1 Answers1

0

Just minor changes were needed. I had to send a vector of SendValue to gst::Array::from_owned

let v = vec![];
for word in transcript.words {
    v.push(gst::Structure::new("word-info",
    &[("word", &word.word), ("confidence", &word.confidence)]).to_send_value());
}

let structure = gst::Structure::new(
    "result",
    &[
        ("text", &transcript.text),
        ("words", &gst::Array::from_owned(&v[..]))
    ],
);

element
    .emit_by_name("new-transcript", &[&structure])
    .unwrap();
s4eed
  • 7,173
  • 9
  • 67
  • 104
  • Just some comments - It's usually nicer to use `gst::Structure::builder()` instead of `::new()` because then you can omit all the `&`s everywhere and it looks more like idiomatic Rust. - You can use [`gst::Array::new(v)`](https://gstreamer.pages.freedesktop.org/gstreamer-rs/0.18/docs/gstreamer/struct.Array.html#method.new) with version 0.18 of the GStreamer bindings without having to call `to_send_value()` or other complications. – Sebastian Dröge Feb 16 '22 at 08:14
  • @SebastianDröge Thanks man, Where have you been so far? :D – s4eed Feb 16 '22 at 09:33