#include <movit/image_format.h>
#include <movit/init.h>
#include <movit/resource_pool.h>
+#include <pthread.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
alsa.reset(new ALSAOutput(OUTPUT_FREQUENCY, /*num_channels=*/2));
}
if (global_flags.output_card != -1) {
- set_output_card(global_flags.output_card);
+ desired_output_card_index = global_flags.output_card;
+ set_output_card_internal(global_flags.output_card);
}
}
cards[card_index].new_frames_changed.notify_all();
}
cards[card_index].capture->stop_dequeue_thread();
+ if (cards[card_index].output) {
+ cards[card_index].output->end_output();
+ cards[card_index].output.reset();
+ }
}
video_encoder.reset(nullptr);
CaptureCard *card = &cards[card_index];
if (card->capture != nullptr) {
card->capture->stop_dequeue_thread();
- delete card->capture;
}
- card->capture = capture;
+ card->capture.reset(capture);
card->is_fake_capture = is_fake_capture;
- card->output = output;
+ if (card->output.get() != output) {
+ card->output.reset(output);
+ }
card->capture->set_frame_callback(bind(&Mixer::bm_frame, this, card_index, _1, _2, _3, _4, _5, _6, _7));
if (card->frame_allocator == nullptr) {
card->frame_allocator.reset(new PBOFrameAllocator(8 << 20, global_flags.width, global_flags.height)); // 8 MB.
audio_mixer.trigger_state_changed_callback();
}
-void Mixer::set_output_card(int card_index)
+void Mixer::set_output_card_internal(int card_index)
{
- if (card_index == output_card_index) {
- return;
- }
+ // We don't really need to take card_mutex, since we're in the mixer
+ // thread and don't mess with any queues (which is the only thing that happens
+ // from other threads), but it's probably the safest in the long run.
unique_lock<mutex> lock(card_mutex);
if (output_card_index != -1) {
// Switch the old card from output to input.
CaptureCard *old_card = &cards[output_card_index];
old_card->output->end_output();
- old_card->capture->stop_dequeue_thread();
- delete old_card->capture;
-
- old_card->capture = old_card->parked_capture;
+ // Stop the fake card that we put into place.
+ // This needs to _not_ happen under the mutex, to avoid deadlock
+ // (delivering the last frame needs to take the mutex).
+ bmusb::CaptureInterface *fake_capture = old_card->capture.get();
+ lock.unlock();
+ fake_capture->stop_dequeue_thread();
+ lock.lock();
+ old_card->capture = move(old_card->parked_capture);
old_card->is_fake_capture = false;
- old_card->parked_capture = nullptr;
old_card->capture->start_bm_capture();
}
-
- CaptureCard *card = &cards[card_index];
- card->capture->stop_dequeue_thread();
- card->parked_capture = card->capture;
- FakeCapture *capture = new FakeCapture(global_flags.width, global_flags.height, FAKE_FPS, OUTPUT_FREQUENCY, card_index, global_flags.fake_cards_audio);
- configure_card(card_index, capture, /*is_fake_capture=*/true, card->output);
- card->queue_length_policy.reset(card_index);
- card->capture->start_bm_capture();
- card->output->start_output(bmdModeHD720p5994, pts_int); // FIXME
+ if (card_index != -1) {
+ CaptureCard *card = &cards[card_index];
+ bmusb::CaptureInterface *capture = card->capture.get();
+ // TODO: DeckLinkCapture::stop_dequeue_thread can actually take
+ // several seconds to complete (blocking on DisableVideoInput);
+ // see if we can maybe do it asynchronously.
+ lock.unlock();
+ capture->stop_dequeue_thread();
+ lock.lock();
+ card->parked_capture = move(card->capture);
+ bmusb::CaptureInterface *fake_capture = new FakeCapture(global_flags.width, global_flags.height, FAKE_FPS, OUTPUT_FREQUENCY, card_index, global_flags.fake_cards_audio);
+ configure_card(card_index, fake_capture, /*is_fake_capture=*/true, card->output.release());
+ card->queue_length_policy.reset(card_index);
+ card->capture->start_bm_capture();
+ card->output->start_output(bmdModeHD720p5994, pts_int); // FIXME
+ }
output_card_index = card_index;
}
void Mixer::thread_func()
{
+ pthread_setname_np(pthread_self(), "Mixer_OpenGL");
+
eglBindAPI(EGL_OPENGL_API);
QOpenGLContext *context = create_context(mixer_surface);
if (!make_current(context, mixer_surface)) {
int stats_dropped_frames = 0;
while (!should_quit) {
+ if (desired_output_card_index != output_card_index) {
+ set_output_card_internal(desired_output_card_index);
+ }
+
CaptureCard::NewFrame new_frames[MAX_VIDEO_CARDS];
bool has_new_frame[MAX_VIDEO_CARDS] = { false };
}
OutputFrameInfo output_frame_info = get_one_frame_from_each_card(master_card_index, master_card_is_output, new_frames, has_new_frame);
- schedule_audio_resampling_tasks(output_frame_info.dropped_frames, output_frame_info.num_samples, output_frame_info.frame_duration);
+ schedule_audio_resampling_tasks(output_frame_info.dropped_frames, output_frame_info.num_samples, output_frame_info.frame_duration, output_frame_info.is_preroll);
stats_dropped_frames += output_frame_info.dropped_frames;
handle_hotplugged_cards();
unique_lock<mutex> lock(card_mutex, defer_lock);
if (master_card_is_output) {
// Clocked to the output, so wait for it to be ready for the next frame.
- cards[master_card_index].output->wait_for_frame(pts_int, &output_frame_info.dropped_frames, &output_frame_info.frame_duration);
+ cards[master_card_index].output->wait_for_frame(pts_int, &output_frame_info.dropped_frames, &output_frame_info.frame_duration, &output_frame_info.is_preroll);
lock.lock();
} else {
// Wait for the master card to have a new frame.
// TODO: Add a timeout.
+ output_frame_info.is_preroll = false;
lock.lock();
cards[master_card_index].new_frames_changed.wait(lock, [this, master_card_index]{ return !cards[master_card_index].new_frames.empty() || cards[master_card_index].capture->get_disconnected(); });
}
}
-void Mixer::schedule_audio_resampling_tasks(unsigned dropped_frames, int num_samples_per_frame, int length_per_frame)
+void Mixer::schedule_audio_resampling_tasks(unsigned dropped_frames, int num_samples_per_frame, int length_per_frame, bool is_preroll)
{
// Resample the audio as needed, including from previously dropped frames.
assert(num_cards > 0);
// since dropped frames are expected to be rare, and it might be
// better to just wait until we have a slightly more normal situation).
unique_lock<mutex> lock(audio_mutex);
- bool adjust_rate = !dropped_frame;
+ bool adjust_rate = !dropped_frame && !is_preroll;
audio_task_queue.push(AudioTask{pts_int, num_samples_per_frame, adjust_rate});
audio_task_queue_changed.notify_one();
}
void Mixer::audio_thread_func()
{
+ pthread_setname_np(pthread_self(), "Mixer_Audio");
+
while (!should_quit) {
AudioTask task;