[Live-devel] Can't stream Mpeg4

Georgy Steshin gosha212 at gmail.com
Mon Sep 14 14:16:32 PDT 2009


Hi everybody,

I have a problem with streaming mpeg4. My program capture the frames from
the camera encodes it to xvid and streams it. For some reason I don't see
outgoing packet in wireshark and if I put a breakpoint in
OutputSocket::write it never reaches it. Hope for your help. Sorry for the
poor english.

 

Here is the source code:

 

========Start  DeviceSourceCamera.h =======

#pragma once

 

#include <DeviceSource.hh>

#include "liveMedia.hh"

#include "BasicUsageEnvironment.hh"

 

#include "cv.h"

#include "highgui.h"

 

#include <revel.h>

 

class DeviceSourceCammera: public DeviceSource {

public:

      static DeviceSourceCammera* createNew(UsageEnvironment& env,
DeviceParameters params);

 

      CvCapture* capture;

 

protected:

      DeviceSourceCammera(UsageEnvironment& env, DeviceParameters params);

 

      virtual ~DeviceSourceCammera();

 

private:

      virtual void doGetNextFrame();

 

private:

      void deliverFrame();

 

private:

      DeviceParameters fParams;

};

 

========End  DeviceSourceCamera.h =======

 

========Start  DeviceSourceCamera.cpp =======

#include "DeviceSourceCamera.h"

 

 

 

DeviceSourceCammera::DeviceSourceCammera(UsageEnvironment& env,
DeviceParameters params) : DeviceSource(env, params)

{

      //initiate the device

      

      //Create a window to display

      capture = cvCreateCameraCapture(0);

      cvNamedWindow("playBack", 1); 

}

 

DeviceSourceCammera* DeviceSourceCammera::createNew(UsageEnvironment& env,
DeviceParameters params) 

{

      return new DeviceSourceCammera(env, params);

}

 

void DeviceSourceCammera::doGetNextFrame() 

