 So we start our next presentation this time on philis multimedia programming using g-streamer and rest. Please welcome the shum Thank you for the warm welcome Can you hear me at the back still? Yeah, good so Start First of all who am I? My name is the channeling as you know, I work for redhead at least nowadays And most of my background is in the false different kinds of free software open source stuff I've been doing mostly on gnomes stuff, but some other things as well And nowadays I work on cloud stuff or at least I'm expected to And I love flying and I love cats so Think about me And what am I talking about? I'll talk about two different technologies when they put together. They make a really good combination Tim in the previous talk I heard that he talked a bit about that already So I don't have to explain a lot. I hope But these are two Like Sebastian he's sitting over there. He's the real hero here. I'm not I'm just talking about the subject He did all the work and made it possible. So If you want to thank someone thank him and buy him beers For this work And it's my talk is inspired by his own talk we he has presented about the same subject before Um, I have a lot to cover So I'll go a bit fast. So but if you don't understand anything, please sketch me after the talk and I'll can I'll happily explain things to you If you don't if I don't explain well in the talk Firstly rust It's a system programming language and it's designed from the beginning to be both efficient and Very safe at the same time. There are many programming languages out there that Focus on efficiency But they are not very safe and then there is the others are very Safe, but they're not efficient. So but rust is one of the few programming languages out there that focus on both And one of the ways it does that is through um, there's no Pointer arithmetic in normal rust in safe rust And which means you can't deref dangling pointers. You can't deref null pointers And that's like the biggest one of the biggest source of problems memory problems in c and c++ and those languages And even in higher many higher languages, you still have null pointers Raw null pointers that you can easily deref and you have null pointer exception and stuff So but rust doesn't have that But of course, you can't do everything with safe In the safe paradigm of rust So rust allows you to make an exception, which is called unsafe and you just mark your code Unsafe and that part of your code usually deals with foreign function interface with c and other programming languages to that world But since rust cannot assume this about safety of what this the the memory handled by those programming languages From ffi It cannot guarantee safety for you and that's why you have to mark it unsafe to rust And it also allows you to do some pointer arithmetic, which you need to do for interfacing with c But the good news is that since you mark it as unsafe that part of the code If you have a memory problem, you know where the problem lies and it's much easier to debug if it's marked as unsafe that part of the code Another thing is that the concept is a non mutable state by default So you cannot once you assign a resource to a variable, for example, you cannot modify it unless it's marked as mutable That also does the similar thing because If you have a problem any kind of problem, you know that it can only be mutable state that that's problematic It cannot be unmutable state that gives you any problems. So it's a similar concept Another thing rust has is strict ownership Semantics, this is not a concept in c and c plus plus in some modern c plus plus Yeah, it is but not really It's not at least enforced by the compiler So you can still do even with the most modern c plus plus Standards you can still do a lot of harm Through memory management and stuff if you want to and it's really easy to do without even wanting to do it And In other languages, it's the same the same concept that the same thing is achieved with garbage collector But garbage collector is always on runtime. So you are using your users resources to to manage the memory for you But rust doesn't do that rust is At least as efficient as c and c plus plus So it doesn't make use of garbage collector and because of that it has these concepts like Ownership of resources. I'll just It's like the concept is very simple each resources can only and only have one Owner so once you assign to To a variable for example here the resource It's Owned by that variable. So in this example This code won't work because once you assign s1 to s2 you have passed the ownership over to To s2 and you cannot use s1 anymore because it doesn't have any resources assigned to it anymore Um similarly with functions You if you pass by value the Any resource to a function you have passed the Ownership to that function and you don't own it anymore unless that function returns it to you and you assign it to a variable If this was s1 equal to some function s1 then it would work because you got the ownership back to you But the normal the small data types like That are efficient to copy. They are they implement a trait in rust called copy And because of that when you pass it to a function by value or to another variable They just get copied. So you can have in a way a multiple ownership, but it's just copies When if you have like a very Small data type which is easy to transfer and stuff and or memory Memory efficiency doesn't matter to you in your case then you can implement the same trait and you can just copy things around And you can borrow Because if you just have one owner and you have your ownership gets transferred It's just not going to work. You you have so many problems that Yeah, you need something like borrowing so You can which is it's a bit like references Pass by reference in c++ you give a reference and you can have multiple Non-mutable references to the same resource in the same code block That borrows our temporary So we have other data types in rust Something like for example rc which stands for reference counting So you can have multiple objects actually pointed to the same resource with this one And they are reference counted for you So each time you need another one. You just clone the the container of the of the actual resource And it's lightweight. So you don't you don't have to worry about that you're copying a lot of the rc structures But the actual resource might be a bit more heavy. So you use rc for that and instead of copying or references So this code will work for example because you clone then you're using separately It's Now we have come to the The the the topic which is kind of like inspiring the title of this talk In rest there is a concept of fearless concurrency Which means that in other programming languages when you as soon as you involve threads Things go wrong and it's really really hard to manage threads in other programming languages But rust makes it safe So you you will get a lot of errors from compilers from compiler when you first do threading in in rust but Once you get your code compiled usually it just works and you can be sure You can rest assured that it's working fine and at runtime Um And for achieving that we have data types like arc One thing I didn't mention is that you you don't have Shared you can't share a state between threads Unless they implement some particular traits The the the data type that you want to share and most of them don't so you you need some kind of container that's Efficient and at the same time safe to use from multiple threads at the same time And that container is called arc which is just atomic reference counting So if you need to share resources between the same thread, then you should use rc Which is a reference counter just but it's not atomic. So it's not safe to to Use from two different threads at the same time But arc is atomics the reference counting is atomic so you can use it At the same time from different threads Um, similarly mutex like arc is only giving you read only access So if you want, um, if you want to modify Something from two different threads, then you have to put it in another container called mutex and then you can you can achieve the same That was my really really quick introduction to rest I'm I'm sure that you have a lot of questions, but you can ask me later And now we come to the second part of the topic It's G streamer How many of you were in the previous talk? Quite a lot, but I'll explain very quickly then G streamer is a multimedia framework It's a very popular especially in the embedded systems out there. There's a lot of embedded systems using it and It's it's a very it uses very simple concepts like elements and pipelines You have a pipeline for each multimedia application you want to create You can have multiple pipelines in the same application as well, but And then you you just like it's like lego's you have multiple elements Doing different things. You just connect them to each other and then You you have source you have filters different kinds of filters and then you have a sync to actually play the media on a quick example is if you want to play Og video with the verb is audio and teora video in it So you will create a pipeline like that You something to read from the source which is in this case a file if you want to read from files of file source And then a demuxer a demultiplexer which demultiplexes the video and audio parts and gives it to These two different allen the decoders and then they go to the sync to to be played audio and video sync whichever it is in this case and The elements they connect through something called pads and And the way you connect pads is that Each pad has a bunch of capabilities on them, which means what can they do for example in verb is decoder case the sync Pad will have capability saying that oh, I can handle verb is a video of different kinds with different properties But it can't for example, you can't Connect it to the pad from demuxer, which gives you the Teora audio for example, because it it doesn't tell you that I can handle it. So you won't ever connect it It's heavily plugin based So the core of distributor is actually pretty small. So most of the things happen through plugins like all the actual capabilities come from there It's written in C It's multi-threaded Well, it's for most app developers. It doesn't really matter in case of g-streamer But when you're writing plugins if you are writing some complex plugins I can be corrected if I'm wrong, but in that in those cases you might need to You don't actually have to handle threads, but you have to take care of that This code is running from multiple threads and you have to take care of locking and stuff. Am I right? Yeah, good so Yeah, and so in a way you have to care of threading at some point or another and Also, like I've never seen a g-share plugin developer who has not touched the core Like if they keep doing plugin development at some point the line gets blurred very soon And it's it's very object-oriented api using the geo object type system from glib So you can do object-oriented programming on c level Which means that on higher level if you have bindings then they get exposed as in an object-oriented way as well So why is rust relevant here? First of all like Major thing is like g-streamer has to be used from media parsing quite a lot and It basically is about media parsing all of all it's about And you you cannot really just trust media Content like just start parsing it you will have you can have many kind of memory problems and you can have kind of memory problem-based attacks and stuff so Especially if you're getting it from untrusted sources, which you usually do from internet and stuff so Safe language would be really good choice for for this Because it makes sure that you don't do most of the memory You you don't you don't make most of the memory handling errors in it so And another thing is multi-threading is hard. It's hard in every language Especially in c and c++. It's even even much harder so As I said rust makes multi-threading really easy to handle Easy in the sense that it makes it makes sure that you don't do most of the errors you will do in You know when sharing now for example state between threads So since it doesn't allow you to do those things It it makes it easier and that's why in in case of g-streamer since it's multi-threaded base So it it's very relevant here And the concepts of mutability and ownership in in rust they map really well To the g-streamer concepts. I had an example here, but I'm skipping it because We don't have enough time But I'll have other examples later And to avoid memory many memory errors in general As I said in c and c++. It's really easy to do those. So rust let's you avoid those so Also sees a very archaic language not it's not just extremely unsafe. It's it's from 70s For 70s 60s something like that. Anyway 70s. Yeah, thanks So it's really old. We need some modern language which has more modern constructs and Stuff like that So Sebastian sitting over there still he made these First rust bindings, which is the first link here on github. I think it's now on gitlab, right on g-streamer It's been merged. I forgot to update these links Anyway, you can find it easily And there's bindings cover a lot of api. I think most of it all of it. I can say all of it. Yeah And just and then he wrote a bunch of plugins based on in rust And based on these bindings So you have really good examples already of how to write plugins in rust if you want to and there's a place for them to Go to upstream If you want to write a new plugin in g-streamer in rust you can do that um A simple example how it helps rust helps g-streamer So I mentioned in my Interaction to the pipelines and elements that you have pads and you have capabilities on them those capabilities are represented in In g-streamer api by something called caps gst caps And each capability because each pad can have multiple capabilities, right? So each capability is represented by something called structure And so caps can have multiple structures, right? And this code what it's trying to do is It's a very silly code. Actually you won't never actually use that so it's Taking of the first structure In the caps there is the first capability And then it tells the caps that I want to remove this capability now and then the capability it got It's trying to set a geologic property on that um In c if you write the same code in c it will compiler will be happy It will let you do what you want, but you will have multiple memory problems at runtime Um, but rust would not allow you to do that as you can see here The first this is the first error you will get from this compiling this code Um, and it seems a bit weird at first But I'll go back If you see the first line you see get structure But there could be that caps doesn't have any structure So How does a rust handle it because as I said it doesn't do null pointers So instead what it has is a enum sorry Instead it has something called it's it has a enum called option and that enum Has two values some or none in case of none. There is no data associated with it But if it's some that means there's something to be given like it's not null And then you can you can get it get that structure object or resource whatever it's in the option in some And so that's why you're getting this error Which just says that what you got was option and option doesn't have this Set method you're calling on it. So it's giving you error. So let's let's handle that now we parse that option Rust gives you a nice construct to parse these return values So you just say I I want to assign this to if it's some then I want to assign it to s The actual structure structure And then you use it in that block and then If you just ignore the the case of none here, but you can handle it if you have an else block here That will handle the none case So now should it should work right but no you have other problems now You have a problem that you're trying to modify Something that is not marked mutable. So the return value of this get struct like when you get something It's not mutable. It's not marked mutable. So you cannot Mutate it anymore and you are trying to do it when you say caps dot remove structure And you are doing s dot set Both things are modifying something. So they are they need a mutable reference to work with Um, so that's why you're getting these all these errors Um, so that's all the mutability issue So instead of that we just tell caps that give us a mutable reference first Then you get a mutable reference to caps Then from a mutable reference of caps you can get a mutable rep to no structure And then you can do what you want Right So you do it's the same thing remove struct and then set I'm out of time already Okay There's also then simultaneous mutability issue, but we don't have time but yeah, anyway the main thing is that C compiler would have let you do all these horrible things but rust stopped you from doing all the All these things and then in the end you come up with really really safe code. It was still useless, but it was safe So anyway, I'll skip this and yeah, thanks We have a few minutes for questions if you want Hi, uh, how hard is it to come to make a bi compare to have a bi compatibility basically With rust because in c you can just define something in the c file and then You don't expose it or the object. You also have the private kind of offset struct thing Uh, how do we do that in rust? Is it I as far as I know you you can't really like There's no a bi compatibility right now like most Yeah, the long answer is a bit long. So I can't explain everything but yeah in short You currently it's not it's not easily possible at least Other questions Yeah, so so you mean that um, you have a c a wrapper And then you um, what I was just saying is you can always export c a bi and that's stable It's just that rust itself doesn't have a stable a bi at this point, but you can export the c a bi You can tell rust that I'm on Other questions. All right, then Well, it was crystal clear I guess Everybody understood everything it's cool Not hesitate. We still have two minutes if you want. Yeah Yep Does the rust does the rust version generate Basically exactly the same code as the c as the safe c version would or is it better or is it worse? It's not the same, but it's yeah, it's safer. You know, you have you have to do something to make it safe, right? Can't be the same You you don't have null pointers even at c level as far as I know Last question maybe I have two questions Good I was just wondering in your examples, uh from rust you were defining a variable With you were defining a variable with lead Yeah, but it was immutable And then you were still defining uh In your example again As lead, but then you got back a mutable variable. Yeah, isn't that kind of no you got a reference Like when you do the lead so you don't always have to give it the type So, um, it can rust can do type inference So it was getting a mutable reference to it just got but how would I know if it's mutable? Because of the return value Okay, yeah, I mean just I've never done anything with with rust. So it's just wondering so At least in the method it said much So it was obvious from the method at least We sometimes talk about glue languages as in higher level languages like quick languages and uh Is there like a trend or can you see a trend where? Rust could be used as a subsystem for higher performance and higher level language or like script or maybe go because I'm more of a go programmer Well, they both could like collaborate in a like in an application Does that make sense or do you are rust applications meant to be like entirely written in rust? Well, you you have a way to do FFI through unsafe code. So you can interact with any kind of language through through that not just see But it's not like if there's no I don't think there's a helper to make it easy for you that For specifically for go. I mean or any particular language I think there is some something for python that makes it easy to To do like interaction between python the guy sitting next to you would know about that Now the other side. Yeah You So but no, I don't know of any go Specifically go for go. I mean as in go coding rust Or or anyway either way around. Yeah, okay. Thanks But as Sebastian said you can expose See a bi an api to the true rust And from go. It's really easy to do see so you can use it that way Thank you Thank you