This repository has been archived by the owner on Oct 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 98
/
Copy pathsimpleplayer.cpp
200 lines (187 loc) · 6.04 KB
/
simpleplayer.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
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
/*
* simpleplayer.cpp - simpleplayer to demo API usage, no tricks
*
* Copyright (C) 2013-2014 Intel Corporation
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2.1
* of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free
* Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "decodeinput.h"
#include "common/log.h"
#include "common/utils.h"
#include "VideoDecoderHost.h"
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <X11/Xlib.h>
#include <va/va_x11.h>
using namespace YamiMediaCodec;
class SimplePlayer
{
public:
bool init(int argc, char** argv)
{
if (argc != 2) {
printf("usage: simpleplayer xxx.264\n");
return false;
}
m_input.reset(DecodeInput::create(argv[1]));
if (!m_input) {
fprintf(stderr, "failed to open %s", argv[1]);
return false;
}
//init decoder
m_decoder.reset(createVideoDecoder(m_input->getMimeType()), releaseVideoDecoder);
if (!m_decoder) {
fprintf(stderr, "failed create decoder for %s", m_input->getMimeType());
return false;
}
if (!initDisplay()) {
return false;
}
//set native display
m_decoder->setNativeDisplay(m_nativeDisplay.get());
return true;
}
bool run()
{
VideoConfigBuffer configBuffer;
memset(&configBuffer, 0, sizeof(configBuffer));
configBuffer.profile = VAProfileNone;
const string codecData = m_input->getCodecData();
if (codecData.size()) {
configBuffer.data = (uint8_t*)codecData.data();
configBuffer.size = codecData.size();
}
Decode_Status status = m_decoder->start(&configBuffer);
assert(status == DECODE_SUCCESS);
VideoDecodeBuffer inputBuffer;
while (m_input->getNextDecodeUnit(inputBuffer)) {
status = m_decoder->decode(&inputBuffer);
if (DECODE_FORMAT_CHANGE == status) {
//drain old buffers
renderOutputs();
const VideoFormatInfo *formatInfo = m_decoder->getFormatInfo();
resizeWindow(formatInfo->width, formatInfo->height);
//resend the buffer
status = m_decoder->decode(&inputBuffer);
}
if(status == DECODE_SUCCESS) {
renderOutputs();
} else {
ERROR("decode error status = %d", status);
break;
}
}
m_decoder->stop();
return true;
}
SimplePlayer():m_window(0), m_width(0), m_height(0) {}
~SimplePlayer()
{
if (m_nativeDisplay) {
vaTerminate(m_vaDisplay);
}
if (m_window) {
XDestroyWindow(m_display.get(), m_window);
}
}
private:
void renderOutputs()
{
VAStatus status = VA_STATUS_SUCCESS;
do {
SharedPtr<VideoFrame> frame = m_decoder->getOutput();
if (!frame)
break;
status = vaPutSurface(m_vaDisplay, (VASurfaceID)frame->surface,
m_window, 0, 0, m_width, m_height, 0, 0, m_width, m_height,
NULL, 0, 0);
if (status != VA_STATUS_SUCCESS) {
ERROR("vaPutSurface return %d", status);
break;
}
} while (1);
}
bool initDisplay()
{
Display* display = XOpenDisplay(NULL);
if (!display) {
fprintf(stderr, "Failed to XOpenDisplay \n");
return false;
}
m_display.reset(display, XCloseDisplay);
m_vaDisplay = vaGetDisplay(m_display.get());
int major, minor;
VAStatus status;
status = vaInitialize(m_vaDisplay, &major, &minor);
if (status != VA_STATUS_SUCCESS) {
fprintf(stderr, "init va failed status = %d", status);
return false;
}
m_nativeDisplay.reset(new NativeDisplay);
m_nativeDisplay->type = NATIVE_DISPLAY_VA;
m_nativeDisplay->handle = (intptr_t)m_vaDisplay;
return true;
}
void resizeWindow(int width, int height)
{
Display* display = m_display.get();
if (m_window) {
//todo, resize window;
} else {
DefaultScreen(display);
XSetWindowAttributes x11WindowAttrib;
x11WindowAttrib.event_mask = KeyPressMask;
m_window = XCreateWindow(display, DefaultRootWindow(display),
0, 0, width, height, 0, CopyFromParent, InputOutput,
CopyFromParent, CWEventMask, &x11WindowAttrib);
XMapWindow(display, m_window);
}
XSync(display, false);
{
DEBUG("m_window=%lu", m_window);
XWindowAttributes wattr;
XGetWindowAttributes(display, m_window, &wattr);
}
m_width = width;
m_height = height;
}
SharedPtr<Display> m_display;
SharedPtr<NativeDisplay> m_nativeDisplay;
VADisplay m_vaDisplay;
Window m_window;
SharedPtr<IVideoDecoder> m_decoder;
SharedPtr<DecodeInput> m_input;
int m_width, m_height;
};
int main(int argc, char** argv)
{
SimplePlayer player;
if (!player.init(argc, argv)) {
ERROR("init player failed with %s", argv[1]);
return -1;
}
if (!player.run()){
ERROR("run simple player failed");
return -1;
}
printf("play file done\n");
return 0;
}