Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

109 rindas
3.4KB

  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 <string.h>
  11. #include <stdlib.h>
  12. #include <stdio.h>
  13. #include "mk_mediakit.h"
  14. typedef struct {
  15. mk_player player;
  16. mk_decoder video_decoder;
  17. mk_swscale swscale;
  18. } Context;
  19. void API_CALL on_track_frame_out(void *user_data, mk_frame frame) {
  20. Context *ctx = (Context *) user_data;
  21. mk_decoder_decode(ctx->video_decoder, frame, 1, 1);
  22. }
  23. void API_CALL on_frame_decode(void *user_data, mk_frame_pix frame) {
  24. Context *ctx = (Context *) user_data;
  25. int w = mk_get_av_frame_width(mk_frame_pix_get_av_frame(frame));
  26. int h = mk_get_av_frame_height(mk_frame_pix_get_av_frame(frame));
  27. #if 1
  28. uint8_t *brg24 = malloc(w * h * 3);
  29. mk_swscale_input_frame(ctx->swscale, frame, brg24);
  30. free(brg24);
  31. #else
  32. //todo 此处转换为opencv对象
  33. cv::Mat *mat = new cv::Mat();
  34. mat->create(h, w, CV_8UC3);
  35. mk_swscale_input_frame(ctx->swscale, frame, (uint8_t *) mat->data);
  36. #endif
  37. log_trace("decode frame output, pts:%d, w:%d, h:%d", mk_get_av_frame_dts(mk_frame_pix_get_av_frame(frame)), w, h);
  38. }
  39. void API_CALL on_mk_play_event_func(void *user_data, int err_code, const char *err_msg, mk_track tracks[],
  40. int track_count) {
  41. Context *ctx = (Context *) user_data;
  42. if (err_code == 0) {
  43. //success
  44. log_debug("play success!");
  45. int i;
  46. for (i = 0; i < track_count; ++i) {
  47. if (mk_track_is_video(tracks[i])) {
  48. log_info("got video track: %s", mk_track_codec_name(tracks[i]));
  49. ctx->video_decoder = mk_decoder_create(tracks[i], 0);
  50. mk_decoder_set_cb(ctx->video_decoder, on_frame_decode, user_data);
  51. //监听track数据回调
  52. mk_track_add_delegate(tracks[i], on_track_frame_out, user_data);
  53. }
  54. }
  55. } else {
  56. log_warn("play failed: %d %s", err_code, err_msg);
  57. }
  58. }
  59. void API_CALL on_mk_shutdown_func(void *user_data, int err_code, const char *err_msg, mk_track tracks[], int track_count) {
  60. log_warn("play interrupted: %d %s", err_code, err_msg);
  61. }
  62. int main(int argc, char *argv[]) {
  63. mk_config config = {
  64. .ini = NULL,
  65. .ini_is_path = 0,
  66. .log_level = 0,
  67. .log_mask = LOG_CONSOLE,
  68. .ssl = NULL,
  69. .ssl_is_path = 1,
  70. .ssl_pwd = NULL,
  71. .thread_num = 0
  72. };
  73. mk_env_init(&config);
  74. if (argc != 2) {
  75. printf("Usage: ./player url\n");
  76. return -1;
  77. }
  78. Context ctx;
  79. memset(&ctx, 0, sizeof(Context));
  80. ctx.player = mk_player_create();
  81. ctx.swscale = mk_swscale_create(3, 0, 0);
  82. mk_player_set_on_result(ctx.player, on_mk_play_event_func, &ctx);
  83. mk_player_set_on_shutdown(ctx.player, on_mk_shutdown_func, &ctx);
  84. mk_player_play(ctx.player, argv[1]);
  85. log_info("enter any key to exit");
  86. getchar();
  87. if (ctx.player) {
  88. mk_player_release(ctx.player);
  89. }
  90. if (ctx.video_decoder) {
  91. mk_decoder_release(ctx.video_decoder, 1);
  92. }
  93. if (ctx.swscale) {
  94. mk_swscale_release(ctx.swscale);
  95. }
  96. return 0;
  97. }