当前位置:   article > 正文

庖丁解牛之-Android平台RTSP|RTMP播放器设计_android rtsp

android rtsp

背景

我们在做Android平台RTSP或者RTMP播放器开发的时候,需要注意的点非常多,以下,以大牛直播SDK(官方)的接口为例,大概介绍下相关接口设计:

接口设计

1. Open() 接口

Open接口的目的,主要是创建实例,正常返回player实例句柄,如有多路播放诉求,创建多个实例即可。

  1. /**
  2. * Initialize Player(启动播放实例)
  3. *
  4. * @param ctx: get by this.getApplicationContext()
  5. *
  6. * <pre>This function must be called firstly.</pre>
  7. *
  8. * @return player handle if successful, if return 0, which means init failed.
  9. */
  10. public native long SmartPlayerOpen(Object ctx);

2. Close()接口

Close接口,和Open()接口对应,负责释放相应实例的资源,调用Close()接口后,记得实例句柄置0即可。

注意:比如一个实例既可以实现播放,又可同时录像,亦或拉流(转发),这种情况下,调Close()接口时,需要确保录像、拉流都正常停止后,再调用。

  1. /**
  2. * 关闭播放实例,结束时必须调用close接口释放资源
  3. *
  4. * @param handle: return value from SmartPlayerOpen()
  5. *
  6. * <pre> NOTE: it could not use player handle after call this function. </pre>
  7. *
  8. * @return {0} if successful
  9. */
  10. public native int SmartPlayerClose(long handle);

3. 网络状态回调

一个好的播放器,好的状态回调必不可少,比如网络连通状态、快照、录像状态、当前下载速度等实时反馈,可以让上层开发者更好的掌控播放端状态,给用户更好的播放体验。

  1. /**
  2. * Set callback event(设置事件回调)
  3. *
  4. * @param handle: return value from SmartPlayerOpen()
  5. *
  6. * @param callbackv2: callback function
  7. *
  8. * @return {0} if successful
  9. */
  10. public native int SetSmartPlayerEventCallbackV2(long handle, NTSmartEventCallbackV2 callbackv2);

demo实现实例:

  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. break;
  29. case NTSmartEventID.EVENT_DANIULIVE_ERC_PLAYER_NO_MEDIADATA_RECEIVED:
  30. player_event = "收不到媒体数据,可能是url错误..";
  31. break;
  32. case NTSmartEventID.EVENT_DANIULIVE_ERC_PLAYER_SWITCH_URL:
  33. player_event = "切换播放URL..";
  34. break;
  35. case NTSmartEventID.EVENT_DANIULIVE_ERC_PLAYER_CAPTURE_IMAGE:
  36. player_event = "快照: " + param1 + " 路径:" + param3;
  37. if (param1 == 0) {
  38. player_event = player_event + ", 截取快照成功";
  39. } else {
  40. player_event = player_event + ", 截取快照失败";
  41. }
  42. break;
  43. case NTSmartEventID.EVENT_DANIULIVE_ERC_PLAYER_RECORDER_START_NEW_FILE:
  44. player_event = "[record]开始一个新的录像文件 : " + param3;
  45. break;
  46. case NTSmartEventID.EVENT_DANIULIVE_ERC_PLAYER_ONE_RECORDER_FILE_FINISHED:
  47. player_event = "[record]已生成一个录像文件 : " + param3;
  48. break;
  49. case NTSmartEventID.EVENT_DANIULIVE_ERC_PLAYER_START_BUFFERING:
  50. Log.i(TAG, "Start Buffering");
  51. break;
  52. case NTSmartEventID.EVENT_DANIULIVE_ERC_PLAYER_BUFFERING:
  53. Log.i(TAG, "Buffering:" + param1 + "%");
  54. break;
  55. case NTSmartEventID.EVENT_DANIULIVE_ERC_PLAYER_STOP_BUFFERING:
  56. Log.i(TAG, "Stop Buffering");
  57. break;
  58. case NTSmartEventID.EVENT_DANIULIVE_ERC_PLAYER_DOWNLOAD_SPEED:
  59. player_event = "download_speed:" + param1 + "Byte/s" + ", "
  60. + (param1 * 8 / 1000) + "kbps" + ", " + (param1 / 1024)
  61. + "KB/s";
  62. break;
  63. case NTSmartEventID.EVENT_DANIULIVE_ERC_PLAYER_RTSP_STATUS_CODE:
  64. Log.e(TAG, "RTSP error code received, please make sure username/password is correct, error code:" + param1);
  65. player_event = "RTSP error code:" + param1;
  66. break;
  67. case NTSmartEventID.EVENT_DANIULIVE_ERC_PLAYER_NEED_KEY:
  68. Log.e(TAG, "RTMP加密流,请设置播放需要的Key..");
  69. player_event = "RTMP加密流,请设置播放需要的Key..";
  70. break;
  71. case NTSmartEventID.EVENT_DANIULIVE_ERC_PLAYER_KEY_ERROR:
  72. Log.e(TAG, "RTMP加密流,Key错误,请重新设置..");
  73. player_event = "RTMP加密流,Key错误,请重新设置..";
  74. break;
  75. }
  76. if (player_event.length() > 0) {
  77. Log.i(TAG, player_event);
  78. Message message = new Message();
  79. message.what = PLAYER_EVENT_MSG;
  80. message.obj = player_event;
  81. handler.sendMessage(message);
  82. }
  83. }
  84. }

