Skip to content

Commit 589971d

Browse files
committed
Optimized Streaming
1 parent cef9fb9 commit 589971d

File tree

2 files changed

+25
-38
lines changed

2 files changed

+25
-38
lines changed

ntgcalls/stream.cpp

Lines changed: 22 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,11 @@ namespace ntgcalls {
88
Stream::Stream() {
99
audio = std::make_shared<AudioStreamer>();
1010
video = std::make_shared<VideoStreamer>();
11-
streamQueue = std::make_shared<DispatchQueue>(5);
1211
updateQueue = std::make_shared<DispatchQueue>();
1312
}
1413

1514
Stream::~Stream() {
1615
stop();
17-
streamQueue = nullptr;
1816
updateQueue = nullptr;
1917

2018
std::lock_guard lock(mutex);
@@ -71,29 +69,6 @@ namespace ntgcalls {
7169
}
7270
}
7371

74-
void Stream::sendSample() {
75-
std::shared_lock lock(mutex);
76-
if (running) {
77-
if (idling || !reader || !(reader->audio || reader->video)) {
78-
lock.unlock();
79-
std::this_thread::sleep_for(std::chrono::milliseconds(500));
80-
lock.lock();
81-
} else {
82-
if (auto [fst, snd] = unsafePrepareForSample(lock); fst && snd) {
83-
if (const auto sample = snd->read()) {
84-
fst->sendData(sample);
85-
}
86-
}
87-
checkStream();
88-
}
89-
if (streamQueue) {
90-
streamQueue->dispatch([&] {
91-
sendSample();
92-
});
93-
}
94-
}
95-
}
96-
9772
void Stream::setAVStream(const MediaDescription& streamConfig, const bool noUpgrade) {
9873
std::lock_guard lock(mutex);
9974
const auto audioConfig = streamConfig.audio;
@@ -156,20 +131,30 @@ namespace ntgcalls {
156131

157132
Stream::Status Stream::status() {
158133
std::shared_lock lock(mutex);
159-
if (reader && (reader->audio || reader->video) && running) {
134+
if (reader && (reader->audio || reader->video)) {
160135
return idling ? Paused : Playing;
161136
}
162137
return Idling;
163138
}
164139

165140
void Stream::start() {
166-
std::lock_guard lock(mutex);
167-
if (!running) {
168-
running = true;
169-
streamQueue->dispatch([&] {
170-
sendSample();
171-
});
172-
}
141+
thread = std::thread([this] {
142+
do {
143+
std::shared_lock lock(mutex);
144+
if (idling || !reader || !(reader->audio || reader->video)) {
145+
lock.unlock();
146+
std::this_thread::sleep_for(std::chrono::milliseconds(500));
147+
lock.lock();
148+
} else {
149+
if (auto [fst, snd] = unsafePrepareForSample(lock); fst && snd) {
150+
if (const auto sample = snd->read()) {
151+
fst->sendData(sample);
152+
}
153+
}
154+
checkStream();
155+
}
156+
} while (!quit);
157+
});
173158
}
174159

175160
bool Stream::pause() {
@@ -212,8 +197,11 @@ namespace ntgcalls {
212197
}
213198

214199
void Stream::stop() {
200+
quit = true;
201+
if (thread.joinable()) {
202+
thread.join();
203+
}
215204
std::lock_guard lock(mutex);
216-
running = false;
217205
idling = false;
218206
if (reader) {
219207
if (reader->audio) {

ntgcalls/stream.hpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,14 @@ namespace ntgcalls {
6363
std::shared_ptr<VideoStreamer> video;
6464
wrtc::MediaStreamTrack *audioTrack{}, *videoTrack{};
6565
std::shared_ptr<MediaReaderFactory> reader;
66-
bool running = false, idling = false, hasVideo = false;
66+
bool idling = false;
67+
std::atomic_bool hasVideo = false, quit = false;
6768
wrtc::synchronized_callback<Type> onEOF;
6869
wrtc::synchronized_callback<MediaState> onChangeStatus;
69-
std::shared_ptr<DispatchQueue> streamQueue;
70+
std::thread thread;
7071
std::shared_ptr<DispatchQueue> updateQueue;
7172
std::shared_mutex mutex;
7273

73-
void sendSample();
74-
7574
void checkStream() const;
7675

7776
std::pair<std::shared_ptr<BaseStreamer>, std::shared_ptr<BaseReader>> unsafePrepareForSample(std::shared_lock<std::shared_mutex>& lock) const;

0 commit comments

Comments
 (0)