Skip to content

Commit c20fc7c

Browse files
Merge pull request #28 from cchampet/dev_Dummy
Dummy stream: new way to add
2 parents 265f968 + 717aa26 commit c20fc7c

19 files changed

+407
-489
lines changed
Lines changed: 19 additions & 187 deletions
Original file line numberDiff line numberDiff line change
@@ -1,200 +1,38 @@
11
#include "AudioDesc.hpp"
2-
#include "AudioFrame.hpp"
32

4-
extern "C" {
5-
#ifndef __STDC_CONSTANT_MACROS
6-
#define __STDC_CONSTANT_MACROS
7-
#endif
8-
#include <libavcodec/avcodec.h>
9-
#include <libavformat/avformat.h>
10-
#include <libavutil/avutil.h>
11-
#include <libavutil/pixdesc.h>
12-
#include <libavutil/imgutils.h>
13-
#include <libavutil/mathematics.h>
14-
#include <libavutil/opt.h>
15-
#include <libavutil/error.h>
16-
}
17-
#include <iostream>
18-
#include <stdexcept>
19-
#include <sstream>
203
#include <cassert>
214

225
namespace avtranscoder
236
{
247

258
AudioDesc::AudioDesc( const std::string& codecName )
26-
: m_codec( NULL )
27-
, m_codecContext( NULL )
9+
: EssenceDesc( codecName )
2810
{
29-
if( codecName.size() )
30-
setAudioCodec( codecName );
3111
}
3212

3313
AudioDesc::AudioDesc( const AVCodecID codecId )
34-
: m_codec( NULL )
35-
, m_codecContext( NULL )
14+
: EssenceDesc( codecId )
3615
{
37-
setAudioCodec( codecId );
3816
}
3917

40-
void AudioDesc::setAudioCodec( const std::string& codecName )
18+
AudioDesc::AudioDesc( const EssenceDesc& essenceDesc )
19+
: EssenceDesc( essenceDesc.getCodecId() )
4120
{
42-
avcodec_register_all(); // Warning: should be called only once
43-
m_codec = avcodec_find_encoder_by_name( codecName.c_str() );
44-
initCodecContext();
21+
m_codec = essenceDesc.getCodec();
22+
m_codecContext = essenceDesc.getCodecContext();
4523
}
4624

47-
void AudioDesc::setAudioCodec( const AVCodecID codecId )
48-
{
49-
avcodec_register_all(); // Warning: should be called only once
50-
m_codec = avcodec_find_encoder( codecId );
51-
initCodecContext();
52-
}
53-
54-
void AudioDesc::setAudioParameters( const size_t sampleRate, const size_t channels, const AVSampleFormat sampleFormat )
55-
{
56-
m_codecContext->sample_rate = sampleRate;
57-
m_codecContext->channels = channels;
58-
m_codecContext->sample_fmt = sampleFormat;
59-
}
60-
61-
void AudioDesc::initCodecContext( )
62-
{
63-
if( m_codec == NULL )
64-
{
65-
throw std::runtime_error( "unknown audio codec" );
66-
}
67-
68-
if( ( m_codecContext = avcodec_alloc_context3( m_codec ) ) == NULL )
69-
{
70-
throw std::runtime_error( "unable to create context for audio context" );
71-
}
72-
73-
// Set default codec parameters
74-
if( avcodec_get_context_defaults3( m_codecContext, m_codec ) != 0 )
75-
{
76-
throw std::runtime_error( "unable to find audio codec default values" );
77-
}
78-
}
79-
80-
void AudioDesc::set( const std::string& key, const std::string& flag, const bool enable )
25+
AudioFrameDesc AudioDesc::getFrameDesc() const
8126
{
82-
int error = 0;
83-
int64_t optVal;
27+
assert( m_codecContext != NULL );
28+
AudioFrameDesc audioFrameDesc;
8429

85-
const AVOption* flagOpt = av_opt_find( m_codecContext, flag.c_str(), key.c_str(), 0, 0 );
86-
87-
if( ! flagOpt )
88-
{
89-
std::cout << flag << std::endl << " : " << flagOpt->default_val.i64 << std::endl;
90-
throw std::runtime_error( "unknown flag " + flag );
91-
}
92-
93-
error = av_opt_get_int( m_codecContext, key.c_str(), AV_OPT_SEARCH_CHILDREN, &optVal );
94-
if( error != 0 )
95-
{
96-
std::string err( "", AV_ERROR_MAX_STRING_SIZE );
97-
//av_make_error_string( const_cast<char*>(err.c_str()), err.size(), error );
98-
av_strerror( error, const_cast<char*>(err.c_str()), err.size() );
99-
throw std::runtime_error( "unknown key " + key + ": " + err );
100-
}
101-
102-
if( enable )
103-
optVal = optVal | flagOpt->default_val.i64;
104-
else
105-
optVal = optVal &~ flagOpt->default_val.i64;
30+
audioFrameDesc.setChannels( m_codecContext->channels );
31+
audioFrameDesc.setSampleRate( m_codecContext->sample_rate );
32+
audioFrameDesc.setSampleFormat( m_codecContext->sample_fmt );
33+
// audioFrameDesc.setFps( 25 );
10634

107-
error = av_opt_set_int( m_codecContext, key.c_str(), optVal, AV_OPT_SEARCH_CHILDREN );
108-
if( error != 0 )
109-
{
110-
std::string err( "", AV_ERROR_MAX_STRING_SIZE );
111-
//av_make_error_string( const_cast<char*>(err.c_str()), err.size(), error );
112-
av_strerror( error, const_cast<char*>(err.c_str()), err.size() );
113-
throw std::runtime_error( "setting " + key + " parameter to " + flag + ": " + err );
114-
}
115-
}
116-
117-
void AudioDesc::set( const std::string& key, const bool value )
118-
{
119-
int error = av_opt_set_int( m_codecContext, key.c_str(), value, AV_OPT_SEARCH_CHILDREN );
120-
if( error != 0 )
121-
{
122-
std::string err( "", AV_ERROR_MAX_STRING_SIZE );
123-
//av_make_error_string( const_cast<char*>(err.c_str()), err.size(), error );
124-
av_strerror( error, const_cast<char*>(err.c_str()), err.size() );
125-
throw std::runtime_error( "setting " + key + " parameter to " + ( value ? "true" : "false" ) + ": " + err );
126-
}
127-
}
128-
129-
void AudioDesc::set( const std::string& key, const int value )
130-
{
131-
//const AVOption* flagOpt = av_opt_find( m_codecContext, key.c_str(), NULL, 0, AV_OPT_SEARCH_CHILDREN );
132-
133-
int error = av_opt_set_int( m_codecContext, key.c_str(), value, AV_OPT_SEARCH_CHILDREN );
134-
if( error != 0 )
135-
{
136-
std::ostringstream os;
137-
os << value;
138-
std::string err( "", AV_ERROR_MAX_STRING_SIZE );
139-
//av_make_error_string( const_cast<char*>(err.c_str()), err.size(), error );
140-
av_strerror( error, const_cast<char*>(err.c_str()), err.size() );
141-
throw std::runtime_error( "setting " + key + " parameter to " + os.str() + ": " + err );
142-
}
143-
}
144-
145-
void AudioDesc::set( const std::string& key, const int num, const int den )
146-
{
147-
AVRational ratio;
148-
ratio.num = num;
149-
ratio.den = den;
150-
int error = av_opt_set_q( m_codecContext, key.c_str(), ratio, AV_OPT_SEARCH_CHILDREN );
151-
if( error != 0 )
152-
{
153-
std::ostringstream os;
154-
os << num << "/" << den;
155-
std::string err( "", AV_ERROR_MAX_STRING_SIZE );
156-
//av_make_error_string( const_cast<char*>(err.c_str()), err.size(), error );
157-
av_strerror( error, const_cast<char*>(err.c_str()), err.size() );
158-
throw std::runtime_error( "setting " + key + " parameter to " + os.str() + ": " + err );
159-
}
160-
}
161-
162-
void AudioDesc::set( const std::string& key, const double value )
163-
{
164-
int error = av_opt_set_double( m_codecContext, key.c_str(), value, AV_OPT_SEARCH_CHILDREN );
165-
if( error != 0 )
166-
{
167-
std::ostringstream os;
168-
os << value;
169-
std::string err( "", AV_ERROR_MAX_STRING_SIZE );
170-
//av_make_error_string( const_cast<char*>(err.c_str()), err.size(), error );
171-
av_strerror( error, const_cast<char*>(err.c_str()), err.size() );
172-
throw std::runtime_error( "setting " + key + " parameter to " + os.str() + ": " + err );
173-
}
174-
}
175-
176-
void AudioDesc::set( const std::string& key, const std::string& value )
177-
{
178-
int error = av_opt_set( m_codecContext, key.c_str(), value.c_str(), AV_OPT_SEARCH_CHILDREN );
179-
if( error != 0 )
180-
{
181-
std::string err( "", AV_ERROR_MAX_STRING_SIZE );
182-
//av_make_error_string( const_cast<char*>(err.c_str()), err.size(), error );
183-
av_strerror( error, const_cast<char*>(err.c_str()), err.size() );
184-
throw std::runtime_error( "setting " + key + " parameter to " + value + ": " + err );
185-
}
186-
}
187-
188-
std::string AudioDesc::getAudioCodec() const
189-
{
190-
assert( m_codecContext != NULL );
191-
return avcodec_descriptor_get( m_codecContext->codec_id )->name;
192-
}
193-
194-
AVCodecID AudioDesc::getAudioCodecId() const
195-
{
196-
assert( m_codecContext != NULL );
197-
return m_codecContext->codec_id;
35+
return audioFrameDesc;
19836
}
19937

20038
const size_t AudioDesc::getSampleRate() const
@@ -215,18 +53,12 @@ const AVSampleFormat AudioDesc::getSampleFormat() const
21553
return m_codecContext->sample_fmt;
21654
}
21755

