| 1 | /********** |
| 2 | This library is free software; you can redistribute it and/or modify it under |
| 3 | 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 | option) any later version. (See <http://www.gnu.org/copyleft/lesser.html>.) |
| 6 | |
| 7 | This library is distributed in the hope that it will be useful, but WITHOUT |
| 8 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 9 | FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for |
| 10 | more details. |
| 11 | |
| 12 | You should have received a copy of the GNU Lesser General Public License |
| 13 | along with this library; if not, write to the Free Software Foundation, Inc., |
| 14 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 15 | **********/ |
| 16 | // "liveMedia" |
| 17 | // Copyright (c) 1996-2020 Live Networks, Inc. All rights reserved. |
| 18 | // An class that can be used to create (possibly multiple) 'replicas' of an incoming stream. |
| 19 | // C++ header |
| 20 | |
| 21 | #ifndef _STREAM_REPLICATOR_HH |
| 22 | #define _STREAM_REPLICATOR_HH |
| 23 | |
| 24 | #ifndef _FRAMED_SOURCE_HH |
| 25 | #include "FramedSource.hh" |
| 26 | #endif |
| 27 | |
| 28 | class StreamReplica; // forward |
| 29 | |
| 30 | class StreamReplicator: public Medium { |
| 31 | public: |
| 32 | static StreamReplicator* createNew(UsageEnvironment& env, FramedSource* inputSource, Boolean deleteWhenLastReplicaDies = True); |
| 33 | // If "deleteWhenLastReplicaDies" is True (the default), then the "StreamReplicator" object is deleted when (and only when) |
| 34 | // all replicas have been deleted. (In this case, you must *not* call "Medium::close()" on the "StreamReplicator" object, |
| 35 | // unless you never created any replicas from it to begin with.) |
| 36 | // If "deleteWhenLastReplicaDies" is False, then the "StreamReplicator" object remains in existence, even when all replicas |
| 37 | // have been deleted. (This allows you to create new replicas later, if you wish.) In this case, you delete the |
| 38 | // "StreamReplicator" object by calling "Medium::close()" on it - but you must do so only when "numReplicas()" returns 0. |
| 39 | |
| 40 | FramedSource* createStreamReplica(); |
| 41 | |
| 42 | unsigned numReplicas() const { return fNumReplicas; } |
| 43 | |
| 44 | FramedSource* inputSource() const { return fInputSource; } |
| 45 | |
| 46 | // Call before destruction if you want to prevent the destructor from closing the input source |
| 47 | void detachInputSource() { fInputSource = NULL; } |
| 48 | |
| 49 | protected: |
| 50 | StreamReplicator(UsageEnvironment& env, FramedSource* inputSource, Boolean deleteWhenLastReplicaDies); |
| 51 | // called only by "createNew()" |
| 52 | virtual ~StreamReplicator(); |
| 53 | |
| 54 | private: |
| 55 | // Routines called by replicas to implement frame delivery, and the stopping/restarting/deletion of replicas: |
| 56 | friend class StreamReplica; |
| 57 | void getNextFrame(StreamReplica* replica); |
| 58 | void deactivateStreamReplica(StreamReplica* replica); |
| 59 | void removeStreamReplica(StreamReplica* replica); |
| 60 | |
| 61 | private: |
| 62 | static void afterGettingFrame(void* clientData, unsigned frameSize, |
| 63 | unsigned numTruncatedBytes, |
| 64 | struct timeval presentationTime, |
| 65 | unsigned durationInMicroseconds); |
| 66 | void afterGettingFrame(unsigned frameSize, unsigned numTruncatedBytes, |
| 67 | struct timeval presentationTime, unsigned durationInMicroseconds); |
| 68 | |
| 69 | static void onSourceClosure(void* clientData); |
| 70 | void onSourceClosure(); |
| 71 | |
| 72 | void deliverReceivedFrame(); |
| 73 | |
| 74 | private: |
| 75 | FramedSource* fInputSource; |
| 76 | Boolean fDeleteWhenLastReplicaDies, fInputSourceHasClosed; |
| 77 | unsigned fNumReplicas, fNumActiveReplicas, fNumDeliveriesMadeSoFar; |
| 78 | int fFrameIndex; // 0 or 1; used to figure out if a replica is requesting the current frame, or the next frame |
| 79 | |
| 80 | StreamReplica* fMasterReplica; // the first replica that requests each frame. We use its buffer when copying to the others. |
| 81 | StreamReplica* fReplicasAwaitingCurrentFrame; // other than the 'master' replica |
| 82 | StreamReplica* fReplicasAwaitingNextFrame; // replicas that have already received the current frame, and have asked for the next |
| 83 | }; |
| 84 | #endif |
| 85 | |