Skip to content

Commit

Permalink
Add ClosableStream support to network streams
Browse files Browse the repository at this point in the history
  • Loading branch information
palana committed Sep 24, 2014
1 parent 70b4322 commit c497f71
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 18 deletions.
2 changes: 1 addition & 1 deletion Source/OBS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
3 changes: 2 additions & 1 deletion Source/OBS.h
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,7 @@ class OBS
//---------------------------------------------------

std::unique_ptr<NetworkStream> network;
StopInfo networkStop;

//---------------------------------------------------
// audio sources/encoder
Expand Down Expand Up @@ -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
Expand Down
63 changes: 49 additions & 14 deletions Source/OBSCapture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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
{
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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();
Expand Down
7 changes: 5 additions & 2 deletions Source/OBSVideoCapture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down

0 comments on commit c497f71

Please sign in to comment.