OSVR-Core
MediaSampleExchange.h
Go to the documentation of this file.
1 
11 // Copyright 2015 Sensics, Inc.
12 //
13 // Licensed under the Apache License, Version 2.0 (the "License");
14 // you may not use this file except in compliance with the License.
15 // You may obtain a copy of the License at
16 //
17 // http://www.apache.org/licenses/LICENSE-2.0
18 //
19 // Unless required by applicable law or agreed to in writing, software
20 // distributed under the License is distributed on an "AS IS" BASIS,
21 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 // See the License for the specific language governing permissions and
23 // limitations under the License.
24 
25 #ifndef INCLUDED_MediaSampleExchange_h_GUID_6E82A284_0E96_4391_6408_D9602D2658D7
26 #define INCLUDED_MediaSampleExchange_h_GUID_6E82A284_0E96_4391_6408_D9602D2658D7
27 
28 // Internal Includes
29 // - none
30 
31 // Library/third-party includes
32 #include <osvr/Util/TimeValue.h>
33 
34 // Standard includes
35 #include <cassert>
36 #include <chrono>
37 #include <memory>
38 #include <utility>
39 
40 struct IMediaSample;
44 class Sample {
45  public:
47  ~Sample();
49  Sample(Sample &&other)
50  : sampleExchange_(other.sampleExchange_), sample_(other.sample_),
51  timestamp_(other.timestamp_), owning_(false) {
52  std::swap(owning_, other.owning_);
53  }
54  Sample &operator=(Sample const &) = delete;
55 
59  IMediaSample &get() {
60  assert(owning_ && "Shouldn't try to get the contained sample from a "
61  "moved-from wrapper!");
62  return sample_;
63  }
64 
65  osvr::util::time::TimeValue const &getTimestamp() const {
66  assert(owning_ && "Shouldn't try to get the contained timestamp from a "
67  "moved-from wrapper!");
68  return timestamp_;
69  }
70 
71  private:
72  Sample(MediaSampleExchange &exchange, IMediaSample &sample,
73  osvr::util::time::TimeValue const &timestamp)
74  : sampleExchange_(exchange), sample_(sample), timestamp_(timestamp),
75  owning_(true) {}
76  friend class MediaSampleExchange;
77  MediaSampleExchange &sampleExchange_;
78  IMediaSample &sample_;
79  osvr::util::time::TimeValue timestamp_;
80  bool owning_;
81 };
82 
90  public:
96  void signalSampleProduced(IMediaSample *sample,
97  osvr::util::time::TimeValue const &timestamp);
98  void signalSampleProduced(IMediaSample *sample);
99 
101  bool waitForSample(std::chrono::milliseconds timeout);
102 
105  Sample get() {
106  IMediaSample *pin{sample_};
107  return Sample{*this, *pin, timestamp_};
108  }
109 
111  bool waitForSampleConsumed(std::chrono::milliseconds timeout);
112 
113  private:
115  void signalSampleConsumed();
116  friend class Sample;
117  struct Impl;
118  IMediaSample *sample_ = nullptr;
119  osvr::util::time::TimeValue timestamp_ = {};
120  std::unique_ptr<Impl> impl_;
121 };
122 
123 using MediaSampleExchangePtr = std::shared_ptr<MediaSampleExchange>;
124 
125 inline Sample::~Sample() {
126  if (owning_) {
127  sampleExchange_.signalSampleConsumed();
128  }
129 }
130 
131 #endif // INCLUDED_MediaSampleExchange_h_GUID_6E82A284_0E96_4391_6408_D9602D2658D7
Definition: MediaSampleExchange.cpp:35
A class to mediate between a consumer of directshow image samples and the ISampleGrabberCallback impl...
Definition: MediaSampleExchange.h:89
Sample(Sample &&other)
Move constructor.
Definition: MediaSampleExchange.h:49
Header providing a C++ wrapper around TimeValueC.h.
~Sample()
Destructor - signals consumer finished.
Definition: MediaSampleExchange.h:125
Standardized, portable parallel to struct timeval for representing both absolute times and time inter...
Definition: TimeValueC.h:81
An RAII class for retrieving the sample and signalling consumption complete.
Definition: MediaSampleExchange.h:44