4. 软解码还是硬解码?

随着Android发展越来越好,各个厂商芯片对硬解码的支持,也越来越友好,一般情况下,如果适合通用的产品,在设备性能保障的情况下,优先建议软解,如果特定机型设备,可酌情考虑硬解,硬解码,又分为264硬解、HEVC硬解,直接设置surface模式的硬解,接口如下:

  1. /**
  2. * Set Video H.264 HW decoder(设置H.264硬解码)
  3. *
  4. * @param handle: return value from SmartPlayerOpen()
  5. *
  6. * @param isHWDecoder: 0: software decoder; 1: hardware decoder.
  7. *
  8. * @return {0} if successful
  9. */
  10. public native int SetSmartPlayerVideoHWDecoder(long handle, int isHWDecoder);
  11. /**
  12. * Set Video H.265(hevc) HW decoder(设置H.265硬解码)
  13. *
  14. * @param handle: return value from SmartPlayerOpen()
  15. *
  16. * @param isHevcHWDecoder: 0: software decoder; 1: hardware decoder.
  17. *
  18. * @return {0} if successful
  19. */
  20. public native int SetSmartPlayerVideoHevcHWDecoder(long handle, int isHevcHWDecoder);

设置surface模式的硬解:

  1. /**
  2. * 设置视频硬解码下Mediacodec自行绘制模式(此种模式下,硬解码兼容性和效率更好,回调YUV/RGB和快照功能将不可用)
  3. *
  4. * @param handle: return value from SmartPlayerOpen()
  5. *
  6. * @param isHWRenderMode: 0: not enable; 1: 用SmartPlayerSetSurface设置的surface自行绘制
  7. *
  8. * @return {0} if successful
  9. */
  10. public native int SmartPlayerSetHWRenderMode(long handle, int isHWRenderMode);
  11. /**
  12. * 更新硬解码surface
  13. *
  14. * @param handle: return value from SmartPlayerOpen()
  15. *
  16. * @return {0} if successful
  17. */
  18. public native int SmartPlayerUpdateHWRenderSurface(long handle);

5. 音频输出类型

音频输出,android平台支持audiotrack模式和opensl es模式,一般来说,考虑到设备通用性,建议采用audiotrack模式。

  1. /**
  2. * Set AudioOutput Type(设置audio输出类型)
  3. *
  4. * @param handle: return value from SmartPlayerOpen()
  5. *
  6. * @param use_audiotrack:
  7. *
  8. * <pre> NOTE: if use_audiotrack with 0: it will use auto-select output devices; if with 1: will use audio-track mode. </pre>
  9. *
  10. * @return {0} if successful
  11. */
  12. public native int SmartPlayerSetAudioOutputType(long handle, int use_audiotrack);

