]> git.sesse.net Git - nageru/commitdiff
Add an HTTP endpoint for enumerating channels and one for getting only their colors...
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Thu, 15 Feb 2018 18:23:46 +0000 (19:23 +0100)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Thu, 15 Feb 2018 18:23:46 +0000 (19:23 +0100)
Makefile
httpd.cpp
httpd.h
json.proto [new file with mode: 0644]
mixer.cpp
mixer.h

index ff9b4b5f18014bb25ab9712fef4497f2f002727a..7a973294dfe7455920ef3d9cffef98929c48767a 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -25,12 +25,12 @@ AUDIO_MIXER_OBJS = audio_mixer.o alsa_input.o alsa_pool.o ebu_r128_proc.o stereo
 OBJS += chroma_subsampler.o v210_converter.o mixer.o basic_stats.o metrics.o pbo_frame_allocator.o context.o ref_counted_frame.o theme.o httpd.o flags.o image_input.o alsa_output.o disk_space_estimator.o print_latency.o timecode_renderer.o tweaked_inputs.o $(AUDIO_MIXER_OBJS)
 
 # Streaming and encoding objects
-OBJS += quicksync_encoder.o x264_encoder.o x264_dynamic.o x264_speed_control.o video_encoder.o metacube2.o mux.o audio_encoder.o ffmpeg_raii.o ffmpeg_util.o
+OBJS += quicksync_encoder.o x264_encoder.o x264_dynamic.o x264_speed_control.o video_encoder.o metacube2.o mux.o audio_encoder.o ffmpeg_raii.o ffmpeg_util.o json.pb.o
 
 # DeckLink
 OBJS += decklink_capture.o decklink_util.o decklink_output.o decklink/DeckLinkAPIDispatch.o
 
-KAERU_OBJS = kaeru.o x264_encoder.o mux.o basic_stats.o metrics.o flags.o audio_encoder.o x264_speed_control.o print_latency.o x264_dynamic.o ffmpeg_raii.o ref_counted_frame.o ffmpeg_capture.o ffmpeg_util.o httpd.o metacube2.o
+KAERU_OBJS = kaeru.o x264_encoder.o mux.o basic_stats.o metrics.o flags.o audio_encoder.o x264_speed_control.o print_latency.o x264_dynamic.o ffmpeg_raii.o ref_counted_frame.o ffmpeg_capture.o ffmpeg_util.o httpd.o json.pb.o metacube2.o
 
 # bmusb
 ifeq ($(EMBEDDED_BMUSB),yes)
@@ -77,6 +77,7 @@ mainwindow.o: ui_mainwindow.h ui_display.h ui_audio_miniview.h ui_audio_expanded
 mainwindow.o: midi_mapping.pb.h
 midi_mapper.o: midi_mapping.pb.h
 midi_mapping_dialog.o: ui_midi_mapping.h midi_mapping.pb.h
+httpd.o: json.pb.h
 
 DEPS=$(OBJS:.o=.d) $(BM_OBJS:.o=.d) $(KAERU_OBJS:.o=.d)
 -include $(DEPS)
index 13d2a7138e3c362ab796e025f7e1fa867f7dde6c..811e095dc9541a442742e02c7d7454b0284cc207 100644 (file)
--- a/httpd.cpp
+++ b/httpd.cpp
@@ -88,6 +88,15 @@ int HTTPD::answer_to_connection(MHD_Connection *connection,
                MHD_destroy_response(response);  // Only decreases the refcount; actual free is after the request is done.
                return ret;
        }
+       if (endpoints.count(url)) {
+               pair<string, string> contents_and_type = endpoints[url]();
+               MHD_Response *response = MHD_create_response_from_buffer(
+                       contents_and_type.first.size(), &contents_and_type.first[0], MHD_RESPMEM_MUST_COPY);
+               MHD_add_response_header(response, "Content-type", contents_and_type.second.c_str());
+               int ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
+               MHD_destroy_response(response);  // Only decreases the refcount; actual free is after the request is done.
+               return ret;
+       }
 
        HTTPD::Stream *stream = new HTTPD::Stream(this, framing);
        stream->add_data(header.data(), header.size(), Stream::DATA_TYPE_HEADER);
diff --git a/httpd.h b/httpd.h
index 630b0c38bfefb53205050e4eb9563e06455ba69d..878a184d12858341287a17f20d22e03a1e19bc83 100644 (file)
--- a/httpd.h
+++ b/httpd.h
@@ -9,15 +9,21 @@
 #include <atomic>
 #include <condition_variable>
 #include <deque>
+#include <functional>
 #include <mutex>
 #include <set>
 #include <string>
