2 * @defgroup howto0_samplegraph Howto 1: build a sample audio graph.
3 * @ingroup mediastreamer2
5 <H1>Initialize mediastreamer2</H1>
7 When using mediastreamer2, your first task is to initialize the
11 ##include <mediastreamer2/mscommon.h>
21 Mediastreamer2 provides internal components which are called filters. Those
22 filters must be linked together so that OUTPUT from one filter is sent to
23 INPUT of the other filters.
25 Usually, filters are used for processing audio or video data. They could
26 capture data, play/draw data, encode/decode data, mix data (conference),
27 transform data (echo canceller). One of the most important filter is the
28 RTP filters that are able to send and receive RTP data.
32 If you are using mediastreamer2, you probably want to do Voice Over IP
33 and get a graph providing a 2 way communication. This 2 graphs are very
36 This first graph shows the filters needed to capture data from a sound
37 card, encode them and send it through an RTP session.
40 AUDIO -> ENCODER -> RTP
44 This second graph shows the filters needed to receive data from an RTP
45 session decode it and send it to the playback device.
48 RTP -> DECODER -> DTMF -> AUDIO
49 RECEIVER -> -> GENERATION -> PLAYBACK
52 <H1>Code to initiate the filters of the Graph sample</H1>
54 Note that the NULL/error checks are not done for better reading.
55 To build the graph, you'll need some information: you need to
56 select the sound card and of course have an RTP session created
61 sndcard=ms_snd_card_manager_get_default_card(ms_snd_card_manager_get());
63 /* audio capture filter */
64 MSFilter *soundread=ms_snd_card_create_reader(captcard);
65 MSFilter *soundwrite=ms_snd_card_create_writer(playcard);
67 MSFilter *encoder=ms_filter_create_encoder("PCMU");
68 MSFilter *decoder=ms_filter_create_decoder("PCMU");
70 MSFilter *rtpsend=ms_filter_new(MS_RTP_SEND_ID);
71 MSFilter *rtprecv=ms_filter_new(MS_RTP_RECV_ID);
73 RtpSession *rtp_session = *** your_ortp_session *** ;
75 ms_filter_call_method(rtpsend,MS_RTP_SEND_SET_SESSION,rtp_session);
76 ms_filter_call_method(rtprecv,MS_RTP_RECV_SET_SESSION,rtp_session);
78 MSFilter *dtmfgen=ms_filter_new(MS_DTMF_GEN_ID);
81 In most cases, the above graph is not enough: you'll need to configure
82 filter's options. As an example, you need to set sampling rate of sound
88 ms_filter_call_method(soundread,MS_FILTER_SET_SAMPLE_RATE,&sr);
89 ms_filter_call_method(soundwrite,MS_FILTER_SET_SAMPLE_RATE,&sr);
90 ms_filter_call_method(stream->encoder,MS_FILTER_SET_SAMPLE_RATE,&sr);
91 ms_filter_call_method(stream->decoder,MS_FILTER_SET_SAMPLE_RATE,&sr);
93 ms_filter_call_method(soundwrite,MS_FILTER_SET_NCHANNELS, &chan);
95 /* if you have some fmtp parameters (from SDP for example!)
96 char *fmtp1 = ** get your fmtp line **;
97 char *fmtp2 = ** get your fmtp line **;
98 ms_filter_call_method(stream->encoder,MS_FILTER_ADD_FMTP, (void*)fmtp1);
99 ms_filter_call_method(stream->decoder,MS_FILTER_ADD_FMTP,(void*)fmtp2);
103 <H1>Code to link the filters and run the graph sample</H1>
106 ms_filter_link(stream->soundread,0,stream->encoder,0);
107 ms_filter_link(stream->encoder,0,stream->rtpsend,0);
109 ms_filter_link(stream->rtprecv,0,stream->decoder,0);
110 ms_filter_link(stream->decoder,0,stream->dtmfgen,0);
111 ms_filter_link(stream->dtmfgen,0,stream->soundwrite,0);
114 Then you need to 'attach' the filters to a ticker. A ticker is a graph
115 manager responsible for running filters.
117 In the above case, there is 2 independant graph within the ticker: you
118 need to attach the first element of each graph (the one that does not
119 contains any INPUT pins)
123 MSTicker *ticker=ms_ticker_new();
125 ms_ticker_attach(ticker,soundread);
126 ms_ticker_attach(ticker,rtprecv);
129 <H1>Code to unlink the filters and stop the graph sample</H1>
132 ms_ticker_detach(ticker,soundread);
133 ms_ticker_detach(ticker,rtprecv);
135 ms_filter_unlink(stream->soundread,0,stream->encoder,0);
136 ms_filter_unlink(stream->encoder,0,stream->rtpsend,0);
138 ms_filter_unlink(stream->rtprecv,0,stream->decoder,0);
139 ms_filter_unlink(stream->decoder,0,stream->dtmfgen,0);
140 ms_filter_unlink(stream->dtmfgen,0,stream->soundwrite,0);
142 if (rtp_session!=NULL) rtp_session_destroy(rtp_session);
143 if (rtpsend!=NULL) ms_filter_destroy(rtpsend);
144 if (rtprecv!=NULL) ms_filter_destroy(rtprecv);
145 if (soundread!=NULL) ms_filter_destroy(soundread);
146 if (soundwrite!=NULL) ms_filter_destroy(soundwrite);
147 if (encoder!=NULL) ms_filter_destroy(encoder);
148 if (decoder!=NULL) ms_filter_destroy(decoder);
149 if (dtmfgen!=NULL) ms_filter_destroy(dtmfgen);
150 if (ticker!=NULL) ms_ticker_destroy(ticker);