-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpusher.cpp
156 lines (126 loc) · 3.36 KB
/
pusher.cpp
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
146
147
148
149
150
151
152
153
154
155
156
#include "pusher.h"
Pusher::Pusher()
{
}
Pusher::~Pusher()
{
Stop();
}
bool Pusher::Init(const Properties& properties)
{
avdevice_register_all();
av_log_set_level(AV_LOG_DEBUG);
ShowDevices();
bool ret = false;
//init capture
ret = video_capture_.Init();
if (!ret) {
DEBUG("video_capture_.Init() failed");
return false;
}
video_capture_.SetFrameCob(std::bind(&Pusher::VideoFrameHandler, this, std::placeholders::_1));
ret = audio_capture_.Init();
if (!ret) {
DEBUG("audio_capture_.Init() failed");
return false;
}
audio_capture_.SetFrameCob(std::bind(&Pusher::AudioFrameHandler, this, std::placeholders::_1));
//////////////////////////
//init encoder
ret = video_encoder_.Init();
if (!ret) {
DEBUG("video_encoder_.Init() failed");
return false;
}
ret = audio_encoder_.Init();
if (!ret) {
DEBUG("audio_encoder_.Init() failed");
return false;
}
//////////////////////////
//init muxer
ret = muxer_.Init(properties,
video_encoder_.GetAVCodecContext(),
audio_encoder_.GetAVCodecContext());
if (!ret) {
DEBUG("muxer_.Init() failed");
return false;
}
return true;
}
bool Pusher::Run()
{
int ret = -1;
timer.Rest();
//run
ret = video_capture_.Run();
if (!ret) {
DEBUG("video_capture_.Run() failed");
return false;
}
ret = audio_capture_.Run();
if (!ret) {
DEBUG("audio_capture_.Run() failed");
return false;
}
ret = muxer_.Run();
if (!ret) {
DEBUG("muxer_.Run() failed");
return false;
}
return true;
}
void Pusher::Stop()
{
audio_capture_.Stop();
video_capture_.Stop();
muxer_.Stop();
}
void Pusher::VideoFrameHandler(AVFrame* frame)
{
bool ret = false;
int time_base = timer.GetTimeBase();
int video_index = muxer_.GetVideoIndex();
std::vector<AVPacket*> pkts;
// DEBUG("video capture frame pts:%lld", frame->pts);
ret = video_encoder_.Encode(frame, pkts, video_index, time_base);
if (!ret) {
DEBUG("video_encoder_.Encode failed");
return;
}
auto delete_packet = [](AVPacket* pkt) {
av_packet_free(&pkt);
};
//push to queue
for (auto pkt: pkts) {
// DEBUG("video encode packet pts:%lld", pkt->pts);
std::shared_ptr<AVPacket> pkt_ptr(pkt, delete_packet);
ret = muxer_.PushQueue(pkt_ptr, MediaType::VIDEO);
if (!ret)
DEBUG("muxer_.PushQueue pkt failed");
}
}
void Pusher::AudioFrameHandler(AVFrame *frame)
{
bool ret = false;
int time_base = timer.GetTimeBase();
int audio_index = muxer_.GetAudioIndex();
std::vector<AVPacket*> pkts;
// DEBUG("audio capture frame pts:%lld", frame->pts);
ret = audio_encoder_.Encode(frame, pkts, audio_index, time_base);
if (!ret) {
DEBUG("audio_encoder_.Encode failed");
return;
}
auto delete_packet = [](AVPacket* pkt) {
av_packet_free(&pkt);
};
//push to queue
for (auto pkt: pkts) {
// DEBUG("audio encode packet pts:%lld", pkt->pts);
std::shared_ptr<AVPacket> pkt_ptr(pkt, delete_packet);
ret = muxer_.PushQueue(pkt_ptr, MediaType::AUDIO);
if (!ret)
DEBUG("muxer_.PushQueue pkt failed");
}
}