AvTranscoder  0.9.4
C++APIforLibav/FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
InputStream.cpp
Go to the documentation of this file.
1 #include "InputStream.hpp"
2 
4 
5 extern "C" {
6 #include <libavcodec/avcodec.h>
7 #include <libavformat/avformat.h>
8 }
9 
10 #include <stdexcept>
11 #include <cassert>
12 
13 namespace avtranscoder
14 {
15 
16 InputStream::InputStream(InputFile& inputFile, const size_t streamIndex)
17  : IInputStream()
18  , _inputFile(&inputFile)
19  , _codec(NULL)
20  , _streamCache()
21  , _streamIndex(streamIndex)
22  , _isActivated(false)
23 {
24  AVCodecContext* context = _inputFile->getFormatContext().getAVStream(_streamIndex).codec;
25 
26  switch(context->codec_type)
27  {
28  case AVMEDIA_TYPE_VIDEO:
29  {
30  _codec = new VideoCodec(eCodecTypeDecoder, *context);
31  break;
32  }
33  case AVMEDIA_TYPE_AUDIO:
34  {
35  _codec = new AudioCodec(eCodecTypeDecoder, *context);
36  break;
37  }
38  case AVMEDIA_TYPE_DATA:
39  {
40  _codec = new DataCodec(eCodecTypeDecoder, *context);
41  break;
42  }
43  default:
44  break;
45  }
46 }
47 
49 {
50  delete _codec;
51 }
52 
54 {
55  if(!_isActivated)
56  throw std::runtime_error("Can't read packet on non-activated input stream.");
57 
58  // if packet is already cached
59  if(!_streamCache.empty())
60  {
61  LOG_DEBUG("Get packet of '" << _inputFile->getFilename() << "' (stream " << _streamIndex << ") from the cache")
62  data.copyData(_streamCache.front().getData(), _streamCache.front().getSize());
63  _streamCache.pop();
64  }
65  // else read next packet
66  else
67  {
68  LOG_DEBUG("Read next packet of '" << _inputFile->getFilename() << "' (stream " << _streamIndex << ")")
69  return _inputFile->readNextPacket(data, _streamIndex) && _streamCache.empty();
70  }
71 
72  return true;
73 }
74 
76 {
77  assert(_streamIndex <= _inputFile->getFormatContext().getNbStreams());
78 
79  if(getProperties().getStreamType() != AVMEDIA_TYPE_VIDEO)
80  {
81  throw std::runtime_error("unable to get video descriptor on non-video stream");
82  }
83 
84  return *static_cast<VideoCodec*>(_codec);
85 }
86 
88 {
89  assert(_streamIndex <= _inputFile->getFormatContext().getNbStreams());
90 
91  if(getProperties().getStreamType() != AVMEDIA_TYPE_AUDIO)
92  {
93  throw std::runtime_error("unable to get audio descriptor on non-audio stream");
94  }
95 
96  return *static_cast<AudioCodec*>(_codec);
97 }
98 
100 {
101  assert(_streamIndex <= _inputFile->getFormatContext().getNbStreams());
102 
103  if(getProperties().getStreamType() != AVMEDIA_TYPE_DATA)
104  {
105  throw std::runtime_error("unable to get data descriptor on non-data stream");
106  }
107 
108  return *static_cast<DataCodec*>(_codec);
109 }
110 
112 {
114 }
115 
116 void InputStream::addPacket(const AVPacket& packet)
117 {
118  // Do not cache data if the stream is declared as unused in process
119  if(!_isActivated)
120  {
121  LOG_DEBUG("Do not add a packet data for the stream " << _streamIndex << " to the cache: stream not activated")
122  return;
123  }
124 
125  LOG_DEBUG("Add a packet data for the stream " << _streamIndex << " to the cache")
126  _streamCache.push(CodedData());
127  _streamCache.back().copyData(packet.data, packet.size);
128 }
129 
131 {
132  _streamCache = std::queue<CodedData>();
133 }
134 }
bool readNextPacket(CodedData &data)
Read the next packet of the stream.
Definition: InputStream.cpp:53
AudioCodec & getAudioCodec()
Definition: InputStream.cpp:87
void addPacket(const AVPacket &packet)
size_t _streamIndex
Index of the stream in the input file.
Definition: InputStream.hpp:45
DataCodec & getDataCodec()
Definition: InputStream.cpp:99
std::string getFilename() const
Definition: InputFile.hpp:80
void copyData(unsigned char *buffer, const size_t size)
Copy external data buffer.
Definition: CodedData.cpp:58
FormatContext & getFormatContext()
Definition: InputFile.hpp:82
const avtranscoder::StreamProperties & getStreamPropertiesWithIndex(const size_t streamIndex) const
InputFile * _inputFile
Has link (no ownership)
Definition: InputStream.hpp:40
const FileProperties & getProperties() const
Return media properties on the current InputFile.
Definition: InputFile.hpp:71
const StreamProperties & getProperties() const
Virtual based class of properties for all types of stream.
#define LOG_DEBUG(...)
Definition: log.hpp:17
This class describes coded data.
Definition: CodedData.hpp:18
VideoCodec & getVideoCodec()
Definition: InputStream.cpp:75
bool _isActivated
If the stream is activated, data read from it will be buffered.
Definition: InputStream.hpp:46
InputStream(const InputStream &inputStream)
std::queue< CodedData > _streamCache
Cache of packet data already read and corresponding to this stream.
Definition: InputStream.hpp:43
bool readNextPacket(CodedData &data, const size_t streamIndex)
Read the next packet of the specified stream.
Definition: InputFile.cpp:63
ICodec * _codec
Has ownership.
Definition: InputStream.hpp:41
AVStream & getAVStream(size_t index) const
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy