Files

40 lines
3.0 KiB
C++

// OctoRadio (Gitea #5): station table from the Octo client, add-on commands, MP3 frame sync, ICY titles.
#include "gateway/radio_stream.hpp"
#include <cstdio>
#include <cstdlib>
using namespace gateway;
#define expect(x) do{if(!(x)){std::fprintf(stderr,"radio test line %d\n",__LINE__);std::exit(1);}}while(0)
int main(){
expect(RadioStations.size()==2 && RadioStations[0].cmd==1 && RadioStations[0].sound==61001 && RadioStations[1].sound==61002);
for(const auto& s:RadioStations)expect(s.url.starts_with("http://radio.octowow.st/listen/"));
expect(radio_command("P 1")->kind=='P' && radio_command("P 2")->value==2);
expect(!radio_command("P 3") && !radio_command("P x") && !radio_command("P") && !radio_command("http://evil/"));
expect(radio_command("V 0")->value==0 && radio_command("V 100")->value==100 && !radio_command("V 101"));
expect(radio_command("S")->kind=='S' && radio_command("Q")->kind=='Q' && !radio_command(""));
// MPEG-1 layer III 256 kbit/s 44.1 kHz joint stereo (the live stream's header), padded and unpadded.
const uint8_t h[4]={0xff,0xfb,0xd0,0x64},hp[4]={0xff,0xfb,0xd2,0x64};
auto f=mp3_frame(h,4);expect(f && f->rate==44100 && f->channels==2 && f->kbps==256 && f->length==835);
expect(mp3_frame(hp,4)->length==836 && !mp3_frame(h,3));
const uint8_t mono64[4]={0xff,0xfb,0x50,0xc4};auto m=mp3_frame(mono64,4);expect(m && m->channels==1 && m->kbps==64 && m->length==208);
std::vector<uint8_t> b{1,2,0xff,0xfb,9};
expect(!mp3_sync(b)); // a lone sync word needs its next frame
b.assign(7,0);b.insert(b.end(),h,h+4);b.resize(7+835,0);b.insert(b.end(),h,h+4);
expect(mp3_sync(b)==std::size_t(7));
// ICY: 4 audio bytes, metadata block (1*16), 4 audio bytes.
IcyDemux icy(4);std::vector<uint8_t> audio;
std::string meta="StreamTitle='A - B;C';";meta.resize(16*2,'\0');
std::vector<uint8_t> in{1,2,3,4,2};in.insert(in.end(),meta.begin(),meta.end());in.insert(in.end(),{5,6,7,8,0,9});
std::optional<std::string> title;
for(auto c:in){auto t=icy.feed(&c,1,audio);if(t)title=t;} // byte at a time: split blocks
expect(title && *title=="A - B C" && (audio==std::vector<uint8_t>{1,2,3,4,5,6,7,8,9}));
// Live streams send a zero length byte between titles (Gitea #5: the old demuxer then read the next
// length byte as audio and leaked metadata into the MP3 every metaint bytes).
IcyDemux z(2);std::vector<uint8_t> za;
std::vector<uint8_t> zin{1,2,0,3,4,0,5,6,1};std::string m1="StreamTitle='X';";m1.resize(16,'\0');zin.insert(zin.end(),m1.begin(),m1.end());zin.insert(zin.end(),{7,8,0,9});
std::optional<std::string> zt;for(std::size_t i=0;i<zin.size();i+=3){auto t=z.feed(zin.data()+i,std::min<std::size_t>(3,zin.size()-i),za);if(t)zt=t;}
expect(zt && *zt=="X" && (za==std::vector<uint8_t>{1,2,3,4,5,6,7,8,9}));
expect(icy_stream_title("StreamTitle='';")=="" && icy_stream_title("x")=="");
expect(radio_status(2,1,"A - B")=="T;2;1;A - B");
std::puts("OctoRadio stations, commands, MP3 frames and ICY titles pass.");
}