Skip to content

Commit 9c432e8

Browse files
author
Clement Champetier
committed
mediaProperty: check if access of properties is possible
Throw exception if it's not possible and we can't return any value.
1 parent 01e578e commit 9c432e8

File tree

4 files changed

+107
-13
lines changed

4 files changed

+107
-13
lines changed

src/AvTranscoder/mediaProperty/AudioProperties.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#include "AudioProperties.hpp"
22

3+
#include <stdexcept>
4+
35
extern "C" {
46
#include <libavcodec/avcodec.h>
57
#include <libavformat/avformat.h>
@@ -124,6 +126,34 @@ std::string AudioProperties::getChannelDescription() const
124126
#endif
125127
}
126128

129+
size_t AudioProperties::getCodecId() const
130+
{
131+
if( ! _codecContext )
132+
throw std::runtime_error( "unknown codec context" );
133+
return _codecContext->codec_id;
134+
}
135+
136+
size_t AudioProperties::getSampleRate() const
137+
{
138+
if( ! _codecContext )
139+
throw std::runtime_error( "unknown codec context" );
140+
return _codecContext->sample_rate;
141+
}
142+
143+
size_t AudioProperties::getChannels() const
144+
{
145+
if( ! _codecContext )
146+
throw std::runtime_error( "unknown codec context" );
147+
return _codecContext->channels;
148+
}
149+
150+
size_t AudioProperties::getBitRate() const
151+
{
152+
if( ! _codecContext )
153+
throw std::runtime_error( "unknown codec context" );
154+
return _codecContext->bit_rate;
155+
}
156+
127157
MetadatasMap AudioProperties::getDataMap() const
128158
{
129159
MetadatasMap dataMap;

src/AvTranscoder/mediaProperty/AudioProperties.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ class AvExport AudioProperties
2929
std::string getChannelName() const;
3030
std::string getChannelDescription() const;
3131
size_t getStreamId() const { return _streamId; }
32-
size_t getCodecId() const { return _codecContext->codec_id; }
33-
size_t getSampleRate() const { return _codecContext->sample_rate; }
34-
size_t getChannels() const { return _codecContext->channels; }
35-
size_t getBitRate() const { return _codecContext->bit_rate; }
32+
size_t getCodecId() const;
33+
size_t getSampleRate() const;
34+
size_t getChannels() const;
35+
size_t getBitRate() const;
3636
MetadatasMap& getMetadatas() { return _metadatas; }
3737

3838
#ifndef SWIG

src/AvTranscoder/mediaProperty/FileProperties.cpp

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#include "FileProperties.hpp"
22

3+
#include <stdexcept>
4+
35
namespace avtranscoder
46
{
57

@@ -16,6 +18,68 @@ FileProperties::FileProperties( const AVFormatContext* formatContext )
1618
detail::fillMetadataDictionnary( _formatContext->metadata, _metadatas );
1719
}
1820

21+
std::string FileProperties::getFilename() const
22+
{
23+
if( ! _formatContext )
24+
throw std::runtime_error( "unknown format context" );
25+
return _formatContext->filename;
26+
}
27+
28+
std::string FileProperties::getFormatName() const
29+
{
30+
if( ! _formatContext || ! _formatContext->iformat )
31+
throw std::runtime_error( "unknown format context" );
32+
return _formatContext->iformat->name;
33+
}
34+
35+
std::string FileProperties::getFormatLongName() const
36+
{
37+
if( ! _formatContext || ! _formatContext->iformat )
38+
throw std::runtime_error( "unknown format context" );
39+
return _formatContext->iformat->long_name;
40+
}
41+
42+
size_t FileProperties::getProgramsCount() const
43+
{
44+
if( ! _formatContext )
45+
throw std::runtime_error( "unknown format context" );
46+
return _formatContext->nb_programs;
47+
}
48+
49+
double FileProperties::getStartTime() const
50+
{
51+
if( ! _formatContext )
52+
throw std::runtime_error( "unknown format context" );
53+
return 1.0 * (unsigned int)_formatContext->start_time / AV_TIME_BASE;
54+
}
55+
56+
double FileProperties::getDuration() const
57+
{
58+
if( ! _formatContext )
59+
throw std::runtime_error( "unknown format context" );
60+
return 1.0 * _formatContext->duration / AV_TIME_BASE;
61+
}
62+
63+
size_t FileProperties::getBitRate() const
64+
{
65+
if( ! _formatContext )
66+
throw std::runtime_error( "unknown format context" );
67+
return _formatContext->bit_rate;
68+
}
69+
70+
size_t FileProperties::getPacketSize() const
71+
{
72+
if( ! _formatContext )
73+
throw std::runtime_error( "unknown format context" );
74+
return _formatContext->packet_size;
75+
}
76+
77+
size_t FileProperties::getNbStreams() const
78+
{
79+
if( ! _formatContext )
80+
throw std::runtime_error( "unknown format context" );
81+
return _formatContext->nb_streams;
82+
}
1983

2084
MetadatasMap FileProperties::getDataMap() const
2185
{

src/AvTranscoder/mediaProperty/FileProperties.hpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,19 @@ class AvExport FileProperties
2525
public:
2626
FileProperties( const AVFormatContext* formatContext );
2727

28-
std::string getFilename() const { return _formatContext->filename; }
29-
std::string getFormatName() const { return _formatContext->iformat->name; }
30-
std::string getFormatLongName() const { return _formatContext->iformat->long_name; }
28+
std::string getFilename() const;
29+
std::string getFormatName() const;
30+
std::string getFormatLongName() const;
3131

32-
size_t getProgramsCount() const { return _formatContext->nb_programs; }
33-
double getStartTime() const { return 1.0 * (unsigned int)_formatContext->start_time / AV_TIME_BASE; }
34-
double getDuration() const { return 1.0 * _formatContext->duration / AV_TIME_BASE; }
35-
size_t getBitRate() const { return _formatContext->bit_rate; }
36-
size_t getPacketSize() const { return _formatContext->packet_size; }
32+
size_t getProgramsCount() const;
33+
double getStartTime() const;
34+
double getDuration() const;
35+
size_t getBitRate() const;
36+
size_t getPacketSize() const;
3737

3838
MetadatasMap& getMetadatas() { return _metadatas; }
3939

40-
size_t getNbStreams() const { return _formatContext->nb_streams; }
40+
size_t getNbStreams() const;
4141
size_t getNbVideoStreams() const { return _videoStreams.size(); }
4242
size_t getNbAudioStreams() const { return _audioStreams.size(); }
4343
size_t getNbDataStreams() const { return _dataStreams.size(); }

0 commit comments

Comments
 (0)