Skip to content

Commit f757baf

Browse files
committed
Merge pull request #260 from cchampet/dev_InputStreamGetProperties
IInputStream: add getProperties() method
2 parents e9dbd42 + 5c7573e commit f757baf

File tree

11 files changed

+64
-49
lines changed

11 files changed

+64
-49
lines changed

src/AvTranscoder/file/IOutputFile.hpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ class AvExport IOutputFile
2626

2727
/**
2828
* @brief Add a video output stream
29-
* @note call setup() before adding any stream
3029
* @param videoCodec description of output stream
3130
**/
3231
virtual IOutputStream& addVideoStream( const VideoCodec& videoCodec )
@@ -36,7 +35,6 @@ class AvExport IOutputFile
3635

3736
/**
3837
* @brief Add an audio output stream
39-
* @note call setup() before adding any stream
4038
* @param audioCodec description of output stream
4139
**/
4240
virtual IOutputStream& addAudioStream( const AudioCodec& audioCodec )
@@ -46,7 +44,6 @@ class AvExport IOutputFile
4644

4745
/**
4846
* @brief Add a data output stream
49-
* @note call setup() before adding any stream
5047
* @param dataCodec description of output stream
5148
**/
5249
virtual IOutputStream& addDataStream( const DataCodec& dataCodec )
@@ -62,7 +59,9 @@ class AvExport IOutputFile
6259
/**
6360
* @brief Wrap a packet of data in the output ressource
6461
* @param data coded packet information for the current stream
65-
* @param streamId refers to the stream in output ressource
62+
* @param streamIndex refers to the stream in output ressource
63+
* @return the wrapping status after wrapping
64+
* @see EWrappingStatus
6665
**/
6766
virtual IOutputStream::EWrappingStatus wrap( const CodedData& data, const size_t streamIndex ) = 0;
6867

@@ -73,7 +72,7 @@ class AvExport IOutputFile
7372

7473
/**
7574
* @brief Get the output stream
76-
* @param streamId select the output stream
75+
* @param streamIndex select the output stream
7776
* @return the output stream reference
7877
**/
7978
virtual IOutputStream& getStream( const size_t streamIndex ) = 0;

src/AvTranscoder/file/OutputFile.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,11 @@ IOutputStream& OutputFile::addDataStream( const DataCodec& dataDesc )
101101
return *outputStream;
102102
}
103103

104-
IOutputStream& OutputFile::getStream( const size_t streamId )
104+
IOutputStream& OutputFile::getStream( const size_t streamIndex )
105105
{
106-
if( streamId >= _outputStreams.size() )
106+
if( streamIndex >= _outputStreams.size() )
107107
throw std::runtime_error( "unable to get output stream (out of range)" );
108-
return *_outputStreams.at( streamId );
108+
return *_outputStreams.at( streamIndex );
109109
}
110110

111111
std::string OutputFile::getFilename() const
@@ -159,16 +159,16 @@ bool OutputFile::beginWrap( )
159159
return true;
160160
}
161161

