Skip to content

Commit eeffa04

Browse files
author
Clement Champetier
committed
Add IOutputFile interface
* Overload this interface to create your own wrapper. * OutputFile: the default implentation of wrapper which uses LibAV/FFMpeg. * OutputFile: remove getProgressDuration. Add this function in IOutputStream interface.
1 parent 1082844 commit eeffa04

File tree

12 files changed

+137
-92
lines changed

12 files changed

+137
-92
lines changed

app/avProcessor/avProcessor.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <AvTranscoder/transcoder/Transcoder.hpp>
2+
#include <AvTranscoder/file/OutputFile.hpp>
23
#include <AvTranscoder/progress/ConsoleProgress.hpp>
34

45
#include <iostream>

src/AvTranscoder/file/IOutputFile.hpp

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
#ifndef _AV_TRANSCODER_FILE_IOUTPUT_FILE_HPP_
2+
#define _AV_TRANSCODER_FILE_IOUTPUT_FILE_HPP_
3+
4+
#include <AvTranscoder/common.hpp>
5+
6+
#include <AvTranscoder/codec/VideoCodec.hpp>
7+
#include <AvTranscoder/codec/AudioCodec.hpp>
8+
#include <AvTranscoder/codec/DataCodec.hpp>
9+
10+
#include <AvTranscoder/stream/OutputStream.hpp>
11+
#include <AvTranscoder/ProfileLoader.hpp>
12+
13+
#include <string>
14+
#include <exception>
15+
16+
namespace avtranscoder
17+
{
18+
19+
/**
20+
* @brief IOutputfile is the interface to wrap and write medias.
21+
* It can be overloaded to integrate custom wrapper.
22+
**/
23+
class AvExport IOutputFile
24+
{
25+
public:
26+
virtual ~IOutputFile() {};
27+
28+
/**
29+
* @brief Initialize the wrapper
30+
**/
31+
virtual bool setup() = 0;
32+
33+
/**
34+
* @brief Add a video output stream
35+
* @note call setup() before adding any stream
36+
* @param videoCodec description of output stream
37+
**/
38+
virtual IOutputStream& addVideoStream( const VideoCodec& videoCodec )
39+
{
40+
throw std::logic_error("function is not implemented");
41+
}
42+
43+
/**
44+
* @brief Add an audio output stream
45+
* @note call setup() before adding any stream
46+
* @param audioCodec description of output stream
47+
**/
48+
virtual IOutputStream& addAudioStream( const AudioCodec& audioCodec )
49+
{
50+
throw std::logic_error("function is not implemented");
51+
}
52+
53+
/**
54+
* @brief Add a data output stream
55+
* @note call setup() before adding any stream
56+
* @param dataCodec description of output stream
57+
**/
58+
virtual IOutputStream& addDataStream( const DataCodec& dataCodec )
59+
{
60+
throw std::logic_error("function is not implemented");
61+
}
62+
63+
/**
64+
* @brief Write the header of file (if necessary)
65+
**/
66+
virtual bool beginWrap() = 0;
67+
68+
/**
69+
* @brief Wrap a packet of data in the output ressource
70+
* @param data coded packet information for the current stream
71+
* @param streamId refers to the stream in output ressource
72+
**/
73+
virtual IOutputStream::EWrappingStatus wrap( const CodedData& data, const size_t streamIndex ) = 0;
74+
75+
/**
76+
* @brief Write the footer of file (if necessary)
77+
**/
78+
virtual bool endWrap() = 0;
79+
80+
/**
81+
* @brief Get the output stream
82+
* @param streamId select the output stream
83+
* @return the output stream reference
84+
**/
85+
virtual IOutputStream& getStream( const size_t streamIndex ) = 0;
86+
};
87+
88+
}
89+
90+
#endif

