Commit a964bf24 authored by Simon Morlat's avatar Simon Morlat

drop windows-only file player and file recorder implementations.

Use posix implementation instead.
parent 63aef125
......@@ -335,11 +335,11 @@
</FileConfiguration>
</File>
<File
RelativePath="..\..\src\audiofilters\msfileplayer_win.c"
RelativePath="..\..\src\audiofilters\msfileplayer.c"
>
</File>
<File
RelativePath="..\..\src\audiofilters\msfilerec_win.c"
RelativePath="..\..\src\audiofilters\msfilerec.c"
>
</File>
<File
......
......@@ -20,7 +20,7 @@ ANDROID_SRC_FILES= \
EXTRA_DIST= audiofilters/winsnd2.c audiofilters/winsnd.c videofilters/winvideo.c \
videofilters/winvideods.c videofilters/wincevideods.c dxfilter.h dxfilter.cpp \
audiofilters/msfileplayer_win.c audiofilters/msfilerec_win.c audiofilters/winsndds.cpp \
audiofilters/winsndds.cpp \
voip/nowebcamCIF.jpg audiofilters/winsnd3.c utils/vfw-missing.h \
videofilters/winvideo2.c utils/msjava.c $(ANDROID_SRC_FILES) \
$(GITVERSION_FILE) yuv2rgb.fs yuv2rgb.vs
......@@ -99,7 +99,10 @@ libmediastreamer_voip_la_SOURCES+= audiofilters/alaw.c \
utils/g722_encode.c \
audiofilters/msg722.c \
audiofilters/l16.c \
audiofilters/genericplc.c
audiofilters/genericplc.c \
audiofilters/msfileplayer.c \
audiofilters/msfilerec.c \
audiofilters/waveheader.h
if BUILD_SPEEX
libmediastreamer_voip_la_SOURCES+= audiofilters/msspeex.c audiofilters/speexec.c
......@@ -114,13 +117,7 @@ libmediastreamer_voip_la_SOURCES+= audiofilters/g726.c
endif
if BUILD_WIN32
libmediastreamer_voip_la_SOURCES+= audiofilters/winsnd3.c \
audiofilters/msfileplayer_win.c \
audiofilters/msfilerec_win.c
else
libmediastreamer_voip_la_SOURCES+= audiofilters/msfileplayer.c \
audiofilters/msfilerec.c \
audiofilters/waveheader.h
libmediastreamer_voip_la_SOURCES+= audiofilters/winsnd3.c
endif
if BUILD_RESAMPLE
......
......@@ -29,6 +29,11 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include <pcap/pcap.h>
#endif
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
static int player_close(MSFilter *f, void *arg);
......@@ -175,7 +180,7 @@ static int player_open(MSFilter *f, void *arg){
if (d->fd>=0){
player_close(f,NULL);
}
if ((fd=open(file,O_RDONLY))==-1){
if ((fd=open(file,O_RDONLY|O_BINARY))==-1){
ms_warning("Failed to open %s",file);
return -1;
}
......
/*
mediastreamer2 library - modular sound and video processing and streaming
Copyright (C) 2006 Simon MORLAT (simon.morlat@linphone.org)
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#define UNICODE
#include "mediastreamer2/msfileplayer.h"
#include "mediastreamer2/waveheader.h"
#include "mediastreamer2/msticker.h"
typedef enum {
CLOSED,
STARTED,
STOPPED
} PlayerState;
struct _PlayerData{
HANDLE fd;
PlayerState state;
int rate;
int nchannels;
int hsize;
int loop_after;
int pause_time;
bool_t swap;
int stat;
int big_buffer; /* ouput less & bigger buffer. (default => no change) */
};
typedef struct _PlayerData PlayerData;
static void player_init(MSFilter *f){
PlayerData *d=(PlayerData *)ms_new(PlayerData,1);
d->fd=INVALID_HANDLE_VALUE;
d->state=CLOSED;
d->swap=FALSE;
d->rate=8000;
d->nchannels=1;
d->hsize=0;
d->loop_after=-1;
d->pause_time=0;
d->stat=-1;
d->big_buffer=1;
f->data=d;
}
static int read_wav_header(PlayerData *d){
char header1[sizeof(riff_t)];
char header2[sizeof(format_t)];
char header3[sizeof(data_t)];
int count;
riff_t *riff_chunk=(riff_t*)header1;
format_t *format_chunk=(format_t*)header2;
data_t *data_chunk=(data_t*)header3;
unsigned long len=0;
BOOL res;
res = ReadFile(d->fd, header1, sizeof(header1), &len, NULL) ;
if (!res || len != sizeof(header1)){
ms_warning("Wrong wav header: cannot read file");
return -1;
}
if (0!=strncmp(riff_chunk->riff, "RIFF", 4) || 0!=strncmp(riff_chunk->wave, "WAVE", 4)){
ms_warning("Wrong wav header (not RIFF/WAV)");
return -1;
}
res = ReadFile(d->fd, header2, sizeof(header2), &len, NULL) ;
if (!res || len != sizeof(header2)){
ms_warning("Wrong wav header: cannot read file");
return -1;
}
d->rate=le_uint32(format_chunk->rate);
d->nchannels=le_uint16(format_chunk->channel);
if (format_chunk->len-0x10>0)
{
SetFilePointer(d->fd, (format_chunk->len-0x10), NULL, FILE_CURRENT);
}
d->hsize=sizeof(wave_header_t)-0x10+format_chunk->len;
res = ReadFile(d->fd, header3, sizeof(header3), &len, NULL) ;
if (!res || len != sizeof(header3)){
ms_warning("Wrong wav header: cannot read file");
return -1;
}
count=0;
while (strncmp(data_chunk->data, "data", 4)!=0 && count<30)
{
SetFilePointer(d->fd, data_chunk->len, NULL, FILE_CURRENT);
count++;
d->hsize=d->hsize+len+data_chunk->len;
res = ReadFile(d->fd, header3, sizeof(header3), &len, NULL) ;
if (!res || len != sizeof(header3)){
ms_warning("Wrong wav header: cannot read file");
return -1;
}
}
#ifdef WORDS_BIGENDIAN
d->swap=TRUE;
#endif
return 0;
}
static int player_open(MSFilter *f, void *arg){
PlayerData *d=(PlayerData*)f->data;
HANDLE fd;
const char *file=(const char*)arg;
WCHAR wUnicode[1024];
MultiByteToWideChar(CP_UTF8, 0, file, -1, wUnicode, 1024);
fd = CreateFile(wUnicode, GENERIC_READ, FILE_SHARE_READ, NULL,
OPEN_EXISTING, 0, NULL);
if (fd==INVALID_HANDLE_VALUE){
ms_warning("Failed to open %s",file);
return -1;
}
d->state=STOPPED;
d->fd=fd;
if (strstr(file,".wav")!=NULL) read_wav_header(d);
return 0;
}
static int player_close(MSFilter *f, void *arg){
PlayerData *d=(PlayerData*)f->data;
if (d->fd!=INVALID_HANDLE_VALUE) CloseHandle(d->fd);
d->fd=NULL;
d->state=CLOSED;
d->stat=-1;
return 0;
}
static int player_start(MSFilter *f, void *arg){
PlayerData *d=(PlayerData*)f->data;
if (d->state==STOPPED)
d->state=STARTED;
return 0;
}
static int player_stop(MSFilter *f, void *arg){
PlayerData *d=(PlayerData*)f->data;
if (d->state==STARTED){
d->state=STOPPED;
d->stat=-1;
SetFilePointer(d->fd, d->hsize, NULL, FILE_BEGIN);
//read_wav_header(d);
}
return 0;
}
static void player_uninit(MSFilter *f){
PlayerData *d=(PlayerData*)f->data;
if (d->fd!=INVALID_HANDLE_VALUE) player_close(f,NULL);
ms_free(d);
}
static void player_process(MSFilter *f){
PlayerData *d=(PlayerData*)f->data;
int bytes =d->big_buffer * 2*(f->ticker->interval*d->rate*d->nchannels)/1000;
if (d->big_buffer>1)
{
/* when starting reading a file: prepare more data
so that sound card buffer will never remain empty.
*/
d->stat++;
if (d->stat>3)
{
if (d->stat%(d->big_buffer)!=0)
return;
}
}
if (d->state==STARTED){
unsigned long err;
mblk_t *om=allocb(bytes,0);
if (d->pause_time>0){
err=bytes;
memset(om->b_wptr,0,bytes);
d->pause_time-=f->ticker->interval;
}else{
err=0;
ReadFile(d->fd, om->b_wptr, bytes, &err, NULL) ;
}
if (err>=0){
if (err==bytes){
om->b_wptr+=err;
ms_queue_put(f->outputs[0],om);
}
else if (err>0){
om->b_wptr+=err;
ms_filter_notify_no_arg(f,MS_FILE_PLAYER_EOF);
SetFilePointer(d->fd, d->hsize, NULL, FILE_BEGIN);
//read_wav_header(d);
/* special value for playing file only once */
if (d->loop_after==-2)
{
freemsg(om);
player_close(f,NULL);
return;
}
if (d->loop_after>0)
{
d->stat=-1;
d->pause_time=d->loop_after;
}
else
{
bytes=bytes-err;
err=0;
ReadFile(d->fd, om->b_wptr, bytes, &err, NULL);
if (err>0){
om->b_wptr+=err;
}
}
ms_queue_put(f->outputs[0],om);
}
else if (err==0){
ms_filter_notify_no_arg(f,MS_FILE_PLAYER_EOF);
SetFilePointer(d->fd, d->hsize, NULL, FILE_BEGIN);
if (d->loop_after==-2)
{
freemsg(om);
player_close(f,NULL);
return;
}
if (d->loop_after>0)
{
d->stat=-1;
d->pause_time=d->loop_after;
}
else
{
bytes=bytes-err;
err=0;
ReadFile(d->fd, om->b_wptr, bytes, &err, NULL);
if (err>0){
om->b_wptr+=err;
ms_queue_put(f->outputs[0],om);
return;
}
}
freemsg(om);
}else freemsg(om);
}else{
#if !defined(_WIN32_WCE)
ms_warning("Fail to read %i bytes: %s",bytes,strerror(errno));
#else
ms_warning("Fail to read %i bytes: %i",bytes,WSAGetLastError());
#endif
}
}
}
static int player_get_sr(MSFilter *f, void*arg){
PlayerData *d=(PlayerData*)f->data;
*((int*)arg)=d->rate;
return 0;
}
static int player_loop(MSFilter *f, void *arg){
PlayerData *d=(PlayerData*)f->data;
d->loop_after=*((int*)arg);
return 0;
}
static int player_set_big_buffer(MSFilter *f, void *arg){
PlayerData *d=(PlayerData*)f->data;
d->big_buffer=*((int*)arg);
return 0;
}
static int player_eof(MSFilter *f, void *arg){
PlayerData *d=(PlayerData*)f->data;
if (d->fd==NULL && d->state==CLOSED)
*((int*)arg) = TRUE; /* 1 */
else
*((int*)arg) = FALSE; /* 0 */
return 0;
}
static int player_get_nch(MSFilter *f, void *arg){
PlayerData *d=(PlayerData*)f->data;
*((int*)arg)=d->nchannels;
return 0;
}
static MSFilterMethod player_methods[]={
{ MS_FILE_PLAYER_OPEN, player_open },
{ MS_FILE_PLAYER_START, player_start },
{ MS_FILE_PLAYER_STOP, player_stop },
{ MS_FILE_PLAYER_CLOSE, player_close },
{ MS_FILTER_GET_SAMPLE_RATE, player_get_sr},
{ MS_FILTER_GET_NCHANNELS, player_get_nch },
{ MS_FILE_PLAYER_LOOP, player_loop },
{ MS_FILE_PLAYER_DONE, player_eof },
{ MS_FILE_PLAYER_BIG_BUFFER, player_set_big_buffer },
{ 0, NULL }
};
#ifdef _MSC_VER
MSFilterDesc ms_file_player_desc={
MS_FILE_PLAYER_ID,
"MSFilePlayer",
"Raw files and wav reader",
MS_FILTER_OTHER,
NULL,
0,
1,
player_init,
NULL,
player_process,
NULL,
player_uninit,
player_methods
};
#else
MSFilterDesc ms_file_player_desc={
.id=MS_FILE_PLAYER_ID,
.name="MSFilePlayer",
.text="Raw files and wav reader",
.category=MS_FILTER_OTHER,
.ninputs=0,
.noutputs=1,
.init=player_init,
.process=player_process,
.uninit=player_uninit,
.methods=player_methods
};
#endif
MS_FILTER_DESC_EXPORT(ms_file_player_desc)
......@@ -24,10 +24,6 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "mediastreamer2/msfilerec.h"
#include "waveheader.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
static int rec_close(MSFilter *f, void *arg);
......@@ -74,7 +70,7 @@ static void rec_process(MSFilter *f){
static int rec_get_length(const char *file, int *length){
wave_header_t header;
int fd=open(file,O_RDONLY);
int fd=open(file,O_RDONLY|O_BINARY);
int ret=ms_read_wav_header_from_fd(&header,fd);
close(fd);
if (ret>0){
......@@ -93,12 +89,12 @@ static int rec_open(MSFilter *f, void *arg){
if (s->fd!=-1) rec_close(f,NULL);
if (access(filename,R_OK|W_OK)==0){
flags=O_WRONLY;
flags=O_WRONLY|O_BINARY;
if (rec_get_length(filename,&s->size)>0){
ms_message("Opening wav file in append mode, current data size is %i",s->size);
}
}else{
flags=O_WRONLY|O_CREAT|O_TRUNC;
flags=O_WRONLY|O_CREAT|O_TRUNC|O_BINARY;
s->size=0;
}
s->fd=open(filename,flags, S_IRUSR|S_IWUSR);
......
/*
mediastreamer2 library - modular sound and video processing and streaming
Copyright (C) 2006 Simon MORLAT (simon.morlat@linphone.org)
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#define UNICODE
#include "mediastreamer2/msfilerec.h"
#include "mediastreamer2/waveheader.h"
#if !defined(_WIN32_WCE)
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#endif
typedef enum{
Closed,
Stopped,
Started
} State;
typedef struct RecState{
HANDLE fd;
int rate;
int size;
State state;
char filename[256];
} RecState;
static void rec_init(MSFilter *f){
RecState *s=(RecState *)ms_new(RecState,1);
s->fd=INVALID_HANDLE_VALUE;
s->rate=8000;
s->size=0;
s->state=Closed;
f->data=s;
}
static void rec_process(MSFilter *f){
RecState *s=(RecState*)f->data;
mblk_t *m;
int err;
while((m=ms_queue_get(f->inputs[0]))!=NULL){
mblk_t *it=m;
ms_mutex_lock(&f->lock);
if (s->state==Started){
while(it!=NULL){
int len=it->b_wptr-it->b_rptr;
DWORD byte_written=0;
if ((err=WriteFile(s->fd,it->b_rptr,len, &byte_written, NULL))!=len){
if (err<0)
{
#if !defined(_WIN32_WCE)
ms_warning("MSFileRec: fail to write %i bytes: %s",len,strerror(errno));
#else
ms_warning("MSFileRec: fail to write %i bytes: %i",len,WSAGetLastError());
#endif
}
}
it=it->b_cont;
s->size+=len;
}
}
ms_mutex_unlock(&f->lock);
freemsg(m);
}
}
static void write_wav_header(int rate,int size, char *filename){
wave_header_t header;
DWORD bytes_written=0;
HANDLE fd;
WCHAR wUnicode[1024];
MultiByteToWideChar(CP_UTF8, 0, filename, -1, wUnicode, 1024);
memcpy(&header.riff_chunk.riff,"RIFF",4);
header.riff_chunk.len=le_uint32(size+32);
memcpy(&header.riff_chunk.wave,"WAVE",4);
memcpy(&header.format_chunk.fmt,"fmt ",4);
header.format_chunk.len=le_uint32(0x10);
header.format_chunk.type=le_uint16(0x1);
header.format_chunk.channel=le_uint16(0x1);
header.format_chunk.rate=le_uint32(rate);
header.format_chunk.bps=le_uint32(rate*2);
header.format_chunk.blockalign=le_uint16(2);
header.format_chunk.bitpspl=le_uint16(16);
memcpy(&header.data_chunk.data,"data",4);
header.data_chunk.len=le_uint32(size);
/* TODO: replace with "lseek" equivalent for windows */
fd=CreateFile(wUnicode, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
if (fd==INVALID_HANDLE_VALUE){
#if !defined(_WIN32_WCE)
ms_warning("Cannot open %s: %s",filename,strerror(errno));
#else
ms_warning("Cannot open %s: %i",filename,WSAGetLastError());
#endif
return;
}
WriteFile(fd,&header,sizeof(header), &bytes_written, NULL);
if (bytes_written!=sizeof(header)){
ms_warning("Fail to write wav header.");
}
CloseHandle(fd);
}
static int rec_open(MSFilter *f, void *arg){
wave_header_t header;
DWORD bytes_written=0;
RecState *s=(RecState*)f->data;
const char *filename=(const char*)arg;
WCHAR wUnicode[1024];
MultiByteToWideChar(CP_UTF8, 0, filename, -1, wUnicode, 1024);
ms_mutex_lock(&f->lock);
snprintf(s->filename, sizeof(s->filename), "%s", filename);
s->fd=CreateFile(wUnicode, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL);
if (s->fd==INVALID_HANDLE_VALUE){
#if !defined(_WIN32_WCE)
ms_warning("Cannot open %s: %s",filename,strerror(errno));
#else
ms_warning("Cannot open %s: %i",filename,WSAGetLastError());
#endif
ms_mutex_unlock(&f->lock);
return -1;
}
memset(&header ,0,sizeof(header));
WriteFile(s->fd,&header,sizeof(header), &bytes_written, NULL);
if (bytes_written!=sizeof(header)){
ms_warning("Fail to write wav header.");
}
s->state=Stopped;
ms_mutex_unlock(&f->lock);
return 0;
}
static int rec_start(MSFilter *f, void *arg){
RecState *s=(RecState*)f->data;
ms_mutex_lock(&f->lock);
s->state=Started;
ms_mutex_unlock(&f->lock);
return 0;
}
static int rec_stop(MSFilter *f, void *arg){
RecState *s=(RecState*)f->data;
ms_mutex_lock(&f->lock);
s->state=Stopped;
ms_mutex_unlock(&f->lock);
return 0;
}
static int rec_close(MSFilter *f, void *arg){
RecState *s=(RecState*)f->data;
ms_mutex_lock(&f->lock);
s->state=Closed;
if (s->fd!=INVALID_HANDLE_VALUE) {
CloseHandle(s->fd);
write_wav_header(s->rate, s->size, s->filename);
s->fd=INVALID_HANDLE_VALUE;
s->size=0;
}
ms_mutex_unlock(&f->lock);
return 0;
}
static int rec_set_sr(MSFilter *f, void *arg){
RecState *s=(RecState*)f->data;
ms_mutex_lock(&f->lock);
s->rate=*((int*)arg);
ms_mutex_unlock(&f->lock);
return 0;
}
static void rec_uninit(MSFilter *f){
RecState *s=(RecState*)f->data;
if (s->fd!=INVALID_HANDLE_VALUE) rec_close(f,NULL);
ms_free(s);
}
static MSFilterMethod rec_methods[]={
{ MS_FILTER_SET_SAMPLE_RATE, rec_set_sr },
{ MS_FILE_REC_OPEN , rec_open },
{ MS_FILE_REC_START , rec_start },
{ MS_FILE_REC_STOP , rec_stop },
{ MS_FILE_REC_CLOSE , rec_close },
{ 0 , NULL }
};
#ifdef WIN32
MSFilterDesc ms_file_rec_desc={
MS_FILE_REC_ID,
"MSFileRec",
N_("Wav file recorder"),
MS_FILTER_OTHER,
NULL,
1,
0,
rec_init,
NULL,
rec_process,
NULL,
rec_uninit,
rec_methods
};
#else
MSFilterDesc ms_file_rec_desc={
.id=MS_FILE_REC_ID,
.name="MSFileRec",
.text=N_("Wav file recorder"),
.category=MS_FILTER_OTHER,
.ninputs=1,
.noutputs=0,
.init=rec_init,
.process=rec_process,
.uninit=rec_uninit,
.methods=rec_methods
};
#endif
MS_FILTER_DESC_EXPORT(ms_file_rec_desc)
......@@ -17,12 +17,45 @@ along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
/* the following code was taken from a free software utility that I don't remember the name. */
/* sorry */