162-
IOutputStream::EWrappingStatus OutputFile::wrap( const CodedData& data, const size_t streamId )
162+
IOutputStream::EWrappingStatus OutputFile::wrap( const CodedData& data, const size_t streamIndex )
163163
{
164164
if( ! data.getSize() )
165165
return IOutputStream::eWrappingSuccess;
166166

167-
LOG_DEBUG( "Wrap on stream " << streamId << " (" << data.getSize() << " bytes for frame " << _frameCount.at( streamId ) << ")" )
167+
LOG_DEBUG( "Wrap on stream " << streamIndex << " (" << data.getSize() << " bytes for frame " << _frameCount.at( streamIndex ) << ")" )
168168

169169
AVPacket packet;
170170
av_init_packet( &packet );
171-
packet.stream_index = streamId;
171+
packet.stream_index = streamIndex;
172172
packet.data = (uint8_t*)data.getData();
173173
packet.size = data.getSize();
174174

@@ -177,15 +177,15 @@ IOutputStream::EWrappingStatus OutputFile::wrap( const CodedData& data, const si
177177
// free packet.side_data, set packet.data to NULL and packet.size to 0
178178
av_free_packet( &packet );
179179

180-
const double currentStreamDuration = _outputStreams.at( streamId )->getStreamDuration();
180+
const double currentStreamDuration = _outputStreams.at( streamIndex )->getStreamDuration();
181181
if( currentStreamDuration < _previousProcessedStreamDuration )
182182
{
183183
// if the current stream is strictly shorter than the previous, wait for more data
184184
return IOutputStream::eWrappingWaitingForData;
185185
}
186186

187187
_previousProcessedStreamDuration = currentStreamDuration;
188-
_frameCount.at( streamId )++;
188+
_frameCount.at( streamIndex )++;
189189

190190
return IOutputStream::eWrappingSuccess;
191191
}

src/AvTranscoder/file/OutputFile.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ class AvExport OutputFile : public IOutputFile
3737
* @brief Open ressource, write header, and setup specific wrapping options given when call setupWrapping.
3838
* @note Need to add the streams to mux before calling this method.
3939
* @note After this call, a new list of AVOption, relative to the format choosen, will be available for the OutputFile.
40-
*/
40+
*/
4141
bool beginWrap();
4242

43-
IOutputStream::EWrappingStatus wrap( const CodedData& data, const size_t streamId );
43+
IOutputStream::EWrappingStatus wrap( const CodedData& data, const size_t streamIndex );
4444

4545
/**
4646
* @brief Close ressource and write trailer.
@@ -54,7 +54,7 @@ class AvExport OutputFile : public IOutputFile
5454
void addMetadata( const PropertyVector& data );
5555
void addMetadata( const std::string& key, const std::string& value );
5656

57-
IOutputStream& getStream( const size_t streamId );
57+
IOutputStream& getStream( const size_t streamIndex );
5858

5959
std::string getFilename() const;
6060

src/AvTranscoder/mediaProperty/StreamProperties.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,13 @@ float StreamProperties::getDuration() const
4343
return ( timeBase.num / (float) timeBase.den ) * _formatContext->streams[_streamIndex]->duration;
4444
}
4545

46+
AVMediaType StreamProperties::getStreamType() const
47+
{
48+
if( ! _formatContext )
49+
throw std::runtime_error( "unknown format context" );
50+
return _formatContext->streams[_streamIndex]->codec->codec_type;
51+
}
52+
4653
PropertyVector StreamProperties::getPropertiesAsVector() const
4754
{
4855
PropertyVector data;

src/AvTranscoder/mediaProperty/StreamProperties.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,11 @@ class AvExport StreamProperties
1919
size_t getStreamId() const;
2020
Rational getTimeBase() const;
2121
float getDuration() const; ///< in seconds
22+
AVMediaType getStreamType() const;
2223
const PropertyVector& getMetadatas() const { return _metadatas; }
2324

2425
#ifndef SWIG
25-
const AVFormatContext& getAVFormatContext() { return *_formatContext; }
26+
const AVFormatContext& getAVFormatContext() const { return *_formatContext; }
2627
#endif
2728

2829
PropertyMap getPropertiesAsMap() const; ///< Return all properties as a map (name of property, value)

src/AvTranscoder/stream/IInputStream.hpp

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
#ifndef _AV_TRANSCODER_CODED_STREAM_I_INPUT_STREAM_HPP_
22
#define _AV_TRANSCODER_CODED_STREAM_I_INPUT_STREAM_HPP_
33

4+
#include <AvTranscoder/mediaProperty/StreamProperties.hpp>
5+
46
#include <AvTranscoder/codec/AudioCodec.hpp>
57
#include <AvTranscoder/codec/VideoCodec.hpp>
68
#include <AvTranscoder/codec/DataCodec.hpp>
9+
710
#include <AvTranscoder/frame/Frame.hpp>
811

912
namespace avtranscoder
@@ -21,9 +24,17 @@ class AvExport IInputStream
2124
**/
2225
virtual bool readNextPacket( CodedData& data ) = 0;
2326

27+
/**
28+
* @note The returned object could be cast depending on the type of the stream (video, audio...)
29+
* @see VideoProperties, AudioProperties...
30+
* @return the properties of the stream
31+
*/
32+
virtual const StreamProperties& getProperties() const = 0;
33+
34+
/**
35+
* @return the index of the stream
36+
*/
2437
virtual size_t getStreamIndex() const = 0;
25-
virtual float getDuration() const = 0;
26-
virtual AVMediaType getStreamType() const = 0;
2738

2839
//@{
2940
/**
@@ -35,12 +46,16 @@ class AvExport IInputStream
3546
virtual DataCodec& getDataCodec() = 0;
3647
//@}
3748

38-
/**
39-
* @brief Activate the stream will buffered its data when read packets.
40-
**/
49+
//@{
50+
/**
51+
* @brief Functions about buffering
52+
* Activate the stream will buffered its data when read packets.
53+
* @see IInputStream methods
54+
*/
4155
virtual void activate( const bool activate = true ) = 0;
4256
virtual bool isActivated() const = 0;
4357
virtual void clearBuffering() = 0;
58+
//@}
4459
};
4560

