diff --git a/erizo/src/erizo/WebRtcConnection.cpp b/erizo/src/erizo/WebRtcConnection.cpp index 9c642e6460..38fe2dba06 100644 --- a/erizo/src/erizo/WebRtcConnection.cpp +++ b/erizo/src/erizo/WebRtcConnection.cpp @@ -662,7 +662,7 @@ void WebRtcConnection::updateState(TransportState state, Transport * transport) global_state_ = temp; - ELOG_INFO("%s newGlobalState: %d", toLog(), global_state_); + ELOG_INFO("%s newGlobalState: %d", toLog(), temp); maybeNotifyWebRtcConnectionEvent(global_state_, msg); } diff --git a/erizo/src/erizo/WebRtcConnection.h b/erizo/src/erizo/WebRtcConnection.h index 4da0552a74..a202aae7a2 100644 --- a/erizo/src/erizo/WebRtcConnection.h +++ b/erizo/src/erizo/WebRtcConnection.h @@ -163,6 +163,9 @@ class WebRtcConnection: public TransportListener, public LogContext, void maybeNotifyWebRtcConnectionEvent(const WebRTCEvent& event, const std::string& message, const std::string& stream_id = ""); + protected: + std::atomic global_state_; + private: std::string connection_id_; bool audio_enabled_; @@ -180,7 +183,6 @@ class WebRtcConnection: public TransportListener, public LogContext, std::shared_ptr video_transport_, audio_transport_; std::shared_ptr stats_; - WebRTCEvent global_state_; boost::mutex update_state_mutex_; boost::mutex event_listener_mutex_; diff --git a/erizo/src/erizo/rtp/FakeKeyframeGeneratorHandler.cpp b/erizo/src/erizo/rtp/FakeKeyframeGeneratorHandler.cpp index a2a178a23f..2e7c92fb54 100644 --- a/erizo/src/erizo/rtp/FakeKeyframeGeneratorHandler.cpp +++ b/erizo/src/erizo/rtp/FakeKeyframeGeneratorHandler.cpp @@ -16,7 +16,7 @@ constexpr uint64_t kPliPeriodMs = 300; FakeKeyframeGeneratorHandler::FakeKeyframeGeneratorHandler() : stream_{nullptr}, enabled_{true}, first_keyframe_received_{false}, plis_scheduled_{false}, video_source_ssrc_{0}, video_sink_ssrc_{0} { - } +} void FakeKeyframeGeneratorHandler::enable() { enabled_ = true; @@ -43,15 +43,15 @@ void FakeKeyframeGeneratorHandler::read(Context *ctx, std::shared_ptr packet) { RtcpHeader *chead = reinterpret_cast(packet->data); - if (enabled_) { - if (!first_keyframe_received_ && packet->type == VIDEO_PACKET && !chead->isRtcp()) { + if (!enabled_) { + ctx->fireWrite(std::move(packet)); + return; + } + + if (!first_keyframe_received_ && packet->type == VIDEO_PACKET && !chead->isRtcp()) { + if (stream_->getCurrentState() == CONN_READY) { // TODO(pedro): Find a solution for this in all handlers if (!packet->is_keyframe) { - if (!plis_scheduled_) { - plis_scheduled_ = true; - ELOG_DEBUG("Scheduling PLIs"); - sendPLI(); - schedulePLI(); - } + maybeSendAndSchedulePLIs(); ELOG_DEBUG("Building a black keyframe from packet"); auto keyframe_packet = transformIntoKeyframePacket(packet); ctx->fireWrite(keyframe_packet); @@ -60,38 +60,51 @@ void FakeKeyframeGeneratorHandler::write(Context *ctx, std::shared_ptrfireWrite(std::move(packet)); } std::shared_ptr FakeKeyframeGeneratorHandler::transformIntoKeyframePacket -(std::shared_ptr packet) { - if (packet->codec == "VP8") { - auto keyframe_packet = RtpUtils::makeVP8BlackKeyframePacket(packet); - return keyframe_packet; - } else { - ELOG_DEBUG("Generate keyframe packet is not available for codec %s", packet->codec); - return packet; + (std::shared_ptr packet) { + if (packet->codec == "VP8") { + auto keyframe_packet = RtpUtils::makeVP8BlackKeyframePacket(packet); + return keyframe_packet; + } else { + ELOG_DEBUG("Generate keyframe packet is not available for codec %s", packet->codec); + return packet; + } + } + +void FakeKeyframeGeneratorHandler::maybeSendAndSchedulePLIs() { + if (!plis_scheduled_) { + plis_scheduled_ = true; + ELOG_DEBUG("Scheduling PLIs"); + sendPLI(); + schedulePLI(); } } + void FakeKeyframeGeneratorHandler::sendPLI() { getContext()->fireRead(RtpUtils::createPLI(video_sink_ssrc_, video_source_ssrc_)); } + void FakeKeyframeGeneratorHandler::schedulePLI() { std::weak_ptr weak_this = shared_from_this(); stream_->getWorker()->scheduleEvery([weak_this] { - if (auto this_ptr = weak_this.lock()) { - if (!this_ptr->first_keyframe_received_) { - ELOG_DEBUG("Sending PLI in FakeGenerator, scheduling another"); - this_ptr->sendPLI(); - return true; - } else { - ELOG_DEBUG("Stop sending scheduled PLI packets"); - return false; - } + if (auto this_ptr = weak_this.lock()) { + if (!this_ptr->first_keyframe_received_) { + ELOG_DEBUG("Sending PLI in FakeGenerator, scheduling another"); + this_ptr->sendPLI(); + return true; + } else { + ELOG_DEBUG("Stop sending scheduled PLI packets"); + return false; } - return false; - }, std::chrono::milliseconds(kPliPeriodMs)); + } + return false; + }, std::chrono::milliseconds(kPliPeriodMs)); } } // namespace erizo diff --git a/erizo/src/erizo/rtp/FakeKeyframeGeneratorHandler.h b/erizo/src/erizo/rtp/FakeKeyframeGeneratorHandler.h index b14ec1e553..87f529ad72 100644 --- a/erizo/src/erizo/rtp/FakeKeyframeGeneratorHandler.h +++ b/erizo/src/erizo/rtp/FakeKeyframeGeneratorHandler.h @@ -34,6 +34,7 @@ class FakeKeyframeGeneratorHandler: public Handler, public std::enable_shared_fr private: std::shared_ptr transformIntoKeyframePacket(std::shared_ptr packet); + void maybeSendAndSchedulePLIs(); void sendPLI(); void schedulePLI(); diff --git a/erizo/src/test/log4cxx.properties b/erizo/src/test/log4cxx.properties index 005ddd8cfb..e3f2a27e55 100644 --- a/erizo/src/test/log4cxx.properties +++ b/erizo/src/test/log4cxx.properties @@ -49,6 +49,7 @@ log4j.logger.rtp.RtpPacketQueue=ERROR log4j.logger.rtp.RtpRetransmissionHandler=ERROR log4j.logger.rtp.RtpVP8Fragmenter=ERROR log4j.logger.rtp.RtpVP8Parser=ERROR +log4j.logger.rtp.FakeKeyframeGeneratorHandler=ERROR log4j.logger.rtp.RtpSlideShowHandler=ERROR log4j.logger.rtp.RtpTrackMuteHandler=ERROR log4j.logger.rtp.RtpSink=ERROR diff --git a/erizo/src/test/utils/Mocks.h b/erizo/src/test/utils/Mocks.h index 8210e9943e..33183ec2ce 100644 --- a/erizo/src/test/utils/Mocks.h +++ b/erizo/src/test/utils/Mocks.h @@ -91,7 +91,9 @@ class MockWebRtcConnection: public WebRtcConnection { public: MockWebRtcConnection(std::shared_ptr worker, std::shared_ptr io_worker, const IceConfig &ice_config, const std::vector rtp_mappings) : - WebRtcConnection(worker, io_worker, "", ice_config, rtp_mappings, std::vector(), nullptr) {} + WebRtcConnection(worker, io_worker, "", ice_config, rtp_mappings, std::vector(), nullptr) { + global_state_ = CONN_READY; + } virtual ~MockWebRtcConnection() { }