218-
AudioFrameDesc AudioDesc::getFrameDesc() const
56+
57+
void AudioDesc::setAudioParameters( const size_t sampleRate, const size_t channels, const AVSampleFormat sampleFormat )
21958
{
220-
assert( m_codecContext != NULL );
221-
AudioFrameDesc audioFrameDesc;
222-
223-
audioFrameDesc.setChannels( m_codecContext->channels );
224-
audioFrameDesc.setSampleRate( m_codecContext->sample_rate );
225-
audioFrameDesc.setSampleFormat( m_codecContext->sample_fmt );
226-
// audioFrameDesc.setFps( 25 );
227-
228-
return audioFrameDesc;
59+
m_codecContext->sample_rate = sampleRate;
60+
m_codecContext->channels = channels;
61+
m_codecContext->sample_fmt = sampleFormat;
22962
}
23063

231-
23264
}

src/AvTranscoder/DatasStructures/AudioDesc.hpp

Lines changed: 6 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#ifndef _AV_TRANSCODER_DATA_AUDIO_DESC_HPP_
22
#define _AV_TRANSCODER_DATA_AUDIO_DESC_HPP_
33

4-
#include "Image.hpp"
54
#include <string>
65

76
extern "C" {
@@ -15,50 +14,26 @@ extern "C" {
1514
#include <libavcodec/avcodec.h>
1615
}
1716

17+
#include <AvTranscoder/common.hpp>
18+
#include <AvTranscoder/DatasStructures/EssenceDesc.hpp>
1819
#include <AvTranscoder/DatasStructures/AudioFrame.hpp>
1920

2021
namespace avtranscoder
2122
{
2223

23-
class AvExport AudioDesc
24+
class AvExport AudioDesc : public EssenceDesc
2425
{
2526
public:
2627
AudioDesc( const std::string& codecName = "" );
2728
AudioDesc( const AVCodecID codecId );
28-
29-
void setAudioCodec( const std::string& codecName );
30-
void setAudioCodec( const AVCodecID codecId );
31-
32-
void setAudioParameters( const size_t sampleRate, const size_t channels, const AVSampleFormat sampleFormat );
33-
34-
void set( const std::string& key, const std::string& flag, const bool enable );
35-
void set( const std::string& key, const bool value );
36-
void set( const std::string& key, const int value );
37-
void set( const std::string& key, const int num, const int den );
38-
void set( const std::string& key, const double value );
39-
void set( const std::string& key, const std::string& value );
29+
AudioDesc( const EssenceDesc& essenceDesc );
4030

41-
std::string getAudioCodec() const;
42-
AVCodecID getAudioCodecId() const;
43-
31+
AudioFrameDesc getFrameDesc() const;
4432
const size_t getSampleRate() const;
4533
const size_t getChannels() const;
4634
const AVSampleFormat getSampleFormat() const;
47-
48-
#ifndef SWIG
49-
AVCodec* getCodec() const { return m_codec; }
50-
AVCodecContext* getCodecContext() const { return m_codecContext; }
51-
#endif
52-
53-
AudioFrameDesc getFrameDesc() const;
5435

55-
private:
56-
void initCodecContext( );
57-
58-
void checkError( int error );
59-
60-
AVCodec* m_codec;
61-
AVCodecContext* m_codecContext;
36+
void setAudioParameters( const size_t sampleRate, const size_t channels, const AVSampleFormat sampleFormat );
6237
};
6338

6439
}

0 commit comments

Comments
 (0)