add linux.

This commit is contained in:
2024-10-02 03:01:37 +08:00
parent 4d2affb862
commit 7c3dbcedfe
9 changed files with 251 additions and 25 deletions

View File

@ -29,27 +29,39 @@ VideoPlayer::~VideoPlayer() {
bool VideoPlayer::open(const std::string &deviceName) {
bool ret = true;
m_formatContext = avformat_alloc_context();
auto format = av_find_input_format("dshow");
#ifdef WIN32
constexpr auto format = "dshow";
std::ostringstream oss;
oss << "video=" << deviceName;
auto device = oss.str();
#else
constexpr auto format = "v4l2";
auto &device = deviceName;
#endif
auto inputFormat = av_find_input_format(format);
AVDictionary *dictionary = nullptr;
// ffmpeg -f dshow -list_options true -i video="UVC Camera"
av_dict_set(&dictionary, "video_size", "800*600", 0);
std::ostringstream oss;
oss << "video=" << deviceName;
auto device = oss.str();
int status = avformat_open_input(&m_formatContext, "video=UVC Camera", format, &dictionary);
int status = avformat_open_input(&m_formatContext, device.c_str(), inputFormat, &dictionary);
if (status != 0) {
char message[256] = {0};
av_make_error_string(message, sizeof(message), status);
LOG(error) << "open device[" << device << "] failed: " << status << "," << message;
ret = false;
} else {
av_dump_format(m_formatContext, 0, device.c_str(), 0);
// for (unsigned int i = 0; i < m_formatContext->nb_streams; i++) {
// AVStream *stream = m_formatContext->streams[i];
// if (stream->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
// std::cout << "当前分辨率: " << stream->codecpar->width << "x" << stream->codecpar->height << std::endl;
// }
// }
m_exit = false;
m_thread = std::thread(&VideoPlayer::run, this);
}
av_dict_free(&dictionary);
return ret;
}