Comments (2)
I also created simple flask streaming scirpt, which works as expected(showing mjpeg stream in browsers).
import io
import random
from flask import Flask, Response
from flask import make_response
from PIL import Image
import time
app = Flask(__name__)
@app.route('/stream')
def stream():
def generate_noise_image():
while True:
# Generate a random noise image
img = Image.frombytes('RGB', (640, 480), bytes(random.getrandbits(8) for _ in range(640 * 480 * 3)))
# Convert the image to JPEG format
img_io = io.BytesIO()
img.save(img_io, 'JPEG')
img_io.seek(0)
# Yield the image data
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + img_io.getvalue() + b'\r\n')
#pause script execution for half a second
time.sleep(0.5)
return Response(generate_noise_image(), mimetype='multipart/x-mixed-replace; boundary=frame')
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8013, debug=True)
I tried to analyze the difference in responses between Flask and Pistache.
Here is Flask with curl:
curl -v 127.0.0.1:8013/stream
* Trying 127.0.0.1:8013...
* Connected to 127.0.0.1 (127.0.0.1) port 8013 (#0)
> GET /stream HTTP/1.1
> Host: 127.0.0.1:8013
> User-Agent: curl/7.81.0
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Server: Werkzeug/2.2.2 Python/3.10.12
< Date: Sat, 27 Apr 2024 04:44:57 GMT
< Content-Type: multipart/x-mixed-replace; boundary=frame
< Transfer-Encoding: chunked
< Connection: close
<
Warning: Binary output can mess up your terminal. Use "--output -" to tell
Warning: curl to output it to your terminal anyway, or consider "--output
Warning: <FILE>" to save to a file.
* Failure writing output to destination
* Failed reading the chunked-encoded stream
* Closing connection 0
I wanted to see the body of request, so I replaced image with shorter string:
curl --output - -v 127.0.0.1:8013/stream
* Trying 127.0.0.1:8013...
* Connected to 127.0.0.1 (127.0.0.1) port 8013 (#0)
> GET /stream HTTP/1.1
> Host: 127.0.0.1:8013
> User-Agent: curl/7.81.0
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Server: Werkzeug/2.2.2 Python/3.10.12
< Date: Fri, 26 Apr 2024 14:38:44 GMT
< Content-Type: multipart/x-mixed-replace; boundary=frame
< Transfer-Encoding: chunked
< Connection: close
<
--frame
Content-Type: image/jpeg
happy string
--frame
Content-Type: image/jpeg
happy string
--frame
Content-Type: image/jpeg
happy string
--frame
Content-Type: image/jpeg
I noticed that Flask sends response with "chunked" encoding. So through Pistache docs I wrote new function:
void VideoStreamer::streamVideoFramesChunked(const Pistache::Rest::Request& request, Pistache::Http::ResponseWriter response)
{
response.headers()
.add<Pistache::Http::Header::Server>("pistache/0.1")
.add<Pistache::Http::Header::Connection>(Pistache::Http::ConnectionControl::KeepAlive)
.add<Pistache::Http::Header::ContentType>("multipart/x-mixed-replace; boundary=frame");
auto stream = response.stream(Pistache::Http::Code::Ok);
auto cameraId = request.param(":cameraId").as<std::string>();
const int camSearchTimeout = 20000; // ms
int time_passed = 0; // ms
const int frameRetrievalInterval = 500; //ms
uint64_t lastFrameTimestamp = UINT64_MAX;
try {
while (true)
{
std::vector<unsigned char> frameBuffer;
{
std::lock_guard<std::mutex> lock(frameMutex);
auto it = frameMap.find(cameraId);
if (it != frameMap.end() && it->second.second != lastFrameTimestamp) {
cv::imencode(".jpg", it->second.first, frameBuffer);
frameBuffer.size() > 0 ? time_passed = 0 : time_passed += frameRetrievalInterval;
lastFrameTimestamp = it->second.second;
}
else {
time_passed += frameRetrievalInterval;
if ((time_passed % 5000) == 0) UMOJO_LOG("Pistache: Waiting for cam {}/{} ...", cameraId, time_passed / 1000);
}
}
if (frameBuffer.size() > 0) {
std::string frameBufferStr(frameBuffer.begin(), frameBuffer.end());
LOG("Pistache: Sending frame for camera {}, buffer length: {}", cameraId, frameBufferStr.size());
stream << "--frame\r\nContent-Type: image/jpeg\r\n\r\n";
stream << frameBufferStr.c_str();
stream << "\r\n";
stream << Pistache::Http::flush;
}
if (time_passed >= camSearchTimeout) {
LOG("Pistache: Camera {} not found, exiting by timeout", cameraId);
break;
}
std::this_thread::sleep_for(std::chrono::milliseconds(frameRetrievalInterval));
}
}
catch (const std::exception& e) {
// Handle exceptions if needed
LOG("Pistache exception: {}", e.what());
}
}
here is curl:
curl --output - -v 127.0.0.1:8013/stream
* Trying 127.0.0.1:8013...
* Connected to 127.0.0.1 (127.0.0.1) port 8013 (#0)
> GET /stream/arlington_SCR2326W_media-inference-arlingtons-k8s_dev HTTP/1.1
> Host: 127.0.0.1:8013
> User-Agent: curl/7.81.0
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Content-Type: multipart/x-mixed-replace; boundary=frame
< Server: pistache/0.1
< Connection: Close
< Transfer-Encoding: chunked
<
--frame
Content-Type: image/jpeg
����
--frame
Content-Type: image/jpeg
����
--frame
Content-Type: image/jpeg
����
--frame
Content-Type: image/jpeg
Everything looks the same as a Flask output, in structure. Except with --output -
flag curl with Flask with real image will print out huge portions of unreadable binary data, which is to be expected. But with this flag with Pistache all binary data curl prints is "����", altough string Im sending is quite big, buffer length: 523798
on average.
And it still doesnt work. I feel like Ive broken both my legs here trying to make it work, in last three days... Any help appreaciated
from pistache.
Well, I couldnt make it work. As previous person who had similar issue, I decided to switch to Asio, which worked as expected. To me it feels like there is some issue with library with multipart, or would be nice to have a code example with multipart streaming. I liked modern Pistache interface, but it didnt worked out for me as expected.
from pistache.
Related Issues (20)
- Getting SIGILL with gcc12 HOT 11
- Memory corruption when using the server in multi-threaded mode. HOT 6
- serveFile crashes if passed-by-reference fileName parameter goes out of scope HOT 8
- WriteEntry Constructor called with Incorrect Parameters in Transport::asyncWriteImpl
- [BUG] ResponseWriter::setCompression() chokes on identity encoding
- [BUG] Fails to fetch build dependencies in autopkgtest HOT 7
- [BUG] All PPA builds failing due to missing zlib headers
- [BUG] New compression code fails to build on armhf HOT 5
- decode uri
- Complete BSD support HOT 4
- "Stack smashing detected" error in ubuntu docker container HOT 17
- New release to conan HOT 1
- keep alive timeout doesn't add to response header
- Pistache very poor performance when POST size exceeds `Endpoint::options().maxRequestSize()`, and `maxRequestSize` is large. HOT 4
- Out of bound read in std::strtol while parsing HTTP requests HOT 4
- Endpoint::Options::backlog_ is not used HOT 2
- What's up with the logging? HOT 3
- [FEATURE REQUEST] Add support for zstd compression
- ci: coverage fails on debian:testing runners HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pistache.