Sound shutters - buffer mismatch in android jni dev - armv7 cortex m5

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Well there are some news.

The returned minimum buffer size is always constant 640 Bytes!

One of PJSIPs endpoint is the android jni device (according to https://trac.pjsip.org/repos/wiki/media-flow ). 
This device holds a record stream. 
To open the stream it's necessary to get the minimum buffer size from androids jni layer. This is done by

...
inputBuffSizeRec = (*jni_env)->CallStaticIntMethod(jni_env,
                                                           stream->record_class,
                                                           bufsize_method,
                                                           param->clock_rate,
                                                           channelInCfg,
                                                           sampleFormat);
...

BUT android returns always 640 bytes.

This will be a big problem for the functionality of the jni dev since the AndroidRecorderCallback expects a exactly amount of read bytes. Those amount of bytes must match
the requested amount of bytes.
The requested amount of bytes will be calculated in the android_create_stream(...) function by the following code sniped:

...
buffSize = stream->param.samples_per_frame*stream->param.bits_per_sample/8;
stream->rec_buf_size = stream->play_buf_size = buffSize;
...

In my case this size is 1764 bytes.

The read back amount of bytes will be 640, because the stream->record is initialized with a 640 bytes buffer.

So if the AndroidRecorderCallback will be called it never copies any bytes!


======================================================================
    while (!stream->quit_flag) {
        pjmedia_frame frame;
        pj_status_t status;
        int bytesRead;
        
        if (!stream->running) {
            (*jni_env)->CallVoidMethod(jni_env, stream->record, stop_method);
            pj_sem_wait(stream->rec_sem);
            if (stream->quit_flag)
                break;
            (*jni_env)->CallVoidMethod(jni_env, stream->record, record_method);
        }
        
        bytesRead = (*jni_env)->CallIntMethod(jni_env, stream->record,
                                              read_method, inputBuffer,
                                              0, size);
        if (bytesRead <= 0 || bytesRead != size) {                                                                                                                                                                                              <--------------========== There is the problem
            PJ_LOG (4, (THIS_FILE, "Record thread : error %d reading data Bytes read: (%d), size(rec_buf_size): (%d)",bytesRead, size));           
            continue;
        }

        buf = (*jni_env)->GetByteArrayElements(jni_env, inputBuffer, 0);
        frame.type = PJMEDIA_FRAME_TYPE_AUDIO;
        frame.size =  size;
        frame.bit_info = 0;
        frame.buf = (void *)buf;
        frame.timestamp.u64 = stream->rec_timestamp.u64;

        status = (*stream->rec_cb)(stream->user_data, &frame);
        (*jni_env)->ReleaseByteArrayElements(jni_env, inputBuffer, buf,
        				     JNI_ABORT);

        stream->rec_timestamp.u64 += stream->param.samples_per_frame /
                                     stream->param.channel_count;
    }
======================================================================

Now I figured out that the audio hal will statically return an integer value of 320. This value will be doubled by the framework and leads to the 640 Bytes.

What can I do to fix this Problem?


Ralf Zeuka

Callom GmbH
Neue Stra?e 97-99
89073 Ulm

Tel.: +49 731 140015 0
Fax: +49 731 140015 15

eMail:? ralf.zeuka at callom.com
www.callom.com 
Gesch?ftsf?hrer:
Marcus Brachert, Timo Weith?ner - Registergericht Ulm HRB 72968

> -----Urspr?ngliche Nachricht-----
> Von: pjsip [mailto:pjsip-bounces at lists.pjsip.org] Im Auftrag von Ralf Zeuka |
> Callom GmbH
> Gesendet: Freitag, 25. September 2015 17:25
> An: pjsip list <pjsip at lists.pjsip.org>
> Betreff: [pjsip] Sound shutters - buffer mismatch in android jni dev - armv7
> cortex m5
> 
> Hi there,
> 
> I'am using PJSIP on an arm-cortex M5 architecture. (C1 from Hardkernel) But
> I've some trouble with the sound.
> 
> System: 	Android 4.4.2 on ARMv7 Cortex-M5
> PJSIP:		Version 2.3
> 
> I'd like to use the android_jni_dev and the libpjsua.so .
> When I start an outgoing call I get frequently errors via logcat out from the
> android_jni_dev.c .
> 
>     "D/PJSUA Native( 1559): 17:55:02.479 android_jni_de  Record thread : error
> 640 reading data"
> 
> According to this error I expanded the corresponding log message.
> 
> =======================================================
> 	...
>         bytesRead = (*jni_env)->CallIntMethod(jni_env, stream->record,
> 					 read_method, inputBuffer,
> 					  0, size);
> 
>         if (bytesRead <= 0 || bytesRead != size) {
>             	PJ_LOG (4, (THIS_FILE, "Record thread : error %d reading
> 			data Bytes read: (%d), size(rec_buf_size): (%d)",
> 			bytesRead, bytesRead, (int) size));
>             	continue;
>         }
> 	...
> =======================================================
> Code: android_jni_dev::AndroidRecorderCallback(...)
> 
> So I could see a buffer mismatch:
> 
> 	D/PJSUA Native( 1559): 17:55:02.525 android_jni_de  Record thread :
> error 640 reading data Bytes read: (640), size(rec_buf_size): (1764)
> 
> A fast but bad fix for this would be to set the 'inputBuffSizeRec' to 1764
> within the following piece of code:
> ==============================================
> ...
> 	stream->record =  (*jni_env)->NewObject(jni_env,
> 					stream->record_class,
> 					constructor_method,
> 					mic_source,
> 					param->clock_rate,
> 					channelInCfg,
> 					sampleFormat,
> 					inputBuffSizeRec);   // ==> 1764
> ...
> ==============================================
> Code: android_jni_dev::android_create_stream(...)
> 
> The right size should be calculated by:
> ==========================================================
> =====================
> ...
> 	inputBuffSizeRec = (*jni_env)->CallStaticIntMethod(jni_env,
> 				   	stream->record_class,
> 				   	bufsize_method,
> 				   	param->clock_rate,
> 				   	channelInCfg,
> 				   	sampleFormat);
> 
> ...
> ==========================================================
> =====================
> Code: android_jni_dev::android_create_stream(...)
> 
> The 'size' var is also calculated near this code by the following:
> ==========================================================
> ========
> ...
> 	buffSize = stream->param.samples_per_frame*stream-
> >param.bits_per_sample/8;
> 		stream->rec_buf_size = stream->play_buf_size = buffSize; ...
> ==========================================================
> ========
> Code: android_jni_dev::android_create_stream(...)
> 
> 
> After setting the 'inputBuffSizeRec' to the fix value 1764 the error message
> doesn't appear anymore.
> 
> But now the sound shutters' from time to time.
> AudioFlinger has some overflows and PJSUA has some underflows.
> Furthermore messages with 'buffer adjustments' will be printed out.
> 
> I measured the output signal with an oscilloscope and printed the pcm
> stream within the 'conference.c' to a file.
> 
> Both show me the same:
> 
> During the shutter phase the same signal will be repeated. So I think if this
> happens, pjsua will send the same frame again and again.
> 
> 
> I'am wondering why the buffer size will not be calculated correctly?
> Now I need some help to figure out what's going wrong.
> 
> Regards,
> 
> 
> Ralf Zeuka
> 
> Callom GmbH
> Neue Stra?e 97-99
> 89073 Ulm
> 
> Tel.: +49 731 140015 0
> Fax: +49 731 140015 15
> 
> eMail:? ralf.zeuka at callom.com
> www.callom.com
> Gesch?ftsf?hrer:
> Marcus Brachert, Timo Weith?ner - Registergericht Ulm HRB 72968
> 
> _______________________________________________
> Visit our blog: http://blog.pjsip.org
> 
> pjsip mailing list
> pjsip at lists.pjsip.org
> http://lists.pjsip.org/mailman/listinfo/pjsip_lists.pjsip.org


[Index of Archives]     [Asterisk Users]     [Asterisk App Development]     [Linux ARM Kernel]     [Linux ARM]     [Linux Omap]     [Fedora ARM]     [IETF Annouce]     [Security]     [Bugtraq]     [Linux]     [Linux OMAP]     [Linux MIPS]     [Linux API]
  Powered by Linux