| 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 "GameClientStreamAudio.h" |
| 10 |
|
| 11 |
#include "cores/RetroPlayer/streams/RetroPlayerAudio.h" |
| 12 |
#include "games/addons/GameClientTranslator.h" |
| 13 |
#include "utils/log.h" |
| 14 |
|
| 15 |
using namespace KODI; |
| 16 |
using namespace GAME; |
| 17 |
|
| 18 |
CGameClientStreamAudio::CGameClientStreamAudio(double sampleRate) : m_sampleRate(sampleRate) |
| 19 |
{ |
| 20 |
} |
| 21 |
|
| 22 |
bool CGameClientStreamAudio::OpenStream(RETRO::IRetroPlayerStream* stream, |
| 23 |
const game_stream_properties& properties) |
| 24 |
{ |
| 25 |
RETRO::CRetroPlayerAudio* audioStream = dynamic_cast<RETRO::CRetroPlayerAudio*>(stream); |
| 26 |
if (audioStream == nullptr) |
| 27 |
{ |
| 28 |
CLog::Log(LOGERROR, "GAME: RetroPlayer stream is not an audio stream"); |
| 29 |
return false; |
| 30 |
} |
| 31 |
|
| 32 |
std::unique_ptr<RETRO::AudioStreamProperties> audioProperties( |
| 33 |
TranslateProperties(properties.audio, m_sampleRate)); |
| 34 |
if (audioProperties) |
| 35 |
{ |
| 36 |
if (audioStream->OpenStream(static_cast<const RETRO::StreamProperties&>(*audioProperties))) |
| 37 |
m_stream = stream; |
| 38 |
} |
| 39 |
|
| 40 |
return m_stream != nullptr; |
| 41 |
} |
| 42 |
|
| 43 |
void CGameClientStreamAudio::CloseStream() |
| 44 |
{ |
| 45 |
if (m_stream != nullptr) |
| 46 |
{ |
| 47 |
m_stream->CloseStream(); |
| 48 |
m_stream = nullptr; |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
void CGameClientStreamAudio::AddData(const game_stream_packet& packet) |
| 53 |
{ |
| 54 |
if (packet.type != GAME_STREAM_AUDIO) |
| 55 |
return; |
| 56 |
|
| 57 |
if (m_stream != nullptr) |
| 58 |
{ |
| 59 |
const game_stream_audio_packet& audio = packet.audio; |
| 60 |
|
| 61 |
RETRO::AudioStreamPacket audioPacket{audio.data, audio.size}; |
| 62 |
|
| 63 |
m_stream->AddStreamData(static_cast<RETRO::StreamPacket&>(audioPacket)); |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
RETRO::AudioStreamProperties* CGameClientStreamAudio::TranslateProperties( |
| 68 |
const game_stream_audio_properties& properties, double sampleRate) |
| 69 |
{ |
| 70 |
const RETRO::PCMFormat pcmFormat = CGameClientTranslator::TranslatePCMFormat(properties.format); |
| 71 |
if (pcmFormat == RETRO::PCMFormat::FMT_UNKNOWN) |
| 72 |
{ |
| 73 |
CLog::Log(LOGERROR, "GAME: Unknown PCM format: {}", static_cast<int>(properties.format)); |
| 74 |
return nullptr; |
| 75 |
} |
| 76 |
|
| 77 |
RETRO::AudioChannelMap channelMap = {{RETRO::AudioChannel::CH_NULL}}; |
| 78 |
unsigned int i = 0; |
| 79 |
if (properties.channel_map != nullptr) |
| 80 |
{ |
| 81 |
for (const GAME_AUDIO_CHANNEL* channelPtr = properties.channel_map; *channelPtr != GAME_CH_NULL; |
| 82 |
channelPtr++) |
| 83 |
{ |
| 84 |
RETRO::AudioChannel channel = CGameClientTranslator::TranslateAudioChannel(*channelPtr); |
| 85 |
if (channel == RETRO::AudioChannel::CH_NULL) |
| 86 |
{ |
| 87 |
CLog::Log(LOGERROR, "GAME: Unknown channel ID: {}", static_cast<int>(*channelPtr)); |
| 88 |
return nullptr; |
| 89 |
} |
| 90 |
|
| 91 |
channelMap[i++] = channel; |
| 92 |
if (i + 1 >= channelMap.size()) |
| 93 |
break; |
| 94 |
} |
| 95 |
} |
| 96 |
channelMap[i] = RETRO::AudioChannel::CH_NULL; |
| 97 |
|
| 98 |
if (channelMap[0] == RETRO::AudioChannel::CH_NULL) |
| 99 |
{ |
| 100 |
CLog::Log(LOGERROR, "GAME: Empty channel layout"); |
| 101 |
return nullptr; |
| 102 |
} |
| 103 |
|
| 104 |
return new RETRO::AudioStreamProperties{pcmFormat, sampleRate, channelMap}; |
| 105 |
} |