<div>Dear all,</div>
<div> </div>
<div>This is a follow up email, regarding mp3 streamer. My previous mail is in the rear forwarded message part of this mail.</div>
<div>Below presents my full code, which can be compiled and executable.</div>
<div> </div>
<div>As I tested, the code will crush when it plays test.mp3 for the second round.</div>
<div>But if you change to use mpg file or ts file, the code will be OK.</div>
<div>Also, as I mentioned in previous mail, if I directly use MP3FileSource, without using MPEG2TransportStreamFromESSource related classes, the code will be OK without fault.</div>
<div> </div>
<div>Experts, please suggest whether my code is wrong or there is potential bug in livemedia lib?</div>
<div> </div>
<div>Thanks</div>
<div> </div>
<div>Woods</div>
<div> </div>
<div>-----------------------------------------------------------------------</div>
<div><br>#include "liveMedia.hh"<br>#include "BasicUsageEnvironment.hh"<br>#include "GroupsockHelper.hh"</div>
<div>#define TRANSPORT_PACKETS_PER_NETWORK_PACKET 7<br>#define TRANSPORT_PACKET_SIZE 188</div>
<div>class MyStreamer {</div>
<div> public:<br> UsageEnvironment *env;</div>
<div> char * fileName;<br> char * indexName;<br> char * address;<br> unsigned short port;</div>
<div> Groupsock *rtpGroupsock;<br> MediaSink *sink;<br> MPEG1or2Demux* baseDemultiplexor;<br> FramedSource* source;</div>
<div> public:<br> MyStreamer(UsageEnvironment* env, char *file, char * index, char * udpAddress, unsigned short udpPort);<br> virtual ~MyStreamer();</div>
<div> void play();<br> void stop();<br>};</div>
<div>static void replay(void *param)<br>{<br> MyStreamer *streamer = (MyStreamer*)param;<br> streamer->stop();<br> streamer->play();<br>}</div>
<div>MyStreamer::<br> MyStreamer(UsageEnvironment* environment, char * file, char * index, char * udpAddress, unsigned short udpPort)<br>{<br> env = environment;</div>
<div> indexName = new char[strlen(index)+1];<br> strcpy(indexName,index);<br> address = new char[strlen(udpAddress)+1];<br> strcpy(address,udpAddress);<br> fileName = file;<br> fileName = new char[strlen(file)+1];<br> strcpy(fileName,file);<br>
port = udpPort;<br> <br> const unsigned char ttl = 16; // reduce it in case routers don't admin scope<br> struct in_addr destinationAddress;<br> destinationAddress.s_addr = our_inet_addr(address);<br> Port destPort(udpPort);<br>
rtpGroupsock = new Groupsock(*env, destinationAddress, destPort, ttl);<br> sink = BasicUDPSink::createNew(*env,rtpGroupsock,65536);</div>
<div> *env << "Streamer " << indexName << " starting...\n";<br> play();</div>
<div> return;<br>}</div>
<div>MyStreamer::<br> ~MyStreamer()<br>{<br> *env << "Streamer " << indexName << " stopping...\n";<br> stop();</div>
<div> Medium::close(sink);sink = NULL;<br> if(rtpGroupsock != NULL){<br> delete rtpGroupsock;<br> rtpGroupsock = NULL;<br> }<br> delete [] indexName;<br> delete [] address;<br> delete [] fileName;<br> <br> *env << "Streamer Closed\n";<br>
}</div>
<div>void MyStreamer::play() <br>{<br> unsigned const inputDataChunkSize = TRANSPORT_PACKETS_PER_NETWORK_PACKET * TRANSPORT_PACKET_SIZE;<br> ByteStreamFileSource * fileSource = ByteStreamFileSource::createNew(*env, fileName, inputDataChunkSize);<br>
if(fileSource == NULL){<br> *env << "unfound file\n";<br> source = NULL;<br> return;<br> }</div>
<div> char const* extension = strrchr(fileName, '.');</div>
<div> if(strcmp(extension,".mpg")==0){<br> baseDemultiplexor = MPEG1or2Demux::createNew(*env, fileSource);<br> MPEG1or2DemuxedElementaryStream* pesSource = baseDemultiplexor->newRawPESStream();<br> FramedSource* tsFrames = MPEG2TransportStreamFromPESSource::createNew(*env, pesSource);<br>
source = MPEG2TransportStreamFramer::createNew(*env, tsFrames);<br> } else if(strcmp(extension,".ts")==0){<br> source = MPEG2TransportStreamFramer::createNew(*env, fileSource);<br> } else if(strcmp(extension,".mp3")==0){<br>
Medium::close(fileSource);<br> source = MP3FileSource::createNew(*env,fileName);<br> MPEG2TransportStreamFromESSource *m2ts = MPEG2TransportStreamFromESSource::createNew(*env);<br> m2ts->addNewAudioSource(source,1);<br>
source = MPEG2TransportStreamFramer::createNew(*env, m2ts);<br> }</div>
<div> *env << "Streaming file " << fileName << "...\n";<br> sink->startPlaying(*source, replay, this);<br>}</div>
<div>void MyStreamer::stop()<br>{<br> *env << "Stop sink\n";<br> if(sink != NULL)<br> sink->stopPlaying();</div>
<div> *env << "Close source\n";<br> Medium::close(source);<br> source = NULL;</div>
<div> if(baseDemultiplexor != NULL){<br> *env << "Close mpeg related souces\n";<br> Medium::close(baseDemultiplexor);<br> baseDemultiplexor = NULL;<br> }</div>
<div> *env << "Streamer stopped\n";<br>}</div>
<div>int main(int argc, char** argv){</div>
<div>// char file[] = "test.mpg";<br>// char file[] = "test.ts";<br> char file[] = "test.mp3";</div>
<div> TaskScheduler* scheduler = BasicTaskScheduler::createNew();<br> UsageEnvironment *env = BasicUsageEnvironment::createNew(*scheduler);</div>
<div> MyStreamer *streamer1 = new MyStreamer(env, file, "first", "234.100.1.1", 9000);<br> MyStreamer *streamer2 = new MyStreamer(env, file, "second", "234.100.1.2", 9000);<br> scheduler->doEventLoop();<br>
}<br>-----------------------------------------------------------------------------<br><br></div>
<div class="gmail_quote">---------- Forwarded message ----------<br>From: <b class="gmail_sendername">Woods</b> <span dir="ltr"><<a href="http://woods.biz">woods.biz</a>@<a href="http://gmail.com">gmail.com</a>></span><br>
Date: Tue, Jul 7, 2009 at 4:43 PM<br>Subject: mp3 streamer doubt<br>To: LIVE555 Streaming Media - development & use <<a href="mailto:live-devel@ns.live555.com">live-devel@ns.live555.com</a>><br><br><br>Dear Experts,<br>
<br>I have a doubt on my MP3 Streamer.<br><br>This streamer is based on testMP3Streamer code, but I changed to use BasicUDPSink. And it works fine.<br><br>As you know, in play() function, the code was like:<br><br>source = MP3FileSource::createNew(*env,fileName);<br>
<br><br><br>But after I slightly changed my code as follows:<br><br>MP3FileSource *mp3fileSource = MP3FileSource::createNew(*env,fileName);<br>MPEG2TransportStreamFromESSource *m2ts = MPEG2TransportStreamFromESSource::createNew(*env);<br>
m2ts->addNewAudioSource(mp3fileSource,1);<br>source = MPEG2TransportStreamFramer::createNew(*env, m2ts);<br><br>What I want is streaming mp3 within mpeg2ts / udp. For the first round of play, this is perfect. But, when mp3 file reaches end, the stop() and play() functions are called in sequence, the program segment fault when it starts to playing again.<br>
<br>But if I comment the Medium::close() in stop() function, (I do this only for testing purpose), I can repeatedly stream that mp3 file.<br><br>After testing in different ways, I suspect there is something wrong when I use MPEG2TransportStreamFromESSource, especially when I delete it. But according to my understanding, in my code, the delete operations should be:<br>
<br>stop() function --> Medium::close(source) --> delete MPEG2TransportStreamFramer --> delete MPEG2TransportStreamFromESSource --> delete MP3FileSource. It looks OK! But what is wrong? <br><br>Could anyone give me a suggestion?<br>
<br>Thanks<br><br><br>-- <br><font color="#888888">Woods<br></font></div><br><br clear="all">
<div></div><br>-- <br>Woods<br>