现在的位置: 首页 > 综合 > 正文

mediastreamer2学习之—-创建一个示例音频graph

2013年01月08日 ⁄ 综合 ⁄ 共 4128字 ⁄ 字号 评论关闭


 

Initialize mediastreamer2  (初始化一个mediastreamer2)

When using mediastreamer2, your first task is to initialize the library:

	#include <mediastreamer2/mscommon.h>

	int i;

	i=ms_init();
	if (i!=0)
	  return -1;

Mediastreamer2 provides internal components which are called filters. Those filters must be linked together so that OUTPUT from one filter is sent to INPUT of the other filters.

Usually, filters are used for processing audio or video data. They could capture data, play/draw data, encode/decode data, mix data (conference), transform data (echo canceller). One of the most important filter is the RTP filters that are able to send and
receive RTP data.

Graph sample

If you are using mediastreamer2, you probably want to do Voice Over IP and get a graph providing a 2 way communication. This 2 graphs are very simple:

This first graph shows the filters needed to capture data from a sound card, encode them and send it through an RTP session.

这个Graph需要这么几个filter:从声卡捕捉音频数据的filter、为它们编码的filter,通过一个RTP会话发送这些编码后的音频

             AUDIO    ->    ENCODER   ->   RTP
            CAPTURE   ->              ->  SENDER

This second graph shows the filters needed to receive data from an RTP session decode it and send it to the playback device.

从RTP会话中接收音频数据,解码,发送给播放设备进行播放

        RTP      ->    DECODER   ->   DTMF       ->   AUDIO
       RECEIVER  ->              ->  GENERATION  ->  PLAYBACK

Code to initiate the filters of the Graph sample

Note that the NULL/error checks are not done for better reading. To build the graph, you'll need some information: you need to select the sound card and of course have an RTP session created with oRTP.

	MSSndCard *sndcard;
	sndcard=ms_snd_card_manager_get_default_card(ms_snd_card_manager_get());   //获取一个声卡

        /* audio capture filter */
	MSFilter *soundread=ms_snd_card_create_reader(captcard);
	MSFilter *soundwrite=ms_snd_card_create_writer(playcard);

	MSFilter *encoder=ms_filter_create_encoder("PCMU");
	MSFilter *decoder=ms_filter_create_decoder("PCMU");

	MSFilter *rtpsend=ms_filter_new(MS_RTP_SEND_ID);
	MSFilter *rtprecv=ms_filter_new(MS_RTP_RECV_ID);

	RtpSession *rtp_session = *** your_ortp_session *** ;

	ms_filter_call_method(rtpsend,MS_RTP_SEND_SET_SESSION,rtp_session);
	ms_filter_call_method(rtprecv,MS_RTP_RECV_SET_SESSION,rtp_session);

	MSFilter *dtmfgen=ms_filter_new(MS_DTMF_GEN_ID);

In most cases, the above graph is not enough: you'll need to configure filter's options. As an example, you need to set sampling rate of sound cards' filters:

        int sr = 8000;
	int chan=1;
	ms_filter_call_method(soundread,MS_FILTER_SET_SAMPLE_RATE,&sr);
	ms_filter_call_method(soundwrite,MS_FILTER_SET_SAMPLE_RATE,&sr);
	ms_filter_call_method(stream->encoder,MS_FILTER_SET_SAMPLE_RATE,&sr);
	ms_filter_call_method(stream->decoder,MS_FILTER_SET_SAMPLE_RATE,&sr);

	ms_filter_call_method(soundwrite,MS_FILTER_SET_NCHANNELS, &chan);

        /* if you have some fmtp parameters (from SDP for example!)
        char *fmtp1 = ** get your fmtp line **;
        char *fmtp2 = ** get your fmtp line **;
	ms_filter_call_method(stream->encoder,MS_FILTER_ADD_FMTP, (void*)fmtp1);
	ms_filter_call_method(stream->decoder,MS_FILTER_ADD_FMTP,(void*)fmtp2);

Code to link the filters and run the graph sample

	ms_filter_link(stream->soundread,0,stream->encoder,0);
	ms_filter_link(stream->encoder,0,stream->rtpsend,0);

	ms_filter_link(stream->rtprecv,0,stream->decoder,0);
	ms_filter_link(stream->decoder,0,stream->dtmfgen,0);
	ms_filter_link(stream->dtmfgen,0,stream->soundwrite,0);	

Then you need to 'attach' the filters to a ticker. A ticker is a graph manager responsible for running filters.

In the above case, there is 2 independant graph within the ticker: you need to attach the first element of each graph (the one that does not contains any INPUT pins)

	/* create ticker */
	MSTicker *ticker=ms_ticker_new();

	ms_ticker_attach(ticker,soundread);
	ms_ticker_attach(ticker,rtprecv);

Code to unlink the filters and stop the graph sample

	ms_ticker_detach(ticker,soundread);
	ms_ticker_detach(ticker,rtprecv);

	ms_filter_unlink(stream->soundread,0,stream->encoder,0);
	ms_filter_unlink(stream->encoder,0,stream->rtpsend,0);

	ms_filter_unlink(stream->rtprecv,0,stream->decoder,0);
	ms_filter_unlink(stream->decoder,0,stream->dtmfgen,0);
	ms_filter_unlink(stream->dtmfgen,0,stream->soundwrite,0);

	if (rtp_session!=NULL) rtp_session_destroy(rtp_session);
	if (rtpsend!=NULL) ms_filter_destroy(rtpsend);
	if (rtprecv!=NULL) ms_filter_destroy(rtprecv);
	if (soundread!=NULL) ms_filter_destroy(soundread);
	if (soundwrite!=NULL) ms_filter_destroy(soundwrite);
	if (encoder!=NULL) ms_filter_destroy(encoder);
	if (decoder!=NULL) ms_filter_destroy(decoder);
	if (dtmfgen!=NULL) ms_filter_destroy(dtmfgen);
	if (ticker!=NULL) ms_ticker_destroy(ticker);

抱歉!评论已关闭.