全部產品
Search
文件中心

:HLS封裝介面

更新時間:Aug 30, 2018

OSS MEDIA C SDK 用戶端部分支援將接收到的H.264、AAC封裝為TS、M3U8格式後寫入OSS,除了基礎介面外,還提供封裝好的錄播、直播介面。

介面

HLS相關封裝介面都位於oss_media_hls_stream.h中,目前提供的介面有:

  • oss_media_hls_stream_open
  • oss_media_hls_stream_write
  • oss_media_hls_stream_close

下面詳細介紹各個介面的功能和注意事項

基礎結構體介紹

  1. /**
  2. * OSS MEDIA HLS STREAM OPTIONS的描述資訊
  3. */
  4. typedef struct oss_media_hls_stream_options_s {
  5. int8_t is_live;
  6. char *bucket_name;
  7. char *ts_name_prefix;
  8. char *m3u8_name;
  9. int32_t video_frame_rate;
  10. int32_t audio_sample_rate;
  11. int32_t hls_time;
  12. int32_t hls_list_size;
  13. } oss_media_hls_stream_options_t;
  14. /**
  15. * OSS MEDIA HLS STREAM的描述資訊
  16. */
  17. typedef struct oss_media_hls_stream_s {
  18. const oss_media_hls_stream_options_t *options;
  19. oss_media_hls_file_t *ts_file;
  20. oss_media_hls_file_t *m3u8_file;
  21. oss_media_hls_frame_t *video_frame;
  22. oss_media_hls_frame_t *audio_frame;
  23. oss_media_hls_m3u8_info_t *m3u8_infos;
  24. int32_t ts_file_index;
  25. int64_t current_file_begin_pts;
  26. int32_t has_aud;
  27. aos_pool_t *pool;
  28. } oss_media_hls_stream_t;

註:

  • is_live,是否是直播模式,直播模式的時候M3U8檔案裡面只最新的幾個ts檔案資訊
  • bucket_name,儲存HLS檔案的儲存空間名稱
  • ts_name_prefix,TS檔案名稱的首碼
  • m3u8_name,M3U8檔案名稱
  • video_frame_rate,視頻資料的幀率
  • audio_sample_rate,音頻資料的採樣率
  • hls_time,每個ts檔案持續時間上限
  • hls_list_size,直播模式時在M3U8檔案中最多保留的ts檔案個數

開啟HLS stream檔案

  1. /**
  2. * @brief 開啟一個oss hls檔案
  3. * @param[in] auth_func 授權函數,設定access_key_id/access_key_secret等
  4. * @param[in] options 配置資訊
  5. * @return:
  6. * 返回非NULL時成功,否則失敗
  7. */
  8. oss_media_hls_stream_t* oss_media_hls_stream_open(auth_fn_t auth_func,
  9. const oss_media_hls_stream_options_t *options);

註:

  • 範例程式碼參考:GitHub

關閉HLS stream檔案

  1. /**
  2. * @brief 關閉HLS stream檔案
  3. */
  4. int oss_media_hls_stream_close(oss_media_hls_stream_t *stream);

註:

  • 範例程式碼參考:GitHub

寫HLS stream檔案

  1. /**
  2. * @brief 寫入視頻和音頻資料
  3. * @param[in] video_buf 視頻資料
  4. * @param[in] video_len 視頻資料的長度,可以為0
  5. * @param[in] audio_buf 音頻資料
  6. * @param[in] audio_len 音頻資料的長度,可以為0
  7. * @param[in] stream HLS stream
  8. * @return:
  9. * 返回0時表示成功
  10. * 否則, 表示出現了錯誤
  11. */
  12. int oss_media_hls_stream_write(uint8_t *video_buf,
  13. uint64_t video_len,
  14. uint8_t *audio_buf,
  15. uint64_t audio_len,
  16. oss_media_hls_stream_t *stream);

樣本程式:

  1. static void write_video_audio_vod() {
  2. int ret;
  3. int max_size = 10 * 1024 * 1024;
  4. FILE *h264_file, *aac_file;
  5. uint8_t *h264_buf, *aac_buf;
  6. int h264_len, aac_len;
  7. oss_media_hls_stream_options_t options;
  8. oss_media_hls_stream_t *stream;
  9. /* 設定HLS stream的參數值 */
  10. options.is_live = 0;
  11. options.bucket_name = "<your buckete name>";
  12. options.ts_name_prefix = "vod/video_audio/test";
  13. options.m3u8_name = "vod/video_audio/vod.m3u8";
  14. options.video_frame_rate = 30;
  15. options.audio_sample_rate = 24000;
  16. options.hls_time = 5;
  17. /* 開啟HLS stream */
  18. stream = oss_media_hls_stream_open(auth_func, &options);
  19. if (stream == NULL) {
  20. printf("open hls stream failed.\n");
  21. return;
  22. }
  23. /* 建立兩個buffer用來儲存音頻和視頻資料 */
  24. h264_buf = malloc(max_size);
  25. aac_buf = malloc(max_size);
  26. /* 讀取一段視頻資料和音頻資料,然後調用介面寫入OSS */
  27. {
  28. h264_file = fopen("/path/to/video/1.h264", "r");
  29. h264_len = fread(h264_buf, 1, max_size, h264_file);
  30. fclose(h264_file);
  31. aac_file = fopen("/path/to/audio/1.aac", "r");
  32. aac_len = fread(aac_buf, 1, max_size, aac_file);
  33. fclose(aac_file);
  34. ret = oss_media_hls_stream_write(h264_buf, h264_len,
  35. aac_buf, aac_len, stream);
  36. if (ret != 0) {
  37. printf("write vod stream failed.\n");
  38. return;
  39. }
  40. }
  41. /* 再讀取一段視頻資料和音頻資料,然後調用介面寫入OSS */
  42. {
  43. h264_file = fopen("/path/to/video/2.h264", "r");
  44. h264_len = fread(h264_buf, 1, max_size, h264_file);
  45. fclose(h264_file);
  46. aac_file = fopen("/path/to/audio/1.aac", "r");
  47. aac_len = fread(aac_buf, 1, max_size, aac_file);
  48. fclose(aac_file);
  49. ret = oss_media_hls_stream_write(h264_buf, h264_len,
  50. aac_buf, aac_len, stream);
  51. if (ret != 0) {
  52. printf("write vod stream failed.\n");
  53. return;
  54. }
  55. }
  56. /* 寫完資料後,關閉HLS stream */
  57. ret = oss_media_hls_stream_close(stream);
  58. if (ret != 0) {
  59. printf("close vod stream failed.\n");
  60. return;
  61. }
  62. /* 釋放資源 */
  63. free(h264_buf);
  64. free(aac_buf);
  65. printf("convert H.264 and aac to HLS vod succeeded\n");
  66. }

註:

  • 目前的錄播、直播介面都支援只有視頻,只有音頻,同時有音視頻等。
  • 範例程式碼參考:GitHub
  • 目前的錄播、直播介面比較初級,使用者如果有進階需求,可以模擬這兩個介面,使用基礎介面自助實現進階定製功能。
  • 可以通過樣本程式觀看效果