6. 缓冲时间设置

缓冲时间,顾名思义,缓存多少数据才开始播放,比如设置2000ms的buffer time,直播模式下,收到2秒数据后,才正常播放。

加大buffer time,会增大播放延迟,好处是,网络抖动的时候,流畅性更好。

  1. /**
  2. * Set buffer(设置缓冲时间,单位:毫秒)
  3. *
  4. * @param handle: return value from SmartPlayerOpen()
  5. *
  6. * @param buffer:
  7. *
  8. * <pre> NOTE: Unit is millisecond, range is 0-5000 ms </pre>
  9. *
  10. * @return {0} if successful
  11. */
  12. public native int SmartPlayerSetBuffer(long handle, int buffer);

7. 实时静音、实时音量调节

实时静音、实时音量调节顾名思义,播放端可以实时调整播放音量,或者直接静音掉,特别是多路播放场景下,非常有必要。

此外我们还提供了音量放大的功能,支持放大至原始音量的2倍。

  1. /**
  2. * Set mute or not(设置实时静音)
  3. *
  4. * @param handle: return value from SmartPlayerOpen()
  5. *
  6. * @param is_mute: if with 1:mute, if with 0: does not mute
  7. *
  8. * @return {0} if successful
  9. */
  10. public native int SmartPlayerSetMute(long handle, int is_mute);
  11. /**
  12. * 设置播放音量
  13. *
  14. * @param handle: return value from SmartPlayerOpen()
  15. *
  16. * @param volume: 范围是[0, 100], 0是静音,100是最大音量, 默认是100
  17. *
  18. * @return {0} if successful
  19. */
  20. public native int SmartPlayerSetAudioVolume(long handle, int volume);

8. RTSP TCP-UDP模式设置、超时时间设置或模式切换

有的RTSP服务器或摄像机,只支持RTSP TCP模式或者UDP模式,这个时候,默认设置TCP、UDP模式就至关重要,此外,我们还设计支持如TCP或UDP模式收不到数据,在超时时间后,可以自动切换到UDP或TCP。

  1. /**
  2. * 设置RTSP TCP/UDP模式(默认UDP模式)
  3. *
  4. * @param handle: return value from SmartPlayerOpen()
  5. *
  6. * @param is_using_tcp: if with 1, it will via TCP mode, while 0 with UDP mode
  7. *
  8. * @return {0} if successful
  9. */
  10. public native int SmartPlayerSetRTSPTcpMode(long handle, int is_using_tcp);
  11. /**
  12. * 设置RTSP超时时间, timeout单位为秒,必须大于0
  13. *
  14. * @param handle: return value from SmartPlayerOpen()
  15. *
  16. * @param timeout: RTSP timeout setting
  17. *
  18. * @return {0} if successful
  19. */
  20. public native int SmartPlayerSetRTSPTimeout(long handle, int timeout);
  21. /**
  22. * 设置RTSP TCP/UDP自动切换
  23. *
  24. * @param handle: return value from SmartPlayerOpen()
  25. *
  26. * NOTE: 对于RTSP来说,有些可能支持rtp over udp方式,有些可能支持使用rtp over tcp方式.
  27. * 为了方便使用,有些场景下可以开启自动尝试切换开关, 打开后如果udp无法播放,sdk会自动尝试tcp, 如果tcp方式播放不了,sdk会自动尝试udp.
  28. *
  29. * @param is_auto_switch_tcp_udp 如果设置1的话, sdk将在tcp和udp之间尝试切换播放,如果设置为0,则不尝试切换.
  30. *
  31. * @return {0} if successful
  32. */
  33. public native int SmartPlayerSetRTSPAutoSwitchTcpUdp(long handle, int is_auto_switch_tcp_udp);

