From b778d9eeca2f3bd30e620f579c2ae30a5a6c6d3f Mon Sep 17 00:00:00 2001 From: Pedro Rodriguez Date: Wed, 11 Apr 2018 16:50:31 +0200 Subject: [PATCH 1/3] Added events from mediaStream to ErizoJS --- erizo/src/erizo/MediaStream.cpp | 12 ++++ erizo/src/erizo/MediaStream.h | 16 +++++ erizo/src/erizo/Stats.cpp | 6 ++ erizo/src/erizo/Stats.h | 6 +- erizo/src/erizo/WebRtcConnection.cpp | 68 +++++++++++-------- erizo/src/erizo/WebRtcConnection.h | 10 +-- erizoAPI/MediaStream.cc | 63 +++++++++++++++-- erizoAPI/MediaStream.h | 17 ++++- erizo_controller/erizoJS/models/Connection.js | 8 +++ 9 files changed, 159 insertions(+), 47 deletions(-) diff --git a/erizo/src/erizo/MediaStream.cpp b/erizo/src/erizo/MediaStream.cpp index 8fa4c8b44a..4cc3f4e9e3 100644 --- a/erizo/src/erizo/MediaStream.cpp +++ b/erizo/src/erizo/MediaStream.cpp @@ -368,6 +368,18 @@ void MediaStream::read(std::shared_ptr packet) { } // if not Feedback } +void MediaStream::setMediaStreamEventListener(MediaStreamEventListener* listener) { + boost::mutex::scoped_lock lock(eventlistener_mutex_); + this->mediastream_event_listener_ = listener; +} + +void MediaStream::notifyMediaStreamEvent(const std::string& type, const std::string& message) { + boost::mutex::scoped_lock lock(eventlistener_mutex_); + if (this->mediastream_event_listener_ != nullptr) { + mediastream_event_listener_->notifyMediaStreamEvent(type, message); + } +} + void MediaStream::notifyToEventSink(MediaEventPtr event) { event_sink_->deliverEvent(event); } diff --git a/erizo/src/erizo/MediaStream.h b/erizo/src/erizo/MediaStream.h index 149a30b9fa..115131796b 100644 --- a/erizo/src/erizo/MediaStream.h +++ b/erizo/src/erizo/MediaStream.h @@ -34,6 +34,13 @@ class MediaStreamStatsListener { virtual void notifyStats(const std::string& message) = 0; }; + +class MediaStreamEventListener { + public: + virtual ~MediaStreamEventListener() { + } + virtual void notifyMediaStreamEvent(const std::string& type, const std::string& message) = 0; +}; /** * A MediaStream. This class represents a Media Stream that can be established with other peers via a SDP negotiation */ @@ -76,6 +83,13 @@ class MediaStream: public MediaSink, public MediaSource, public FeedbackSink, WebRTCEvent getCurrentState(); + /** + * Sets the Event Listener for this MediaStream + */ + void setMediaStreamEventListener(MediaStreamEventListener* listener); + + void notifyMediaStreamEvent(const std::string& type, const std::string& message); + /** * Sets the Stats Listener for this MediaStream */ @@ -148,6 +162,8 @@ class MediaStream: public MediaSink, public MediaSource, public FeedbackSink, // parses incoming payload type, replaces occurence in buf private: + boost::mutex eventlistener_mutex_; + MediaStreamEventListener* mediastream_event_listener_; std::shared_ptr connection_; std::string stream_id_; std::string mslabel_; diff --git a/erizo/src/erizo/Stats.cpp b/erizo/src/erizo/Stats.cpp index 790b82dce3..211b9094e5 100644 --- a/erizo/src/erizo/Stats.cpp +++ b/erizo/src/erizo/Stats.cpp @@ -28,7 +28,13 @@ namespace erizo { return root_.toString(); } + void Stats::setStatsListener(MediaStreamStatsListener* listener) { + boost::mutex::scoped_lock lock(listener_mutex_); + listener_ = listener; + } + void Stats::sendStats() { + boost::mutex::scoped_lock lock(listener_mutex_); if (listener_) listener_->notifyStats(getStats()); } } // namespace erizo diff --git a/erizo/src/erizo/Stats.h b/erizo/src/erizo/Stats.h index 0d982fbf2c..fd96b505bf 100644 --- a/erizo/src/erizo/Stats.h +++ b/erizo/src/erizo/Stats.h @@ -29,13 +29,11 @@ class Stats : public Service { std::string getStats(); - inline void setStatsListener(MediaStreamStatsListener* listener) { - listener_ = listener; - } - + void setStatsListener(MediaStreamStatsListener* listener); void sendStats(); private: + boost::mutex listener_mutex_; MediaStreamStatsListener* listener_; StatNode root_; }; diff --git a/erizo/src/erizo/WebRtcConnection.cpp b/erizo/src/erizo/WebRtcConnection.cpp index 4cc5a10fae..b6be8c5b4d 100644 --- a/erizo/src/erizo/WebRtcConnection.cpp +++ b/erizo/src/erizo/WebRtcConnection.cpp @@ -95,14 +95,12 @@ void WebRtcConnection::close() { } bool WebRtcConnection::init() { - if (conn_event_listener_ != nullptr) { - conn_event_listener_->notifyEvent(global_state_, ""); - } - return true; + maybeNotifyWebRtcConnectionEvent(global_state_, ""); + return true; } bool WebRtcConnection::createOffer(bool video_enabled, bool audioEnabled, bool bundle) { - boost::mutex::scoped_lock lock(updateStateMutex_); + boost::mutex::scoped_lock lock(update_state_mutex_); bundle_ = bundle; video_enabled_ = video_enabled; audio_enabled_ = audioEnabled; @@ -147,10 +145,10 @@ bool WebRtcConnection::createOffer(bool video_enabled, bool audioEnabled, bool b audio_transport_->start(); } } - if (conn_event_listener_ != nullptr) { - std::string msg = this->getLocalSdp(); - conn_event_listener_->notifyEvent(global_state_, msg); - } + + std::string msg = this->getLocalSdp(); + maybeNotifyWebRtcConnectionEvent(global_state_, msg); + std::weak_ptr weak_this = shared_from_this(); forEachMediaStreamAsync([weak_this] (const std::shared_ptr &media_stream) { if (auto connection = weak_this.lock()) { @@ -169,7 +167,7 @@ void WebRtcConnection::addMediaStream(std::shared_ptr media_stream) void WebRtcConnection::removeMediaStream(const std::string& stream_id) { asyncTask([stream_id] (std::shared_ptr connection) { - boost::mutex::scoped_lock lock(connection->updateStateMutex_); + boost::mutex::scoped_lock lock(connection->update_state_mutex_); ELOG_DEBUG("%s message: removing mediaStream, id: %s", connection->toLog(), stream_id.c_str()); connection->media_streams_.erase(std::remove_if(connection->media_streams_.begin(), connection->media_streams_.end(), @@ -218,7 +216,7 @@ bool WebRtcConnection::setRemoteSdpInfo(std::shared_ptr sdp, std::strin } std::shared_ptr WebRtcConnection::getLocalSdpInfo() { - boost::mutex::scoped_lock lock(updateStateMutex_); + boost::mutex::scoped_lock lock(update_state_mutex_); ELOG_DEBUG("%s message: getting local SDPInfo", toLog()); forEachMediaStream([this] (const std::shared_ptr &media_stream) { if (!media_stream->isRunning() || media_stream->isPublisher()) { @@ -287,7 +285,7 @@ void WebRtcConnection::onRemoteSdpsSetToMediaStreams(std::string stream_id) { asyncTask([stream_id] (std::shared_ptr connection) { ELOG_DEBUG("%s message: SDP processed", connection->toLog()); std::string sdp = connection->getLocalSdp(); - connection->conn_event_listener_->notifyEvent(CONN_SDP_PROCESSED, sdp, stream_id); + connection->maybeNotifyWebRtcConnectionEvent(CONN_SDP_PROCESSED, sdp, stream_id); }); } @@ -453,19 +451,17 @@ void WebRtcConnection::onCandidate(const CandidateInfo& cand, Transport *transpo std::string sdp = local_sdp_->addCandidate(cand); ELOG_DEBUG("%s message: Discovered New Candidate, candidate: %s", toLog(), sdp.c_str()); if (trickle_enabled_) { - if (conn_event_listener_ != nullptr) { - if (!bundle_) { - std::string object = this->getJSONCandidate(transport->transport_name, sdp); - conn_event_listener_->notifyEvent(CONN_CANDIDATE, object); - } else { - if (remote_sdp_->hasAudio) { - std::string object = this->getJSONCandidate("audio", sdp); - conn_event_listener_->notifyEvent(CONN_CANDIDATE, object); - } - if (remote_sdp_->hasVideo) { - std::string object2 = this->getJSONCandidate("video", sdp); - conn_event_listener_->notifyEvent(CONN_CANDIDATE, object2); - } + if (!bundle_) { + std::string object = this->getJSONCandidate(transport->transport_name, sdp); + maybeNotifyWebRtcConnectionEvent(CONN_CANDIDATE, object); + } else { + if (remote_sdp_->hasAudio) { + std::string object = this->getJSONCandidate("audio", sdp); + maybeNotifyWebRtcConnectionEvent(CONN_CANDIDATE, object); + } + if (remote_sdp_->hasVideo) { + std::string object2 = this->getJSONCandidate("video", sdp); + maybeNotifyWebRtcConnectionEvent(CONN_CANDIDATE, object2); } } } @@ -540,6 +536,15 @@ void WebRtcConnection::onTransportData(std::shared_ptr packet, Trans } } +void WebRtcConnection::maybeNotifyWebRtcConnectionEvent(const WebRTCEvent& event, const std::string& message, + const std::string& stream_id) { + boost::mutex::scoped_lock lock(eventlistener_mutex_); + if (!conn_event_listener_) { + return; + } + conn_event_listener_->notifyEvent(event, message, stream_id); +} + void WebRtcConnection::asyncTask(std::function)> f) { std::weak_ptr weak_this = shared_from_this(); worker_->task([weak_this, f] { @@ -550,7 +555,7 @@ void WebRtcConnection::asyncTask(std::functiontransport_name.c_str(), state); @@ -655,10 +660,8 @@ void WebRtcConnection::updateState(TransportState state, Transport * transport) global_state_ = temp; - if (conn_event_listener_ != nullptr) { - ELOG_INFO("%s newGlobalState: %d", toLog(), global_state_); - conn_event_listener_->notifyEvent(global_state_, msg); - } + ELOG_INFO("%s newGlobalState: %d", toLog(), global_state_); + maybeNotifyWebRtcConnectionEvent(global_state_, msg); } void WebRtcConnection::trackTransportInfo() { @@ -687,6 +690,11 @@ void WebRtcConnection::setMetadata(std::map metadata) setLogContext(metadata); } +void WebRtcConnection::setWebRtcConnectionEventListener(WebRtcConnectionEventListener* listener) { + boost::mutex::scoped_lock lock(eventlistener_mutex_); + this->conn_event_listener_ = listener; +} + WebRTCEvent WebRtcConnection::getCurrentState() { return global_state_; } diff --git a/erizo/src/erizo/WebRtcConnection.h b/erizo/src/erizo/WebRtcConnection.h index ec84465bc8..245c3bb2fa 100644 --- a/erizo/src/erizo/WebRtcConnection.h +++ b/erizo/src/erizo/WebRtcConnection.h @@ -110,10 +110,7 @@ class WebRtcConnection: public TransportListener, public LogContext, /** * Sets the Event Listener for this WebRtcConnection */ - inline void setWebRtcConnectionEventListener(WebRtcConnectionEventListener* listener) { - this->conn_event_listener_ = listener; - } - + void setWebRtcConnectionEventListener(WebRtcConnectionEventListener* listener); /** * Gets the current state of the Ice Connection @@ -162,6 +159,8 @@ class WebRtcConnection: public TransportListener, public LogContext, void trackTransportInfo(); void onRtcpFromTransport(std::shared_ptr packet, Transport *transport); void onREMBFromTransport(RtcpHeader *chead, Transport *transport); + void maybeNotifyWebRtcConnectionEvent(const WebRTCEvent& event, const std::string& message, + const std::string& stream_id = ""); private: std::string connection_id_; @@ -182,7 +181,8 @@ class WebRtcConnection: public TransportListener, public LogContext, std::shared_ptr stats_; WebRTCEvent global_state_; - boost::mutex updateStateMutex_; // , slideShowMutex_; + boost::mutex update_state_mutex_; + boost::mutex eventlistener_mutex_; std::shared_ptr worker_; std::shared_ptr io_worker_; diff --git a/erizoAPI/MediaStream.cc b/erizoAPI/MediaStream.cc index 180d1dd915..e2e880d2d2 100644 --- a/erizoAPI/MediaStream.cc +++ b/erizoAPI/MediaStream.cc @@ -51,7 +51,9 @@ Nan::Persistent MediaStream::constructor; MediaStream::MediaStream() : closed_{false}, id_{"undefined"} { async_stats_ = new uv_async_t; + async_event_ = new uv_async_t; uv_async_init(uv_default_loop(), async_stats_, &MediaStream::statsCallback); + uv_async_init(uv_default_loop(), async_event_, &MediaStream::eventCallback); } MediaStream::~MediaStream() { @@ -68,15 +70,22 @@ void MediaStream::close() { ELOG_DEBUG("%s, message: Closing", toLog()); if (me) { me->setMediaStreamStatsListener(nullptr); + me->setMediaStreamEventListener(nullptr); me->close(); me.reset(); } - hasCallback_ = false; + hasStatsCallback_ = false; + hasEventCallback_ = false; if (!uv_is_closing(reinterpret_cast(async_stats_))) { - ELOG_DEBUG("%s, message: Closing handle", toLog()); + ELOG_DEBUG("%s, message: Closing Stats handle", toLog()); uv_close(reinterpret_cast(async_stats_), destroyAsyncStats); } async_stats_ = nullptr; + if (!uv_is_closing(reinterpret_cast(async_event_))) { + ELOG_DEBUG("%s, message: Closing MediaStreamEvent handle", toLog()); + uv_close(reinterpret_cast(async_event_), destroyAsyncStats); + } + async_event_ = nullptr; closed_ = true; ELOG_DEBUG("%s, message: Closed", toLog()); } @@ -105,6 +114,7 @@ NAN_MODULE_INIT(MediaStream::Init) { Nan::SetPrototypeMethod(tpl, "muteStream", muteStream); Nan::SetPrototypeMethod(tpl, "setMaxVideoBW", setMaxVideoBW); Nan::SetPrototypeMethod(tpl, "setQualityLayer", setQualityLayer); + Nan::SetPrototypeMethod(tpl, "onMediaStreamEvent", onMediaStreamEvent); Nan::SetPrototypeMethod(tpl, "setVideoConstraints", setVideoConstraints); Nan::SetPrototypeMethod(tpl, "setMetadata", setMetadata); Nan::SetPrototypeMethod(tpl, "enableHandler", enableHandler); @@ -352,7 +362,7 @@ NAN_METHOD(MediaStream::getPeriodicStats) { return; } obj->me->setMediaStreamStatsListener(obj); - obj->hasCallback_ = true; + obj->hasStatsCallback_ = true; obj->statsCallback_ = new Nan::Callback(info[0].As()); } @@ -368,8 +378,19 @@ NAN_METHOD(MediaStream::setFeedbackReports) { me->setFeedbackReports(v, fbreps); } +NAN_METHOD(MediaStream::onMediaStreamEvent) { + MediaStream* obj = Nan::ObjectWrap::Unwrap(info.Holder()); + std::shared_ptr me = obj->me; + if (!me) { + return; + } + me ->setMediaStreamEventListener(obj); + obj->hasEventCallback_ = true; + obj->eventCallback_ = new Nan::Callback(info[0].As()); +} + void MediaStream::notifyStats(const std::string& message) { - if (!this->hasCallback_) { + if (!this->hasStatsCallback_) { return; } if (!async_stats_) { @@ -381,6 +402,19 @@ void MediaStream::notifyStats(const std::string& message) { uv_async_send(async_stats_); } +void MediaStream::notifyMediaStreamEvent(const std::string& type, const std::string& message) { + if (!this->hasEventCallback_) { + return; + } + if (!async_event_) { + return; + } + boost::mutex::scoped_lock lock(mutex); + this->eventMsgs.push(std::make_pair(type, message)); + async_event_->data = this; + uv_async_send(async_event_); +} + NAUV_WORK_CB(MediaStream::statsCallback) { Nan::HandleScope scope; MediaStream* obj = reinterpret_cast(async->data); @@ -388,7 +422,7 @@ NAUV_WORK_CB(MediaStream::statsCallback) { return; } boost::mutex::scoped_lock lock(obj->mutex); - if (obj->hasCallback_) { + if (obj->hasStatsCallback_) { while (!obj->statsMsgs.empty()) { Local args[] = {Nan::New(obj->statsMsgs.front().c_str()).ToLocalChecked()}; Nan::MakeCallback(Nan::GetCurrentContext()->Global(), obj->statsCallback_->GetFunction(), 1, args); @@ -396,3 +430,22 @@ NAUV_WORK_CB(MediaStream::statsCallback) { } } } + +NAUV_WORK_CB(MediaStream::eventCallback) { + Nan::HandleScope scope; + MediaStream* obj = reinterpret_cast(async->data); + if (!obj || !obj->me) { + return; + } + boost::mutex::scoped_lock lock(obj->mutex); + ELOG_DEBUG("%s, message: eventsCallback", obj->toLog()); + if (obj->hasEventCallback_) { + while (!obj->eventMsgs.empty()) { + Local args[] = {Nan::New(obj->eventMsgs.front().first.c_str()).ToLocalChecked(), + Nan::New(obj->eventMsgs.front().second.c_str()).ToLocalChecked()}; + Nan::MakeCallback(Nan::GetCurrentContext()->Global(), obj->eventCallback_->GetFunction(), 2, args); + obj->eventMsgs.pop(); + } + } + ELOG_DEBUG("%s, message: eventsCallback finished", obj->toLog()); +} diff --git a/erizoAPI/MediaStream.h b/erizoAPI/MediaStream.h index 35de27b9c5..9d2966b005 100644 --- a/erizoAPI/MediaStream.h +++ b/erizoAPI/MediaStream.h @@ -31,13 +31,14 @@ class StatCallWorker : public Nan::AsyncWorker { * A WebRTC Connection. This class represents a MediaStream that can be established with other peers via a SDP negotiation * it comprises all the necessary ICE and SRTP components. */ -class MediaStream : public MediaSink, public erizo::MediaStreamStatsListener { +class MediaStream : public MediaSink, public erizo::MediaStreamStatsListener, public erizo::MediaStreamEventListener { public: DECLARE_LOGGER(); static NAN_MODULE_INIT(Init); std::shared_ptr me; std::queue statsMsgs; + std::queue> eventMsgs; boost::mutex mutex; @@ -48,10 +49,13 @@ class MediaStream : public MediaSink, public erizo::MediaStreamStatsListener { void close(); std::string toLog(); - Nan::Callback *statsCallback_; + Nan::Callback *eventCallback_; + uv_async_t *async_event_; + bool hasEventCallback_; + Nan::Callback *statsCallback_; uv_async_t *async_stats_; - bool hasCallback_; + bool hasStatsCallback_; bool closed_; std::string id_; std::string label_; @@ -149,10 +153,17 @@ class MediaStream : public MediaSink, public erizo::MediaStreamStatsListener { static NAN_METHOD(disableHandler); static NAN_METHOD(setQualityLayer); + + static NAN_METHOD(onMediaStreamEvent); + static Nan::Persistent constructor; static NAUV_WORK_CB(statsCallback); virtual void notifyStats(const std::string& message); + + static NAUV_WORK_CB(eventCallback); + virtual void notifyMediaStreamEvent(const std::string& message = "", + const std::string& stream_id = ""); }; #endif // ERIZOAPI_MEDIASTREAM_H_ diff --git a/erizo_controller/erizoJS/models/Connection.js b/erizo_controller/erizoJS/models/Connection.js index 672c8476c0..50292fdc3c 100644 --- a/erizo_controller/erizoJS/models/Connection.js +++ b/erizo_controller/erizoJS/models/Connection.js @@ -88,9 +88,17 @@ class Connection extends events.EventEmitter { mediaStream.metadata = options.metadata; mediaStream.setMetadata(JSON.stringify(options.metadata)); } + mediaStream.onMediaStreamEvent((type, message) => { + this._onMediaStreamEvent(type, message); + }); return mediaStream; } + _onMediaStreamEvent(type, message) { + log.debug(`message: _onMediaStreamEvent, type: ${type}, msg: ${message}`); + this.emit('mediastream_event', type, message); + } + _maybeSendAnswer(evt, streamId) { if (this.isProcessingRemoteSdp) { return; From 24b0bc5411621f97cf9928c58e082db81a3de935 Mon Sep 17 00:00:00 2001 From: Pedro Rodriguez Date: Wed, 11 Apr 2018 17:11:39 +0200 Subject: [PATCH 2/3] Fix typo --- erizoAPI/MediaStream.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/erizoAPI/MediaStream.h b/erizoAPI/MediaStream.h index 9d2966b005..ceb52c42b7 100644 --- a/erizoAPI/MediaStream.h +++ b/erizoAPI/MediaStream.h @@ -162,8 +162,8 @@ class MediaStream : public MediaSink, public erizo::MediaStreamStatsListener, pu virtual void notifyStats(const std::string& message); static NAUV_WORK_CB(eventCallback); - virtual void notifyMediaStreamEvent(const std::string& message = "", - const std::string& stream_id = ""); + virtual void notifyMediaStreamEvent(const std::string& type = "", + const std::string& message = ""); }; #endif // ERIZOAPI_MEDIASTREAM_H_ From e912d6ead11918aa708ad2fc2b089ca5013fdead Mon Sep 17 00:00:00 2001 From: Pedro Rodriguez Date: Thu, 12 Apr 2018 10:00:40 +0200 Subject: [PATCH 3/3] Code review comments --- erizo/src/erizo/MediaStream.cpp | 10 +++--- erizo/src/erizo/MediaStream.h | 4 +-- erizo/src/erizo/WebRtcConnection.cpp | 4 +-- erizo/src/erizo/WebRtcConnection.h | 2 +- erizoAPI/MediaStream.cc | 48 ++++++++++++++-------------- erizoAPI/MediaStream.h | 12 +++---- erizoAPI/WebRtcConnection.cc | 20 ++++++------ erizoAPI/WebRtcConnection.h | 7 ++-- erizo_controller/test/utils.js | 1 + 9 files changed, 54 insertions(+), 54 deletions(-) diff --git a/erizo/src/erizo/MediaStream.cpp b/erizo/src/erizo/MediaStream.cpp index 4cc3f4e9e3..02295cba4a 100644 --- a/erizo/src/erizo/MediaStream.cpp +++ b/erizo/src/erizo/MediaStream.cpp @@ -369,14 +369,14 @@ void MediaStream::read(std::shared_ptr packet) { } void MediaStream::setMediaStreamEventListener(MediaStreamEventListener* listener) { - boost::mutex::scoped_lock lock(eventlistener_mutex_); - this->mediastream_event_listener_ = listener; + boost::mutex::scoped_lock lock(event_listener_mutex_); + this->media_stream_event_listener_ = listener; } void MediaStream::notifyMediaStreamEvent(const std::string& type, const std::string& message) { - boost::mutex::scoped_lock lock(eventlistener_mutex_); - if (this->mediastream_event_listener_ != nullptr) { - mediastream_event_listener_->notifyMediaStreamEvent(type, message); + boost::mutex::scoped_lock lock(event_listener_mutex_); + if (this->media_stream_event_listener_ != nullptr) { + media_stream_event_listener_->notifyMediaStreamEvent(type, message); } } diff --git a/erizo/src/erizo/MediaStream.h b/erizo/src/erizo/MediaStream.h index 115131796b..5b683acfc6 100644 --- a/erizo/src/erizo/MediaStream.h +++ b/erizo/src/erizo/MediaStream.h @@ -162,8 +162,8 @@ class MediaStream: public MediaSink, public MediaSource, public FeedbackSink, // parses incoming payload type, replaces occurence in buf private: - boost::mutex eventlistener_mutex_; - MediaStreamEventListener* mediastream_event_listener_; + boost::mutex event_listener_mutex_; + MediaStreamEventListener* media_stream_event_listener_; std::shared_ptr connection_; std::string stream_id_; std::string mslabel_; diff --git a/erizo/src/erizo/WebRtcConnection.cpp b/erizo/src/erizo/WebRtcConnection.cpp index b6be8c5b4d..2df31da34f 100644 --- a/erizo/src/erizo/WebRtcConnection.cpp +++ b/erizo/src/erizo/WebRtcConnection.cpp @@ -538,7 +538,7 @@ void WebRtcConnection::onTransportData(std::shared_ptr packet, Trans void WebRtcConnection::maybeNotifyWebRtcConnectionEvent(const WebRTCEvent& event, const std::string& message, const std::string& stream_id) { - boost::mutex::scoped_lock lock(eventlistener_mutex_); + boost::mutex::scoped_lock lock(event_listener_mutex_); if (!conn_event_listener_) { return; } @@ -691,7 +691,7 @@ void WebRtcConnection::setMetadata(std::map metadata) } void WebRtcConnection::setWebRtcConnectionEventListener(WebRtcConnectionEventListener* listener) { - boost::mutex::scoped_lock lock(eventlistener_mutex_); + boost::mutex::scoped_lock lock(event_listener_mutex_); this->conn_event_listener_ = listener; } diff --git a/erizo/src/erizo/WebRtcConnection.h b/erizo/src/erizo/WebRtcConnection.h index 245c3bb2fa..4afeadff2b 100644 --- a/erizo/src/erizo/WebRtcConnection.h +++ b/erizo/src/erizo/WebRtcConnection.h @@ -182,7 +182,7 @@ class WebRtcConnection: public TransportListener, public LogContext, WebRTCEvent global_state_; boost::mutex update_state_mutex_; - boost::mutex eventlistener_mutex_; + boost::mutex event_listener_mutex_; std::shared_ptr worker_; std::shared_ptr io_worker_; diff --git a/erizoAPI/MediaStream.cc b/erizoAPI/MediaStream.cc index e2e880d2d2..e789040aa4 100644 --- a/erizoAPI/MediaStream.cc +++ b/erizoAPI/MediaStream.cc @@ -43,7 +43,7 @@ void StatCallWorker::HandleOKCallback() { callback->Call(1, argv); } -void destroyAsyncStats(uv_handle_t *handle) { +void destroyAsyncHandle(uv_handle_t *handle) { delete handle; } @@ -74,16 +74,16 @@ void MediaStream::close() { me->close(); me.reset(); } - hasStatsCallback_ = false; - hasEventCallback_ = false; + has_stats_callback_ = false; + has_event_callback_ = false; if (!uv_is_closing(reinterpret_cast(async_stats_))) { ELOG_DEBUG("%s, message: Closing Stats handle", toLog()); - uv_close(reinterpret_cast(async_stats_), destroyAsyncStats); + uv_close(reinterpret_cast(async_stats_), destroyAsyncHandle); } async_stats_ = nullptr; if (!uv_is_closing(reinterpret_cast(async_event_))) { ELOG_DEBUG("%s, message: Closing MediaStreamEvent handle", toLog()); - uv_close(reinterpret_cast(async_event_), destroyAsyncStats); + uv_close(reinterpret_cast(async_event_), destroyAsyncHandle); } async_event_ = nullptr; closed_ = true; @@ -362,8 +362,8 @@ NAN_METHOD(MediaStream::getPeriodicStats) { return; } obj->me->setMediaStreamStatsListener(obj); - obj->hasStatsCallback_ = true; - obj->statsCallback_ = new Nan::Callback(info[0].As()); + obj->has_stats_callback_ = true; + obj->stats_callback_ = new Nan::Callback(info[0].As()); } NAN_METHOD(MediaStream::setFeedbackReports) { @@ -385,32 +385,32 @@ NAN_METHOD(MediaStream::onMediaStreamEvent) { return; } me ->setMediaStreamEventListener(obj); - obj->hasEventCallback_ = true; - obj->eventCallback_ = new Nan::Callback(info[0].As()); + obj->has_event_callback_ = true; + obj->event_callback_ = new Nan::Callback(info[0].As()); } void MediaStream::notifyStats(const std::string& message) { - if (!this->hasStatsCallback_) { + if (!this->has_stats_callback_) { return; } if (!async_stats_) { return; } boost::mutex::scoped_lock lock(mutex); - this->statsMsgs.push(message); + this->stats_messages.push(message); async_stats_->data = this; uv_async_send(async_stats_); } void MediaStream::notifyMediaStreamEvent(const std::string& type, const std::string& message) { - if (!this->hasEventCallback_) { + if (!this->has_event_callback_) { return; } if (!async_event_) { return; } boost::mutex::scoped_lock lock(mutex); - this->eventMsgs.push(std::make_pair(type, message)); + this->event_messages.push(std::make_pair(type, message)); async_event_->data = this; uv_async_send(async_event_); } @@ -422,11 +422,11 @@ NAUV_WORK_CB(MediaStream::statsCallback) { return; } boost::mutex::scoped_lock lock(obj->mutex); - if (obj->hasStatsCallback_) { - while (!obj->statsMsgs.empty()) { - Local args[] = {Nan::New(obj->statsMsgs.front().c_str()).ToLocalChecked()}; - Nan::MakeCallback(Nan::GetCurrentContext()->Global(), obj->statsCallback_->GetFunction(), 1, args); - obj->statsMsgs.pop(); + if (obj->has_stats_callback_) { + while (!obj->stats_messages.empty()) { + Local args[] = {Nan::New(obj->stats_messages.front().c_str()).ToLocalChecked()}; + Nan::MakeCallback(Nan::GetCurrentContext()->Global(), obj->stats_callback_->GetFunction(), 1, args); + obj->stats_messages.pop(); } } } @@ -439,12 +439,12 @@ NAUV_WORK_CB(MediaStream::eventCallback) { } boost::mutex::scoped_lock lock(obj->mutex); ELOG_DEBUG("%s, message: eventsCallback", obj->toLog()); - if (obj->hasEventCallback_) { - while (!obj->eventMsgs.empty()) { - Local args[] = {Nan::New(obj->eventMsgs.front().first.c_str()).ToLocalChecked(), - Nan::New(obj->eventMsgs.front().second.c_str()).ToLocalChecked()}; - Nan::MakeCallback(Nan::GetCurrentContext()->Global(), obj->eventCallback_->GetFunction(), 2, args); - obj->eventMsgs.pop(); + if (obj->has_event_callback_) { + while (!obj->event_messages.empty()) { + Local args[] = {Nan::New(obj->event_messages.front().first.c_str()).ToLocalChecked(), + Nan::New(obj->event_messages.front().second.c_str()).ToLocalChecked()}; + Nan::MakeCallback(Nan::GetCurrentContext()->Global(), obj->event_callback_->GetFunction(), 2, args); + obj->event_messages.pop(); } } ELOG_DEBUG("%s, message: eventsCallback finished", obj->toLog()); diff --git a/erizoAPI/MediaStream.h b/erizoAPI/MediaStream.h index ceb52c42b7..399429de19 100644 --- a/erizoAPI/MediaStream.h +++ b/erizoAPI/MediaStream.h @@ -37,8 +37,8 @@ class MediaStream : public MediaSink, public erizo::MediaStreamStatsListener, pu static NAN_MODULE_INIT(Init); std::shared_ptr me; - std::queue statsMsgs; - std::queue> eventMsgs; + std::queue stats_messages; + std::queue> event_messages; boost::mutex mutex; @@ -49,13 +49,13 @@ class MediaStream : public MediaSink, public erizo::MediaStreamStatsListener, pu void close(); std::string toLog(); - Nan::Callback *eventCallback_; + Nan::Callback *event_callback_; uv_async_t *async_event_; - bool hasEventCallback_; + bool has_event_callback_; - Nan::Callback *statsCallback_; + Nan::Callback *stats_callback_; uv_async_t *async_stats_; - bool hasStatsCallback_; + bool has_stats_callback_; bool closed_; std::string id_; std::string label_; diff --git a/erizoAPI/WebRtcConnection.cc b/erizoAPI/WebRtcConnection.cc index c24c6800db..f92b2aa52f 100644 --- a/erizoAPI/WebRtcConnection.cc +++ b/erizoAPI/WebRtcConnection.cc @@ -229,7 +229,7 @@ NAN_METHOD(WebRtcConnection::init) { return; } - obj->eventCallback_ = new Nan::Callback(info[0].As()); + obj->event_callback_ = new Nan::Callback(info[0].As()); bool r = me->init(); info.GetReturnValue().Set(Nan::New(r)); @@ -407,8 +407,8 @@ void WebRtcConnection::notifyEvent(erizo::WebRTCEvent event, const std::string& if (!async_) { return; } - this->eventSts.push(event); - this->eventMsgs.push(std::make_pair(message, stream_id)); + this->event_status.push(event); + this->event_messages.push(std::make_pair(message, stream_id)); async_->data = this; uv_async_send(async_); } @@ -422,13 +422,13 @@ NAUV_WORK_CB(WebRtcConnection::eventsCallback) { } boost::mutex::scoped_lock lock(obj->mutex); ELOG_DEBUG("%s, message: eventsCallback", obj->toLog()); - while (!obj->eventSts.empty()) { - Local args[] = {Nan::New(obj->eventSts.front()), - Nan::New(obj->eventMsgs.front().first.c_str()).ToLocalChecked(), - Nan::New(obj->eventMsgs.front().second.c_str()).ToLocalChecked()}; - Nan::MakeCallback(Nan::GetCurrentContext()->Global(), obj->eventCallback_->GetFunction(), 3, args); - obj->eventMsgs.pop(); - obj->eventSts.pop(); + while (!obj->event_status.empty()) { + Local args[] = {Nan::New(obj->event_status.front()), + Nan::New(obj->event_messages.front().first.c_str()).ToLocalChecked(), + Nan::New(obj->event_messages.front().second.c_str()).ToLocalChecked()}; + Nan::MakeCallback(Nan::GetCurrentContext()->Global(), obj->event_callback_->GetFunction(), 3, args); + obj->event_messages.pop(); + obj->event_status.pop(); } ELOG_DEBUG("%s, message: eventsCallback finished", obj->toLog()); } diff --git a/erizoAPI/WebRtcConnection.h b/erizoAPI/WebRtcConnection.h index c469bd05d1..e9fea54826 100644 --- a/erizoAPI/WebRtcConnection.h +++ b/erizoAPI/WebRtcConnection.h @@ -25,9 +25,8 @@ class WebRtcConnection : public erizo::WebRtcConnectionEventListener, static NAN_MODULE_INIT(Init); std::shared_ptr me; - int eventSt; - std::queue eventSts; - std::queue> eventMsgs; + std::queue event_status; + std::queue> event_messages; boost::mutex mutex; @@ -38,7 +37,7 @@ class WebRtcConnection : public erizo::WebRtcConnectionEventListener, std::string toLog(); void close(); - Nan::Callback *eventCallback_; + Nan::Callback *event_callback_; uv_async_t *async_; bool closed_; std::string id_; diff --git a/erizo_controller/test/utils.js b/erizo_controller/test/utils.js index 60cbfe8b92..112c835af0 100644 --- a/erizo_controller/test/utils.js +++ b/erizo_controller/test/utils.js @@ -173,6 +173,7 @@ var reset = module.exports.reset = function() { generatePLIPacket: sinon.stub(), setSlideShowMode: sinon.stub(), muteStream: sinon.stub(), + onMediaStreamEvent: sinon.stub(), }; module.exports.ExternalInput = {