@@ -45,6 +45,23 @@ SOFTWARE.
45
45
#include < utility>
46
46
#include < vector>
47
47
48
+ // / The major version number
49
+ #define NADJIEB_MJPEG_STREAMER_VERSION_MAJOR 1
50
+
51
+ // / The minor version number
52
+ #define NADJIEB_MJPEG_STREAMER_VERSION_MINOR 1
53
+
54
+ // / The patch number
55
+ #define NADJIEB_MJPEG_STREAMER_VERSION_PATCH 0
56
+
57
+ // / The complete version number
58
+ #define NADJIEB_MJPEG_STREAMER_VERSION_CODE \
59
+ (NADJIEB_MJPEG_STREAMER_VERSION_MAJOR * 10000 + NADJIEB_MJPEG_STREAMER_VERSION_MINOR * 100 + \
60
+ NADJIEB_MJPEG_STREAMER_VERSION_PATCH)
61
+
62
+ // / Version number as string
63
+ #define NADJIEB_MJPEG_STREAMER_VERSION_STRING " 10100"
64
+
48
65
namespace nadjieb
49
66
{
50
67
constexpr int NUM_SEND_MUTICES = 100 ;
@@ -57,7 +74,8 @@ class MJPEGStreamer
57
74
void start ();
58
75
void stop ();
59
76
void publish (const std::string &path, const std::string &buffer);
60
- bool shutdownFromBrowser ();
77
+ void setShutdownTarget (const std::string &target);
78
+ bool isAlive ();
61
79
62
80
private:
63
81
struct Payload
@@ -70,8 +88,8 @@ class MJPEGStreamer
70
88
int port_;
71
89
int master_socket_ = -1 ;
72
90
int num_workers_;
73
- bool shutdownFlag = false ;
74
91
struct sockaddr_in address_;
92
+ std::string shutdown_target_ = " /shutdown" ;
75
93
76
94
std::thread thread_listener_;
77
95
std::mutex clients_mutex_;
@@ -128,7 +146,7 @@ void MJPEGStreamer::start()
128
146
for (auto i = 0 ; i < num_workers_; ++i)
129
147
{
130
148
workers_.emplace_back ([this ]() {
131
- while (this ->master_socket_ > 0 )
149
+ while (this ->isAlive () )
132
150
{
133
151
Payload payload;
134
152
@@ -190,7 +208,7 @@ void MJPEGStreamer::start()
190
208
to.tv_sec = 1 ;
191
209
to.tv_usec = 0 ;
192
210
193
- while (this ->master_socket_ > 0 )
211
+ while (this ->isAlive () )
194
212
{
195
213
FD_SET (this ->master_socket_ , &fd);
196
214
@@ -207,19 +225,19 @@ void MJPEGStreamer::start()
207
225
std::string req (4096 , 0 );
208
226
::read (new_socket, &req[0 ], req.size());
209
227
210
- std::string path;
211
- if (!req.empty ())
228
+ if (req.empty ())
212
229
{
213
- path = req.substr (req.find (" GET" ) + 4 , req.find (" HTTP/" ) - req.find (" GET" ) - 5 );
214
-
215
- if (path == " /shutdown" )
216
- {
217
- shutdownFlag = true ;
218
- }
230
+ ::close (new_socket);
231
+ continue ;
219
232
}
220
- else
233
+
234
+ auto path = req.substr (req.find (" GET" ) + 4 , req.find (" HTTP/" ) - req.find (" GET" ) - 5 );
235
+ if (path == this ->shutdown_target_ )
221
236
{
222
237
::close (new_socket);
238
+ std::unique_lock<std::mutex> lock (this ->payloads_mutex_ );
239
+ this ->master_socket_ = -1 ;
240
+ this ->condition_ .notify_all ();
223
241
continue ;
224
242
}
225
243
@@ -239,7 +257,7 @@ void MJPEGStreamer::start()
239
257
240
258
void MJPEGStreamer::stop ()
241
259
{
242
- if (master_socket_ > 0 )
260
+ if (isAlive () )
243
261
{
244
262
std::unique_lock<std::mutex> lock (payloads_mutex_);
245
263
master_socket_ = -1 ;
@@ -288,18 +306,22 @@ void MJPEGStreamer::publish(const std::string &path, const std::string &buffer)
288
306
clients = path2clients_[path];
289
307
}
290
308
309
+ for (auto i : path2clients_[path])
291
310
{
292
- for (auto i : path2clients_[path])
293
- {
294
- std::unique_lock<std::mutex> lock (payloads_mutex_);
295
- payloads_.emplace (Payload ({buffer, path, i}));
296
- condition_.notify_one ();
297
- }
311
+ std::unique_lock<std::mutex> lock (payloads_mutex_);
312
+ payloads_.emplace (Payload ({buffer, path, i}));
313
+ condition_.notify_one ();
298
314
}
299
315
}
300
316
301
- bool MJPEG_streamer_module::shutdownFromBrowser ()
317
+ void MJPEGStreamer::setShutdownTarget (const std::string &target)
318
+ {
319
+ shutdown_target_ = target;
320
+ }
321
+
322
+ bool MJPEGStreamer::isAlive ()
302
323
{
303
- return shutdownFlag;
324
+ std::unique_lock<std::mutex> lock (payloads_mutex_);
325
+ return master_socket_ > 0 ;
304
326
}
305
327
} // namespace nadjieb
0 commit comments