liveMedia/Media.cpp

Go to the documentation of this file.
00001 /**********
00002 This library is free software; you can redistribute it and/or modify it under
00003 the terms of the GNU Lesser General Public License as published by the
00004 Free Software Foundation; either version 2.1 of the License, or (at your
00005 option) any later version. (See <http://www.gnu.org/copyleft/lesser.html>.)
00006 
00007 This library is distributed in the hope that it will be useful, but WITHOUT
00008 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00009 FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for
00010 more details.
00011 
00012 You should have received a copy of the GNU Lesser General Public License
00013 along with this library; if not, write to the Free Software Foundation, Inc.,
00014 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
00015 **********/
00016 // "liveMedia"
00017 // Copyright (c) 1996-2010 Live Networks, Inc.  All rights reserved.
00018 // Media
00019 // Implementation
00020 
00021 #include "Media.hh"
00022 #include "HashTable.hh"
00023 
00024 // A data structure for looking up a Medium by its string name
00025 class MediaLookupTable {
00026 public:
00027   static MediaLookupTable* ourMedia(UsageEnvironment& env);
00028 
00029   Medium* lookup(char const* name) const;
00030       // Returns NULL if none already exists
00031 
00032   void addNew(Medium* medium, char* mediumName);
00033   void remove(char const* name);
00034 
00035   void generateNewName(char* mediumName, unsigned maxLen);
00036 
00037 protected:
00038   MediaLookupTable(UsageEnvironment& env);
00039   virtual ~MediaLookupTable();
00040 
00041 private:
00042   UsageEnvironment& fEnv;
00043   HashTable* fTable;
00044   unsigned fNameGenerator;
00045 };
00046 
00047 
00049 
00050 Medium::Medium(UsageEnvironment& env)
00051         : fEnviron(env), fNextTask(NULL) {
00052   // First generate a name for the new medium:
00053   MediaLookupTable::ourMedia(env)->generateNewName(fMediumName, mediumNameMaxLen);
00054   env.setResultMsg(fMediumName);
00055 
00056   // Then add it to our table:
00057   MediaLookupTable::ourMedia(env)->addNew(this, fMediumName);
00058 }
00059 
00060 Medium::~Medium() {
00061   // Remove any tasks that might be pending for us:
00062   fEnviron.taskScheduler().unscheduleDelayedTask(fNextTask);
00063 }
00064 
00065 Boolean Medium::lookupByName(UsageEnvironment& env, char const* mediumName,
00066                                   Medium*& resultMedium) {
00067   resultMedium = MediaLookupTable::ourMedia(env)->lookup(mediumName);
00068   if (resultMedium == NULL) {
00069     env.setResultMsg("Medium ", mediumName, " does not exist");
00070     return False;
00071   }
00072 
00073   return True;
00074 }
00075 
00076 void Medium::close(UsageEnvironment& env, char const* name) {
00077   MediaLookupTable::ourMedia(env)->remove(name);
00078 }
00079 
00080 void Medium::close(Medium* medium) {
00081   if (medium == NULL) return;
00082 
00083   close(medium->envir(), medium->name());
00084 }
00085 
00086 Boolean Medium::isSource() const {
00087   return False; // default implementation
00088 }
00089 
00090 Boolean Medium::isSink() const {
00091   return False; // default implementation
00092 }
00093 
00094 Boolean Medium::isRTCPInstance() const {
00095   return False; // default implementation
00096 }
00097 
00098 Boolean Medium::isRTSPClient() const {
00099   return False; // default implementation
00100 }
00101 
00102 Boolean Medium::isRTSPServer() const {
00103   return False; // default implementation
00104 }
00105 
00106 Boolean Medium::isMediaSession() const {
00107   return False; // default implementation
00108 }
00109 
00110 Boolean Medium::isServerMediaSession() const {
00111   return False; // default implementation
00112 }
00113 
00114 Boolean Medium::isDarwinInjector() const {
00115   return False; // default implementation
00116 }
00117 
00118 
00120 
00121 _Tables* _Tables::getOurTables(UsageEnvironment& env, Boolean createIfNotPresent) {
00122   if (env.liveMediaPriv == NULL && createIfNotPresent) {
00123     env.liveMediaPriv = new _Tables(env);
00124   }
00125   return (_Tables*)(env.liveMediaPriv);
00126 }
00127 
00128 void _Tables::reclaimIfPossible() {
00129   if (mediaTable == NULL && socketTable == NULL) {
00130     fEnv.liveMediaPriv = NULL;
00131     delete this;
00132   }
00133 }
00134 
00135 _Tables::_Tables(UsageEnvironment& env)
00136   : mediaTable(NULL), socketTable(NULL), fEnv(env) {
00137 }
00138 
00139 _Tables::~_Tables() {
00140 }
00141 
00142 
00144 
00145 MediaLookupTable* MediaLookupTable::ourMedia(UsageEnvironment& env) {
00146   _Tables* ourTables = _Tables::getOurTables(env);
00147   if (ourTables->mediaTable == NULL) {
00148     // Create a new table to record the media that are to be created in
00149     // this environment:
00150     ourTables->mediaTable = new MediaLookupTable(env);
00151   }
00152   return (MediaLookupTable*)(ourTables->mediaTable);
00153 }
00154 
00155 Medium* MediaLookupTable::lookup(char const* name) const {
00156   return (Medium*)(fTable->Lookup(name));
00157 }
00158 
00159 void MediaLookupTable::addNew(Medium* medium, char* mediumName) {
00160   fTable->Add(mediumName, (void*)medium);
00161 }
00162 
00163 void MediaLookupTable::remove(char const* name) {
00164   Medium* medium = lookup(name);
00165   if (medium != NULL) {
00166     fTable->Remove(name);
00167     if (fTable->IsEmpty()) {
00168       // We can also delete ourselves (to reclaim space):
00169       _Tables* ourTables = _Tables::getOurTables(fEnv);
00170       delete this;
00171       ourTables->mediaTable = NULL;
00172       ourTables->reclaimIfPossible();
00173     }
00174 
00175     delete medium;
00176   }
00177 }
00178 
00179 void MediaLookupTable::generateNewName(char* mediumName,
00180                                        unsigned /*maxLen*/) {
00181   // We should really use snprintf() here, but not all systems have it
00182   sprintf(mediumName, "liveMedia%d", fNameGenerator++);
00183 }
00184 
00185 MediaLookupTable::MediaLookupTable(UsageEnvironment& env)
00186   : fEnv(env), fTable(HashTable::create(STRING_HASH_KEYS)), fNameGenerator(0) {
00187 }
00188 
00189 MediaLookupTable::~MediaLookupTable() {
00190   delete fTable;
00191 }

Generated on Fri Sep 3 02:35:40 2010 for live by  doxygen 1.5.2