Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

94 рядки
2.5KB

  1. /*
  2. * Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
  3. *
  4. * This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
  5. *
  6. * Use of this source code is governed by MIT license that can be found in the
  7. * LICENSE file in the root of the source tree. All contributing project authors
  8. * may be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #include <signal.h>
  11. #include <string.h>
  12. #include <stdio.h>
  13. #ifdef _WIN32
  14. #include "windows.h"
  15. #else
  16. #include "unistd.h"
  17. #endif
  18. #include "mk_mediakit.h"
  19. static int exit_flag = 0;
  20. static void s_on_exit(int sig) {
  21. exit_flag = 1;
  22. }
  23. static void on_h264_frame(void *user_data, mk_h264_splitter splitter, const char *data, int size) {
  24. #ifdef _WIN32
  25. Sleep(40);
  26. #else
  27. usleep(40 * 1000);
  28. #endif
  29. static int dts = 0;
  30. mk_frame frame = mk_frame_create(MKCodecH264, dts, dts, data, size, NULL, NULL);
  31. dts += 40;
  32. mk_media_input_frame((mk_media) user_data, frame);
  33. mk_frame_unref(frame);
  34. }
  35. int main(int argc, char *argv[]) {
  36. mk_config config = {
  37. .ini = NULL,
  38. .ini_is_path = 1,
  39. .log_level = 0,
  40. .log_mask = LOG_CONSOLE,
  41. .log_file_path = NULL,
  42. .log_file_days = 0,
  43. .ssl = NULL,
  44. .ssl_is_path = 1,
  45. .ssl_pwd = NULL,
  46. .thread_num = 0
  47. };
  48. mk_env_init(&config);
  49. mk_http_server_start(80, 0);
  50. mk_rtsp_server_start(554, 0);
  51. mk_rtmp_server_start(1935, 0);
  52. signal(SIGINT, s_on_exit);// 设置退出信号
  53. FILE *fp = fopen(argv[1], "rb");
  54. if (!fp) {
  55. log_error("打开文件失败!");
  56. return -1;
  57. }
  58. mk_media media = mk_media_create("__defaultVhost__", "live", "test", 0, 0, 0);
  59. //h264的codec
  60. //mk_media_init_video(media, 0, 0, 0, 0, 2 * 104 * 1024);
  61. codec_args v_args={0};
  62. mk_track v_track = mk_track_create(MKCodecH264,&v_args);
  63. mk_media_init_track(media,v_track);
  64. mk_media_init_complete(media);
  65. mk_track_unref(v_track);
  66. //创建h264分帧器
  67. mk_h264_splitter splitter = mk_h264_splitter_create(on_h264_frame, media);
  68. char buf[1024];
  69. while (!exit_flag) {
  70. int size = fread(buf, 1, sizeof(buf) - 1, fp);
  71. if (size > 0) {
  72. mk_h264_splitter_input_data(splitter, buf, size);
  73. } else {
  74. //文件读完了,重新开始
  75. fseek(fp, 0, SEEK_SET);
  76. }
  77. }
  78. log_info("文件读取完毕");
  79. mk_h264_splitter_release(splitter);
  80. mk_media_release(media);
  81. fclose(fp);
  82. mk_stop_all_server();
  83. return 0;
  84. }