1/**********
2This library is free software; you can redistribute it and/or modify it under
3the terms of the GNU Lesser General Public License as published by the
4Free Software Foundation; either version 3 of the License, or (at your
5option) any later version. (See <http://www.gnu.org/copyleft/lesser.html>.)
6
7This library is distributed in the hope that it will be useful, but WITHOUT
8ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
9FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
10more details.
11
12You should have received a copy of the GNU Lesser General Public License
13along with this library; if not, write to the Free Software Foundation, Inc.,
1451 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
27DVVideoFileServerMediaSubsession*
28DVVideoFileServerMediaSubsession::createNew(UsageEnvironment& env, char const* fileName, Boolean reuseFirstSource) {
29 return new DVVideoFileServerMediaSubsession(env, fileName, reuseFirstSource);
30}
31
32DVVideoFileServerMediaSubsession
33::DVVideoFileServerMediaSubsession(UsageEnvironment& env, char const* fileName, Boolean reuseFirstSource)
34 : FileServerMediaSubsession(env, fileName, reuseFirstSource),
35 fFileDuration(0.0) {
36}
37
38DVVideoFileServerMediaSubsession::~DVVideoFileServerMediaSubsession() {
39}
40
41FramedSource* 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
64RTPSink* DVVideoFileServerMediaSubsession::createNewRTPSink(Groupsock* rtpGroupsock,
65 unsigned char rtpPayloadTypeIfDynamic,
66 FramedSource* /*inputSource*/) {
67 return DVVideoRTPSink::createNew(envir(), rtpGroupsock, rtpPayloadTypeIfDynamic);
68}
69
70char const* DVVideoFileServerMediaSubsession::getAuxSDPLine(RTPSink* rtpSink, FramedSource* inputSource) {
71 return ((DVVideoRTPSink*)rtpSink)->auxSDPLineFromFramer((DVVideoStreamFramer*)inputSource);
72}
73
74float DVVideoFileServerMediaSubsession::duration() const {
75 return fFileDuration;
76}
77
78void 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
92void 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