Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add basic-tutorial-8.r #52

Closed
wants to merge 1 commit into from
Closed

Conversation

gdesmott
Copy link
Contributor

@gdesmott gdesmott commented Nov 8, 2017

No description provided.

@sdroege
Copy link
Owner

sdroege commented Nov 8, 2017

Awesome, thanks! I'll review this tomorrow

Copy link
Owner

@sdroege sdroege left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally looks good, just a couple of cosmetic things. Not for everything I added a comment to each instance of it, please check if the same cosmetic change would also make sense elsewhere :)

.unwrap();
let audio_caps = info.to_caps().unwrap();
app_source.set_property("caps", &audio_caps).unwrap();
app_source.set_property_from_str("format", "time");
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Setting gst::Format::Time would be nicer

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I missed that; thanks.

println!("start feeding");

let data_clone = data.clone();
d.source_id = Some(idle_add(move || {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

glib::idle_add would be nicer. I don't like globally imported functions very much :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.


let appsink = app_sink.clone().dynamic_cast::<AppSink>().expect(
"Sink element is expected to be an appsink!",
);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe instead of having two variables that have almost the same name (appsrc and app_source), just cast here and use that in the future?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. I just had to move code around a bit to do so.

let data_clone = data.clone();
d.source_id = Some(idle_add(move || {
let data = &data_clone;
let mut samples: Vec<u8> = vec![0; CHUNK_SIZE];
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could also just allocate a buffer and then go over its data. It's potentially faster as the memory doesn't have to be zero-initialized first

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed. Done.

let samples = samples.as_mut_slice_of::<i16>().unwrap();
pts = data.num_samples
.mul_div_floor(gst::SECOND, SAMPLE_RATE as u64)
.unwrap();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you wait long enough, this will panic as u64 overflows ;)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What would be the proper way to handle that?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use expect instead of unwrap for now, or if it returns None (i.e. overflows), you could just go EOS and stop doing anything

}

data.num_samples += num_samples as u64;
appsrc = data.appsrc.clone();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could do let (appsrc, pts) = { the long block };. That's IMHO nicer than having "uninitialized" variable declarations

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed. That's the kind of Rust trick I'm not yet used to. :)

let mut data = data.lock().unwrap();
if let Some(source) = data.source_id.take() {
println!("stop feeding");
source_remove(source);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, glib::source_remove

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done


// configure appsink
app_sink.set_property("emit-signals", &true).unwrap();
app_sink.set_property("caps", &audio_caps).unwrap();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doensn't appsink have proper functions for settings these, instead of stringly-typed properties? (Same for appsrc btw)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed; fixed.
Should appsrc::set_caps() take a Option(Caps) as gst_app_src_set_caps() allows 'caps' being NULL?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, same for appsink. Fixing right now, thanks!

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in c9636bc

{
let data = &data_clone.lock().unwrap();
appsink = data.appsink.clone();
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also here, let the block return the appsink clone

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

@sdroege
Copy link
Owner

sdroege commented Nov 15, 2017

@gdesmott What's the status here? :)

@gdesmott
Copy link
Contributor Author

I have a big train trip on Friday, that should give me some time to fix your comments. :)

@sdroege
Copy link
Owner

sdroege commented Nov 17, 2017

Travis is unhappy with the changes :)

@sdroege
Copy link
Owner

sdroege commented Nov 17, 2017

There were some API improvements/changes in GIT master to improve type-safety and convenience a bit, that's why it fails now.

@gdesmott
Copy link
Contributor Author

Branch updated.

Copy link
Owner

@sdroege sdroege left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks very nice now, just 3 minor things left :)

);

let data: Arc<Mutex<CustomData>> =
Arc::new(Mutex::new(CustomData::new(appsrc.clone(), appsink.clone())));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could let new() take the two elements by reference and clone internally, and directly return the Arc<Mutex<_>> instead of cluttering the code with that here :)

let data_clone = data.clone();
appsrc.connect_need_data(move |_, _size| {
let data = &data_clone;
let mut d = data_clone.lock().unwrap();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be nicer to also use data here instead of data_clone

let num_samples = CHUNK_SIZE / 2; /* Each sample is 16 bits */
let mut buffer = gst::Buffer::with_size(CHUNK_SIZE).unwrap();

let appsrc = {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let (appsrc, buffer) = { ... } would seem nicer, and you can also move the num_samples variable into the block

@sdroege
Copy link
Owner

sdroege commented Nov 17, 2017

See also 483b406

@gdesmott
Copy link
Contributor Author

Great, thanks for your help. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants