25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

mk_h264_splitter.cpp 2.4KB

8 달 전
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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_h264_splitter.h"
  11. #include "Http/HttpRequestSplitter.h"
  12. using namespace mediakit;
  13. class H264Splitter : public HttpRequestSplitter {
  14. public:
  15. using onH264 = std::function<void(const char *data, size_t len)>;
  16. H264Splitter() = default;
  17. ~H264Splitter() override;
  18. void setOnSplitted(onH264 cb);
  19. protected:
  20. ssize_t onRecvHeader(const char *data, size_t len) override;
  21. const char *onSearchPacketTail(const char *data, size_t len) override;
  22. private:
  23. onH264 _cb;
  24. };
  25. void H264Splitter::setOnSplitted(H264Splitter::onH264 cb) {
  26. _cb = std::move(cb);
  27. }
  28. H264Splitter::~H264Splitter() {
  29. if (remainDataSize()) {
  30. _cb(remainData(), remainDataSize());
  31. }
  32. }
  33. ssize_t H264Splitter::onRecvHeader(const char *data, size_t len) {
  34. _cb(data, len);
  35. return 0;
  36. }
  37. const char *H264Splitter::onSearchPacketTail(const char *data, size_t len) {
  38. for (size_t i = 2; len > 2 && i < len - 2; ++i) {
  39. //判断0x00 00 01
  40. if (data[i] == 0 && data[i + 1] == 0 && data[i + 2] == 1) {
  41. if (i > 0 && data[i - 1] == 0) {
  42. //找到0x00 00 00 01
  43. return data + i - 1;
  44. }
  45. return data + i;
  46. }
  47. }
  48. return nullptr;
  49. }
  50. ////////////////////////////////////////////////////////////////////////////////////////////////////////
  51. API_EXPORT mk_h264_splitter API_CALL mk_h264_splitter_create(on_mk_h264_splitter_frame cb, void *user_data) {
  52. assert(cb);
  53. auto ptr = new H264Splitter();
  54. ptr->setOnSplitted([cb, ptr, user_data](const char *data, size_t len) {
  55. cb(user_data, reinterpret_cast<mk_h264_splitter>(ptr), data, len);
  56. });
  57. return reinterpret_cast<mk_h264_splitter>(ptr);
  58. }
  59. API_EXPORT void API_CALL mk_h264_splitter_release(mk_h264_splitter ctx){
  60. assert(ctx);
  61. auto ptr = reinterpret_cast<H264Splitter *>(ctx);
  62. delete ptr;
  63. }
  64. API_EXPORT void API_CALL mk_h264_splitter_input_data(mk_h264_splitter ctx, const char *data, int size) {
  65. assert(ctx && data && size);
  66. auto ptr = reinterpret_cast<H264Splitter *>(ctx);
  67. ptr->input(data, size);
  68. }