alphacep / vosk-android-demo

Offline speech recognition for Android with Vosk library.
Apache License 2.0
755 stars 207 forks source link

Run into A/libc: Fatal signal 11 (SIGSEGV), code 1 (SEGV_MAPERR) when loading the model #191

Closed ohnowade closed 2 years ago

ohnowade commented 2 years ago

I run into this issue when I call StorageService.unpack in my own application. I had the same structure as this demo, in which there is a separate module called models that contains the model files in its assets, and I had no problem running the demo on my device. Here is the logcat output:

2022-11-11 00:51:00.373 3540-3616/com.example.masgserver V/StorageService: Making directory /storage/emulated/0/Android/data/com.example.masgserver/files/model/model-en-us
2022-11-11 00:51:00.374 3540-3616/com.example.masgserver V/StorageService: Copy model-en-us/README to /storage/emulated/0/Android/data/com.example.masgserver/files/model
2022-11-11 00:51:00.376 3540-3616/com.example.masgserver V/StorageService: Making directory /storage/emulated/0/Android/data/com.example.masgserver/files/model/model-en-us/am
2022-11-11 00:51:00.376 3540-3616/com.example.masgserver V/StorageService: Copy model-en-us/am/final.mdl to /storage/emulated/0/Android/data/com.example.masgserver/files/model
2022-11-11 00:51:00.399 3540-3540/com.example.masgserver A/libc: Fatal signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x804 in tid 3540 (mple.masgserver), pid 3540 (mple.masgserver)
2022-11-11 00:51:00.606 3540-3616/com.example.masgserver V/StorageService: Making directory /storage/emulated/0/Android/data/com.example.masgserver/files/model/model-en-us/conf
2022-11-11 00:51:00.607 3540-3616/com.example.masgserver V/StorageService: Copy model-en-us/conf/mfcc.conf to /storage/emulated/0/Android/data/com.example.masgserver/files/model
2022-11-11 00:51:00.608 3540-3616/com.example.masgserver V/StorageService: Copy model-en-us/conf/model.conf to /storage/emulated/0/Android/data/com.example.masgserver/files/model
2022-11-11 00:51:00.610 3540-3616/com.example.masgserver V/StorageService: Making directory /storage/emulated/0/Android/data/com.example.masgserver/files/model/model-en-us/graph
2022-11-11 00:51:00.610 3540-3616/com.example.masgserver V/StorageService: Copy model-en-us/graph/Gr.fst to /storage/emulated/0/Android/data/com.example.masgserver/files/model

This is the build.gradle file of the application:

plugins {
    id 'com.android.application'
}

android {
    namespace 'com.example.masgserver'
    compileSdk 32

    defaultConfig {
        applicationId "com.example.masgserver"
        minSdk 21
        targetSdk 32
        versionCode 1
        versionName "1.0"
        ndk {
            abiFilters 'armeabi-v7a', 'arm64-v8a', 'x86_64', 'x86'
        }
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

dependencies {

    implementation 'androidx.appcompat:appcompat:1.5.1'
    implementation 'com.google.android.material:material:1.6.1'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    implementation 'androidx.mediarouter:mediarouter:1.3.1'
    implementation 'com.androidplot:androidplot-core:1.5.10'
    implementation 'net.java.dev.jna:jna:5.8.0@aar'
    implementation group: 'com.alphacephei', name: 'vosk-android', version: '0.3.32'
    implementation project(path: ':AniSignCommunication')
    implementation project(path: ':models')
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}

and here is the manifest:

<application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.MASGServer"
        android:usesCleartextTraffic="true"
        tools:targetApi="31" >
        <activity
            android:name=".MainActivity"
            android:exported="true" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

            <meta-data
                android:name="android.app.lib_name"
                android:value="" />
        </activity>

The manifest of the models module is copied from the demo. I'd really appreciate if someone has any idea on what causes this C error and how to solve it. Thank you!

nshmyrev commented 2 years ago

If you write your code it seems you are trying to load incomplete model from the device while the model is being copied. You need to wait till the model is unpacked

ohnowade commented 2 years ago

Thanks a lot! That seemed to be the problem. I should have created the Recognizer either in the callback of unpack or using any other way to make sure that the model is unpacked.