In my Android app, I receive a remote video stream in a proprietary format. As a new network packet arrives, I decode it natively and obtain a bitmap. In the UI, this bitmap is displayed in an ImageView control via setImageBitmap() method.

在我的Android应用程序中,我收到了专有格式的远程视频流。当一个新的网络数据包到达时,我本地解码它并获得一个位图。在UI中,此位图通过setImageBitmap()方法显示在ImageView控件中。

I am able to process 12 to 14 frames per second. I am wondering if there is a way to optimize this in terms of performance. Perhaps I can use hardware acceleration, if possible.

我能够每秒处理12到14帧。我想知道是否有一种方法可以在性能方面对其进行优化。如果可能的话,也许我可以使用硬件加速。

1 个解决方案

#1


Why not using a VideoView that already does all that kind of things for you?

为什么不使用已经为您做过所有这类事情的VideoView?

However:

  • I think you should use SurfaceView instead of ImageView. With SurfaceView you have your own dedicated thread that is just responsible for drawing on the canvas. Therefore redrawing is faster.

    我认为你应该使用SurfaceView而不是ImageView。使用SurfaceView,您可以拥有自己的专用线程,负责在画布上绘图。因此重绘速度更快。

  • I guess you are encoding a single Frame bitmap in an async thread which is completely fine. But you should think about how to parallelize that. Basically use a ThreadPool or something similar that decodes your video to single Bitmaps. Put them in a Buffer which can be a Fifo, List or Queue or whatever, but just keep in mind that one thread can be faster than another, so you better give every decoding task an order number so that you can add the encoded bitmap in correct order into the Buffer. The SurfaceView next should always take the first image from the buffer and display it on screen.

    我猜你是在异步线程中编码一个帧位图,这完全没问题。但你应该考虑如何并行化。基本上使用ThreadPool或类似的东西将视频解码为单个位图。将它们放在一个可以是Fifo,List或Queue等的Buffer中,但请记住,一个线程可以比另一个更快,所以你最好给每个解码任务一个订单号,这样你就可以添加编码的位图了正确的顺序进入缓冲区。 SurfaceView接下来应始终从缓冲区中获取第一个图像并将其显示在屏幕上。

  • Schedule the SurfaceView Thread redrawing to achive 60 frames per seconds. So not redraw over and over again in a while-loop think about a smarter (scheduled) solution.

    安排SurfaceView Thread重绘为每秒60帧。所以不要在一个while循环中反复重新思考一个更智能(预定)的解决方案。

  • Only redraw if the Buffer contains enough images (you have to determine the number of images in the buffer by your own or google it). Otherwise display a "loading / buffering" animation

    如果缓冲区包含足够的图像,则只重绘(您必须自己确定缓冲区中的图像数量或谷歌)。否则显示“加载/缓冲”动画

  • Garbage Collection can take some time, so try to reduce the time garbage collection runs. Bitmap association can consume a lot of memory and therefore garbage collector needs to run often to find free memory for the next bitmap. So think about the video source file if you can deliver it with a width / height that is more suitable for smartphones (maybe different files for different display sizes). Think about how you can decrease memory consumption. There are various options in BitmapFactory that you can use to reduce memory consumption (which reduces running garbage collection) like inSampleSize or inBitmap which seems a good choice since all bitmaps should have the same width / height. Have a look here. You could also reduce the Bitmap quality by using BitmapConfig.RGB_565.

    垃圾收集可能需要一些时间,因此请尽量减少垃圾收集运行的时间。位图关联可能会占用大量内存,因此垃圾收集器需要经常运行才能为下一个位图找到空闲内存。因此,如果您能够以更适合智能手机的宽度/高度(可能是针对不同显示尺寸的不同文件)提供视频源文件,请考虑一下。想想你如何减少内存消耗。 BitmapFactory中有各种选项可用于减少内存消耗(这减少了运行的垃圾收集),如inSampleSize或inBitmap,这似乎是一个不错的选择,因为所有位图都应具有相同的宽度/高度。看看这里。您还可以使用BitmapConfig.RGB_565降低位图质量。

更多相关文章

  1. Android学习笔记(三一):线程:Message和Runnable
  2. android多线程数据存储 - ThreadLocal的工作原理
  3. java基础---多线程---java内存模型
  4. java 使用Callable和Future返回线程执行的结果
  5. Java多线程聊天对话框
  6. Android 多线程下载文件原理霸气解析介绍 (完结版)-----greendao
  7. Java多线程六:线程优先级和yield()让步函数
  8. java多线程爬虫
  9. javafx 和netty 混合使用出现线程不一致问题,求大神指点

随机推荐

  1. 开源阅读器FBReader Android版本的编译
  2. android瀑布流
  3. android bionic缺失pthread_cancel的解决
  4. Android四大组件之Activity---生命周期那
  5. Android(安卓)3D旋转动画之Camera 和 Mat
  6. Android中的动画有哪几类?各自的特点和区
  7. 终于松了口气,说说这周我调试Sate210 andr
  8. Android(安卓)Material Design 详解(使用s
  9. 移动端开发新趋势Flutter
  10. Android(安卓)Layout 之TableLayout