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.

115 rindas
4.6KB

  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 <signal.h>
  11. #include <iostream>
  12. #include "Util/logger.h"
  13. #include "Util/NoticeCenter.h"
  14. #include "Poller/EventPoller.h"
  15. #include "Player/PlayerProxy.h"
  16. #include "Rtmp/RtmpPusher.h"
  17. #include "Common/config.h"
  18. #include "Pusher/MediaPusher.h"
  19. using namespace std;
  20. using namespace toolkit;
  21. using namespace mediakit;
  22. //推流器,保持强引用
  23. MediaPusher::Ptr pusher;
  24. Timer::Ptr g_timer;
  25. //声明函数
  26. void rePushDelay(const EventPoller::Ptr &poller,const string &schema,const string &vhost,const string &app, const string &stream, const string &url);
  27. //创建推流器并开始推流
  28. void createPusher(const EventPoller::Ptr &poller, const string &schema,const string &vhost,const string &app, const string &stream, const string &url) {
  29. //创建推流器并绑定一个MediaSource
  30. pusher.reset(new MediaPusher(schema,vhost, app, stream,poller));
  31. //可以指定rtsp推流方式,支持tcp和udp方式,默认tcp
  32. // (*pusher)[Client::kRtpType] = Rtsp::RTP_UDP;
  33. //设置推流中断处理逻辑
  34. pusher->setOnShutdown([poller,schema,vhost, app, stream, url](const SockException &ex) {
  35. WarnL << "Server connection is closed:" << ex.getErrCode() << " " << ex.what();
  36. //重试
  37. rePushDelay(poller,schema,vhost,app, stream, url);
  38. });
  39. //设置发布结果处理逻辑
  40. pusher->setOnPublished([poller,schema,vhost, app, stream, url](const SockException &ex) {
  41. if (ex) {
  42. WarnL << "Publish fail:" << ex.getErrCode() << " " << ex.what();
  43. //如果发布失败,就重试
  44. rePushDelay(poller,schema,vhost,app, stream, url);
  45. } else {
  46. InfoL << "Publish success,Please play with player:" << url;
  47. }
  48. });
  49. pusher->publish(url);
  50. }
  51. //推流失败或断开延迟2秒后重试推流
  52. void rePushDelay(const EventPoller::Ptr &poller,const string &schema,const string &vhost,const string &app, const string &stream, const string &url) {
  53. g_timer = std::make_shared<Timer>(2.0f,[poller,schema,vhost,app, stream, url]() {
  54. InfoL << "Re-Publishing...";
  55. //重新推流
  56. createPusher(poller,schema,vhost,app, stream, url);
  57. //此任务不重复
  58. return false;
  59. }, poller);
  60. }
  61. //这里才是真正执行main函数,你可以把函数名(domain)改成main,然后就可以输入自定义url了
  62. int domain(const string &playUrl, const string &pushUrl) {
  63. //设置日志
  64. Logger::Instance().add(std::make_shared<ConsoleChannel>());
  65. Logger::Instance().setWriter(std::make_shared<AsyncLogWriter>());
  66. auto poller = EventPollerPool::Instance().getPoller();
  67. //拉一个流,生成一个RtmpMediaSource,源的名称是"app/stream"
  68. //你也可以以其他方式生成RtmpMediaSource,比如说MP4文件(请查看test_rtmpPusherMp4.cpp代码)
  69. MediaInfo info(pushUrl);
  70. ProtocolOption option;
  71. option.enable_hls = false;
  72. option.enable_mp4 = false;
  73. PlayerProxy::Ptr player(new PlayerProxy(DEFAULT_VHOST, "app", "stream", option, -1, poller));
  74. //可以指定rtsp拉流方式,支持tcp和udp方式,默认tcp
  75. // (*player)[Client::kRtpType] = Rtsp::RTP_UDP;
  76. player->play(playUrl.data());
  77. //监听RtmpMediaSource注册事件,在PlayerProxy播放成功后触发
  78. NoticeCenter::Instance().addListener(nullptr, Broadcast::kBroadcastMediaChanged,
  79. [pushUrl,poller](BroadcastMediaChangedArgs) {
  80. //媒体源"app/stream"已经注册,这时方可新建一个RtmpPusher对象并绑定该媒体源
  81. if(bRegist && pushUrl.find(sender.getSchema()) == 0){
  82. createPusher(poller,sender.getSchema(),sender.getVhost(),sender.getApp(), sender.getId(), pushUrl);
  83. }
  84. });
  85. //设置退出信号处理函数
  86. static semaphore sem;
  87. signal(SIGINT, [](int) { sem.post(); });// 设置退出信号
  88. sem.wait();
  89. pusher.reset();
  90. g_timer.reset();
  91. return 0;
  92. }
  93. int main(int argc, char *argv[]) {
  94. return domain("rtmp://live.hkstv.hk.lxdns.com/live/hks1", "rtsp://127.0.0.1/live/rtsp_push");
  95. }