Android的 - 包括我自己的项目本地StageFright功能自己的、功能、项目、Android

2023-09-12 10:56:01 作者:欲擒故纵纵然满城骚动

我目前正在开发一个需要录制音频的应用,连接code作为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.

我成功地记录AAC(包裹在MP4容器)使用 MediaRecorder ,并成功向上流使用 AudioRecord 一流的音频。但是,我需要能够连接code中的声音,因为我流了,但没有这些类似乎帮助我做到这一点。

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 ,具有天然code,可以做编码和解码(例如,AAC编码和AAC解码),有没有办法使用这种原生code在我的应用程序?我该怎么办呢?

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类与他们的本土code需要。此外,因为它是一个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 /框架/基/有/媒体/ 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 code /德code,这是非常简单的,线可以会做几百。

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

我用stagefright捕捉屏幕截图,以创建视频,这将是我们的Andr​​oid 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为en codeA的电影更好。您可以添加音频来源。

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();
 
精彩推荐
图片推荐