当前位置:   article > 正文

Android平台RTSP|RTMP播放器如何实现TextureView渲染_android rtsp播放器

android rtsp播放器

技术背景

自2015年我们发布Android平台RTSP、RTMP直播播放模块以来,渲染这块,支持SurfaceView或GlSurfaceView,当然如果开发者需要TextureView渲染,可以把RTSP、RTMP流数据解码回调YUV或RGB数据上来,上层自己渲染。本文主要介绍,如何实现RTSP、RTMP播放器TextureView渲染。在此之前,我们先看看TextureView优缺点:

先说优点:

  1. 更高的性能:TextureView使用基于硬件加速的渲染管道,可以在GPU中进行图像处理和渲染,这比SurfaceView的软件渲染方式更高效。因此,TextureView在图像和视频渲染方面具有更好的性能。
  2. 更强的功能:TextureView可以与其他控件进行自由组合,可以在布局中灵活地放置和调整大小。而SurfaceView只能全屏显示,无法与其他控件混合使用。
  3. 更灵活的绘制方式:TextureView允许开发者直接在其上面绘制图像,通过Canvas和OpenGL ES等API,可以实现更丰富的渲染效果。
  4. 支持动画和截图:TextureView支持移动、旋转、缩放等动画,并且支持截图功能。

再说缺点:

  1. 必须在硬件加速的窗口中使用:TextureView必须在硬件加速的窗口中使用,如果设备不支持硬件加速或者硬件加速被禁用,TextureView可能无法正常工作。
  2. 占用内存较高:与SurfaceView相比,TextureView占用内存更高,这可能会影响到应用的性能,特别是在处理大型图像或视频时。
  3. 渲染线程问题:在Android 5.0以前,TextureView在主线程进行渲染,这可能导致UI卡顿。虽然在Android 5.0及以后版本中,TextureView有了单独的渲染线程,但在高GPU负荷的场景下,可能存在帧率下降的问题。
  4. 同步问题:TextureView需要在多个线程之间进行写读同步,包括CPU和GPU的同步。当同步失调时,可能会出现掉帧或吞帧导致的卡顿和抖动现象。

技术实现

本文以大牛直播SDK的Android平台SmartPlayerV2工程demo为例:

开始播放之前,CreateView()实现如下:

  1. /*
  2. * SmartPlayer.java
  3. * Author: daniusdk.com
  4. * Create rendering with different type
  5. */
  6. private boolean CreateView() {
  7. if (sSurfaceView != null)
  8. return true;
  9. Log.i(TAG, "CreateView");
  10. if (SURFACE_TYPE_NULL == surface_type_) {
  11. String manufacturer = Build.MANUFACTURER;
  12. Log.i(TAG, "CreateView, current manufacturer: " + manufacturer);
  13. if (is_enable_hardware_render_mode) {
  14. //hardware render模式,第二个参数设置为false
  15. sSurfaceView = NTRenderer.CreateRenderer(this, false);
  16. } else {
  17. //这个字符串可以自己定义,例如判断华为就填写huawei,魅族就填写meizu
  18. if ("huawei".equalsIgnoreCase(manufacturer)) {
  19. sSurfaceView = NTRenderer.CreateRenderer(this, true);
  20. } else {
  21. /*
  22. * useOpenGLES2: If with true: Check if system supports openGLES, if
  23. * supported, it will choose openGLES. If with false: it will set
  24. * with default surfaceView;
  25. */
  26. sSurfaceView = NTRenderer.CreateRenderer(this, true);
  27. }
  28. }
  29. } else {
  30. if (SURFACE_TYPE_TEXTURE_VIEW == surface_type_) {
  31. TextureView texture_view = new TextureView(this);
  32. texture_view.setSurfaceTextureListener(this);
  33. sSurfaceView = texture_view;
  34. } else
  35. sSurfaceView= new SurfaceView(this);
  36. }
  37. if (sSurfaceView == null) {
  38. Log.i(TAG, "Create render failed..");
  39. return false;
  40. }
  41. if (is_enable_hardware_render_mode || SURFACE_TYPE_SURFACE_VIEW == surface_type_) {
  42. if (sSurfaceView instanceof SurfaceView) {
  43. SurfaceHolder surfaceHolder = ((SurfaceView)sSurfaceView).getHolder();
  44. if (surfaceHolder == null)
  45. Log.e(TAG, "CreateView, surfaceHolder with null..");
  46. else
  47. surfaceHolder.addCallback(this);
  48. }
  49. }
  50. return true;
  51. }