{

      //Grab the image from the camera

 

      if (0 /* the source stops being readable */) 

      {

            handleClosure(this);

            fprintf(stderr,"In Grab Image V4l, the source stops being
readable!!!\n");

            return;

      }

 

      deliverFrame();

}

 

void DeviceSourceCammera::deliverFrame() 

{

      //Encode here the frame

 

 

      IplImage *img = 0;

 

      if (!isCurrentlyAwaitingData()) 

            return;

 

      int width = 640;

      int height = 480; 

      int frameSize;

 

      const char *filename = "test.m4v";

      Revel_Error revError;

 

      if (REVEL_API_VERSION != Revel_GetApiVersion())

      {

            printf("ERROR: Revel version mismatch!\n");

            printf("Headers: version %06x, API version %d\n", REVEL_VERSION,
REVEL_API_VERSION);

            printf("Library: version %06x, API version %d\n",
Revel_GetVersion(), Revel_GetApiVersion());

            exit(1);

      }

 

 

      int encoderHandle;

      revError = Revel_CreateEncoder(&encoderHandle);

      if (revError != REVEL_ERR_NONE)

      {

            printf("Revel Error while creating encoder: %d\n", revError);

            exit(1);

      }

 

      Revel_Params revParams;

    Revel_InitializeParams(&revParams);

    revParams.width = width;

    revParams.height = height;

    revParams.frameRate = 25.0f;

    revParams.quality = 1.0f;

    revParams.codec = REVEL_CD_XVID;

 

 

      revError = Revel_EncodeStart(encoderHandle, filename, &revParams);

      if (revError != REVEL_ERR_NONE)

      {

            printf("Revel Error while starting encoding: %d\n", revError);

            exit(1);

      }

 

 

      //Grab image

      do

      {

            cvGrabFrame(capture);  

            img=cvRetrieveFrame(capture);

 

            cvShowImage("playBack", img);

            cvWaitKey(20);

      }while(img->imageData == 0);

 

      Revel_VideoFrame frame;

      frame.width = width;

      frame.height = height;

      frame.bytesPerPixel = 3;

      frame.pixelFormat =  REVEL_PF_BGR;

      frame.pixels = new int[img->imageSize];

      memset(frame.pixels, 0, img->imageSize);

 

      memcpy(frame.pixels, img->imageData, img->imageSize);

 

      revError = Revel_EncodeFrame(encoderHandle, &frame, &frameSize);

      if (revError != REVEL_ERR_NONE)

      {

            printf("Revel Error while writing frame: %d\n", revError);

            exit(1);

      }

 

      if (img->imageSize > fMaxSize)

      {

            frameSize = fMaxSize;

      }

      else 

            frameSize = img->imageSize;

      fFrameSize = frameSize;

 

//    delete fTo;

      fTo = new unsigned char[img->imageSize];

      memcpy(fTo, frame.pixels, frameSize);

 

      Revel_DestroyEncoder(encoderHandle);

  

      nextTask() = envir().taskScheduler().scheduleDelayedTask(0,
(TaskFunc*) afterGetting,this);

}

 

DeviceSourceCammera::~DeviceSourceCammera() {

 //printf("DeviceSourceCammera Deconstructor...");

      //cvReleaseCapture(&capture);

      //cvDestroyWindow("playBack");

}

========End  DeviceSourceCamera.cpp =======

 

======== (Main) Start WebCam.cpp ============

 

#include "liveMedia.hh"

#include "BasicUsageEnvironment.hh"

#include "GroupsockHelper.hh"

#include "DeviceSourceCamera.h"

 

UsageEnvironment* env;

MPEG4VideoStreamFramer* videoSource;

RTPSink* videoSink;

DeviceSourceCammera* fileSource;

 

void afterPlaying(void* /*clientData*/);

void play();

 

int main()

{

      TaskScheduler* scheduler = BasicTaskScheduler::createNew();

      env = BasicUsageEnvironment::createNew(*scheduler);

 

      char const* destinationAddressStr = "192.168.0.7";

      struct in_addr destinationAddress;

      destinationAddress.s_addr = our_inet_addr(destinationAddressStr);

 

      const unsigned short rtpPortNum = 8888;

      const unsigned short rtcpPortNum = rtpPortNum+1;

      const unsigned char ttl = 7;

 

      const Port rtpPort(rtpPortNum);

      const Port rtcpPort(rtcpPortNum);

 

      Groupsock rtpGroupsock(*env, destinationAddress, rtpPort, ttl);

      Groupsock rtcpGroupsock(*env, destinationAddress, rtcpPort, ttl);

 

      

 

      videoSink = MPEG4ESVideoRTPSink::createNew(*env, &rtpGroupsock, 96);

      const unsigned estimatedSessionBandwidth = 4500; 

      const unsigned maxCNAMElen = 100;

      unsigned char CNAME[maxCNAMElen+1];

      gethostname((char*)CNAME, maxCNAMElen);

      CNAME[maxCNAMElen] = '\0';

 

      RTCPInstance* rtcp = RTCPInstance::createNew(*env, &rtcpGroupsock,
estimatedSessionBandwidth, CNAME, videoSink, NULL, False);

      DeviceParameters params;

 

      fileSource = DeviceSourceCammera::createNew(*env, params);

      if (fileSource == NULL) {

            *env << "Unable to open source\n";

            exit(1);

      }

 

 

 

      play();

      env->taskScheduler().doEventLoop();

 

      return 0;

}

 

void play() 

{

 

      FramedSource* videoES = fileSource;

 

      // Create a framer for the Video Elementary Stream:

      videoSource = MPEG4VideoStreamFramer::createNew(*env, videoES);

 

      // Finally, start playing:

      *env << "Beginning to read from file...\n";

      videoSink->startPlaying(*videoSource, afterPlaying, videoSink);

}

 

void afterPlaying(void* /*clientData*/) 

{

      *env << "...done reading from file\n";

 

      Medium::close(videoSource);

 

      // Start playing once again:

      play();

}

 

 

 

 

Georgy Steshin,

 

image001

 

      Hutsot Shefayim P.O.B 348 

      Shefayim        Israel  60990

      Mobile:    +972-52-6222528

      Office:      +972-9-788 7342

      Fax:           +972-9-9576021

 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.live555.com/pipermail/live-devel/attachments/20090915/7460188c/attachment-0001.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: image/jpeg
Size: 3016 bytes
Desc: not available
URL: <http://lists.live555.com/pipermail/live-devel/attachments/20090915/7460188c/attachment-0001.jpe>


More information about the live-devel mailing list