MatroskaFileServerDemux.cpp 5.08 KB
Newer Older
1 2 3
/**********
This library is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the
4
Free Software Foundation; either version 3 of the License, or (at your
5 6 7 8 9 10 11 12 13 14 15 16
option) any later version. (See <http://www.gnu.org/copyleft/lesser.html>.)

This library is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for
more details.

You should have received a copy of the GNU Lesser General Public License
along with this library; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
**********/
// "liveMedia"
17
// Copyright (c) 1996-2019 Live Networks, Inc.  All rights reserved.
18 19 20 21 22
// A server demultiplexor for a Matroska file
// Implementation

#include "MatroskaFileServerDemux.hh"
#include "MP3AudioMatroskaFileServerMediaSubsession.hh"
23
#include "MatroskaFileServerMediaSubsession.hh"
24 25

void MatroskaFileServerDemux
26 27 28 29 30 31
::createNew(UsageEnvironment& env, char const* fileName,
	    onCreationFunc* onCreation, void* onCreationClientData,
	    char const* preferredLanguage) {
  (void)new MatroskaFileServerDemux(env, fileName,
				    onCreation, onCreationClientData,
				    preferredLanguage);
32 33 34
}

ServerMediaSubsession* MatroskaFileServerDemux::newServerMediaSubsession() {
35 36 37 38 39 40
  unsigned dummyResultTrackNumber;
  return newServerMediaSubsession(dummyResultTrackNumber);
}

ServerMediaSubsession* MatroskaFileServerDemux
::newServerMediaSubsession(unsigned& resultTrackNumber) {
41
  ServerMediaSubsession* result;
42
  resultTrackNumber = 0;
43 44

  for (result = NULL; result == NULL && fNextTrackTypeToCheck != MATROSKA_TRACK_TYPE_OTHER; fNextTrackTypeToCheck <<= 1) {
45 46 47
    if (fNextTrackTypeToCheck == MATROSKA_TRACK_TYPE_VIDEO) resultTrackNumber = fOurMatroskaFile->chosenVideoTrackNumber();
    else if (fNextTrackTypeToCheck == MATROSKA_TRACK_TYPE_AUDIO) resultTrackNumber = fOurMatroskaFile->chosenAudioTrackNumber();
    else if (fNextTrackTypeToCheck == MATROSKA_TRACK_TYPE_SUBTITLE) resultTrackNumber = fOurMatroskaFile->chosenSubtitleTrackNumber();
48

49
    result = newServerMediaSubsessionByTrackNumber(resultTrackNumber);
50 51 52 53 54
  }

  return result;
}

55 56
ServerMediaSubsession* MatroskaFileServerDemux
::newServerMediaSubsessionByTrackNumber(unsigned trackNumber) {
57 58 59 60 61
  MatroskaTrack* track = fOurMatroskaFile->lookup(trackNumber);
  if (track == NULL) return NULL;

  // Use the track's "codecID" string to figure out which "ServerMediaSubsession" subclass to use:
  ServerMediaSubsession* result = NULL;
62 63 64 65
  if (strcmp(track->mimeType, "audio/MPEG") == 0) {
    result = MP3AudioMatroskaFileServerMediaSubsession::createNew(*this, track);
  } else {
    result = MatroskaFileServerMediaSubsession::createNew(*this, track);
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
  }

  if (result != NULL) {
#ifdef DEBUG
    fprintf(stderr, "Created 'ServerMediaSubsession' object for track #%d: %s (%s)\n", track->trackNumber, track->codecID, track->mimeType);
#endif
  }

  return result;
}

FramedSource* MatroskaFileServerDemux::newDemuxedTrack(unsigned clientSessionId, unsigned trackNumber) {
  MatroskaDemux* demuxToUse = NULL;

  if (clientSessionId != 0 && clientSessionId == fLastClientSessionId) {
    demuxToUse = fLastCreatedDemux; // use the same demultiplexor as before
      // Note: This code relies upon the fact that the creation of streams for different
      // client sessions do not overlap - so all demuxed tracks are created for one "MatroskaDemux" at a time.
      // Also, the "clientSessionId != 0" test is a hack, because 'session 0' is special; its audio and video streams
      // are created and destroyed one-at-a-time, rather than both streams being
      // created, and then (later) both streams being destroyed (as is the case
      // for other ('real') session ids).  Because of this, a separate demultiplexor is used for each 'session 0' track.
  }

  if (demuxToUse == NULL) demuxToUse = fOurMatroskaFile->newDemux();

  fLastClientSessionId = clientSessionId;
  fLastCreatedDemux = demuxToUse;

95
  return demuxToUse->newDemuxedTrackByTrackNumber(trackNumber);
96 97 98
}

MatroskaFileServerDemux
99 100 101
::MatroskaFileServerDemux(UsageEnvironment& env, char const* fileName,
			  onCreationFunc* onCreation, void* onCreationClientData,
			  char const* preferredLanguage)
102 103 104
  : Medium(env),
    fFileName(fileName), fOnCreation(onCreation), fOnCreationClientData(onCreationClientData),
    fNextTrackTypeToCheck(0x1), fLastClientSessionId(0), fLastCreatedDemux(NULL) {
105
  MatroskaFile::createNew(env, fileName, onMatroskaFileCreation, this, preferredLanguage);
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
}

MatroskaFileServerDemux::~MatroskaFileServerDemux() {
  Medium::close(fOurMatroskaFile);
}

void MatroskaFileServerDemux::onMatroskaFileCreation(MatroskaFile* newFile, void* clientData) {
  ((MatroskaFileServerDemux*)clientData)->onMatroskaFileCreation(newFile);
}

void MatroskaFileServerDemux::onMatroskaFileCreation(MatroskaFile* newFile) {
  fOurMatroskaFile = newFile;

  // Now, call our own creation notification function:
  if (fOnCreation != NULL) (*fOnCreation)(this, fOnCreationClientData);
}