@@ -8,13 +8,11 @@ namespace ntgcalls {
8
8
Stream::Stream () {
9
9
audio = std::make_shared<AudioStreamer>();
10
10
video = std::make_shared<VideoStreamer>();
11
- streamQueue = std::make_shared<DispatchQueue>(5 );
12
11
updateQueue = std::make_shared<DispatchQueue>();
13
12
}
14
13
15
14
Stream::~Stream () {
16
15
stop ();
17
- streamQueue = nullptr ;
18
16
updateQueue = nullptr ;
19
17
20
18
std::lock_guard lock (mutex);
@@ -71,29 +69,6 @@ namespace ntgcalls {
71
69
}
72
70
}
73
71
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
-
97
72
void Stream::setAVStream (const MediaDescription& streamConfig, const bool noUpgrade) {
98
73
std::lock_guard lock (mutex);
99
74
const auto audioConfig = streamConfig.audio ;
@@ -156,20 +131,30 @@ namespace ntgcalls {
156
131
157
132
Stream::Status Stream::status () {
158
133
std::shared_lock lock (mutex);
159
- if (reader && (reader->audio || reader->video ) && running ) {
134
+ if (reader && (reader->audio || reader->video )) {
160
135
return idling ? Paused : Playing;
161
136
}
162
137
return Idling;
163
138
}
164
139
165
140
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
+ });
173
158
}
174
159
175
160
bool Stream::pause () {
@@ -212,8 +197,11 @@ namespace ntgcalls {
212
197
}
213
198
214
199
void Stream::stop () {
200
+ quit = true ;
201
+ if (thread.joinable ()) {
202
+ thread.join ();
203
+ }
215
204
std::lock_guard lock (mutex);
216
- running = false ;
217
205
idling = false ;
218
206
if (reader) {
219
207
if (reader->audio ) {
0 commit comments