Skip to content

IInputStream: add getProperties() method #260

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Nov 2, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions src/AvTranscoder/file/IOutputFile.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ class AvExport IOutputFile

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

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

/**
* @brief Add a data output stream
* @note call setup() before adding any stream
* @param dataCodec description of output stream
**/
virtual IOutputStream& addDataStream( const DataCodec& dataCodec )
Expand All @@ -62,7 +59,9 @@ class AvExport IOutputFile
/**
* @brief Wrap a packet of data in the output ressource
* @param data coded packet information for the current stream
* @param streamId refers to the stream in output ressource
* @param streamIndex refers to the stream in output ressource
* @return the wrapping status after wrapping
* @see EWrappingStatus
**/
virtual IOutputStream::EWrappingStatus wrap( const CodedData& data, const size_t streamIndex ) = 0;

Expand All @@ -73,7 +72,7 @@ class AvExport IOutputFile

/**
* @brief Get the output stream
* @param streamId select the output stream
* @param streamIndex select the output stream
* @return the output stream reference
**/
virtual IOutputStream& getStream( const size_t streamIndex ) = 0;
Expand Down
16 changes: 8 additions & 8 deletions src/AvTranscoder/file/OutputFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,11 @@ IOutputStream& OutputFile::addDataStream( const DataCodec& dataDesc )
return *outputStream;
}

IOutputStream& OutputFile::getStream( const size_t streamId )
IOutputStream& OutputFile::getStream( const size_t streamIndex )
{
if( streamId >= _outputStreams.size() )
if( streamIndex >= _outputStreams.size() )
throw std::runtime_error( "unable to get output stream (out of range)" );
return *_outputStreams.at( streamId );
return *_outputStreams.at( streamIndex );
}

std::string OutputFile::getFilename() const
Expand Down Expand Up @@ -159,16 +159,16 @@ bool OutputFile::beginWrap( )
return true;
}

IOutputStream::EWrappingStatus OutputFile::wrap( const CodedData& data, const size_t streamId )
IOutputStream::EWrappingStatus OutputFile::wrap( const CodedData& data, const size_t streamIndex )
{
if( ! data.getSize() )
return IOutputStream::eWrappingSuccess;

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

AVPacket packet;
av_init_packet( &packet );
packet.stream_index = streamId;
packet.stream_index = streamIndex;
packet.data = (uint8_t*)data.getData();
packet.size = data.getSize();

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

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

_previousProcessedStreamDuration = currentStreamDuration;
_frameCount.at( streamId )++;
_frameCount.at( streamIndex )++;

return IOutputStream::eWrappingSuccess;
}
Expand Down
6 changes: 3 additions & 3 deletions src/AvTranscoder/file/OutputFile.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ class AvExport OutputFile : public IOutputFile
* @brief Open ressource, write header, and setup specific wrapping options given when call setupWrapping.
* @note Need to add the streams to mux before calling this method.
* @note After this call, a new list of AVOption, relative to the format choosen, will be available for the OutputFile.
*/
*/
bool beginWrap();

IOutputStream::EWrappingStatus wrap( const CodedData& data, const size_t streamId );
IOutputStream::EWrappingStatus wrap( const CodedData& data, const size_t streamIndex );

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

IOutputStream& getStream( const size_t streamId );
IOutputStream& getStream( const size_t streamIndex );

std::string getFilename() const;

Expand Down
7 changes: 7 additions & 0 deletions src/AvTranscoder/mediaProperty/StreamProperties.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ float StreamProperties::getDuration() const
return ( timeBase.num / (float) timeBase.den ) * _formatContext->streams[_streamIndex]->duration;
}

AVMediaType StreamProperties::getStreamType() const
{
if( ! _formatContext )
throw std::runtime_error( "unknown format context" );
return _formatContext->streams[_streamIndex]->codec->codec_type;
}

