You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

215 lines
6.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_player.h"
  11. #include "Util/logger.h"
  12. #include "Player/MediaPlayer.h"
  13. #include "Extension/H264.h"
  14. using namespace std;
  15. using namespace toolkit;
  16. using namespace mediakit;
  17. class MediaPlayerForC : public std::enable_shared_from_this<MediaPlayerForC>{
  18. public:
  19. using Ptr = std::shared_ptr<MediaPlayerForC>;
  20. MediaPlayerForC(){
  21. _player = std::make_shared<MediaPlayer>();
  22. }
  23. ~MediaPlayerForC(){}
  24. MediaPlayer *operator->(){
  25. return _player.get();
  26. }
  27. void setup(){
  28. weak_ptr<MediaPlayerForC> weak_self = shared_from_this();
  29. _player->setOnPlayResult([weak_self](const SockException &ex){
  30. auto strong_self = weak_self.lock();
  31. if(strong_self){
  32. strong_self->onEvent(false,ex);
  33. }
  34. });
  35. _player->setOnShutdown([weak_self](const SockException &ex){
  36. auto strong_self = weak_self.lock();
  37. if(strong_self){
  38. strong_self->onEvent(true,ex);
  39. }
  40. });
  41. }
  42. void unset() {
  43. for (auto &track : _player->getTracks(false)) {
  44. track->clear();
  45. }
  46. lock_guard<recursive_mutex> lck(_mtx);
  47. _on_play = nullptr;
  48. _on_shutdown = nullptr;
  49. }
  50. void onEvent(bool is_shutdown, const SockException &ex){
  51. lock_guard<recursive_mutex> lck(_mtx);
  52. if (is_shutdown) {
  53. //播放中断
  54. if (_on_shutdown) {
  55. _on_shutdown(_on_shutdown_data, ex.getErrCode(), ex.what(), nullptr, 0);
  56. }
  57. return;
  58. }
  59. //播放结果
  60. if (_on_play) {
  61. auto cpp_tracks = _player->getTracks(false);
  62. mk_track tracks[TrackMax] = {nullptr};
  63. int track_count = 0;
  64. for (auto &track : cpp_tracks) {
  65. tracks[track_count++] = (mk_track) &track;
  66. }
  67. _on_play(_on_play_data, ex.getErrCode(), ex.what(), tracks, track_count);
  68. }
  69. }
  70. void setOnEvent(on_mk_play_event cb, void *user_data, int type) {
  71. lock_guard<recursive_mutex> lck(_mtx);
  72. if(type == 0){
  73. _on_play_data = user_data;
  74. _on_play = cb;
  75. }else{
  76. _on_shutdown_data = user_data;
  77. _on_shutdown = cb;
  78. }
  79. }
  80. MediaPlayer::Ptr& getPlayer(){
  81. return _player;
  82. }
  83. private:
  84. MediaPlayer::Ptr _player;
  85. recursive_mutex _mtx;
  86. on_mk_play_event _on_play = nullptr;
  87. on_mk_play_event _on_shutdown = nullptr;
  88. void *_on_play_data = nullptr;
  89. void *_on_shutdown_data = nullptr;
  90. };
  91. API_EXPORT mk_player API_CALL mk_player_create() {
  92. MediaPlayerForC::Ptr *obj = new MediaPlayerForC::Ptr(new MediaPlayerForC());
  93. (*obj)->setup();
  94. return obj;
  95. }
  96. API_EXPORT void API_CALL mk_player_release(mk_player ctx) {
  97. assert(ctx);
  98. MediaPlayerForC::Ptr *obj = (MediaPlayerForC::Ptr *)ctx;
  99. (*obj)->unset();
  100. delete obj;
  101. }
  102. API_EXPORT void API_CALL mk_player_set_option(mk_player ctx,const char* key,const char *val){
  103. assert(ctx && key && val);
  104. MediaPlayerForC &obj = **((MediaPlayerForC::Ptr *)ctx);
  105. auto player = obj.getPlayer();
  106. string key_str(key), val_str(val);
  107. player->getPoller()->async([key_str,val_str,player](){
  108. //切换线程后再操作
  109. (*player)[key_str] = val_str;
  110. });
  111. }
  112. API_EXPORT void API_CALL mk_player_play(mk_player ctx, const char *url) {
  113. assert(ctx && url);
  114. MediaPlayerForC &obj = **((MediaPlayerForC::Ptr *)ctx);
  115. auto player = obj.getPlayer();
  116. string url_str(url);
  117. player->getPoller()->async([url_str,player](){
  118. //切换线程后再操作
  119. player->play(url_str);
  120. });
  121. }
  122. API_EXPORT void API_CALL mk_player_pause(mk_player ctx, int pause) {
  123. assert(ctx);
  124. MediaPlayerForC &obj = **((MediaPlayerForC::Ptr *)ctx);
  125. auto player = obj.getPlayer();
  126. player->getPoller()->async([pause,player](){
  127. //切换线程后再操作
  128. player->pause(pause);
  129. });
  130. }
  131. API_EXPORT void API_CALL mk_player_speed(mk_player ctx, float speed) {
  132. assert(ctx);
  133. MediaPlayerForC &obj = **((MediaPlayerForC::Ptr *) ctx);
  134. auto player = obj.getPlayer();
  135. player->getPoller()->async([speed, player]() {
  136. //切换线程后再操作
  137. player->speed(speed);
  138. });
  139. }
  140. API_EXPORT void API_CALL mk_player_seekto(mk_player ctx, float progress) {
  141. assert(ctx);
  142. MediaPlayerForC &obj = **((MediaPlayerForC::Ptr *)ctx);
  143. auto player = obj.getPlayer();
  144. player->getPoller()->async([progress,player](){
  145. //切换线程后再操作
  146. player->seekTo(progress);
  147. });
  148. }
  149. API_EXPORT void API_CALL mk_player_seekto_pos(mk_player ctx, int seek_pos) {
  150. assert(ctx);
  151. MediaPlayerForC &obj = **((MediaPlayerForC::Ptr *) ctx);
  152. auto player = obj.getPlayer();
  153. player->getPoller()->async([seek_pos, player]() {
  154. //切换线程后再操作
  155. player->seekTo((uint32_t) seek_pos);
  156. });
  157. }
  158. static void mk_player_set_on_event(mk_player ctx, on_mk_play_event cb, void *user_data, int type) {
  159. assert(ctx);
  160. MediaPlayerForC &obj = **((MediaPlayerForC::Ptr *)ctx);
  161. obj.setOnEvent(cb,user_data, type);
  162. }
  163. API_EXPORT void API_CALL mk_player_set_on_result(mk_player ctx, on_mk_play_event cb, void *user_data) {
  164. mk_player_set_on_event(ctx,cb,user_data,0);
  165. }
  166. API_EXPORT void API_CALL mk_player_set_on_shutdown(mk_player ctx, on_mk_play_event cb, void *user_data) {
  167. mk_player_set_on_event(ctx,cb,user_data,1);
  168. }
  169. API_EXPORT float API_CALL mk_player_duration(mk_player ctx) {
  170. assert(ctx);
  171. MediaPlayerForC &obj = **((MediaPlayerForC::Ptr *)ctx);
  172. return obj->getDuration();
  173. }
  174. API_EXPORT float API_CALL mk_player_progress(mk_player ctx) {
  175. assert(ctx);
  176. MediaPlayerForC &obj = **((MediaPlayerForC::Ptr *)ctx);
  177. return obj->getProgress();
  178. }
  179. API_EXPORT int API_CALL mk_player_progress_pos(mk_player ctx) {
  180. assert(ctx);
  181. MediaPlayerForC &obj = **((MediaPlayerForC::Ptr *) ctx);
  182. return obj->getProgressPos();
  183. }
  184. API_EXPORT float API_CALL mk_player_loss_rate(mk_player ctx, int track_type) {
  185. assert(ctx);
  186. MediaPlayerForC &obj = **((MediaPlayerForC::Ptr *)ctx);
  187. return obj->getPacketLossRate((TrackType)track_type);
  188. }