Ejemplo n.º 1
0
static int extractGenericTrak(const MP4_CONTAINER_T *pMp4, 
                              const MP4_TRAK_T *pMp4BoxSet, 
                              const char *path, 
                              float fStart,
                              float fDuration) {

  FILE_STREAM_T fStreamOut;
  CBDATA_WRITE_GENERIC_SAMPLE_T genericDescr;
  uint64_t startHz = 0;
  uint64_t durationHz = 0;
  int rc = 0;

  durationHz = (uint64_t) (fDuration * pMp4BoxSet->pMdhd->timescale);
  startHz = (uint64_t) (fStart * pMp4BoxSet->pMdhd->timescale);

  if((fStreamOut.fp = fileops_Open(path, O_RDWR | O_CREAT)) == FILEOPS_INVALID_FP) {
    LOG(X_CRITICAL("Unable to open file for writing: %s"), path);
    return -1;
  }
  fStreamOut.offset = 0;

  LOG(X_DEBUG("Extracting raw media from %s"), pMp4->pStream->cbGetName(pMp4->pStream));

  genericDescr.pStreamOut = &fStreamOut;
  rc = mp4_readSamplesFromTrack(pMp4BoxSet, pMp4->pStream, writeGenericSample_mp4wrap, &genericDescr,
                                startHz, durationHz);

  fileops_Close(fStreamOut.fp);

  return rc;
}
Ejemplo n.º 2
0
int streamer_run_tofile(STREAM_XMIT_NODE_T *pStreamIn, STREAM_XMIT_NODE_T *pStreamOut, const char *path) {
  FILE_STREAM_T fileOut;

  if(pStreamIn->pNext) {
    LOG(X_ERROR("Cannot not stream multiple  streams to one file"));
    return -1;
  }

  memset(&fileOut, 0, sizeof(fileOut));
  if(pStreamOut->poverwritefile && ! *pStreamOut->poverwritefile) {
    if((fileOut.fp = fileops_Open(path, O_RDONLY)) != FILEOPS_INVALID_FP) {
      fileops_Close(fileOut.fp);
      LOG(X_ERROR("File %s already exists.  Will not overwrite."), path);
      return -1;
    }
  }

  if((fileOut.fp = fileops_Open(path, O_RDWR | O_CREAT)) == FILEOPS_INVALID_FP) {
    LOG(X_ERROR("Unable to open file for writing: %s"), path);
    return -1;
  }
  LOG(X_DEBUG("Writing to local file: %s"), path);

  // TODO: disable loop setting

  pStreamOut->pXmitCbs->pCbRecordData = &fileOut;

  while(*pStreamIn->prunning == STREAMER_STATE_RUNNING && !g_proc_exit) {

    if(pStreamIn->cbCanSend(pStreamIn->pCbData) < 0) {
      // TODO: check error condition vs eof
      //fprintf(stderr, "no more pkts...\n");
      break;
    }

    if(pStreamIn->cbPreparePkt(pStreamIn->pCbData) < 0) {
      // TODO: check error condition vs eof
      //fprintf(stderr, "no more pkts...\n");
      break;
    }

  }

  return 0;
}
Ejemplo n.º 3
0
static int mp4_extractAvcVid(const MP4_CONTAINER_T *pMp4, const char *path, 
                      float fStart, float fDuration) {
  FILE_STREAM_T fStreamOut;
  MP4_TRAK_AVCC_T mp4BoxSet;
  AVCC_CBDATA_WRITE_SAMPLE_T avcDescr;
  uint64_t startHz = 0;
  uint64_t durationHz = 0;
  uint32_t sampledelta = 0;
  int rc = 0;

  if(!pMp4|| !pMp4->pStream || pMp4->pStream == FILEOPS_INVALID_FP) {
    return -1;
  }

  LOG(X_DEBUG("Extracting h.264 video from %s"), pMp4->pStream->cbGetName(pMp4->pStream));

  if(mp4_initAvcCTrack(pMp4, &mp4BoxSet) < 0) {
    return -1;
  }

  durationHz = (uint64_t) (fDuration * mp4BoxSet.tk.pMdhd->timescale);
  startHz = (uint64_t) (fStart * mp4BoxSet.tk.pMdhd->timescale);

  if((fStreamOut.fp = fileops_Open(path, O_RDWR | O_CREAT)) == FILEOPS_INVALID_FP) {
    LOG(X_CRITICAL("Unable to open file for writing: %s"), path);
    return -1;
  }
  fStreamOut.offset = 0;

  if((sampledelta = mp4_getSttsSampleDelta(&mp4BoxSet.tk.pStts->list)) == 0) {
    LOG(X_WARNING("Unable to get video sample timing from stts w/ %d entries"), 
                   mp4BoxSet.tk.pStts->list.entrycnt);
  }

  rc = avcc_writeSPSPPS(&fStreamOut, &mp4BoxSet.pAvcC->avcc,
                         sampledelta, mp4BoxSet.tk.pMdhd->timescale);
    
  
  if(rc >= 0) {
    avcDescr.lenNalSzField = mp4BoxSet.pAvcC->avcc.lenminusone + 1;
    avcDescr.pStreamOut = &fStreamOut;

    if(avcDescr.lenNalSzField <= 0 || avcDescr.lenNalSzField > 4) {
      LOG(X_ERROR("Invalid avcC start frame length field: %u"), avcDescr.lenNalSzField);
      fileops_Close(fStreamOut.fp);
      return -1;
    }

    rc = mp4_readSamplesFromTrack(&mp4BoxSet.tk, pMp4->pStream,  
                   avcc_cbWriteSample_mp4wrap, &avcDescr, startHz, durationHz);
  }

  fileops_Close(fStreamOut.fp);

  return rc;
}
Ejemplo n.º 4
0
static int mp4_extractAacAud(const MP4_CONTAINER_T *pMp4, const char *path, 
                      float fStart, float fDuration) {
  FILE_STREAM_T fStreamOut;
  MP4_TRAK_MP4A_T mp4BoxSet;
  ESDS_CBDATA_WRITE_SAMPLE_T esdsDescr;
  unsigned char buf[4096];
  uint64_t startHz = 0;
  uint64_t durationHz = 0;
  int rc = 0;

  if(!pMp4|| !pMp4->pStream || pMp4->pStream == FILEOPS_INVALID_FP) {
    return -1;
  }

  LOG(X_DEBUG("Extracting aac audio from %s"), pMp4->pStream->cbGetName(pMp4->pStream));

  esdsDescr.bs.buf = buf;
  esdsDescr.bs.sz = sizeof(buf);
  esdsDescr.bs.idx = 0;

  if(mp4_initMp4aTrack(pMp4, &mp4BoxSet) < 0) {
    return -1;
  }

  if(mp4BoxSet.pEsds->config.objtypeid != ESDS_OBJTYPE_MP4A) {
    LOG(X_ERROR("Unsupported mp4a esds object type descriptor: %u"), 
                mp4BoxSet.pEsds->config.objtypeid);
    return -1;
  } else if(esds_decodeDecoderSp(mp4BoxSet.pEsds->specific.startcodes, 
        (mp4BoxSet.pEsds->specific.type.length > sizeof(mp4BoxSet.pEsds->specific.startcodes) ?
        sizeof(mp4BoxSet.pEsds->specific.startcodes) : mp4BoxSet.pEsds->specific.type.length), 
        &esdsDescr.decoderCfg) != 0) {
    LOG(X_ERROR("Failed to decode mp4a esds decoder specific data for type descriptor: %u"), 
                mp4BoxSet.pEsds->config.type.descriptor);
    return -1;
  }

  durationHz = (uint64_t) (fDuration * mp4BoxSet.tk.pMdhd->timescale);
  startHz = (uint64_t) (fStart * mp4BoxSet.tk.pMdhd->timescale);

  if((fStreamOut.fp = fileops_Open(path, O_RDWR | O_CREAT)) == FILEOPS_INVALID_FP) {
    LOG(X_CRITICAL("Unable to open file for writing: %s"), path);
    return -1;
  }
  fStreamOut.offset = 0;

  esdsDescr.pStreamOut = &fStreamOut;
  rc = mp4_readSamplesFromTrack(&mp4BoxSet.tk, pMp4->pStream, esds_cbWriteSample_mp4wrap, 
                                &esdsDescr, startHz, durationHz);

  fileops_Close(fStreamOut.fp);

  return rc;
}
Ejemplo n.º 5
0
SRV_CONF_T *conf_parse(const char *path) {
  FILE_HANDLE fp;
  SRV_CONF_T *pConf = NULL;
  KEYVAL_PAIR_T kv;
  KEYVAL_PAIR_T *pKv = NULL;
  KEYVAL_PAIR_T *pKvPrev = NULL;
  char buf[1024];
  unsigned int numEntries = 0;

  if(!path) {
    return NULL;
  }

  if((fp = fileops_Open(path, O_RDONLY)) == FILEOPS_INVALID_FP) {
    LOG(X_CRITICAL("Unable to open file for reading: %s"), path);
    return NULL;
  }

  pConf = (SRV_CONF_T *) avc_calloc(1, sizeof(SRV_CONF_T));

  while(fileops_fgets(buf, sizeof(buf) - 1, fp)) {

    if(conf_parse_keyval(&kv, buf, strlen(buf), '=', 0) > 0) {

      if((pKv = (KEYVAL_PAIR_T *) avc_calloc(1, sizeof(KEYVAL_PAIR_T))) == NULL) {
        LOG(X_CRITICAL("Failed to allocate %d"), sizeof(KEYVAL_PAIR_T));
        break;
      }

      memcpy(pKv->key, kv.key, sizeof(pKv->key));
      memcpy(pKv->val, kv.val, sizeof(pKv->val));

      if(pKvPrev == NULL) {
        pConf->pKeyvals = pKv;
      } else {
        pKvPrev->pnext = pKv;
      }

      pKvPrev = pKv;
      numEntries++;

    }

  }

  fileops_Close(fp);

  return pConf;
}
Ejemplo n.º 6
0
static int mp4_extractMp4Vid(const MP4_CONTAINER_T *pMp4, const char *path, 
                      float fStart, float fDuration) {
  FILE_STREAM_T fStreamOut;
  MP4_TRAK_MP4V_T mp4BoxSet;
  MPG4V_CBDATA_WRITE_SAMPLE_T mpg4vDescr;
  uint64_t startHz = 0;
  uint64_t durationHz = 0;
  uint32_t sampledelta = 0;
  int rc = 0;

  if(!pMp4|| !pMp4->pStream || pMp4->pStream == FILEOPS_INVALID_FP) {
    return -1;
  }

  LOG(X_DEBUG("Extracting mpeg-4 video from %s"), pMp4->pStream->cbGetName(pMp4->pStream));

  if(mp4_initMp4vTrack(pMp4, &mp4BoxSet) < 0) {
    return -1;
  }

  durationHz = (uint64_t) (fDuration * mp4BoxSet.tk.pMdhd->timescale);
  startHz = (uint64_t) (fStart * mp4BoxSet.tk.pMdhd->timescale);

  if((fStreamOut.fp = fileops_Open(path, O_RDWR | O_CREAT)) == FILEOPS_INVALID_FP) {
    LOG(X_CRITICAL("Unable to open file for writing: %s"), path);
    return -1;
  }
  fStreamOut.offset = 0;

  if((sampledelta = mp4_getSttsSampleDelta(&mp4BoxSet.tk.pStts->list)) == 0) {
    LOG(X_WARNING("Unable to get video sample timing from stts w/ %d entries"), 
                   mp4BoxSet.tk.pStts->list.entrycnt);
  }

  rc = mpg4v_writeStart(&fStreamOut, mp4BoxSet.pEsds->specific.startcodes,
                        mp4BoxSet.pEsds->specific.type.length);

  if(rc >= 0) {
    mpg4vDescr.pStreamOut = &fStreamOut;

    rc = mp4_readSamplesFromTrack(&mp4BoxSet.tk, pMp4->pStream,  
                   mpg4v_cbWriteSample_mp4wrap, &mpg4vDescr, startHz, durationHz);
  }

  fileops_Close(fStreamOut.fp);

  return rc;
}
Ejemplo n.º 7
0
static int create_and_write_mpd(MPD_CREATE_CTXT_T *pCtxt, 
                                const MPD_ADAPTATION_T *pAdaptationList,
                                DASH_MPD_TYPE_T mpdType, 
                                int outidx,
                                int idxmax,
                                unsigned int mediaSequenceIndex,
                                int mediaSequenceMin) {
  int rc = 0;
  int idxbuf = 0;
  FILE_HANDLE fp;
  char buf[4096];
  char mpdpath[VSX_MAX_PATH_LEN];

  //
  // Create the .mpd xml file contents
  //
  if((idxbuf = mpd_doc_create(mpdType, pAdaptationList, pCtxt, 
                              outidx, mediaSequenceIndex, buf, sizeof(buf))) <= 0) {
    return -1;
  }

  //
  // Write the .mpd xml file to disk 
  //
  if((rc = write_mpd_path(pCtxt, outidx, mpdType, mpdpath, sizeof(mpdpath))) < 0) {
    return rc;
  }

  if((fp = fileops_Open(mpdpath, O_RDWR | O_CREAT)) == FILEOPS_INVALID_FP) {
    LOG(X_ERROR("Failed to open DASH mpd '%s' for writing"), mpdpath);
    return -1;
  }

  if((rc = fileops_WriteBinary(fp, (unsigned char *) buf, idxbuf)) < 0) {
    LOG(X_ERROR("Failed to write %d to DASH mpd '%s'"), idxbuf, mpdpath);
  }

  fileops_Close(fp);

  if(rc >= 0) {
    LOG(X_DEBUG("Updated DASH playlist '%s' (%d - %d)"), mpdpath, mediaSequenceMin, idxmax);
  } else {
    LOG(X_ERROR("Failed to update DASH playlist '%s' (%d - %d)"), mpdpath, mediaSequenceMin, idxmax);
  }

  return rc;
}
Ejemplo n.º 8
0
static int cpu_snapshot(CPU_SNAPSHOT_T *pS) {
  int rc = -1;

#if defined(__linux__)
  FILE_HANDLE fp;
  char buf[1024];
  const char *path = "/proc/stat";
  const char *p;
  int have_cpu = 0;

  if(!pS) {
    return -1;
  }

  memset(pS, 0, sizeof(CPU_SNAPSHOT_T));

  if((fp = fileops_Open(path, O_RDONLY)) == FILEOPS_INVALID_FP) {
    LOG(X_ERROR("Unable to open %s for reading. "ERRNO_FMT_STR), path, ERRNO_FMT_ARGS);
    return -1;
  }

  while(fileops_fgets(buf, sizeof(buf) -1, fp)) {
    if(!have_cpu && !strncmp(buf, "cpu ", 4)) {
      p = &buf[4];
      MOVE_WHILE_SPACE(p);
      sscanf(p, "%Lf %Lf %Lf %Lf", &pS->tmuser, &pS->tmnice, &pS->tmsys, &pS->tmidle);
      pS->tmtot = pS->tmuser + pS->tmnice + pS->tmsys + pS->tmidle;
      have_cpu = 1;
    } else if(!strncmp(buf, "cpu", 3)) {
      pS->numcpu++;
    }
  }

  fileops_Close(fp);

  if(have_cpu > 0) {
    rc = 0;
  }

#else // (__linux__)
  LOG(X_ERROR("CPU snapshot not implemented on this architecure"));
#endif // (__linux__)

  return rc;
}
Ejemplo n.º 9
0
static int http_mp4_init(CAP_HTTP_MP4_T *pCapHttpMp4) {
  int rc = 0;
  struct stat st;

  if(pCapHttpMp4->rcvState.fStream.filename[0] == '\0') {
    return -1;
  }

  //if((codecfmt_init(&pCapHttpMp4->av, RTMP_TMPFRAME_VID_SZ, RTMP_TMPFRAME_AUD_SZ)) < 0) {
  //  http_mp4_close(pCapHttpMp4);
  //  return -1;
  //}

  if(!(pCapHttpMp4->pMp4FromNet = mp4net_open(pCapHttpMp4))) {
    http_mp4_close(pCapHttpMp4);
    return -1;
  }

  //
  // Delete any prior temporary mp4 file
  //
  if(fileops_stat(pCapHttpMp4->rcvState.fStream.filename, &st) == 0) {
    fileops_DeleteFile(pCapHttpMp4->rcvState.fStream.filename);
  }

  if((pCapHttpMp4->rcvState.fStream.fp = fileops_Open(pCapHttpMp4->rcvState.fStream.filename, 
                                                      O_RDWR | O_CREAT)) == FILEOPS_INVALID_FP) {
    LOG(X_ERROR("Unable to open %s for writing"), pCapHttpMp4->rcvState.fStream.filename);
    http_mp4_close(pCapHttpMp4);
    return -1;
  }

  pCapHttpMp4->rcvState.fStream.offset = 0;
  pCapHttpMp4->rcvState.fStream.size = 0;

  //fprintf(stderr, "VID PQ maxPkts:%d, maxPktLen:%d, grow:%d,%d\n", pCapHttpMp4->client.pQVid->cfg.maxPkts, pCapHttpMp4->client.pQVid->cfg.maxPktLen, pCapHttpMp4->client.pQVid->cfg.growMaxPkts, pCapHttpMp4->client.pQVid->cfg.growMaxPktLen);

  return rc;
}
Ejemplo n.º 10
0
int mp4_extractRaw(const MP4_CONTAINER_T *pMp4, const char *outPrfx, 
                   float fStart, float fDuration, int overwrite,
                   int extractVid, int extractAud) {

  BOX_HDLR_T *pBoxHdlr;
  BOX_T *pBoxTrak;
  BOX_T *pBox;
  MP4_TRAK_T mp4Trak;
  char *outPath = NULL;
  size_t szPath;
  FILE_HANDLE fp;
  int rc = 0;

  if(!pMp4|| !pMp4->pStream || !pMp4->pStream->cbCheckFd(pMp4->pStream) ||
     !outPrfx) {
    return -1;
  } else if(!extractVid && !extractAud) {
    return -1;
  }

  if(!(pBoxTrak = mp4_findBoxInTree(pMp4->proot, *((uint32_t *) "moov"))) || 
     !(pBoxTrak = pBoxTrak->child)) {
    LOG(X_ERROR("No tracks found in mp4"));
    return -1;
  }

  while(pBoxTrak) {

    if(pBoxTrak->type != *((uint32_t *) "trak") ||
       !(pBoxHdlr = (BOX_HDLR_T *) mp4_findBoxInTree(pBoxTrak, *((uint32_t *) "hdlr")))) {

      pBoxTrak = pBoxTrak->pnext;
      continue;
    }

    memset(&mp4Trak, 0, sizeof(MP4_TRAK_T));
    mp4Trak.pTrak = pBoxTrak;
    pBox = fillTrack(&mp4Trak, 0);

    if(!pBox) {
      pBoxTrak = pBoxTrak->pnext;
      continue;
    }

    szPath = strlen(outPrfx);
    outPath = (char *) avc_calloc(1, szPath + 8);
    memcpy(outPath, outPrfx, szPath);
    rc = 0;

    if(pBoxHdlr->handlertype == *((uint32_t *) "soun") ||
       pBoxHdlr->handlertype == *((uint32_t *) "sdsm")) {

      if(extractAud) {
        if(mp4_findBoxInTree(pBoxTrak,  *((uint32_t *) "mp4a"))) {
          strncpy(&outPath[szPath], ".aac", 7);
        } else {
          LOG(X_WARNING("Unknown audio track %c%c%c%c written as raw output"), 
                ((unsigned char *)&pBox->type)[0], ((unsigned char *)&pBox->type)[1],
                ((unsigned char *)&pBox->type)[2], ((unsigned char *)&pBox->type)[3]);
          strncpy(&outPath[szPath], ".araw", 7);
        }
      } else {
        pBox = NULL;
      }    

    } else if(pBoxHdlr->handlertype == *((uint32_t *) "vide")) {

      if(extractVid) {
        if(mp4_findBoxInTree(pBoxTrak,  *((uint32_t *) "avc1"))) {
          strncpy(&outPath[szPath], ".h264", 7);
        } else if(mp4_findBoxInTree(pBoxTrak,  *((uint32_t *) "mp4v"))) {
          strncpy(&outPath[szPath], ".mpg4", 7);
        } else {
          LOG(X_WARNING("Unknown video track %c%c%c%c written as raw output"), 
                ((unsigned char *)&pBox->type)[0], ((unsigned char *)&pBox->type)[1],
                ((unsigned char *)&pBox->type)[2], ((unsigned char *)&pBox->type)[3]);
          strncpy(&outPath[szPath], ".vraw", 7);
        }
      } else {
        pBox = NULL;
      }    

    } else {
      pBox = NULL; 
    }

    if(pBox) {

      if(!overwrite && (fp = fileops_Open(outPath, O_RDONLY)) != FILEOPS_INVALID_FP) {
         fileops_Close(fp);
         LOG(X_ERROR("File %s already exists.  Will not overwrite."), outPath);
         free(outPath);
         pBoxTrak = pBoxTrak->pnext;
         continue;
      }

      if(pBox->type == *((uint32_t *) "avc1")) {
        rc = mp4_extractAvcVid(pMp4, outPath, fStart, fDuration);
      } else if(pBox->type == *((uint32_t *) "mp4v")) {
        rc = mp4_extractMp4Vid(pMp4, outPath, fStart, fDuration);
      } else if(pBox->type == *((uint32_t *) "mp4a")) {
        rc = mp4_extractAacAud(pMp4, outPath, fStart, fDuration);
      } else {
        rc = extractGenericTrak(pMp4, &mp4Trak, outPath, fStart, fDuration);
      }

      if(rc == 0) {
        LOG(X_INFO("Created %s"), outPath);
      }
      free(outPath);

    }

    pBoxTrak = pBoxTrak->pnext;
  }


  return rc;
} 
Ejemplo n.º 11
0
static int httplive_writepl_multibr(const char *path, const HTTPLIVE_DATA_T *pLiveArg) {
  FILE_HANDLE fp;
  int sz = 0;
  int rc = 0;
  char buf[4096];
  char tmp[32];
  int progId = 1;
  unsigned int bitrate;
  const char *uriprfxdelimeter = "";
  const HTTPLIVE_DATA_T *pLive = pLiveArg;

  if((rc = snprintf(&buf[sz], sizeof(buf) - sz, "#EXTM3U\r\n")) > 0) {
    sz += rc; 
  }

  while(pLive) {

    if((rc = snprintf(&buf[sz], sizeof(buf) - sz, 
                      "#%s:PROGRAM-ID=%d", HTTPLIVE_EXTX_STREAM_INFO, progId)) > 0) {
      sz += rc; 
    }

#define HTTPLIVE_BANDWIDTH(bw) (bw>1000 ? bw/1000*1000 : bw>100 ? bw/100*100 : bw>10 ? bw/10*10 : bw)

    bitrate = pLive->publishedBitrate ? pLive->publishedBitrate : pLive->autoBitrate;

    if(bitrate > 0) {
      if((rc = snprintf(&buf[sz], sizeof(buf) - sz, 
                      ", BANDWIDTH=%u", HTTPLIVE_BANDWIDTH(bitrate))) > 0) {
        sz += rc; 
      }
    }

    if((rc = snprintf(&buf[sz], sizeof(buf) - sz, "\r\n")) > 0) {
      sz += rc; 
    }

    if((rc = strlen(pLive->uriprefix)) > 0 && pLive->uriprefix[rc - 1] != '/') {
      uriprfxdelimeter = "/";
    } 

    if(pLive->outidx > 0) {
      snprintf(tmp, sizeof(tmp), "%d", pLive->outidx + 1);
    } else {
      tmp[0] = '\0';
    }

    if((rc = snprintf(&buf[sz], sizeof(buf) - sz, "%s%s%s%s%s\r\n",
          (pLive->uriprefix[0] != '\0' ? pLive->uriprefix : ""), 
          uriprfxdelimeter,
          tmp,
          //&pLive->fileprefix[pLive->outidx > 0 ? 1 : 0], HTTPLIVE_PL_NAME_EXT)) < 0) {
          HTTPLIVE_TS_NAME_PRFX, HTTPLIVE_PL_NAME_EXT)) < 0) {
      break;
    } else {
      sz += rc;
    }

    pLive = pLive->pnext;
  }

  if(rc >= 0) {

    if((fp = fileops_Open(path, O_RDWR | O_CREAT)) == FILEOPS_INVALID_FP) {
      LOG(X_ERROR("Failed to open '%s' for writing"), path);
      return -1;
    }

    if((rc = fileops_WriteBinary(fp, (unsigned char *) buf, sz)) < 0) {
      LOG(X_ERROR("Failed to write %d to '%s'"), sz, path);
    }

    fileops_Close(fp);

  }

  if(rc >= 0) {
    LOG(X_DEBUG("Updated master playlist '%s'"), path);
  } else {
    LOG(X_ERROR("Failed to update master playlist '%s'"), path);
  }

  return rc;
}
Ejemplo n.º 12
0
static int mem_snapshot(MEM_SNAPSHOT_T *pM, TIME_VAL tmnow) {
  int rc = -1;
 
#if defined(__linux__)
  FILE_HANDLE fp;
  char buf[1024];
  char *p;
  const char *path = "/proc/meminfo";
  int haveCached = 0;
  int haveBuffers = 0;
  int haveFree = 0;

  if(!pM) {
    return -1;
  }

  memset(pM, 0, sizeof(MEM_SNAPSHOT_T));

  if((fp = fileops_Open(path, O_RDONLY)) == FILEOPS_INVALID_FP) {
    LOG(X_ERROR("Unable to open %s for reading. "ERRNO_FMT_STR), path, ERRNO_FMT_ARGS);
    return -1;
  }

  while(fileops_fgets(buf, sizeof(buf) -1, fp)) {

    if(!strncasecmp(buf, "MemTotal:", 9)) {
      p = &buf[9];
      MOVE_WHILE_SPACE(p);
      pM->memTotal = strutil_read_numeric(p, 0, 0, 0);
    } else if(!strncasecmp(buf, "MemFree:", 8)) {
      p = &buf[8];
      MOVE_WHILE_SPACE(p);
      pM->memFree = strutil_read_numeric(p, 0, 0, 0);
      haveFree = 1;
    } else if(!strncasecmp(buf, "Buffers:", 8)) {
      p = &buf[8];
      MOVE_WHILE_SPACE(p);
      pM->buffers = strutil_read_numeric(p, 0, 0, 0);
      haveBuffers = 1;
    } else if(!strncasecmp(buf, "Cached:", 7)) {
      p = &buf[7];
      MOVE_WHILE_SPACE(p);
      pM->cached = strutil_read_numeric(p, 0, 0, 0);
      haveCached = 1;
    }

    if(pM->memTotal > 0 && haveFree  && haveBuffers && haveCached) {
      pM->memUsed = pM->memTotal - pM->memFree;
      pM->tvsnapshot = tmnow;
      rc = 0;
      break;
    }
  }

  fileops_Close(fp);

#else // (__linux__)
  LOG(X_ERROR("Memory snapshot not implemented on this architecure"));
#endif // (__linux__)

  return rc;
}
Ejemplo n.º 13
0
int devtype_loadcfg(const char *path) {
  STREAM_DEVICE_T *pdev = NULL;
  STREAM_DEVICE_T *pdevprev = NULL;
  FILE_HANDLE fp;
  char buf[1024];
  const char *p;
  int linenum = 1;
  int count = 0;
  PARSE_ENTRY_DATA_T parseData;

  if(!path) {
    return -1;
  }

  if(g_devtypes) {
    devtype_free(g_devtypes);
    g_devtypes = NULL;
  }

  if((fp = fileops_Open(path, O_RDONLY)) == FILEOPS_INVALID_FP) {
    LOG(X_ERROR("Unable to open metafile for reading: %s"), path);
    return -1;
  }

  while(fileops_fgets(buf, sizeof(buf) - 1, fp)) {

    p = buf;
    while(*p == ' ' || *p == '\t') {
      p++;
    }

    if(*p == '#') {
      continue;
    }

    memset(&parseData, 0, sizeof(parseData));
    if(strutil_parse_csv(cbparse_entry_devtype, &parseData, p) < 0) {
      LOG(X_ERROR("Failed to parse line %d in file %s"), linenum, path);
      devtype_free(g_devtypes);
      break;
    } else if((parseData.flags & PARSE_FLAG_HAVE_ALL)) { 
      if(!(pdev = create_device(parseData.devname, parseData.strmatch,
                           parseData.strmatch2, parseData.methods,
                           parseData.devtype))) {
        LOG(X_ERROR("Failed to create device config from line %d"), linenum);
        devtype_free(g_devtypes);
        break;
      } else if(pdevprev) {
        pdevprev->pnext = pdev;
      } else {
        g_devtypes = pdev;
      }
      pdevprev = pdev;
      count++;

    } else if(strlen(p) > 1) {
      LOG(X_WARNING("Incomplete line %d in file %s"), linenum, path);
    }

    

    linenum++;
  }

  fileops_Close(fp);

  LOG(X_DEBUG("Read %d device profiles from %s"), count, path);

  //devtype_dump(g_devtypes);

  return count;
}
Ejemplo n.º 14
0
int httplive_cbOnPkt(void *pUserData, const unsigned char *pPktData, unsigned int len) {
  int rc = 0, rc2;
  struct timeval tv;
  char filename[VSX_MAX_PATH_LEN];
  unsigned int idxMin = 0;
  int keepIdx;
  HTTPLIVE_DATA_T *pLive = (HTTPLIVE_DATA_T *) pUserData;

  if(!pLive || !pPktData) {
    return -1;
  }

  //fprintf(stderr, "httplive_cbOnPkt len:%d, outidx[%d]\n", len, pLive->outidx);

  if(len == 0) {
    LOG(X_WARNING("httplive cb called with 0 length"));
    return 0;  
  }

  gettimeofday(&tv, NULL);

  if(pLive->tvNextRoll.tv_sec == 0 ||
     TIME_TV_DIFF_MS(tv, pLive->tvNextRoll) >= (long) (pLive->duration * 1000.0f)) {

    if(pLive->fs.fp != FILEOPS_INVALID_FP) {
      //LOG(X_DEBUG("HTTPLIVE CLOSING fp:0x%x, fileno:%d %s"), pLive->fs.fp, pLive->fs.fp ? fileno(pLive->fs.fp) : -99, pLive->fs.filename);
      closeFp(pLive);
      //LOG(X_DEBUG("HTTPLIVE CLOSED fp:0x%x, fileno:%d %s"), pLive->fs.fp, pLive->fs.fp ? fileno(pLive->fs.fp) : -99, pLive->fs.filename);
    }

    keepIdx = HTTPLIVE_NUM_INDEXES_KEEP(pLive);

    if(pLive->curIdx > keepIdx + 2) {
      idxMin = pLive->curIdx - keepIdx - 2;
      httplive_purgetsfiles(pLive, idxMin);
    }

    //
    // Update MPEG-DASH playlist using .ts segments
    //
    if(pLive->pMpdMp2tsCtxt && pLive->curIdx > 0) {
      on_new_ts(pLive->curIdx-1, idxMin, pLive->outidx, 
                pLive->dir, pLive->curIdx,
               //pLive->fs.filename, 
                pLive->pMpdMp2tsCtxt, 90000, (pLive->duration * 90000), 
                TIME_TV_DIFF_MS(pLive->tvPriorRoll, pLive->tvRoll0) * 90);
    }

    httplive_format_path(filename, sizeof(filename), pLive->fileprefix, pLive->curIdx);
    mediadb_prepend_dir(pLive->dir, filename, pLive->fs.filename, sizeof(pLive->fs.filename));

    if((pLive->fs.fp = fileops_Open(pLive->fs.filename, O_RDWR | O_CREAT)) == FILEOPS_INVALID_FP) {
      LOG(X_ERROR("Failed to open '%s' for writing"), pLive->fs.filename);
      rc = -1;    
    } else {

      LOG(X_DEBUG("Opened '%s'"), pLive->fs.filename);

      rc = httplive_updatepl(pLive);

    }

    pLive->curIdx++;
    if(pLive->tvNextRoll.tv_sec == 0) {
      TIME_TV_SET(pLive->tvNextRoll, tv);
      TIME_TV_SET(pLive->tvRoll0, tv);
    }
    //fprintf(stderr, "TV  at %u:%u\n", tv.tv_sec, tv.tv_usec);
    
    TIME_TV_SET(pLive->tvPriorRoll, pLive->tvNextRoll);
    TV_INCREMENT_MS(pLive->tvNextRoll, (pLive->duration * 1000.0f));
    //fprintf(stderr, "TV next at %u:%u, msElapsed:%u, idxMin:%d, curIdx:%d\n", pLive->tvNextRoll.tv_sec, pLive->tvNextRoll.tv_usec, TIME_TV_DIFF_MS(pLive->tvNextRoll, pLive->tvRoll0), idxMin, pLive->curIdx);

  }

  //LOG(X_DEBUG("HTTPLIVE WRITE  %d fp:0x%x, pPktData:0x%x, fileno:%d %s"), len, pLive->fs.fp, pPktData, pLive->fs.fp ? fileno(pLive->fs.fp) : -99, pLive->fs.filename);
  if(pLive->fs.fp != FILEOPS_INVALID_FP &&
     (rc2 = fileops_WriteBinary(pLive->fs.fp, (unsigned char *) pPktData, len)) != len) {
    LOG(X_ERROR("Failed to write %d/%d to output ts %s"), rc2, len, pLive->fs.filename);

    //LOG(X_DEBUG("HTTPLIVE WRITE ERROR %d, %d fp:0x%x, pPktData:0x%x, fileno:%d %s"), rc2, len, pLive->fs.fp, pPktData, pLive->fs.fp ? fileno(pLive->fs.fp) : -99, pLive->fs.filename);
    return -1;
  }


  return rc;
}
Ejemplo n.º 15
0
int metafile_findprofs(const char *path, const char *devname, 
                      char profs[][META_FILE_PROFILESTR_MAX], unsigned int max) {
  int rc = 0;
  FILE_HANDLE fp;
  char *p;
  unsigned int idx = 0;
  PARSE_ENTRY_DATA_T parseData;
  char buf[2048];
  PROFILE_LIST_ALL_T foundProfs;

  if(!path || !profs) {
    return -1;
  }  

  if((fp = fileops_Open(path, O_RDONLY)) == FILEOPS_INVALID_FP) {
    LOG(X_ERROR("Unable to open metafile for reading profiles: %s"), path);
    return -1;
  }

  VSX_DEBUG_METAFILE( LOG(X_DEBUG("META - metafile_findprofs: '%s', devname: '%s'"), path, devname));

  memset(&foundProfs, 0, sizeof(foundProfs));
  memset(&parseData, 0, sizeof(parseData));
  parseData.path = path;

  while(fileops_fgets(buf, sizeof(buf) - 1, fp)) {

    parseData.linenum++;

    p = buf;
    while(*p == ' ' || *p == '\t') {
      p++;
    }

    if(*p == '#') {
      continue;
    } 

    //
    // Reset the parse context storage
    //
    reset_parsedata_ctxt(&parseData);

    strutil_parse_csv(cbparse_entry_metafile, &parseData, p);

    if(parseData.flags == 0) {
      continue;
    }

    check_profs(&parseData, devname, &foundProfs, path);

  }

  fileops_Close(fp);

  rc = 0;
  for(idx = 0; idx < foundProfs.profs_devmatch.count && idx < max; idx++) {
    if(!find_prof(foundProfs.profs_devmatch.profiles[idx], profs, rc)) { 
      strncpy(profs[rc], foundProfs.profs_devmatch.profiles[idx], 
            META_FILE_PROFILESTR_MAX);
//fprintf(stderr, "ADDING DEVMATCH[%d] '%s'\n", rc, profs[rc]);
      rc++;
    }
  }  

  if(foundProfs.profs_devmatch.count == 0 || !foundProfs.profs_devmatch.haveCatchAll) {
    for(idx = 0; idx < foundProfs.profs_nodevmatch.count && idx < max; idx++) {
      if(!find_prof(foundProfs.profs_nodevmatch.profiles[idx], profs, rc)) { 
        strncpy(profs[rc], foundProfs.profs_nodevmatch.profiles[idx], 
                META_FILE_PROFILESTR_MAX);
//fprintf(stderr, "ADDING NODEVMATCH[%d] '%s'\n", rc, profs[rc]);
        rc++;
      }  
    }
  }

  return rc;
}
Ejemplo n.º 16
0
int metafile_open(const char *path, 
                  META_FILE_T *pMetaFile, 
                  int readIgnores,
                  int readTitles) {
 
  int rc = 0;
  FILE_HANDLE fp;
  char *p;
  int match = 0;
  PARSE_ENTRY_DATA_T parseData;
  ENTRY_IGNORE_T *pIgnore;
  ENTRY_IGNORE_T *pIgnorePrev = NULL;
  ENTRY_META_DESCRIPTION_T *pDesc;
  ENTRY_META_DESCRIPTION_T *pDescPrev = NULL;
  char buf[1024];

  if(!path || !pMetaFile) {
    return -1;
  }  

  if((fp = fileops_Open(path, O_RDONLY)) == FILEOPS_INVALID_FP) {
    LOG(X_ERROR("Unable to open metafile for reading: %s (ignore list:%d)"), 
        path, readIgnores);
    return -1;
  }

  VSX_DEBUG_METAFILE( LOG(X_DEBUG("META - metafile_open: '%s', readIgnores: %d, readTitles: %d"), 
                      path, readIgnores, readTitles));

  pMetaFile->filestr[0] = '\0';
  pMetaFile->linkstr[0] = '\0';
  pMetaFile->instr[0] = '\0';
  pMetaFile->xcodestr[0] = '\0';
  pMetaFile->userpass[0] = '\0';
  pMetaFile->methodBits = 0;

  memset(&parseData, 0, sizeof(parseData));
  parseData.path = path;

  while(fileops_fgets(buf, sizeof(buf) - 1, fp)) {

    parseData.linenum++;
    p = buf;
    while(*p == ' ' || *p == '\t') {
      p++;
    }

    if(*p == '#' || *p == '\r' || *p == '\n') {
      continue;
    } 

    //
    // Reset the parse context storage
    //
    reset_parsedata_ctxt(&parseData);

    //
    // We allow multiple configuration items on one line, separated by comma
    //
    rc = strutil_parse_csv(cbparse_entry_metafile, &parseData, p);

    if(parseData.flags == 0) {
      continue;
    }

    if(!(readIgnores || readTitles)) {
      handle_parsed_line(&parseData, pMetaFile, path, &match);
    }

    //
    // We're only interested in reading the ignore list
    //
    if(readIgnores && parseData.ignore[0] != '\0') {
      if(!(pIgnore = (ENTRY_IGNORE_T *) avc_calloc(1, sizeof(ENTRY_IGNORE_T)))) {
        rc = -1;
        break;
      }
      strncpy(pIgnore->filename, parseData.ignore, sizeof(pIgnore->filename));

      if(pIgnorePrev) {
        pIgnorePrev->pnext = pIgnore;
      } else {
        pMetaFile->pignoreList = pIgnore;
      }
      pIgnorePrev = pIgnore;
    }

    //
    // We're only interested in reading the resource title name
    //
    if(readTitles && parseData.title[0] != '\0' && parseData.filename[0] != '\0') {

      if(!(pDesc = (ENTRY_META_DESCRIPTION_T *) avc_calloc(1, sizeof(ENTRY_META_DESCRIPTION_T)))) {
        rc = -1;
        break;
      }

      strncpy(pDesc->title, parseData.title, sizeof(pDesc->title));
      strncpy(pDesc->filename, parseData.filename, sizeof(pDesc->filename));
      if(pDescPrev) {
        pDescPrev->pnext = pDesc;
      } else {
        pMetaFile->pDescriptionList = pDesc;
      }
      pDescPrev = pDesc;
    }

  }

  fileops_Close(fp);

  VSX_DEBUG_METAFILE( 
    LOG(X_DEBUG("META - metafile_open done '%s', meta-devicefilter:'%s', meta-profilefilter: '%s' returning rc: %d, "
                "file: '%s', link: '%s', input: '%s', xcode: '%s', digestauth: '%s', methods: '%s', id: '%s'" ), 
                 path, pMetaFile->devicefilterstr, pMetaFile->profilefilterstr, rc, pMetaFile->filestr, 
                 pMetaFile->linkstr, pMetaFile->instr, pMetaFile->xcodestr, pMetaFile->userpass,
                 devtype_dump_methods(pMetaFile->methodBits, buf, sizeof(buf)), pMetaFile->id));

  return rc;
}
Ejemplo n.º 17
0
static int httplive_writepl(const char *path, int idxmin, int idxmax, HTTPLIVE_DATA_T *pLive) {
  FILE_HANDLE fp;
  int idx;
  int sz = 0;
  int rc = 0;
  char filename[128];
  char buf[4096];
  const char *uriprfxdelimeter = "";
  unsigned int duration = (unsigned int) pLive->duration;
  
  if(pLive->duration > (float) duration) {
    duration++;
  }

  if((rc = snprintf(buf, sizeof(buf), 
                "#EXTM3U\r\n#%s:%d\r\n#%s:%d\r\n",
                HTTPLIVE_EXTX_TARGET_DURATION, duration,
                HTTPLIVE_EXTX_MEDIA_SEQUENCE, idxmin)) > 0) {
    sz += rc; 

    if(rc >= 0 && (rc = snprintf(&buf[sz], sizeof(buf) - sz, "#EXT-X-ALLOW-CACHE:NO\r\n")) > 0) {
      sz += rc;
    }

#if defined(HTTPLIVE_INCLUDE_PROGRAM_DATE_TIME)
    char tmbuf[256];
    time_t tm = s_httplive_tm + (pLive->curIdx * duration);
    strftime(tmbuf, sizeof(tmbuf) - 1, "%Y-%m-%dT%H:%M:%S+08:00", gmtime(&tm));
    //fprintf(stderr, "TM:%u, %u, %d * %d '%s'\n", tm, s_httplive_tm, pLive->curIdx, duration, tmbuf);
    if(rc >= 0 && (rc = snprintf(&buf[sz], sizeof(buf) - sz, "#EXT-X-PROGRAM-DATE-TIME:%s\r\n", tmbuf)) > 0) {
      sz += rc;
    }
#endif // HTTPLIVE_INCLUDE_PROGRAM_DATE_TIME

    if(pLive->uriprefix && (rc = strlen(pLive->uriprefix)) > 0 && pLive->uriprefix[rc - 1] != '/') {
      uriprfxdelimeter = "/";
    } 

    for(idx = idxmin; idx <= idxmax; idx++) {

      httplive_format_path(filename, sizeof(filename), pLive->fileprefix, idx);

      if((rc = snprintf(&buf[sz], sizeof(buf) - sz, "#EXTINF:%d,\r\n%s%s%s\r\n"
          ,duration, 
          (pLive->uriprefix ? pLive->uriprefix : ""), 
          uriprfxdelimeter, filename)) < 0) {
        break;
      } else {
        sz += rc;
      }
    }

  }

  //
  // For live streams, do not end with #EXT-X-ENDLIST
  //

  if(rc >= 0) {

    if((fp = fileops_Open(path, O_RDWR | O_CREAT)) == FILEOPS_INVALID_FP) {
      LOG(X_ERROR("Failed to open httplive playlist '%s' for writing"), path);
      return -1;
    }

    if((rc = fileops_WriteBinary(fp, (unsigned char *) buf, sz)) < 0) {
      LOG(X_ERROR("Failed to write %d to httplive playlist '%s'"), sz, path);
    }

    fileops_Close(fp);

  }

  if(rc >= 0) {
    LOG(X_DEBUG("Updated httplive playlist '%s' (%d - %d)"), path, idxmin, idxmax);
  } else {
    LOG(X_ERROR("Failed to update httplive playlist '%s' (%d - %d)"), path, idxmin, idxmax);
  }

  return rc;
}