diff --git a/Source/OBS.cpp b/Source/OBS.cpp index 6bfbfbc9c..8823c02ca 100644 --- a/Source/OBS.cpp +++ b/Source/OBS.cpp @@ -1368,7 +1368,7 @@ void OBS::RefreshStreamButtons(bool disable) if (bShuttingDown) return; int networkMode = AppConfig->GetInt(TEXT("Publish"), TEXT("Mode"), 2); bRecordingOnly = (networkMode == 1); - bool canStream = networkMode == 0 && !bTestStream; + bool canStream = networkMode == 0 && !bTestStream && bStreamFlushed; canRecord = !bTestStream; bool canTest = !bRecordingReplayBuffer && !bRecording && (!bStreaming || bTestStream); diff --git a/Source/OBS.h b/Source/OBS.h index c785489e5..2e1d42818 100644 --- a/Source/OBS.h +++ b/Source/OBS.h @@ -648,6 +648,7 @@ class OBS //--------------------------------------------------- std::unique_ptr network; + StopInfo networkStop; //--------------------------------------------------- // audio sources/encoder @@ -740,7 +741,7 @@ class OBS String strLanguage; bool bTestStream; bool bUseMultithreadedOptimizations; - bool bRunning, bRecording, bRecordingReplayBuffer, bRecordingOnly, bStartingUp, bStreaming, bKeepRecording; + bool bRunning, bRecording, bRecordingReplayBuffer, bRecordingOnly, bStartingUp, bStreaming, bStreamFlushed = true, bKeepRecording; bool canRecord; volatile bool bShutdownVideoThread, bShutdownEncodeThread; int renderFrameWidth, renderFrameHeight; // The size of the preview only diff --git a/Source/OBSCapture.cpp b/Source/OBSCapture.cpp index d0729605f..914b5eeed 100644 --- a/Source/OBSCapture.cpp +++ b/Source/OBSCapture.cpp @@ -337,9 +337,11 @@ void OBS::Start(bool recordingOnly, bool replayBufferOnly) if (bRecording && networkMode != 0) return; - if((bRecording || bRecordingReplayBuffer) && networkMode == 0 && delayTime == 0 && !recordingOnly && !replayBufferOnly) { - bFirstConnect = !bReconnecting; + if (!bRunning && !bStreamFlushed && !recordingOnly && !replayBufferOnly) return; + if((bRecording || bRecordingReplayBuffer) && networkMode == 0 && delayTime == 0 && !recordingOnly && !replayBufferOnly && bStreamFlushed) { + bFirstConnect = !bReconnecting; + network.reset(CreateRTMPPublisher()); Log(TEXT("=====Stream Start (while recording): %s============================="), CurrentDateTimeString().Array()); @@ -460,7 +462,7 @@ void OBS::Start(bool recordingOnly, bool replayBufferOnly) bFirstConnect = !bReconnecting; - if(bTestStream || recordingOnly || replayBufferOnly) + if(bTestStream || recordingOnly || replayBufferOnly || !bStreamFlushed) network.reset(CreateNullNetwork()); else { @@ -918,16 +920,36 @@ void OBS::Stop(bool overrideKeepRecording, bool stopReplayBuffer) if((!overrideKeepRecording && ((bRecording && bKeepRecording) || (!stopReplayBuffer && bRecordingReplayBuffer))) && networkMode == 0) { videoEncoder->RequestKeyframe(); - Log(TEXT("=====Stream End (recording continues): %s========================="), CurrentDateTimeString().Array()); - network.reset(); + if (!networkStop.func && network) + { + networkStop.func = [this]() + { + videoEncoder->RequestKeyframe(); + auto stream = move(network); - bStreaming = false; - bSentHeaders = false; + Log(TEXT("=====Stream End (recording continues): %s========================="), CurrentDateTimeString().Array()); - ReportStopStreamingTrigger(); + bStreamFlushed = false; - ConfigureStreamButtons(); + ConfigureStreamButtons(); + + AddPendingStream(stream.release(), [this]() + { + bStreaming = false; + bSentHeaders = false; + + ReportStopStreamingTrigger(); + + bStreamFlushed = true; + + ConfigureStreamButtons(); + + if (!bStreaming && !bRecordingReplayBuffer && bRunning && !bRecording) PostStopMessage(true); + }); + }; + networkStop.time = (DWORD)(GetVideoTime() - firstFrameTimestamp); + } OSLeaveMutex(hHotkeyMutex); @@ -991,9 +1013,24 @@ void OBS::Stop(bool overrideKeepRecording, bool stopReplayBuffer) //------------------------------------------------------------- - network.reset(); - if (bStreaming) ReportStopStreamingTrigger(); - bStreaming = false; + if (bStreaming) + { + bStreamFlushed = false; + AddPendingStream(network.release(), [this]() + { + ReportStopStreamingTrigger(); + bStreaming = false; + bStreamFlushed = true; + bTestStream = false; + ConfigureStreamButtons(); + }); + } + else + { + network.reset(); + bStreaming = false; + bTestStream = false; + } if (bRecording) StopRecording(true); if (bRecordingReplayBuffer) StopReplayBuffer(true); @@ -1123,8 +1160,6 @@ void OBS::Stop(bool overrideKeepRecording, bool stopReplayBuffer) if (scmp(processPriority, TEXT("Normal"))) SetPriorityClass(GetCurrentProcess(), NORMAL_PRIORITY_CLASS); - bTestStream = false; - ConfigureStreamButtons(); UpdateRenderViewMessage(); diff --git a/Source/OBSVideoCapture.cpp b/Source/OBSVideoCapture.cpp index 4816c7b33..4493f64c9 100644 --- a/Source/OBSVideoCapture.cpp +++ b/Source/OBSVideoCapture.cpp @@ -208,8 +208,11 @@ void OBS::SendFrame(VideoSegment &curSegment, QWORD firstFrameTime) //Log(TEXT("v:%u, %llu"), curSegment.timestamp, frameInfo.firstFrameTime+curSegment.timestamp); - if(network) - network->SendPacket(packet.data.Array(), packet.data.Num(), curSegment.timestamp, packet.type); + if (network) + { + if (!HandleStreamStopInfo(networkStop, packet.type, curSegment)) + network->SendPacket(packet.data.Array(), packet.data.Num(), curSegment.timestamp, packet.type); + } if (fileStream || replayBufferStream) {