2022-07-25 13:15:01 +02:00
|
|
|
#include "common/printing.h"
|
2023-06-10 15:01:54 +08:00
|
|
|
#include "common/jsonconfig.h"
|
2022-09-19 20:05:23 +08:00
|
|
|
#include "detection/media/media.h"
|
2023-06-07 16:19:07 +08:00
|
|
|
#include "modules/player/player.h"
|
2023-06-19 15:21:49 +08:00
|
|
|
#include "util/stringUtils.h"
|
2022-02-13 23:51:13 +01:00
|
|
|
|
2022-03-23 15:43:30 +01:00
|
|
|
#include <ctype.h>
|
|
|
|
|
|
2023-06-07 16:19:07 +08:00
|
|
|
#define FF_PLAYER_DISPLAY_NAME "Media Player"
|
2022-09-11 13:09:33 +02:00
|
|
|
#define FF_PLAYER_NUM_FORMAT_ARGS 4
|
2022-02-13 23:51:13 +01:00
|
|
|
|
2023-06-24 10:59:53 +08:00
|
|
|
void ffPrintPlayer(FFPlayerOptions* options)
|
2022-02-13 23:51:13 +01:00
|
|
|
{
|
2023-06-24 10:59:53 +08:00
|
|
|
const FFMediaResult* media = ffDetectMedia();
|
2022-02-13 23:51:13 +01:00
|
|
|
|
2022-09-19 17:53:00 +02:00
|
|
|
if(media->error.length > 0)
|
2022-02-13 23:51:13 +01:00
|
|
|
{
|
2023-06-24 10:59:53 +08:00
|
|
|
ffPrintError(FF_PLAYER_DISPLAY_NAME, 0, &options->moduleArgs, "%s", media->error.chars);
|
2022-02-13 23:51:13 +01:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-15 15:29:54 +08:00
|
|
|
FF_STRBUF_AUTO_DESTROY playerPretty = ffStrbufCreate();
|
2022-03-23 15:43:30 +01:00
|
|
|
|
|
|
|
|
//If we are on a website, prepend the website name
|
2022-09-18 17:32:15 +02:00
|
|
|
if(
|
2023-06-08 16:05:01 +08:00
|
|
|
ffStrbufIgnCaseCompS(&media->playerId, "spotify") == 0 ||
|
|
|
|
|
ffStrbufIgnCaseCompS(&media->playerId, "vlc") == 0
|
2022-09-18 17:32:15 +02:00
|
|
|
) {} // do noting, surely not a website, even if the url is set
|
|
|
|
|
else if(ffStrbufStartsWithS(&media->url, "https://www."))
|
2022-03-23 15:43:30 +01:00
|
|
|
ffStrbufAppendS(&playerPretty, media->url.chars + 12);
|
|
|
|
|
else if(ffStrbufStartsWithS(&media->url, "http://www."))
|
|
|
|
|
ffStrbufAppendS(&playerPretty, media->url.chars + 11);
|
|
|
|
|
else if(ffStrbufStartsWithS(&media->url, "https://"))
|
|
|
|
|
ffStrbufAppendS(&playerPretty, media->url.chars + 8);
|
|
|
|
|
else if(ffStrbufStartsWithS(&media->url, "http://"))
|
|
|
|
|
ffStrbufAppendS(&playerPretty, media->url.chars + 7);
|
|
|
|
|
|
|
|
|
|
//If we found a website name, make it more pretty
|
|
|
|
|
if(playerPretty.length > 0)
|
|
|
|
|
{
|
|
|
|
|
ffStrbufSubstrBeforeFirstC(&playerPretty, '/'); //Remove the path
|
|
|
|
|
ffStrbufSubstrBeforeLastC(&playerPretty, '.'); //Remove the TLD
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Check again for length, as we may have removed everything.
|
|
|
|
|
bool playerPrettyIsCustom = playerPretty.length > 0;
|
|
|
|
|
|
|
|
|
|
//If we don't have subdomains, it is usually more pretty to capitalize the first letter.
|
|
|
|
|
if(playerPrettyIsCustom && ffStrbufFirstIndexC(&playerPretty, '.') == playerPretty.length)
|
|
|
|
|
playerPretty.chars[0] = (char) toupper(playerPretty.chars[0]);
|
|
|
|
|
|
|
|
|
|
if(playerPrettyIsCustom)
|
|
|
|
|
ffStrbufAppendS(&playerPretty, " (");
|
|
|
|
|
|
|
|
|
|
ffStrbufAppend(&playerPretty, &media->player);
|
|
|
|
|
|
|
|
|
|
if(playerPrettyIsCustom)
|
|
|
|
|
ffStrbufAppendC(&playerPretty, ')');
|
|
|
|
|
|
2023-06-07 16:19:07 +08:00
|
|
|
if(options->moduleArgs.outputFormat.length == 0)
|
2022-02-13 23:51:13 +01:00
|
|
|
{
|
2023-08-15 21:40:22 +08:00
|
|
|
ffPrintLogoAndKey(FF_PLAYER_DISPLAY_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT);
|
2022-03-23 15:43:30 +01:00
|
|
|
ffStrbufPutTo(&playerPretty, stdout);
|
2022-02-13 23:51:13 +01:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2023-06-24 10:59:53 +08:00
|
|
|
ffPrintFormat(FF_PLAYER_DISPLAY_NAME, 0, &options->moduleArgs, FF_PLAYER_NUM_FORMAT_ARGS, (FFformatarg[]){
|
2022-03-23 15:43:30 +01:00
|
|
|
{FF_FORMAT_ARG_TYPE_STRBUF, &playerPretty},
|
2022-03-22 18:59:04 +01:00
|
|
|
{FF_FORMAT_ARG_TYPE_STRBUF, &media->player},
|
2023-06-08 16:05:01 +08:00
|
|
|
{FF_FORMAT_ARG_TYPE_STRBUF, &media->playerId},
|
2022-09-11 13:09:33 +02:00
|
|
|
{FF_FORMAT_ARG_TYPE_STRBUF, &media->url}
|
2022-02-13 23:51:13 +01:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-06-07 16:19:07 +08:00
|
|
|
|
|
|
|
|
bool ffParsePlayerCommandOptions(FFPlayerOptions* options, const char* key, const char* value)
|
|
|
|
|
{
|
|
|
|
|
const char* subKey = ffOptionTestPrefix(key, FF_PLAYER_MODULE_NAME);
|
|
|
|
|
if (!subKey) return false;
|
|
|
|
|
if (ffOptionParseModuleArgs(key, subKey, value, &options->moduleArgs))
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-17 10:56:54 +08:00
|
|
|
void ffParsePlayerJsonObject(FFPlayerOptions* options, yyjson_val* module)
|
2023-06-07 16:19:07 +08:00
|
|
|
{
|
2023-08-17 16:09:41 +08:00
|
|
|
yyjson_val *key_, *val;
|
|
|
|
|
size_t idx, max;
|
|
|
|
|
yyjson_obj_foreach(module, idx, max, key_, val)
|
2023-06-07 16:19:07 +08:00
|
|
|
{
|
2023-08-17 16:09:41 +08:00
|
|
|
const char* key = yyjson_get_str(key_);
|
|
|
|
|
if(ffStrEqualsIgnCase(key, "type"))
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
if (ffJsonConfigParseModuleArgs(key, val, &options->moduleArgs))
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
ffPrintError(FF_PLAYER_MODULE_NAME, 0, &options->moduleArgs, "Unknown JSON key %s", key);
|
2023-06-07 16:19:07 +08:00
|
|
|
}
|
|
|
|
|
}
|
2023-09-06 15:53:52 +08:00
|
|
|
|
2023-10-24 14:41:29 +08:00
|
|
|
void ffGeneratePlayerJsonConfig(FFPlayerOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module)
|
|
|
|
|
{
|
|
|
|
|
__attribute__((__cleanup__(ffDestroyPlayerOptions))) FFPlayerOptions defaultOptions;
|
|
|
|
|
ffInitPlayerOptions(&defaultOptions);
|
|
|
|
|
|
|
|
|
|
ffJsonConfigGenerateModuleArgsConfig(doc, module, &defaultOptions.moduleArgs, &options->moduleArgs);
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-23 10:14:22 +08:00
|
|
|
void ffGeneratePlayerJsonResult(FF_MAYBE_UNUSED FFMediaOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module)
|
2023-09-06 15:53:52 +08:00
|
|
|
{
|
|
|
|
|
const FFMediaResult* media = ffDetectMedia();
|
|
|
|
|
|
|
|
|
|
if(media->error.length > 0)
|
|
|
|
|
{
|
|
|
|
|
yyjson_mut_obj_add_strbuf(doc, module, "error", &media->error);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
yyjson_mut_val* obj = yyjson_mut_obj_add_obj(doc, module, "result");
|
|
|
|
|
yyjson_mut_obj_add_strbuf(doc, obj, "player", &media->player);
|
|
|
|
|
yyjson_mut_obj_add_strbuf(doc, obj, "playerId", &media->playerId);
|
|
|
|
|
yyjson_mut_obj_add_strbuf(doc, obj, "url", &media->url);
|
|
|
|
|
}
|
2023-10-07 13:40:40 +08:00
|
|
|
|
|
|
|
|
void ffPrintPlayerHelpFormat(void)
|
|
|
|
|
{
|
|
|
|
|
ffPrintModuleFormatHelp(FF_PLAYER_MODULE_NAME, "{1}", FF_PLAYER_NUM_FORMAT_ARGS, (const char* []) {
|
|
|
|
|
"Pretty player name",
|
|
|
|
|
"Player name",
|
|
|
|
|
"Player Identifier",
|
|
|
|
|
"URL name"
|
|
|
|
|
});
|
|
|
|
|
}
|
2023-10-23 13:32:18 +08:00
|
|
|
|
|
|
|
|
void ffInitPlayerOptions(FFPlayerOptions* options)
|
|
|
|
|
{
|
2023-10-23 13:39:07 +08:00
|
|
|
ffOptionInitModuleBaseInfo(
|
|
|
|
|
&options->moduleInfo,
|
|
|
|
|
FF_PLAYER_MODULE_NAME,
|
2023-11-17 14:12:17 +08:00
|
|
|
"Print music player name",
|
2023-10-23 13:39:07 +08:00
|
|
|
ffParsePlayerCommandOptions,
|
|
|
|
|
ffParsePlayerJsonObject,
|
|
|
|
|
ffPrintPlayer,
|
|
|
|
|
ffGeneratePlayerJsonResult,
|
|
|
|
|
ffPrintPlayerHelpFormat,
|
2023-10-24 14:41:29 +08:00
|
|
|
ffGeneratePlayerJsonConfig
|
2023-10-23 13:39:07 +08:00
|
|
|
);
|
2023-10-23 13:32:18 +08:00
|
|
|
ffOptionInitModuleArg(&options->moduleArgs);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ffDestroyPlayerOptions(FFPlayerOptions* options)
|
|
|
|
|
{
|
|
|
|
|
ffOptionDestroyModuleArg(&options->moduleArgs);
|
|
|
|
|
}
|