edit-icon download-icon

HLS encapsulated interfaces

Last Updated: Nov 21, 2017

The OSS MEDIA C SDK client can pack the received H.264 or AAC format data into the TS or M3U8 format and then write the data into the OSS. Apart from basic interfaces, the SDK also provides packaged recording and live video interfaces.

Interface

HLS-related encapsulated interfaces are all located in the oss_media_hls_stream.h. Currently the provided interfaces include:

  • oss_media_hls_stream_open
  • oss_media_hls_stream_write
  • oss_media_hls_stream_close

The functions and notes of usage of various interfaces are mentioned as follows:

Basic structs

  1. /**
  2. * OSS MEDIA HLS STREAM OPTIONS descriptive information
  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 descriptive information
  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: It indicates whether the video is in live mode. When the video is in live mode, the M3U8 object only contains the information of the last few TS objects.
  • bucket_name: Name of the buckets that store the HLS files.
  • ts_name_prefix: Prefix of the TS object name.
  • m3u8_name: Name of the M3U8 object.
  • video_frame_rate: Frame rate of the video data.
  • audio_sample_rate: Sample rate of the audio data.
  • hls_time: The maximum duration of each TS object.
  • hls_list_size: The number of TS objects retained in the M3U8 object in live video mode.

Open an HLS stream object

  1. /**
  2. * @brief Open an OSS HLS file
  3. * @param[in] auth_func The authorization function to set access_key_id/access_key_secret
  4. * @param[in] options Configuration information
  5. * @return:
  6. * If NULL is returned, it indicates success. Otherwise, it indicates failure.
  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);

Note: For the complete example code, see GitHub.

Close an HLS stream object

  1. /**
  2. * @brief Close the HLS stream object
  3. */
  4. int oss_media_hls_stream_close(oss_media_hls_stream_t *stream);

Note: For the complete example code, see GitHub.

Write an HLS stream object

  1. /**
  2. * @brief Write the video and audio data
  3. * @param[in] video_buf Video data
  4. * @param[in] video_len Video data length. It can be 0
  5. * @param[in] audio_buf Audio data
  6. * @param[in] audio_len Audio data length. It can be 0
  7. * @param[in] stream HLS stream
  8. * @return:
  9. * If 0 is returned, it indicates the operation is successful.
  10. * Otherwise, it indicates an error has occurred
  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);

Example project:

  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. /* Set the HLS stream parameter value */
  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. /* Open the 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. /* Create two buffers to store the audio and video data */
  24. h264_buf = malloc(max_size);
  25. aac_buf = malloc(max_size);
  26. /* Read a piece of video data and audio data and call the interface to write data to the 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. /* Read a piece of video data and audio data again and call the interface to write data to the 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. /* After the data write, close the 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. /* Release the resources */
  63. free(h264_buf);
  64. free(aac_buf);
  65. printf("convert H.264 and aac to HLS vod succeeded\n");
  66. }

Note:

  • Currently the recording and live video interfaces support video-only, audio-only and audio-video modes.

  • For the complete example code, see GitHub.

  • Recording and live video interfaces are still relatively basic. If you need advanced features, use the basic interfaces to simulate the two interfaces to implement advanced custom features by yourself..

  • You can view the effect in the example project.

Thank you! We've received your feedback.