]> sjero.net Git - linphone/blob - linphone/mediastreamer2/help/ht0-buildagraph.dox
9acc3b0e58bc82caee2310c8d5b65b467e740097
[linphone] / linphone / mediastreamer2 / help / ht0-buildagraph.dox
1 /**
2  * @defgroup howto0_samplegraph Howto 1: build a sample audio graph.
3  * @ingroup mediastreamer2
4
5 <H1>Initialize mediastreamer2</H1>
6
7 When using mediastreamer2, your first task is to initialize the
8 library:
9
10 <PRE>
11         ##include <mediastreamer2/mscommon.h>
12
13         int i;
14
15         i=ms_init();
16         if (i!=0)
17           return -1;
18
19 </PRE>
20
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.
24
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.
29
30 <H1>Graph sample</H1>
31
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
34 simple:
35
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.
38
39 <PRE>
40              AUDIO    ->    ENCODER   ->   RTP
41             CAPTURE   ->              ->  SENDER
42 </PRE>
43
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.
46
47 <PRE>
48         RTP      ->    DECODER   ->   DTMF       ->   AUDIO
49        RECEIVER  ->              ->  GENERATION  ->  PLAYBACK
50 </PRE>
51
52 <H1>Code to initiate the filters of the Graph sample</H1>
53
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
57 with oRTP.
58
59 <PRE>
60         MSSndCard *sndcard;
61         sndcard=ms_snd_card_manager_get_default_card(ms_snd_card_manager_get());
62
63         /* audio capture filter */
64         MSFilter *soundread=ms_snd_card_create_reader(captcard);
65         MSFilter *soundwrite=ms_snd_card_create_writer(playcard);
66
67         MSFilter *encoder=ms_filter_create_encoder("PCMU");
68         MSFilter *decoder=ms_filter_create_decoder("PCMU");
69
70         MSFilter *rtpsend=ms_filter_new(MS_RTP_SEND_ID);
71         MSFilter *rtprecv=ms_filter_new(MS_RTP_RECV_ID);
72
73         RtpSession *rtp_session = *** your_ortp_session *** ;
74
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);
77
78         MSFilter *dtmfgen=ms_filter_new(MS_DTMF_GEN_ID);
79 </PRE>
80
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
83 cards' filters:
84
85 <PRE>
86         int sr = 8000;
87         int chan=1;
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);
92
93         ms_filter_call_method(soundwrite,MS_FILTER_SET_NCHANNELS, &chan);
94
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);
100 </PRE>
101
102
103 <H1>Code to link the filters and run the graph sample</H1>
104
105 <PRE>
106         ms_filter_link(stream->soundread,0,stream->encoder,0);
107         ms_filter_link(stream->encoder,0,stream->rtpsend,0);
108
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); 
112 </PRE>
113
114 Then you need to 'attach' the filters to a ticker. A ticker is a graph
115 manager responsible for running filters.
116
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)
120
121 <PRE>
122         /* create ticker */
123         MSTicker *ticker=ms_ticker_new();
124
125         ms_ticker_attach(ticker,soundread);
126         ms_ticker_attach(ticker,rtprecv);
127 </PRE>
128
129 <H1>Code to unlink the filters and stop the graph sample</H1>
130
131 <PRE>
132         ms_ticker_detach(ticker,soundread);
133         ms_ticker_detach(ticker,rtprecv);
134
135         ms_filter_unlink(stream->soundread,0,stream->encoder,0);
136         ms_filter_unlink(stream->encoder,0,stream->rtpsend,0);
137
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);
141
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);
151 </PRE>
152
153 */