| 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 "GameClientStreams.h" |
| 10 |
|
| 11 |
#include "GameClientStreamAudio.h" |
| 12 |
#include "GameClientStreamSwFramebuffer.h" |
| 13 |
#include "GameClientStreamVideo.h" |
| 14 |
#include "cores/RetroPlayer/streams/IRetroPlayerStream.h" |
| 15 |
#include "cores/RetroPlayer/streams/IStreamManager.h" |
| 16 |
#include "cores/RetroPlayer/streams/RetroPlayerStreamTypes.h" |
| 17 |
#include "games/addons/GameClient.h" |
| 18 |
#include "games/addons/GameClientTranslator.h" |
| 19 |
#include "utils/log.h" |
| 20 |
|
| 21 |
#include <memory> |
| 22 |
|
| 23 |
using namespace KODI; |
| 24 |
using namespace GAME; |
| 25 |
|
| 26 |
CGameClientStreams::CGameClientStreams(CGameClient& gameClient) : m_gameClient(gameClient) |
| 27 |
{ |
| 28 |
} |
| 29 |
|
| 30 |
void CGameClientStreams::Initialize(RETRO::IStreamManager& streamManager) |
| 31 |
{ |
| 32 |
m_streamManager = &streamManager; |
| 33 |
} |
| 34 |
|
| 35 |
void CGameClientStreams::Deinitialize() |
| 36 |
{ |
| 37 |
m_streamManager = nullptr; |
| 38 |
} |
| 39 |
|
| 40 |
IGameClientStream* CGameClientStreams::OpenStream(const game_stream_properties& properties) |
| 41 |
{ |
| 42 |
if (m_streamManager == nullptr) |
| 43 |
return nullptr; |
| 44 |
|
| 45 |
RETRO::StreamType retroStreamType; |
| 46 |
if (!CGameClientTranslator::TranslateStreamType(properties.type, retroStreamType)) |
| 47 |
{ |
| 48 |
CLog::Log(LOGERROR, "GAME: Invalid stream type: {}", static_cast<int>(properties.type)); |
| 49 |
return nullptr; |
| 50 |
} |
| 51 |
|
| 52 |
std::unique_ptr<IGameClientStream> gameStream = CreateStream(properties.type); |
| 53 |
if (!gameStream) |
| 54 |
{ |
| 55 |
CLog::Log(LOGERROR, "GAME: No stream implementation for type: {}", |
| 56 |
static_cast<int>(properties.type)); |
| 57 |
return nullptr; |
| 58 |
} |
| 59 |
|
| 60 |
RETRO::StreamPtr retroStream = m_streamManager->CreateStream(retroStreamType); |
| 61 |
if (!retroStream) |
| 62 |
{ |
| 63 |
CLog::Log(LOGERROR, "GAME: Invalid RetroPlayer stream type: {}", |
| 64 |
static_cast<int>(retroStreamType)); |
| 65 |
return nullptr; |
| 66 |
} |
| 67 |
|
| 68 |
if (!gameStream->OpenStream(retroStream.get(), properties)) |
| 69 |
{ |
| 70 |
CLog::Log(LOGERROR, "GAME: Failed to open audio stream"); |
| 71 |
return nullptr; |
| 72 |
} |
| 73 |
|
| 74 |
m_streams[gameStream.get()] = std::move(retroStream); |
| 75 |
|
| 76 |
return gameStream.release(); |
| 77 |
} |
| 78 |
|
| 79 |
void CGameClientStreams::CloseStream(IGameClientStream* stream) |
| 80 |
{ |
| 81 |
if (stream != nullptr) |
| 82 |
{ |
| 83 |
std::unique_ptr<IGameClientStream> streamHolder(stream); |
| 84 |
streamHolder->CloseStream(); |
| 85 |
|
| 86 |
m_streamManager->CloseStream(std::move(m_streams[stream])); |
| 87 |
m_streams.erase(stream); |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
std::unique_ptr<IGameClientStream> CGameClientStreams::CreateStream( |
| 92 |
GAME_STREAM_TYPE streamType) const |
| 93 |
{ |
| 94 |
std::unique_ptr<IGameClientStream> gameStream; |
| 95 |
|
| 96 |
switch (streamType) |
| 97 |
{ |
| 98 |
case GAME_STREAM_AUDIO: |
| 99 |
{ |
| 100 |
gameStream = std::make_unique<CGameClientStreamAudio>(m_gameClient.GetSampleRate()); |
| 101 |
break; |
| 102 |
} |
| 103 |
case GAME_STREAM_VIDEO: |
| 104 |
{ |
| 105 |
gameStream = std::make_unique<CGameClientStreamVideo>(); |
| 106 |
break; |
| 107 |
} |
| 108 |
case GAME_STREAM_SW_FRAMEBUFFER: |
| 109 |
{ |
| 110 |
gameStream = std::make_unique<CGameClientStreamSwFramebuffer>(); |
| 111 |
break; |
| 112 |
} |
| 113 |
default: |
| 114 |
break; |
| 115 |
} |
| 116 |
|
| 117 |
return gameStream; |
| 118 |
} |