src/AvTranscoder/file/OutputFile.cpp

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,8 @@ IOutputStream::EWrappingStatus OutputFile::wrap( const CodedData& data, const si
112112

113113
av_free_packet( &packet );
114114

115-
double currentStreamDuration = getProgressDuration( 0 );
115+
// get duration of stream 0
116+
double currentStreamDuration = _outputStreams.at( 0 )->getStreamDuration();
116117
if( currentStreamDuration < _previousProcessedStreamDuration )
117118
{
118119
// if the current stream is strictly shorter than the previous, wait for more data
@@ -194,10 +195,4 @@ void OutputFile::setProfile( const ProfileLoader::Profile& profile )
194195
}
195196
}
196197

197-
double OutputFile::getProgressDuration( const size_t streamIndex )
198-
{
199-
AVStream& outputStream = _formatContext.getAVStream( streamIndex );
200-
return av_q2d( outputStream.time_base ) * outputStream.cur_dts;
201-
}
202-
203198
}

src/AvTranscoder/file/OutputFile.hpp

Lines changed: 22 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,20 @@
11
#ifndef _AV_TRANSCODER_FILE_OUTPUT_FILE_HPP_
22
#define _AV_TRANSCODER_FILE_OUTPUT_FILE_HPP_
33

4-
#include <AvTranscoder/common.hpp>
4+
#include <AvTranscoder/file/IOutputFile.hpp>
5+
56
#include <AvTranscoder/mediaProperty/util.hpp>
67
#include <AvTranscoder/file/FormatContext.hpp>
78

8-
#include <AvTranscoder/codec/VideoCodec.hpp>
9-
#include <AvTranscoder/codec/AudioCodec.hpp>
10-
#include <AvTranscoder/codec/DataCodec.hpp>
11-
12-
#include <AvTranscoder/stream/OutputStream.hpp>
13-
#include <AvTranscoder/ProfileLoader.hpp>
14-
15-
#include <string>
169
#include <vector>
1710

