Commit af119e5c authored by Simon Morlat's avatar Simon Morlat
Browse files

android audio refinements

parent 93ef451f
......@@ -118,6 +118,11 @@ LOCAL_CFLAGS += \
-D_POSIX_SOURCE
ifeq ($(TARGET_ARCH_ABI),armeabi-v7a)
LOCAL_CFLAGS += -DUSE_HARDWARE_RATE=1
endif
#LOCAL_CFLAGS += -DDEBUG
LOCAL_C_INCLUDES += \
......
......@@ -80,6 +80,7 @@ struct _RingStream
MSTicker *ticker;
MSFilter *source;
MSFilter *gendtmf;
MSFilter *write_resampler;
MSFilter *sndwrite;
};
......
......@@ -588,6 +588,9 @@ RingStream * ring_start_with_cb(const char *file,int interval,MSSndCard *sndcard
{
RingStream *stream;
int tmp;
int srcrate,dstrate;
MSConnectionHelper h;
stream=(RingStream *)ms_new0(RingStream,1);
stream->source=ms_filter_new(MS_FILE_PLAYER_ID);
if (file)
......@@ -601,16 +604,28 @@ RingStream * ring_start_with_cb(const char *file,int interval,MSSndCard *sndcard
stream->sndwrite=ms_snd_card_create_writer(sndcard);
ms_filter_call_method(stream->source,MS_FILTER_GET_SAMPLE_RATE,&tmp);
ms_filter_call_method(stream->gendtmf,MS_FILTER_SET_SAMPLE_RATE,&tmp);
ms_filter_call_method(stream->sndwrite,MS_FILTER_SET_SAMPLE_RATE,&tmp);
ms_filter_call_method(stream->source,MS_FILTER_GET_SAMPLE_RATE,&srcrate);
ms_filter_call_method(stream->gendtmf,MS_FILTER_SET_SAMPLE_RATE,&srcrate);
ms_filter_call_method(stream->sndwrite,MS_FILTER_SET_SAMPLE_RATE,&srcrate);
ms_filter_call_method(stream->sndwrite,MS_FILTER_GET_SAMPLE_RATE,&dstrate);
if (srcrate!=dstrate){
stream->write_resampler=ms_filter_new(MS_RESAMPLE_ID);
ms_filter_call_method(stream->write_resampler,MS_FILTER_SET_SAMPLE_RATE,&srcrate);
ms_filter_call_method(stream->write_resampler,MS_FILTER_SET_OUTPUT_SAMPLE_RATE,&dstrate);
ms_message("configuring resampler from rate[%i] to rate [%i]", srcrate,dstrate);
}
ms_filter_call_method(stream->source,MS_FILTER_GET_NCHANNELS,&tmp);
ms_filter_call_method(stream->gendtmf,MS_FILTER_SET_NCHANNELS,&tmp);
ms_filter_call_method(stream->sndwrite,MS_FILTER_SET_NCHANNELS,&tmp);
stream->ticker=ms_ticker_new();
ms_ticker_set_name(stream->ticker,"Audio (ring) MSTicker");
ms_filter_link(stream->source,0,stream->gendtmf,0);
ms_filter_link(stream->gendtmf,0,stream->sndwrite,0);
ms_connection_helper_start(&h);
ms_connection_helper_link(&h,stream->source,-1,0);
ms_connection_helper_link(&h,stream->gendtmf,0,0);
if (stream->write_resampler)
ms_connection_helper_link(&h,stream->write_resampler,0,0);
ms_connection_helper_link(&h,stream->sndwrite,0,-1);
ms_ticker_attach(stream->ticker,stream->source);
return stream;
}
......@@ -626,9 +641,16 @@ void ring_stop_dtmf(RingStream *stream){
}
void ring_stop(RingStream *stream){
MSConnectionHelper h;
ms_ticker_detach(stream->ticker,stream->source);
ms_filter_unlink(stream->source,0,stream->gendtmf,0);
ms_filter_unlink(stream->gendtmf,0,stream->sndwrite,0);
ms_connection_helper_start(&h);
ms_connection_helper_unlink(&h,stream->source,-1,0);
ms_connection_helper_unlink(&h,stream->gendtmf,0,0);
if (stream->write_resampler)
ms_connection_helper_unlink(&h,stream->write_resampler,0,0);
ms_connection_helper_unlink(&h,stream->sndwrite,0,-1);
ms_ticker_destroy(stream->ticker);
ms_filter_destroy(stream->source);
ms_filter_destroy(stream->gendtmf);
......
......@@ -416,18 +416,17 @@ MS_FILTER_DESC_EXPORT(msandroid_sound_read_desc)
/***********************************write filter********************/
static int set_write_rate(MSFilter *f, void *arg){
msandroid_sound_data *d=(msandroid_sound_data*)f->data;
#ifndef USE_HARDWARE_RATE
int proposed_rate = *((int*)arg);
ms_debug("set_rate %d",proposed_rate);
msandroid_sound_data *d=(msandroid_sound_data*)f->data;
d->rate=proposed_rate;
return 0;
/*d->rate=44100; //to improve latency on msn 7k
if (proposed_rate == d->rate) {
return 0;
} else {
return d->rate;
}
*/
#else
/*audioflingler resampling is really bad
we prefer do resampling by ourselves if cpu allows it*/
return -1;
#endif
}
MSFilterMethod msandroid_sound_write_methods[]={
......@@ -441,15 +440,40 @@ MSFilterMethod msandroid_sound_write_methods[]={
class msandroid_sound_write_data : public msandroid_sound_data{
public:
msandroid_sound_write_data() :audio_track_class(0),audio_track(0),write_chunk_size(0),writtenBytes(0),last_sample_date(0){
JNIEnv *jni_env=NULL;
bufferizer = ms_bufferizer_new();
ms_cond_init(&cond,0);
if (jvm->AttachCurrentThread(&jni_env,NULL)!=0){
ms_error("msandroid_sound_write_data(): could not attach current thread.");
return;
}
audio_track_class = (jclass)jni_env->NewGlobalRef(jni_env->FindClass("android/media/AudioTrack"));
if (audio_track_class == 0) {
ms_error("cannot find android/media/AudioTrack\n");
return;
}
jmethodID hwrate_id = jni_env->GetStaticMethodID(audio_track_class,"getNativeOutputSampleRate", "(I)I");
if (hwrate_id == 0) {
ms_error("cannot find int AudioRecord.getNativeOutputSampleRate(int streamType)");
return;
}
rate = jni_env->CallStaticIntMethod(audio_track_class,hwrate_id,0 /*STREAM_VOICE_CALL*/);
ms_message("Hardware sample rate is %i",rate);
};
~msandroid_sound_write_data() {
JNIEnv *jni_env=NULL;
ms_mutex_lock(&mutex);
ms_bufferizer_flush(bufferizer);
ms_mutex_unlock(&mutex);
ms_bufferizer_destroy(bufferizer);
ms_cond_destroy(&cond);
if (audio_track_class!=0){
if (jvm->AttachCurrentThread(&jni_env,NULL)!=0){
ms_error("~msandroid_sound_write_data(): could not attach current thread.");
return;
}
jni_env->DeleteGlobalRef(audio_track_class);
}
}
jclass audio_track_class;
jobject audio_track;
......@@ -551,9 +575,8 @@ void msandroid_sound_write_preprocess(MSFilter *f){
ms_error("cannot attach VM\n");
goto end;
}
d->audio_track_class = (jclass)jni_env->NewGlobalRef(jni_env->FindClass("android/media/AudioTrack"));
if (d->audio_track_class == 0) {
ms_error("cannot find android/media/AudioTrack\n");
goto end;
}
......@@ -667,7 +690,6 @@ void msandroid_sound_write_postprocess(MSFilter *f){
goto end;
end: {
if (d->audio_track) jni_env->DeleteGlobalRef(d->audio_track);
jni_env->DeleteGlobalRef(d->audio_track_class);
//d->jvm->DetachCurrentThread();
return;
}
......
......@@ -304,7 +304,7 @@ static int enc_add_fmtp(MSFilter *f, void *arg){
s->mode=atoi(buf);
}
else {
s->mode = -1; /* deault mode */
s->mode = -1; /* default mode */
}
return 0;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment