<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Membrane Framework - Medium]]></title>
        <description><![CDATA[Membrane is more than another tool providing certain solutions. It’s a know-how to build a tool that answers your needs. - Medium]]></description>
        <link>https://medium.com/membraneframework?source=rss----4543cfe12af7---4</link>
        <image>
            <url>https://cdn-images-1.medium.com/proxy/1*TGH72Nnw24QL3iV9IOm4VA.png</url>
            <title>Membrane Framework - Medium</title>
            <link>https://medium.com/membraneframework?source=rss----4543cfe12af7---4</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 22:21:46 GMT</lastBuildDate>
        <atom:link href="https://medium.com/feed/membraneframework" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Modular RTC Engine is our little-big revolution in video conferencing]]></title>
            <link>https://medium.com/membraneframework/modular-rtc-engine-is-our-little-big-revolution-in-video-conferencing-cfde806c5beb?source=rss----4543cfe12af7---4</link>
            <guid isPermaLink="false">https://medium.com/p/cfde806c5beb</guid>
            <category><![CDATA[multimedia]]></category>
            <category><![CDATA[streaming]]></category>
            <category><![CDATA[membrane]]></category>
            <category><![CDATA[elixir]]></category>
            <category><![CDATA[webrtc]]></category>
            <dc:creator><![CDATA[Błażej Pierzak]]></dc:creator>
            <pubDate>Wed, 27 Jul 2022 14:44:45 GMT</pubDate>
            <atom:updated>2022-07-27T14:44:26.506Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*P3NOgwb8ix-dlr9-u2n9Zg.png" /></figure><p>There are already quite a few WebRTC SFU implementations in different programming languages. They’ve got their pros and cons, but the biggest problem about them is that you need to take them as they are. If you need to do something extraordinary, like creating a custom logic, there is a lot of work you need to do. Probably you need to rewrite almost everything from the original library to finally get something that matches your requirement. But the result could still be hardly reusable.</p><p>We’ve made Membrane as a solution for that kind of trouble. That’s why we decided on the pipeline to be the core concept of the framework. And that’s why we have made it easy to create your own element for the pipeline besides the ones we provided. That modularity is the main advantage of Membrane’s approach.</p><p>Later when we started working on our WebRTC implementation we concentrated on implementing the SFU architecture and making things work. We encapsulated the pipeline inside RTC Engine, and we made it do nothing more, but receive a stream from each peer and transfer it to other peers (OK, many other things happen there, but they’re less directly related to media transferring).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/631/0*3bgiXj8_DXh697lK" /></figure><p>Technically, the RTC Engine is still a pipeline, so now we decided to rebuild it and make it available to add new functionalities. What kind? You can decide for yourself. With the new architecture of the Membrane RTC Engine, we give you the possibility to arrange your SFU server however you need.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/631/0*DHT51fKr5Clu35W5" /></figure><p>But let’s talk about the basic configuration. We’ve got an RTC Engine running on some machine with a basic configuration for WebRTC connections. After you establish a connection with the server it will send info to the RTC Engine about a new peer connected. RTC Engine will create a process called Endpoint (based on the specification we provide) for each connected peer. An Endpoint is a new abstraction that allows us to connect different types of I/O within the RTC Engine. In our case, it will be a WebRTC type Endpoint. Next, our Endpoint will receive from the RTC Engine the list of tracks broadcasted by other Endpoints and can subscribe to each of those. (Or not. You may write some custom logic for that if you need it). After subscription, your Endpoint will start receiving data from the broadcaster and pass it to your browser.</p><p>Accordingly, if you start sending data, your Endpoint will send a message to the RTC Engine that it’s starting broadcasting. RTC Engine will pass this message to other Endpoints, and they’ll subscribe (or not) to your broadcast.</p><p>So, what is so revolutionary here? Let’s Imagine that you have an existing application like that — a simple video conference application, and you want to record each peer video to a file. Now you can do this without changing the whole SFU application. All you need is to create a new Endpoint that will subscribe to each peer broadcast, and write it down into a file. It won’t require much work, because that Endpoint is a regular Membrane Bin. One new thing is, to receive data from the other endpoints, it needs to register into the RTC Engine. After receiving a message about currently available tracks, it needs to answer with proper subscription info. That’s all. The rest is just a simple pipeline linking:</p><p>link_bin_input() |&gt; to(:encoder) |&gt; to(:file_sink)</p><p>We can also create a filter Endpoint. Like in the previous example, we can subscribe to some tracks, process them and then send them back, letting other Endpoints subscribe to our work.</p><p>You can just imagine scenarios that might find that useful: wanna add a watermark or overlay on each peer video? Sure. Dump it to file before that watermark is added? Done. Mix videos and stream them as one using RTMP protocol? Not easy but doable. Capture an audio track and send it directly to Speech-To-Text. We can do it. Or maybe you need to turn an SFU to MCU architecture? That’s tough but possible. That list is endless.</p><p>We created sample Endpoints for HLS output and obviously for WebRTC, but we’ll keep working to provide more plug-and-play Endpoints for some common scenarios. Everything else it’s up to you — what bricks you’re going to use to build the app you need?</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=cfde806c5beb" width="1" height="1" alt=""><hr><p><a href="https://medium.com/membraneframework/modular-rtc-engine-is-our-little-big-revolution-in-video-conferencing-cfde806c5beb">Modular RTC Engine is our little-big revolution in video conferencing</a> was originally published in <a href="https://medium.com/membraneframework">Membrane Framework</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Bundlex — compiling C with Elixir made easy]]></title>
            <link>https://medium.com/membraneframework/bundlex-compiling-c-with-elixir-made-easy-9ac5d7d24da5?source=rss----4543cfe12af7---4</link>
            <guid isPermaLink="false">https://medium.com/p/9ac5d7d24da5</guid>
            <category><![CDATA[compilation]]></category>
            <category><![CDATA[elixir]]></category>
            <category><![CDATA[erlang-nif]]></category>
            <category><![CDATA[erlang]]></category>
            <category><![CDATA[c-language]]></category>
            <dc:creator><![CDATA[Mateusz Front]]></dc:creator>
            <pubDate>Thu, 30 Jun 2022 12:08:42 GMT</pubDate>
            <atom:updated>2022-06-30T12:08:42.544Z</atom:updated>
            <content:encoded><![CDATA[<h3>Bundlex — compiling C with Elixir made easy</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Kr4ALj2bUdXyjhzzie8EjQ.png" /></figure><h3>Note: Bundlex has grown since this article was firstly published</h3><p>In particular, it now supports C nodes besides NIFs. See <a href="https://github.com/membraneframework/bundlex">Bundlex GitHub</a> for more details.</p><p>When integrating Elixir with native code (e.g., written in C) one of the important aspects is adapting new tooling responsible for compiling that code into our workflow. Of course, a native compiler can be invoked manually, but this approach is far from perfect, mainly for the following reasons:</p><ul><li>you have to take care of compiling the code each time it changes,</li><li>if someone uses your project as a dependency (no matter whether directly or indirectly), they still have to execute the native compiler manually,</li><li>if you wish to support different operating systems, you have to handle this yourself,</li><li>if you have several projects and dependencies in the native code between them, you need to resolve these dependencies somehow.</li></ul><p>All these are problems we have encountered while developing <a href="https://membraneframework.org/">Membrane</a>. As we intended this framework to be as modular as possible, we have split it into multiple Elixir projects, which often rely on native libraries and sometimes depend on a common C code. We quickly realized that managing all the native stuff manually is a huge pain, and that’s why we created <a href="https://github.com/membraneframework/bundlex">Bundlex</a>. It lets you declare and use the native code in a convenient way.</p><h4>Declaring NIFs</h4><p>In projects using Bundlex native code is grouped into NIFs. Here is an example of how they are declared:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/437df705ec27225ea7a09ba206349a29/href">https://medium.com/media/437df705ec27225ea7a09ba206349a29/href</a></iframe><p>When mix compiles our app, it runs Bundlex which creates and runs the compilation script for each NIF based on the above configuration. Built binaries (so/dll) are stored in the priv directory, ready to be loaded. Simple as that!</p><p>NIFs may also be specified differently for different platforms:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0eba5221d9f3270699cf1c7b599ad89e/href">https://medium.com/media/0eba5221d9f3270699cf1c7b599ad89e/href</a></iframe><p>For more details and all supported options for declaring NIFs, see <a href="https://hexdocs.pm/bundlex/Bundlex.Project.html">the documentation of Bundlex.Project module</a>.</p><h4>Loading NIFs</h4><p>Libraries generated by Bundlex can be loaded the same way as any other native library, but our tool provides a more convenient way of doing so:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/80a60893f2dfdacade72bcaf713ae6a3/href">https://medium.com/media/80a60893f2dfdacade72bcaf713ae6a3/href</a></iframe><p>When using Bundlex.Loader, needed NIF is identified by the name from the specification, and the boilerplate for loading it is generated. Moreover, native functions can be concisely declared with defnif macro. A private counterpart, defnifpis also available, being useful especially when some argument/result parsing is needed:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6b6db345decaedeeb9c87310573f5826/href">https://medium.com/media/6b6db345decaedeeb9c87310573f5826/href</a></iframe><h4>Let’s try it!</h4><p>Sounds great? Be sure to take a look at the <a href="https://hexdocs.pm/bundlex/readme.html">readme</a> which describes step-by-step how to add Bundlex to your project. You can also check out how we use it in our tools, such as <a href="https://github.com/membraneframework/shmex">Shmex</a> or <a href="https://github.com/membraneframework">Membrane elements</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9ac5d7d24da5" width="1" height="1" alt=""><hr><p><a href="https://medium.com/membraneframework/bundlex-compiling-c-with-elixir-made-easy-9ac5d7d24da5">Bundlex — compiling C with Elixir made easy</a> was originally published in <a href="https://medium.com/membraneframework">Membrane Framework</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Unifex — integrate native code with Elixir without a hassle]]></title>
            <link>https://medium.com/membraneframework/unifex-integrate-native-code-with-elixir-without-a-hassle-9cff3b6b4554?source=rss----4543cfe12af7---4</link>
            <guid isPermaLink="false">https://medium.com/p/9cff3b6b4554</guid>
            <category><![CDATA[elixir]]></category>
            <category><![CDATA[erlang]]></category>
            <category><![CDATA[erlang-nif]]></category>
            <category><![CDATA[c-language]]></category>
            <category><![CDATA[membrane-framework]]></category>
            <dc:creator><![CDATA[Bartosz Błaszków]]></dc:creator>
            <pubDate>Thu, 02 Jun 2022 13:58:04 GMT</pubDate>
            <atom:updated>2022-06-07T08:47:24.262Z</atom:updated>
            <content:encoded><![CDATA[<h3>Unifex — integrate native code with Elixir without a hassle</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BV5Vlb4dlPmRMSbi1zXeUw.png" /></figure><p>While developing <a href="https://www.membraneframework.org/">Membrane Framework</a>, which is our solution for multimedia streaming written in Elixir, we often have to rely on native code to integrate with existing libraries or when we need a native performance in crucial bits of code.</p><p>The Erlang VM, on which the Elixir code is executed, allows you to create <a href="http://erlang.org/doc/man/erl_nif.html">NIFs (Native Implemented Functions)</a> — functions implemented in C that can be used just like any other function defined in Elixir or Erlang module. With the v0.2.0 release of <a href="https://hex.pm/packages/membrane_core">Membrane Core</a>, we introduced <a href="https://github.com/membraneframework/unifex"><strong>Unifex</strong></a>, a tool that makes NIF creation easier.</p><p>Unifex job is to generate an interface between a native C code and Elixir. Based on a file with .spec.exs extension, it creates the C boilerplate. All you have to do is to provide the definition (and destructor) of a struct storing state and implement the functions using generated helpers.</p><p>Here’s an example of .spec.exs file:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0f2cea46e2fd43b3696667e3dda98046/href">https://medium.com/media/0f2cea46e2fd43b3696667e3dda98046/href</a></iframe><p>The above spec defined that we would like to implement two functions for Example.Native module called foo and init; we’d also like to send a message {:example_msg, number} from the native code. Below you can find the native part of the implementation:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b924955d5bc28ec8bf88f3d9eae24b5c/href">https://medium.com/media/b924955d5bc28ec8bf88f3d9eae24b5c/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/dc56c1e30d4207b234e0f2a679122f64/href">https://medium.com/media/dc56c1e30d4207b234e0f2a679122f64/href</a></iframe><h3>What are the benefits of using Unifex?</h3><h4>Argument parsing</h4><p>Please take a look at the init function from the example.c above — it gets an int as an argument. How could that be possible with Elixir (and Erlang) using dynamic typing? Well, normally when implementing a NIF, your function would receive an array of generic Erlang terms (ERL_NIF_TERM type) that you would parse to a proper type (and if that fails, return an error). Unifex handles that for you. And saves you from mistakes like confusing the order of arguments and using incorrect indices. Here’s the comparison of code with and without Unifex:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fbd30aa849ee76f8a6d60eabbfc99c89/href">https://medium.com/media/fbd30aa849ee76f8a6d60eabbfc99c89/href</a></iframe><h4>Automatically generated helpers for returned values and messages sent — one simple call to rule them all!</h4><p>Thanks to the helpers generated by our tool, a conversion the other way around (from native types to terms) and sending messages become as easy as can be. Below you can find an example contrasting plain NIF and Unifex. Both snippets send the {:example_msg, number} message and return either {:error, :send_failed} or {:ok, number}. Just imagine how complicated it would become if we wanted to return a more complex result, e.g., nested tuples. Another example shows the same can be said about message sending.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b68a19abc612aec70ca9ea2ed1bede42/href">https://medium.com/media/b68a19abc612aec70ca9ea2ed1bede42/href</a></iframe><h4>Separation from erl_nif library</h4><p>Unifex creates an abstraction layer above erl_nif library. The NIFs written with Unifex does not directly use that library. Thanks to that, if in the future we would like to run the native code in a separate process (e.g., using <a href="http://erlang.org/doc/tutorial/c_port.html">Erlang port</a> mechanism instead of NIFs), we could add support for it in Unifex while keeping the API (more or less) the same.</p><p>That’s not the only benefit of having an abstraction layer on top of erl_nif. Let’s consider a call that sends a message:</p><pre>enif_send(NULL, target, env, term);</pre><p>As a matter of fact, both 1st and 3rd arguments are ErlNifEnvs. Without getting into details — if you’re making a call from a created thread (i.e., outside of Erlang VM scheduler thread executing the NIF), you should swap the NULL and env in that call. Unfortunately, it’s quite easy to forget which variant should be used for each case. Unifex wraps that call and uses more explicit UNIFEX_NO_FLAGS and UNIFEX_SEND_THREADED flags to determine the proper NULL and env order.</p><p>If you’re interested in using our tool and would like to see it in action, be sure to check out <a href="https://hexdocs.pm/unifex/creating_unifex_nif.html#content">the simple tutorial</a> that will, step by step, guide you to writing your first Unifex-based NIF. Also, <a href="https://hexdocs.pm/unifex">the docs</a> might be quite helpful, and the source code is available on <a href="https://github.com/membraneframework/unifex">GitHub</a>. Of course, since this is an initial release of our tool, it may still be a bit rough around the edges (e.g., some native types are not supported yet), but it has already proven its worth in Membrane elements, such as <a href="https://github.com/membraneframework/membrane-element-mad">MAD Decoder</a> or <a href="https://github.com/membraneframework/membrane-element-ffmpeg-h264">FFmpeg-based H264 Encoder/Decoder</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9cff3b6b4554" width="1" height="1" alt=""><hr><p><a href="https://medium.com/membraneframework/unifex-integrate-native-code-with-elixir-without-a-hassle-9cff3b6b4554">Unifex — integrate native code with Elixir without a hassle</a> was originally published in <a href="https://medium.com/membraneframework">Membrane Framework</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[We speed up Membrane by reshaping Maps operations]]></title>
            <link>https://medium.com/membraneframework/we-speed-up-membrane-by-reshaping-maps-operations-56b2a5e35153?source=rss----4543cfe12af7---4</link>
            <guid isPermaLink="false">https://medium.com/p/56b2a5e35153</guid>
            <category><![CDATA[membrane]]></category>
            <category><![CDATA[erlang]]></category>
            <category><![CDATA[functional-programming]]></category>
            <category><![CDATA[elixir]]></category>
            <category><![CDATA[multimedia]]></category>
            <dc:creator><![CDATA[Błażej Pierzak]]></dc:creator>
            <pubDate>Mon, 30 May 2022 10:41:14 GMT</pubDate>
            <atom:updated>2022-05-31T08:29:25.481Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*A0nPeoXg3r2h3WN2PcbyjQ.png" /></figure><p>We’re constantly working on improving the performance of the Membrane. It’s not easy, as it’s not obvious where to look for improvements. There are many factors that may affect performance and so it’s hard to point out just one that could be a bottleneck. We also don’t want to spend weeks or even months making changes that may not be as satisfying as we all expect. On the other hand, it could be pleasing when your work is making a real difference in the end. Here is a little story of our latest work on speeding up our pipeline mechanism.</p><p>It all began when we started to work on improving read and update time when using Elixir’s Map in Membrane. It’s crucial as we are using maps all the time to keep the state of processes and pass-through messages. Basically, all the data flow through the pipeline is done through maps. We’ve done many benchmarks, tested all provided access and update methods, and finally noticed significant differences between operation times using them.</p><p>The most popular way to access the value by key from the map is to type:</p><pre>var = our_map.key</pre><p>But the issue is that the syntax here is ambiguous. Using a function from a module stored in the variable will look pretty much the same:</p><pre>var = module_name.func</pre><p>So the compiler firstly needs to check what types of data it’s operating on. It’s not an expensive check, but done multiple times, and/or on a nested map might be noticeable</p><p>The other commonly used option is to use Map module functions:</p><pre>var = Map.get(our_map, key)</pre><p>That looks simple, but using a function from an external module (in that case Map module) always takes more time. Moreover, on compile-time, there are some limitations of optimizing code as Erlang allows to switch module implementation while running (that’s a cool trick but against performance in that particular case)</p><p>The fastest way to get data stored in a map in Elixir is to use pattern matching:</p><pre>%{^key = value} = our_map</pre><p>This syntax is well known in the Elixir community. It’s straightforward and self-descriptive, but it starts to be unreadable when it comes to nested maps. It becomes even more crooked when you need to update the value. In the Kernel module, there is a ‘update_in/3’ function but it’s based on Access behavior, which makes it quite generic, and also generates unwanted overhead.</p><p>To make it as fast as possible, we decided to create our own, Map specific, macro based on the same mechanics as ‘update_in/3’. We’ve compared it with other methods and the results are pleasing:</p><pre>CPU Information: Intel(R) Core(TM) i5-1038NG7 CPU @ 2.00GHz</pre><pre>Number of Available Cores: 8</pre><pre>Available memory: 16 GB</pre><pre>Elixir 1.14.0-dev</pre><pre>Erlang 24.3</pre><pre>Name               ips        average  deviation         median         99th %</pre><pre>fast_map        3.37 K      297.03 μs    ±18.08%         284 μs         513 μs</pre><pre>map             1.47 K      681.08 μs    ±25.72%         644 μs     1119.77 μs</pre><pre>Comparison:</pre><pre>fast_map        3.37 K</pre><pre>map             1.47 K - 2.29x slower +384.06 μs</pre><pre>Name                ips        average  deviation         median         99th %</pre><pre>fast_map         1.32 K        0.76 ms   ±123.00%        0.66 ms        1.74 ms</pre><pre>update_in        0.43 K        2.34 ms    ±76.97%        2.17 ms        4.11 ms</pre><pre>Comparison:</pre><pre>fast_map         1.32 K</pre><pre>update_in        0.43 K - 3.10x slower +1.59 ms</pre><p>We also measured it with our <a href="https://github.com/membraneframework/membrane_performance_test">internal tool for measuring the pipeline’s performance</a> — it measures the number of messages per second sent through the pipeline:</p><pre>Fast-map: PUSH: 57391.27 PULL: 41546.39 AUTODEMAND: 53491.85 [msg/s]</pre><pre>v0.9.0: PUSH: 46394.90 PULL: 27361.77 AUTODEMAND: 34696.72 [msg/s]</pre><p>Then we tested Membrane Videoroom (one room with 21 participants) using <a href="https://github.com/membraneframework/beamchmark">BEAMchmark</a>:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Mcxo1IjFE5sknTVNeB0x4Q.png" /></figure><p>We introduced it in the v0.10.0 of Membrane Core, but due to Elixir’s bug, we found it wasn’t available right away. We found out that the code generated by our macro is freezing the compiler for some reason. We were debugging it for a pretty long time, blaming ourselves for accidental infinite recursion, but the problem seems to lay in elixir’s compiler type checking mechanism. The newest Elixir release v1.13.4 contains a fix for that bug and it’s already released (there are some inadequate type-check warnings while compiling a code, but those will disappear in Elixir v.1.14), we can finally announce a faster <a href="https://github.com/membraneframework/membrane_core">Membrane Core</a> is ready to check!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=56b2a5e35153" width="1" height="1" alt=""><hr><p><a href="https://medium.com/membraneframework/we-speed-up-membrane-by-reshaping-maps-operations-56b2a5e35153">We speed up Membrane by reshaping Maps operations</a> was originally published in <a href="https://medium.com/membraneframework">Membrane Framework</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[What’s up in your BEAM? — introducing BEAMchmark]]></title>
            <link>https://medium.com/membraneframework/whats-up-in-your-beam-introducing-beamchmark-8c2a12a54881?source=rss----4543cfe12af7---4</link>
            <guid isPermaLink="false">https://medium.com/p/8c2a12a54881</guid>
            <category><![CDATA[benchmark]]></category>
            <category><![CDATA[erlang]]></category>
            <category><![CDATA[membrane]]></category>
            <category><![CDATA[elixir]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Błażej Pierzak]]></dc:creator>
            <pubDate>Thu, 14 Apr 2022 08:04:32 GMT</pubDate>
            <atom:updated>2022-04-15T08:58:21.627Z</atom:updated>
            <content:encoded><![CDATA[<h3>What’s up in your BEAM? — introducing BEAMchmark</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4K_uTPm2bMaKr9vz5bFLbg.png" /></figure><p>Every software creator knows well that benchmarks are pretty important. They prove that your software is not only working but also scales well, and acts properly when overloaded. Benchmarking also might let you learn the limits of your program. We have been making benchmarks of Membrane since the very beginning, but we have noticed that measuring CPU and memory usage is often not enough for us. That led us to create a tool for measuring more parameters. As most of Membrane’s software is written in Elixir we focused on Elixir’s specific parameters like scheduler utilization, reductions, and the number of context switches. And that’s how BEAMchmark was born.</p><p>BEAMchmark, as the name suggests is used to benchmark BEAM (Erlang Virtual Machine), which stands behind executing every Elixir application. To understand how BEAMchmark is different from typical benchmarking scripts, let’s take a quick look at how Elixir’s processes are executed.</p><p>Let’s start with OTP. OTP (Open Telecom Platform) is an integral part of Elixir distribution that provides abstractions used by developers to define processes, control them, set up a supervision tree (to describe relations between processes), and pass messages between processes. Processes in OTP can be executed concurrently and also can communicate with each other, so they are able to interact with each other. Processes are sometimes compared to threads existing in other languages, but they are much more lightweight.</p><p>The part of BEAM that controls the execution of processes is called the scheduler, and its work is based on reductions (every function call is a reduction). It’s working in a continuous loop, sequentially running processes from the run queue, but allowing them to make only certain numbers of reductions each in one stage. That allows BEAM to run processes concurrently even with only one scheduler. Processes that haven’t finished execution, but just hit a reduction limit, are still in the run queue, even if they’re not affecting the CPU. That’s why measuring BEAM performance might be sometimes more precise for Elixir applications than measuring bare CPU usage.</p><p>BEAMchmark is reporting a percentage of runtime when a particular scheduler is busy. It can also show you the total number of reductions, and the number of context switches (moments when the scheduler is changing the currently running process). It’s crucial when you need to observe how small changes impact the performance of your application.</p><p>To let you easily see how it changed, BEAMchmark is presenting results also as a difference between the current and previous run of the script:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NmBdVG6DcIVtGYtL8pmx-Q.png" /><figcaption>example HTML BEAMchmark report</figcaption></figure><p>To use BEAMchmark you need to create a script with a scenario you want to test out. You can then start your application with the specified conditions you want to test.</p><p>You might increase the load gradually or just see how it will behave as the time passes:</p><pre>defmodule SimpleScenario do<br>  @moduledoc false<br><br>  @behaviour Beamchmark.Scenario<br><br>  @impl true<br>  def run() do<br>    1..1_000<br>    |&gt; Stream.cycle()<br>    |&gt; Stream.each(fn i -&gt; Integer.pow(i, 2) end)<br>  end<br>end<br><br>Beamchmark.run(SimpleScenario, duration: 5, delay: 1)</pre><p>We’re using BEAMchmark while working on the performance of Membrane’s libraries and it helped us see things we didn’t have a chance to notice before. And now you can do the same with your applications. You can find BEAMchmark here: <a href="https://github.com/membraneframework/beamchmark">https://github.com/membraneframework/beamchmark</a>. It’s open-source like all other elements of Membrane, so take it and use it to make your software better.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8c2a12a54881" width="1" height="1" alt=""><hr><p><a href="https://medium.com/membraneframework/whats-up-in-your-beam-introducing-beamchmark-8c2a12a54881">What’s up in your BEAM? — introducing BEAMchmark</a> was originally published in <a href="https://medium.com/membraneframework">Membrane Framework</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How we made Membrane SFU less ICE-y]]></title>
            <link>https://medium.com/membraneframework/how-we-made-membrane-sfu-less-ice-y-9625472ec386?source=rss----4543cfe12af7---4</link>
            <guid isPermaLink="false">https://medium.com/p/9625472ec386</guid>
            <category><![CDATA[elixir]]></category>
            <category><![CDATA[videoconference]]></category>
            <category><![CDATA[multimedia]]></category>
            <category><![CDATA[membrane]]></category>
            <category><![CDATA[webrtc]]></category>
            <dc:creator><![CDATA[Błażej Pierzak]]></dc:creator>
            <pubDate>Mon, 11 Apr 2022 08:20:11 GMT</pubDate>
            <atom:updated>2022-04-11T08:20:11.017Z</atom:updated>
            <content:encoded><![CDATA[<p><em>This article was previously published on the </em><a href="https://blog.swmansion.com/"><em>Software Mansion blog</em></a><em>.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3uKLnMSJNMd3CQiVzzAFng.png" /></figure><p>A well-known problem in software development you probably noticed in your career is when existing technology with solutions it provides is not exactly matching the problem you need to solve. In most cases, there are a lot of workarounds to be found, additional settings, and proxy options, but in the end, the architecture or the code doesn’t look too good. Sometimes it’s wise to just stand aside and look if we really need all that stuff.</p><p><a href="https://en.wikipedia.org/wiki/WebRTC">WebRTC</a> has been made to let people communicate peer-to-peer using browsers (it’s mostly used to send media, but it’s not the only purpose). It’s working well because of <a href="https://en.wikipedia.org/wiki/Interactive_Connectivity_Establishment">ICE</a> which lets browsers connect even if they’re hidden behind <a href="https://en.wikipedia.org/wiki/Network_address_translation">NAT</a>. But what works well in direct communication between browsers, also creates unwanted overhead if you are building an <a href="https://webrtcglossary.com/sfu/">SFU</a>. You need to establish ICE communication between your server and each of the browsers wanting to join. In our case (Membrane is written in Elixir, therefore uses <a href="https://en.wikipedia.org/wiki/BEAM_(Erlang_virtual_machine)">Erlang Virtual Machine</a> to run), we decided to use <a href="https://libnice.freedesktop.org/">libnice</a> for that. It’s pretty popular, complete, and easier to integrate as it’s written in C.</p><p>It’s not bad in general but it is definitely expensive. Running a libnice process for each peer connection is a bit overkill, and it won’t scale up nicely. We’ve started building Membrane with scalability in mind so we needed to do something with it.</p><h3>Good things are not cheap</h3><p>Going back to the WebRTC basics there are two common problems for peer connection that ICE is helping you to solve: browser unawareness of its public identity because of NAT, and inaccessibility for UDP connections which are required, but not allowed by some firewalls. They’re commonly solved by <a href="https://webrtcglossary.com/stun/">STUN</a> and <a href="https://webrtcglossary.com/turn/">TURN</a> servers depending on the particular situation.</p><p>The STUN server allows you to obtain your public IP address which can be then used by the other side to establish a connection. Request to the STUN server creates a binding in the NAT table from our private IP address to a public one. Next, STUN receives our request and replies informing us about the public IP address it received our request from.</p><p>If it’s not enough to establish a connection (e.g. because of a restrictive firewall, or symmetric NAT) there is a TURN server that can help you. TURN shares its public IP with you by creating an association between your address and one of its ports, letting you present yourself like that to the other side. Then it tunnels all the media you’re transmitting. Moreover, because the other peer is making a connection with TURN, a transmission between TURN and you doesn’t have to be UDP. Generally, ICE requires UDP (there are some exceptions), but the ICE connection is now between the other peer and TURN.</p><p>Both STUN and TURN are commonly used but while STUN is quite cheap and easy (all it does is it provides a simple answer to the “Who am I?” question), TURN needs to be a powerful machine as its primary role is to translate all the media and address them properly, sometimes handling a lot of bindings at the time.</p><h3>Do we really need it?</h3><p>Considering SFU architecture, all WebRTC naming conventions might be a little misleading. We’re still having peers but one of them is an SFU server. So we’ve got a bunch of browsers connecting to SFU. Inside, each of those connections is operating through a libnice process. If we assume that some of the browsers might need to connect through the TURN server, we’ll end up creating two servers (SFU and TURN) needing to send media between each other.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zd41vaSscVO2zBSm8a_GHQ.png" /></figure><p>Of course, external TURN is a must-have in peer-to-peer connections but in SFU architecture its work is limited to receiving TURN messages from the browser, translating them to UDP datagram, and sending it to the SFU server. And backwards of course. That proxy is surely expensive. It requires a machine to run on and doubles the number of expensive media transferring connections. But it doesn’t have to be like that.</p><p>First, let’s give all browsers only one option: connection via TURN. It might sound ridiculous, but there is a method to this madness. Then, let’s look up two pairs we’ve got: The TURN — browser connection is crucial as it requires a dedicated format, but the SFU — TURN connection is something we can easily simplify. Imagine TURN and SFU servers running on the same machine. We’ll save a lot of bandwidth and have faster connections. And what if they both run inside an EVM and communicate via Erlang messages? That will speed it up even more, and lets us control all the processes in one place. Moreover, there are existing TURN implementations in Erlang, so it’s not too much work. To be precise we decided on a quasi TURN server because browser—TURN communication is twofold. Connectivity checks are passed and handled by libnice, while media packets are not captured and internally sent directly into the SFU process.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dWXgTv4OWhjOuMGpV1ZxwA.png" /></figure><p>Of course, we still can’t live without relying on libnice but we’ve limited its responsibility, and in near future, we’re planning to make its usage fully optional. It’s a lot of work to make a full Elixir ICE implementation, a lot more than to implement a TURN server, but, as I mentioned above, for SFU we don’t need full implementation. Having all communication going through TURN we need to implement only a small part of negotiation and connectivity checks. That will help us to keep the whole connection in control of an SFU application.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9625472ec386" width="1" height="1" alt=""><hr><p><a href="https://medium.com/membraneframework/how-we-made-membrane-sfu-less-ice-y-9625472ec386">How we made Membrane SFU less ICE-y</a> was originally published in <a href="https://medium.com/membraneframework">Membrane Framework</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Introducing Auto Demands — release of Membrane v0.9.0]]></title>
            <link>https://medium.com/membraneframework/introducing-auto-demands-release-of-membrane-v0-9-0-c0213d8efa51?source=rss----4543cfe12af7---4</link>
            <guid isPermaLink="false">https://medium.com/p/c0213d8efa51</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[multimedia]]></category>
            <category><![CDATA[streaming]]></category>
            <category><![CDATA[membrane]]></category>
            <category><![CDATA[elixir]]></category>
            <dc:creator><![CDATA[Błażej Pierzak]]></dc:creator>
            <pubDate>Wed, 30 Mar 2022 13:36:41 GMT</pubDate>
            <atom:updated>2022-03-30T13:36:40.967Z</atom:updated>
            <content:encoded><![CDATA[<h3>Introducing Auto Demands — release of Membrane v0.9.0</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7pZvb6ckIw9ZiuMDuPkIQA.png" /></figure><p>In the v.0.9.0 release of Membrane, among small but important improvements (<a href="https://github.com/membraneframework/membrane_core/releases/tag/v0.9.0">all listed here</a>) there is one quite a big change: Auto Demands.</p><p>To understand how auto demands can ease the development of Membrane’s applications, let’s have a quick look at how demands used to work before that change, and what they serve for.</p><p>Imagine a simple pipeline, where media packets are passed from source element A through filter element B into sink element C. So A is reading some data, preparing a package, and sending it to B, where logic is making some changes to this package and sending it to C. Finally C is receiving the package and transferring it into the output device. Even in that simple example, since A, B, and C are separated processes working asynchronously, each of them can process the same package at different times. Moreover, the package itself will be different at different stages. That asynchronicity is of course the power of the pipeline, but it also requires some coordination. That’s what demands are used for.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/381/0*LL4oe6fxwZq0EiaS" /></figure><p>After receiving a package, when its processing is about to finish, every element (except for the Source elements of course) should send a demand message to the previous element with the size of the next package it wants to receive. The previous element should handle that message by sending the next portion of data when it’s ready.</p><p>By implementing that mechanism by themselves developers can keep full control of the data flow between elements, but in most easy cases it’s just a bunch of boilerplate code to be written. Also, it’s not hard to make a mistake, which will be pretty hard to debug later. A bug in setting up demands may cause that element to receive too much data — which leads to overflow, or if the demand is not sent, receive no data at all, without pointing any error, as the sender will constantly be waiting for the demand that will never come.</p><p>Now, to ease the process of developing custom elements we decided to implement a basic logic behind demands inside Membrane’s Element code. It’s still up to you if you want to use it or override it, but in most cases, auto demands will provide a working solution.</p><p>To use auto demands, simply pass <strong>demand_mode: :auto</strong> in your pad’s definition:</p><pre>def_input_pad :input,<br>  demand_mode: :auto,<br>  caps: :any```</pre><p>It will result in <strong>Membrane.Element </strong>will now handle sending a <strong>:demand</strong> message to a preceding element by itself.</p><p>Similarly, adding <strong>demand_mode: :auto</strong> to the output pad will cause the element will handle answering to <strong>:demand</strong> method.</p><p>We are hoping that easing the development of custom elements will encourage more developers to write them, as we believe that the ability of customization is one of the Membrane’s superpowers.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c0213d8efa51" width="1" height="1" alt=""><hr><p><a href="https://medium.com/membraneframework/introducing-auto-demands-release-of-membrane-v0-9-0-c0213d8efa51">Introducing Auto Demands — release of Membrane v0.9.0</a> was originally published in <a href="https://medium.com/membraneframework">Membrane Framework</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>