9. 快速启动

快速启动,主要是针对服务器缓存GOP的场景下,快速刷到最新的数据,确保画面的持续性。

  1. /**
  2. * Set fast startup(设置快速启动模式,此模式针对服务器缓存GOP的场景有效)
  3. *
  4. * @param handle: return value from SmartPlayerOpen()
  5. *
  6. * @param is_fast_startup: if with 1, it will second play back, if with 0: does not it
  7. *
  8. * @return {0} if successful
  9. */
  10. public native int SmartPlayerSetFastStartup(long handle, int is_fast_startup);

10. 低延迟模式

低延迟模式下,设置buffer time为0,延迟更低,适用于比如需要操控控制的超低延迟场景下。

  1. /**
  2. * Set low latency mode(设置低延迟模式)
  3. *
  4. * @param handle: return value from SmartPlayerOpen()
  5. *
  6. * @param mode: if with 1, low latency mode, if with 0: normal mode
  7. *
  8. * @return {0} if successful
  9. */
  10. public native int SmartPlayerSetLowLatencyMode(long handle, int mode);

11. 视频view旋转、水平|垂直翻转

接口主要用于,比如原始的视频倒置等场景下,设备端无法调整时,通过播放端完成图像的正常角度播放。

  1. /**
  2. * 设置视频垂直反转
  3. *
  4. * @param handle: return value from SmartPlayerOpen()
  5. *
  6. * @param is_flip: 0: 不反转, 1: 反转
  7. *
  8. * @return {0} if successful
  9. */
  10. public native int SmartPlayerSetFlipVertical(long handle, int is_flip);
  11. /**
  12. * 设置视频水平反转
  13. *
  14. * @param handle: return value from SmartPlayerOpen()
  15. *
  16. * @param is_flip: 0: 不反转, 1: 反转
  17. *
  18. * @return {0} if successful
  19. */
  20. public native int SmartPlayerSetFlipHorizontal(long handle, int is_flip);
  21. /**
  22. * 设置顺时针旋转, 注意除了0度之外, 其他角度都会额外消耗性能
  23. *
  24. * @param handle: return value from SmartPlayerOpen()
  25. *
  26. * @param degress: 当前支持 0度,90度, 180度, 270度 旋转
  27. *
  28. * @return {0} if successful
  29. */
  30. public native int SmartPlayerSetRotation(long handle, int degress);

12. 设置实时回调下载速度

调用实时下载速度接口,通过设置下载速度时间间隔,和是否需要上报当前下载速度,实现APP层和底层SDK更友好的交互。

  1. /**
  2. * Set report download speed(设置实时回调下载速度)
  3. *
  4. * @param handle: return value from SmartPlayerOpen()
  5. *
  6. * @param is_report: if with 1, it will report download speed, it with 0: does not it.
  7. *
  8. * @param report_interval: report interval, unit is second, it must be greater than 0.
  9. *
  10. * @return {0} if successful
  11. */
  12. public native int SmartPlayerSetReportDownloadSpeed(long handle, int is_report, int report_interval );

13. 实时快照

简单来说,播放过程中,是不是要存取当前的播放画面。

  1. /**
  2. * Set if needs to save image during playback stream(是否启动快照功能)
  3. *
  4. * @param handle: return value from SmartPlayerOpen()
  5. *
  6. * @param is_save_image: if with 1, it will save current image via the interface of SmartPlayerSaveCurImage(), if with 0: does not it
  7. *
  8. * @return {0} if successful
  9. */
  10. public native int SmartPlayerSaveImageFlag(long handle, int is_save_image);
  11. /**
  12. * Save current image during playback stream(实时快照)
  13. *
  14. * @param handle: return value from SmartPlayerOpen()
  15. *
  16. * @param imageName: image name, which including fully path, "/sdcard/daniuliveimage/daniu.png", etc.
  17. *
  18. * @return {0} if successful
  19. */
  20. public native int SmartPlayerSaveCurImage(long handle, String imageName);

