@@ -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 ) {
0 commit comments