Skip to content

Commit

Permalink
Add ClosableStream support to ReplayBuffer
Browse files Browse the repository at this point in the history
  • Loading branch information
palana committed Sep 24, 2014
1 parent 3069c2e commit 25c1a11
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 17 deletions.
3 changes: 1 addition & 2 deletions Source/OBS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -816,8 +816,7 @@ OBS::OBS()

OBS::~OBS()
{
Stop(true);
StopReplayBuffer();
Stop(true, true);

bShuttingDown = true;

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

std::unique_ptr<VideoFileStream> replayBufferStream;
ReplayBuffer *replayBuffer;
StopInfo replayBufferStop;

bool bRequestKeyframe;
int keyframeWait;
Expand Down Expand Up @@ -1017,11 +1018,11 @@ class OBS
void ResetItemCrops();

void Start(bool recordingOnly=false, bool replayBufferOnly=false);
void Stop(bool overrideKeepRecording=false);
void Stop(bool overrideKeepRecording=false, bool stopReplayBuffer=false);
bool StartRecording(bool force=false);
void StopRecording();
void StartReplayBuffer();
void StopReplayBuffer();
void StopReplayBuffer(bool immediate=false);

static void STDCALL StartStreamHotkey(DWORD hotkey, UPARAM param, bool bDown);
static void STDCALL StopStreamHotkey(DWORD hotkey, UPARAM param, bool bDown);
Expand Down
31 changes: 22 additions & 9 deletions Source/OBSCapture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,20 +109,32 @@ void OBS::StartReplayBuffer()
ConfigureStreamButtons();
}

void OBS::StopReplayBuffer()
void OBS::StopReplayBuffer(bool immediate)
{
if (!replayBufferStream) return;

bRecordingReplayBuffer = false;
ReportStopRecordingReplayBufferTrigger();
if (!immediate && replayBufferStop.func) return;

if (!bStreaming && !bRecording && bRunning) Stop(true);
auto shutdown = [this]()
{
bRecordingReplayBuffer = false;
ReportStopRecordingReplayBufferTrigger();

auto stream = move(replayBufferStream);
if (!bStreaming && !bRecording && bRunning) PostStopMessage(true);

replayBuffer = nullptr;
auto stream = move(replayBufferStream);

ConfigureStreamButtons();
replayBuffer = nullptr;

ConfigureStreamButtons();
};

if (immediate)
return shutdown();

replayBufferStop.func = shutdown;

replayBufferStop.time = (DWORD)(GetVideoTime() - firstFrameTimestamp);
}

String ExpandRecordingFilename(String filename)
Expand Down Expand Up @@ -876,7 +888,7 @@ void OBS::Start(bool recordingOnly, bool replayBufferOnly)
ConfigureStreamButtons();
}

void OBS::Stop(bool overrideKeepRecording)
void OBS::Stop(bool overrideKeepRecording, bool stopReplayBuffer)
{
if((!bStreaming && !bRecording && !bRunning && !bRecordingReplayBuffer) && (!bTestStream)) return;

Expand All @@ -886,7 +898,7 @@ void OBS::Stop(bool overrideKeepRecording)

int networkMode = AppConfig->GetInt(TEXT("Publish"), TEXT("Mode"), 2);

if(((!overrideKeepRecording && bRecording && bKeepRecording) || bRecordingReplayBuffer) && networkMode == 0) {
if((!overrideKeepRecording && ((bRecording && bKeepRecording) || (!stopReplayBuffer && bRecordingReplayBuffer))) && networkMode == 0) {
videoEncoder->RequestKeyframe();

Log(TEXT("=====Stream End (recording continues): %s========================="), CurrentDateTimeString().Array());
Expand Down Expand Up @@ -967,6 +979,7 @@ void OBS::Stop(bool overrideKeepRecording)
bStreaming = false;

if(bRecording) StopRecording();
if (bRecordingReplayBuffer) StopReplayBuffer(true);

delete micAudio;
micAudio = NULL;
Expand Down
5 changes: 4 additions & 1 deletion Source/OBSVideoCapture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,10 @@ void OBS::SendFrame(VideoSegment &curSegment, QWORD firstFrameTime)
if (fileStream)
fileStream->AddPacket(shared_data, curSegment.timestamp, curSegment.pts, packet.type);
if (replayBufferStream)
replayBufferStream->AddPacket(shared_data, curSegment.timestamp, curSegment.pts, packet.type);
{
if (!HandleStreamStopInfo(replayBufferStop, packet.type, curSegment))
replayBufferStream->AddPacket(shared_data, curSegment.timestamp, curSegment.pts, packet.type);
}
}
}
}
Expand Down
8 changes: 5 additions & 3 deletions Source/ReplayBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ namespace
using packet_vec_t = deque<shared_ptr<const packet_t>>;
}

void CreateRecordingHelper(VideoFileStream *&stream, packet_list_t &packets);
void CreateRecordingHelper(unique_ptr<VideoFileStream> &stream, packet_list_t &packets);

static DWORD STDCALL SaveReplayBufferThread(void *arg);

Expand Down Expand Up @@ -66,6 +66,8 @@ struct ReplayBuffer : VideoFileStream
for (auto &thread : threads)
if (WaitForSingleObject(thread.second.get(), seconds * 100) != WAIT_OBJECT_0)
OSTerminateThread(thread.first.release(), 0);
else
App->AddPendingStreamThread(thread.first.release());
}

virtual void AddPacket(const BYTE *data, UINT size, DWORD timestamp, DWORD pts, PacketType type) override
Expand Down Expand Up @@ -369,7 +371,7 @@ struct RecordingHelper : VideoFileStream
}
};

void CreateRecordingHelper(VideoFileStream *&stream, packet_list_t &packets)
void CreateRecordingHelper(unique_ptr<VideoFileStream> &stream, packet_list_t &packets)
{
if (stream)
{
Expand All @@ -382,7 +384,7 @@ void CreateRecordingHelper(VideoFileStream *&stream, packet_list_t &packets)

auto helper = make_unique<RecordingHelper>(packet_vec_t{begin(packets), end(packets)});
if (helper->StartRecording())
stream = helper.release();
stream.reset(helper.release());
}

pair<ReplayBuffer*, unique_ptr<VideoFileStream>> CreateReplayBuffer(int seconds)
Expand Down

0 comments on commit 25c1a11

Please sign in to comment.