Exploring the Mediastreamer2 VoIP engine. Part 3

The material of the article is taken from my zen channel.

Exploring the Mediastreamer2 VoIP engine. Part 3

Improving the Tone Generator Example

In the previous article we wrote a tone generator application and used it to extract sound from a computer speaker. Now we'll notice that our program doesn't put the memory back on the heap when it finishes running. The time has come to clarify this issue.

After we no longer need the schema, freeing memory should begin by stopping the data pipeline. To do this, you need to disconnect the clock source from the circuit, the ticker using the function ms_ticker_detach(). In our case, we have to disable the ticker from the filter input void source:

ms_ticker_detach(ticker, voidsource)

By the way, after stopping the conveyor, we can change its scheme and put it back into operation by connecting the ticker again.

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

ms_ticker_destroy(ticker)

The conveyor is stopped and we can start dismantling parts of it, separating the filters. For this, the function is used ms_filter_unlink():

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

the assignment of arguments is the same as that of a function ms_filter_link().

We remove, now disjointed, 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 get the correct, from the point of view of memory management, program termination.

As we can see, the correct termination of the program required us to add about the same number of lines of code as at its beginning, with an average of four lines of code for each filter. It turns out that the size of the program code will grow in proportion to the number of filters used in the project. If we talk about a thousand filters in the scheme, then four thousand lines of routine operations for creating and destroying them will be added to your code.

Now you know how to properly terminate a program that uses a media streamer. In the following examples, for the sake of brevity, I will "forget" doing this. But you won't forget?

The media streamer developers did not provide software tools to facilitate filter manipulations when assembling/disassembling circuits. Nevertheless, there is a helper that allows you to quickly insert / remove the filter from the circuit.

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

Next article we will assemble a signal level meter circuit and learn how to read the measurement result from the filter. Let us estimate the measurement accuracy.

Source: habr.com

Add a comment