ring_jni.c 3.32 KB
Newer Older
jehan's avatar
jehan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
mediastreamer2 library - modular sound and video processing and streaming
Copyright (C) 2006  Simon MORLAT (simon.morlat@linphone.org)

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
17
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
jehan's avatar
jehan committed
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
*/

#ifdef HAVE_CONFIG_H
#include "mediastreamer-config.h"
#endif

#include "mediastreamer2/mediastream.h"
#include <jni.h>

extern void ms_andsnd_register_card(JavaVM *jvm);

void Java_org_mediastreamer2_test_Ring_init(JNIEnv*  env,jobject  this){
	ortp_init();
	ortp_set_log_level_mask(ORTP_MESSAGE|ORTP_WARNING|ORTP_ERROR|ORTP_FATAL);
	ms_init();
}
void Java_org_mediastreamer2_test_Ring_play(JNIEnv*  env,jobject  this,jstring jfile){
	RingStream *r;
	const char *file;
	MSSndCard *sc;
	const char * card_id=NULL;

	file = (*env)->GetStringUTFChars(env, jfile, NULL);
	sc=ms_snd_card_manager_get_card(ms_snd_card_manager_get(),card_id);
	r=ring_start(file,2000,sc);
	ms_sleep(2);
	ring_stop(r);
    (*env)->ReleaseStringUTFChars(env, jfile, file);

	return ;
}


void Java_org_mediastreamer2_test_Ring_echo(JNIEnv*  env,jobject  this,jint freq){
    MSFilter *reader,*writer;
    MSFilter *resampler=0;
    MSSndCard *card_capture;
    MSSndCard *card_playback;
    MSTicker *ticker;
    int read_rate = freq;
    int write_rate = freq;
	const char * card_id="ANDROID SND: Android Sound card";
	card_capture = ms_snd_card_manager_get_card(ms_snd_card_manager_get(),card_id);
	card_playback = ms_snd_card_manager_get_card(ms_snd_card_manager_get(),card_id);
	if (card_playback==NULL || card_capture==NULL){
		ms_error("No card.");
		return ;
	}
	reader=ms_snd_card_create_reader(card_capture);
	writer=ms_snd_card_create_writer(card_playback);

	if (read_rate !=write_rate) {
70
		resampler = ms_fcatory_create_filter(ms_snd_card_get_factory(card_playback),MS_RESAMPLE_ID);
jehan's avatar
jehan committed
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
		ms_filter_call_method(resampler,MS_FILTER_SET_SAMPLE_RATE,&read_rate);
		ms_filter_call_method(resampler,MS_FILTER_SET_OUTPUT_SAMPLE_RATE,&write_rate);
	}
	ms_filter_call_method (reader, MS_FILTER_SET_SAMPLE_RATE,
			&read_rate);
	ms_filter_call_method (writer, MS_FILTER_SET_SAMPLE_RATE,
			&write_rate);

	ticker=ms_ticker_new();
	if (resampler) {
		ms_filter_link(reader,0,resampler,0);
		ms_filter_link(resampler,0,writer,0);
	} else {
		ms_filter_link(reader,0,writer,0);
	}
	ms_ticker_attach(ticker,reader);
	while(1)
		ms_sleep(1);
	ms_ticker_detach(ticker,reader);
	ms_ticker_destroy(ticker);
	if (resampler) {
		ms_filter_unlink(reader,0,resampler,0);
		ms_filter_unlink(resampler,0,writer,0);
		ms_filter_destroy(resampler);
	} else {
		ms_filter_unlink(reader,0,writer,0);
	}
	ms_filter_destroy(reader);
	ms_filter_destroy(writer);

}

JNIEXPORT jint JNICALL  JNI_OnLoad(JavaVM *ajvm, void *reserved)
{
105
#ifdef __ANDROID__
jehan's avatar
jehan committed
106
	ms_set_jvm(ajvm);
107
#endif /* __ANDROID__ */
jehan's avatar
jehan committed
108 109
	return JNI_VERSION_1_2;
}