[Live-devel] mp3 streamer doubt
Woods
woods.biz at gmail.com
Wed Jul 8 08:09:21 PDT 2009
Dear all,
This is a follow up email, regarding mp3 streamer. My previous mail is in
the rear forwarded message part of this mail.
Below presents my full code, which can be compiled and executable.
As I tested, the code will crush when it plays test.mp3 for the second
round.
But if you change to use mpg file or ts file, the code will be OK.
Also, as I mentioned in previous mail, if I directly use MP3FileSource,
without using MPEG2TransportStreamFromESSource related classes, the code
will be OK without fault.
Experts, please suggest whether my code is wrong or there is potential bug
in livemedia lib?
Thanks
Woods
-----------------------------------------------------------------------
#include "liveMedia.hh"
#include "BasicUsageEnvironment.hh"
#include "GroupsockHelper.hh"
#define TRANSPORT_PACKETS_PER_NETWORK_PACKET 7
#define TRANSPORT_PACKET_SIZE 188
class MyStreamer {
public:
UsageEnvironment *env;
char * fileName;
char * indexName;
char * address;
unsigned short port;
Groupsock *rtpGroupsock;
MediaSink *sink;
MPEG1or2Demux* baseDemultiplexor;
FramedSource* source;
public:
MyStreamer(UsageEnvironment* env, char *file, char * index, char *
udpAddress, unsigned short udpPort);
virtual ~MyStreamer();
void play();
void stop();
};
static void replay(void *param)
{
MyStreamer *streamer = (MyStreamer*)param;
streamer->stop();
streamer->play();
}
MyStreamer::
MyStreamer(UsageEnvironment* environment, char * file, char * index, char *
udpAddress, unsigned short udpPort)
{
env = environment;
indexName = new char[strlen(index)+1];
strcpy(indexName,index);
address = new char[strlen(udpAddress)+1];
strcpy(address,udpAddress);
fileName = file;
fileName = new char[strlen(file)+1];
strcpy(fileName,file);
port = udpPort;
const unsigned char ttl = 16; // reduce it in case routers don't admin
scope
struct in_addr destinationAddress;
destinationAddress.s_addr = our_inet_addr(address);
Port destPort(udpPort);
rtpGroupsock = new Groupsock(*env, destinationAddress, destPort, ttl);
sink = BasicUDPSink::createNew(*env,rtpGroupsock,65536);
*env << "Streamer " << indexName << " starting...\n";
play();
return;
}
MyStreamer::
~MyStreamer()
{
*env << "Streamer " << indexName << " stopping...\n";
stop();
Medium::close(sink);sink = NULL;
if(rtpGroupsock != NULL){
delete rtpGroupsock;
rtpGroupsock = NULL;
}
delete [] indexName;
delete [] address;
delete [] fileName;
*env << "Streamer Closed\n";
}
void MyStreamer::play()
{
unsigned const inputDataChunkSize = TRANSPORT_PACKETS_PER_NETWORK_PACKET *
TRANSPORT_PACKET_SIZE;
ByteStreamFileSource * fileSource = ByteStreamFileSource::createNew(*env,
fileName, inputDataChunkSize);
if(fileSource == NULL){
*env << "unfound file\n";
source = NULL;
return;
}
char const* extension = strrchr(fileName, '.');
if(strcmp(extension,".mpg")==0){
baseDemultiplexor = MPEG1or2Demux::createNew(*env, fileSource);
MPEG1or2DemuxedElementaryStream* pesSource =
baseDemultiplexor->newRawPESStream();
FramedSource* tsFrames =
MPEG2TransportStreamFromPESSource::createNew(*env, pesSource);
source = MPEG2TransportStreamFramer::createNew(*env, tsFrames);
} else if(strcmp(extension,".ts")==0){
source = MPEG2TransportStreamFramer::createNew(*env, fileSource);
} else if(strcmp(extension,".mp3")==0){
Medium::close(fileSource);
source = MP3FileSource::createNew(*env,fileName);
MPEG2TransportStreamFromESSource *m2ts =
MPEG2TransportStreamFromESSource::createNew(*env);
m2ts->addNewAudioSource(source,1);
source = MPEG2TransportStreamFramer::createNew(*env, m2ts);
}
*env << "Streaming file " << fileName << "...\n";
sink->startPlaying(*source, replay, this);
}
void MyStreamer::stop()
{
*env << "Stop sink\n";
if(sink != NULL)
sink->stopPlaying();
*env << "Close source\n";
Medium::close(source);
source = NULL;
if(baseDemultiplexor != NULL){
*env << "Close mpeg related souces\n";
Medium::close(baseDemultiplexor);
baseDemultiplexor = NULL;
}
*env << "Streamer stopped\n";
}
int main(int argc, char** argv){
// char file[] = "test.mpg";
// char file[] = "test.ts";
char file[] = "test.mp3";
TaskScheduler* scheduler = BasicTaskScheduler::createNew();
UsageEnvironment *env = BasicUsageEnvironment::createNew(*scheduler);
MyStreamer *streamer1 = new MyStreamer(env, file, "first", "234.100.1.1",
9000);
MyStreamer *streamer2 = new MyStreamer(env, file, "second", "234.100.1.2",
9000);
scheduler->doEventLoop();
}
-----------------------------------------------------------------------------
---------- Forwarded message ----------
From: Woods <woods.biz at gmail.com>
Date: Tue, Jul 7, 2009 at 4:43 PM
Subject: mp3 streamer doubt
To: LIVE555 Streaming Media - development & use <live-devel at ns.live555.com>
Dear Experts,
I have a doubt on my MP3 Streamer.
This streamer is based on testMP3Streamer code, but I changed to use
BasicUDPSink. And it works fine.
As you know, in play() function, the code was like:
source = MP3FileSource::createNew(*env,fileName);
But after I slightly changed my code as follows:
MP3FileSource *mp3fileSource = MP3FileSource::createNew(*env,fileName);
MPEG2TransportStreamFromESSource *m2ts =
MPEG2TransportStreamFromESSource::createNew(*env);
m2ts->addNewAudioSource(mp3fileSource,1);
source = MPEG2TransportStreamFramer::createNew(*env, m2ts);
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.
But if I comment the Medium::close() in stop() function, (I do this only for
testing purpose), I can repeatedly stream that mp3 file.
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:
stop() function --> Medium::close(source) --> delete
MPEG2TransportStreamFramer --> delete MPEG2TransportStreamFromESSource -->
delete MP3FileSource. It looks OK! But what is wrong?
Could anyone give me a suggestion?
Thanks
--
Woods
--
Woods
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.live555.com/pipermail/live-devel/attachments/20090708/c16f43ab/attachment.html>
More information about the live-devel
mailing list