1811
namespace avtranscoder
1912
{
2013

2114
/**
22-
* @brief Outputfile is a simple C++ API to wrap and write medias.\n
23-
* the default implentation use avformat wrapper frome the LibAV/FFMpeg.\n
24-
* It can be overloaded to integrate custom wrapper.
15+
* @brief Outputfile is the default implentation of wrapper which uses LibAV/FFMpeg.
2516
**/
26-
class AvExport OutputFile
17+
class AvExport OutputFile : public IOutputFile
2718
{
2819
public:
2920
/**
@@ -32,79 +23,38 @@ class AvExport OutputFile
3223
**/
3324
OutputFile( const std::string& filename = "" );
3425

35-
virtual ~OutputFile(){};
36-
3726
/**
3827
* @brief Initialize the OutputFile, create format context to wrap essences into output file.
3928
* @note call this before adding streams using addVideoStream() or addAudioStream()
4029
* @exception ios_base::failure launched if unable to guess format or open output
4130
**/
42-
virtual bool setup();
43-
44-
/**
45-
* @brief Add an video output stream using the description.
46-
* @note call setup() before adding any stream
47-
* @param videoDesc description of output stream
48-
**/
49-
virtual IOutputStream& addVideoStream( const VideoCodec& videoDesc );
50-
51-
/**
52-
* @brief Add an audio output stream using the description.
53-
* @note call setup() before adding any stream
54-
* @param audioDesc description of output stream
55-
**/
56-
virtual IOutputStream& addAudioStream( const AudioCodec& audioDesc );
31+
bool setup();
5732

58-
/**
59-
* @brief Add an data output stream using the description.
60-
* @note call setup() before adding any stream
61-
* @param dataDesc description of output stream
62-
**/
63-
virtual IOutputStream& addDataStream( const DataCodec& dataDesc );
33+
IOutputStream& addVideoStream( const VideoCodec& videoDesc );
34+
IOutputStream& addAudioStream( const AudioCodec& audioDesc );
35+
IOutputStream& addDataStream( const DataCodec& dataDesc );
6436

65-
/**
66-
* @brief get the output stream description.
67-
* @param streamId select the output stream
68-
* @return the output stream reference
69-
**/
70-
virtual IOutputStream& getStream( const size_t streamId );
37+
bool beginWrap();
38+
IOutputStream::EWrappingStatus wrap( const CodedData& data, const size_t streamId );
39+
bool endWrap();
7140

72-
/**
73-
* @brief Initialise the wrapping
74-
* @note this method write the header of file if necessary
75-
**/
76-
virtual bool beginWrap( );
77-
78-
/**
79-
* @brief Wrap a packet of data in the output ressource
80-
* @param data coded packet information for the current stream
81-
* @param streamId refers to the stream in output ressource
82-
**/
83-
virtual IOutputStream::EWrappingStatus wrap( const CodedData& data, const size_t streamId );
84-
85-
/**
86-
* @brief Finalize the end of the wrapping
87-
* @note this method write the footer of file if necessary
88-
**/
89-
virtual bool endWrap( );
90-
91-
/**
92-
* @brief Set the format of the output file
93-
* @param desc: the profile of the output format
94-
*/
95-
virtual void setProfile( const ProfileLoader::Profile& profile );
96-
9741
/**
9842
* @brief Add metadata to the output file.
9943
* @note Depending on the format, you are not sure to find your metadata after the transcode.
10044
*/
101-
virtual void addMetadata( const PropertiesMap& dataMap );
102-
virtual void addMetadata( const std::string& key, const std::string& value );
45+
void addMetadata( const PropertiesMap& dataMap );
46+
void addMetadata( const std::string& key, const std::string& value );
47+
48+
IOutputStream& getStream( const size_t streamId );
49+
FormatContext& getFormatContext() { return _formatContext; }
10350

104-
virtual void setVerbose( bool verbose = false ){ _verbose = verbose; }
51+
/**
52+
* @brief Set the format of the output file
53+
* @param profile: the profile of the output format
54+
*/
55+
void setProfile( const ProfileLoader::Profile& profile );
10556

106-
/// By default get the duration, in seconds, of the first output stream
107-
virtual double getProgressDuration( const size_t streamIndex = 0 );
57+
void setVerbose( bool verbose = false ){ _verbose = verbose; }
10858

10959
private:
11060
FormatContext _formatContext;

src/AvTranscoder/file/file.i

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22
#include <AvTranscoder/file/util.hpp>
33
#include <AvTranscoder/file/FormatContext.hpp>
44
#include <AvTranscoder/file/InputFile.hpp>
5+
#include <AvTranscoder/file/IOutputFile.hpp>
56
#include <AvTranscoder/file/OutputFile.hpp>
67
%}
78

89
%include <AvTranscoder/file/util.hpp>
910
%include <AvTranscoder/file/FormatContext.hpp>
1011
%include <AvTranscoder/file/InputFile.hpp>
12+
%include <AvTranscoder/file/IOutputFile.hpp>
1113
%include <AvTranscoder/file/OutputFile.hpp>

src/AvTranscoder/stream/IOutputStream.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class AvExport IOutputStream
2525
virtual ~IOutputStream() {};
2626

2727
virtual size_t getStreamIndex() const = 0;
28+
virtual double getStreamDuration() const = 0;
2829

2930
virtual EWrappingStatus wrap( const CodedData& data ) = 0;
3031
};

src/AvTranscoder/stream/OutputStream.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ OutputStream::OutputStream( OutputFile& outputFile, const size_t streamIndex )
1414
{
1515
}
1616

