Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

197 linhas
5.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 "mk_track.h"
  11. #include "Extension/Track.h"
  12. #include "Extension/Factory.h"
  13. using namespace std;
  14. using namespace toolkit;
  15. using namespace mediakit;
  16. class VideoTrackForC : public VideoTrack, public std::enable_shared_from_this<VideoTrackForC> {
  17. public:
  18. VideoTrackForC(int codec_id, codec_args *args) {
  19. _codec_id = (CodecId) codec_id;
  20. if (args) {
  21. _args = *args;
  22. } else {
  23. memset(&_args, 0, sizeof(_args));
  24. }
  25. }
  26. ~VideoTrackForC() override = default;
  27. int getVideoHeight() const override {
  28. return _args.video.height;
  29. }
  30. int getVideoWidth() const override {
  31. return _args.video.width;
  32. }
  33. float getVideoFps() const override {
  34. return _args.video.fps;
  35. }
  36. CodecId getCodecId() const override {
  37. return _codec_id;
  38. }
  39. bool ready() override {
  40. return true;
  41. }
  42. Track::Ptr clone() override {
  43. auto track_in = std::shared_ptr<Track>(shared_from_this());
  44. return Factory::getTrackByAbstractTrack(track_in);
  45. }
  46. Sdp::Ptr getSdp() override {
  47. return nullptr;
  48. }
  49. private:
  50. CodecId _codec_id;
  51. codec_args _args;
  52. };
  53. class AudioTrackForC : public AudioTrackImp, public std::enable_shared_from_this<AudioTrackForC> {
  54. public:
  55. ~AudioTrackForC() override = default;
  56. AudioTrackForC(int codec_id, codec_args *args) :
  57. AudioTrackImp((CodecId) codec_id, args->audio.sample_rate, args->audio.channels, 16) {}
  58. Track::Ptr clone() override {
  59. auto track_in = std::shared_ptr<Track>(shared_from_this());
  60. return Factory::getTrackByAbstractTrack(track_in);
  61. }
  62. Sdp::Ptr getSdp() override {
  63. return nullptr;
  64. }
  65. };
  66. API_EXPORT mk_track API_CALL mk_track_create(int codec_id, codec_args *args) {
  67. switch (getTrackType((CodecId) codec_id)) {
  68. case TrackVideo: return new Track::Ptr(std::make_shared<VideoTrackForC>(codec_id, args));
  69. case TrackAudio: return new Track::Ptr(std::make_shared<AudioTrackForC>(codec_id, args));
  70. default: WarnL << "unrecognized codec:" << codec_id; return nullptr;
  71. }
  72. }
  73. API_EXPORT void API_CALL mk_track_unref(mk_track track) {
  74. assert(track);
  75. delete (Track::Ptr *)track;
  76. }
  77. API_EXPORT mk_track API_CALL mk_track_ref(mk_track track) {
  78. assert(track);
  79. return new Track::Ptr(*( (Track::Ptr *)track));
  80. }
  81. API_EXPORT int API_CALL mk_track_codec_id(mk_track track) {
  82. assert(track);
  83. return (*((Track::Ptr *) track))->getCodecId();
  84. }
  85. API_EXPORT const char *API_CALL mk_track_codec_name(mk_track track) {
  86. assert(track);
  87. return (*((Track::Ptr *) track))->getCodecName();
  88. }
  89. API_EXPORT int API_CALL mk_track_bit_rate(mk_track track) {
  90. assert(track);
  91. return (*((Track::Ptr *) track))->getBitRate();
  92. }
  93. API_EXPORT void *API_CALL mk_track_add_delegate(mk_track track, on_mk_frame_out cb, void *user_data) {
  94. assert(track && cb);
  95. return (*((Track::Ptr *) track))->addDelegate([cb, user_data](const Frame::Ptr &frame) {
  96. cb(user_data, (mk_frame) &frame);
  97. return true;
  98. });
  99. }
  100. API_EXPORT void API_CALL mk_track_del_delegate(mk_track track, void *tag) {
  101. assert(track && tag);
  102. (*((Track::Ptr *) track))->delDelegate((FrameWriterInterface *) tag);
  103. }
  104. API_EXPORT void API_CALL mk_track_input_frame(mk_track track, mk_frame frame) {
  105. assert(track && frame);
  106. (*((Track::Ptr *) track))->inputFrame((*((Frame::Ptr *) frame)));
  107. }
  108. API_EXPORT int API_CALL mk_track_is_video(mk_track track) {
  109. assert(track);
  110. return (*((Track::Ptr *) track))->getTrackType() == TrackVideo;
  111. }
  112. API_EXPORT int API_CALL mk_track_video_width(mk_track track) {
  113. assert(track);
  114. auto video = dynamic_pointer_cast<VideoTrack>((*((Track::Ptr *) track)));
  115. if (video) {
  116. return video->getVideoWidth();
  117. }
  118. WarnL << "not video track";
  119. return 0;
  120. }
  121. API_EXPORT int API_CALL mk_track_video_height(mk_track track) {
  122. assert(track);
  123. auto video = dynamic_pointer_cast<VideoTrack>((*((Track::Ptr *) track)));
  124. if (video) {
  125. return video->getVideoHeight();
  126. }
  127. WarnL << "not video track";
  128. return 0;
  129. }
  130. API_EXPORT int API_CALL mk_track_video_fps(mk_track track) {
  131. assert(track);
  132. auto video = dynamic_pointer_cast<VideoTrack>((*((Track::Ptr *) track)));
  133. if (video) {
  134. return video->getVideoFps();
  135. }
  136. WarnL << "not video track";
  137. return 0;
  138. }
  139. API_EXPORT int API_CALL mk_track_audio_sample_rate(mk_track track) {
  140. assert(track);
  141. auto audio = dynamic_pointer_cast<AudioTrack>((*((Track::Ptr *) track)));
  142. if (audio) {
  143. return audio->getAudioSampleRate();
  144. }
  145. WarnL << "not audio track";
  146. return 0;
  147. }
  148. API_EXPORT int API_CALL mk_track_audio_channel(mk_track track) {
  149. assert(track);
  150. auto audio = dynamic_pointer_cast<AudioTrack>((*((Track::Ptr *) track)));
  151. if (audio) {
  152. return audio->getAudioChannel();
  153. }
  154. WarnL << "not audio track";
  155. return 0;
  156. }
  157. API_EXPORT int API_CALL mk_track_audio_sample_bit(mk_track track) {
  158. assert(track);
  159. auto audio = dynamic_pointer_cast<AudioTrack>((*((Track::Ptr *) track)));
  160. if (audio) {
  161. return audio->getAudioSampleBit();
  162. }
  163. WarnL << "not audio track";
  164. return 0;
  165. }