forked from mikrosimage/avTranscoder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInputStream.cpp
More file actions
145 lines (119 loc) · 3.16 KB
/
Copy pathInputStream.cpp
File metadata and controls
145 lines (119 loc) · 3.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include "InputStream.hpp"
#include <AvTranscoder/file/InputFile.hpp>
extern "C" {
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
}
#include <stdexcept>
#include <cassert>
namespace avtranscoder
{
InputStream::InputStream( InputFile& inputFile, const size_t streamIndex )
: IInputStream( )
, _inputFile( &inputFile )
, _codec( NULL )
, _streamCache()
, _streamIndex( streamIndex )
, _isActivated( false )
{
AVCodecContext* context = _inputFile->getFormatContext().getAVStream( _streamIndex ).codec;
switch( context->codec_type )
{
case AVMEDIA_TYPE_VIDEO:
{
_codec = new VideoCodec( eCodecTypeDecoder, *context );
break;
}
case AVMEDIA_TYPE_AUDIO:
{
_codec = new AudioCodec( eCodecTypeDecoder, *context );
break;
}
case AVMEDIA_TYPE_DATA:
{
_codec = new DataCodec( eCodecTypeDecoder, *context );
break;
}
default:
break;
}
}
InputStream::InputStream( const InputStream& inputStream )
: IInputStream( )
, _inputFile( inputStream._inputFile )
, _codec( inputStream._codec )
, _streamCache()
, _streamIndex( inputStream._streamIndex )
, _isActivated( inputStream._isActivated )
{
}
InputStream::~InputStream( )
{
delete _codec;
}
bool InputStream::readNextPacket( CodedData& data )
{
if( ! _isActivated )
throw std::runtime_error( "Can't read packet on non-activated input stream." );
// if packet is already cached
if( ! _streamCache.empty() )
{
data.copyData( _streamCache.front().getData(), _streamCache.front().getSize() );
_streamCache.pop();
}
// else read next packet
else
{
return _inputFile->readNextPacket( data, _streamIndex ) && _streamCache.empty();
}
return true;
}
VideoCodec& InputStream::getVideoCodec()
{
assert( _streamIndex <= _inputFile->getFormatContext().getNbStreams() );
if( getStreamType() != AVMEDIA_TYPE_VIDEO )
{
throw std::runtime_error( "unable to get video descriptor on non-video stream" );
}
return *static_cast<VideoCodec*>( _codec );
}
AudioCodec& InputStream::getAudioCodec()
{
assert( _streamIndex <= _inputFile->getFormatContext().getNbStreams() );
if( getStreamType() != AVMEDIA_TYPE_AUDIO )
{
throw std::runtime_error( "unable to get audio descriptor on non-audio stream" );
}
return *static_cast<AudioCodec*>( _codec );
}
DataCodec& InputStream::getDataCodec()
{
assert( _streamIndex <= _inputFile->getFormatContext().getNbStreams() );
if( 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
{
return _inputFile->getFormatContext().getAVStream( _streamIndex ).codec->codec_type;
}
double InputStream::getDuration() const
{
// @todo: return stream duration, depending on its type (instead of format duration)
return _inputFile->getProperties().getDuration();
}
void InputStream::addPacket( AVPacket& packet )
{
// Do not cache data if the stream is declared as unused in process
if( ! _isActivated )
return;
_streamCache.push( CodedData() );
_streamCache.back().copyData( packet.data, packet.size );
}
void InputStream::clearBuffering()
{
_streamCache = std::queue<CodedData>();
}
}