/
j_simple_player.h
277 lines (240 loc) · 7.46 KB
/
j_simple_player.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
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
//
// Created by user on 11/17/23.
//
#ifndef FFMPEG_VIDEO_PLAYER_J_SIMPLE_PLAYER_H
#define FFMPEG_VIDEO_PLAYER_J_SIMPLE_PLAYER_H
#include "j_video_player/modules/j_i_audio_output.h"
#include "j_video_player/modules/j_i_source.h"
#include "j_video_player/modules/j_i_video_output.h"
namespace j_video_player {
class SimplePlayer {
public:
SimplePlayer(std::shared_ptr<IVideoSource> video_source,
std::shared_ptr<IAudioSource> audio_source,
std::shared_ptr<IVideoOutput> video_output,
std::shared_ptr<IAudioOutput> audio_output)
: video_source(std::move(video_source)),
audio_source(std::move(audio_source)),
video_output(std::move(video_output)),
audio_output(std::move(audio_output)),
image_converter_(
std::make_shared<ffmpeg_utils::FFMPEGImageConverter>()),
resampler_(std::make_shared<ffmpeg_utils::FFmpegAudioResampler>()),
clock_(std::make_shared<utils::ClockManager>()) {
attachComponents();
}
int open(const std::string &in_file) {
auto ret = video_source->open(in_file);
RETURN_IF_ERROR_LOG(ret, "open source failed, exit");
ret = audio_source->open(in_file);
RETURN_IF_ERROR_LOG(ret, "open source failed, exit");
media_file_info_ = video_source->getMediaFileInfo();
return 0;
}
const MediaFileInfo &getMediaFileInfo() const { return media_file_info_; }
MediaFileInfo getMediaFileInfo() { return media_file_info_; }
int prepareForOutput(const VideoOutputParameters &v_out_params,
const AudioOutputParameters &a_out_params) {
int ret = prepareImageConverter(media_file_info_, v_out_params);
RETURN_IF_ERROR(ret);
ret = prepareAudioResampler(media_file_info_, a_out_params);
RETURN_IF_ERROR(ret);
ret = prepareVideoOutput(v_out_params);
RETURN_IF_ERROR(ret);
ret = prepareAudioOutput(a_out_params);
RETURN_IF_ERROR(ret);
return 0;
}
int play() {
int ret = 0;
if (video_source) {
ret |= video_source->play();
}
if (audio_source) {
ret |= audio_source->play();
}
if (video_output) {
ret |= video_output->play();
}
if (audio_output) {
ret |= audio_output->play();
}
if (ret != 0) {
LOGE("play failed, exit");
return -1;
}
return 0;
}
int stop() {
int ret = 0;
if (video_output) {
ret |= video_output->stop();
}
if (audio_output) {
ret |= audio_output->stop();
}
if (video_source) {
ret |= video_source->stop();
}
if (audio_source) {
ret |= audio_source->stop();
}
if (ret != 0) {
LOGE("stop failed, exit");
return -1;
}
return 0;
}
int seek(int64_t timestamp) {
int ret = 0;
if (video_output) {
ret |= video_output->play();
}
if (audio_output) {
// seek with no audio output
ret |= audio_output->stop();
}
if (video_source) {
ret |= video_source->seek(timestamp);
}
if (audio_source) {
ret |= audio_source->seek(timestamp);
}
if (ret != 0) {
LOGE("seek failed, exit");
return -1;
}
return 0;
}
int pause() {
int ret = 0;
if (video_output) {
ret |= video_output->stop();
}
if (audio_output) {
ret |= audio_output->stop();
}
if (video_source) {
ret |= video_source->pause();
}
if (audio_source) {
ret |= audio_source->pause();
}
if (ret != 0) {
LOGE("pause failed, exit");
return -1;
}
return 0;
}
int64_t getDuration() const {
int64_t v_duration = 0;
int64_t a_duration = 0;
if (video_source) {
v_duration = video_source->getDuration();
}
if (audio_source) {
a_duration = audio_source->getDuration();
}
return std::max(v_duration, a_duration);
}
int64_t getCurrentPosition() {
if (video_source) {
return video_source->getCurrentPosition();
}
if (audio_source) {
return audio_source->getCurrentPosition();
}
return 0;
}
bool isPlaying() const {
if (video_source) {
return video_source->getState() == SourceState::kPlaying;
}
if (audio_source) {
return audio_source->getState() == SourceState::kPlaying;
}
return false;
}
bool isStopped() const {
if (video_source) {
return video_source->getState() == SourceState::kStopped ||
video_source->getState() == SourceState::kIdle;
}
if (audio_source) {
return audio_source->getState() == SourceState::kStopped ||
audio_source->getState() == SourceState::kIdle;
}
return false;
}
std::shared_ptr<IVideoSource> video_source;
std::shared_ptr<IAudioSource> audio_source;
std::shared_ptr<IVideoOutput> video_output;
std::shared_ptr<IAudioOutput> audio_output;
private:
int prepareImageConverter(const MediaFileInfo &src_media_file_info,
const VideoOutputParameters &v_out_params) {
if (video_output == nullptr) {
return 0;
}
int expected_width = v_out_params.width;
int expected_height = v_out_params.height;
int expected_pixel_format = v_out_params.pixel_format;
auto ret = image_converter_->prepare(
src_media_file_info.width, src_media_file_info.height,
(AVPixelFormat)src_media_file_info.pixel_format, expected_width,
expected_height, (AVPixelFormat)expected_pixel_format, 0, nullptr,
nullptr, nullptr);
RETURN_IF_ERROR_LOG(ret, "prepare image converter failed, exit");
return 0;
}
int prepareAudioResampler(const MediaFileInfo &src_media_file_info,
const AudioOutputParameters &a_out_params) {
if (audio_output == nullptr) {
return 0;
}
int max_frame_size =
a_out_params.num_frames_of_buffer * 4 * a_out_params.channels;
int output_channel_layout =
(a_out_params.channels == 1) ? AV_CH_LAYOUT_MONO : AV_CH_LAYOUT_STEREO;
auto ret = resampler_->prepare(
src_media_file_info.channels, a_out_params.channels,
src_media_file_info.channel_layout, output_channel_layout,
src_media_file_info.sample_rate, a_out_params.sample_rate,
(AVSampleFormat)src_media_file_info.sample_format, AV_SAMPLE_FMT_S16,
max_frame_size);
RETURN_IF_ERROR_LOG(ret, "prepare audio resampler failed, exit");
return 0;
}
int prepareVideoOutput(const VideoOutputParameters &v_out_params) {
if (video_output) {
auto ret = video_output->prepare(v_out_params);
RETURN_IF_ERROR_LOG(ret, "prepare video output failed, exit");
}
return 0;
}
int prepareAudioOutput(const AudioOutputParameters &a_out_params) {
if (audio_output) {
auto ret = audio_output->prepare(a_out_params);
RETURN_IF_ERROR_LOG(ret, "prepare audio output failed, exit");
}
return 0;
}
void attachComponents() {
if (video_output) {
video_output->attachVideoSource(video_source);
video_output->attachImageConverter(image_converter_);
video_output->attachAVSyncClock(clock_);
}
if (audio_output) {
audio_output->attachAudioSource(audio_source);
audio_output->attachResampler(resampler_);
audio_output->attachAVSyncClock(clock_);
}
}
std::shared_ptr<ffmpeg_utils::FFMPEGImageConverter> image_converter_;
std::shared_ptr<ffmpeg_utils::FFmpegAudioResampler> resampler_;
std::shared_ptr<utils::ClockManager> clock_;
MediaFileInfo media_file_info_;
};
} // namespace j_video_player
#endif // FFMPEG_VIDEO_PLAYER_J_SIMPLE_PLAYER_H