splitted message files

git-svn-id: svn://elaine/murooma/trunk@236 d8a302eb-03bc-478d-80e4-98257eca68ef
This commit is contained in:
(no author) 2014-09-06 09:50:42 +00:00
parent 18f3c5178f
commit 344892b8f1
15 changed files with 939 additions and 159 deletions

View file

@ -3,7 +3,7 @@ CC = /usr/bin/g++
CFLAGS = -std=gnu++0x -Wall -Wno-unused-function -g -D_REENTRANT -DVERSION=\"$(VERSION)\" -I..
LDFLAGS = -lrt -lpthread -lboost_system -lboost_program_options -lasound -logg -lvorbis -lvorbisenc
OBJ = snapClient.o stream.o player.o serverConnection.o oggDecoder.o pcmDecoder.o ../common/message.o ../common/log.o ../common/sampleFormat.o
OBJ = snapClient.o stream.o player.o serverConnection.o oggDecoder.o pcmDecoder.o ../common/pcmChunk.o ../common/log.o ../common/sampleFormat.o
BIN = snapclient
all: client

View file

@ -1,6 +1,7 @@
#ifndef DECODER_H
#define DECODER_H
#include "common/message.h"
#include "common/pcmChunk.h"
#include "common/headerMessage.h"
class Decoder
{

View file

@ -9,6 +9,7 @@
#include <memory>
#include "doubleBuffer.h"
#include "common/message.h"
#include "common/pcmChunk.h"
#include "common/timeUtils.h"
#include "common/queue.h"
#include "common/sampleFormat.h"

48
common/headerMessage.h Normal file
View file

@ -0,0 +1,48 @@
#ifndef HEADER_MESSAGE_H
#define HEADER_MESSAGE_H
#include "common/message.h"
class HeaderMessage : public BaseMessage
{
public:
HeaderMessage(size_t size = 0) : BaseMessage(message_type::header), payloadSize(size)
{
payload = (char*)malloc(size);
}
virtual ~HeaderMessage()
{
free(payload);
}
virtual void read(std::istream& stream)
{
stream.read(reinterpret_cast<char *>(&payloadSize), sizeof(uint32_t));
payload = (char*)realloc(payload, payloadSize);
stream.read(payload, payloadSize);
}
virtual uint32_t getSize()
{
return sizeof(uint32_t) + payloadSize;
}
uint32_t payloadSize;
char* payload;
protected:
virtual void doserialize(std::ostream& stream)
{
stream.write(reinterpret_cast<char *>(&payloadSize), sizeof(uint32_t));
stream.write(payload, payloadSize);
}
};
#endif

View file

@ -1,18 +1,12 @@
#ifndef CHUNK_H
#define CHUNK_H
#ifndef MESSAGE_H
#define MESSAGE_H
#include <chrono>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <streambuf>
#include <vector>
#include "common/sampleFormat.h"
typedef std::chrono::time_point<std::chrono::high_resolution_clock, std::chrono::milliseconds> time_point_ms;
using namespace std;
template<typename CharT, typename TraitsT = std::char_traits<CharT> >
class vectorwrapbuf : public std::basic_streambuf<CharT, TraitsT> {
@ -47,7 +41,7 @@ struct BaseMessage
{
}
virtual void read(istream& stream)
virtual void read(std::istream& stream)
{
stream.read(reinterpret_cast<char*>(&type), sizeof(uint16_t));
stream.read(reinterpret_cast<char*>(&size), sizeof(uint32_t));
@ -60,7 +54,7 @@ struct BaseMessage
read(is);
}
virtual void serialize(ostream& stream)
virtual void serialize(std::ostream& stream)
{
stream.write(reinterpret_cast<char*>(&type), sizeof(uint16_t));
size = getSize();
@ -76,152 +70,12 @@ struct BaseMessage
uint16_t type;
uint32_t size;
protected:
virtual void doserialize(ostream& stream)
virtual void doserialize(std::ostream& stream)
{
};
};
class WireChunk : public BaseMessage
{
public:
WireChunk(size_t size = 0) : BaseMessage(message_type::payload), payloadSize(size)
{
payload = (char*)malloc(size);
}
virtual ~WireChunk()
{
free(payload);
}
virtual void read(istream& stream)
{
stream.read(reinterpret_cast<char *>(&tv_sec), sizeof(int32_t));
stream.read(reinterpret_cast<char *>(&tv_usec), sizeof(int32_t));
stream.read(reinterpret_cast<char *>(&payloadSize), sizeof(uint32_t));
payload = (char*)realloc(payload, payloadSize);
stream.read(payload, payloadSize);
}
virtual uint32_t getSize()
{
return sizeof(int32_t) + sizeof(int32_t) + sizeof(uint32_t) + payloadSize;
}
int32_t tv_sec;
int32_t tv_usec;
uint32_t payloadSize;
char* payload;
protected:
virtual void doserialize(ostream& stream)
{
stream.write(reinterpret_cast<char *>(&tv_sec), sizeof(int32_t));
stream.write(reinterpret_cast<char *>(&tv_usec), sizeof(int32_t));
stream.write(reinterpret_cast<char *>(&payloadSize), sizeof(uint32_t));
stream.write(payload, payloadSize);
}
};
class HeaderMessage : public BaseMessage
{
public:
HeaderMessage(size_t size = 0) : BaseMessage(message_type::header), payloadSize(size)
{
payload = (char*)malloc(size);
}
virtual ~HeaderMessage()
{
free(payload);
}
virtual void read(istream& stream)
{
stream.read(reinterpret_cast<char *>(&payloadSize), sizeof(uint32_t));
payload = (char*)realloc(payload, payloadSize);
stream.read(payload, payloadSize);
}
virtual uint32_t getSize()
{
return sizeof(uint32_t) + payloadSize;
}
uint32_t payloadSize;
char* payload;
protected:
virtual void doserialize(ostream& stream)
{
stream.write(reinterpret_cast<char *>(&payloadSize), sizeof(uint32_t));
stream.write(payload, payloadSize);
}
};
class PcmChunk : public WireChunk
{
public:
PcmChunk(const SampleFormat& sampleFormat, size_t ms);
~PcmChunk();
int readFrames(void* outputBuffer, size_t frameCount);
bool isEndOfChunk() const;
inline time_point_ms timePoint() const
{
time_point_ms tp;
std::chrono::milliseconds::rep relativeIdxTp = ((double)idx / ((double)format.rate/1000.));
return
tp +
std::chrono::seconds(tv_sec) +
std::chrono::milliseconds(tv_usec / 1000) +
std::chrono::milliseconds(relativeIdxTp);
}
template<typename T>
inline T getAge() const
{
return getAge<T>(timePoint());
}
inline long getAge() const
{
return getAge<std::chrono::milliseconds>().count();
}
inline static long getAge(const time_point_ms& time_point)
{
return getAge<std::chrono::milliseconds>(time_point).count();
}
template<typename T, typename U>
static inline T getAge(const std::chrono::time_point<U>& time_point)
{
return std::chrono::duration_cast<T>(std::chrono::high_resolution_clock::now() - time_point);
}
int seek(int frames);
double getDuration() const;
double getDurationUs() const;
double getTimeLeft() const;
double getFrameCount() const;
SampleFormat format;
private:
// SampleFormat format_;
uint32_t idx;
};
#endif

View file

@ -1,9 +1,12 @@
#include "message.h"
#include "pcmChunk.h"
#include <string.h>
#include <iostream>
#include "common/log.h"
using namespace std;
PcmChunk::PcmChunk(const SampleFormat& sampleFormat, size_t ms) : WireChunk(), format(sampleFormat), idx(0)
{
payloadSize = format.rate*format.frameSize*ms / 1000;

72
common/pcmChunk.h Normal file
View file

@ -0,0 +1,72 @@
#ifndef PCM_CHUNK_H
#define PCM_CHUNK_H
#include "message.h"
#include "wireChunk.h"
#include "sampleFormat.h"
typedef std::chrono::time_point<std::chrono::high_resolution_clock, std::chrono::milliseconds> time_point_ms;
class PcmChunk : public WireChunk
{
public:
PcmChunk(const SampleFormat& sampleFormat, size_t ms);
~PcmChunk();
int readFrames(void* outputBuffer, size_t frameCount);
bool isEndOfChunk() const;
inline time_point_ms timePoint() const
{
time_point_ms tp;
std::chrono::milliseconds::rep relativeIdxTp = ((double)idx / ((double)format.rate/1000.));
return
tp +
std::chrono::seconds(tv_sec) +
std::chrono::milliseconds(tv_usec / 1000) +
std::chrono::milliseconds(relativeIdxTp);
}
template<typename T>
inline T getAge() const
{
return getAge<T>(timePoint());
}
inline long getAge() const
{
return getAge<std::chrono::milliseconds>().count();
}
inline static long getAge(const time_point_ms& time_point)
{
return getAge<std::chrono::milliseconds>(time_point).count();
}
template<typename T, typename U>
static inline T getAge(const std::chrono::time_point<U>& time_point)
{
return std::chrono::duration_cast<T>(std::chrono::high_resolution_clock::now() - time_point);
}
int seek(int frames);
double getDuration() const;
double getDurationUs() const;
double getTimeLeft() const;
double getFrameCount() const;
SampleFormat format;
private:
// SampleFormat format_;
uint32_t idx;
};
#endif

View file

@ -5,13 +5,18 @@
#include <iostream>
SampleFormat::SampleFormat(const std::string& format) //: rate(rate_), bits(bits_), channels(channels_), sampleSize(bytes_), frameSize(frameSize_)
SampleFormat::SampleFormat() : BaseMessage(message_type::payload)
{
}
SampleFormat::SampleFormat(const std::string& format) : BaseMessage(message_type::payload)
{
setFormat(format);
}
SampleFormat::SampleFormat(uint16_t sampleRate, uint16_t bitsPerSample, uint16_t channelCount) //: rate(rate_), bits(bits_), channels(channels_), sampleSize(bytes_), frameSize(frameSize_)
SampleFormat::SampleFormat(uint16_t sampleRate, uint16_t bitsPerSample, uint16_t channelCount) : BaseMessage(message_type::payload)
{
setFormat(sampleRate, bitsPerSample, channelCount);
}

View file

@ -2,9 +2,10 @@
#define SAMPLE_FORMAT_H
#include <string>
#include "message.h"
class SampleFormat
class SampleFormat : public BaseMessage
{
public:
SampleFormat();
@ -23,6 +24,30 @@ public:
float msRate() const { return (float)rate/1000.f; }
virtual void read(std::istream& stream)
{
stream.read(reinterpret_cast<char *>(&rate), sizeof(uint16_t));
stream.read(reinterpret_cast<char *>(&bits), sizeof(uint16_t));
stream.read(reinterpret_cast<char *>(&channels), sizeof(uint16_t));
stream.read(reinterpret_cast<char *>(&sampleSize), sizeof(uint16_t));
stream.read(reinterpret_cast<char *>(&frameSize), sizeof(uint16_t));
}
virtual uint32_t getSize()
{
return 5*sizeof(int16_t);
}
protected:
virtual void doserialize(std::ostream& stream)
{
stream.write(reinterpret_cast<char *>(&rate), sizeof(uint16_t));
stream.write(reinterpret_cast<char *>(&bits), sizeof(uint16_t));
stream.write(reinterpret_cast<char *>(&channels), sizeof(uint16_t));
stream.write(reinterpret_cast<char *>(&sampleSize), sizeof(uint16_t));
stream.write(reinterpret_cast<char *>(&frameSize), sizeof(uint16_t));
}
/*private:
uint16_t rate_;
uint16_t bits_;

61
common/wireChunk.h Normal file
View file

@ -0,0 +1,61 @@
#ifndef WIRE_CHUNK_H
#define WIRE_CHUNK_H
#include <chrono>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <streambuf>
#include <vector>
#include "message.h"
class WireChunk : public BaseMessage
{
public:
WireChunk(size_t size = 0) : BaseMessage(message_type::payload), payloadSize(size)
{
payload = (char*)malloc(size);
}
virtual ~WireChunk()
{
free(payload);
}
virtual void read(std::istream& stream)
{
stream.read(reinterpret_cast<char *>(&tv_sec), sizeof(int32_t));
stream.read(reinterpret_cast<char *>(&tv_usec), sizeof(int32_t));
stream.read(reinterpret_cast<char *>(&payloadSize), sizeof(uint32_t));
payload = (char*)realloc(payload, payloadSize);
stream.read(payload, payloadSize);
}
virtual uint32_t getSize()
{
return sizeof(int32_t) + sizeof(int32_t) + sizeof(uint32_t) + payloadSize;
}
int32_t tv_sec;
int32_t tv_usec;
uint32_t payloadSize;
char* payload;
protected:
virtual void doserialize(std::ostream& stream)
{
stream.write(reinterpret_cast<char *>(&tv_sec), sizeof(int32_t));
stream.write(reinterpret_cast<char *>(&tv_usec), sizeof(int32_t));
stream.write(reinterpret_cast<char *>(&payloadSize), sizeof(uint32_t));
stream.write(payload, payloadSize);
}
};
#endif

314
decoder_example.c Normal file
View file

@ -0,0 +1,314 @@
/********************************************************************
* *
* THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. *
* USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS *
* GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
* IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. *
* *
* THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2009 *
* by the Xiph.Org Foundation http://www.xiph.org/ *
* *
********************************************************************
function: simple example decoder
last mod: $Id$
********************************************************************/
/* Takes a vorbis bitstream from stdin and writes raw stereo PCM to
stdout. Decodes simple and chained OggVorbis files from beginning
to end. Vorbisfile.a is somewhat more complex than the code below. */
/* Note that this is POSIX, not ANSI code */
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <vorbis/codec.h>
#ifdef _WIN32 /* We need the following two to set stdin/stdout to binary */
#include <io.h>
#include <fcntl.h>
#endif
#if defined(__MACOS__) && defined(__MWERKS__)
#include <console.h> /* CodeWarrior's Mac "command-line" support */
#endif
ogg_int16_t convbuffer[4096]; /* take 8k out of the data segment, not the stack */
int convsize=4096;
extern void _VDBG_dump(void);
int main(){
ogg_sync_state oy; /* sync and verify incoming physical bitstream */
ogg_stream_state os; /* take physical pages, weld into a logical
stream of packets */
ogg_page og; /* one Ogg bitstream page. Vorbis packets are inside */
ogg_packet op; /* one raw packet of data for decode */
vorbis_info vi; /* struct that stores all the static vorbis bitstream
settings */
vorbis_comment vc; /* struct that stores all the bitstream user comments */
vorbis_dsp_state vd; /* central working state for the packet->PCM decoder */
vorbis_block vb; /* local working space for packet->PCM decode */
char *buffer;
int bytes;
#ifdef _WIN32 /* We need to set stdin/stdout to binary mode. Damn windows. */
/* Beware the evil ifdef. We avoid these where we can, but this one we
cannot. Don't add any more, you'll probably go to hell if you do. */
_setmode( _fileno( stdin ), _O_BINARY );
_setmode( _fileno( stdout ), _O_BINARY );
#endif
#if defined(macintosh) && defined(__MWERKS__)
{
int argc;
char **argv;
argc=ccommand(&argv); /* get a "command line" from the Mac user */
/* this also lets the user set stdin and stdout */
}
#endif
/********** Decode setup ************/
ogg_sync_init(&oy); /* Now we can read pages */
while(1){ /* we repeat if the bitstream is chained */
int eos=0;
int i;
/* grab some data at the head of the stream. We want the first page
(which is guaranteed to be small and only contain the Vorbis
stream initial header) We need the first page to get the stream
serialno. */
/* submit a 4k block to libvorbis' Ogg layer */
buffer=ogg_sync_buffer(&oy,4096);
bytes=fread(buffer,1,4096,stdin);
ogg_sync_wrote(&oy,bytes);
/* Get the first page. */
if(ogg_sync_pageout(&oy,&og)!=1){
/* have we simply run out of data? If so, we're done. */
if(bytes<4096)break;
/* error case. Must not be Vorbis data */
fprintf(stderr,"Input does not appear to be an Ogg bitstream.\n");
exit(1);
}
/* Get the serial number and set up the rest of decode. */
/* serialno first; use it to set up a logical stream */
ogg_stream_init(&os,ogg_page_serialno(&og));
/* extract the initial header from the first page and verify that the
Ogg bitstream is in fact Vorbis data */
/* I handle the initial header first instead of just having the code
read all three Vorbis headers at once because reading the initial
header is an easy way to identify a Vorbis bitstream and it's
useful to see that functionality seperated out. */
vorbis_info_init(&vi);
vorbis_comment_init(&vc);
if(ogg_stream_pagein(&os,&og)<0){
/* error; stream version mismatch perhaps */
fprintf(stderr,"Error reading first page of Ogg bitstream data.\n");
exit(1);
}
if(ogg_stream_packetout(&os,&op)!=1){
/* no page? must not be vorbis */
fprintf(stderr,"Error reading initial header packet.\n");
exit(1);
}
if(vorbis_synthesis_headerin(&vi,&vc,&op)<0){
/* error case; not a vorbis header */
fprintf(stderr,"This Ogg bitstream does not contain Vorbis "
"audio data.\n");
exit(1);
}
/* At this point, we're sure we're Vorbis. We've set up the logical
(Ogg) bitstream decoder. Get the comment and codebook headers and
set up the Vorbis decoder */
/* The next two packets in order are the comment and codebook headers.
They're likely large and may span multiple pages. Thus we read
and submit data until we get our two packets, watching that no
pages are missing. If a page is missing, error out; losing a
header page is the only place where missing data is fatal. */
i=0;
while(i<2){
while(i<2){
int result=ogg_sync_pageout(&oy,&og);
if(result==0)break; /* Need more data */
/* Don't complain about missing or corrupt data yet. We'll
catch it at the packet output phase */
if(result==1){
ogg_stream_pagein(&os,&og); /* we can ignore any errors here
as they'll also become apparent
at packetout */
while(i<2){
result=ogg_stream_packetout(&os,&op);
if(result==0)break;
if(result<0){
/* Uh oh; data at some point was corrupted or missing!
We can't tolerate that in a header. Die. */
fprintf(stderr,"Corrupt secondary header. Exiting.\n");
exit(1);
}
result=vorbis_synthesis_headerin(&vi,&vc,&op);
if(result<0){
fprintf(stderr,"Corrupt secondary header. Exiting.\n");
exit(1);
}
i++;
}
}
}
/* no harm in not checking before adding more */
buffer=ogg_sync_buffer(&oy,4096);
bytes=fread(buffer,1,4096,stdin);
if(bytes==0 && i<2){
fprintf(stderr,"End of file before finding all Vorbis headers!\n");
exit(1);
}
ogg_sync_wrote(&oy,bytes);
}
/* Throw the comments plus a few lines about the bitstream we're
decoding */
{
char **ptr=vc.user_comments;
while(*ptr){
fprintf(stderr,"%s\n",*ptr);
++ptr;
}
fprintf(stderr,"\nBitstream is %d channel, %ldHz\n",vi.channels,vi.rate);
fprintf(stderr,"Encoded by: %s\n\n",vc.vendor);
}
convsize=4096/vi.channels;
/* OK, got and parsed all three headers. Initialize the Vorbis
packet->PCM decoder. */
if(vorbis_synthesis_init(&vd,&vi)==0){ /* central decode state */
vorbis_block_init(&vd,&vb); /* local state for most of the decode
so multiple block decodes can
proceed in parallel. We could init
multiple vorbis_block structures
for vd here */
/* The rest is just a straight decode loop until end of stream */
while(!eos){
while(!eos){
int result=ogg_sync_pageout(&oy,&og);
if(result==0)break; /* need more data */
if(result<0){ /* missing or corrupt data at this page position */
fprintf(stderr,"Corrupt or missing data in bitstream; "
"continuing...\n");
}else{
ogg_stream_pagein(&os,&og); /* can safely ignore errors at
this point */
while(1){
result=ogg_stream_packetout(&os,&op);
if(result==0)break; /* need more data */
if(result<0){ /* missing or corrupt data at this page position */
/* no reason to complain; already complained above */
}else{
/* we have a packet. Decode it */
float **pcm;
int samples;
if(vorbis_synthesis(&vb,&op)==0) /* test for success! */
vorbis_synthesis_blockin(&vd,&vb);
/*
**pcm is a multichannel float vector. In stereo, for
example, pcm[0] is left, and pcm[1] is right. samples is
the size of each channel. Convert the float values
(-1.<=range<=1.) to whatever PCM format and write it out */
while((samples=vorbis_synthesis_pcmout(&vd,&pcm))>0){
int j;
int clipflag=0;
int bout=(samples<convsize?samples:convsize);
/* convert floats to 16 bit signed ints (host order) and
interleave */
for(i=0;i<vi.channels;i++){
ogg_int16_t *ptr=convbuffer+i;
float *mono=pcm[i];
for(j=0;j<bout;j++){
#if 1
int val=floor(mono[j]*32767.f+.5f);
#else /* optional dither */
int val=mono[j]*32767.f+drand48()-0.5f;
#endif
/* might as well guard against clipping */
if(val>32767){
val=32767;
clipflag=1;
}
if(val<-32768){
val=-32768;
clipflag=1;
}
*ptr=val;
ptr+=vi.channels;
}
}
if(clipflag)
fprintf(stderr,"Clipping in frame %ld\n",(long)(vd.sequence));
fwrite(convbuffer,2*vi.channels,bout,stdout);
vorbis_synthesis_read(&vd,bout); /* tell libvorbis how
many samples we
actually consumed */
}
}
}
if(ogg_page_eos(&og))eos=1;
}
}
if(!eos){
buffer=ogg_sync_buffer(&oy,4096);
bytes=fread(buffer,1,4096,stdin);
ogg_sync_wrote(&oy,bytes);
if(bytes==0)eos=1;
}
}
/* ogg_page and ogg_packet structs always point to storage in
libvorbis. They're never freed or manipulated directly */
vorbis_block_clear(&vb);
vorbis_dsp_clear(&vd);
}else{
fprintf(stderr,"Error: Corrupt header during playback initialization.\n");
}
/* clean up this logical bitstream; before exit we see if we're
followed by another [chained] */
ogg_stream_clear(&os);
vorbis_comment_clear(&vc);
vorbis_info_clear(&vi); /* must be called last */
}
/* OK, clean up the framer */
ogg_sync_clear(&oy);
fprintf(stderr,"Done.\n");
return(0);
}

252
encoder_example.cpp Normal file
View file

@ -0,0 +1,252 @@
/********************************************************************
* *
* THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. *
* USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS *
* GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
* IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. *
* *
* THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2007 *
* by the Xiph.Org Foundation http://www.xiph.org/ *
* *
********************************************************************
function: simple example encoder
last mod: $Id$
********************************************************************/
/* takes a stereo 16bit 44.1kHz WAV file from stdin and encodes it into
a Vorbis bitstream */
/* Note that this is POSIX, not ANSI, code */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <math.h>
#include <vorbis/vorbisenc.h>
#ifdef _WIN32 /* We need the following two to set stdin/stdout to binary */
#include <io.h>
#include <fcntl.h>
#endif
#if defined(__MACOS__) && defined(__MWERKS__)
#include <console.h> /* CodeWarrior's Mac "command-line" support */
#endif
#define READ 1024
signed char readbuffer[READ*4+44]; /* out of the data segment, not the stack */
int main(){
ogg_stream_state os; /* take physical pages, weld into a logical
stream of packets */
ogg_page og; /* one Ogg bitstream page. Vorbis packets are inside */
ogg_packet op; /* one raw packet of data for decode */
vorbis_info vi; /* struct that stores all the static vorbis bitstream
settings */
vorbis_comment vc; /* struct that stores all the user comments */
vorbis_dsp_state vd; /* central working state for the packet->PCM decoder */
vorbis_block vb; /* local working space for packet->PCM decode */
int eos=0,ret;
int i, founddata;
#if defined(macintosh) && defined(__MWERKS__)
int argc = 0;
char **argv = NULL;
argc = ccommand(&argv); /* get a "command line" from the Mac user */
/* this also lets the user set stdin and stdout */
#endif
/* we cheat on the WAV header; we just bypass 44 bytes (simplest WAV
header is 44 bytes) and assume that the data is 44.1khz, stereo, 16 bit
little endian pcm samples. This is just an example, after all. */
#ifdef _WIN32 /* We need to set stdin/stdout to binary mode. Damn windows. */
/* if we were reading/writing a file, it would also need to in
binary mode, eg, fopen("file.wav","wb"); */
/* Beware the evil ifdef. We avoid these where we can, but this one we
cannot. Don't add any more, you'll probably go to hell if you do. */
_setmode( _fileno( stdin ), _O_BINARY );
_setmode( _fileno( stdout ), _O_BINARY );
#endif
/* we cheat on the WAV header; we just bypass the header and never
verify that it matches 16bit/stereo/44.1kHz. This is just an
example, after all. */
readbuffer[0] = '\0';
for (i=0, founddata=0; i<30 && ! feof(stdin) && ! ferror(stdin); i++)
{
fread(readbuffer,1,2,stdin);
if ( ! strncmp((char*)readbuffer, "da", 2) ){
founddata = 1;
fread(readbuffer,1,6,stdin);
break;
}
}
/********** Encode setup ************/
vorbis_info_init(&vi);
/* choose an encoding mode. A few possibilities commented out, one
actually used: */
/*********************************************************************
Encoding using a VBR quality mode. The usable range is -.1
(lowest quality, smallest file) to 1. (highest quality, largest file).
Example quality mode .4: 44kHz stereo coupled, roughly 128kbps VBR
ret = vorbis_encode_init_vbr(&vi,2,44100,.4);
---------------------------------------------------------------------
Encoding using an average bitrate mode (ABR).
example: 44kHz stereo coupled, average 128kbps VBR
ret = vorbis_encode_init(&vi,2,44100,-1,128000,-1);
---------------------------------------------------------------------
Encode using a quality mode, but select that quality mode by asking for
an approximate bitrate. This is not ABR, it is true VBR, but selected
using the bitrate interface, and then turning bitrate management off:
ret = ( vorbis_encode_setup_managed(&vi,2,44100,-1,128000,-1) ||
vorbis_encode_ctl(&vi,OV_ECTL_RATEMANAGE2_SET,NULL) ||
vorbis_encode_setup_init(&vi));
*********************************************************************/
ret=vorbis_encode_init_vbr(&vi,2,44100,0.1);
/* do not continue if setup failed; this can happen if we ask for a
mode that libVorbis does not support (eg, too low a bitrate, etc,
will return 'OV_EIMPL') */
if(ret)exit(1);
/* add a comment */
vorbis_comment_init(&vc);
vorbis_comment_add_tag(&vc,"ENCODER","encoder_example.c");
/* set up the analysis state and auxiliary encoding storage */
vorbis_analysis_init(&vd,&vi);
vorbis_block_init(&vd,&vb);
/* set up our packet->stream encoder */
/* pick a random serial number; that way we can more likely build
chained streams just by concatenation */
srand(time(NULL));
ogg_stream_init(&os,rand());
/* Vorbis streams begin with three headers; the initial header (with
most of the codec setup parameters) which is mandated by the Ogg
bitstream spec. The second header holds any comment fields. The
third header holds the bitstream codebook. We merely need to
make the headers, then pass them to libvorbis one at a time;
libvorbis handles the additional Ogg bitstream constraints */
{
ogg_packet header;
ogg_packet header_comm;
ogg_packet header_code;
vorbis_analysis_headerout(&vd,&vc,&header,&header_comm,&header_code);
ogg_stream_packetin(&os,&header); /* automatically placed in its own
page */
ogg_stream_packetin(&os,&header_comm);
ogg_stream_packetin(&os,&header_code);
/* This ensures the actual
* audio data will start on a new page, as per spec
*/
while(!eos){
int result=ogg_stream_flush(&os,&og);
if(result==0)break;
fwrite(og.header,1,og.header_len,stdout);
fwrite(og.body,1,og.body_len,stdout);
}
}
while(!eos){
long i;
long bytes=fread(readbuffer,1,READ*4,stdin); /* stereo hardwired here */
if(bytes==0){
/* end of file. this can be done implicitly in the mainline,
but it's easier to see here in non-clever fashion.
Tell the library we're at end of stream so that it can handle
the last frame and mark end of stream in the output properly */
vorbis_analysis_wrote(&vd,0);
}else{
/* data to encode */
/* expose the buffer to submit data */
float **buffer=vorbis_analysis_buffer(&vd,READ);
/* uninterleave samples */
for(i=0;i<bytes/4;i++){
buffer[0][i]=((readbuffer[i*4+1]<<8)|
(0x00ff&(int)readbuffer[i*4]))/32768.f;
buffer[1][i]=((readbuffer[i*4+3]<<8)|
(0x00ff&(int)readbuffer[i*4+2]))/32768.f;
}
/* tell the library how much we actually submitted */
vorbis_analysis_wrote(&vd,i);
}
/* vorbis does some data preanalysis, then divvies up blocks for
more involved (potentially parallel) processing. Get a single
block for encoding now */
while(vorbis_analysis_blockout(&vd,&vb)==1){
/* analysis, assume we want to use bitrate management */
vorbis_analysis(&vb,NULL);
vorbis_bitrate_addblock(&vb);
while(vorbis_bitrate_flushpacket(&vd,&op)){
/* weld the packet into the bitstream */
ogg_stream_packetin(&os,&op);
/* write out pages (if any) */
while(!eos){
int result=ogg_stream_pageout(&os,&og);
if(result==0)break;
fwrite(og.header,1,og.header_len,stdout);
fwrite(og.body,1,og.body_len,stdout);
/* this could be set above, but for illustrative purposes, I do
it here (to show that vorbis does know where the stream ends) */
if(ogg_page_eos(&og))eos=1;
}
}
}
}
/* clean up and exit. vorbis_info_clear() must be called last */
ogg_stream_clear(&os);
vorbis_block_clear(&vb);
vorbis_dsp_clear(&vd);
vorbis_comment_clear(&vc);
vorbis_info_clear(&vi);
/* ogg_page and ogg_packet structs always point to storage in
libvorbis. They're never freed or manipulated directly */
fprintf(stderr,"Done.\n");
return(0);
}

143
play.cpp Normal file
View file

@ -0,0 +1,143 @@
/*
* Simple sound playback using ALSA API and libasound.
*
* Compile:
* $ cc -o play sound_playback.c -lasound
*
* Usage:
* $ ./play <sample_rate> <channels> <seconds> < <file>
*
* Examples:
* $ ./play 44100 2 5 < /dev/urandom
* $ ./play 22050 1 8 < /path/to/file.wav
*
* Copyright (C) 2009 Alessandro Ghedini <alessandro@ghedini.me>
* --------------------------------------------------------------
* "THE BEER-WARE LICENSE" (Revision 42):
* Alessandro Ghedini wrote this file. As long as you retain this
* notice you can do whatever you want with this stuff. If we
* meet some day, and you think this stuff is worth it, you can
* buy me a beer in return.
* --------------------------------------------------------------
*/
#include <alsa/asoundlib.h>
#include <stdio.h>
#include <iostream>
using namespace std;
#define PCM_DEVICE "default"
int main(int argc, char **argv) {
unsigned int pcm, tmp, dir, rate;
int channels, seconds;
snd_pcm_t *pcm_handle;
snd_pcm_hw_params_t *params;
snd_pcm_uframes_t frames;
char *buff;
int buff_size, loops;
if (argc < 4) {
printf("Usage: %s <sample_rate> <channels> <seconds>\n",
argv[0]);
return -1;
}
rate = atoi(argv[1]);
channels = atoi(argv[2]);
seconds = atoi(argv[3]);
/* Open the PCM device in playback mode */
if (pcm = snd_pcm_open(&pcm_handle, PCM_DEVICE,
SND_PCM_STREAM_PLAYBACK, 0) < 0)
printf("ERROR: Can't open \"%s\" PCM device. %s\n",
PCM_DEVICE, snd_strerror(pcm));
/* Allocate parameters object and fill it with default values*/
snd_pcm_hw_params_alloca(&params);
snd_pcm_hw_params_any(pcm_handle, params);
/* Set parameters */
if (pcm = snd_pcm_hw_params_set_access(pcm_handle, params,
SND_PCM_ACCESS_RW_INTERLEAVED) < 0)
printf("ERROR: Can't set interleaved mode. %s\n", snd_strerror(pcm));
if (pcm = snd_pcm_hw_params_set_format(pcm_handle, params,
SND_PCM_FORMAT_S16_LE) < 0)
printf("ERROR: Can't set format. %s\n", snd_strerror(pcm));
if (pcm = snd_pcm_hw_params_set_channels(pcm_handle, params, channels) < 0)
printf("ERROR: Can't set channels number. %s\n", snd_strerror(pcm));
if (pcm = snd_pcm_hw_params_set_rate_near(pcm_handle, params, &rate, 0) < 0)
printf("ERROR: Can't set rate. %s\n", snd_strerror(pcm));
long unsigned int periodsize = 2*480;
if (pcm = snd_pcm_hw_params_set_buffer_size_near(pcm_handle, params, &periodsize) < 0)
printf("Unable to set buffer size %li: %s\n", (long int)periodsize, snd_strerror(pcm));
/* Write parameters */
if (pcm = snd_pcm_hw_params(pcm_handle, params) < 0)
printf("ERROR: Can't set harware parameters. %s\n", snd_strerror(pcm));
/* Resume information */
printf("PCM name: '%s'\n", snd_pcm_name(pcm_handle));
printf("PCM state: %s\n", snd_pcm_state_name(snd_pcm_state(pcm_handle)));
snd_pcm_hw_params_get_channels(params, &tmp);
printf("channels: %i ", tmp);
if (tmp == 1)
printf("(mono)\n");
else if (tmp == 2)
printf("(stereo)\n");
snd_pcm_hw_params_get_rate(params, &tmp, 0);
printf("rate: %d bps\n", tmp);
printf("seconds: %d\n", seconds);
/* Allocate buffer to hold single period */
snd_pcm_hw_params_get_period_size(params, &frames, 0);
printf("frames: %d\n", frames);
buff_size = frames * channels * 2 /* 2 -> sample size */;
buff = (char *) malloc(buff_size);
snd_pcm_hw_params_get_period_time(params, &tmp, NULL);
printf("period time: %d\n", tmp);
for (loops = (seconds * 1000000) / tmp; loops > 0; loops--) {
if (pcm = read(0, buff, buff_size) == 0) {
printf("Early end of file.\n");
return 0;
}
//usleep(10000);
if (pcm = snd_pcm_writei(pcm_handle, buff, frames) == -EPIPE) {
printf("XRUN.\n");
snd_pcm_prepare(pcm_handle);
} else if (pcm < 0) {
printf("ERROR. Can't write to PCM device. %s\n", snd_strerror(pcm));
}
snd_pcm_sframes_t avail;
snd_pcm_sframes_t delay;
snd_pcm_avail_delay(pcm_handle, &avail, &delay);
cout << "avail: " << avail << "\t" << delay / 48 << "\n";
cout.flush();
}
cout << "end\n";
cout.flush();
snd_pcm_drain(pcm_handle);
snd_pcm_close(pcm_handle);
free(buff);
return 0;
}

View file

@ -3,7 +3,7 @@ CC = /usr/bin/g++
CFLAGS = -std=gnu++0x -Wall -Wno-unused-function -D_REENTRANT -DVERSION=\"$(VERSION)\" -I..
LDFLAGS = -lrt -lpthread -lboost_system -lboost_program_options -lvorbis -lvorbisenc -logg
OBJ = snapServer.o pcmEncoder.o oggEncoder.o ../common/message.o ../common/sampleFormat.o
OBJ = snapServer.o pcmEncoder.o oggEncoder.o ../common/pcmChunk.o ../common/sampleFormat.o
BIN = snapserver
all: server

View file

@ -1,6 +1,7 @@
#ifndef ENCODER_H
#define ENCODER_H
#include "common/message.h"
#include "common/pcmChunk.h"
#include "common/headerMessage.h"
#include "common/sampleFormat.h"