Add message queue for result callback

This commit is contained in:
Benau 2017-04-05 12:31:39 +08:00
parent 1a072d609f
commit bb529c011e
5 changed files with 58 additions and 16 deletions

View File

@ -33,7 +33,7 @@
namespace Recorder
{
// ------------------------------------------------------------------------
void writeMKV(const std::string& video, const std::string& audio)
std::string writeMKV(const std::string& video, const std::string& audio)
{
time_t rawtime;
time(&rawtime);
@ -51,13 +51,13 @@ namespace Recorder
if (!writer.Open(file_name.c_str()))
{
Log::error("writeMKV", "Error while opening output file.");
return;
return "";
}
mkvmuxer::Segment muxer_segment;
if (!muxer_segment.Init(&writer))
{
Log::error("writeMKV", "Could not initialize muxer segment.");
return;
return "";
}
std::list<mkvmuxer::Frame*> audio_frames;
uint8_t* buf = (uint8_t*)malloc(1024 * 1024);
@ -75,14 +75,14 @@ namespace Recorder
if (!aud_track)
{
Log::error("writeMKV", "Could not add audio track.");
return;
return "";
}
mkvmuxer::AudioTrack* const at = static_cast<mkvmuxer::AudioTrack*>
(muxer_segment.GetTrackByNumber(aud_track));
if (!at)
{
Log::error("writeMKV", "Could not get audio track.");
return;
return "";
}
uint32_t codec_private_size;
fread(&codec_private_size, 1, sizeof(uint32_t), input);
@ -90,7 +90,7 @@ namespace Recorder
if (!at->SetCodecPrivate(buf, codec_private_size))
{
Log::warn("writeMKV", "Could not add audio private data.");
return;
return "";
}
while (fread(buf, 1, 12, input) == 12)
{
@ -103,7 +103,7 @@ namespace Recorder
if (!audio_frame->Init(buf, frame_size))
{
Log::error("writeMKV", "Failed to construct a frame.");
return;
return "";
}
audio_frame->set_track_number(aud_track);
audio_frame->set_timestamp(timestamp);
@ -122,14 +122,14 @@ namespace Recorder
if (!vid_track)
{
Log::error("writeMKV", "Could not add video track.");
return;
return "";
}
mkvmuxer::VideoTrack* const vt = static_cast<mkvmuxer::VideoTrack*>(
muxer_segment.GetTrackByNumber(vid_track));
if (!vt)
{
Log::error("writeMKV", "Could not get video track.");
return;
return "";
}
vt->set_frame_rate(UserConfigParams::m_record_fps);
switch (vf)
@ -162,7 +162,7 @@ namespace Recorder
if (!muxer_frame.Init(buf, frame_size))
{
Log::error("writeMKV", "Failed to construct a frame.");
return;
return "";
}
muxer_frame.set_track_number(vid_track);
muxer_frame.set_timestamp(timestamp);
@ -183,7 +183,7 @@ namespace Recorder
if (!muxer_segment.AddGenericFrame(cur_aud_frame))
{
Log::error("writeMKV", "Could not add audio frame.");
return;
return "";
}
delete cur_aud_frame;
audio_frames.pop_front();
@ -198,7 +198,7 @@ namespace Recorder
if (!muxer_segment.AddGenericFrame(&muxer_frame))
{
Log::error("writeMKV", "Could not add video frame.");
return;
return "";
}
}
free(buf);
@ -214,9 +214,10 @@ namespace Recorder
if (!muxer_segment.Finalize())
{
Log::error("writeMKV", "Finalization of segment failed.");
return;
return "";
}
writer.Close();
return file_name;
} // writeMKV
};
#endif

View File

@ -24,7 +24,7 @@
namespace Recorder
{
void writeMKV(const std::string& video, const std::string& audio);
std::string writeMKV(const std::string& video, const std::string& audio);
};
#endif

View File

@ -78,6 +78,8 @@ namespace Recorder
// ========================================================================
std::string g_recording_name;
// ========================================================================
Synchronised<bool> g_display_progress(false);
// ========================================================================
void* fbiConversion(void* obj)
{
VS::setThreadName("fbiConversion");
@ -104,18 +106,35 @@ namespace Recorder
g_idle.setAtomic(true);
pthread_join(g_audio_thread, NULL);
g_jpg_list.lock();
if (!g_destroy && g_jpg_list.getData().size() > 100)
{
MessageQueue::add(MessageQueue::MT_GENERIC,
_("Please wait while encoding is finished."));
}
g_jpg_list.getData().emplace_back((uint8_t*)NULL, 0, 0);
pthread_cond_signal(&g_jpg_request);
g_jpg_list.unlock();
g_display_progress.setAtomic(true);
pthread_join(*g_video_thread.getData(), NULL);
delete g_video_thread.getData();
g_video_thread.setAtomic(NULL);
Recorder::writeMKV(g_recording_name + ".video",
std::string f = Recorder::writeMKV(g_recording_name + ".video",
g_recording_name + ".audio");
g_display_progress.setAtomic(false);
if (g_destroy)
{
return NULL;
}
if (f.empty())
{
MessageQueue::add(MessageQueue::MT_ERROR,
_("Error when saving video."));
}
else
{
MessageQueue::add(MessageQueue::MT_GENERIC,
_("Video saved in \"%s\".", f.c_str()));
}
continue;
}
else if (fbi == NULL)
@ -372,6 +391,11 @@ namespace Recorder
stopRecording();
g_common_data.destroy();
} // destroyRecorder
// ------------------------------------------------------------------------
bool displayProgress()
{
return g_display_progress.getAtomic();
} // displayProgress
}
#endif

View File

@ -51,6 +51,8 @@ namespace Recorder
bool isRecording();
// ------------------------------------------------------------------------
void destroyRecorder();
// ------------------------------------------------------------------------
bool displayProgress();
};
#endif

View File

@ -24,6 +24,7 @@
#include "utils/synchronised.hpp"
#include "utils/vs.hpp"
#include <chrono>
#include <turbojpeg.h>
#include <vpx/vpx_encoder.h>
#include <vpx/vp8cx.h>
@ -159,7 +160,7 @@ namespace Recorder
fclose(vpx_data);
return NULL;
}
std::chrono::high_resolution_clock::time_point tp;
while (true)
{
jpg_data->lock();
@ -169,6 +170,20 @@ namespace Recorder
pthread_cond_wait(cond_request, jpg_data->getMutex());
waiting = jpg_data->getData().empty();
}
if (displayProgress())
{
auto rate = std::chrono::high_resolution_clock::now() -
tp;
double t = std::chrono::duration_cast<std::chrono::
duration<double> >(rate).count();
if (t > 3.)
{
tp = std::chrono::high_resolution_clock::now();
Log::info("vpxEncoder", "%d frames remaining.",
jpg_data->getData().size());
}
}
auto& p = jpg_data->getData().front();
uint8_t* jpg = std::get<0>(p);
unsigned jpg_size = std::get<1>(p);