summaryrefslogtreecommitdiff
path: root/media/filters/legacy_frame_processor.cc
blob: f7b0547f586e85d69ec6cc96461eba4c1459e83f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "media/filters/legacy_frame_processor.h"

#include "media/base/buffers.h"
#include "media/base/stream_parser_buffer.h"

namespace media {

LegacyFrameProcessor::LegacyFrameProcessor(
    const IncreaseDurationCB& increase_duration_cb)
    : increase_duration_cb_(increase_duration_cb) {
  DVLOG(2) << __FUNCTION__ << "()";
  DCHECK(!increase_duration_cb_.is_null());
}

LegacyFrameProcessor::~LegacyFrameProcessor() {
  DVLOG(2) << __FUNCTION__ << "()";
}

void LegacyFrameProcessor::SetSequenceMode(bool sequence_mode) {
  DVLOG(2) << __FUNCTION__ << "(" << sequence_mode << ")";

  sequence_mode_ = sequence_mode;
}

bool LegacyFrameProcessor::ProcessFrames(
    const StreamParser::BufferQueue& audio_buffers,
    const StreamParser::BufferQueue& video_buffers,
    const StreamParser::TextBufferQueueMap& text_map,
    base::TimeDelta append_window_start,
    base::TimeDelta append_window_end,
    bool* new_media_segment,
    base::TimeDelta* timestamp_offset) {
  DVLOG(2) << __FUNCTION__ << "()";
  DCHECK(new_media_segment);
  DCHECK(timestamp_offset);

  // NOTE: Legacy coded frame processing does not update timestamp offset.
  base::TimeDelta offset = *timestamp_offset;

  DCHECK(!audio_buffers.empty() || !video_buffers.empty() ||
         !text_map.empty());

  MseTrackBuffer* audio_track = FindTrack(kAudioTrackId);
  DCHECK(audio_buffers.empty() || audio_track);

  MseTrackBuffer* video_track = FindTrack(kVideoTrackId);
  DCHECK(video_buffers.empty() || video_track);

  // TODO(wolenetz): DCHECK + return false if any of these buffers have UNKNOWN
  // type() in upcoming coded frame processing compliant implementation. See
  // http://crbug.com/249422.

  StreamParser::BufferQueue filtered_audio;
  StreamParser::BufferQueue filtered_video;

  if (audio_track) {
    AdjustBufferTimestamps(audio_buffers, offset);
    FilterWithAppendWindow(append_window_start, append_window_end,
                           audio_buffers, audio_track,
                           new_media_segment, &filtered_audio);
  }

  if (video_track) {
    AdjustBufferTimestamps(video_buffers, offset);
    FilterWithAppendWindow(append_window_start, append_window_end,
                           video_buffers, video_track,
                           new_media_segment, &filtered_video);
  }

  if ((!filtered_audio.empty() || !filtered_video.empty()) &&
      *new_media_segment) {
    // Find the earliest timestamp in the filtered buffers and use that for the
    // segment start timestamp.
    base::TimeDelta segment_timestamp = kNoTimestamp();

    if (!filtered_audio.empty())
      segment_timestamp = filtered_audio.front()->GetDecodeTimestamp();

    if (!filtered_video.empty() &&
        (segment_timestamp == kNoTimestamp() ||
         filtered_video.front()->GetDecodeTimestamp() < segment_timestamp)) {
      segment_timestamp = filtered_video.front()->GetDecodeTimestamp();
    }

    *new_media_segment = false;

    for (TrackBufferMap::iterator itr = track_buffers_.begin();
         itr != track_buffers_.end(); ++itr) {
      itr->second->stream()->OnNewMediaSegment(segment_timestamp);
    }
  }

  if (!filtered_audio.empty() &&
      !AppendAndUpdateDuration(audio_track->stream(), filtered_audio)) {
    return false;
  }

  if (!filtered_video.empty() &&
      !AppendAndUpdateDuration(video_track->stream(), filtered_video)) {
    return false;
  }

  if (text_map.empty())
    return true;

  // Process any buffers for each of the text tracks in the map.
  bool all_text_buffers_empty = true;
  for (StreamParser::TextBufferQueueMap::const_iterator itr = text_map.begin();
       itr != text_map.end();
       ++itr) {
    const StreamParser::BufferQueue text_buffers = itr->second;
    if (text_buffers.empty())
      continue;

    all_text_buffers_empty = false;
    if (!OnTextBuffers(itr->first, append_window_start, append_window_end,
                       offset, text_buffers, new_media_segment)) {
      return false;
    }
  }

  DCHECK(!all_text_buffers_empty);
  return true;
}

void LegacyFrameProcessor::AdjustBufferTimestamps(
    const StreamParser::BufferQueue& buffers,
    base::TimeDelta timestamp_offset) {
  if (timestamp_offset == base::TimeDelta())
    return;

  for (StreamParser::BufferQueue::const_iterator itr = buffers.begin();
       itr != buffers.end(); ++itr) {
    (*itr)->SetDecodeTimestamp(
        (*itr)->GetDecodeTimestamp() + timestamp_offset);
    (*itr)->set_timestamp((*itr)->timestamp() + timestamp_offset);
  }
}

void LegacyFrameProcessor::FilterWithAppendWindow(
    base::TimeDelta append_window_start,
    base::TimeDelta append_window_end,
    const StreamParser::BufferQueue& buffers,
    MseTrackBuffer* track,
    bool* new_media_segment,
    StreamParser::BufferQueue* filtered_buffers) {
  DCHECK(track);
  DCHECK(new_media_segment);
  DCHECK(filtered_buffers);

  // This loop implements steps 1.9, 1.10, & 1.11 of the "Coded frame
  // processing loop" in the Media Source Extensions spec.
  // These steps filter out buffers that are not within the "append
  // window" and handles resyncing on the next random access point
  // (i.e., next keyframe) if a buffer gets dropped.
  for (StreamParser::BufferQueue::const_iterator itr = buffers.begin();
       itr != buffers.end(); ++itr) {
    // Filter out buffers that are outside the append window. Anytime
    // a buffer gets dropped we need to set |*needs_keyframe| to true
    // because we can only resume decoding at keyframes.
    base::TimeDelta presentation_timestamp = (*itr)->timestamp();

    // TODO(acolwell): Change |frame_end_timestamp| value to
    // |presentation_timestamp + (*itr)->duration()|, like the spec
    // requires, once frame durations are actually present in all buffers.
    base::TimeDelta frame_end_timestamp = presentation_timestamp;
    if (presentation_timestamp < append_window_start ||
        frame_end_timestamp > append_window_end) {
      DVLOG(1) << "Dropping buffer outside append window."
               << " presentation_timestamp "
               << presentation_timestamp.InSecondsF();
      track->set_needs_random_access_point(true);

      // This triggers a discontinuity so we need to treat the next frames
      // appended within the append window as if they were the beginning of a
      // new segment.
      *new_media_segment = true;
      continue;
    }

    // If the track needs a keyframe, then filter out buffers until we
    // encounter the next keyframe.
    if (track->needs_random_access_point()) {
      if (!(*itr)->IsKeyframe()) {
        DVLOG(1) << "Dropping non-keyframe. presentation_timestamp "
                 << presentation_timestamp.InSecondsF();
        continue;
      }

      track->set_needs_random_access_point(false);
    }

    filtered_buffers->push_back(*itr);
  }
}

bool LegacyFrameProcessor::AppendAndUpdateDuration(
    ChunkDemuxerStream* stream,
    const StreamParser::BufferQueue& buffers) {
  DCHECK(!buffers.empty());

  if (!stream || !stream->Append(buffers))
    return false;

  increase_duration_cb_.Run(buffers.back()->timestamp(), stream);
  return true;
}

bool LegacyFrameProcessor::OnTextBuffers(
    StreamParser::TrackId text_track_id,
    base::TimeDelta append_window_start,
    base::TimeDelta append_window_end,
    base::TimeDelta timestamp_offset,
    const StreamParser::BufferQueue& buffers,
    bool* new_media_segment) {
  DCHECK(!buffers.empty());
  DCHECK(text_track_id != kAudioTrackId && text_track_id != kVideoTrackId);
  DCHECK(new_media_segment);

  MseTrackBuffer* track = FindTrack(text_track_id);
  if (!track)
    return false;

  AdjustBufferTimestamps(buffers, timestamp_offset);

  StreamParser::BufferQueue filtered_buffers;
  track->set_needs_random_access_point(false);
  FilterWithAppendWindow(append_window_start, append_window_end,
                         buffers, track, new_media_segment, &filtered_buffers);

  if (filtered_buffers.empty())
    return true;

  return AppendAndUpdateDuration(track->stream(), filtered_buffers);
}

}  // namespace media