forked from avTranscoder/avTranscoder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStreamProperties.cpp
More file actions
199 lines (166 loc) · 5.88 KB
/
StreamProperties.cpp
File metadata and controls
199 lines (166 loc) · 5.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#include "StreamProperties.hpp"
#include <AvTranscoder/properties/util.hpp>
#include <AvTranscoder/properties/JsonWriter.hpp>
#include <AvTranscoder/properties/FileProperties.hpp>
#include <stdexcept>
namespace avtranscoder
{
StreamProperties::StreamProperties(const FileProperties& fileProperties, const size_t index)
: _fileProperties(&fileProperties)
, _formatContext(&fileProperties.getAVFormatContext())
, _codecContext(NULL)
, _codec(NULL)
, _streamIndex(index)
{
if(_formatContext)
detail::fillMetadataDictionnary(_formatContext->streams[index]->metadata, _metadatas);
if(_formatContext)
{
if(_streamIndex > _formatContext->nb_streams)
{
std::stringstream ss;
ss << "Stream at index " << _streamIndex << " does not exist.";
throw std::runtime_error(ss.str());
}
AVStream* stream = _formatContext->streams[_streamIndex];
_codec = avcodec_find_decoder(stream->codecpar->codec_id);
_codecContext = avcodec_alloc_context3(_codec);
avcodec_parameters_to_context(_codecContext, stream->codecpar);
_codecContext->time_base = stream->time_base;
_codecContext->coded_side_data = stream->side_data;
}
// find the decoder
if(_formatContext && _codecContext && _codec)
{
// load specific options of the codec
loadOptions(_options, _codecContext);
}
}
StreamProperties::~StreamProperties()
{
}
size_t StreamProperties::getStreamId() const
{
if(!_formatContext)
throw std::runtime_error("unknown format context");
return _formatContext->streams[_streamIndex]->id;
}
Rational StreamProperties::getTimeBase() const
{
if(!_formatContext)
throw std::runtime_error("unknown format context");
return _formatContext->streams[_streamIndex]->time_base;
}
float StreamProperties::getDuration() const
{
const Rational timeBase = getTimeBase();
const size_t duration = _formatContext->streams[_streamIndex]->duration;
if(duration == (size_t)AV_NOPTS_VALUE)
{
LOG_WARN("The duration of the stream '" << _streamIndex << "' of file '" << _formatContext->url
<< "' is unknown.")
return 0;
}
return av_q2d(timeBase) * duration;
}
AVMediaType StreamProperties::getStreamType() const
{
if(!_formatContext)
throw std::runtime_error("unknown format context");
return _formatContext->streams[_streamIndex]->codecpar->codec_type;
}
size_t StreamProperties::getCodecId() const
{
if(!_codecContext)
throw std::runtime_error("unknown codec context");
return _codecContext->codec_id;
}
std::string StreamProperties::getCodecName() const
{
if(!_codecContext || !_codec)
throw std::runtime_error("unknown codec");
#ifdef AV_CODEC_CAP_TRUNCATED
if(_codec->capabilities & AV_CODEC_CAP_TRUNCATED)
_codecContext->flags |= AV_CODEC_FLAG_TRUNCATED;
#else
if(_codec->capabilities & CODEC_CAP_TRUNCATED)
_codecContext->flags |= CODEC_FLAG_TRUNCATED;
#endif
if(!_codec->name)
throw std::runtime_error("unknown codec name");
return std::string(_codec->name);
}
std::string StreamProperties::getCodecLongName() const
{
if(!_codecContext || !_codec)
throw std::runtime_error("unknown codec");
#ifdef AV_CODEC_CAP_TRUNCATED
if(_codec->capabilities & AV_CODEC_CAP_TRUNCATED)
_codecContext->flags |= AV_CODEC_FLAG_TRUNCATED;
#else
if(_codec->capabilities & CODEC_CAP_TRUNCATED)
_codecContext->flags |= CODEC_FLAG_TRUNCATED;
#endif
if(!_codec->long_name)
throw std::runtime_error("unknown codec long name");
return std::string(_codec->long_name);
}
std::vector<Option> StreamProperties::getCodecOptions()
{
std::vector<Option> optionsArray;
for(OptionMap::iterator it = _options.begin(); it != _options.end(); ++it)
{
optionsArray.push_back(it->second);
}
return optionsArray;
}
PropertyVector StreamProperties::asVector() const
{
PropertyVector propertyVector;
return fillVector(propertyVector);
}
PropertyVector& StreamProperties::fillVector(PropertyVector& data) const
{
addProperty(data, "streamId", &StreamProperties::getStreamId);
addProperty(data, "streamIndex", &StreamProperties::getStreamIndex);
addProperty(data, "timeBase", &StreamProperties::getTimeBase);
addProperty(data, "duration", &StreamProperties::getDuration);
addProperty(data, "codecId", &StreamProperties::getCodecId);
addProperty(data, "codecName", &StreamProperties::getCodecName);
addProperty(data, "codecLongName", &StreamProperties::getCodecLongName);
for(size_t metadataIndex = 0; metadataIndex < _metadatas.size(); ++metadataIndex)
{
detail::add(data, _metadatas.at(metadataIndex).first, _metadatas.at(metadataIndex).second);
}
return data;
}
PropertyMap StreamProperties::asMap() const
{
PropertyMap dataMap;
PropertyVector dataVector(asVector());
for(PropertyVector::const_iterator it = dataVector.begin(); it != dataVector.end(); ++it)
{
dataMap.insert(std::make_pair(it->first, it->second));
}
return dataMap;
}
std::string StreamProperties::asJson() const
{
json::JsonObjectStreamWriter writer;
PropertyMap properties = asMap();
for(PropertyMap::iterator it = properties.begin(); it != properties.end(); ++it)
writer << std::make_pair(it->first.c_str(), it->second.c_str());
return writer.build();
}
std::ostream& operator<<(std::ostream& flux, const StreamProperties& streamProperties)
{
flux << std::left;
flux << detail::separator << " Stream " << detail::separator << std::endl;
PropertyVector properties = streamProperties.asVector();
for(PropertyVector::iterator it = properties.begin(); it != properties.end(); ++it)
{
flux << std::setw(detail::keyWidth) << it->first << ": " << it->second << std::endl;
}
return flux;
}
}