Exploring the VoIP engine Mediastreamer2. Part 3

The article material is taken from my Zen channel.

Exploring the VoIP engine Mediastreamer2. Part 3

Improving the tone generator example

In the previous article We wrote a tone generator app that extracts sound from the computer speakers. Now let's focus on the fact that our program, upon completion, does not return memory back to the heap. It's time to clarify this issue.

Once the schema is no longer needed, memory release should begin with stopping the data pipeline. For this, we need to disconnect the clock source from the schema using the function ms_ticker_detach(). In our case, we need to disconnect the ticker from the input of the filter voidsource:

ms_ticker_detach(ticker, voidsource)

By the way, after stopping the pipeline, we can change its schema and restart it, reconnecting the ticker again.

Now we can remove it using the function ms_ticker_destroy():

ms_ticker_destroy(ticker)

The pipeline is stopped and we can begin dismantling its parts by unlinking the filters. The function used for this is ms_filter_unlink():

ms_filter_unlink(voidsource, 0, dtmfgen, 0);
ms_filter_unlink(dtmfgen, 0, snd_card_write, 0);

The argument assignment is the same as for the function ms_filter_link().

Now we remove the unlinked filters using ms_filter_destroy():

ms_filter_destroy(voidsource);
ms_filter_destroy(dtmfgen);
ms_filter_destroy(snd_card_write);

By adding these lines to our example, we will achieve a proper program termination in terms of memory management.

As we can see, properly terminating the program required us to add about the same number of lines of code as at its beginning; on average, we need four lines of code for each filter. This means that the size of the program code will increase proportionally to the number of filters used in the project. If there are a thousand filters in the schema, then your code will expand by four thousand lines of routine operations for their creation and destruction.

Now you know how to correctly terminate a program using a media streamer. In the following examples, for the sake of conciseness, I will "forget" to do this. But you won't forget, will you?

The media streamer developers did not provide software tools to facilitate filter manipulation during schema assembly/disassembly. Nevertheless, there is a helper that allows for quickly inserting/removing a filter from the schema.

We will return to this issue later, when the number of filters in our examples exceeds a couple dozen.

In the next article We will assemble a signal level meter schematic and learn to read the measurement result from the filter. We will evaluate the measurement accuracy.

Source: habr.com

Buy reliable website hosting with DDoS protection, VPS VDS servers 🔥 Buy reliable website hosting with DDoS protection, VPS VDS servers | ProHoster