| 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 | // A 'ServerMediaSubsession' object that creates new, unicast, "RTPSink"s |
| 19 | // on demand, from a DV video file. |
| 20 | // Implementation |
| 21 | |
| 22 | #include "DVVideoFileServerMediaSubsession.hh" |
| 23 | #include "DVVideoRTPSink.hh" |
| 24 | #include "ByteStreamFileSource.hh" |
| 25 | #include "DVVideoStreamFramer.hh" |
| 26 | |
| 27 | DVVideoFileServerMediaSubsession* |
| 28 | DVVideoFileServerMediaSubsession::createNew(UsageEnvironment& env, char const* fileName, Boolean reuseFirstSource) { |
| 29 | return new DVVideoFileServerMediaSubsession(env, fileName, reuseFirstSource); |
| 30 | } |
| 31 | |
| 32 | DVVideoFileServerMediaSubsession |
| 33 | ::DVVideoFileServerMediaSubsession(UsageEnvironment& env, char const* fileName, Boolean reuseFirstSource) |
| 34 | : FileServerMediaSubsession(env, fileName, reuseFirstSource), |
| 35 | fFileDuration(0.0) { |
| 36 | } |
| 37 | |
| 38 | DVVideoFileServerMediaSubsession::~DVVideoFileServerMediaSubsession() { |
| 39 | } |
| 40 | |
| 41 | FramedSource* DVVideoFileServerMediaSubsession |
| 42 | ::createNewStreamSource(unsigned /*clientSessionId*/, unsigned& estBitrate) { |
| 43 | // Create the video source: |
| 44 | ByteStreamFileSource* fileSource = ByteStreamFileSource::createNew(envir(), fFileName); |
| 45 | if (fileSource == NULL) return NULL; |
| 46 | fFileSize = fileSource->fileSize(); |
| 47 | |
| 48 | // Create a framer for the Video Elementary Stream: |
| 49 | DVVideoStreamFramer* framer = DVVideoStreamFramer::createNew(envir(), fileSource, True/*the file source is seekable*/); |
| 50 | |
| 51 | // Use the framer to figure out the file's duration: |
| 52 | unsigned frameSize; |
| 53 | double frameDuration; |
| 54 | if (framer->getFrameParameters(frameSize, frameDuration)) { |
| 55 | fFileDuration = (float)(((int64_t)fFileSize*frameDuration)/(frameSize*1000000.0)); |
| 56 | estBitrate = (unsigned)((8000.0*frameSize)/frameDuration); // in kbps |
| 57 | } else { |
| 58 | estBitrate = 50000; // kbps, estimate |
| 59 | } |
| 60 | |
| 61 | return framer; |
| 62 | } |
| 63 | |
| 64 | RTPSink* DVVideoFileServerMediaSubsession::createNewRTPSink(Groupsock* rtpGroupsock, |
| 65 | unsigned char rtpPayloadTypeIfDynamic, |
| 66 | FramedSource* /*inputSource*/) { |
| 67 | return DVVideoRTPSink::createNew(envir(), rtpGroupsock, rtpPayloadTypeIfDynamic); |
| 68 | } |
| 69 | |
| 70 | char const* DVVideoFileServerMediaSubsession::getAuxSDPLine(RTPSink* rtpSink, FramedSource* inputSource) { |
| 71 | return ((DVVideoRTPSink*)rtpSink)->auxSDPLineFromFramer((DVVideoStreamFramer*)inputSource); |
| 72 | } |
| 73 | |
| 74 | float DVVideoFileServerMediaSubsession::duration() const { |
| 75 | return fFileDuration; |
| 76 | } |
| 77 | |
| 78 | void DVVideoFileServerMediaSubsession |
| 79 | ::seekStreamSource(FramedSource* inputSource, double& seekNPT, double streamDuration, u_int64_t& numBytes) { |
| 80 | // First, get the file source from "inputSource" (a framer): |
| 81 | DVVideoStreamFramer* framer = (DVVideoStreamFramer*)inputSource; |
| 82 | ByteStreamFileSource* fileSource = (ByteStreamFileSource*)(framer->inputSource()); |
| 83 | |
| 84 | // Then figure out where to seek to within the file: |
| 85 | if (fFileDuration > 0.0) { |
| 86 | u_int64_t seekByteNumber = (u_int64_t)(((int64_t)fFileSize*seekNPT)/fFileDuration); |
| 87 | numBytes = (u_int64_t)(((int64_t)fFileSize*streamDuration)/fFileDuration); |
| 88 | fileSource->seekToByteAbsolute(seekByteNumber, numBytes); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | void DVVideoFileServerMediaSubsession |
| 93 | ::setStreamSourceDuration(FramedSource* inputSource, double streamDuration, u_int64_t& numBytes) { |
| 94 | // First, get the file source from "inputSource" (a framer): |
| 95 | DVVideoStreamFramer* framer = (DVVideoStreamFramer*)inputSource; |
| 96 | ByteStreamFileSource* fileSource = (ByteStreamFileSource*)(framer->inputSource()); |
| 97 | |
| 98 | // Then figure out how many bytes to limit the streaming to: |
| 99 | if (fFileDuration > 0.0) { |
| 100 | numBytes = (u_int64_t)(((int64_t)fFileSize*streamDuration)/fFileDuration); |
| 101 | fileSource->seekToByteRelative(0, numBytes); |
| 102 | } |
| 103 | } |
| 104 | |