PlayerProxy.cpp 12.1 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
 */

xiongziliang committed
11
#include "Common/config.h"
xzl committed
12
#include "PlayerProxy.h"
xiongzilaing committed
13
#include "Util/mini.h"
xzl committed
14 15
#include "Util/MD5.h"
#include "Util/logger.h"
xiongziliang committed
16
#include "Extension/AAC.h"
xzl committed
17

xiongziliang committed
18
using namespace toolkit;
xzl committed
19

xiongziliang committed
20
namespace mediakit {
xzl committed
21

22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
static uint8_t s_mute_adts[] = {0xff, 0xf1, 0x6c, 0x40, 0x2d, 0x3f, 0xfc, 0x00, 0xe0, 0x34, 0x20, 0xad, 0xf2, 0x3f, 0xb5, 0xdd,
                                0x73, 0xac, 0xbd, 0xca, 0xd7, 0x7d, 0x4a, 0x13, 0x2d, 0x2e, 0xa2, 0x62, 0x02, 0x70, 0x3c, 0x1c,
                                0xc5, 0x63, 0x55, 0x69, 0x94, 0xb5, 0x8d, 0x70, 0xd7, 0x24, 0x6a, 0x9e, 0x2e, 0x86, 0x24, 0xea,
                                0x4f, 0xd4, 0xf8, 0x10, 0x53, 0xa5, 0x4a, 0xb2, 0x9a, 0xf0, 0xa1, 0x4f, 0x2f, 0x66, 0xf9, 0xd3,
                                0x8c, 0xa6, 0x97, 0xd5, 0x84, 0xac, 0x09, 0x25, 0x98, 0x0b, 0x1d, 0x77, 0x04, 0xb8, 0x55, 0x49,
                                0x85, 0x27, 0x06, 0x23, 0x58, 0xcb, 0x22, 0xc3, 0x20, 0x3a, 0x12, 0x09, 0x48, 0x24, 0x86, 0x76,
                                0x95, 0xe3, 0x45, 0x61, 0x43, 0x06, 0x6b, 0x4a, 0x61, 0x14, 0x24, 0xa9, 0x16, 0xe0, 0x97, 0x34,
                                0xb6, 0x58, 0xa4, 0x38, 0x34, 0x90, 0x19, 0x5d, 0x00, 0x19, 0x4a, 0xc2, 0x80, 0x4b, 0xdc, 0xb7,
                                0x00, 0x18, 0x12, 0x3d, 0xd9, 0x93, 0xee, 0x74, 0x13, 0x95, 0xad, 0x0b, 0x59, 0x51, 0x0e, 0x99,
                                0xdf, 0x49, 0x98, 0xde, 0xa9, 0x48, 0x4b, 0xa5, 0xfb, 0xe8, 0x79, 0xc9, 0xe2, 0xd9, 0x60, 0xa5,
                                0xbe, 0x74, 0xa6, 0x6b, 0x72, 0x0e, 0xe3, 0x7b, 0x28, 0xb3, 0x0e, 0x52, 0xcc, 0xf6, 0x3d, 0x39,
                                0xb7, 0x7e, 0xbb, 0xf0, 0xc8, 0xce, 0x5c, 0x72, 0xb2, 0x89, 0x60, 0x33, 0x7b, 0xc5, 0xda, 0x49,
                                0x1a, 0xda, 0x33, 0xba, 0x97, 0x9e, 0xa8, 0x1b, 0x6d, 0x5a, 0x77, 0xb6, 0xf1, 0x69, 0x5a, 0xd1,
                                0xbd, 0x84, 0xd5, 0x4e, 0x58, 0xa8, 0x5e, 0x8a, 0xa0, 0xc2, 0xc9, 0x22, 0xd9, 0xa5, 0x53, 0x11,
                                0x18, 0xc8, 0x3a, 0x39, 0xcf, 0x3f, 0x57, 0xb6, 0x45, 0x19, 0x1e, 0x8a, 0x71, 0xa4, 0x46, 0x27,
                                0x9e, 0xe9, 0xa4, 0x86, 0xdd, 0x14, 0xd9, 0x4d, 0xe3, 0x71, 0xe3, 0x26, 0xda, 0xaa, 0x17, 0xb4,
                                0xac, 0xe1, 0x09, 0xc1, 0x0d, 0x75, 0xba, 0x53, 0x0a, 0x37, 0x8b, 0xac, 0x37, 0x39, 0x41, 0x27,
                                0x6a, 0xf0, 0xe9, 0xb4, 0xc2, 0xac, 0xb0, 0x39, 0x73, 0x17, 0x64, 0x95, 0xf4, 0xdc, 0x33, 0xbb,
                                0x84, 0x94, 0x3e, 0xf8, 0x65, 0x71, 0x60, 0x7b, 0xd4, 0x5f, 0x27, 0x79, 0x95, 0x6a, 0xba, 0x76,
                                0xa6, 0xa5, 0x9a, 0xec, 0xae, 0x55, 0x3a, 0x27, 0x48, 0x23, 0xcf, 0x5c, 0x4d, 0xbc, 0x0b, 0x35,
                                0x5c, 0xa7, 0x17, 0xcf, 0x34, 0x57, 0xc9, 0x58, 0xc5, 0x20, 0x09, 0xee, 0xa5, 0xf2, 0x9c, 0x6c,
                                0x39, 0x1a, 0x77, 0x92, 0x9b, 0xff, 0xc6, 0xae, 0xf8, 0x36, 0xba, 0xa8, 0xaa, 0x6b, 0x1e, 0x8c,
                                0xc5, 0x97, 0x39, 0x6a, 0xb8, 0xa2, 0x55, 0xa8, 0xf8};
#define MUTE_ADTS_DATA s_mute_adts
#define MUTE_ADTS_DATA_LEN sizeof(s_mute_adts)
47
#define MUTE_ADTS_DATA_MS 130
48

49
PlayerProxy::PlayerProxy(const string &vhost, const string &app, const string &stream_id,
50
                         bool enable_hls, bool enable_mp4, int retry_count, const EventPoller::Ptr &poller)
51
        : MediaPlayer(poller) {
52 53 54 55 56 57
    _vhost = vhost;
    _app = app;
    _stream_id = stream_id;
    _enable_hls = enable_hls;
    _enable_mp4 = enable_mp4;
    _retry_count = retry_count;
58
    _on_close = [](const SockException &) {};
xzl committed
59
}
60

61
void PlayerProxy::setPlayCallbackOnce(const function<void(const SockException &ex)> &cb) {
62
    _on_play = cb;
63
}
64

65 66
void PlayerProxy::setOnClose(const function<void(const SockException &ex)> &cb) {
    _on_close = cb ? cb : [](const SockException &) {};
67 68
}

xiongziliang committed
69
void PlayerProxy::play(const string &strUrlTmp) {
70 71
    weak_ptr<PlayerProxy> weakSelf = shared_from_this();
    std::shared_ptr<int> piFailedCnt(new int(0)); //连续播放失败次数
72
    setOnPlayResult([weakSelf, strUrlTmp, piFailedCnt](const SockException &err) {
73
        auto strongSelf = weakSelf.lock();
74
        if (!strongSelf) {
75 76
            return;
        }
77

78
        if (strongSelf->_on_play) {
79 80
            strongSelf->_on_play(err);
            strongSelf->_on_play = nullptr;
81 82
        }

83
        if (!err) {
84 85 86
            // 播放成功
            *piFailedCnt = 0;//连续播放失败次数清0
            strongSelf->onPlaySuccess();
87
        } else if (*piFailedCnt < strongSelf->_retry_count || strongSelf->_retry_count < 0) {
88
            // 播放失败,延时重试播放
89 90 91 92
            strongSelf->rePlay(strUrlTmp, (*piFailedCnt)++);
        } else {
            //达到了最大重试次数,回调关闭
            strongSelf->_on_close(err);
93 94
        }
    });
95
    setOnShutdown([weakSelf, strUrlTmp, piFailedCnt](const SockException &err) {
96
        auto strongSelf = weakSelf.lock();
97
        if (!strongSelf) {
98 99
            return;
        }
100 101 102 103

        //注销直接拉流代理产生的流:#532
        strongSelf->setMediaSource(nullptr);

104
        if (strongSelf->_muxer) {
105
            auto tracks = strongSelf->MediaPlayer::getTracks(false);
106
            for (auto &track : tracks) {
107
                track->delDelegate(strongSelf->_muxer.get());
108
            }
109

110
            GET_CONFIG(bool, resetWhenRePlay, General::kResetWhenRePlay);
111
            if (resetWhenRePlay) {
112
                strongSelf->_muxer.reset();
113
            } else {
114
                strongSelf->_muxer->resetTracks();
115 116 117
            }
        }
        //播放异常中断,延时重试播放
118 119 120 121 122
        if (*piFailedCnt < strongSelf->_retry_count || strongSelf->_retry_count < 0) {
            strongSelf->rePlay(strUrlTmp, (*piFailedCnt)++);
        } else {
            //达到了最大重试次数,回调关闭
            strongSelf->_on_close(err);
123 124 125
        }
    });
    MediaPlayer::play(strUrlTmp);
126
    _pull_url = strUrlTmp;
127 128
    setDirectProxy();
}
129

130
void PlayerProxy::setDirectProxy() {
131
    MediaSource::Ptr mediaSource;
132
    if (dynamic_pointer_cast<RtspPlayer>(_delegate)) {
133
        //rtsp拉流
134 135
        GET_CONFIG(bool, directProxy, Rtsp::kDirectProxy);
        if (directProxy) {
136
            mediaSource = std::make_shared<RtspMediaSource>(_vhost, _app, _stream_id);
137
        }
138
    } else if (dynamic_pointer_cast<RtmpPlayer>(_delegate)) {
139 140
        //rtmp拉流,rtmp强制直接代理
        mediaSource = std::make_shared<RtmpMediaSource>(_vhost, _app, _stream_id);
141
    }
142
    if (mediaSource) {
xiongziliang committed
143
        setMediaSource(mediaSource);
144 145
        mediaSource->setListener(shared_from_this());
    }
xzl committed
146 147 148
}

PlayerProxy::~PlayerProxy() {
149
    _timer.reset();
xzl committed
150
}
151

152 153
void PlayerProxy::rePlay(const string &strUrl, int iFailedCnt) {
    auto iDelay = MAX(2 * 1000, MIN(iFailedCnt * 3000, 60 * 1000));
154
    weak_ptr<PlayerProxy> weakSelf = shared_from_this();
155
    _timer = std::make_shared<Timer>(iDelay / 1000.0f, [weakSelf, strUrl, iFailedCnt]() {
156 157
        //播放失败次数越多,则延时越长
        auto strongPlayer = weakSelf.lock();
158
        if (!strongPlayer) {
159 160
            return false;
        }
161
        WarnL << "重试播放[" << iFailedCnt << "]:" << strUrl;
162
        strongPlayer->MediaPlayer::play(strUrl);
163
        strongPlayer->setDirectProxy();
164 165
        return false;
    }, getPoller());
xzl committed
166
}
167

168 169
bool PlayerProxy::close(MediaSource &sender, bool force) {
    if (!force && totalReaderCount()) {
170 171 172
        return false;
    }

173
    //通知其停止推流
174 175 176 177 178 179 180
    weak_ptr<PlayerProxy> weakSelf = dynamic_pointer_cast<PlayerProxy>(shared_from_this());
    getPoller()->async_first([weakSelf]() {
        auto strongSelf = weakSelf.lock();
        if (!strongSelf) {
            return;
        }
        strongSelf->_muxer.reset();
xiongziliang committed
181
        strongSelf->setMediaSource(nullptr);
182
        strongSelf->teardown();
183
    });
184
    _on_close(SockException(Err_shutdown, "closed by user"));
xiongziliang committed
185
    WarnL << sender.getSchema() << "/" << sender.getVhost() << "/" << sender.getApp() << "/" << sender.getId() << " " << force;
186 187
    return true;
}
xzl committed
188

189
int PlayerProxy::totalReaderCount() {
190
    return (_muxer ? _muxer->totalReaderCount() : 0) + (_pMediaSrc ? _pMediaSrc->readerCount() : 0);
191 192 193
}

int PlayerProxy::totalReaderCount(MediaSource &sender) {
194
    return totalReaderCount();
195 196
}

197
MediaOriginType PlayerProxy::getOriginType(MediaSource &sender) const {
198 199 200
    return MediaOriginType::pull;
}

201
string PlayerProxy::getOriginUrl(MediaSource &sender) const {
202 203 204
    return _pull_url;
}

205
std::shared_ptr<SockInfo> PlayerProxy::getOriginSock(MediaSource &sender) const {
206 207 208
    return getSockInfo();
}

209
class MuteAudioMaker : public FrameDispatcher {
xiongziliang committed
210
public:
211
    typedef std::shared_ptr<MuteAudioMaker> Ptr;
xiongziliang committed
212

213
    MuteAudioMaker() {};
214 215
    ~MuteAudioMaker() override {}

216
    void inputFrame(const Frame::Ptr &frame) override {
217
        if (frame->getTrackType() == TrackVideo) {
218
            auto audio_idx = frame->dts() / MUTE_ADTS_DATA_MS;
219
            if (_audio_idx != audio_idx) {
220
                _audio_idx = audio_idx;
221
                auto aacFrame = std::make_shared<FrameFromStaticPtr>(CodecAAC, (char *)MUTE_ADTS_DATA, MUTE_ADTS_DATA_LEN, _audio_idx * MUTE_ADTS_DATA_MS, 0 ,ADTS_HEADER_LEN);
222 223 224 225
                FrameDispatcher::inputFrame(aacFrame);
            }
        }
    }
226 227

private:
228
    class FrameFromStaticPtr : public FrameFromPtr {
229
    public:
230 231
        template<typename ... ARGS>
        FrameFromStaticPtr(ARGS &&...args) : FrameFromPtr(std::forward<ARGS>(args)...) {};
232
        ~FrameFromStaticPtr() override = default;
233

234 235 236 237
        bool cacheAble() const override {
            return true;
        }
    };
238

xiongziliang committed
239
private:
240
    uint32_t _audio_idx = 0;
xiongziliang committed
241 242 243
};

void PlayerProxy::onPlaySuccess() {
244
    GET_CONFIG(bool, resetWhenRePlay, General::kResetWhenRePlay);
245 246
    if (dynamic_pointer_cast<RtspMediaSource>(_pMediaSrc)) {
        //rtsp拉流代理
247
        if (resetWhenRePlay || !_muxer) {
248
            _muxer.reset(new MultiMediaSourceMuxer(_vhost, _app, _stream_id, getDuration(), false, true, _enable_hls, _enable_mp4));
249 250 251
        }
    } else if (dynamic_pointer_cast<RtmpMediaSource>(_pMediaSrc)) {
        //rtmp拉流代理
252
        if (resetWhenRePlay || !_muxer) {
253
            _muxer.reset(new MultiMediaSourceMuxer(_vhost, _app, _stream_id, getDuration(), true, false, _enable_hls, _enable_mp4));
254 255 256
        }
    } else {
        //其他拉流代理
257
        if (resetWhenRePlay || !_muxer) {
258
            _muxer.reset(new MultiMediaSourceMuxer(_vhost, _app, _stream_id, getDuration(), true, true, _enable_hls, _enable_mp4));
259 260
        }
    }
261
    _muxer->setMediaListener(shared_from_this());
xiongziliang committed
262

263 264
    auto videoTrack = getTrack(TrackVideo, false);
    if (videoTrack) {
265
        //添加视频
266
        _muxer->addTrack(videoTrack);
267
        //视频数据写入_mediaMuxer
268
        videoTrack->addDelegate(_muxer);
269
    }
xiongziliang committed
270

271
    //是否添加静音音频
272
    GET_CONFIG(bool, addMuteAudio, General::kAddMuteAudio);
273

274
    auto audioTrack = getTrack(TrackAudio, false);
275
    if (audioTrack) {
276
        //添加音频
277
        _muxer->addTrack(audioTrack);
278
        //音频数据写入_mediaMuxer
279 280
        audioTrack->addDelegate(_muxer);
    } else if (addMuteAudio && videoTrack) {
281 282 283 284 285
        //没有音频信息,产生一个静音音频
        MuteAudioMaker::Ptr audioMaker = std::make_shared<MuteAudioMaker>();
        //videoTrack把数据写入MuteAudioMaker
        videoTrack->addDelegate(audioMaker);
        //添加一个静音Track至_mediaMuxer
286
        _muxer->addTrack(std::make_shared<AACTrack>());
287
        //MuteAudioMaker生成静音音频然后写入_mediaMuxer;
288
        audioMaker->addDelegate(_muxer);
289
    }
xiongziliang committed
290

291
    //添加完毕所有track,防止单track情况下最大等待3秒
292
    _muxer->addTrackCompleted();
293

294
    if (_pMediaSrc) {
295 296
        //让_muxer对象拦截一部分事件(比如说录像相关事件)
        _pMediaSrc->setListener(_muxer);
xiongziliang committed
297
    }
298 299
}

xiongziliang committed
300
} /* namespace mediakit */