14. 扩展录像操作

播放端录像,我们做的非常细化,比如可以只录制音频或者只录制视频,设置录像存储路径,设置单个文件size,如果非AAC数据,可以转AAC后再录像。

  1. /**
  2. * Create file directory(创建录像目录)
  3. *
  4. * @param path, E.g: /sdcard/daniulive/rec
  5. *
  6. * <pre> The interface is only used for recording the stream data to local side. </pre>
  7. *
  8. * @return {0} if successful
  9. */
  10. public native int SmartPlayerCreateFileDirectory(String path);
  11. /**
  12. * Set recorder directory(设置录像目录)
  13. *
  14. * @param handle: return value from SmartPlayerOpen()
  15. *
  16. * @param path: the directory of recorder file
  17. *
  18. * <pre> NOTE: make sure the path should be existed, or else the setting failed. </pre>
  19. *
  20. * @return {0} if successful
  21. */
  22. public native int SmartPlayerSetRecorderDirectory(long handle, String path);
  23. /**
  24. * Set the size of every recorded file(设置单个录像文件大小,如超过设定大小则自动切换到下个文件录制)
  25. *
  26. * @param handle: return value from SmartPlayerOpen()
  27. *
  28. * @param size: (MB), (5M~500M), if not in this range, set default size with 200MB.
  29. *
  30. * @return {0} if successful
  31. */
  32. public native int SmartPlayerSetRecorderFileMaxSize(long handle, int size);
  33. /*
  34. * 设置录像时音频转AAC编码的开关
  35. *
  36. * @param handle: return value from SmartPlayerOpen()
  37. *
  38. * aac比较通用,sdk增加其他音频编码(比如speex, pcmu, pcma等)转aac的功能.
  39. *
  40. * @param is_transcode: 设置为1的话,如果音频编码不是aac,则转成aac,如果是aac,则不做转换. 设置为0的话,则不做任何转换. 默认是0.
  41. *
  42. * 注意: 转码会增加性能消耗
  43. *
  44. * @return {0} if successful
  45. */
  46. public native int SmartPlayerSetRecorderAudioTranscodeAAC(long handle, int is_transcode);
  47. /*
  48. *设置是否录视频,默认的话,如果视频源有视频就录,没有就没得录, 但有些场景下可能不想录制视频,只想录音频,所以增加个开关
  49. *
  50. *@param is_record_video: 1 表示录制视频, 0 表示不录制视频, 默认是1
  51. *
  52. * @return {0} if successful
  53. */
  54. public native int SmartPlayerSetRecorderVideo(long handle, int is_record_video);
  55. /*
  56. *设置是否录音频,默认的话,如果视频源有音频就录,没有就没得录, 但有些场景下可能不想录制音频,只想录视频,所以增加个开关
  57. *
  58. *@param is_record_audio: 1 表示录制音频, 0 表示不录制音频, 默认是1
  59. *
  60. * @return {0} if successful
  61. */
  62. public native int SmartPlayerSetRecorderAudio(long handle, int is_record_audio);
  1. /**
  2. * Start recorder stream(开始录像)
  3. *
  4. * @param handle: return value from SmartPlayerOpen()
  5. *
  6. * @return {0} if successful
  7. */
  8. public native int SmartPlayerStartRecorder(long handle);
  9. /**
  10. * Stop recorder stream(停止录像)
  11. *
  12. * @param handle: return value from SmartPlayerOpen()
  13. *
  14. * @return {0} if successful
  15. */
  16. public native int SmartPlayerStopRecorder(long handle);

15. 拉流回调编码后的数据(配合转发模块使用)

