2020-05-18 15:31:49 +08:00
|
|
|
|
/*
|
2020-05-17 18:00:37 +08:00
|
|
|
|
* Copyright (c) 2020 The ZLMediaKit project authors. All Rights Reserved.
|
|
|
|
|
*
|
2021-01-17 18:31:50 +08:00
|
|
|
|
* This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
|
2020-05-17 18:00:37 +08:00
|
|
|
|
*
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#ifndef HTTP_HLSPLAYER_H
|
|
|
|
|
#define HTTP_HLSPLAYER_H
|
|
|
|
|
|
2022-01-20 15:39:40 +08:00
|
|
|
|
#include "Common/Stamp.h"
|
|
|
|
|
#include "Player/PlayerBase.h"
|
2020-05-17 18:00:37 +08:00
|
|
|
|
#include "HttpTSPlayer.h"
|
2022-01-20 15:39:40 +08:00
|
|
|
|
#include "HlsParser.h"
|
2020-05-17 18:00:37 +08:00
|
|
|
|
#include "Rtp/TSDecoder.h"
|
|
|
|
|
|
|
|
|
|
using namespace toolkit;
|
2021-12-01 22:09:05 +08:00
|
|
|
|
|
2020-05-17 18:00:37 +08:00
|
|
|
|
namespace mediakit {
|
|
|
|
|
|
2022-01-20 14:48:45 +08:00
|
|
|
|
class HlsDemuxer
|
|
|
|
|
: public MediaSinkInterface
|
|
|
|
|
, public TrackSource
|
|
|
|
|
, public std::enable_shared_from_this<HlsDemuxer> {
|
|
|
|
|
public:
|
|
|
|
|
HlsDemuxer() = default;
|
|
|
|
|
~HlsDemuxer() override { _timer = nullptr; }
|
|
|
|
|
|
|
|
|
|
void start(const EventPoller::Ptr &poller, TrackListener *listener);
|
|
|
|
|
bool inputFrame(const Frame::Ptr &frame) override;
|
|
|
|
|
bool addTrack(const Track::Ptr &track) override { return _delegate.addTrack(track); }
|
|
|
|
|
void addTrackCompleted() override { _delegate.addTrackCompleted(); }
|
|
|
|
|
void resetTracks() override { ((MediaSink &)_delegate).resetTracks(); }
|
|
|
|
|
vector<Track::Ptr> getTracks(bool ready = true) const override { return _delegate.getTracks(ready); }
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
void onTick();
|
|
|
|
|
int64_t getBufferMS();
|
|
|
|
|
int64_t getPlayPosition();
|
|
|
|
|
void setPlayPosition(int64_t pos);
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
int64_t _ticker_offset = 0;
|
|
|
|
|
Ticker _ticker;
|
|
|
|
|
Stamp _stamp[2];
|
|
|
|
|
Timer::Ptr _timer;
|
|
|
|
|
MediaSinkDelegate _delegate;
|
|
|
|
|
multimap<int64_t, Frame::Ptr> _frame_cache;
|
|
|
|
|
};
|
|
|
|
|
|
2020-05-17 18:00:37 +08:00
|
|
|
|
class HlsPlayer : public HttpClientImp , public PlayerBase , public HlsParser{
|
|
|
|
|
public:
|
|
|
|
|
HlsPlayer(const EventPoller::Ptr &poller);
|
2022-01-20 14:48:45 +08:00
|
|
|
|
~HlsPlayer() override = default;
|
2020-05-17 18:00:37 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 开始播放
|
|
|
|
|
*/
|
2022-01-20 14:48:45 +08:00
|
|
|
|
void play(const string &url) override;
|
2020-05-17 18:00:37 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 停止播放
|
|
|
|
|
*/
|
|
|
|
|
void teardown() override;
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
/**
|
|
|
|
|
* 收到ts包
|
|
|
|
|
* @param data ts数据负载
|
|
|
|
|
* @param len ts包长度
|
|
|
|
|
*/
|
2021-01-17 18:31:50 +08:00
|
|
|
|
virtual void onPacket(const char *data, size_t len) = 0;
|
2020-05-17 18:00:37 +08:00
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
void onParsed(bool is_m3u8_inner,int64_t sequence,const map<int,ts_segment> &ts_map) override;
|
2022-01-20 14:48:45 +08:00
|
|
|
|
void onResponseHeader(const string &status,const HttpHeader &headers) override;
|
|
|
|
|
void onResponseBody(const char *buf,size_t size) override;
|
|
|
|
|
void onResponseCompleted(const SockException &e) override;
|
2020-05-17 18:00:37 +08:00
|
|
|
|
bool onRedirectUrl(const string &url,bool temporary) override;
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
void playDelay();
|
|
|
|
|
float delaySecond();
|
2022-01-20 14:48:45 +08:00
|
|
|
|
void fetchSegment();
|
2020-05-17 18:00:37 +08:00
|
|
|
|
void teardown_l(const SockException &ex);
|
2022-01-20 14:48:45 +08:00
|
|
|
|
void fetchIndexFile();
|
2021-01-17 18:31:50 +08:00
|
|
|
|
void onPacket_l(const char *data, size_t len);
|
2020-05-17 18:00:37 +08:00
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
struct UrlComp {
|
|
|
|
|
//url忽略?后面的参数
|
|
|
|
|
bool operator()(const string& __x, const string& __y) const {
|
|
|
|
|
return split(__x,"?")[0] < split(__y,"?")[0];
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
private:
|
2022-01-20 14:48:45 +08:00
|
|
|
|
bool _play_result = false;
|
2020-05-17 18:00:37 +08:00
|
|
|
|
int64_t _last_sequence = -1;
|
|
|
|
|
string _m3u8;
|
2022-01-20 14:48:45 +08:00
|
|
|
|
string _play_url;
|
2020-05-17 18:00:37 +08:00
|
|
|
|
Timer::Ptr _timer;
|
|
|
|
|
Timer::Ptr _timer_ts;
|
|
|
|
|
list<ts_segment> _ts_list;
|
|
|
|
|
list<string> _ts_url_sort;
|
|
|
|
|
set<string, UrlComp> _ts_url_cache;
|
|
|
|
|
HttpTSPlayer::Ptr _http_ts_player;
|
|
|
|
|
TSSegment _segment;
|
|
|
|
|
};
|
|
|
|
|
|
2021-12-01 22:09:05 +08:00
|
|
|
|
class HlsPlayerImp : public PlayerImp<HlsPlayer, PlayerBase>, private TrackListener {
|
2020-05-17 18:00:37 +08:00
|
|
|
|
public:
|
|
|
|
|
typedef std::shared_ptr<HlsPlayerImp> Ptr;
|
|
|
|
|
HlsPlayerImp(const EventPoller::Ptr &poller = nullptr);
|
2021-12-01 22:14:28 +08:00
|
|
|
|
~HlsPlayerImp() override = default;
|
2020-05-17 18:00:37 +08:00
|
|
|
|
|
|
|
|
|
private:
|
2021-12-01 22:09:05 +08:00
|
|
|
|
//// HlsPlayer override////
|
2021-01-17 18:31:50 +08:00
|
|
|
|
void onPacket(const char *data, size_t len) override;
|
2021-12-01 22:09:05 +08:00
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
//// PlayerBase override////
|
2020-05-17 18:00:37 +08:00
|
|
|
|
void onPlayResult(const SockException &ex) override;
|
2021-12-01 22:09:05 +08:00
|
|
|
|
vector<Track::Ptr> getTracks(bool ready = true) const override;
|
2020-05-26 23:19:13 +08:00
|
|
|
|
void onShutdown(const SockException &ex) override;
|
2020-09-06 17:56:45 +08:00
|
|
|
|
|
2021-12-01 22:09:05 +08:00
|
|
|
|
private:
|
|
|
|
|
//// TrackListener override////
|
|
|
|
|
bool addTrack(const Track::Ptr &track) override { return true; };
|
|
|
|
|
void addTrackCompleted() override;
|
2020-09-06 17:56:45 +08:00
|
|
|
|
|
2020-05-17 18:00:37 +08:00
|
|
|
|
private:
|
2020-09-06 17:56:45 +08:00
|
|
|
|
DecoderImp::Ptr _decoder;
|
2021-12-01 22:09:05 +08:00
|
|
|
|
MediaSinkInterface::Ptr _demuxer;
|
2020-05-17 18:00:37 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}//namespace mediakit
|
|
|
|
|
#endif //HTTP_HLSPLAYER_H
|