+#include <unordered_map>
+#include <utility>
 
 struct MHD_Connection;
 struct MHD_Daemon;
 
 class HTTPD {
 public:
+       // Returns a pair of content and content-type.
+       using EndpointCallback = std::function<std::pair<std::string, std::string>()>;
+
        HTTPD();
        ~HTTPD();
 
@@ -26,6 +32,11 @@ public:
                header = data;
        }
 
+       // Should be called before start() (due to threading issues).
+       void add_endpoint(const std::string &url, const EndpointCallback &callback) {
+               endpoints[url] = callback;
+       }
+
        void start(int port);
        void add_data(const char *buf, size_t size, bool keyframe);
        int64_t get_num_connected_clients() const {
@@ -81,6 +92,7 @@ private:
        MHD_Daemon *mhd = nullptr;
        std::mutex streams_mutex;
        std::set<Stream *> streams;  // Not owned.
+       std::unordered_map<std::string, EndpointCallback> endpoints;
        std::string header;
 
        // Metrics.
diff --git a/json.proto b/json.proto
new file mode 100644 (file)
index 0000000..ae10a7f
--- /dev/null
@@ -0,0 +1,12 @@
+// Messages used to produce JSON (it's the simplest way we can create valid
+// JSON without pulling in an external JSON library).
+
+message Channels {
+       repeated Channel channel = 1;
+}
+
+message Channel {
+       required int32 index = 1;
+       required string name = 2;
+       required string color = 3;
+}
index 4deba02ac52e97554257bbb109684451a3de6ea9..70175a163c4a2c9a51be1690a9c9c408ac16f81f 100644 (file)
--- a/mixer.cpp
+++ b/mixer.cpp
 #include "v210_converter.h"
 #include "video_encoder.h"
 
+#undef Status
+#include <google/protobuf/util/json_util.h>
+#include "json.pb.h"
+
 class IDeckLink;
 class QOpenGLContext;
 
@@ -352,6 +356,13 @@ Mixer::Mixer(const QSurfaceFormat &format, unsigned num_cards)
        // Must be instantiated after VideoEncoder has initialized global_flags.use_zerocopy.
        theme.reset(new Theme(global_flags.theme_filename, global_flags.theme_dirs, resource_pool.get(), num_cards));
 
+       httpd.add_endpoint("/channels", bind(&Mixer::get_channels_json, this));
+       for (int channel_idx = 2; channel_idx < theme->get_num_channels(); ++channel_idx) {
+               char url[256];
+               snprintf(url, sizeof(url), "/channels/%d/color", channel_idx);
+               httpd.add_endpoint(url, bind(&Mixer::get_channel_color_http, this, unsigned(channel_idx)));
+       }
+
        // Start listening for clients only once VideoEncoder has written its header, if any.
        httpd.start(global_flags.http_port);
 
@@ -1096,6 +1107,24 @@ void Mixer::trim_queue(CaptureCard *card, size_t safe_queue_length)
 #endif
 }
 
+pair<string, string> Mixer::get_channels_json()
+{
+       Channels ret;
+       for (int channel_idx = 2; channel_idx < theme->get_num_channels(); ++channel_idx) {
+               Channel *channel = ret.add_channel();
+               channel->set_index(channel_idx);
+               channel->set_name(theme->get_channel_name(channel_idx));
+               channel->set_color(theme->get_channel_color(channel_idx));
+       }
+       string contents;
+       google::protobuf::util::MessageToJsonString(ret, &contents);  // Ignore any errors.
+       return make_pair(contents, "text/json");
+}
+
+pair<string, string> Mixer::get_channel_color_http(unsigned channel_idx)
+{
+       return make_pair(theme->get_channel_color(channel_idx), "text/plain");
+}
 
 Mixer::OutputFrameInfo Mixer::get_one_frame_from_each_card(unsigned master_card_index, bool master_card_is_output, CaptureCard::NewFrame new_frames[MAX_VIDEO_CARDS], bool has_new_frame[MAX_VIDEO_CARDS])
 {
diff --git a/mixer.h b/mixer.h
index 1d2e200ca45be9acb886f487b30ff5d2f478805a..aa3baac2fff99e90b1113f61af65e7f82761f42e 100644 (file)
--- a/mixer.h
+++ b/mixer.h
@@ -428,6 +428,8 @@ private:
        void release_display_frame(DisplayFrame *frame);
        double pts() { return double(pts_int) / TIMEBASE; }
        void trim_queue(CaptureCard *card, size_t safe_queue_length);
+       std::pair<std::string, std::string> get_channels_json();
+       std::pair<std::string, std::string> get_channel_color_http(unsigned channel_idx);
 
        HTTPD httpd;
        unsigned num_cards, num_video_inputs;