Debian Codegraph @

xbmc/games/addons/streams/GameClientStreamVideo.cpp

raw
1 /*
2 * Copyright (C) 2018 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
4 *
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
7 */
8
9 #include "GameClientStreamVideo.h"
10
11 #include "cores/RetroPlayer/streams/RetroPlayerVideo.h"
12 #include "games/addons/GameClientTranslator.h"
13 #include "utils/log.h"
14
15 using namespace KODI;
16 using namespace GAME;
17
18 bool CGameClientStreamVideo::OpenStream(RETRO::IRetroPlayerStream* stream,
19 const game_stream_properties& properties)
20 {
21 RETRO::CRetroPlayerVideo* videoStream = dynamic_cast<RETRO::CRetroPlayerVideo*>(stream);
22 if (videoStream == nullptr)
23 {
24 CLog::Log(LOGERROR, "GAME: RetroPlayer stream is not a video stream");
25 return false;
26 }
27
28 std::unique_ptr<RETRO::VideoStreamProperties> videoProperties(
29 TranslateProperties(properties.video));
30 if (videoProperties)
31 {
32 if (videoStream->OpenStream(static_cast<const RETRO::StreamProperties&>(*videoProperties)))
33 m_stream = stream;
34 }
35
36 return m_stream != nullptr;
37 }
38
39 void CGameClientStreamVideo::CloseStream()
40 {
41 if (m_stream != nullptr)
42 {
43 m_stream->CloseStream();
44 m_stream = nullptr;
45 }
46 }
47
48 void CGameClientStreamVideo::AddData(const game_stream_packet& packet)
49 {
50 if (packet.type != GAME_STREAM_VIDEO && packet.type != GAME_STREAM_SW_FRAMEBUFFER)
51 return;
52
53 if (m_stream != nullptr)
54 {
55 const game_stream_video_packet& video = packet.video;
56
57 RETRO::VideoRotation rotation = CGameClientTranslator::TranslateRotation(video.rotation);
58
59 RETRO::VideoStreamPacket videoPacket{
60 video.width, video.height, rotation, video.data, video.size,
61 };
62
63 m_stream->AddStreamData(static_cast<const RETRO::StreamPacket&>(videoPacket));
64 }
65 }
66
67 RETRO::VideoStreamProperties* CGameClientStreamVideo::TranslateProperties(
68 const game_stream_video_properties& properties)
69 {
70 const AVPixelFormat pixelFormat = CGameClientTranslator::TranslatePixelFormat(properties.format);
71 if (pixelFormat == AV_PIX_FMT_NONE)
72 {
73 CLog::Log(LOGERROR, "GAME: Unknown pixel format: {}", properties.format);
74 return nullptr;
75 }
76
77 const unsigned int nominalWidth = properties.nominal_width;
78 const unsigned int nominalHeight = properties.nominal_height;
79 if (nominalWidth == 0 || nominalHeight == 0)
80 {
81 CLog::Log(LOGERROR, "GAME: Invalid nominal dimensions: {}x{}", nominalWidth, nominalHeight);
82 return nullptr;
83 }
84
85 const unsigned int maxWidth = properties.max_width;
86 const unsigned int maxHeight = properties.max_height;
87 if (maxWidth == 0 || maxHeight == 0)
88 {
89 CLog::Log(LOGERROR, "GAME: Invalid max dimensions: {}x{}", maxWidth, maxHeight);
90 return nullptr;
91 }
92
93 if (nominalWidth > maxWidth || nominalHeight > maxHeight)
94 CLog::Log(LOGERROR, "GAME: Nominal dimensions ({}x{}) bigger than max dimensions ({}x{})",
95 nominalWidth, nominalHeight, maxWidth, maxHeight);
96
97 float pixelAspectRatio;
98
99 // Game API: If aspect_ratio is <= 0.0, an aspect ratio of
100 // (nominal_width / nominal_height) is assumed
101 if (properties.aspect_ratio <= 0.0f)
102 pixelAspectRatio = 1.0f;
103 else
104 pixelAspectRatio = properties.aspect_ratio * nominalHeight / nominalWidth;
105
106 return new RETRO::VideoStreamProperties{pixelFormat, nominalWidth, nominalHeight,
107 maxWidth, maxHeight, pixelAspectRatio};
108 }