拉流回调编码后的数据,主要是为了配合转发模块使用,比如拉取rtsp流数据,直接转RTMP推送到RTMP服务。

  1. /*
  2. * 设置拉流时音频转AAC编码的开关
  3. *
  4. * @param handle: return value from SmartPlayerOpen()
  5. *
  6. * aac比较通用,sdk增加其他音频编码(比如speex, pcmu, pcma等)转aac的功能.
  7. *
  8. * @param is_transcode: 设置为1的话,如果音频编码不是aac,则转成aac, 如果是aac,则不做转换. 设置为0的话,则不做任何转换. 默认是0.
  9. * 注意: 转码会增加性能消耗
  10. *
  11. * @return {0} if successful
  12. */
  13. public native int SmartPlayerSetPullStreamAudioTranscodeAAC(long handle, int is_transcode);
  14. /**
  15. * Start pull stream(开始拉流,用于数据转发,只拉流不播放)
  16. *
  17. * @param handle: return value from SmartPlayerOpen()
  18. *
  19. * @return {0} if successful
  20. */
  21. public native int SmartPlayerStartPullStream(long handle);
  22. /**
  23. * Stop pull stream(停止拉流)
  24. *
  25. * @param handle: return value from SmartPlayerOpen()
  26. *
  27. * @return {0} if successful
  28. */
  29. public native int SmartPlayerStopPullStream(long handle);
  1. /**
  2. * Set Audio Data Callback(设置回调编码后音频数据)
  3. *
  4. * @param handle: return value from SmartPlayerOpen()
  5. *
  6. * @param audio_data_callback: Audio Data Callback.
  7. *
  8. * @return {0} if successful
  9. */
  10. public native int SmartPlayerSetAudioDataCallback(long handle, Object audio_data_callback);
  11. /**
  12. * Set Video Data Callback(设置回调编码后视频数据)
  13. *
  14. * @param handle: return value from SmartPlayerOpen()
  15. *
  16. * @param video_data_callback: Video Data Callback.
  17. *
  18. * @return {0} if successful
  19. */
  20. public native int SmartPlayerSetVideoDataCallback(long handle, Object video_data_callback);

16. H264用户数据回调或SEI数据回调

如发送端在264编码时,加了自定义的user data数据,可以通过以下接口实现数据回调,如需直接回调SEI数据,调下面SEI回调接口即可。

  1. /**
  2. * Set user data Callback(设置回调SEI扩展用户数据)
  3. *
  4. * @param handle: return value from SmartPlayerOpen()
  5. *
  6. * @param user_data_callback: user data callback.
  7. *
  8. * @return {0} if successful
  9. */
  10. public native int SmartPlayerSetUserDataCallback(long handle, Object user_data_callback);
  11. /**
  12. * Set SEI data Callback(设置回调SEI数据)
  13. *
  14. * @param handle: return value from SmartPlayerOpen()
  15. *
  16. * @param sei_data_callback: sei data callback.
  17. *
  18. * @return {0} if successful
  19. */
  20. public native int SmartPlayerSetSEIDataCallback(long handle, Object sei_data_callback);

17. 设置回调解码后YUV、RGB数据

如需对解码后的yuv或rgb数据,进行二次处理,如人脸识别等,可以通回调yuv rgb接口实现数据二次处理。

