Mutex: 删除一些不必要的互斥锁

This commit is contained in:
ziyue 2021-11-10 11:36:21 +08:00
parent 143979f354
commit 817542cd0c
6 changed files with 2 additions and 30 deletions

View File

@ -32,7 +32,6 @@ bool MediaSink::addTrack(const Track::Ptr &track_in) {
return false; return false;
} }
} }
lock_guard<recursive_mutex> lck(_mtx);
if (_all_track_ready) { if (_all_track_ready) {
WarnL << "all track is ready, add this track too late!"; WarnL << "all track is ready, add this track too late!";
return false; return false;
@ -64,7 +63,6 @@ bool MediaSink::addTrack(const Track::Ptr &track_in) {
} }
void MediaSink::resetTracks() { void MediaSink::resetTracks() {
lock_guard<recursive_mutex> lck(_mtx);
_all_track_ready = false; _all_track_ready = false;
_track_map.clear(); _track_map.clear();
_track_ready_callback.clear(); _track_ready_callback.clear();
@ -74,7 +72,6 @@ void MediaSink::resetTracks() {
} }
bool MediaSink::inputFrame(const Frame::Ptr &frame) { bool MediaSink::inputFrame(const Frame::Ptr &frame) {
lock_guard<recursive_mutex> lck(_mtx);
auto it = _track_map.find(frame->getTrackType()); auto it = _track_map.find(frame->getTrackType());
if (it == _track_map.end()) { if (it == _track_map.end()) {
return false; return false;
@ -135,7 +132,6 @@ void MediaSink::checkTrackIfReady(const Track::Ptr &track){
} }
void MediaSink::addTrackCompleted(){ void MediaSink::addTrackCompleted(){
lock_guard<recursive_mutex> lck(_mtx);
_max_track_size = _track_map.size(); _max_track_size = _track_map.size();
checkTrackIfReady(nullptr); checkTrackIfReady(nullptr);
} }
@ -190,7 +186,6 @@ void MediaSink::onAllTrackReady_l() {
vector<Track::Ptr> MediaSink::getTracks(bool trackReady) const{ vector<Track::Ptr> MediaSink::getTracks(bool trackReady) const{
vector<Track::Ptr> ret; vector<Track::Ptr> ret;
lock_guard<recursive_mutex> lck(_mtx);
for (auto &pr : _track_map){ for (auto &pr : _track_map){
if(trackReady && !pr.second->ready()){ if(trackReady && !pr.second->ready()){
continue; continue;

View File

@ -148,7 +148,6 @@ private:
private: private:
bool _all_track_ready = false; bool _all_track_ready = false;
size_t _max_track_size = 2; size_t _max_track_size = 2;
mutable recursive_mutex _mtx;
unordered_map<int,Track::Ptr> _track_map; unordered_map<int,Track::Ptr> _track_map;
unordered_map<int,List<Frame::Ptr> > _frame_unread; unordered_map<int,List<Frame::Ptr> > _frame_unread;
unordered_map<int,function<void()> > _track_ready_callback; unordered_map<int,function<void()> > _track_ready_callback;

View File

@ -40,9 +40,7 @@ void RtmpPlayer::teardown() {
CLEAR_ARR(_fist_stamp); CLEAR_ARR(_fist_stamp);
CLEAR_ARR(_now_stamp); CLEAR_ARR(_now_stamp);
lock_guard<recursive_mutex> lck(_mtx_on_result);
_map_on_result.clear(); _map_on_result.clear();
lock_guard<recursive_mutex> lck2(_mtx_on_status);
_deque_on_status.clear(); _deque_on_status.clear();
} }
@ -271,7 +269,6 @@ inline void RtmpPlayer::send_pause(bool pause) {
void RtmpPlayer::onCmd_result(AMFDecoder &dec){ void RtmpPlayer::onCmd_result(AMFDecoder &dec){
auto req_id = dec.load<int>(); auto req_id = dec.load<int>();
lock_guard<recursive_mutex> lck(_mtx_on_result);
auto it = _map_on_result.find(req_id); auto it = _map_on_result.find(req_id);
if (it != _map_on_result.end()) { if (it != _map_on_result.end()) {
it->second(dec); it->second(dec);
@ -293,7 +290,6 @@ void RtmpPlayer::onCmd_onStatus(AMFDecoder &dec) {
throw std::runtime_error("onStatus:the result object was not found"); throw std::runtime_error("onStatus:the result object was not found");
} }
lock_guard<recursive_mutex> lck(_mtx_on_status);
if (_deque_on_status.size()) { if (_deque_on_status.size()) {
_deque_on_status.front()(val); _deque_on_status.front()(val);
_deque_on_status.pop_front(); _deque_on_status.pop_front();

View File

@ -65,12 +65,10 @@ protected:
template<typename FUNC> template<typename FUNC>
void addOnResultCB(const FUNC &func) { void addOnResultCB(const FUNC &func) {
lock_guard<recursive_mutex> lck(_mtx_on_result);
_map_on_result.emplace(_send_req_id, func); _map_on_result.emplace(_send_req_id, func);
} }
template<typename FUNC> template<typename FUNC>
void addOnStatusCB(const FUNC &func) { void addOnStatusCB(const FUNC &func) {
lock_guard<recursive_mutex> lck(_mtx_on_status);
_deque_on_status.emplace_back(func); _deque_on_status.emplace_back(func);
} }
@ -98,9 +96,6 @@ private:
uint32_t _fist_stamp[2] = {0, 0}; uint32_t _fist_stamp[2] = {0, 0};
uint32_t _now_stamp[2] = {0, 0}; uint32_t _now_stamp[2] = {0, 0};
Ticker _now_stamp_ticker[2]; Ticker _now_stamp_ticker[2];
recursive_mutex _mtx_on_result;
recursive_mutex _mtx_on_status;
deque<function<void(AMFValue &dec)> > _deque_on_status; deque<function<void(AMFValue &dec)> > _deque_on_status;
unordered_map<int, function<void(AMFDecoder &dec)> > _map_on_result; unordered_map<int, function<void(AMFDecoder &dec)> > _map_on_result;

View File

@ -32,14 +32,8 @@ void RtmpPusher::teardown() {
_app.clear(); _app.clear();
_stream_id.clear(); _stream_id.clear();
_tc_url.clear(); _tc_url.clear();
{ _map_on_result.clear();
lock_guard<recursive_mutex> lck(_mtx_on_result); _deque_on_status.clear();
_map_on_result.clear();
}
{
lock_guard<recursive_mutex> lck(_mtx_on_status);
_deque_on_status.clear();
}
_publish_timer.reset(); _publish_timer.reset();
reset(); reset();
shutdown(SockException(Err_shutdown, "teardown")); shutdown(SockException(Err_shutdown, "teardown"));
@ -241,7 +235,6 @@ void RtmpPusher::setSocketFlags(){
void RtmpPusher::onCmd_result(AMFDecoder &dec){ void RtmpPusher::onCmd_result(AMFDecoder &dec){
auto req_id = dec.load<int>(); auto req_id = dec.load<int>();
lock_guard<recursive_mutex> lck(_mtx_on_result);
auto it = _map_on_result.find(req_id); auto it = _map_on_result.find(req_id);
if (it != _map_on_result.end()) { if (it != _map_on_result.end()) {
it->second(dec); it->second(dec);
@ -263,7 +256,6 @@ void RtmpPusher::onCmd_onStatus(AMFDecoder &dec) {
throw std::runtime_error("onStatus:the result object was not found"); throw std::runtime_error("onStatus:the result object was not found");
} }
lock_guard<recursive_mutex> lck(_mtx_on_status);
if (_deque_on_status.size()) { if (_deque_on_status.size()) {
_deque_on_status.front()(val); _deque_on_status.front()(val);
_deque_on_status.pop_front(); _deque_on_status.pop_front();

View File

@ -44,12 +44,10 @@ private:
template<typename FUN> template<typename FUN>
inline void addOnResultCB(const FUN &fun) { inline void addOnResultCB(const FUN &fun) {
lock_guard<recursive_mutex> lck(_mtx_on_result);
_map_on_result.emplace(_send_req_id, fun); _map_on_result.emplace(_send_req_id, fun);
} }
template<typename FUN> template<typename FUN>
inline void addOnStatusCB(const FUN &fun) { inline void addOnStatusCB(const FUN &fun) {
lock_guard<recursive_mutex> lck(_mtx_on_status);
_deque_on_status.emplace_back(fun); _deque_on_status.emplace_back(fun);
} }
@ -67,9 +65,6 @@ private:
string _app; string _app;
string _stream_id; string _stream_id;
string _tc_url; string _tc_url;
recursive_mutex _mtx_on_result;
recursive_mutex _mtx_on_status;
deque<function<void(AMFValue &dec)> > _deque_on_status; deque<function<void(AMFValue &dec)> > _deque_on_status;
unordered_map<int, function<void(AMFDecoder &dec)> > _map_on_result; unordered_map<int, function<void(AMFDecoder &dec)> > _map_on_result;