Android - 在我自己的项目中包含本机 StageFright 功能 [英] Android - Include native StageFright features in my own project

查看:17
本文介绍了Android - 在我自己的项目中包含本机 StageFright 功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发一个应用程序,需要记录音频、将其编码为 AAC、流式传输并在反向执行相同的操作 - 接收流、解码 AAC 和播放音频.

I am currently developing an application that needs to record audio, encode it as AAC, stream it, and do the same in reverse - receiving stream, decoding AAC and playing audio.

我使用 MediaRecorder 成功录制了 AAC(包装在 MP4 容器中),并成功使用 AudioRecord 类上传音频.但是,我需要能够在流式传输音频时对其进行编码,但这些类似乎都无法帮助我做到这一点.

I successfully recorded AAC (wrapped in a MP4 container) using the MediaRecorder, and successfully up-streamed audio using the AudioRecord class. But, I need to be able to encode the audio as I stream it, but none of these classes seem to help me do that.

我研究了一下,发现大多数遇到这个问题的人最终都使用了像 ffmpeg 这样的原生库.

I researched a bit, and found that most people that have this problem end up using a native library like ffmpeg.

但我想知道,由于 Android 已经包含 StageFright,它具有可以进行编码和解码的本机代码(例如,AAC 编码AAC 解码),有没有办法在我的应用程序上使用这个本机代码?我该怎么做?

But I was wondering, since Android already includes StageFright, that has native code that can do encoding and decoding (for example, AAC encoding and AAC decoding), is there a way to use this native code on my application? How can I do that?

如果我只需要用它们的本机代码实现一些 JNI 类,那就太好了.另外,由于它是一个 Android 库,因此不会有任何许可问题(如果我错了,请纠正我).

It would be great if I only needed to implement some JNI classes with their native code. Plus, since it is an Android library, it would be no licensing problems whatever (correct me if I'm wrong).

推荐答案

是的,你可以使用 libstagefright,它非常强大.

yes, you can use libstagefright, it's very powerful.

因为stagefright 没有暴露于NDK,所以你必须做额外的工作.

Since stagefright is not exposed to NDK, so you will have to do extra work.

有两种方式:

(1) 使用 android 完整源代码树构建您的项目.这种方式需要几天的时间来设置,一旦准备好,就很容易了,你可以充分利用 stagefright.

(1) build your project using android full source tree. This way takes a few days to setup, once ready, it's very easy, and you can take full advantage of stagefright.

(2) 您可以将包含文件复制到您的项目中,它在此文件夹中:

(2) you can just copy include file to your project, it's inside this folder:

android-4.0.4_r1.1/frameworks/base/include/media/stagefright

android-4.0.4_r1.1/frameworks/base/include/media/stagefright

那么你就可以通过动态加载libstagefright.so来导出库函数,并且你可以链接到你的jni项目.

then you will have export the library function by dynamically loading libstagefright.so, and you can link with your jni project.

要使用 statgefright 进行编码/解码,非常简单,几百行就可以了.

To encode/decode using statgefright, it's very straightforward, a few hundred of lines can will do.

我使用 stagefright 截取屏幕截图创建了一个视频,该视频将在我们的 Android VNC 服务器中提供,并将很快发布.

I used stagefright to capture screenshots to create a video, which will be available in our Android VNC server, to be released soon.

以下是一个片段,我认为比使用ffmpeg编码电影更好.您也可以添加音频源.

the following is a snippet, I think it's better than using ffmpeg to encode a movie. You can add audio source as well.

class ImageSource : public MediaSource {
   ImageSource(int width, int height, int colorFormat)
    : mWidth(width),
      mHeight(height),
      mColorFormat(colorFormat)
   {
   }

   virtual status_t read(
        MediaBuffer **buffer, const MediaSource::ReadOptions *options) {
       // here you can fill the buffer with your pixels
   }

   ...
};

int width = 720;
int height = 480;
sp<MediaSource> img_source = new ImageSource(width, height, colorFormat);

sp<MetaData> enc_meta = new MetaData;
// enc_meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_H263);
// enc_meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_MPEG4);
enc_meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_AVC);
enc_meta->setInt32(kKeyWidth, width);
enc_meta->setInt32(kKeyHeight, height);
enc_meta->setInt32(kKeySampleRate, kFramerate);
enc_meta->setInt32(kKeyBitRate, kVideoBitRate);
enc_meta->setInt32(kKeyStride, width);
enc_meta->setInt32(kKeySliceHeight, height);
enc_meta->setInt32(kKeyIFramesInterval, kIFramesIntervalSec);
enc_meta->setInt32(kKeyColorFormat, colorFormat);

sp<MediaSource> encoder =
    OMXCodec::Create(
            client.interface(), enc_meta, true, image_source);

sp<MPEG4Writer> writer = new MPEG4Writer("/sdcard/screenshot.mp4");
writer->addSource(encoder);

// you can add an audio source here if you want to encode audio as well
// 
//sp<MediaSource> audioEncoder =
//    OMXCodec::Create(client.interface(), encMetaAudio, true, audioSource);
//writer->addSource(audioEncoder);

writer->setMaxFileDuration(kDurationUs);
CHECK_EQ(OK, writer->start());
while (!writer->reachedEOS()) {
    fprintf(stderr, ".");
    usleep(100000);
}
err = writer->stop();

这篇关于Android - 在我自己的项目中包含本机 StageFright 功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