Simplified a bit the AvInfo part

This commit is contained in:
emeric
2018-03-21 23:12:14 +01:00
parent 4683a9ef1e
commit 937729f8ff
8 changed files with 211 additions and 259 deletions
+32 -104
View File
@@ -25,18 +25,6 @@
namespace Av {
static std::string streamType_to_string(Stream::Type type)
{
switch (type)
{
case Stream::Type::Audio: return "audio";
case Stream::Type::Video: return "video";
case Stream::Type::Subtitle: return "subtitle";
}
return "unknown";
}
static std::string averror_to_string(int error)
{
std::array<char, 128> buf = {0};
@@ -47,6 +35,10 @@ static std::string averror_to_string(int error)
return "Unknown error";
}
MediaFileException::MediaFileException(int avError)
: LmsException("MediaFileException: " + averror_to_string(avError))
{
}
void AvInit()
{
@@ -62,52 +54,30 @@ void AvInit()
MediaFile::MediaFile(const boost::filesystem::path& p)
: _p(p), _context(nullptr)
{
int error = avformat_open_input(&_context, _p.string().c_str(), nullptr, nullptr);
if (error < 0)
{
LMS_LOG(AV, ERROR) << "Cannot open " << _p << ": " << averror_to_string(error);
throw MediaFileException(error);
}
error = avformat_find_stream_info(_context, nullptr);
if (error < 0)
{
LMS_LOG(AV, ERROR) << "Cannot find stream information on " << _p << ": " << averror_to_string(error);
avformat_close_input(&_context);
throw MediaFileException(error);
}
}
MediaFile::~MediaFile()
{
if (_context != nullptr)
avformat_close_input(&_context);
}
bool
MediaFile::open(void)
{
if (_context != nullptr)
throw std::logic_error("inputfile '" + _p.string() + "' already open");
int error = avformat_open_input(&_context, _p.string().c_str(), nullptr, nullptr);
if (error < 0)
{
LMS_LOG(AV, ERROR) << "Cannot open '" << _p.string() << "': " << averror_to_string(error);
return false;
}
return true;
}
bool
MediaFile::scan(void)
{
if (_context == nullptr)
throw std::logic_error("inputfile '" + _p.string() + "' not open");
int error = avformat_find_stream_info(_context, nullptr);
if (error < 0)
{
LMS_LOG(AV, ERROR) << "Cannot find stream information on '" << _p.string() << "': " << averror_to_string(error);
return false;
}
return true;
}
boost::posix_time::time_duration
MediaFile::getDuration() const
{
if (_context == nullptr)
throw std::logic_error("inputfile '" + _p.string() + "' not open");
if (static_cast<int>(_context->duration) != AV_NOPTS_VALUE )
return boost::posix_time::seconds(_context->duration / AV_TIME_BASE);
else
@@ -130,9 +100,6 @@ getMetaDataFromDictionnary(AVDictionary* dictionnary, std::map<std::string, std:
std::map<std::string, std::string>
MediaFile::getMetaData(void)
{
if (_context == nullptr)
throw std::logic_error("inputfile '" + _p.string() + "' not open");
std::map<std::string, std::string> res;
getMetaDataFromDictionnary(_context->metadata, res);
@@ -153,13 +120,10 @@ MediaFile::getMetaData(void)
return res;
}
std::vector<Stream>
MediaFile::getStreams(Stream::Type type) const
std::vector<StreamInfo>
MediaFile::getStreamInfo() const
{
if (_context == nullptr)
throw std::logic_error("inputfile '" + _p.string() + "' not open");
std::vector<Stream> res;
std::vector<StreamInfo> res;
for (std::size_t i = 0; i < _context->nb_streams; ++i)
{
@@ -169,66 +133,33 @@ MediaFile::getStreams(Stream::Type type) const
if (avstream->disposition & AV_DISPOSITION_ATTACHED_PIC)
continue;
if (avstream->codec == nullptr)
if (!avstream->codecpar)
{
LMS_LOG(AV, ERROR) << "Skipping stream " << i << " since no codec is set";
LMS_LOG(AV, ERROR) << "Skipping stream " << i << " since no codecpar is set";
continue;
}
if (type == Stream::Type::Audio && avstream->codec->codec_type != AVMEDIA_TYPE_AUDIO)
continue;
else if (type == Stream::Type::Video && avstream->codec->codec_type != AVMEDIA_TYPE_VIDEO)
continue;
else if (type == Stream::Type::Subtitle && avstream->codec->codec_type != AVMEDIA_TYPE_SUBTITLE)
if (avstream->codecpar->codec_type != AVMEDIA_TYPE_AUDIO)
continue;
Stream stream;
stream.id = i; // or use stream->id ?
stream.type = type;
stream.bitrate = avstream->codec->bit_rate;
{
std::array<char, 256> buf = {0};
avcodec_string(buf.data(), buf.size(), avstream->codec, 0);
stream.desc = buf.data();
}
res.push_back(stream);
res.push_back( {.id = i, .bitrate = static_cast<std::size_t>(avstream->codecpar->bit_rate)} );
}
return res;
}
boost::optional<std::size_t>
MediaFile::getBestStreamId(Stream::Type type) const
MediaFile::getBestStream() const
{
if (_context == nullptr)
throw std::logic_error("inputfile '" + _p.string() + "' not open");
enum AVMediaType avMediaType;
switch (type)
{
case Stream::Type::Audio: avMediaType = AVMEDIA_TYPE_AUDIO; break;
case Stream::Type::Video: avMediaType = AVMEDIA_TYPE_VIDEO; break;
case Stream::Type::Subtitle: avMediaType = AVMEDIA_TYPE_SUBTITLE; break;
default:
return boost::none;
}
int res = av_find_best_stream(_context,
avMediaType,
AVMEDIA_TYPE_AUDIO,
-1, // Auto
-1, // Auto
NULL,
0);
if (res < 0)
{
LMS_LOG(AV, ERROR) << "Cannot find best stream for type " << streamType_to_string(type);
return boost::none;
}
return res;
}
@@ -248,9 +179,6 @@ MediaFile::hasAttachedPictures(void) const
std::vector<Picture>
MediaFile::getAttachedPictures(std::size_t nbMaxPictures) const
{
if (_context == nullptr)
throw std::logic_error("inputfile '" + _p.string() + "' not open");
static const std::map<int, std::string> codecMimeMap =
{
{ AV_CODEC_ID_BMP, "image/x-bmp" },
@@ -271,15 +199,15 @@ MediaFile::getAttachedPictures(std::size_t nbMaxPictures) const
if (!(avstream->disposition & AV_DISPOSITION_ATTACHED_PIC))
continue;
if (avstream->codec == nullptr)
if (avstream->codecpar == nullptr)
{
LMS_LOG(AV, ERROR) << "Skipping stream " << i << " since no codec is set";
LMS_LOG(AV, ERROR) << "Skipping stream " << i << " since no codecpar is set";
continue;
}
Picture picture;
auto itMime = codecMimeMap.find(avstream->codec->codec_id);
auto itMime = codecMimeMap.find(avstream->codecpar->codec_id);
if (itMime != codecMimeMap.end())
{
picture.mimeType = itMime->second;
@@ -287,7 +215,7 @@ MediaFile::getAttachedPictures(std::size_t nbMaxPictures) const
else
{
picture.mimeType = "application/octet-stream";
LMS_LOG(AV, ERROR) << "CODEC ID " << avstream->codec->codec_id << " not handled in mime type conversion";
LMS_LOG(AV, ERROR) << "CODEC ID " << avstream->codecpar->codec_id << " not handled in mime type conversion";
}
AVPacket pkt = avstream->attached_pic;
+14 -23
View File
@@ -19,8 +19,7 @@
/* This file contains some classes in order to get info from file using the libavconv */
#ifndef AV_INFO_HPP
#define AV_INFO_HPP
#pragma once
extern "C"
{
@@ -39,6 +38,8 @@ extern "C"
#include <boost/filesystem/path.hpp>
#include <boost/date_time/posix_time/posix_time_types.hpp> //no i/o just types
#include "utils/Exception.hpp"
namespace Av
{
@@ -50,25 +51,21 @@ struct Picture
std::vector<uint8_t> data;
};
struct Stream
struct StreamInfo
{
enum class Type
{
Audio,
Video,
Subtitle,
};
int id;
Type type;
size_t id;
std::size_t bitrate;
std::string desc; // Description of the stream
};
class MediaFileException : public LmsException
{
public:
MediaFileException(int avError);
};
class MediaFile
{
public:
MediaFile(const boost::filesystem::path& p);
~MediaFile();
@@ -76,16 +73,13 @@ class MediaFile
MediaFile(const MediaFile&) = delete;
MediaFile& operator=(const MediaFile&) = delete;
boost::filesystem::path getPath() const {return _p;};
bool open(void);
bool scan(void);
const boost::filesystem::path& getPath() const {return _p;};
boost::posix_time::time_duration getDuration() const;
std::map<std::string, std::string> getMetaData(void);
std::vector<Stream> getStreams(Stream::Type type) const;
boost::optional<std::size_t> getBestStreamId(Stream::Type type) const; // none if failure/unknown
std::vector<StreamInfo> getStreamInfo() const;
boost::optional<std::size_t> getBestStream() const; // none if failure/unknown
bool hasAttachedPictures(void) const;
std::vector<Picture> getAttachedPictures(std::size_t nbMaxPictures) const;
@@ -96,8 +90,5 @@ class MediaFile
AVFormatContext* _context;
};
} // namespace Av
#endif
+8 -2
View File
@@ -130,11 +130,17 @@ Grabber::getCoverPaths(const boost::filesystem::path& directoryPath, std::size_t
std::vector<Image::Image>
Grabber::getFromTrack(const boost::filesystem::path& p, std::size_t nbMaxCovers) const
{
try
{
Av::MediaFile input(p);
if (input.open())
return getFromAvMediaFile(input, nbMaxCovers);
else
}
catch (Av::MediaFileException& e)
{
LMS_LOG(COVER, ERROR) << "Cannot get covers from track " << p << ": " << e.what();
}
return std::vector<Image::Image>();
}
+10 -15
View File
@@ -40,28 +40,18 @@ AvFormat::parse(const boost::filesystem::path& p)
{
Items items;
try
{
Av::MediaFile mediaFile(p);
if (!mediaFile.open())
return boost::none;
if (!mediaFile.scan())
return boost::none;
// Stream info
{
std::vector<AudioStream> audioStreams;
std::vector<Av::Stream> streams = mediaFile.getStreams(Av::Stream::Type::Audio);
auto streams = mediaFile.getStreamInfo();
for (Av::Stream& stream : streams)
{
AudioStream audioStream;
audioStream.desc = stream.desc;
audioStream.bitRate = stream.bitrate;
audioStreams.push_back(audioStream);
}
for (auto stream : streams)
audioStreams.push_back( {.bitRate = stream.bitrate } );
if (!audioStreams.empty())
items.insert( std::make_pair(MetaData::Type::AudioStreams, audioStreams));
@@ -179,6 +169,11 @@ AvFormat::parse(const boost::filesystem::path& p)
if (!clusters.empty())
items.insert( std::make_pair(MetaData::Type::Clusters, clusters) );
}
catch(Av::MediaFileException& e)
{
return items;
}
return items;
}
+1 -1
View File
@@ -56,7 +56,7 @@ namespace MetaData
// Used by Streams
struct AudioStream
{
std::string desc;
// TODO codec?
std::size_t bitRate;
};
+1 -1
View File
@@ -59,7 +59,7 @@ TagLibParser::parse(const boost::filesystem::path& p)
items.insert( std::make_pair(MetaData::Type::Duration, duration) );
MetaData::AudioStream audioStream = { .desc = "", .bitRate = static_cast<std::size_t>(properties->bitrate() * 1000) };
MetaData::AudioStream audioStream = { .bitRate = static_cast<std::size_t>(properties->bitrate() * 1000) };
items.insert( std::make_pair(MetaData::Type::AudioStreams, std::vector<MetaData::AudioStream>(1, audioStream ) ));
}
+9 -7
View File
@@ -89,21 +89,23 @@ MediaPlayer::playTrack(Database::Track::id_type trackId)
transaction.commit();
// Analyse track, select the best media stream
try
{
Av::MediaFile mediaFile(track->getPath());
if (!mediaFile.open() || !mediaFile.scan())
{
LMS_LOG(UI, ERROR) << "Cannot open file '" << track->getPath();
return;
}
auto streamId = mediaFile.getBestStreamId(Av::Stream::Type::Audio);
auto streamId = mediaFile.getBestStream();
_audio->pause();
_audio->clearSources();
_audio->addSource(LmsApp->getTranscodeResource()->getUrl(trackId, Av::Encoding::MP3, boost::posix_time::seconds(0), streamId));
_audio->setPreloadMode(Wt::WAudio::PreloadNone);
_audio->play();
}
catch (Av::MediaFileException& e)
{
LMS_LOG(UI, ERROR) << "MediaFileException: " << e.what();
stop();
}
}
void
+30
View File
@@ -0,0 +1,30 @@
/*
* Copyright (C) 2018 Emeric Poupon
*
* This file is part of LMS.
*
* LMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <stdexcept>
#include <string>
class LmsException : public std::runtime_error
{
public:
LmsException(const std::string& error) : std::runtime_error(error) {}
};