2014-03-22 15:29:21 +01:00
|
|
|
// Copyright (c) 2014, Thomas Goyne <plorkyeran@aegisub.org>
|
2006-02-25 09:26:29 +01:00
|
|
|
//
|
2014-03-22 15:29:21 +01:00
|
|
|
// Permission to use, copy, modify, and distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice appear in all copies.
|
2006-02-25 09:26:29 +01:00
|
|
|
//
|
2014-03-22 15:29:21 +01:00
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
2006-02-25 09:26:29 +01:00
|
|
|
//
|
2009-07-29 07:43:02 +02:00
|
|
|
// Aegisub Project http://www.aegisub.org/
|
|
|
|
|
2014-03-24 03:30:03 +01:00
|
|
|
#include "include/aegisub/audio_provider.h"
|
2012-12-23 00:18:38 +01:00
|
|
|
|
2012-01-08 02:33:39 +01:00
|
|
|
#include "audio_controller.h"
|
2013-01-07 02:50:09 +01:00
|
|
|
#include "options.h"
|
2006-02-25 09:39:15 +01:00
|
|
|
|
2014-03-21 02:51:08 +01:00
|
|
|
#include <libaegisub/file_mapping.h>
|
2013-01-04 16:01:50 +01:00
|
|
|
#include <libaegisub/fs.h>
|
|
|
|
#include <libaegisub/path.h>
|
2014-04-23 22:53:24 +02:00
|
|
|
#include <libaegisub/make_unique.h>
|
2013-01-04 16:01:50 +01:00
|
|
|
|
2014-05-23 00:40:16 +02:00
|
|
|
#include <boost/filesystem/path.hpp>
|
2014-03-22 15:29:21 +01:00
|
|
|
#include <boost/format.hpp>
|
|
|
|
#include <boost/interprocess/detail/os_thread_functions.hpp>
|
2014-04-22 21:34:20 +02:00
|
|
|
#include <thread>
|
2013-01-04 16:01:50 +01:00
|
|
|
|
2014-03-24 03:30:03 +01:00
|
|
|
namespace {
|
|
|
|
class HDAudioProvider final : public AudioProviderWrapper {
|
|
|
|
std::unique_ptr<agi::temp_file_mapping> file;
|
2014-04-22 21:34:20 +02:00
|
|
|
std::atomic<bool> cancelled = {false};
|
|
|
|
std::thread decoder;
|
2012-06-15 15:08:39 +02:00
|
|
|
|
2014-03-24 03:30:03 +01:00
|
|
|
void FillBuffer(void *buf, int64_t start, int64_t count) const override {
|
2014-04-22 21:34:20 +02:00
|
|
|
auto missing = std::min(count, start + count - decoded_samples);
|
|
|
|
if (missing > 0) {
|
|
|
|
memset(static_cast<int16_t*>(buf) + count - missing, 0, missing * bytes_per_sample);
|
|
|
|
count -= missing;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count > 0) {
|
|
|
|
start *= bytes_per_sample;
|
|
|
|
count *= bytes_per_sample;
|
|
|
|
memcpy(buf, file->read(start, count), count);
|
|
|
|
}
|
2014-03-24 03:30:03 +01:00
|
|
|
}
|
2006-02-25 09:39:15 +01:00
|
|
|
|
2014-03-24 03:30:03 +01:00
|
|
|
public:
|
2014-04-22 21:34:20 +02:00
|
|
|
HDAudioProvider(std::unique_ptr<AudioProvider> src)
|
2014-03-24 03:30:03 +01:00
|
|
|
: AudioProviderWrapper(std::move(src))
|
|
|
|
{
|
2014-04-22 21:34:20 +02:00
|
|
|
decoded_samples = 0;
|
|
|
|
|
2014-03-24 03:30:03 +01:00
|
|
|
auto path = OPT_GET("Audio/Cache/HD/Location")->GetString();
|
|
|
|
if (path == "default")
|
|
|
|
path = "?temp";
|
|
|
|
auto cache_dir = config::path->MakeAbsolute(config::path->Decode(path), "?temp");
|
2014-03-22 15:29:21 +01:00
|
|
|
|
2014-03-24 03:30:03 +01:00
|
|
|
// Check free space
|
2014-04-22 21:34:20 +02:00
|
|
|
if ((uint64_t)num_samples * bytes_per_sample > agi::fs::FreeSpace(cache_dir))
|
2014-03-24 03:30:03 +01:00
|
|
|
throw agi::AudioCacheOpenError("Not enough free disk space in " + cache_dir.string() + " to cache the audio", nullptr);
|
2014-03-22 15:29:21 +01:00
|
|
|
|
2014-03-24 03:30:03 +01:00
|
|
|
auto filename = str(boost::format("audio-%lld-%lld")
|
|
|
|
% (long long)time(nullptr)
|
|
|
|
% (long long)boost::interprocess::ipcdetail::get_current_process_id());
|
|
|
|
|
2014-04-23 22:53:24 +02:00
|
|
|
file = agi::make_unique<agi::temp_file_mapping>(cache_dir / filename, num_samples * bytes_per_sample);
|
2014-04-22 21:34:20 +02:00
|
|
|
decoder = std::thread([&] {
|
2014-03-24 03:30:03 +01:00
|
|
|
int64_t block = 65536;
|
|
|
|
for (int64_t i = 0; i < num_samples; i += block) {
|
2014-04-22 21:34:20 +02:00
|
|
|
if (cancelled) break;
|
2014-03-24 03:30:03 +01:00
|
|
|
block = std::min(block, num_samples - i);
|
2014-04-22 21:34:20 +02:00
|
|
|
source->GetAudio(file->write(i * bytes_per_sample, block * bytes_per_sample), i, block);
|
|
|
|
decoded_samples += block;
|
2014-03-24 03:30:03 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2014-04-22 21:34:20 +02:00
|
|
|
|
|
|
|
~HDAudioProvider() {
|
|
|
|
cancelled = true;
|
|
|
|
decoder.join();
|
|
|
|
}
|
2014-03-24 03:30:03 +01:00
|
|
|
};
|
|
|
|
}
|
2006-12-26 03:05:35 +01:00
|
|
|
|
2014-04-22 21:34:20 +02:00
|
|
|
std::unique_ptr<AudioProvider> CreateHDAudioProvider(std::unique_ptr<AudioProvider> src) {
|
2014-04-23 22:53:24 +02:00
|
|
|
return agi::make_unique<HDAudioProvider>(std::move(src));
|
2006-02-25 09:39:15 +01:00
|
|
|
}
|