PlayerProxy.h 2.46 KB
Newer Older
xiongziliang committed
1
/*
xiongziliang committed
2
 * Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
xiongziliang committed
3
 *
4
 * This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
xiongziliang committed
5
 *
xiongziliang committed
6 7 8
 * Use of this source code is governed by MIT license that can be found in the
 * 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.
xzl committed
9 10 11 12 13 14
 */

#ifndef SRC_DEVICE_PLAYERPROXY_H_
#define SRC_DEVICE_PLAYERPROXY_H_

#include <memory>
xiongziliang committed
15
#include "Common/Device.h"
xzl committed
16 17 18
#include "Player/MediaPlayer.h"
#include "Util/TimeTicker.h"
using namespace std;
xiongziliang committed
19
using namespace toolkit;
xzl committed
20

xiongziliang committed
21
namespace mediakit {
xzl committed
22

23
class PlayerProxy : public MediaPlayer, public MediaSourceEvent, public std::enable_shared_from_this<PlayerProxy> {
xzl committed
24
public:
25
    typedef std::shared_ptr<PlayerProxy> Ptr;
xiongziliang committed
26

27
    //如果retry_count<0,则一直重试播放;否则重试retry_count次数
28
    //默认一直重试
29
    PlayerProxy(const string &vhost, const string &app, const string &stream_id,
30
                bool enable_hls = true, bool enable_mp4 = false,
31
                int retry_count = -1, const EventPoller::Ptr &poller = nullptr);
32

33
    ~PlayerProxy() override;
34

35 36 37 38 39 40
    /**
     * 设置play结果回调,只触发一次;在play执行之前有效
     * @param cb
     */
    void setPlayCallbackOnce(const function<void(const SockException &ex)> &cb);

41 42 43 44 45
    /**
     * 设置主动关闭回调
     * @param cb
     */
    void setOnClose(const function<void()> &cb);
46

47 48 49 50 51
    /**
     * 开始拉流播放
     * @param strUrl
     */
    void play(const string &strUrl) override;
52 53 54 55 56

    /**
     * 获取观看总人数
     */
    int totalReaderCount() ;
57

58
private:
59 60 61
    //MediaSourceEvent override
    bool close(MediaSource &sender,bool force) override;
    int totalReaderCount(MediaSource &sender) override;
62 63 64 65
    MediaOriginType getOriginType(MediaSource &sender) const override;
    string getOriginUrl(MediaSource &sender) const override;
    std::shared_ptr<SockInfo> getOriginSock(MediaSource &sender) const override;

66 67
    void rePlay(const string &strUrl,int iFailedCnt);
    void onPlaySuccess();
68
    void setDirectProxy();
69

70
private:
71 72 73 74 75 76
    bool _enable_hls;
    bool _enable_mp4;
    int _retry_count;
    string _vhost;
    string _app;
    string _stream_id;
77
    string _pull_url;
78
    Timer::Ptr _timer;
79 80 81
    function<void()> _on_close;
    function<void(const SockException &ex)> _on_play;
    MultiMediaSourceMuxer::Ptr _muxer;
xzl committed
82 83
};

xiongziliang committed
84
} /* namespace mediakit */
xzl committed
85
#endif /* SRC_DEVICE_PLAYERPROXY_H_ */