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 class for streaming data from a (static) memory buffer, as if it were a file. |
19 | // Implementation |
20 | |
21 | #include "ByteStreamMemoryBufferSource.hh" |
22 | #include "GroupsockHelper.hh" |
23 | |
24 | ////////// ByteStreamMemoryBufferSource ////////// |
25 | |
26 | ByteStreamMemoryBufferSource* |
27 | ByteStreamMemoryBufferSource::createNew(UsageEnvironment& env, |
28 | u_int8_t* buffer, u_int64_t bufferSize, |
29 | Boolean deleteBufferOnClose, |
30 | unsigned preferredFrameSize, |
31 | unsigned playTimePerFrame) { |
32 | if (buffer == NULL) return NULL; |
33 | |
34 | return new ByteStreamMemoryBufferSource(env, buffer, bufferSize, deleteBufferOnClose, preferredFrameSize, playTimePerFrame); |
35 | } |
36 | |
37 | ByteStreamMemoryBufferSource::ByteStreamMemoryBufferSource(UsageEnvironment& env, |
38 | u_int8_t* buffer, u_int64_t bufferSize, |
39 | Boolean deleteBufferOnClose, |
40 | unsigned preferredFrameSize, |
41 | unsigned playTimePerFrame) |
42 | : FramedSource(env), fBuffer(buffer), fBufferSize(bufferSize), fCurIndex(0), fDeleteBufferOnClose(deleteBufferOnClose), |
43 | fPreferredFrameSize(preferredFrameSize), fPlayTimePerFrame(playTimePerFrame), fLastPlayTime(0), |
44 | fLimitNumBytesToStream(False), fNumBytesToStream(0) { |
45 | } |
46 | |
47 | ByteStreamMemoryBufferSource::~ByteStreamMemoryBufferSource() { |
48 | if (fDeleteBufferOnClose) delete[] fBuffer; |
49 | } |
50 | |
51 | void ByteStreamMemoryBufferSource::seekToByteAbsolute(u_int64_t byteNumber, u_int64_t numBytesToStream) { |
52 | fCurIndex = byteNumber; |
53 | if (fCurIndex > fBufferSize) fCurIndex = fBufferSize; |
54 | |
55 | fNumBytesToStream = numBytesToStream; |
56 | fLimitNumBytesToStream = fNumBytesToStream > 0; |
57 | } |
58 | |
59 | void ByteStreamMemoryBufferSource::seekToByteRelative(int64_t offset, u_int64_t numBytesToStream) { |
60 | int64_t newIndex = fCurIndex + offset; |
61 | if (newIndex < 0) { |
62 | fCurIndex = 0; |
63 | } else { |
64 | fCurIndex = (u_int64_t)offset; |
65 | if (fCurIndex > fBufferSize) fCurIndex = fBufferSize; |
66 | } |
67 | |
68 | fNumBytesToStream = numBytesToStream; |
69 | fLimitNumBytesToStream = fNumBytesToStream > 0; |
70 | } |
71 | |
72 | void ByteStreamMemoryBufferSource::doGetNextFrame() { |
73 | if (fCurIndex >= fBufferSize || (fLimitNumBytesToStream && fNumBytesToStream == 0)) { |
74 | handleClosure(); |
75 | return; |
76 | } |
77 | |
78 | // Try to read as many bytes as will fit in the buffer provided (or "fPreferredFrameSize" if less) |
79 | fFrameSize = fMaxSize; |
80 | if (fLimitNumBytesToStream && fNumBytesToStream < (u_int64_t)fFrameSize) { |
81 | fFrameSize = (unsigned)fNumBytesToStream; |
82 | } |
83 | if (fPreferredFrameSize > 0 && fPreferredFrameSize < fFrameSize) { |
84 | fFrameSize = fPreferredFrameSize; |
85 | } |
86 | |
87 | if (fCurIndex + fFrameSize > fBufferSize) { |
88 | fFrameSize = (unsigned)(fBufferSize - fCurIndex); |
89 | } |
90 | |
91 | memmove(fTo, &fBuffer[fCurIndex], fFrameSize); |
92 | fCurIndex += fFrameSize; |
93 | fNumBytesToStream -= fFrameSize; |
94 | |
95 | // Set the 'presentation time': |
96 | if (fPlayTimePerFrame > 0 && fPreferredFrameSize > 0) { |
97 | if (fPresentationTime.tv_sec == 0 && fPresentationTime.tv_usec == 0) { |
98 | // This is the first frame, so use the current time: |
99 | gettimeofday(&fPresentationTime, NULL); |
100 | } else { |
101 | // Increment by the play time of the previous data: |
102 | unsigned uSeconds = fPresentationTime.tv_usec + fLastPlayTime; |
103 | fPresentationTime.tv_sec += uSeconds/1000000; |
104 | fPresentationTime.tv_usec = uSeconds%1000000; |
105 | } |
106 | |
107 | // Remember the play time of this data: |
108 | fLastPlayTime = (fPlayTimePerFrame*fFrameSize)/fPreferredFrameSize; |
109 | fDurationInMicroseconds = fLastPlayTime; |
110 | } else { |
111 | // We don't know a specific play time duration for this data, |
112 | // so just record the current time as being the 'presentation time': |
113 | gettimeofday(&fPresentationTime, NULL); |
114 | } |
115 | |
116 | // Inform the downstream object that it has data: |
117 | FramedSource::afterGetting(this); |
118 | } |
119 | |