PropertyVector StreamProperties::getPropertiesAsVector() const
{
PropertyVector data;
Expand Down
3 changes: 2 additions & 1 deletion src/AvTranscoder/mediaProperty/StreamProperties.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ class AvExport StreamProperties
size_t getStreamId() const;
Rational getTimeBase() const;
float getDuration() const; ///< in seconds
AVMediaType getStreamType() const;
const PropertyVector& getMetadatas() const { return _metadatas; }

#ifndef SWIG
const AVFormatContext& getAVFormatContext() { return *_formatContext; }
const AVFormatContext& getAVFormatContext() const { return *_formatContext; }
#endif

PropertyMap getPropertiesAsMap() const; ///< Return all properties as a map (name of property, value)
Expand Down
25 changes: 20 additions & 5 deletions src/AvTranscoder/stream/IInputStream.hpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
#ifndef _AV_TRANSCODER_CODED_STREAM_I_INPUT_STREAM_HPP_
#define _AV_TRANSCODER_CODED_STREAM_I_INPUT_STREAM_HPP_

#include <AvTranscoder/mediaProperty/StreamProperties.hpp>

#include <AvTranscoder/codec/AudioCodec.hpp>
#include <AvTranscoder/codec/VideoCodec.hpp>
#include <AvTranscoder/codec/DataCodec.hpp>

#include <AvTranscoder/frame/Frame.hpp>

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

/**
* @note The returned object could be cast depending on the type of the stream (video, audio...)
* @see VideoProperties, AudioProperties...
* @return the properties of the stream
*/
virtual const StreamProperties& getProperties() const = 0;

/**
* @return the index of the stream
*/
virtual size_t getStreamIndex() const = 0;
virtual float getDuration() const = 0;
virtual AVMediaType getStreamType() const = 0;

//@{
/**
Expand All @@ -35,12 +46,16 @@ class AvExport IInputStream
virtual DataCodec& getDataCodec() = 0;
//@}

/**
* @brief Activate the stream will buffered its data when read packets.
**/
//@{
/**
* @brief Functions about buffering
* Activate the stream will buffered its data when read packets.
* @see IInputStream methods
*/
virtual void activate( const bool activate = true ) = 0;
virtual bool isActivated() const = 0;
virtual void clearBuffering() = 0;
//@}
};

}
Expand Down
5 changes: 5 additions & 0 deletions src/AvTranscoder/stream/IOutputStream.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ class AvExport IOutputStream
*/
virtual size_t getNbFrames() const = 0;

/**
* @brief Wrap a packet of data
* @return the wrapping status after wrapping
* @see EWrappingStatus
**/
virtual EWrappingStatus wrap( const CodedData& data ) = 0;
};

Expand Down
15 changes: 5 additions & 10 deletions src/AvTranscoder/stream/InputStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ VideoCodec& InputStream::getVideoCodec()
{
assert( _streamIndex <= _inputFile->getFormatContext().getNbStreams() );

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

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

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

return *static_cast<DataCodec*>( _codec );
}

AVMediaType InputStream::getStreamType() const
const StreamProperties& InputStream::getProperties() const
{
return _inputFile->getFormatContext().getAVStream( _streamIndex ).codec->codec_type;
}

float InputStream::getDuration() const
{
return _inputFile->getProperties().getStreamPropertiesWithIndex( _streamIndex ).getDuration();
return _inputFile->getProperties().getStreamPropertiesWithIndex( _streamIndex );
}

void InputStream::addPacket( const AVPacket& packet )
Expand Down
10 changes: 1 addition & 9 deletions src/AvTranscoder/stream/InputStream.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,17 @@ class AvExport InputStream : public IInputStream

bool readNextPacket( CodedData& data );

const StreamProperties& getProperties() const;
size_t getStreamIndex() const { return _streamIndex; }
/// Get duration of the stream, in seconds
float getDuration() const;
AVMediaType getStreamType() const;

VideoCodec& getVideoCodec();
AudioCodec& getAudioCodec();
DataCodec& getDataCodec();

