2019-08-08 19:01:45 +08:00
|
|
|
|
/*
|
2023-12-09 16:23:51 +08:00
|
|
|
|
* Copyright (c) 2016-present The ZLMediaKit project authors. All Rights Reserved.
|
2020-04-04 20:30:09 +08:00
|
|
|
|
*
|
2023-12-09 16:23:51 +08:00
|
|
|
|
* This file is part of ZLMediaKit(https://github.com/ZLMediaKit/ZLMediaKit).
|
2020-04-04 20:30:09 +08:00
|
|
|
|
*
|
2023-12-09 16:23:51 +08:00
|
|
|
|
* Use of this source code is governed by MIT-like license that can be found in the
|
2020-04-04 20:30:09 +08:00
|
|
|
|
* LICENSE file in the root of the source tree. All contributing project authors
|
|
|
|
|
* may be found in the AUTHORS file in the root of the source tree.
|
|
|
|
|
*/
|
2019-03-27 18:41:52 +08:00
|
|
|
|
|
|
|
|
|
#ifndef SRC_PUSHER_PUSHERBASE_H_
|
|
|
|
|
#define SRC_PUSHER_PUSHERBASE_H_
|
|
|
|
|
|
|
|
|
|
#include <map>
|
|
|
|
|
#include <memory>
|
|
|
|
|
#include <string>
|
|
|
|
|
#include <functional>
|
|
|
|
|
#include "Network/Socket.h"
|
|
|
|
|
#include "Util/mini.h"
|
|
|
|
|
#include "Common/MediaSource.h"
|
|
|
|
|
|
|
|
|
|
namespace mediakit {
|
|
|
|
|
|
2022-02-02 20:34:50 +08:00
|
|
|
|
class PusherBase : public toolkit::mINI {
|
2019-03-27 18:41:52 +08:00
|
|
|
|
public:
|
2021-11-09 15:32:48 +08:00
|
|
|
|
using Ptr = std::shared_ptr<PusherBase>;
|
2022-02-02 20:34:50 +08:00
|
|
|
|
using Event = std::function<void(const toolkit::SockException &ex)>;
|
2019-03-27 18:41:52 +08:00
|
|
|
|
|
2022-02-02 20:34:50 +08:00
|
|
|
|
static Ptr createPusher(const toolkit::EventPoller::Ptr &poller,
|
2019-04-01 10:16:15 +08:00
|
|
|
|
const MediaSource::Ptr &src,
|
2022-02-02 20:34:50 +08:00
|
|
|
|
const std::string &strUrl);
|
2019-03-27 18:41:52 +08:00
|
|
|
|
|
|
|
|
|
PusherBase();
|
2021-11-09 15:32:48 +08:00
|
|
|
|
virtual ~PusherBase() = default;
|
2019-03-27 18:41:52 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 开始推流
|
|
|
|
|
* @param strUrl 视频url,支持rtsp/rtmp
|
|
|
|
|
*/
|
2022-02-02 20:34:50 +08:00
|
|
|
|
virtual void publish(const std::string &strUrl) {};
|
2019-03-27 18:41:52 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 中断推流
|
|
|
|
|
*/
|
2021-11-09 16:46:38 +08:00
|
|
|
|
virtual void teardown() {};
|
2019-03-27 18:41:52 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 摄像推流结果回调
|
|
|
|
|
*/
|
|
|
|
|
virtual void setOnPublished(const Event &cb) = 0;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 设置断开回调
|
|
|
|
|
*/
|
|
|
|
|
virtual void setOnShutdown(const Event &cb) = 0;
|
2021-11-09 16:46:38 +08:00
|
|
|
|
|
|
|
|
|
protected:
|
2022-02-02 20:34:50 +08:00
|
|
|
|
virtual void onShutdown(const toolkit::SockException &ex) = 0;
|
|
|
|
|
virtual void onPublishResult(const toolkit::SockException &ex) = 0;
|
2019-03-27 18:41:52 +08:00
|
|
|
|
};
|
|
|
|
|
|
2021-11-09 15:32:48 +08:00
|
|
|
|
template<typename Parent, typename Delegate>
|
2019-03-27 18:41:52 +08:00
|
|
|
|
class PusherImp : public Parent {
|
|
|
|
|
public:
|
2021-11-09 15:32:48 +08:00
|
|
|
|
using Ptr = std::shared_ptr<PusherImp>;
|
2019-04-01 10:16:15 +08:00
|
|
|
|
|
|
|
|
|
template<typename ...ArgsType>
|
2021-11-09 15:32:48 +08:00
|
|
|
|
PusherImp(ArgsType &&...args) : Parent(std::forward<ArgsType>(args)...) {}
|
2019-03-27 18:41:52 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 开始推流
|
2021-11-09 16:46:38 +08:00
|
|
|
|
* @param url 推流url,支持rtsp/rtmp
|
2019-03-27 18:41:52 +08:00
|
|
|
|
*/
|
2022-02-02 20:34:50 +08:00
|
|
|
|
void publish(const std::string &url) override {
|
2021-11-09 16:46:38 +08:00
|
|
|
|
return _delegate ? _delegate->publish(url) : Parent::publish(url);
|
2019-03-27 18:41:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 中断推流
|
|
|
|
|
*/
|
2021-11-09 15:32:48 +08:00
|
|
|
|
void teardown() override {
|
2021-11-09 16:46:38 +08:00
|
|
|
|
return _delegate ? _delegate->teardown() : Parent::teardown();
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-02 20:34:50 +08:00
|
|
|
|
std::shared_ptr<toolkit::SockInfo> getSockInfo() const {
|
|
|
|
|
return std::dynamic_pointer_cast<toolkit::SockInfo>(_delegate);
|
2019-03-27 18:41:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 摄像推流结果回调
|
|
|
|
|
*/
|
2021-11-09 15:32:48 +08:00
|
|
|
|
void setOnPublished(const PusherBase::Event &cb) override {
|
2019-12-04 10:59:13 +08:00
|
|
|
|
if (_delegate) {
|
|
|
|
|
_delegate->setOnPublished(cb);
|
2019-03-27 18:41:52 +08:00
|
|
|
|
}
|
2021-11-09 15:32:48 +08:00
|
|
|
|
_on_publish = cb;
|
2019-03-27 18:41:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 设置断开回调
|
|
|
|
|
*/
|
2021-11-09 15:32:48 +08:00
|
|
|
|
void setOnShutdown(const PusherBase::Event &cb) override {
|
2019-12-04 10:59:13 +08:00
|
|
|
|
if (_delegate) {
|
|
|
|
|
_delegate->setOnShutdown(cb);
|
2019-03-27 18:41:52 +08:00
|
|
|
|
}
|
2021-11-09 15:32:48 +08:00
|
|
|
|
_on_shutdown = cb;
|
2019-03-27 18:41:52 +08:00
|
|
|
|
}
|
2020-09-27 11:32:49 +08:00
|
|
|
|
|
2021-11-09 16:46:38 +08:00
|
|
|
|
protected:
|
2022-02-02 20:34:50 +08:00
|
|
|
|
void onShutdown(const toolkit::SockException &ex) override {
|
2021-11-09 16:46:38 +08:00
|
|
|
|
if (_on_shutdown) {
|
|
|
|
|
_on_shutdown(ex);
|
|
|
|
|
_on_shutdown = nullptr;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-02 20:34:50 +08:00
|
|
|
|
void onPublishResult(const toolkit::SockException &ex) override {
|
2021-11-09 16:46:38 +08:00
|
|
|
|
if (_on_publish) {
|
|
|
|
|
_on_publish(ex);
|
|
|
|
|
_on_publish = nullptr;
|
|
|
|
|
}
|
2020-09-27 11:32:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-03-27 18:41:52 +08:00
|
|
|
|
protected:
|
2021-11-09 15:32:48 +08:00
|
|
|
|
PusherBase::Event _on_shutdown;
|
|
|
|
|
PusherBase::Event _on_publish;
|
2019-12-04 10:59:13 +08:00
|
|
|
|
std::shared_ptr<Delegate> _delegate;
|
2019-03-27 18:41:52 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} /* namespace mediakit */
|
|
|
|
|
#endif /* SRC_PUSHER_PUSHERBASE_H_ */
|