17+
double OutputStream::getStreamDuration() const
18+
{
19+
AVStream& outputStream = _outputFile->getFormatContext().getAVStream( _streamIndex );
20+
return av_q2d( outputStream.time_base ) * outputStream.cur_dts;
21+
}
22+
1723
IOutputStream::EWrappingStatus OutputStream::wrap( const CodedData& data )
1824
{
1925
assert( _outputFile != NULL );

src/AvTranscoder/stream/OutputStream.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class AvExport OutputStream : public IOutputStream
1414
OutputStream( OutputFile& outputFile, const size_t streamIndex );
1515

1616
size_t getStreamIndex() const { return _streamIndex; }
17+
double getStreamDuration() const;
1718

1819
IOutputStream::EWrappingStatus wrap( const CodedData& data );
1920

src/AvTranscoder/transcoder/StreamTranscoder.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ namespace avtranscoder
2222

2323
StreamTranscoder::StreamTranscoder(
2424
IInputStream& inputStream,
25-
OutputFile& outputFile
25+
IOutputFile& outputFile
2626
)
2727
: _inputStream( &inputStream )
2828
, _outputStream( NULL )
@@ -66,7 +66,7 @@ StreamTranscoder::StreamTranscoder(
6666

6767
StreamTranscoder::StreamTranscoder(
6868
IInputStream& inputStream,
69-
OutputFile& outputFile,
69+
IOutputFile& outputFile,
7070
const ProfileLoader::Profile& profile,
7171
const int subStreamIndex,
7272
const size_t offset
@@ -163,7 +163,7 @@ StreamTranscoder::StreamTranscoder(
163163

164164
StreamTranscoder::StreamTranscoder(
165165
const ICodec& inputCodec,
166-
OutputFile& outputFile,
166+
IOutputFile& outputFile,
167167
const ProfileLoader::Profile& profile
168168
)
169169
: _inputStream( NULL )

src/AvTranscoder/transcoder/StreamTranscoder.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#include <AvTranscoder/decoder/IDecoder.hpp>
1010
#include <AvTranscoder/encoder/IEncoder.hpp>
1111

12-
#include <AvTranscoder/file/OutputFile.hpp>
12+
#include <AvTranscoder/file/IOutputFile.hpp>
1313

1414
#include <AvTranscoder/ProfileLoader.hpp>
1515

@@ -25,18 +25,18 @@ class AvExport StreamTranscoder
2525
* @brief rewrap stream
2626
* @note offset feature when rewrap a stream is not supported
2727
**/
28-
StreamTranscoder( IInputStream& inputStream, OutputFile& outputFile );
28+
StreamTranscoder( IInputStream& inputStream, IOutputFile& outputFile );
2929

3030
/**
3131
* @brief transcode stream
3232
**/
33-
StreamTranscoder( IInputStream& inputStream, OutputFile& outputFile, const ProfileLoader::Profile& profile, const int subStreamIndex = -1, const size_t offset = 0 );
33+
StreamTranscoder( IInputStream& inputStream, IOutputFile& outputFile, const ProfileLoader::Profile& profile, const int subStreamIndex = -1, const size_t offset = 0 );
3434

3535
/**
3636
* @brief encode from a generated stream
3737
* @note offset feature has no sense here
3838
**/
39-
StreamTranscoder( const ICodec& inputCodec, OutputFile& outputFile, const ProfileLoader::Profile& profile );
39+
StreamTranscoder( const ICodec& inputCodec, IOutputFile& outputFile, const ProfileLoader::Profile& profile );
4040

4141
~StreamTranscoder();
4242

src/AvTranscoder/transcoder/Transcoder.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
namespace avtranscoder
1212
{
1313

14-
Transcoder::Transcoder( OutputFile& outputFile )
14+
Transcoder::Transcoder( IOutputFile& outputFile )
1515
: _outputFile( outputFile )
1616
, _inputFiles()
1717
, _streamTranscoders()
@@ -288,7 +288,7 @@ void Transcoder::process( IProgress& progress )
288288

289289
frameProcessed = processFrame();
290290

291-
double progressDuration = _outputFile.getProgressDuration();
291+
double progressDuration = _outputFile.getStream( 0 ).getStreamDuration();
292292
if( progress.progress( ( progressDuration > totalDuration )? totalDuration : progressDuration, totalDuration ) == eJobStatusCancel )
293293
break;
294294

@@ -314,7 +314,6 @@ void Transcoder::setVerbose( bool verbose )
314314
{
315315
(*it)->setVerbose( _verbose );
316316
}
317-
_outputFile.setVerbose( _verbose );
318317

319318
// Print stuff which is only useful for ffmpeg developers.
320319
if( _verbose )

0 commit comments

Comments
 (0)