使用 gstreamer 在 android 上录制音频(使用 Qt5)

Posted

技术标签:

【中文标题】使用 gstreamer 在 android 上录制音频(使用 Qt5)【英文标题】:Use gstreamer to record audio on android(with Qt5) 【发布时间】:2019-08-07 17:16:02 【问题描述】:

尝试用gstreamer在android上录制音频(已经启用录音权限),我更喜欢gstreamer而不是java api,因为将来需要添加更多功能,比如 将音频转换为特定格式(S16LE、channels=1、rate=16000 等)、编码为 wav、分离视频/音频等

我使用的管道是

opensles-s-rc ! audioconvert ! wavenc ! fakesink

opensles-s-rc ! audioconvert ! wavenc ! openslessink location=\"%1\"

但是他们都给我错误信息

E IAudioFlinger: createRecord returned error -22
E AudioRecord: AudioFlinger could not create record track, status: -22
E libOpenSLES: android_audioRecorder_realize(0xcea58a00) error creating AudioRecord object; status -22
W libOpenSLES: Leaving Object::Realize (SL_RESULT_CONTENT_UNSUPPORTED)

我的 .pro 文件的一部分

#plugins
LIBS += $$GSTREAMER_ROOT/lib/gstreamer-1.0/libgstcoreelements.a
LIBS += $$GSTREAMER_ROOT/lib/gstreamer-1.0/libgsttypefindfunctions.a
LIBS += $$GSTREAMER_ROOT/lib/gstreamer-1.0/libgstvideoconvert.a
LIBS += $$GSTREAMER_ROOT/lib/gstreamer-1.0/libgstvideorate.a
LIBS += $$GSTREAMER_ROOT/lib/gstreamer-1.0/libgstvideoscale.a
LIBS += $$GSTREAMER_ROOT/lib/gstreamer-1.0/libgstvideofilter.a
LIBS += $$GSTREAMER_ROOT/lib/gstreamer-1.0/libgstautodetect.a
LIBS += $$GSTREAMER_ROOT/lib/gstreamer-1.0/libgstvideotestsrc.a
LIBS += $$GSTREAMER_ROOT/lib/gstreamer-1.0/libgstplayback.a
#LIBS += $$GSTREAMER_ROOT/lib/gstreamer-1.0/libgstsoup.a
LIBS += $$GSTREAMER_ROOT/lib/gstreamer-1.0/libgstx264.a
LIBS += $$GSTREAMER_ROOT/lib/gstreamer-1.0/libgstvideoparsersbad.a
LIBS += $$GSTREAMER_ROOT/lib/gstreamer-1.0/libgstandroidmedia.a
LIBS += $$GSTREAMER_ROOT/lib/gstreamer-1.0/libgstopengl.a

LIBS += $$GSTREAMER_ROOT/lib/gstreamer-1.0/libgstopensles.a
LIBS += $$GSTREAMER_ROOT/lib/gstreamer-1.0/libgstaudioconvert.a
LIBS += $$GSTREAMER_ROOT/lib/gstreamer-1.0/libgstaudiotestsrc.a
LIBS += $$GSTREAMER_ROOT/lib/gstreamer-1.0/libgstautodetect.a
LIBS += $$GSTREAMER_ROOT/lib/gstreamer-1.0/libgstautoconvert.a
LIBS += $$GSTREAMER_ROOT/lib/gstreamer-1.0/libgstaudioresample.a
LIBS += $$GSTREAMER_ROOT/lib/gstreamer-1.0/libgstplayback.a
LIBS += $$GSTREAMER_ROOT/lib/gstreamer-1.0/libgstwavenc.a
LIBS += $$GSTREAMER_ROOT/lib/gstreamer-1.0/libgstvoaacenc.a

LIBS += -lOpenSLES

#LIBS += $$GSTREAMER_ROOT/lib/libavcodec.a