4661
}

src/AvTranscoder/stream/IOutputStream.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ class AvExport IOutputStream
3636
*/
3737
virtual size_t getNbFrames() const = 0;
3838

39+
/**
40+
* @brief Wrap a packet of data
41+
* @return the wrapping status after wrapping
42+
* @see EWrappingStatus
43+
**/
3944
virtual EWrappingStatus wrap( const CodedData& data ) = 0;
4045
};
4146

src/AvTranscoder/stream/InputStream.cpp

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ VideoCodec& InputStream::getVideoCodec()
7676
{
7777
assert( _streamIndex <= _inputFile->getFormatContext().getNbStreams() );
7878

79-
if( getStreamType() != AVMEDIA_TYPE_VIDEO )
79+
if( getProperties().getStreamType() != AVMEDIA_TYPE_VIDEO )
8080
{
8181
throw std::runtime_error( "unable to get video descriptor on non-video stream" );
8282
}
@@ -88,7 +88,7 @@ AudioCodec& InputStream::getAudioCodec()
8888
{
8989
assert( _streamIndex <= _inputFile->getFormatContext().getNbStreams() );
9090

91-
if( getStreamType() != AVMEDIA_TYPE_AUDIO )
91+
if( getProperties().getStreamType() != AVMEDIA_TYPE_AUDIO )
9292
{
9393
throw std::runtime_error( "unable to get audio descriptor on non-audio stream" );
9494
}
@@ -100,22 +100,17 @@ DataCodec& InputStream::getDataCodec()
100100
{
101101
assert( _streamIndex <= _inputFile->getFormatContext().getNbStreams() );
102102

103-
if( getStreamType() != AVMEDIA_TYPE_DATA )
103+
if( getProperties().getStreamType() != AVMEDIA_TYPE_DATA )
104104
{
105105
throw std::runtime_error( "unable to get data descriptor on non-data stream" );
106106
}
107107

108108
return *static_cast<DataCodec*>( _codec );
109109
}
110110

111-
AVMediaType InputStream::getStreamType() const
111+
const StreamProperties& InputStream::getProperties() const
112112
{
113-
return _inputFile->getFormatContext().getAVStream( _streamIndex ).codec->codec_type;
114-
}
115-
116-
float InputStream::getDuration() const
117-
{
118-
return _inputFile->getProperties().getStreamPropertiesWithIndex( _streamIndex ).getDuration();
113+
return _inputFile->getProperties().getStreamPropertiesWithIndex( _streamIndex );
119114
}
120115

121116
void InputStream::addPacket( const AVPacket& packet )

src/AvTranscoder/stream/InputStream.hpp

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,25 +24,17 @@ class AvExport InputStream : public IInputStream
2424

2525
bool readNextPacket( CodedData& data );
2626

27+
const StreamProperties& getProperties() const;
2728
size_t getStreamIndex() const { return _streamIndex; }
28-
/// Get duration of the stream, in seconds
29-
float getDuration() const;
30-
AVMediaType getStreamType() const;
3129

3230
VideoCodec& getVideoCodec();
3331
AudioCodec& getAudioCodec();
3432
DataCodec& getDataCodec();
3533

36-
//@{
37-
/**
38-
* @brief Functions about buffering
39-
* @see IInputStream methods
40-
*/
4134
void activate( const bool activate = true ){ _isActivated = activate; };
4235
bool isActivated() const { return _isActivated; };
4336
void addPacket( const AVPacket& packet );
4437
void clearBuffering();
45-
//@}
4638

4739
private:
4840
InputFile* _inputFile; ///< Has link (no ownership)

src/AvTranscoder/transcoder/StreamTranscoder.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ StreamTranscoder::StreamTranscoder(
3939
, _needToSwitchToGenerator( false )
4040
{
4141
// create a re-wrapping case
42-
switch( _inputStream->getStreamType() )
42+
switch( _inputStream->getProperties().getStreamType() )
4343
{
4444
case AVMEDIA_TYPE_VIDEO :
4545
{
@@ -141,7 +141,7 @@ StreamTranscoder::StreamTranscoder(
141141
, _needToSwitchToGenerator( false )
142142
{
143143
// create a transcode case
144-
switch( _inputStream->getStreamType() )
144+
switch( _inputStream->getProperties().getStreamType() )
145145
{
146146
case AVMEDIA_TYPE_VIDEO :
147147
{
@@ -383,7 +383,7 @@ bool StreamTranscoder::processFrame()
383383
}
384384
else if( _offset < 0 )
385385
{
386-
const bool endOfStream = _outputStream->getStreamDuration() >= ( _inputStream->getDuration() + _offset );
386+
const bool endOfStream = _outputStream->getStreamDuration() >= ( _inputStream->getProperties().getDuration() + _offset );
387387
if( endOfStream )
388388
{
389389
LOG_INFO( "End of negative offset" )
@@ -516,10 +516,11 @@ float StreamTranscoder::getDuration() const
516516
{
517517
if( _inputStream )
518518
{
519-
const float totalDuration = _inputStream->getDuration() + _offset;
519+
const StreamProperties& streamProperties = _inputStream->getProperties();
520+
const float totalDuration = streamProperties.getDuration() + _offset;
520521
if( totalDuration < 0 )
521522
{
522-
LOG_WARN( "Offset of " << _offset << "s applied to a stream with a duration of " << _inputStream->getDuration() << "s. Set its duration to 0s." )
523+
LOG_WARN( "Offset of " << _offset << "s applied to a stream with a duration of " << streamProperties.getDuration() << "s. Set its duration to 0s." )
523524
return 0.;
524525
}
525526
return totalDuration;

src/AvTranscoder/transcoder/Transcoder.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ void Transcoder::addTranscodeStream( const std::string& filename, const size_t s
317317
// Add input file
318318
InputFile* referenceFile = addInputFile( filename, streamIndex, offset );
319319

320-
switch( referenceFile->getStream( streamIndex ).getStreamType() )
320+
switch( referenceFile->getStream( streamIndex ).getProperties().getStreamType() )
321321
{
322322
case AVMEDIA_TYPE_VIDEO:
323323
case AVMEDIA_TYPE_AUDIO:
@@ -385,7 +385,7 @@ ProfileLoader::Profile Transcoder::getProfileFromFile( InputFile& inputFile, con
385385
const StreamProperties* streamProperties = &inputFile.getProperties().getStreamPropertiesWithIndex( streamIndex );
386386
const VideoProperties* videoProperties = NULL;
387387
const AudioProperties* audioProperties = NULL;
388-
switch( inputFile.getStream( streamIndex ).getStreamType() )
388+
switch( inputFile.getStream( streamIndex ).getProperties().getStreamType() )
389389
{
390390
case AVMEDIA_TYPE_VIDEO:
391391
{
@@ -522,7 +522,7 @@ void Transcoder::fillProcessStat( ProcessStat& processStat )
522522
for( size_t streamIndex = 0; streamIndex < _streamTranscoders.size(); ++streamIndex )
523523
{
524524
IOutputStream& stream = _streamTranscoders.at( streamIndex )->getOutputStream();
525-
const AVMediaType mediaType = _streamTranscoders.at( streamIndex )->getInputStream().getStreamType();
525+
const AVMediaType mediaType = _streamTranscoders.at( streamIndex )->getInputStream().getProperties().getStreamType();
526526
switch( mediaType )
527527
{
528528
case AVMEDIA_TYPE_VIDEO:

0 commit comments

Comments
 (0)