Ocean
SampleGrabber.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) Meta Platforms, Inc. and affiliates.
3  *
4  * This source code is licensed under the MIT license found in the
5  * LICENSE file in the root directory of this source tree.
6  */
7 
8 #ifndef META_OCEAN_MEDIA_MF_SAMPLE_GRABBER_H
9 #define META_OCEAN_MEDIA_MF_SAMPLE_GRABBER_H
10 
12 
13 #include "ocean/base/Callback.h"
14 
15 namespace Ocean
16 {
17 
18 namespace Media
19 {
20 
21 namespace MediaFoundation
22 {
23 
24 // Forward declaration.
25 class SampleGrabber;
26 
27 /**
28  * Definition of a scoped object holding a SampleGrabber object.
29  * The wrapped SampleGrabber object will be released automatically once the scoped object does not exist anymore.
30  * @ingroup mediamf
31  */
33 
34 /**
35  * This class implements a callback interface.
36  * @ingroup mediamf
37  */
39 {
40  public:
41 
42  /**
43  * Definition of a callback for samples.
44  */
46 
47  public:
48 
49  /**
50  * Destructs this object.
51  */
52  virtual ~SampleGrabber() = default;
53 
54  /**
55  * Deactivates the callback.
56  */
57  void deactivate();
58 
59  /**
60  * Returns the time of the most recent sample.
61  * @return The most recent sample time
62  */
63  LONGLONG recentSampleTime() const;
64 
65  /**
66  * Interface query function.
67  * @param iid The identifier of the interface being requested
68  * @param ppv The address of a pointer variable that receives the interface pointer requested in the riid parameter
69  * @return This method returns S_OK if the interface is supported
70  */
71  STDMETHODIMP QueryInterface(REFIID iid, void** ppv) override;
72 
73  /**
74  * Increments the reference count for an interface on an object.
75  * @return The method returns the new reference count
76  */
77  STDMETHODIMP_(ULONG) AddRef() override;
78 
79  /**
80  * Decrements the reference count for an interface on an object.
81  * @return The method returns the new reference count
82  */
83  STDMETHODIMP_(ULONG) Release() override;
84 
85  /**
86  * Called when the presentation clock starts.
87  * @param hnsSystemTime The system time when the clock started, in 100-nanosecond units
88  * @param llClockStartOffset The new starting time for the clock, in 100-nanosecond units
89  * @return If this method succeeds, it returns S_OK
90  */
91  STDMETHODIMP OnClockStart(MFTIME hnsSystemTime, LONGLONG llClockStartOffset) override;
92 
93  /**
94  * Called when the presentation clock stops.
95  * @param hnsSystemTime The system time when the clock stopped, in 100-nanosecond units
96  * @return If this method succeeds, it returns S_OK
97  */
98  STDMETHODIMP OnClockStop(MFTIME hnsSystemTime) override;
99 
100  /**
101  * Called when the presentation clock pauses.
102  * @param hnsSystemTime The system time when the clock was paused, in 100-nanosecond units
103  * @return If this method succeeds, it returns S_OK
104  */
105  STDMETHODIMP OnClockPause(MFTIME hnsSystemTime) override;
106 
107  /**
108  * Called when the presentation clock restarts from the same position while paused.
109  * @param hnsSystemTime The system time when the clock restarted, in 100-nanosecond units
110  * @return If this method succeeds, it returns S_OK
111  */
112  STDMETHODIMP OnClockRestart(MFTIME hnsSystemTime) override;
113 
114  /**
115  * Called when the rate changes on the presentation clock.
116  * @param hnsSystemTime The system time when the rate was set, in 100-nanosecond units
117  * @param flRate The new rate, as a multiplier of the normal playback rate
118  * @return If this method succeeds, it returns S_OK
119  */
120  STDMETHODIMP OnClockSetRate(MFTIME hnsSystemTime, float flRate) override;
121 
122  /**
123  * Called when the presentation clock is set on the sample-grabber sink.
124  * @param pClock Pointer to the presentation clock's IMFPresentationClock interface
125  * @return If this method succeeds, it returns S_OK
126  */
127  STDMETHODIMP OnSetPresentationClock(IMFPresentationClock* pClock) override;
128 
129  /**
130  * Called when the sample-grabber sink receives a new media sample.
131  * @param guidMajorMediaType The major type that specifies the format of the data
132  * @param dwSampleFlags Reserved
133  * @param llSampleTime The presentation time for this sample, in 100-nanosecond units
134  * @param llSampleDuration The duration of the sample, in 100-nanosecond units
135  * @param pSampleBuffer A pointer to a buffer that contains the sample data
136  * @param dwSampleSize Size of the pSampleBuffer buffer, in bytes
137  */
138  STDMETHODIMP OnProcessSample(REFGUID guidMajorMediaType, DWORD dwSampleFlags, LONGLONG llSampleTime, LONGLONG llSampleDuration, const BYTE * pSampleBuffer, DWORD dwSampleSize) override;
139 
140  /**
141  * Called when the sample-grabber sink is shut down.
142  * @return If this method succeeds, it returns S_OK
143  */
144  STDMETHODIMP OnShutdown() override;
145 
146  /**
147  * Creates a new instance of the sample grabber.
148  * @param sampleCallback Sample callback function
149  * @return Resulting sample grabber
150  */
151  static ScopedSampleGrabber createInstance(const SampleCallback& sampleCallback);
152 
153  private:
154 
155  /**
156  * Default constructor.
157  * @param callback Sample callback function
158  */
159  SampleGrabber(const SampleCallback& callback);
160 
161  private:
162 
163  /// The reference counter.
165 
166  /// Sample callback function.
168 
169  /// True if the callback is active.
170  bool active_ = true;
171 
172  /// The time of the most recent sample.
173  LONGLONG recentSampleTime_ = LONGLONG(-1);
174 };
175 
176 }
177 
178 }
179 
180 }
181 
182 #endif // META_OCEAN_MEDIA_MF_SAMPLE_GRABBER_H
This class implements a callback interface.
Definition: SampleGrabber.h:39
LONGLONG recentSampleTime_
The time of the most recent sample.
Definition: SampleGrabber.h:173
STDMETHODIMP_(ULONG) Release() override
Decrements the reference count for an interface on an object.
SampleGrabber(const SampleCallback &callback)
Default constructor.
LONGLONG recentSampleTime() const
Returns the time of the most recent sample.
STDMETHODIMP OnClockSetRate(MFTIME hnsSystemTime, float flRate) override
Called when the rate changes on the presentation clock.
bool active_
True if the callback is active.
Definition: SampleGrabber.h:170
STDMETHODIMP OnClockPause(MFTIME hnsSystemTime) override
Called when the presentation clock pauses.
static ScopedSampleGrabber createInstance(const SampleCallback &sampleCallback)
Creates a new instance of the sample grabber.
STDMETHODIMP QueryInterface(REFIID iid, void **ppv) override
Interface query function.
long referenceCounter_
The reference counter.
Definition: SampleGrabber.h:164
STDMETHODIMP OnClockRestart(MFTIME hnsSystemTime) override
Called when the presentation clock restarts from the same position while paused.
STDMETHODIMP OnClockStop(MFTIME hnsSystemTime) override
Called when the presentation clock stops.
SampleCallback sampleCallback_
Sample callback function.
Definition: SampleGrabber.h:167
void deactivate()
Deactivates the callback.
STDMETHODIMP OnClockStart(MFTIME hnsSystemTime, LONGLONG llClockStartOffset) override
Called when the presentation clock starts.
STDMETHODIMP OnProcessSample(REFGUID guidMajorMediaType, DWORD dwSampleFlags, LONGLONG llSampleTime, LONGLONG llSampleDuration, const BYTE *pSampleBuffer, DWORD dwSampleSize) override
Called when the sample-grabber sink receives a new media sample.
STDMETHODIMP_(ULONG) AddRef() override
Increments the reference count for an interface on an object.
STDMETHODIMP OnShutdown() override
Called when the sample-grabber sink is shut down.
STDMETHODIMP OnSetPresentationClock(IMFPresentationClock *pClock) override
Called when the presentation clock is set on the sample-grabber sink.
Callback< void, const GUID &, const unsigned int, const long long, const long long, const void *, const unsigned int > SampleCallback
Definition of a callback for samples.
Definition: SampleGrabber.h:45
virtual ~SampleGrabber()=default
Destructs this object.
This class wraps an unmanaged object (or reference) which needs to be released after usage.
Definition: ScopedObject.h:166
ScopedMediaFoundationObject< SampleGrabber > ScopedSampleGrabber
Definition of a scoped object holding a SampleGrabber object.
Definition: SampleGrabber.h:25
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15