视频流开始播放后,我们会把视频宽高信息回调上来(EVENT_DANIULIVE_ERC_PLAYER_RESOLUTION_INFO),然后,根据获取到的宽高信息,调用adjustTextureViewAspectRatio()按比例显示窗口,如果需要铺满显示,不调用比例显示即可。

  1. class EventHandeV2 implements NTSmartEventCallbackV2 {
  2. @Override
  3. public void onNTSmartEventCallbackV2(long handle, int id, long param1,
  4. long param2, String param3, String param4, Object param5) {
  5. //Log.i(TAG, "EventHandeV2: handle=" + handle + " id:" + id);
  6. String player_event = "";
  7. switch (id) {
  8. case NTSmartEventID.EVENT_DANIULIVE_ERC_PLAYER_STARTED:
  9. player_event = "开始..";
  10. break;
  11. case NTSmartEventID.EVENT_DANIULIVE_ERC_PLAYER_CONNECTING:
  12. player_event = "连接中..";
  13. break;
  14. case NTSmartEventID.EVENT_DANIULIVE_ERC_PLAYER_CONNECTION_FAILED:
  15. player_event = "连接失败..";
  16. break;
  17. case NTSmartEventID.EVENT_DANIULIVE_ERC_PLAYER_CONNECTED:
  18. player_event = "连接成功..";
  19. break;
  20. case NTSmartEventID.EVENT_DANIULIVE_ERC_PLAYER_DISCONNECTED:
  21. player_event = "连接断开..";
  22. break;
  23. case NTSmartEventID.EVENT_DANIULIVE_ERC_PLAYER_STOP:
  24. player_event = "停止播放..";
  25. break;
  26. case NTSmartEventID.EVENT_DANIULIVE_ERC_PLAYER_RESOLUTION_INFO:
  27. player_event = "分辨率信息: width: " + param1 + ", height: " + param2;
  28. handler.post(new OnNTPlayerVideoSize((int)param1, (int)param2));
  29. break;
  30. case NTSmartEventID.EVENT_DANIULIVE_ERC_PLAYER_NO_MEDIADATA_RECEIVED:
  31. player_event = "收不到媒体数据,可能是url错误..";
  32. break;
  33. case NTSmartEventID.EVENT_DANIULIVE_ERC_PLAYER_SWITCH_URL:
  34. player_event = "切换播放URL..";
  35. break;
  36. case NTSmartEventID.EVENT_DANIULIVE_ERC_PLAYER_CAPTURE_IMAGE:
  37. player_event = "快照: " + param1 + " 路径:" + param3;
  38. if (param1 == 0)
  39. player_event = player_event + ", 截取快照成功";
  40. else
  41. player_event = player_event + ", 截取快照失败";
  42. if (param4 != null && !param4.isEmpty())
  43. player_event += (", user data:" + param4);
  44. break;
  45. case NTSmartEventID.EVENT_DANIULIVE_ERC_PLAYER_RECORDER_START_NEW_FILE:
  46. player_event = "[record]开始一个新的录像文件 : " + param3;
  47. break;
  48. case NTSmartEventID.EVENT_DANIULIVE_ERC_PLAYER_ONE_RECORDER_FILE_FINISHED:
  49. player_event = "[record]已生成一个录像文件 : " + param3;
  50. break;
  51. case NTSmartEventID.EVENT_DANIULIVE_ERC_PLAYER_START_BUFFERING:
  52. Log.i(TAG, "Start Buffering");
  53. break;
  54. case NTSmartEventID.EVENT_DANIULIVE_ERC_PLAYER_BUFFERING:
  55. Log.i(TAG, "Buffering:" + param1 + "%");
  56. break;
  57. case NTSmartEventID.EVENT_DANIULIVE_ERC_PLAYER_STOP_BUFFERING:
  58. Log.i(TAG, "Stop Buffering");
  59. break;
  60. case NTSmartEventID.EVENT_DANIULIVE_ERC_PLAYER_DOWNLOAD_SPEED:
  61. player_event = "download_speed:" + param1 + "Byte/s" + ", "
  62. + (param1 * 8 / 1000) + "kbps" + ", " + (param1 / 1024)
  63. + "KB/s";
  64. break;
  65. case NTSmartEventID.EVENT_DANIULIVE_ERC_PLAYER_RTSP_STATUS_CODE:
  66. Log.e(TAG, "RTSP error code received, please make sure username/password is correct, error code:" + param1);
  67. player_event = "RTSP error code:" + param1;
  68. break;
  69. ....
  70. }
  71. if (player_event.length() > 0) {
  72. Log.i(TAG, player_event);
  73. Message message = new Message();
  74. message.what = PLAYER_EVENT_MSG;
  75. message.obj = player_event;
  76. handler.sendMessage(message);
  77. }
  78. }
  79. }

