-
Notifications
You must be signed in to change notification settings - Fork 42
/
iencoder.h
66 lines (61 loc) · 1.97 KB
/
iencoder.h
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
#ifndef _IENCODER_H
#define _IENCODER_H
#include <memory>
#include <vector>
#include "CoreAudioToolbox.h"
#include "ISource.h"
#include "ISink.h"
struct IEncoder {
virtual ~IEncoder() {}
virtual void setSource(const std::shared_ptr<ISource> &source) = 0;
virtual void setSink(const std::shared_ptr<ISink> &sink) = 0;
virtual uint32_t encodeChunk(UInt32 npackets) = 0;
virtual ISource *src() = 0;
virtual const AudioStreamBasicDescription &getInputDescription() const = 0;
virtual const AudioStreamBasicDescription &getOutputDescription() const = 0;
};
struct IEncoderStat {
virtual ~IEncoderStat() {}
virtual uint64_t samplesRead() const = 0;
virtual uint64_t samplesWritten() const = 0;
virtual uint64_t framesWritten() const = 0;
virtual double currentBitrate() const = 0;
virtual double overallBitrate() const = 0;
};
class EncoderStat {
uint64_t m_samples_written;
uint64_t m_frames_written;
uint64_t m_bytes_written;
double m_current_bitrate;
AudioStreamBasicDescription m_desc;
public:
EncoderStat()
: m_samples_written(0),
m_frames_written(0),
m_bytes_written(0)
{}
void setBasicDescription(const AudioStreamBasicDescription &desc)
{
m_desc = desc;
}
void updateWritten(uint32_t samples, uint32_t bytes)
{
m_frames_written += 1;
m_samples_written += samples;
m_bytes_written += bytes;
m_current_bitrate = calcBitrate(bytes, samples);
}
uint64_t samplesWritten() const { return m_samples_written; }
uint64_t framesWritten() const { return m_frames_written; }
double currentBitrate() const { return m_current_bitrate; }
double overallBitrate() const
{
return calcBitrate(m_bytes_written, m_samples_written);
}
double calcBitrate(uint64_t bytes, uint64_t samples) const
{
return samples ? (bytes * m_desc.mSampleRate * 8) / (1000.0 * samples)
: 0.0;
}
};
#endif