-
Notifications
You must be signed in to change notification settings - Fork 63
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
Conversation
Awesome, thanks! I'll review this tomorrow |
There was a problem hiding this 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"); |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 || { |
There was a problem hiding this comment.
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 :)
There was a problem hiding this comment.
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!", | ||
); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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]; |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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(); |
There was a problem hiding this comment.
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 ;)
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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(); |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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(); |
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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!
There was a problem hiding this comment.
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(); | ||
} |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done.
@gdesmott What's the status here? :) |
I have a big train trip on Friday, that should give me some time to fix your comments. :) |
6206ef8
to
34e1cc5
Compare
Travis is unhappy with the changes :) |
There were some API improvements/changes in GIT master to improve type-safety and convenience a bit, that's why it fails now. |
34e1cc5
to
e018fc7
Compare
Branch updated. |
There was a problem hiding this 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()))); |
There was a problem hiding this comment.
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(); |
There was a problem hiding this comment.
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 = { |
There was a problem hiding this comment.
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
e018fc7
to
4a9fb29
Compare
See also 483b406 |
Great, thanks for your help. :) |
No description provided.