OnNTPlayerVideoSize实现如下:

  1. class OnNTPlayerVideoSize implements Runnable{
  2. int width_;
  3. int height_;
  4. OnNTPlayerVideoSize(int w, int h ) {
  5. this.width_ = w;
  6. this.height_ = h;
  7. }
  8. public void run() {
  9. if (this.width_ < 1 || this.height_ < 1)
  10. return;
  11. video_width_ = this.width_;
  12. video_height_ = this.height_;
  13. if (null == sSurfaceView)
  14. return;
  15. if (SURFACE_TYPE_TEXTURE_VIEW == surface_type_ && sSurfaceView instanceof TextureView)
  16. adjustTextureViewAspectRatio((TextureView)sSurfaceView, this.width_, this.height_);
  17. else if (((isHardwareDecoder&&is_enable_hardware_render_mode) || SURFACE_TYPE_SURFACE_VIEW == surface_type_)
  18. && sSurfaceView instanceof SurfaceView )
  19. adjustSurfaceViewAspectRatio((SurfaceView)sSurfaceView, this.width_, this.height_);
  20. }
  21. }

针对TextureView处理如下:

  1. /**
  2. * Invoked when a {@link TextureView}'s SurfaceTexture is ready for use.
  3. *
  4. * @param surface The surface returned by
  5. * {@link android.view.TextureView#getSurfaceTexture()}
  6. * @param width The width of the surface
  7. * @param height The height of the surface
  8. */
  9. public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
  10. Log.i(TAG, "TextureView onSurfaceTextureAvailable w:" + width + ", h:" + height);
  11. if (texture_view_surface_ != null) {
  12. texture_view_surface_.release();
  13. texture_view_surface_ = null;
  14. }
  15. texture_view_surface_ = new Surface(surface);
  16. if (isPlaying && SURFACE_TYPE_TEXTURE_VIEW == surface_type_) {
  17. libPlayer.SetSurface(playerHandle, texture_view_surface_, 0, disable_codec_render_surface_, disable_sdk_render_surface_);
  18. if (video_width_ > 0 && video_height_ > 0 && sSurfaceView != null && (sSurfaceView instanceof TextureView))
  19. adjustTextureViewAspectRatio((TextureView)sSurfaceView, video_width_, video_height_);
  20. }
  21. }
  22. /**
  23. * Invoked when the {@link SurfaceTexture}'s buffers size changed.
  24. *
  25. * @param surface The surface returned by
  26. * {@link android.view.TextureView#getSurfaceTexture()}
  27. * @param width The new width of the surface
  28. * @param height The new height of the surface
  29. */
  30. public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
  31. Log.i(TAG, "TextureView onSurfaceTextureSizeChanged w:" + width + ", h:" + height);
  32. if(isPlaying && SURFACE_TYPE_TEXTURE_VIEW == surface_type_) {
  33. if (sSurfaceView != null && (sSurfaceView instanceof TextureView) && video_width_ > 0 && video_height_ > 0)
  34. adjustTextureViewAspectRatio((TextureView) sSurfaceView, video_width_, video_height_);
  35. }
  36. }
  37. /**
  38. * Invoked when the specified {@link SurfaceTexture} is about to be destroyed.
  39. * If returns true, no rendering should happen inside the surface texture after this method
  40. * is invoked. If returns false, the client needs to call {@link SurfaceTexture#release()}.
  41. * Most applications should return true.
  42. *
  43. * @param surface The surface about to be destroyed
  44. */
  45. public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
  46. Log.i(TAG, "TextureView onSurfaceTextureDestroyed");
  47. if(isPlaying && SURFACE_TYPE_TEXTURE_VIEW == surface_type_)
  48. libPlayer.SetSurface(playerHandle, null, 0, 0, 0);
  49. if (texture_view_surface_ != null) {
  50. texture_view_surface_.release();
  51. texture_view_surface_ = null;
  52. }
  53. return true;
  54. }
  55. /**
  56. * Invoked when the specified {@link SurfaceTexture} is updated through
  57. * {@link SurfaceTexture#updateTexImage()}.
  58. *
  59. * @param surface The surface just updated
  60. */
  61. public void onSurfaceTextureUpdated(SurfaceTexture surface) {
  62. // Log.i(TAG, "TextureView onSurfaceTextureUpdated");
  63. }

