Files

99 lines
4.8 KiB
C++

// OctoRadio (Gitea #5): portable parts of the in-client radio player. Stations come from the Octo client
// (radio_stations.hpp, generated by ops/octo_radio_stations.py); the Windows player streams the station's MP3
// over HTTP. ICY metadata ("Icy-MetaData: 1") carries the current "Artist - Title".
#pragma once
#include "gateway/radio_stations.hpp"
#include <cstddef>
#include <cstdint>
#include <optional>
#include <string>
#include <string_view>
#include <vector>
namespace gateway {
inline constexpr std::string_view RadioPrefix="OctoRadio";
// Add-on -> Backend: "P <cmd>" play station, "S" stop, "V <0..100>" volume, "Q" status query.
struct RadioCommand { char kind; unsigned value; };
inline std::optional<RadioCommand> radio_command(std::string_view text){
if(text=="S" || text=="Q")return RadioCommand{text[0],0};
if(text.size()<3 || text.size()>5 || (text[0]!='P' && text[0]!='V') || text[1]!=' ')return std::nullopt;
unsigned v=0;
for(char c:text.substr(2)){if(c<'0' || c>'9')return std::nullopt;v=v*10+unsigned(c-'0');}
if(text[0]=='V')return v<=100?std::optional<RadioCommand>{RadioCommand{'V',v}}:std::nullopt;
for(const auto& s:RadioStations)if(s.cmd==v)return RadioCommand{'P',v};
return std::nullopt;
}
inline const RadioStation* radio_station(unsigned cmd){
for(const auto& s:RadioStations)if(s.cmd==cmd)return &s;
return nullptr;
}
// MPEG audio layer III frame header (MPEG 1, 2 and 2.5).
struct Mp3Frame { unsigned rate,channels,kbps,length; };
inline std::optional<Mp3Frame> mp3_frame(const uint8_t* p,std::size_t n){
if(n<4 || p[0]!=0xff || (p[1]&0xe0)!=0xe0)return std::nullopt;
const unsigned version=(p[1]>>3)&3,layer=(p[1]>>1)&3,bi=p[2]>>4,si=(p[2]>>2)&3,pad=(p[2]>>1)&1;
if(version==1 || layer!=1 || bi==0 || bi==15 || si==3)return std::nullopt;
static constexpr unsigned v1[15]={0,32,40,48,56,64,80,96,112,128,160,192,224,256,320};
static constexpr unsigned v2[15]={0,8,16,24,32,40,48,56,64,80,96,112,128,144,160};
static constexpr unsigned rates[3]={44100,48000,32000};
const bool mpeg1=version==3;
const unsigned kbps=mpeg1?v1[bi]:v2[bi],rate=rates[si]>>(mpeg1?0:version==2?1:2);
const unsigned length=(mpeg1?144000u:72000u)*kbps/rate+pad;
return Mp3Frame{rate,(p[3]>>6)==3?1u:2u,kbps,length};
}
// First offset where two consecutive valid frames start (skips junk before the first real frame).
inline std::optional<std::size_t> mp3_sync(const std::vector<uint8_t>& b){
for(std::size_t i=0;i+4<=b.size();++i){
auto f=mp3_frame(b.data()+i,b.size()-i);
if(!f)continue;
if(i+f->length+4>b.size())return std::nullopt; // need more bytes to confirm
auto g=mp3_frame(b.data()+i+f->length,b.size()-i-f->length);
if(g && g->rate==f->rate && g->channels==f->channels)return i;
}
return std::nullopt;
}
// "StreamTitle='Artist - Title';StreamUrl='';" -> Artist - Title
inline std::string icy_stream_title(std::string_view meta){
constexpr std::string_view key="StreamTitle='";
auto at=meta.find(key);if(at==std::string_view::npos)return {};
auto from=at+key.size(),to=meta.find("';",from);
if(to==std::string_view::npos)to=meta.rfind('\'');
if(to==std::string_view::npos || to<from)return {};
std::string title(meta.substr(from,to-from));
for(auto& c:title)if(c==';' || c=='\n' || c=='\r')c=' '; // the status line is ';'-separated
return title.size()>200?title.substr(0,200):title;
}
// Splits an ICY stream (metaint audio bytes, then a 16*n byte metadata block) into audio and titles.
class IcyDemux {
public:
explicit IcyDemux(std::size_t metaint):metaint_(metaint),left_(metaint){}
// Appends audio to `audio`; returns the new title when a metadata block carried one.
std::optional<std::string> feed(const uint8_t* p,std::size_t n,std::vector<uint8_t>& audio){
std::optional<std::string> title;
while(n){
if(!metaint_){audio.insert(audio.end(),p,p+n);return title;}
if(left_){auto k=left_<n?left_:n;audio.insert(audio.end(),p,p+k);p+=k;n-=k;left_-=k;continue;}
if(meta_need_<0){meta_need_=int(*p++)*16;--n;meta_.clear();if(!meta_need_){meta_need_=-1;left_=metaint_;}continue;}
auto k=std::size_t(meta_need_)<n?std::size_t(meta_need_):n;
meta_.append(reinterpret_cast<const char*>(p),k);p+=k;n-=k;meta_need_-=int(k);
if(!meta_need_){
if(auto t=icy_stream_title(meta_);!t.empty())title=t;
meta_need_=-1;left_=metaint_;
}
}
return title;
}
private:
std::size_t metaint_,left_;
int meta_need_=-1;
std::string meta_;
};
// Backend -> add-on status line: "T;<state>;<cmd>;<title>" (state 0 off, 1 connecting, 2 playing, 3 error).
inline std::string radio_status(int state,unsigned cmd,std::string_view title){
return "T;"+std::to_string(state)+";"+std::to_string(cmd)+";"+std::string(title);
}
}