X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=bmusb.h;h=941c57c27d358ba22f28cb97268f71c475178f9c;hb=e614cf402e0a7f754a0b88a68b11c766ab9baebe;hp=8178ac0ba3e992fd1a6cb350143c9b9cb57ca12f;hpb=753cbaf5cec7411bdfe5171404c8586b8015faec;p=bmusb diff --git a/bmusb.h b/bmusb.h index 8178ac0..941c57c 100644 --- a/bmusb.h +++ b/bmusb.h @@ -1,18 +1,20 @@ #ifndef _BMUSB_H #define _BMUSB_H +#include #include #include #include #include #include +#include #include #include #include #include #include -struct libusb_transfer; +class BMUSBCapture; // An interface for frame allocators; if you do not specify one // (using set_video_frame_allocator), a default one that pre-allocates @@ -80,7 +82,6 @@ private: // Represents an input mode you can tune a card to. struct VideoMode { - uint32_t id = 0; std::string name; bool autodetect = false; // If true, all the remaining fields are irrelevant. unsigned width = 0, height = 0; @@ -96,6 +97,7 @@ struct VideoFormat { unsigned frame_rate_nom = 0, frame_rate_den = 0; bool interlaced = false; bool has_signal = false; + bool is_connected = true; // If false, then has_signal makes no sense. }; struct AudioFormat { @@ -109,14 +111,25 @@ typedef std::function frame_callback_t; +typedef std::function card_connected_callback_t; +typedef std::function card_disconnected_callback_t; + class CaptureInterface { public: virtual ~CaptureInterface() {} - virtual std::vector get_available_video_modes() const = 0; - + virtual std::map get_available_video_modes() const = 0; + virtual uint32_t get_current_video_mode() const = 0; virtual void set_video_mode(uint32_t video_mode_id) = 0; + virtual std::map get_available_video_inputs() const = 0; + virtual void set_video_input(uint32_t video_input_id) = 0; + virtual uint32_t get_current_video_input() const = 0; + + virtual std::map get_available_audio_inputs() const = 0; + virtual void set_audio_input(uint32_t audio_input_id) = 0; + virtual uint32_t get_current_audio_input() const = 0; + // Does not take ownership. virtual void set_video_frame_allocator(FrameAllocator *allocator) = 0; @@ -140,26 +153,41 @@ class CaptureInterface { virtual void start_bm_capture() = 0; virtual void stop_dequeue_thread() = 0; + + // If a card is disconnected, it cannot come back; you should call stop_dequeue_thread() + // and delete it. + virtual bool get_disconnected() const = 0; }; // The actual capturing class, representing capture from a single card. class BMUSBCapture : public CaptureInterface { public: - BMUSBCapture(int card_index) - : card_index(card_index) + BMUSBCapture(int card_index, libusb_device *dev = nullptr) + : card_index(card_index), dev(dev) { } ~BMUSBCapture() {} - std::vector get_available_video_modes() const override; - + std::map get_available_video_modes() const override; + uint32_t get_current_video_mode() const override; void set_video_mode(uint32_t video_mode_id) override; + virtual std::map get_available_video_inputs() const override; + virtual void set_video_input(uint32_t video_input_id) override; + virtual uint32_t get_current_video_input() const override { return current_video_input; } + + virtual std::map get_available_audio_inputs() const override; + virtual void set_audio_input(uint32_t audio_input_id) override; + virtual uint32_t get_current_audio_input() const override { return current_audio_input; } + // Does not take ownership. void set_video_frame_allocator(FrameAllocator *allocator) override { video_frame_allocator = allocator; + if (owned_video_frame_allocator.get() != allocator) { + owned_video_frame_allocator.reset(); + } } FrameAllocator *get_video_frame_allocator() override @@ -171,6 +199,9 @@ class BMUSBCapture : public CaptureInterface { void set_audio_frame_allocator(FrameAllocator *allocator) override { audio_frame_allocator = allocator; + if (owned_audio_frame_allocator.get() != allocator) { + owned_audio_frame_allocator.reset(); + } } FrameAllocator *get_audio_frame_allocator() override @@ -199,11 +230,34 @@ class BMUSBCapture : public CaptureInterface { void configure_card() override; void start_bm_capture() override; void stop_dequeue_thread() override; + bool get_disconnected() const override { return disconnected; } // TODO: It's rather messy to have these outside the interface. static void start_bm_thread(); static void stop_bm_thread(); + // Hotplug event (for devices being inserted between start_bm_thread() + // and stop_bm_thread()); entirely optional, but must be set before + // start_bm_capture(). Note that your callback should do as little work + // as possible, since the callback comes from the main USB handling + // thread, which is very time-sensitive. + // + // The callback function transfers ownership. If you don't want to hold + // on to the device given to you in the callback, you need to call + // libusb_unref_device(). + static void set_card_connected_callback(card_connected_callback_t callback) + { + card_connected_callback = callback; + } + + // Similar to set_card_connected_callback(), with the same caveats. + // (Note that this is set per-card and not global, as it is logically + // connected to an existing BMUSBCapture object.) + void set_card_disconnected_callback(card_disconnected_callback_t callback) + { + card_disconnected_callback = callback; + } + private: struct QueuedFrame { uint16_t timecode; @@ -219,6 +273,9 @@ class BMUSBCapture : public CaptureInterface { static void usb_thread_func(); static void cb_xfr(struct libusb_transfer *xfr); + static int cb_hotplug(libusb_context *ctx, libusb_device *dev, libusb_hotplug_event event, void *user_data); + + void update_capture_mode(); std::string description; @@ -232,7 +289,11 @@ class BMUSBCapture : public CaptureInterface { FrameAllocator *video_frame_allocator = nullptr; FrameAllocator *audio_frame_allocator = nullptr; + std::unique_ptr owned_video_frame_allocator; + std::unique_ptr owned_audio_frame_allocator; frame_callback_t frame_callback = nullptr; + static card_connected_callback_t card_connected_callback; + card_disconnected_callback_t card_disconnected_callback = nullptr; std::thread dequeue_thread; std::atomic dequeue_thread_should_quit; @@ -245,9 +306,20 @@ class BMUSBCapture : public CaptureInterface { static constexpr int NUM_BMUSB_REGISTERS = 60; uint8_t register_file[NUM_BMUSB_REGISTERS]; - int card_index; + // If is nullptr, will choose device number from the list + // of available devices on the system. is not used after configure_card() + // (it will be unref-ed). + int card_index = -1; + libusb_device *dev = nullptr; + std::vector iso_xfrs; int assumed_frame_width = 1280; + + libusb_device_handle *devh = nullptr; + uint32_t current_video_input = 0x00000000; // HDMI/SDI. + uint32_t current_audio_input = 0x00000000; // Embedded. + + bool disconnected = false; }; // Get details for the given video format; returns false if detection was incomplete.