]> git.sesse.net Git - nageru/commitdiff
Actually send the MJPEG frames on to the HTTP stream.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Sat, 18 Aug 2018 22:03:11 +0000 (00:03 +0200)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Sat, 15 Sep 2018 17:39:49 +0000 (19:39 +0200)
jpeg_frame_view.cpp
jpeg_frame_view.h
mux.cpp
mux.h
player.cpp
player.h

index 96243529b2b8184d0717ac382f606a7d92c76f46..16224ad3db7fbd19df2ab465e30aacae335f14e9 100644 (file)
@@ -20,8 +20,6 @@
 using namespace movit;
 using namespace std;
 
-string filename_for_frame(unsigned stream_idx, int64_t pts);
-
 struct JPEGID {
        unsigned stream_idx;
        int64_t pts;
index 6f37bfccd4f88d72d4cafaaa95156446a13d97ff..277241b69d70c77c5375a10dfec8b1fdfcb84f8b 100644 (file)
@@ -11,6 +11,8 @@
 
 #include <memory>
 
+std::string filename_for_frame(unsigned stream_idx, int64_t pts);
+
 struct Frame {
        std::unique_ptr<uint8_t[]> y, cb, cr;
        unsigned width, height;
diff --git a/mux.cpp b/mux.cpp
index 37bf321d5020208d8f4c2d501ab4926a5431eedb..2f682c9b3c63967487d8c52986b44b65cba9933e 100644 (file)
--- a/mux.cpp
+++ b/mux.cpp
@@ -58,10 +58,12 @@ Mux::Mux(AVFormatContext *avctx, int width, int height, Codec video_codec, const
        avstream_video->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
        if (video_codec == CODEC_H264) {
                avstream_video->codecpar->codec_id = AV_CODEC_ID_H264;
-       } else {
-               assert(video_codec == CODEC_NV12);
+       } else if (video_codec == CODEC_NV12) {
                avstream_video->codecpar->codec_id = AV_CODEC_ID_RAWVIDEO;
                avstream_video->codecpar->codec_tag = avcodec_pix_fmt_to_codec_tag(AV_PIX_FMT_NV12);
+       } else {
+               assert(video_codec == CODEC_MJPEG);
+               avstream_video->codecpar->codec_id = AV_CODEC_ID_MJPEG;
        }
        avstream_video->codecpar->width = width;
        avstream_video->codecpar->height = height;
@@ -86,6 +88,8 @@ Mux::Mux(AVFormatContext *avctx, int width, int height, Codec video_codec, const
                memcpy(avstream_video->codecpar->extradata, video_extradata.data(), video_extradata.size());
        }
 
+       avstream_audio = nullptr;
+#if 0
        avstream_audio = avformat_new_stream(avctx, nullptr);
        if (avstream_audio == nullptr) {
                fprintf(stderr, "avformat_new_stream() failed\n");
@@ -96,6 +100,7 @@ Mux::Mux(AVFormatContext *avctx, int width, int height, Codec video_codec, const
                fprintf(stderr, "avcodec_parameters_copy() failed\n");
                exit(1);
        }
+#endif
 
        AVDictionary *options = NULL;
        vector<pair<string, string>> opts = MUX_OPTS;
diff --git a/mux.h b/mux.h
index 9614bffbb5ae6603d1ef0d50353ef61b27715679..53e553915c91e15a9f6b991e6eb6c8a219964167 100644 (file)
--- a/mux.h
+++ b/mux.h
@@ -42,6 +42,7 @@ public:
        enum Codec {
                CODEC_H264,
                CODEC_NV12,  // Uncompressed 4:2:0.
+               CODEC_MJPEG
        };
        enum WriteStrategy {
                // add_packet() will write the packet immediately, unless plugged.
index 9f524bb884cdc0b36a46bb3069f7092224a4e6be..e3998914cb8173fd0070c0bd6ed9b19f73359133 100644 (file)
@@ -5,9 +5,19 @@
 #include <thread>
 #include <vector>
 
+#include <stdio.h>
+
+extern "C" {
+#include <libavformat/avformat.h>
+#include <libavformat/avio.h>
+}
+
 #include "clip_list.h"
 #include "defs.h"
+#include "ffmpeg_raii.h"
+#include "httpd.h"
 #include "jpeg_frame_view.h"
+#include "mux.h"
 #include "player.h"
 
 using namespace std;
@@ -15,6 +25,30 @@ using namespace std::chrono;
 
 extern mutex frame_mu;
 extern vector<int64_t> frames[MAX_STREAMS];
+extern HTTPD *global_httpd;
+
+namespace {
+
+string read_file(const string &filename)
+{
+       FILE *fp = fopen(filename.c_str(), "rb");
+       if (fp == nullptr) {
+               perror(filename.c_str());
+               return "";
+       }
+
+       fseek(fp, 0, SEEK_END);
+       long len = ftell(fp);
+       rewind(fp);
+
+       string ret;
+       ret.resize(len);
+       fread(&ret[0], len, 1, fp);
+       fclose(fp);
+       return ret;
+}
+
+}  // namespace
 
 void Player::thread_func()
 {
@@ -76,6 +110,17 @@ void Player::thread_func()
 
                        destination->setFrame(stream_idx, next_pts);
 
+                       // Send the frame to the stream.
+                       // FIXME: Vaguely less crazy pts, perhaps.
+                       double pts_float = fmod(duration<double>(next_frame_start.time_since_epoch()).count(), 86400.0f);
+                       int64_t pts = lrint(pts_float * TIMEBASE);
+                       string jpeg = read_file(filename_for_frame(stream_idx, next_pts));
+                       AVPacket pkt;
+                       av_init_packet(&pkt);
+                       pkt.stream_index = 0;
+                       pkt.data = (uint8_t *)jpeg.data();
+                       pkt.size = jpeg.size();
+                       stream_mux->add_packet(pkt, pts, pts);
                }
 
                {
@@ -91,6 +136,7 @@ void Player::thread_func()
 Player::Player(JPEGFrameView *destination)
        : destination(destination)
 {
+       open_output_stream();
        thread(&Player::thread_func, this).detach();
 }
 
@@ -151,3 +197,49 @@ void Player::override_angle(unsigned stream_idx)
        }
        destination->setFrame(stream_idx, *it);
 }
+
+void Player::open_output_stream()
+{
+       AVFormatContext *avctx = avformat_alloc_context();
+       avctx->oformat = av_guess_format("nut", nullptr, nullptr);
+
+       uint8_t *buf = (uint8_t *)av_malloc(MUX_BUFFER_SIZE);
+       avctx->pb = avio_alloc_context(buf, MUX_BUFFER_SIZE, 1, this, nullptr, nullptr, nullptr);
+       avctx->pb->write_data_type = &Player::write_packet2_thunk;
+       avctx->pb->ignore_boundary_point = 1;
+
+       Mux::Codec video_codec = Mux::CODEC_MJPEG;
+
+       avctx->flags = AVFMT_FLAG_CUSTOM_IO;
+
+       string video_extradata;
+
+       constexpr int width = 1280, height = 720;  // Doesn't matter for MJPEG.
+       stream_mux.reset(new Mux(avctx, width, height, video_codec, video_extradata, /*audio_codec_parameters=*/nullptr, COARSE_TIMEBASE,
+               /*write_callback=*/nullptr, Mux::WRITE_FOREGROUND, {}));
+}
+
+int Player::write_packet2_thunk(void *opaque, uint8_t *buf, int buf_size, AVIODataMarkerType type, int64_t time)
+{
+        Player *player = (Player *)opaque;
+        return player->write_packet2(buf, buf_size, type, time);
+}
+
+int Player::write_packet2(uint8_t *buf, int buf_size, AVIODataMarkerType type, int64_t time)
+{
+        if (type == AVIO_DATA_MARKER_SYNC_POINT || type == AVIO_DATA_MARKER_BOUNDARY_POINT) {
+                seen_sync_markers = true;
+        } else if (type == AVIO_DATA_MARKER_UNKNOWN && !seen_sync_markers) {
+                // We don't know if this is a keyframe or not (the muxer could
+                // avoid marking it), so we just have to make the best of it.
+                type = AVIO_DATA_MARKER_SYNC_POINT;
+        }
+
+        if (type == AVIO_DATA_MARKER_HEADER) {
+                stream_mux_header.append((char *)buf, buf_size);
+                global_httpd->set_header(stream_mux_header);
+        } else {
+                global_httpd->add_data((char *)buf, buf_size, type == AVIO_DATA_MARKER_SYNC_POINT, time, AVRational{ AV_TIME_BASE, 1 });
+        }
+        return buf_size;
+}
index e251c6e86d8f20446e8692361bad34c2123d15bd..ab8f0787573799723ec62c6f083d8eb5334db35e 100644 (file)
--- a/player.h
+++ b/player.h
@@ -7,7 +7,12 @@
 #include <functional>
 #include <mutex>
 
+extern "C" {
+#include <libavformat/avio.h>
+}
+
 class JPEGFrameView;
+class Mux;
 
 class Player {
 public:
@@ -23,6 +28,9 @@ public:
 
 private:
        void thread_func();
+       void open_output_stream();
+       static int write_packet2_thunk(void *opaque, uint8_t *buf, int buf_size, AVIODataMarkerType type, int64_t time);
+       int write_packet2(uint8_t *buf, int buf_size, AVIODataMarkerType type, int64_t time);
 
        JPEGFrameView *destination;
        done_callback_func done_callback;
@@ -36,6 +44,11 @@ private:
        bool new_clip_ready = false;  // Under queue_state_mu.
        bool playing = false;  // Under queue_state_mu.
        int override_stream_idx = -1;  // Under queue_state_mu.
+
+       // For streaming.
+       std::unique_ptr<Mux> stream_mux;  // To HTTP.
+       std::string stream_mux_header;
+       bool seen_sync_markers = false;
 };
 
 #endif  // !defined(_PLAYER_H)