1/* ---------------------------------------------------------------------------
2** This software is in the public domain, furnished "as is", without technical
3** support, and with no warranty, express or implied, as to its usefulness for
4** any purpose.
5**
6** MemoryBufferSink.cpp
7**
8** -------------------------------------------------------------------------*/
9
10#include "MemoryBufferSink.h"
11
12// -----------------------------------------
13// MemoryBufferSink
14// -----------------------------------------
15MemoryBufferSink::MemoryBufferSink(UsageEnvironment& env, unsigned bufferSize, unsigned int sliceDuration) : MediaSink(env), m_bufferSize(bufferSize), m_refTime(0), m_sliceDuration(sliceDuration)
16{
17 m_buffer = new unsigned char[m_bufferSize];
18}
19
20MemoryBufferSink::~MemoryBufferSink()
21{
22 delete[] m_buffer;
23}
24
25
26Boolean MemoryBufferSink::continuePlaying()
27{
28 Boolean ret = False;
29 if (fSource != NULL)
30 {
31 fSource->getNextFrame(m_buffer, m_bufferSize,
32 afterGettingFrame, this,
33 onSourceClosure, this);
34 ret = True;
35 }
36 return ret;
37}
38
39
40void MemoryBufferSink::afterGettingFrame(unsigned frameSize, unsigned numTruncatedBytes, struct timeval presentationTime)
41{
42 if (numTruncatedBytes > 0)
43 {
44 envir() << "FileSink::afterGettingFrame(): The input frame data was too large for our buffer size \n";
45 // realloc a bigger buffer
46 m_bufferSize += numTruncatedBytes;
47 delete[] m_buffer;
48 m_buffer = new unsigned char[m_bufferSize];
49 }
50 else
51 {
52 // append buffer to slice buffer
53 if (m_refTime == 0)
54 {
55 m_refTime = presentationTime.tv_sec;
56 }
57 unsigned int slice = (presentationTime.tv_sec-m_refTime)/m_sliceDuration;
58 std::string& outputBuffer = m_outputBuffers[slice];
59 outputBuffer.append((const char*)m_buffer, frameSize);
60
61 // remove old buffers
62 while (m_outputBuffers.size()>3)
63 {
64 m_outputBuffers.erase(m_outputBuffers.begin());
65 }
66 }
67
68 continuePlaying();
69}
70
71unsigned int MemoryBufferSink::getBufferSize(unsigned int slice)
72{
73 unsigned int size = 0;
74 std::map<unsigned int,std::string>::iterator it = m_outputBuffers.find(slice);
75 if (it != m_outputBuffers.end())
76 {
77 size = it->second.size();
78 }
79 return size;
80}
81
82std::string MemoryBufferSink::getBuffer(unsigned int slice)
83{
84 std::string content;
85 std::map<unsigned int,std::string>::iterator it = m_outputBuffers.find(slice);
86 if (it != m_outputBuffers.end())
87 {
88 content = it->second;
89
90 }
91 return content;
92}
93
94unsigned int MemoryBufferSink::firstTime()
95{
96 unsigned int firstTime = 0;
97 if (m_outputBuffers.size() != 0)
98 {
99 firstTime = m_outputBuffers.begin()->first;
100 }
101 return firstTime*m_sliceDuration;
102}
103
104unsigned int MemoryBufferSink::duration()
105{
106 unsigned int duration = 0;
107 if (m_outputBuffers.size() != 0)
108 {
109 duration = m_outputBuffers.rbegin()->first - m_outputBuffers.begin()->first;
110 }
111 return (duration)*m_sliceDuration;
112}
113