-
Notifications
You must be signed in to change notification settings - Fork 30
/
MediaPlayer.h
95 lines (84 loc) · 2.98 KB
/
MediaPlayer.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
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
#include "MediaCore/Snapshot.h"
#include "MediaCore/MediaReader.h"
#include "MediaCore/AudioRender.h"
#include <chrono>
using Clock = std::chrono::steady_clock;
namespace MEC
{
class SimplePcmStream : public MediaCore::AudioRender::ByteStream
{
public:
SimplePcmStream(MediaCore::MediaReader::Holder audrdr) : m_audrdr(audrdr) {}
uint32_t Read(uint8_t* buff, uint32_t buffSize, bool blocking) override
{
if (!m_audrdr)
return 0;
uint32_t readSize = buffSize;
int64_t pos;
bool eof;
if (!m_audrdr->ReadAudioSamples(buff, readSize, pos, eof, blocking))
return 0;
m_audPos = (double)pos/1000;
return readSize;
}
void SetAudioReader(MediaCore::MediaReader::Holder hAudioReader)
{
m_audrdr = hAudioReader;
}
void Flush() override {}
bool GetTimestampMs(int64_t& ts) override
{
return false;
}
public:
double m_audPos {0};
private:
MediaCore::MediaReader::Holder m_audrdr;
};
class MediaPlayer
{
public:
MediaPlayer(RenderUtils::TextureManager::Holder hTxmgr);
~MediaPlayer();
void Open(const std::string& url);
void Open(MediaCore::MediaParser::Holder hParser);
void Close();
bool IsOpened() const { return m_bIsVideoReady || m_bIsAudioReady; }
bool HasVideo() const { return m_bIsVideoReady; }
bool HasAudio() const { return m_bIsAudioReady; }
std::string GetUrl() { return m_playURL; };
float GetVideoDuration();
float GetAudioDuration();
float GetCurrentPos();
bool Play();
bool Pause();
bool Seek(float pos, bool bSeekingMode);
bool IsPlaying() const { return m_bIsPlay; }
bool IsSeeking() const { return m_bIsSeeking; }
bool Step(bool forward);
public:
ImTextureID GetFrame(float pos, bool blocking = false);
private:
std::string m_playURL;
RenderUtils::TextureManager::Holder m_txmgr;
RenderUtils::ManagedTexture::Holder m_tx;
MediaCore::MediaParser::Holder m_mediaParser;
bool m_useHwAccel {true};
int32_t m_audioStreamCount {0};
int32_t m_chooseAudioIndex {-1};
bool m_bIsVideoReady {false};
MediaCore::MediaReader::Holder m_vidrdr; // video
double m_playStartPos = 0.f;
Clock::time_point m_playStartTp;
bool m_bIsPlay {false};
bool m_bIsSeeking {false};
bool m_bIsAudioReady {false};
MediaCore::MediaReader::Holder m_audrdr; // audio
MediaCore::AudioRender* m_audrnd {nullptr};
MediaCore::AudioRender::PcmFormat c_audioRenderFormat {MediaCore::AudioRender::PcmFormat::FLOAT32};
int c_audioRenderChannels {2};
int c_audioRenderSampleRate {44100};
SimplePcmStream* m_pcmStream {nullptr};
bool m_audioNeedSeek {false};
};
}