#plugin libs
LIBS += $$GSTREAMER_ROOT/lib/liborc-0.4.a
LIBS += $$GSTREAMER_ROOT/lib/libgstgl-1.0.a
LIBS += $$GSTREAMER_ROOT/lib/libgraphene-1.0.a
LIBS += $$GSTREAMER_ROOT/lib/libpng16.a
LIBS += $$GSTREAMER_ROOT/lib/libjpeg.a
LIBS += $$GSTREAMER_ROOT/lib/libx264.a
LIBS += $$GSTREAMER_ROOT/lib/libgstvideo-1.0.a
#LIBS += $$GSTREAMER_ROOT/lib/libgstbadbase-1.0.a
#LIBS += $$GSTREAMER_ROOT/lib/libgstbadvideo-1.0.a
LIBS += $$GSTREAMER_ROOT/lib/libgstaudio-1.0.a
LIBS += $$GSTREAMER_ROOT/lib/libgstpbutils-1.0.a
LIBS += $$GSTREAMER_ROOT/lib/libgsttag-1.0.a
LIBS += $$GSTREAMER_ROOT/lib/libgstphotography-1.0.a
LIBS += $$GSTREAMER_ROOT/lib/libgstcontroller-1.0.a
LIBS += $$GSTREAMER_ROOT/lib/libgstcodecparsers-1.0.a
LIBS += -lEGL -lGLESv2

#gstreamer libs
LIBS += $$GSTREAMER_ROOT/lib/libgstbase-1.0.a
LIBS += $$GSTREAMER_ROOT/lib/libgstreamer-1.0.a
LIBS += $$GSTREAMER_ROOT/lib/libgmodule-2.0.a
LIBS += $$GSTREAMER_ROOT/lib/libgobject-2.0.a
LIBS += $$GSTREAMER_ROOT/lib/libglib-2.0.a
LIBS += $$GSTREAMER_ROOT/lib/libffi.a
#LIBS += $$GSTREAMER_ROOT/lib/libpcre.a
LIBS += $$GSTREAMER_ROOT/lib/libiconv.a
LIBS += $$GSTREAMER_ROOT/lib/libintl.a
LIBS += $$GSTREAMER_ROOT/lib/libz.a

gstreamer的初始化代码,在main.cpp中

extern "C"


#include <gst/gst.h>

#ifdef Q_OS_ANDROID
GST_PLUGIN_STATIC_DECLARE(coreelements);
GST_PLUGIN_STATIC_DECLARE(typefindfunctions);
GST_PLUGIN_STATIC_DECLARE(videoconvert);
GST_PLUGIN_STATIC_DECLARE(videorate);
GST_PLUGIN_STATIC_DECLARE(videoscale);
GST_PLUGIN_STATIC_DECLARE(videofilter);
GST_PLUGIN_STATIC_DECLARE(autodetect);
GST_PLUGIN_STATIC_DECLARE(playback);
GST_PLUGIN_STATIC_DECLARE(videotestsrc);
GST_PLUGIN_STATIC_DECLARE(playback);
//GST_PLUGIN_STATIC_DECLARE(soup);
GST_PLUGIN_STATIC_DECLARE(x264);
GST_PLUGIN_STATIC_DECLARE(videoparsersbad);
GST_PLUGIN_STATIC_DECLARE(androidmedia);
GST_PLUGIN_STATIC_DECLARE(opengl);
//GST_PLUGIN_STATIC_DECLARE(qmlgl);

GST_PLUGIN_STATIC_DECLARE(opensles);
GST_PLUGIN_STATIC_DECLARE(audioconvert);
GST_PLUGIN_STATIC_DECLARE(audiotestsrc);
GST_PLUGIN_STATIC_DECLARE(autodetect);
GST_PLUGIN_STATIC_DECLARE(autoconvert);
GST_PLUGIN_STATIC_DECLARE(audioresample);
GST_PLUGIN_STATIC_DECLARE(playback);
GST_PLUGIN_STATIC_DECLARE(wavenc);
//GST_PLUGIN_STATIC_DECLARE(voaacenc);

#endif



int main(int argc, char *argv[])

    gst_init (&argc, &argv);

