diff --git a/java/src/edu/wpi/cscore/VideoSink.java b/java/src/edu/wpi/cscore/VideoSink.java index bb84780b6f..205a77ad7b 100644 --- a/java/src/edu/wpi/cscore/VideoSink.java +++ b/java/src/edu/wpi/cscore/VideoSink.java @@ -84,7 +84,11 @@ public class VideoSink { /// provide frames to multiple clients. /// @param source Source public void setSource(VideoSource source) { - CameraServerJNI.setSinkSource(m_handle, source.m_handle); + if (source == null) { + CameraServerJNI.setSinkSource(m_handle, 0); + } else { + CameraServerJNI.setSinkSource(m_handle, source.m_handle); + } } /// Get the connected source. diff --git a/src/MjpegServerImpl.cpp b/src/MjpegServerImpl.cpp index 29281de006..164411f813 100644 --- a/src/MjpegServerImpl.cpp +++ b/src/MjpegServerImpl.cpp @@ -671,9 +671,9 @@ void MjpegServerImpl::SetSourceImpl(std::shared_ptr source) { if (auto thr = connThread.GetThread()) { if (thr->m_source != source) { bool streaming = thr->m_streaming; - if (streaming) thr->m_source->DisableSink(); + if (thr->m_source && streaming) thr->m_source->DisableSink(); thr->m_source = source; - if (streaming) thr->m_source->EnableSink(); + if (source && streaming) thr->m_source->EnableSink(); } } } diff --git a/src/SinkImpl.cpp b/src/SinkImpl.cpp index f843c6fe1c..803b2d6923 100644 --- a/src/SinkImpl.cpp +++ b/src/SinkImpl.cpp @@ -67,13 +67,16 @@ void SinkImpl::SetEnabled(bool enabled) { void SinkImpl::SetSource(std::shared_ptr source) { { std::lock_guard lock(m_mutex); + if (m_source == source) return; if (m_source) { if (m_enabledCount > 0) m_source->DisableSink(); m_source->RemoveSink(); } m_source = source; - m_source->AddSink(); - if (m_enabledCount > 0) m_source->EnableSink(); + if (m_source) { + m_source->AddSink(); + if (m_enabledCount > 0) m_source->EnableSink(); + } } SetSourceImpl(source); } diff --git a/src/cscore_cpp.cpp b/src/cscore_cpp.cpp index 2c9274f5b7..1654108721 100644 --- a/src/cscore_cpp.cpp +++ b/src/cscore_cpp.cpp @@ -407,12 +407,16 @@ void SetSinkSource(CS_Sink sink, CS_Source source, CS_Status* status) { *status = CS_INVALID_HANDLE; return; } - auto sourceData = Sources::GetInstance().Get(source); - if (!sourceData) { - *status = CS_INVALID_HANDLE; - return; + if (source == 0) { + data->sink->SetSource(nullptr); + } else { + auto sourceData = Sources::GetInstance().Get(source); + if (!sourceData) { + *status = CS_INVALID_HANDLE; + return; + } + data->sink->SetSource(sourceData->source); } - data->sink->SetSource(sourceData->source); data->sourceHandle.store(source); Notifier::GetInstance().NotifySinkSourceChanged(data->sink->GetName(), sink, source);