//@{
/**
* @brief Functions about buffering
* @see IInputStream methods
*/
void activate( const bool activate = true ){ _isActivated = activate; };
bool isActivated() const { return _isActivated; };
void addPacket( const AVPacket& packet );
void clearBuffering();
//@}

private:
InputFile* _inputFile; ///< Has link (no ownership)
Expand Down
11 changes: 6 additions & 5 deletions src/AvTranscoder/transcoder/StreamTranscoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ StreamTranscoder::StreamTranscoder(
, _needToSwitchToGenerator( false )
{
// create a re-wrapping case
switch( _inputStream->getStreamType() )
switch( _inputStream->getProperties().getStreamType() )
{
case AVMEDIA_TYPE_VIDEO :
{
Expand Down Expand Up @@ -141,7 +141,7 @@ StreamTranscoder::StreamTranscoder(
, _needToSwitchToGenerator( false )
{
// create a transcode case
switch( _inputStream->getStreamType() )
switch( _inputStream->getProperties().getStreamType() )
{
case AVMEDIA_TYPE_VIDEO :
{
Expand Down Expand Up @@ -383,7 +383,7 @@ bool StreamTranscoder::processFrame()
}
else if( _offset < 0 )
{
const bool endOfStream = _outputStream->getStreamDuration() >= ( _inputStream->getDuration() + _offset );
const bool endOfStream = _outputStream->getStreamDuration() >= ( _inputStream->getProperties().getDuration() + _offset );
if( endOfStream )
{
LOG_INFO( "End of negative offset" )
Expand Down Expand Up @@ -516,10 +516,11 @@ float StreamTranscoder::getDuration() const
{
if( _inputStream )
{
const float totalDuration = _inputStream->getDuration() + _offset;
const StreamProperties& streamProperties = _inputStream->getProperties();
const float totalDuration = streamProperties.getDuration() + _offset;
if( totalDuration < 0 )
{
LOG_WARN( "Offset of " << _offset << "s applied to a stream with a duration of " << _inputStream->getDuration() << "s. Set its duration to 0s." )
LOG_WARN( "Offset of " << _offset << "s applied to a stream with a duration of " << streamProperties.getDuration() << "s. Set its duration to 0s." )
return 0.;
}
return totalDuration;
Expand Down
6 changes: 3 additions & 3 deletions src/AvTranscoder/transcoder/Transcoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ void Transcoder::addTranscodeStream( const std::string& filename, const size_t s
// Add input file
InputFile* referenceFile = addInputFile( filename, streamIndex, offset );

switch( referenceFile->getStream( streamIndex ).getStreamType() )
switch( referenceFile->getStream( streamIndex ).getProperties().getStreamType() )
{
case AVMEDIA_TYPE_VIDEO:
case AVMEDIA_TYPE_AUDIO:
Expand Down Expand Up @@ -385,7 +385,7 @@ ProfileLoader::Profile Transcoder::getProfileFromFile( InputFile& inputFile, con
const StreamProperties* streamProperties = &inputFile.getProperties().getStreamPropertiesWithIndex( streamIndex );
const VideoProperties* videoProperties = NULL;
const AudioProperties* audioProperties = NULL;
switch( inputFile.getStream( streamIndex ).getStreamType() )
switch( inputFile.getStream( streamIndex ).getProperties().getStreamType() )
{
case AVMEDIA_TYPE_VIDEO:
{
Expand Down Expand Up @@ -522,7 +522,7 @@ void Transcoder::fillProcessStat( ProcessStat& processStat )
for( size_t streamIndex = 0; streamIndex < _streamTranscoders.size(); ++streamIndex )
{
IOutputStream& stream = _streamTranscoders.at( streamIndex )->getOutputStream();
const AVMediaType mediaType = _streamTranscoders.at( streamIndex )->getInputStream().getStreamType();
const AVMediaType mediaType = _streamTranscoders.at( streamIndex )->getInputStream().getProperties().getStreamType();
switch( mediaType )
{
case AVMEDIA_TYPE_VIDEO:
Expand Down