Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

177 řádky
5.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 "mk_frame.h"
  11. #include "Extension/Frame.h"
  12. #include "Extension/H264.h"
  13. #include "Extension/H265.h"
  14. #include "Extension/AAC.h"
  15. using namespace mediakit;
  16. extern "C" {
  17. #define XX(name, type, value, str, mpeg_id) API_EXPORT const int MK##name = value;
  18. CODEC_MAP(XX)
  19. #undef XX
  20. }
  21. class FrameFromPtrForC : public FrameFromPtr {
  22. public:
  23. using Ptr = std::shared_ptr<FrameFromPtrForC>;
  24. template<typename ...ARGS>
  25. FrameFromPtrForC(bool cache_able, uint32_t flags, on_mk_frame_data_release cb, void *user_data, ARGS &&...args) : FrameFromPtr(
  26. std::forward<ARGS>(args)...) {
  27. _flags = flags;
  28. _cb = cb;
  29. _user_data = user_data;
  30. _cache_able = cache_able;
  31. }
  32. ~FrameFromPtrForC() override {
  33. if (_cb) {
  34. _cb(_user_data, _ptr);
  35. }
  36. }
  37. bool cacheAble() const override {
  38. return _cache_able;
  39. }
  40. bool keyFrame() const override {
  41. return _flags & MK_FRAME_FLAG_IS_KEY;
  42. }
  43. bool configFrame() const override {
  44. return _flags & MK_FRAME_FLAG_IS_CONFIG;
  45. }
  46. //默认返回false
  47. bool dropAble() const override {
  48. return _flags & MK_FRAME_FLAG_DROP_ABLE;
  49. }
  50. //默认返回true
  51. bool decodeAble() const override {
  52. return !(_flags & MK_FRAME_FLAG_NOT_DECODE_ABLE);
  53. }
  54. private:
  55. uint32_t _flags;
  56. on_mk_frame_data_release _cb;
  57. void *_user_data;
  58. bool _cache_able;
  59. };
  60. static mk_frame mk_frame_create_complex(int codec_id, uint64_t dts, uint64_t pts, uint32_t frame_flags, size_t prefix_size,
  61. char *data, size_t size, on_mk_frame_data_release cb, void *user_data) {
  62. switch (codec_id) {
  63. case CodecH264:
  64. return new Frame::Ptr(new H264FrameHelper<FrameFromPtrForC>(cb, frame_flags, cb, user_data, (CodecId) codec_id,
  65. data, size, dts, pts, prefix_size));
  66. case CodecH265:
  67. return new Frame::Ptr(new H265FrameHelper<FrameFromPtrForC>(cb, frame_flags, cb, user_data, (CodecId) codec_id,
  68. data, size, dts, pts, prefix_size));
  69. default:
  70. return new Frame::Ptr(new FrameFromPtrForC(cb, frame_flags, cb, user_data, (CodecId) codec_id, data,
  71. size, dts, pts, prefix_size));
  72. }
  73. }
  74. API_EXPORT mk_frame API_CALL mk_frame_create(int codec_id, uint64_t dts, uint64_t pts, const char *data, size_t size,
  75. on_mk_frame_data_release cb, void *user_data) {
  76. switch (codec_id) {
  77. case CodecH264:
  78. case CodecH265:
  79. return mk_frame_create_complex(codec_id, dts, pts, 0, prefixSize(data, size), (char *)data, size, cb, user_data);
  80. case CodecAAC: {
  81. int prefix = 0;
  82. if ((((uint8_t *) data)[0] == 0xFF && (((uint8_t *) data)[1] & 0xF0) == 0xF0) && size > ADTS_HEADER_LEN) {
  83. prefix = ADTS_HEADER_LEN;
  84. }
  85. return mk_frame_create_complex(codec_id, dts, pts, 0, prefix, (char *)data, size, cb, user_data);
  86. }
  87. default:
  88. return mk_frame_create_complex(codec_id, dts, pts, 0, 0, (char *)data, size, cb, user_data);
  89. }
  90. }
  91. API_EXPORT void API_CALL mk_frame_unref(mk_frame frame) {
  92. assert(frame);
  93. delete (Frame::Ptr *) frame;
  94. }
  95. API_EXPORT mk_frame API_CALL mk_frame_ref(mk_frame frame) {
  96. assert(frame);
  97. return new Frame::Ptr(Frame::getCacheAbleFrame(*((Frame::Ptr *) frame)));
  98. }
  99. API_EXPORT int API_CALL mk_frame_codec_id(mk_frame frame) {
  100. assert(frame);
  101. return (*((Frame::Ptr *) frame))->getCodecId();
  102. }
  103. API_EXPORT const char *API_CALL mk_frame_codec_name(mk_frame frame) {
  104. assert(frame);
  105. return (*((Frame::Ptr *) frame))->getCodecName();
  106. }
  107. API_EXPORT int API_CALL mk_frame_is_video(mk_frame frame) {
  108. assert(frame);
  109. return (*((Frame::Ptr *) frame))->getTrackType() == TrackVideo;
  110. }
  111. API_EXPORT const char *API_CALL mk_frame_get_data(mk_frame frame) {
  112. assert(frame);
  113. return (*((Frame::Ptr *) frame))->data();
  114. }
  115. API_EXPORT size_t API_CALL mk_frame_get_data_size(mk_frame frame) {
  116. assert(frame);
  117. return (*((Frame::Ptr *) frame))->size();
  118. }
  119. API_EXPORT size_t API_CALL mk_frame_get_data_prefix_size(mk_frame frame) {
  120. assert(frame);
  121. return (*((Frame::Ptr *) frame))->prefixSize();
  122. }
  123. API_EXPORT uint64_t API_CALL mk_frame_get_dts(mk_frame frame) {
  124. assert(frame);
  125. return (*((Frame::Ptr *) frame))->dts();
  126. }
  127. API_EXPORT uint64_t API_CALL mk_frame_get_pts(mk_frame frame) {
  128. assert(frame);
  129. return (*((Frame::Ptr *) frame))->pts();
  130. }
  131. API_EXPORT uint32_t API_CALL mk_frame_get_flags(mk_frame frame) {
  132. assert(frame);
  133. auto &ref = *((Frame::Ptr *) frame);
  134. uint32_t ret = 0;
  135. if (ref->keyFrame()) {
  136. ret |= MK_FRAME_FLAG_IS_KEY;
  137. }
  138. if (ref->configFrame()) {
  139. ret |= MK_FRAME_FLAG_IS_CONFIG;
  140. }
  141. if (ref->dropAble()) {
  142. ret |= MK_FRAME_FLAG_DROP_ABLE;
  143. }
  144. if (!ref->decodeAble()) {
  145. ret |= MK_FRAME_FLAG_NOT_DECODE_ABLE;
  146. }
  147. return ret;
  148. }