以YUV为例:

  1. class I420ExternalRender implements NTExternalRender {
  2. // public static final int NT_FRAME_FORMAT_RGBA = 1;
  3. // public static final int NT_FRAME_FORMAT_ABGR = 2;
  4. // public static final int NT_FRAME_FORMAT_I420 = 3;
  5. private int width_ = 0;
  6. private int height_ = 0;
  7. private int y_row_bytes_ = 0;
  8. private int u_row_bytes_ = 0;
  9. private int v_row_bytes_ = 0;
  10. private ByteBuffer y_buffer_ = null;
  11. private ByteBuffer u_buffer_ = null;
  12. private ByteBuffer v_buffer_ = null;
  13. @Override
  14. public int getNTFrameFormat() {
  15. Log.i(TAG, "I420ExternalRender::getNTFrameFormat return "
  16. + NT_FRAME_FORMAT_I420);
  17. return NT_FRAME_FORMAT_I420;
  18. }
  19. @Override
  20. public void onNTFrameSizeChanged(int width, int height) {
  21. width_ = width;
  22. height_ = height;
  23. y_row_bytes_ = (width_ + 15) & (~15);
  24. u_row_bytes_ = ((width_ + 1) / 2 + 15) & (~15);
  25. v_row_bytes_ = ((width_ + 1) / 2 + 15) & (~15);
  26. y_buffer_ = ByteBuffer.allocateDirect(y_row_bytes_ * height_);
  27. u_buffer_ = ByteBuffer.allocateDirect(u_row_bytes_
  28. * ((height_ + 1) / 2));
  29. v_buffer_ = ByteBuffer.allocateDirect(v_row_bytes_
  30. * ((height_ + 1) / 2));
  31. Log.i(TAG, "I420ExternalRender::onNTFrameSizeChanged width_="
  32. + width_ + " height_=" + height_ + " y_row_bytes_="
  33. + y_row_bytes_ + " u_row_bytes_=" + u_row_bytes_
  34. + " v_row_bytes_=" + v_row_bytes_);
  35. }
  36. @Override
  37. public ByteBuffer getNTPlaneByteBuffer(int index) {
  38. if (index == 0) {
  39. return y_buffer_;
  40. } else if (index == 1) {
  41. return u_buffer_;
  42. } else if (index == 2) {
  43. return v_buffer_;
  44. } else {
  45. Log.e(TAG, "I420ExternalRender::getNTPlaneByteBuffer index error:" + index);
  46. return null;
  47. }
  48. }
  49. @Override
  50. public int getNTPlanePerRowBytes(int index) {
  51. if (index == 0) {
  52. return y_row_bytes_;
  53. } else if (index == 1) {
  54. return u_row_bytes_;
  55. } else if (index == 2) {
  56. return v_row_bytes_;
  57. } else {
  58. Log.e(TAG, "I420ExternalRender::getNTPlanePerRowBytes index error:" + index);
  59. return 0;
  60. }
  61. }
  62. public void onNTRenderFrame(int width, int height, long timestamp) {
  63. if (y_buffer_ == null)
  64. return;
  65. if (u_buffer_ == null)
  66. return;
  67. if (v_buffer_ == null)
  68. return;
  69. y_buffer_.rewind();
  70. u_buffer_.rewind();
  71. v_buffer_.rewind();
  72. /*
  73. if ( !is_saved_image )
  74. {
  75. is_saved_image = true;
  76. int y_len = y_row_bytes_*height_;
  77. int u_len = u_row_bytes_*((height_+1)/2);
  78. int v_len = v_row_bytes_*((height_+1)/2);
  79. int data_len = y_len + (y_row_bytes_*((height_+1)/2));
  80. byte[] nv21_data = new byte[data_len];
  81. byte[] u_data = new byte[u_len];
  82. byte[] v_data = new byte[v_len];
  83. y_buffer_.get(nv21_data, 0, y_len);
  84. u_buffer_.get(u_data, 0, u_len);
  85. v_buffer_.get(v_data, 0, v_len);
  86. int[] strides = new int[2];
  87. strides[0] = y_row_bytes_;
  88. strides[1] = y_row_bytes_;
  89. int loop_row_c = ((height_+1)/2);
  90. int loop_c = ((width_+1)/2);
  91. int dst_row = y_len;
  92. int src_v_row = 0;
  93. int src_u_row = 0;
  94. for ( int i = 0; i < loop_row_c; ++i)
  95. {
  96. int dst_pos = dst_row;
  97. for ( int j = 0; j <loop_c; ++j )
  98. {
  99. nv21_data[dst_pos++] = v_data[src_v_row + j];
  100. nv21_data[dst_pos++] = u_data[src_u_row + j];
  101. }
  102. dst_row += y_row_bytes_;
  103. src_v_row += v_row_bytes_;
  104. src_u_row += u_row_bytes_;
  105. }
  106. String imagePath = "/sdcard" + "/" + "testonv21" + ".jpeg";
  107. Log.e(TAG, "I420ExternalRender::begin test save iamge++ image_path:" + imagePath);
  108. try
  109. {
  110. File file = new File(imagePath);
  111. FileOutputStream image_os = new FileOutputStream(file);
  112. YuvImage image = new YuvImage(nv21_data, ImageFormat.NV21, width_, height_, strides);
  113. image.compressToJpeg(new android.graphics.Rect(0, 0, width_, height_), 50, image_os);
  114. image_os.flush();
  115. image_os.close();
  116. }
  117. catch(IOException e)
  118. {
  119. e.printStackTrace();
  120. }
  121. Log.e(TAG, "I420ExternalRender::begin test save iamge--");
  122. }
  123. */
  124. Log.i(TAG, "I420ExternalRender::onNTRenderFrame w=" + width + " h=" + height + " timestamp=" + timestamp);
  125. // copy buffer
  126. // test
  127. // byte[] test_buffer = new byte[16];
  128. // y_buffer_.get(test_buffer);
  129. // Log.i(TAG, "I420ExternalRender::onNTRenderFrame y data:" + bytesToHexString(test_buffer));
  130. // u_buffer_.get(test_buffer);
  131. // Log.i(TAG, "I420ExternalRender::onNTRenderFrame u data:" + bytesToHexString(test_buffer));
  132. // v_buffer_.get(test_buffer);
  133. // Log.i(TAG, "I420ExternalRender::onNTRenderFrame v data:" + bytesToHexString(test_buffer));
  134. }
  135. }

