+ if (resampler && !flushing) {
+ /* It can be the the data here has a different number of channels than the stream
+ * it comes from (e.g. the files decoded by FFmpegDecoder sometimes have a random
+ * frame, often at the end, with more channels). Insert silence or discard channels
+ * here.
+ */
+ if (resampler->channels() != data->channels()) {
+ LOG_WARNING("Received audio data with an unexpected channel count of %1 instead of %2", data->channels(), resampler->channels());
+ auto data_copy = data->clone();
+ data_copy->set_channels(resampler->channels());
+ data = resampler->run(data_copy);
+ } else {
+ data = resampler->run(data);
+ }
+
+ if (data->frames() == 0) {
+ return;
+ }
+ }
+
+ Data(stream, ContentAudio (data, _positions[stream]));
+ _positions[stream] += data->frames();
+}
+
+
+/** @return Time just after the last thing that was emitted from a given stream */
+ContentTime
+AudioDecoder::stream_position (shared_ptr<const Film> film, AudioStreamPtr stream) const
+{
+ auto i = _positions.find (stream);
+ DCPOMATIC_ASSERT (i != _positions.end ());
+ return ContentTime::from_frames (i->second, _content->resampled_frame_rate(film));
+}
+
+
+boost::optional<ContentTime>
+AudioDecoder::position (shared_ptr<const Film> film) const
+{
+ optional<ContentTime> p;
+ for (auto i: _positions) {
+ auto const ct = stream_position (film, i.first);
+ if (!p || ct < *p) {
+ p = ct;
+ }