std.stream.File help required (and classes)
akaz
nemo at utopia.com
Tue Mar 27 04:58:03 PDT 2012
Hi all,
I am trying to port some application based on a library called
mediastreamer2, part of linphone software.
The basic software component built on top of mediastreamer2 is
called a "filter".
Basically, it is a C structure with parameters and some methods
(pointers to functions). Ammong the latter: a kind of constructor
(.init), a kind of destructor (.uninit), some setters and a sort
of "run method" (.process).
The "run method" is called by an independent thread (a time
ticker). It reads some queues, extract messages (packaged data)
from there and process this data.
Now, I implemented a filter which basically takes packaged data
(messages) from an input queue and *should* write this data into
a file.
This is built around a std.stream.File class.
Now, the questions:
1. why there is std.stdio.File, but also std.stream.File? This
gives a conflict and explicit names must then be used to avoid
conflict.
2. is the std.stream.File the correct choice here? Should I use
std.file instead? Then, why so many file classes (count
std.stdio.File too).
3. std.stream.File does not have, apparently, some
easy-to-interogate properties to know the name (path) and the
state of the associated file. Are there any?
4. the "run method" seems to lose knowledge about the file. The
file is opened in a setter and then it should remain seekable and
writeable. However, into the "run method" it appears as
unseekable and unwriteable. More, trying to access .toString()
property of the std.stream.File variable or to write some data
into the file triggers a segmentation fault error.
Here is the file msf_sinker_binaryfile.d, but if required, then
I will post other files too. I am not sure how to extract a
simpler test case.
//========================msf_sinker_binaryfile.d==========================
module msf_sinker_binaryfile;
import std.stream;
import std.stdio;
import mediastreamer2_layer;
import msf_commons;
const uint MSF_SINKER_BINARYFILE_OPEN =
MS_FILTER_METHOD!(byte)(cast(MSFilterId)MSFilterIdNew.MSF_SINKER_BINARYFILE_ID,0);
const uint MSF_SINKER_BINARYFILE_CLOSE =
MS_FILTER_METHOD_NO_ARG(cast(MSFilterId)MSFilterIdNew.MSF_SINKER_BINARYFILE_ID,1);
private struct _MSF_State{
__gshared std.stream.File filedesc; //file descriptor
};
private alias _MSF_State MSF_State;
private void msf_init(MSFilter* f){ //a constructor
printf("msf_binfile_init-start\n============================\n");
MSF_State* s=ms_new!(MSF_State)(1);
s.filedesc = new std.stream.File;
printf("s.filedesc=%p+++++++++++++++++++++\n",s.filedesc);
writef("s.filedesc.seekable=%b+++++++++++++++++++++\n",s.filedesc.seekable);
writef("s.filedesc.readable=%b+++++++++++++++++++++\n",s.filedesc.readable);
writef("s.filedesc.writeable=%b+++++++++++++++++++++\n",s.filedesc.writeable);
writef("s.filedesc.toString=%b+++++++++++++++++++++\n",s.filedesc.toString);
f.data=s;
printf("msf_binfile_init-stop\n============================\n");
}
private int msf_open(MSFilter* f, void* arg){ //a setter
printf("msf_binfile_open-start\n============================\n");
MSF_State* s=cast(MSF_State*)f.data;
ms_mutex_lock(&(f.lock));
printf("s.filedesc=%p+++++++++++++++++++++\n",s.filedesc);
writef("s.filedesc.seekable=%b+++++++++++++++++++++\n",s.filedesc.seekable);
writef("s.filedesc.readable=%b+++++++++++++++++++++\n",s.filedesc.readable);
writef("s.filedesc.writeable=%b+++++++++++++++++++++\n",s.filedesc.writeable);
writef("s.filedesc.toString=%b+++++++++++++++++++++\n",s.filedesc.toString);
s.filedesc.open(*(cast(string*)arg),FileMode.OutNew);
printf("s.filedesc=%p+++++++++++++++++++++\n",s.filedesc);
writef("s.filedesc.seekable=%b+++++++++++++++++++++\n",s.filedesc.seekable);
writef("s.filedesc.readable=%b+++++++++++++++++++++\n",s.filedesc.readable);
writef("s.filedesc.writeable=%b+++++++++++++++++++++\n",s.filedesc.writeable);
writef("s.filedesc.toString=%b+++++++++++++++++++++\n",s.filedesc.toString);
ms_mutex_unlock(&(f.lock));
printf("msf_binfile_open-stop\n============================\n");
return 0;
}
private void msf_process(MSFilter* f){ //a run() method
printf("msf_binfile_process-start\n============================\n");
MSF_State* s=cast(MSF_State*)f.data;
mblk_t* m;
while((m=ms_queue_get(f.inputs[0]))!=null){ //reading samples
from input
ms_mutex_lock(&(f.lock));
int len=cast(int)((m.b_wptr-m.b_rptr)); //length of message
data block
byte* p=cast(byte*)m.b_rptr;
/*printf("----------------------------\n");
for(int idx=0; idx<len; idx++){
printf("d_data[%d]=%d\n",idx,d_data[idx]);
}
printf("----------------------------\n");*/
//File filedesc;
//filedesc.open("newfile.bin","w+b");
printf("s.filedesc=%p+++++++++++++++++++++\n",s.filedesc);
writef("s.filedesc.seekable=%b+++++++++++++++++++++\n",s.filedesc.seekable);
writef("s.filedesc.readable=%b+++++++++++++++++++++\n",s.filedesc.readable);
writef("s.filedesc.writeable=%b+++++++++++++++++++++\n",s.filedesc.writeable);
writef("s.filedesc.toString=%b+++++++++++++++++++++\n",s.filedesc.toString);
//s.filedesc.writeString("This writes a string to the file. ");
//s.filedesc.writeBlock(p,len);
//filedesc.close();
ms_mutex_unlock(&(f.lock));
freemsg(m);
}
printf("msf_binfile_process-start\n============================\n");
}
private int msf_close(MSFilter* f, void* arg){ //an unsetter
MSF_State* s=cast(MSF_State*)f.data;
ms_mutex_lock(&(f.lock));
s.filedesc.close();
ms_mutex_unlock(&(f.lock));
return 0;
}
private void msf_uninit(MSFilter* f){ //a destructor
MSF_State* s=cast(MSF_State*)f.data;
msf_close(f, null);
ms_free(s);
}
private immutable MSFilterMethod[3] msf_methods=[
{id:MSF_SINKER_BINARYFILE_OPEN,method:cast(MSFilterMethodFunc)&msf_open},
{id:MSF_SINKER_BINARYFILE_CLOSE,method:cast(MSFilterMethodFunc)&msf_close},
{0,null}
];
immutable MSFilterDesc msf_sinker_binaryfile_desc={
id:cast(MSFilterId)MSFilterIdNew.MSF_SINKER_BINARYFILE_ID,
name:"MSFileSink",
text:"File sink",
category:MSFilterCategory.MS_FILTER_OTHER,
ninputs:1,
noutputs:0,
init:cast(MSFilterFunc)&msf_init,
process:cast(MSFilterFunc)&msf_process,
uninit:cast(MSFilterFunc)&msf_uninit,
methods:cast(MSFilterMethod*)&msf_methods[0]
};
//========================================================================
The segmentation fault appears on line 68 (the line
"writef("s.filedesc.toString=%b+++++++++++++++++++++\n",s.filedesc.toString);"
inside the msf_process() function (there are several lines like
that - debug messages).
The output of the software is:
warning: GDB: Failed to set controlling terminal: Operation not
permitted
-------START PROGRAM-------
ALSA lib conf.c:4687:(snd_config_expand) Unknown parameters 0
ALSA lib control.c:951:(snd_ctl_open_noupdate) Invalid CTL
default:0
ortp-warning-Could not attach mixer to card: Invalid argument
ALSA lib conf.c:4687:(snd_config_expand) Unknown parameters 0
ALSA lib pcm.c:2217:(snd_pcm_open_noupdate) Unknown PCM default:0
ALSA lib conf.c:4687:(snd_config_expand) Unknown parameters 0
ALSA lib pcm.c:2217:(snd_pcm_open_noupdate) Unknown PCM default:0
ortp-warning-Strange, sound card HDA Intel does not seems to be
capable of anything, retrying with plughw...
ALSA lib conf.c:4687:(snd_config_expand) Unknown parameters 1
ALSA lib control.c:951:(snd_ctl_open_noupdate) Invalid CTL
default:1
ortp-warning-Could not attach mixer to card: Invalid argument
ALSA lib conf.c:4687:(snd_config_expand) Unknown parameters 1
ALSA lib pcm.c:2217:(snd_pcm_open_noupdate) Unknown PCM default:1
ALSA lib conf.c:4687:(snd_config_expand) Unknown parameters 1
ALSA lib pcm.c:2217:(snd_pcm_open_noupdate) Unknown PCM default:1
ortp-warning-Strange, sound card HDA ATI HDMI does not seems to
be capable of anything, retrying with plughw...
ortp-warning-Strange, sound card HDA ATI HDMI seems totally
unusable.
msf_binfile_init-start
============================
s.filedesc=0x7ffff7ebcf40+++++++++++++++++++++
s.filedesc.seekable=0+++++++++++++++++++++
s.filedesc.readable=0+++++++++++++++++++++
s.filedesc.writeable=0+++++++++++++++++++++
s.filedesc.toString=[1110011, 1110100, 1100100, 101110, 1110011,
1110100, 1110010, 1100101, 1100001, 1101101, 101110, 1000110,
1101001, 1101100, 1100101]+++++++++++++++++++++
msf_binfile_init-stop
============================
msf_set_freq-start
============================
msf_set_freq-stop
============================
msf_set_rate-start
============================
msf_set_rate-stop
============================
msf_binfile_open-start
============================
s.filedesc=0x7ffff7ebcf40+++++++++++++++++++++
s.filedesc.seekable=0+++++++++++++++++++++
s.filedesc.readable=0+++++++++++++++++++++
s.filedesc.writeable=0+++++++++++++++++++++
s.filedesc.toString=[1110011, 1110100, 1100100, 101110, 1110011,
1110100, 1110010, 1100101, 1100001, 1101101, 101110, 1000110,
1101001, 1101100, 1100101]+++++++++++++++++++++
s.filedesc=0x7ffff7ebcf40+++++++++++++++++++++
s.filedesc.seekable=1+++++++++++++++++++++
s.filedesc.readable=0+++++++++++++++++++++
s.filedesc.writeable=1+++++++++++++++++++++
s.filedesc.toString=[1110011, 1110100, 1100100, 101110, 1110011,
1110100, 1110010, 1100101, 1100001, 1101101, 101110, 1000110,
1101001, 1101100, 1100101]+++++++++++++++++++++
msf_binfile_open-stop
============================
msf_sinusoid_process-start
============================
s.rate=4000.000000============================
msf_sinusoid_process-stop
============================
msf_split_process-start
============================
msf_split_process-stop
============================
msf_dbl2int16_process-start
============================
msf_dbl2int16_process-stop
============================
ortp-warning-alsa_set_params: periodsize:0 Using 64
ortp-warning-alsa_set_params: period:8 Using 8
msf_binfile_process-start
============================
s.filedesc=0x7ffff7ebcf40+++++++++++++++++++++
s.filedesc.seekable=0+++++++++++++++++++++
s.filedesc.readable=0+++++++++++++++++++++
s.filedesc.writeable=0+++++++++++++++++++++
Segmentation fault (core dumped)
Strangely, the file is writeable and seekable in the msf_open()
function, but it becomes unseekable and unwriteable in the
msf_process() function. Do I lose my file descriptor when exiting
the msf_open() function? If yes, how to keep it? It is because of
the TLS variables?
Thank you.
More information about the Digitalmars-d-learn
mailing list