18. 设置视频画面填充模式

设置视频画面的填充模式,如填充整个view、等比例填充view,如不设置,默认填充整个view。

相关接口设计如下:

  1. /**
  2. * 设置视频画面的填充模式,如填充整个view、等比例填充view,如不设置,默认填充整个view
  3. * @param handle: return value from SmartPlayerOpen()
  4. * @param render_scale_mode 0: 填充整个view; 1: 等比例填充view, 默认值是0
  5. * @return {0} if successful
  6. */
  7. public native int SmartPlayerSetRenderScaleMode(long handle, int render_scale_mode);

19. 设置SurfaceView模式下render类型

format: 0: RGB565格式,如不设置,默认此模式; 1: ARGB8888格式

  1. /**
  2. * 设置SurfaceView模式下(NTRenderer.CreateRenderer第二个参数传false的情况),render类型
  3. *
  4. * @param handle: return value from SmartPlayerOpen()
  5. *
  6. * @param format: 0: RGB565格式,如不设置,默认此模式; 1: ARGB8888格式
  7. *
  8. * @return {0} if successful
  9. */
  10. public native int SmartPlayerSetSurfaceRenderFormat(long handle, int format);

20. 设置surfaceview模式下抗锯齿

需要注意的是:抗锯齿模式开启后,会增加性能消耗。

  1. /**
  2. * 设置SurfaceView模式下(NTRenderer.CreateRenderer第二个参数传false的情况),抗锯齿效果,注意:抗锯齿模式开启后,可能会影像性能,请慎用
  3. *
  4. * @param handle: return value from SmartPlayerOpen()
  5. *
  6. * @param isEnableAntiAlias: 0: 如不设置,默认不开启抗锯齿模式; 1: 开启抗锯齿模式
  7. *
  8. * @return {0} if successful
  9. */
  10. public native int SmartPlayerSetSurfaceAntiAlias(long handle, int isEnableAntiAlias);

总结

以上就是Android平台RTSP、RTMP播放器接口设计需要参考的点,对于大多数开发者来说,不一定需要实现上述所有部分,只要按照产品诉求,实现其中的40%就足够满足特定场景使用了。

一个好的播放器,特别是要满足低延迟稳定的播放(毫秒级延迟),需要注意的点远不止如此,感兴趣的开发者,可以参考blog其他文章。

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

闽ICP备14008679号