至此,RTSP|RTMP播放,我们是实现的功能如下(如不特别说明,代表Windows、Linux、Android、iOS平台均支持):

  • [支持播放协议]高稳定、超低延迟、业内首屈一指的RTSP|RTMP直播播放器SDK;
  •  [多实例播放]支持多实例播放;
  •  [事件回调]支持网络状态、buffer状态等回调;
  •  [视频格式]支持H.265、H.264,此外,还支持RTSP MJPEG播放;
  •  [音频格式]RTSP支持AAC/PCMA/PCMU、RTMP支持AAC/PCMA/PCMU/Speex;
  •  [H.264/H.265软解码]支持H.264/H.265软解,支持Enhanced RTMP H.265
  •  [H.264硬解码]Windows/Android/iOS支持特定机型H.264硬解;
  •  [H.265硬解]Windows/Android/iOS支持特定机型H.265硬解;
  •  [H.264/H.265硬解码]Android支持设置Surface模式硬解和普通模式硬解码;
  •  [RTSP模式设置]支持RTSP TCP/UDP模式设置;
  •  [RTSP TCP/UDP自动切换]支持RTSP TCP、UDP模式自动切换;
  •  [RTSP超时设置]支持RTSP超时时间设置,单位:秒;
  •  [RTSP 401认证处理]支持上报RTSP 401事件,如URL携带鉴权信息,会自动处理;
  •  [缓冲时间设置]支持buffer time设置;
  •  [首屏秒开]支持首屏秒开模式;
  •  [复杂网络处理]支持断网重连等各种网络环境自动适配;
  •  [快速切换URL]支持播放过程中,快速切换其他URL,内容切换更快;
  •  [音视频多种render机制]Android平台,视频:surfaceview/OpenGL ES,音频:AudioTrack/OpenSL ES;
  •  [实时静音]支持播放过程中,实时静音/取消静音;
  •  [实时音量调节]支持播放过程中实时调节音量;
  •  [实时快照]支持播放过程中截取当前播放画面;
  •  [只播关键帧]Windows平台支持实时设置是否只播放关键帧;
  •  [渲染角度]支持0°,90°,180°和270°四个视频画面渲染角度设置;
  •  [渲染镜像]支持水平反转、垂直反转模式设置;
  •  [等比例缩放]支持图像等比例缩放绘制(Android设置surface模式硬解模式不支持);
  •  [实时下载速度更新]支持当前下载速度实时回调(支持设置回调时间间隔);
  •  [解码前视频数据回调]支持H.264/H.265数据回调;
  •  [解码后视频数据回调]支持解码后YUV/RGB数据回调;
  •  [解码前音频数据回调]支持AAC/PCMA/PCMU数据回调;
  •  [音视频自适应]支持播放过程中,音视频信息改变后自适应;
  •  [扩展录像功能]完美支持和录像SDK组合使用。

总结

做播放器不难,做高稳定低延迟低资源占用的RTMP|RTSP直播播放器还是有点儿难度,以上是大牛直播SDK针对Android平台RTMP|RTSP播放器TextureView渲染相关的技术交流,感兴趣的开发者,也可以找我单独沟通。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/秋刀鱼在做梦/article/detail/852552
推荐阅读
相关标签
  

闽ICP备14008679号