AVPKit
MediaDataWrapper.cpp
1 /*******************************************************************************
2  * Copyright (c) 2024, 2026, Olivier Ayache. All rights reserved.
3  *
4  * This file is part of AVPKit.
5  *
6  * AVPKit is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * AVPKit is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with AVPKit. If not, see <http://www.gnu.org/licenses/>.
18  *******************************************************************************/
19 
20 #include <com/avpkit/ferry/Logger.h>
21 #include <com/avpkit/core/Global.h>
22 #include <com/avpkit/core/MediaDataWrapper.h>
23 
24 namespace com { namespace avpkit { namespace core {
25 
26 using namespace com::avpkit::ferry;
27 
28 VS_LOG_SETUP(VS_CPP_PACKAGE);
29 
30 MediaDataWrapper :: MediaDataWrapper()
31 {
32  mTimeStamp = Global::NO_PTS;
33  mIsKey = true;
34 }
35 
36 MediaDataWrapper :: ~MediaDataWrapper()
37 {
38  mTimeBase = 0;
39 }
40 
41 MediaDataWrapper*
43 {
44  MediaDataWrapper* retval = make();
45  if (retval)
46  {
47  retval->wrap(obj);
48  }
49  return retval;
50 }
51 
52 int64_t
54 {
55  return mTimeStamp;
56 }
57 
58 void
60 {
61  mTimeStamp = aTimeStamp;
62 }
63 
64 IRational*
66 {
67  return mTimeBase.get();
68 }
69 
70 void
72 {
73  mTimeBase.reset(aBase, true);
74 }
75 
76 IBuffer*
78 {
79  IBuffer* retval=0;
80  // forward to wrapped object
81  if (mWrapped)
82  {
83  retval = mWrapped->getData();
84  }
85  return retval;
86 }
87 
88 int32_t
90 {
91  int retval = -1;
92  // forward to wrapped object
93  if (mWrapped)
94  {
95  retval = mWrapped->getSize();
96  }
97  return retval;
98 }
99 
100 bool
102 {
103  return mIsKey;
104 }
105 
106 void
108 {
109  mIsKey = aIsKey;
110 }
111 
112 void
114 {
115  IMediaDataWrapper* wrapper=dynamic_cast<IMediaDataWrapper*>(aObj);
116  // loop detection to make sure we're not wrapping ourselves.
117  if (wrapper) {
119  IMediaDataWrapper *me = static_cast<IMediaDataWrapper*>(this);
120  do
121  {
122  if (wrapper == me)
123  break;
124  obj = wrapper->get();
125  } while (0 != (wrapper = dynamic_cast<IMediaDataWrapper*>(obj.value())));
126 
127  if (wrapper == me)
128  {
129  VS_LOG_ERROR("Attempted to wrap an object that ultimately wraps itself. Ignoring");
130  // we're wrapping ourselves
131  return;
132  }
133  }
134  mWrapped.reset(aObj, true);
135  if (aObj)
136  {
137  setTimeStamp(aObj->getTimeStamp());
138  IRational *base = aObj->getTimeBase();
139  setTimeBase(base);
140  VS_REF_RELEASE(base);
141  setKey(aObj->isKey());
142  }
143  else
144  {
145  setTimeStamp(Global::NO_PTS);
146  setTimeBase(0);
147  setKey(true);
148  }
149 }
150 
151 void
153 {
154  if (mWrapped)
155  mWrapped->setData(buffer);
156 }
157 
158 IMediaData*
160 {
161  return mWrapped.get();
162 }
163 
164 IMediaData*
166 {
167  RefPointer<IMediaData> unwrapped = get();
168 
169  // as long unwrapped points to an IMediaDataWrapper, then unwrap
170  while(unwrapped && dynamic_cast<IMediaDataWrapper*>(unwrapped.value()))
171  unwrapped = (dynamic_cast<IMediaDataWrapper*>(unwrapped.value()))->get();
172  return unwrapped.get();
173 }
174 }}}
static const int64_t NO_PTS
A value that means no time stamp is set for a given object.
Definition: Global.h:50
This class wraps an IMediaData object, but then allows you to set new TimeStamps and TimeBases.
virtual IMediaData * get()=0
Return the object being wrapped.
The parent class of all media objects than can be gotten from an IStream.
Definition: IMediaData.h:34
virtual IRational * getTimeBase()=0
Get the time base that time stamps of this object are represented in.
virtual bool isKey()=0
Is this object a key object? i.e.
virtual int64_t getTimeStamp()=0
Get the time stamp of this object in getTimeBase() units.
This class wraps represents a Rational number for the AVPKit.
Definition: IRational.h:43
virtual int32_t getSize()
Get the size in bytes of the raw data available for this object.
virtual bool isKey()
Is this object a key object? i.e.
virtual void setTimeStamp(int64_t aTimeStamp)
Set the time stamp for this object in getTimeBase() units.
virtual IMediaData * unwrap()
Gets the non IMediaDataWrapper object ultimately wrapped in this wrapper, or null if there isn't one.
static MediaDataWrapper * make(IMediaData *obj)
Create a new IMediaDataWrapper that wraps a given object.
virtual void wrap(IMediaData *aObj)
Set an object to wrap, or null to release the old object.
virtual void setData(com::avpkit::ferry::IBuffer *)
Sets the underlying buffer used by this object.
virtual IRational * getTimeBase()
Get the time base that time stamps of this object are represented in.
virtual com::avpkit::ferry::IBuffer * getData()
Get any underlying raw data available for this object.
virtual void setKey(bool aIsKey)
Allows you to reset whether the wrapper things this is key or not.
virtual int64_t getTimeStamp()
Get the time stamp of this object in getTimeBase() units.
virtual IMediaData * get()
Return the object being wrapped.
virtual void setTimeBase(IRational *aBase)
Set the time base that time stamps of this object are represented in.
Allows Java code to get data from a native buffers, and optionally modify native memory directly.
Definition: IBuffer.h:54
This class is only useful from C++.
Definition: RefPointer.h:47
T * get()
Call RefCounted::acquire() on the managed pointer and return it.
Definition: RefPointer.h:206
T * value()
Return the managed pointer without calling RefCounted::acquire() on it.
Definition: RefPointer.h:226
This library contains routines used by AVPKit libraries for "ferry"ing Java objects to and from nativ...
WARNING: Do not use logging in this class, and do not set any static file variables to values other t...