StreamReplicator.hh 3.9 KB

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