#ifdef Q_OS_ANDROID
    GST_PLUGIN_STATIC_REGISTER(coreelements);
    GST_PLUGIN_STATIC_REGISTER(typefindfunctions);
    GST_PLUGIN_STATIC_REGISTER(videoconvert);
    GST_PLUGIN_STATIC_REGISTER(videorate);
    GST_PLUGIN_STATIC_REGISTER(videoscale);
    GST_PLUGIN_STATIC_REGISTER(videofilter);
    GST_PLUGIN_STATIC_REGISTER(autodetect);
    GST_PLUGIN_STATIC_REGISTER(playback);
    //GST_PLUGIN_STATIC_REGISTER(soup);
    GST_PLUGIN_STATIC_REGISTER(videotestsrc);
    GST_PLUGIN_STATIC_REGISTER(x264);
    GST_PLUGIN_STATIC_REGISTER(videoparsersbad);
    GST_PLUGIN_STATIC_REGISTER(androidmedia);
    GST_PLUGIN_STATIC_REGISTER(opengl);
    //GST_PLUGIN_STATIC_REGISTER(qmlgl);

    GST_PLUGIN_STATIC_REGISTER(opensles);
    GST_PLUGIN_STATIC_REGISTER(audioconvert);
    GST_PLUGIN_STATIC_REGISTER(audiotestsrc);
    GST_PLUGIN_STATIC_REGISTER(autodetect);
    GST_PLUGIN_STATIC_REGISTER(autoconvert);
    GST_PLUGIN_STATIC_REGISTER(audioresample);
    GST_PLUGIN_STATIC_REGISTER(playback);
    GST_PLUGIN_STATIC_REGISTER(wavenc);
    //GST_PLUGIN_STATIC_REGISTER(voaacenc);

    gst_debug_set_active(TRUE);
    gst_debug_set_default_threshold(GST_LEVEL_NONE);
#endif

谢谢

【问题讨论】:

【参考方案1】:

我找到了解决办法,问题是我没有注册合适的插件,正确的插件应该是

GST_PLUGIN_STATIC_DECLARE(coreelements);
GST_PLUGIN_STATIC_DECLARE(coretracers);
GST_PLUGIN_STATIC_DECLARE(adder);
GST_PLUGIN_STATIC_DECLARE(audioconvert);
GST_PLUGIN_STATIC_DECLARE(audiomixer);
GST_PLUGIN_STATIC_DECLARE(audiorate);
GST_PLUGIN_STATIC_DECLARE(audioresample);
GST_PLUGIN_STATIC_DECLARE(audiotestsrc);
GST_PLUGIN_STATIC_DECLARE(typefindfunctions);
GST_PLUGIN_STATIC_DECLARE(videoconvert);
GST_PLUGIN_STATIC_DECLARE(videorate);
GST_PLUGIN_STATIC_DECLARE(videoscale);
GST_PLUGIN_STATIC_DECLARE(videotestsrc);
GST_PLUGIN_STATIC_DECLARE(volume);
GST_PLUGIN_STATIC_DECLARE(autodetect);
GST_PLUGIN_STATIC_DECLARE(videofilter);
GST_PLUGIN_STATIC_DECLARE(playback);
GST_PLUGIN_STATIC_DECLARE(audioparsers);
GST_PLUGIN_STATIC_DECLARE(auparse);
GST_PLUGIN_STATIC_DECLARE(multipart);
GST_PLUGIN_STATIC_DECLARE(wavenc);
GST_PLUGIN_STATIC_DECLARE(wavpack);
GST_PLUGIN_STATIC_DECLARE(wavparse);
GST_PLUGIN_STATIC_DECLARE(androidmedia);
GST_PLUGIN_STATIC_DECLARE(opengl);
GST_PLUGIN_STATIC_DECLARE(opensles);

GST_PLUGIN_STATIC_DECLARE(autoconvert);
GST_PLUGIN_STATIC_DECLARE(amrnb);
GST_PLUGIN_STATIC_DECLARE(amrwbdec);

它们不是插件的最小列表,我还没有时间优化它,我不需要编译 libgstreamer_android.so 以使它们在我的手机上工作,但它们仍然可以工作。

【讨论】:

以上是关于使用 gstreamer 在 android 上录制音频(使用 Qt5)的主要内容,如果未能解决你的问题,请参考以下文章

使用 gstreamer 在 android 上录制音频(使用 Qt5)

Android Studio中的Gstreamer示例

将使用 NDK 和 Gstreamer 的 Eclipse android 项目迁移到 Android Studio

使用 ANativeWindow_lock 返回错误 -22 与 android Gstreamer SDK

GStreamer使用详解

Gstreamer使用说明