PlayerProxy.cpp 4.99 KB
Newer Older
xzl committed
1
/*
xiongziliang committed
2
 * MIT License
xzl committed
3
 *
xiongziliang committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 * Copyright (c) 2016 xiongziliang <771730766@qq.com>
 *
 * This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
xzl committed
25 26
 */

xiongziliang committed
27
#include "Common/config.h"
xzl committed
28
#include "PlayerProxy.h"
xiongzilaing committed
29
#include "Util/mini.h"
xzl committed
30 31
#include "Util/MD5.h"
#include "Util/logger.h"
xiongzilaing committed
32
#include "Thread/AsyncTaskThread.h"
xzl committed
33 34 35 36 37 38 39

using namespace ZL::Util;
using namespace ZL::Thread;

namespace ZL {
namespace DEV {

xiongziliang committed
40 41
const char PlayerProxy::kAliveSecond[] = "alive_second";

xzl committed
42 43 44 45
PlayerProxy::PlayerProxy(const char *strApp,const char *strSrc){
	m_strApp = strApp;
	m_strSrc = strSrc;
}
xiongziliang committed
46 47
void PlayerProxy::play(const char* strUrl) {
	m_aliveSecond = (*this)[kAliveSecond];
xzl committed
48
	weak_ptr<PlayerProxy> weakSelf = shared_from_this();
xiongziliang committed
49
	setOnVideoCB( [weakSelf](const H264Frame &data ) {
xzl committed
50 51 52 53 54 55 56 57 58 59 60
		auto strongSelf = weakSelf.lock();
		if(!strongSelf){
			return;
		}
		if(strongSelf->m_pChn){
			strongSelf->m_pChn->inputH264((char *)data.data.data(), data.data.size(), data.timeStamp);
		}else{
			strongSelf->initMedia();
		}
		strongSelf->checkExpired();
	});
xiongziliang committed
61
	setOnAudioCB( [weakSelf](const AdtsFrame &data ) {
xzl committed
62 63 64 65 66 67 68 69 70 71 72 73 74
		auto strongSelf = weakSelf.lock();
		if(!strongSelf){
			return;
		}
		if(strongSelf->m_pChn){
			strongSelf->m_pChn->inputAAC((char *)data.data, data.aac_frame_length, data.timeStamp);
		}else{
			strongSelf->initMedia();
		}
		strongSelf->checkExpired();
	});

	std::shared_ptr<uint64_t> piFailedCnt(new uint64_t(0)); //连续播放失败次数
xiongziliang committed
75 76
	string strUrlTmp(strUrl);
	setOnPlayResult([weakSelf,strUrlTmp,piFailedCnt](const SockException &err) {
xzl committed
77 78 79 80 81 82 83 84 85 86
		auto strongSelf = weakSelf.lock();
		if(!strongSelf) {
			return;
		}
		static uint64_t replayCnt = mINI::Instance()[Config::Proxy::kReplayCount].as<uint64_t>();
		if(!err) {
			// 播放成功
			*piFailedCnt = 0;//连续播放失败次数清0
		}else if(*piFailedCnt < replayCnt) {
			// 播放失败,延时重试播放
xiongziliang committed
87
			strongSelf->rePlay(strUrlTmp,(*piFailedCnt)++);
xzl committed
88 89 90 91
		}else{
			strongSelf->expired();
		}
	});
xiongziliang committed
92
	setOnShutdown([weakSelf,strUrlTmp,piFailedCnt](const SockException &err) {
xzl committed
93 94 95 96 97 98 99 100 101 102
		auto strongSelf = weakSelf.lock();
		if(!strongSelf) {
			return;
		}
		if(strongSelf->m_pChn) {
			strongSelf->m_pChn.reset();
		}
		//播放异常中断,延时重试播放
		static uint64_t replayCnt = mINI::Instance()[Config::Proxy::kReplayCount].as<uint64_t>();
		if(*piFailedCnt < replayCnt) {
xiongziliang committed
103
			strongSelf->rePlay(strUrlTmp,(*piFailedCnt)++);
xzl committed
104 105 106 107
		}else{
			strongSelf->expired();
		}
	});
xiongziliang committed
108
	MediaPlayer::play(strUrl);
xzl committed
109 110 111 112 113 114
}

PlayerProxy::~PlayerProxy() {
	auto iTaskId = reinterpret_cast<uint64_t>(this);
	AsyncTaskThread::Instance().CancelTask(iTaskId);
}
xiongziliang committed
115
void PlayerProxy::rePlay(const string &strUrl,uint64_t iFailedCnt){
xzl committed
116 117 118
	checkExpired();
	auto iTaskId = reinterpret_cast<uint64_t>(this);
	auto iDelay = MAX((uint64_t)2 * 1000, MIN(iFailedCnt * 3000,(uint64_t)60*1000));
xiongziliang committed
119
	weak_ptr<PlayerProxy> weakSelf = shared_from_this();
xzl committed
120
	AsyncTaskThread::Instance().CancelTask(iTaskId);
xiongziliang committed
121
	AsyncTaskThread::Instance().DoTaskDelay(iTaskId, iDelay, [weakSelf,strUrl,iFailedCnt]() {
xzl committed
122
		//播放失败次数越多,则延时越长
xiongziliang committed
123
		auto strongPlayer = weakSelf.lock();
xzl committed
124 125 126 127
		if(!strongPlayer) {
			return false;
		}
		WarnL << "重试播放[" << iFailedCnt << "]:"  << strUrl;
xiongziliang committed
128
		strongPlayer->MediaPlayer::play(strUrl.data());
xzl committed
129 130 131 132
		return false;
	});
}
void PlayerProxy::initMedia() {
xiongziliang committed
133
	if (!isInited()) {
xzl committed
134 135
		return;
	}
xiongziliang committed
136 137
	m_pChn.reset(new DevChannel(m_strApp.data(),m_strSrc.data(),getDuration()));
	if (containVideo()) {
xzl committed
138
		VideoInfo info;
xiongziliang committed
139 140 141
		info.iFrameRate = getVideoFps();
		info.iWidth = getVideoWidth();
		info.iHeight = getVideoHeight();
xzl committed
142 143
		m_pChn->initVideo(info);
	}
xiongziliang committed
144
	if (containAudio()) {
xzl committed
145
		AudioInfo info;
xiongziliang committed
146 147 148
		info.iSampleRate = getAudioSampleRate();
		info.iChannel = getAudioChannel();
		info.iSampleBit = getAudioSampleBit();
xzl committed
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
		m_pChn->initAudio(info);
	}
}

void PlayerProxy::checkExpired() {
	if(m_aliveSecond && m_aliveTicker.elapsedTime() > m_aliveSecond * 1000){
		//到期
		expired();
	}
}

void PlayerProxy::expired() {
	if(onExpired){
		onExpired();
	}
}

} /* namespace Player */
} /* namespace ZL */