-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathInputStream.cpp
More file actions
134 lines (111 loc) · 3.45 KB
/
Copy pathInputStream.cpp
File metadata and controls
134 lines (111 loc) · 3.45 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
#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()
{
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())
{
LOG_DEBUG("Get packet of '" << _inputFile->getFilename() << "' (stream " << _streamIndex << ") from the cache")
data.copyData(_streamCache.front().getData(), _streamCache.front().getSize());
_streamCache.pop();
}
// else read next packet
else
{
LOG_DEBUG("Read next packet of '" << _inputFile->getFilename() << "' (stream " << _streamIndex << ")")
return _inputFile->readNextPacket(data, _streamIndex) && _streamCache.empty();
}
return true;
}
VideoCodec& InputStream::getVideoCodec()
{
assert(_streamIndex <= _inputFile->getFormatContext().getNbStreams());
if(getProperties().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(getProperties().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(getProperties().getStreamType() != AVMEDIA_TYPE_DATA)
{
throw std::runtime_error("unable to get data descriptor on non-data stream");
}
return *static_cast<DataCodec*>(_codec);
}
const StreamProperties& InputStream::getProperties() const
{
return _inputFile->getProperties().getStreamPropertiesWithIndex(_streamIndex);
}
void InputStream::addPacket(const AVPacket& packet)
{
// Do not cache data if the stream is declared as unused in process
if(!_isActivated)
{
LOG_DEBUG("Do not add a packet data for the stream " << _streamIndex << " to the cache: stream not activated")
return;
}
LOG_DEBUG("Add a packet data for the stream " << _streamIndex << " to the cache")
_streamCache.push(CodedData());
_streamCache.back().copyData(packet.data, packet.size);
}
void InputStream::clearBuffering()
{
_streamCache = std::queue<CodedData>();
}
}