media_source_player_unittest.cc revision cedac228d2dd51db4b79ea1e72c7f249408ee061
1// Copyright 2013 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include <string>
6
7#include "base/basictypes.h"
8#include "base/logging.h"
9#include "base/memory/scoped_ptr.h"
10#include "base/strings/stringprintf.h"
11#include "media/base/android/media_codec_bridge.h"
12#include "media/base/android/media_drm_bridge.h"
13#include "media/base/android/media_player_manager.h"
14#include "media/base/android/media_source_player.h"
15#include "media/base/bind_to_current_loop.h"
16#include "media/base/decoder_buffer.h"
17#include "media/base/test_data_util.h"
18#include "testing/gmock/include/gmock/gmock.h"
19#include "ui/gl/android/surface_texture.h"
20
21namespace media {
22
23// Helper macro to skip the test if MediaCodecBridge isn't available.
24#define SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE()        \
25  do {                                                            \
26    if (!MediaCodecBridge::IsAvailable()) {                       \
27      VLOG(0) << "Could not run test - not supported on device."; \
28      return;                                                     \
29    }                                                             \
30  } while (0)
31
32const base::TimeDelta kDefaultDuration =
33    base::TimeDelta::FromMilliseconds(10000);
34
35// TODO(wolenetz/qinmin): Simplify tests with more effective mock usage, and
36// fix flaky pointer-based MDJ inequality testing. See http://crbug.com/327839.
37
38// Mock of MediaPlayerManager for testing purpose.
39class MockMediaPlayerManager : public MediaPlayerManager {
40 public:
41  explicit MockMediaPlayerManager(base::MessageLoop* message_loop)
42      : message_loop_(message_loop),
43        playback_completed_(false),
44        num_resources_requested_(0),
45        num_resources_released_(0),
46        timestamp_updated_(false) {}
47  virtual ~MockMediaPlayerManager() {}
48
49  // MediaPlayerManager implementation.
50  virtual MediaResourceGetter* GetMediaResourceGetter() OVERRIDE {
51    return NULL;
52  }
53  virtual void OnTimeUpdate(int player_id,
54                            base::TimeDelta current_time) OVERRIDE {
55    timestamp_updated_ = true;
56  }
57  virtual void OnMediaMetadataChanged(
58      int player_id, base::TimeDelta duration, int width, int height,
59      bool success) OVERRIDE {}
60  virtual void OnPlaybackComplete(int player_id) OVERRIDE {
61    playback_completed_ = true;
62    if (message_loop_->is_running())
63      message_loop_->Quit();
64  }
65  virtual void OnMediaInterrupted(int player_id) OVERRIDE {}
66  virtual void OnBufferingUpdate(int player_id, int percentage) OVERRIDE {}
67  virtual void OnSeekComplete(int player_id,
68                              const base::TimeDelta& current_time) OVERRIDE {}
69  virtual void OnError(int player_id, int error) OVERRIDE {}
70  virtual void OnVideoSizeChanged(int player_id, int width,
71                                  int height) OVERRIDE {}
72  virtual MediaPlayerAndroid* GetFullscreenPlayer() OVERRIDE { return NULL; }
73  virtual MediaPlayerAndroid* GetPlayer(int player_id) OVERRIDE { return NULL; }
74  virtual void DestroyAllMediaPlayers() OVERRIDE {}
75  virtual MediaKeys* GetCdm(int cdm_id) OVERRIDE { return NULL; }
76  virtual void RequestFullScreen(int player_id) OVERRIDE {}
77  virtual void OnSessionCreated(int cdm_id,
78                                uint32 session_id,
79                                const std::string& web_session_id) OVERRIDE {}
80  virtual void OnSessionMessage(int cdm_id,
81                                uint32 session_id,
82                                const std::vector<uint8>& message,
83                                const GURL& destination_url) OVERRIDE {}
84  virtual void OnSessionReady(int cdm_id, uint32 session_id) OVERRIDE {}
85  virtual void OnSessionClosed(int cdm_id, uint32 session_id) OVERRIDE {}
86  virtual void OnSessionError(int cdm_id,
87                              uint32 session_id,
88                              media::MediaKeys::KeyError error_code,
89                              uint32 system_code) OVERRIDE {}
90
91  bool playback_completed() const {
92    return playback_completed_;
93  }
94
95  int num_resources_requested() const {
96    return num_resources_requested_;
97  }
98
99  int num_resources_released() const {
100    return num_resources_released_;
101  }
102
103  void OnMediaResourcesRequested(int player_id) {
104    num_resources_requested_++;
105  }
106
107  void OnMediaResourcesReleased(int player_id) {
108    num_resources_released_++;
109  }
110
111  bool timestamp_updated() const {
112    return timestamp_updated_;
113  }
114
115  void ResetTimestampUpdated() {
116    timestamp_updated_ = false;
117  }
118
119 private:
120  base::MessageLoop* message_loop_;
121  bool playback_completed_;
122  // The number of resource requests this object has seen.
123  int num_resources_requested_;
124  // The number of released resources.
125  int num_resources_released_;
126  // Playback timestamp was updated.
127  bool timestamp_updated_;
128
129  DISALLOW_COPY_AND_ASSIGN(MockMediaPlayerManager);
130};
131
132class MockDemuxerAndroid : public DemuxerAndroid {
133 public:
134  explicit MockDemuxerAndroid(base::MessageLoop* message_loop)
135      : message_loop_(message_loop),
136        num_data_requests_(0),
137        num_seek_requests_(0),
138        num_browser_seek_requests_(0) {}
139  virtual ~MockDemuxerAndroid() {}
140
141  virtual void Initialize(DemuxerAndroidClient* client) OVERRIDE {}
142  virtual void RequestDemuxerData(DemuxerStream::Type type) OVERRIDE {
143    num_data_requests_++;
144    if (message_loop_->is_running())
145      message_loop_->Quit();
146  }
147  virtual void RequestDemuxerSeek(const base::TimeDelta& time_to_seek,
148                                  bool is_browser_seek) OVERRIDE {
149    num_seek_requests_++;
150    if (is_browser_seek)
151      num_browser_seek_requests_++;
152  }
153
154  int num_data_requests() const { return num_data_requests_; }
155  int num_seek_requests() const { return num_seek_requests_; }
156  int num_browser_seek_requests() const { return num_browser_seek_requests_; }
157
158 private:
159  base::MessageLoop* message_loop_;
160
161  // The number of encoded data requests this object has seen.
162  int num_data_requests_;
163
164  // The number of regular and browser seek requests this object has seen.
165  int num_seek_requests_;
166
167  // The number of browser seek requests this object has seen.
168  int num_browser_seek_requests_;
169
170  DISALLOW_COPY_AND_ASSIGN(MockDemuxerAndroid);
171};
172
173class MediaSourcePlayerTest : public testing::Test {
174 public:
175  MediaSourcePlayerTest()
176      : manager_(&message_loop_),
177        demuxer_(new MockDemuxerAndroid(&message_loop_)),
178        player_(0, &manager_,
179                base::Bind(&MockMediaPlayerManager::OnMediaResourcesRequested,
180                           base::Unretained(&manager_)),
181                base::Bind(&MockMediaPlayerManager::OnMediaResourcesReleased,
182                           base::Unretained(&manager_)),
183                scoped_ptr<DemuxerAndroid>(demuxer_)),
184        decoder_callback_hook_executed_(false),
185        surface_texture_a_is_next_(true) {}
186  virtual ~MediaSourcePlayerTest() {}
187
188 protected:
189  // Get the decoder job from the MediaSourcePlayer.
190  MediaDecoderJob* GetMediaDecoderJob(bool is_audio) {
191    if (is_audio) {
192      return reinterpret_cast<MediaDecoderJob*>(
193          player_.audio_decoder_job_.get());
194    }
195    return reinterpret_cast<MediaDecoderJob*>(
196        player_.video_decoder_job_.get());
197  }
198
199  // Get the per-job prerolling status from the MediaSourcePlayer's job matching
200  // |is_audio|. Caller must guard against NPE if the player's job is NULL.
201  bool IsPrerolling(bool is_audio) {
202    return GetMediaDecoderJob(is_audio)->prerolling();
203  }
204
205  // Get the preroll timestamp from the MediaSourcePlayer.
206  base::TimeDelta GetPrerollTimestamp() {
207    return player_.preroll_timestamp_;
208  }
209
210  // Simulate player has reached starvation timeout.
211  void TriggerPlayerStarvation() {
212    player_.decoder_starvation_callback_.Cancel();
213    player_.OnDecoderStarved();
214  }
215
216  // Release() the player.
217  void ReleasePlayer() {
218    EXPECT_TRUE(player_.IsPlaying());
219    player_.Release();
220    EXPECT_FALSE(player_.IsPlaying());
221    EXPECT_FALSE(GetMediaDecoderJob(true));
222    EXPECT_FALSE(GetMediaDecoderJob(false));
223  }
224
225  // Upon the next successful decode callback, post a task to call Release()
226  // on the |player_|. TEST_F's do not have access to the private player
227  // members, hence this helper method.
228  // Prevent usage creep of MSP::set_decode_callback_for_testing() by
229  // only using it for the ReleaseWithOnPrefetchDoneAlreadyPosted test.
230  void OnNextTestDecodeCallbackPostTaskToReleasePlayer() {
231    DCHECK_EQ(&message_loop_, base::MessageLoop::current());
232    player_.set_decode_callback_for_testing(media::BindToCurrentLoop(
233      base::Bind(
234          &MediaSourcePlayerTest::ReleaseWithPendingPrefetchDoneVerification,
235          base::Unretained(this))));
236  }
237
238  // Asynch test callback posted upon decode completion to verify that a pending
239  // prefetch done event is cleared across |player_|'s Release(). This helps
240  // ensure the ReleaseWithOnPrefetchDoneAlreadyPosted test scenario is met.
241  void ReleaseWithPendingPrefetchDoneVerification() {
242    EXPECT_TRUE(player_.IsEventPending(player_.PREFETCH_DONE_EVENT_PENDING));
243    ReleasePlayer();
244    EXPECT_FALSE(player_.IsEventPending(player_.PREFETCH_DONE_EVENT_PENDING));
245    EXPECT_FALSE(decoder_callback_hook_executed_);
246    decoder_callback_hook_executed_ = true;
247  }
248
249  // Inspect internal pending_event_ state of |player_|. This is for infrequent
250  // use by tests, only where required.
251  bool IsPendingSurfaceChange() {
252    return player_.IsEventPending(player_.SURFACE_CHANGE_EVENT_PENDING);
253  }
254
255  DemuxerConfigs CreateAudioDemuxerConfigs(AudioCodec audio_codec) {
256    DemuxerConfigs configs;
257    configs.audio_codec = audio_codec;
258    configs.audio_channels = 2;
259    configs.is_audio_encrypted = false;
260    configs.duration = kDefaultDuration;
261
262    if (audio_codec == kCodecVorbis) {
263      configs.audio_sampling_rate = 44100;
264      scoped_refptr<DecoderBuffer> buffer = ReadTestDataFile(
265          "vorbis-extradata");
266      configs.audio_extra_data = std::vector<uint8>(
267          buffer->data(),
268          buffer->data() + buffer->data_size());
269      return configs;
270    }
271
272    // Other codecs are not yet supported by this helper.
273    EXPECT_EQ(audio_codec, kCodecAAC);
274
275    configs.audio_sampling_rate = 48000;
276    uint8 aac_extra_data[] = { 0x13, 0x10 };
277    configs.audio_extra_data = std::vector<uint8>(
278        aac_extra_data,
279        aac_extra_data + 2);
280    return configs;
281  }
282
283  DemuxerConfigs CreateVideoDemuxerConfigs() {
284    DemuxerConfigs configs;
285    configs.video_codec = kCodecVP8;
286    configs.video_size = gfx::Size(320, 240);
287    configs.is_video_encrypted = false;
288    configs.duration = kDefaultDuration;
289    return configs;
290  }
291
292  DemuxerConfigs CreateAudioVideoDemuxerConfigs() {
293    DemuxerConfigs configs = CreateAudioDemuxerConfigs(kCodecVorbis);
294    configs.video_codec = kCodecVP8;
295    configs.video_size = gfx::Size(320, 240);
296    configs.is_video_encrypted = false;
297    return configs;
298  }
299
300  DemuxerConfigs CreateDemuxerConfigs(bool have_audio, bool have_video) {
301    DCHECK(have_audio || have_video);
302
303    if (have_audio && !have_video)
304      return CreateAudioDemuxerConfigs(kCodecVorbis);
305
306    if (have_video && !have_audio)
307      return CreateVideoDemuxerConfigs();
308
309    return CreateAudioVideoDemuxerConfigs();
310  }
311
312  // Starts an audio decoder job.
313  void StartAudioDecoderJob() {
314    Start(CreateAudioDemuxerConfigs(kCodecVorbis));
315  }
316
317  // Starts a video decoder job.
318  void StartVideoDecoderJob() {
319    Start(CreateVideoDemuxerConfigs());
320  }
321
322  // Starts decoding the data.
323  void Start(const DemuxerConfigs& configs) {
324    EXPECT_EQ(demuxer_->num_data_requests(), 0);
325    player_.OnDemuxerConfigsAvailable(configs);
326    player_.Start();
327
328    EXPECT_TRUE(player_.IsPlaying());
329    int expected_num_requests = (GetMediaDecoderJob(true) ? 1 : 0) +
330        (GetMediaDecoderJob(false) ? 1 : 0);
331    EXPECT_EQ(expected_num_requests, demuxer_->num_data_requests());
332  }
333
334  // Resumes decoding the data. Verifies player behavior relative to
335  // |expect_player_requests_audio_data| and
336  // |expect_player_requests_video_data|.
337  void Resume(bool expect_player_requests_audio_data,
338              bool expect_player_requests_video_data) {
339    EXPECT_FALSE(player_.IsPlaying());
340    EXPECT_TRUE(player_.HasVideo() || player_.HasAudio());
341    int original_num_data_requests = demuxer_->num_data_requests();
342    int expected_request_delta =
343        (expect_player_requests_audio_data ? 1 : 0) +
344        (expect_player_requests_video_data ? 1 : 0);
345
346    player_.Start();
347
348    EXPECT_TRUE(player_.IsPlaying());
349    EXPECT_EQ(original_num_data_requests + expected_request_delta,
350              demuxer_->num_data_requests());
351    EXPECT_EQ(expect_player_requests_audio_data,
352              expect_player_requests_audio_data && GetMediaDecoderJob(true));
353    EXPECT_EQ(expect_player_requests_video_data,
354              expect_player_requests_video_data && GetMediaDecoderJob(false));
355  }
356
357  // Keeps decoding audio data until the decoder starts to output samples.
358  // Gives up if no audio output after decoding 10 frames.
359  void DecodeAudioDataUntilOutputBecomesAvailable() {
360    EXPECT_TRUE(player_.IsPlaying());
361    base::TimeDelta current_time = player_.GetCurrentTime();
362    base::TimeDelta start_timestamp = current_time;
363    for (int i = 0; i < 10; ++i) {
364      manager_.ResetTimestampUpdated();
365      player_.OnDemuxerDataAvailable(
366          CreateReadFromDemuxerAckForAudio(i > 3 ? 3 : i));
367      WaitForAudioDecodeDone();
368      base::TimeDelta new_current_time = player_.GetCurrentTime();
369      EXPECT_LE(current_time.InMilliseconds(),
370                new_current_time.InMilliseconds());
371      current_time = new_current_time;
372      if (manager_.timestamp_updated()) {
373        EXPECT_LT(start_timestamp.InMillisecondsF(),
374                  new_current_time.InMillisecondsF());
375        return;
376      }
377    }
378    EXPECT_TRUE(false);
379  }
380
381  AccessUnit CreateAccessUnitWithData(bool is_audio, int audio_packet_id) {
382    AccessUnit unit;
383
384    unit.status = DemuxerStream::kOk;
385    scoped_refptr<DecoderBuffer> buffer = ReadTestDataFile(
386        is_audio ? base::StringPrintf("vorbis-packet-%d", audio_packet_id)
387            : "vp8-I-frame-320x240");
388    unit.data = std::vector<uint8>(
389        buffer->data(), buffer->data() + buffer->data_size());
390
391    if (is_audio) {
392      // Vorbis needs 4 extra bytes padding on Android to decode properly. Check
393      // NuMediaExtractor.cpp in Android source code.
394      uint8 padding[4] = { 0xff , 0xff , 0xff , 0xff };
395      unit.data.insert(unit.data.end(), padding, padding + 4);
396    }
397
398    return unit;
399  }
400
401  DemuxerData CreateReadFromDemuxerAckForAudio(int packet_id) {
402    DemuxerData data;
403    data.type = DemuxerStream::AUDIO;
404    data.access_units.resize(1);
405    data.access_units[0] = CreateAccessUnitWithData(true, packet_id);
406    return data;
407  }
408
409  DemuxerData CreateReadFromDemuxerAckForVideo() {
410    DemuxerData data;
411    data.type = DemuxerStream::VIDEO;
412    data.access_units.resize(1);
413    data.access_units[0] = CreateAccessUnitWithData(false, 0);
414    return data;
415  }
416
417  DemuxerData CreateEOSAck(bool is_audio) {
418    DemuxerData data;
419    data.type = is_audio ? DemuxerStream::AUDIO : DemuxerStream::VIDEO;
420    data.access_units.resize(1);
421    data.access_units[0].status = DemuxerStream::kOk;
422    data.access_units[0].end_of_stream = true;
423    return data;
424  }
425
426  DemuxerData CreateAbortedAck(bool is_audio) {
427    DemuxerData data;
428    data.type = is_audio ? DemuxerStream::AUDIO : DemuxerStream::VIDEO;
429    data.access_units.resize(1);
430    data.access_units[0].status = DemuxerStream::kAborted;
431    return data;
432  }
433
434  // Helper method for use at test start. It starts an audio decoder job and
435  // immediately feeds it some data to decode. Then, without letting the decoder
436  // job complete a decode cycle, it also starts player SeekTo(). Upon return,
437  // the player should not yet have sent the DemuxerSeek IPC request, though
438  // seek event should be pending. The audio decoder job will also still be
439  // decoding.
440  void StartAudioDecoderJobAndSeekToWhileDecoding(
441      const base::TimeDelta& seek_time) {
442    EXPECT_FALSE(GetMediaDecoderJob(true));
443    EXPECT_FALSE(player_.IsPlaying());
444    EXPECT_EQ(0, demuxer_->num_data_requests());
445    EXPECT_EQ(0.0, GetPrerollTimestamp().InMillisecondsF());
446    EXPECT_EQ(player_.GetCurrentTime(), GetPrerollTimestamp());
447    StartAudioDecoderJob();
448    EXPECT_FALSE(GetMediaDecoderJob(true)->is_decoding());
449    player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForAudio(0));
450    EXPECT_EQ(2, demuxer_->num_data_requests());
451    EXPECT_TRUE(GetMediaDecoderJob(true)->is_decoding());
452    player_.SeekTo(seek_time);
453    EXPECT_EQ(0.0, GetPrerollTimestamp().InMillisecondsF());
454    EXPECT_EQ(0, demuxer_->num_seek_requests());
455    player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForAudio(0));
456  }
457
458  // Seek, including simulated receipt of |kAborted| read between SeekTo() and
459  // OnDemuxerSeekDone(). Use this helper method only when the player already
460  // has created the decoder job. Exactly one request for more data is expected
461  // following the seek, so use this helper for players with only audio or only
462  // video.
463  void SeekPlayerWithAbort(bool is_audio, const base::TimeDelta& seek_time) {
464    int original_num_seeks = demuxer_->num_seek_requests();
465    int original_num_data_requests = demuxer_->num_data_requests();
466
467    // Initiate a seek. Skip the round-trip of requesting seek from renderer.
468    // Instead behave as if the renderer has asked us to seek.
469    player_.SeekTo(seek_time);
470
471    // Verify that the seek does not occur until previously outstanding data
472    // request is satisfied.
473    EXPECT_EQ(original_num_seeks, demuxer_->num_seek_requests());
474
475    // Simulate seeking causes the demuxer to abort the outstanding read
476    // caused by the seek.
477    player_.OnDemuxerDataAvailable(CreateAbortedAck(is_audio));
478
479    // Verify that the seek is requested.
480    EXPECT_EQ(original_num_seeks + 1, demuxer_->num_seek_requests());
481
482    // Send back the seek done notification. This should trigger the player to
483    // call OnReadFromDemuxer() again.
484    EXPECT_EQ(original_num_data_requests, demuxer_->num_data_requests());
485    player_.OnDemuxerSeekDone(kNoTimestamp());
486    EXPECT_EQ(original_num_data_requests + 1, demuxer_->num_data_requests());
487
488    // No other seek should have been requested.
489    EXPECT_EQ(original_num_seeks + 1, demuxer_->num_seek_requests());
490  }
491
492  // Preroll the decoder job to |target_timestamp|. The first access unit
493  // to decode will have a timestamp equal to |start_timestamp|.
494  // TODO(qinmin): Add additional test cases for out-of-order decodes.
495  // See http://crbug.com/331421.
496  void PrerollDecoderToTime(bool is_audio,
497                            const base::TimeDelta& start_timestamp,
498                            const base::TimeDelta& target_timestamp) {
499    EXPECT_EQ(target_timestamp, player_.GetCurrentTime());
500    // |start_timestamp| must be smaller than |target_timestamp|.
501    EXPECT_LE(start_timestamp, target_timestamp);
502    DemuxerData data = is_audio ? CreateReadFromDemuxerAckForAudio(1) :
503        CreateReadFromDemuxerAckForVideo();
504    int current_timestamp = start_timestamp.InMilliseconds();
505
506    // Send some data with access unit timestamps before the |target_timestamp|,
507    // and continue sending the data until preroll finishes.
508    // This simulates the common condition that AUs received after browser
509    // seek begin with timestamps before the seek target, and don't
510    // immediately complete preroll.
511    while (IsPrerolling(is_audio)) {
512      data.access_units[0].timestamp =
513          base::TimeDelta::FromMilliseconds(current_timestamp);
514      player_.OnDemuxerDataAvailable(data);
515      EXPECT_TRUE(GetMediaDecoderJob(is_audio)->is_decoding());
516      EXPECT_EQ(target_timestamp, player_.GetCurrentTime());
517      current_timestamp += 30;
518      WaitForDecodeDone(is_audio, !is_audio);
519    }
520    EXPECT_LE(target_timestamp, player_.GetCurrentTime());
521  }
522
523  DemuxerData CreateReadFromDemuxerAckWithConfigChanged(bool is_audio,
524                                                        int config_unit_index) {
525    DemuxerData data;
526    data.type = is_audio ? DemuxerStream::AUDIO : DemuxerStream::VIDEO;
527    data.access_units.resize(config_unit_index + 1);
528
529    for (int i = 0; i < config_unit_index; ++i)
530      data.access_units[i] = CreateAccessUnitWithData(is_audio, i);
531
532    data.access_units[config_unit_index].status = DemuxerStream::kConfigChanged;
533    data.demuxer_configs.resize(1);
534    data.demuxer_configs[0] = CreateDemuxerConfigs(is_audio, !is_audio);
535    return data;
536  }
537
538  // Valid only for video-only player tests. If |trigger_with_release_start| is
539  // true, triggers the browser seek with a Release() + video data received +
540  // Start() with a new surface. If false, triggers the browser seek by
541  // setting a new video surface after beginning decode of received video data.
542  // Such data receipt causes possibility that an I-frame is not next, and
543  // browser seek results once decode completes and surface change processing
544  // begins.
545  void BrowserSeekPlayer(bool trigger_with_release_start) {
546    int expected_num_data_requests = demuxer_->num_data_requests() +
547        (trigger_with_release_start ? 1 : 2);
548    int expected_num_seek_requests = demuxer_->num_seek_requests();
549    int expected_num_browser_seek_requests =
550        demuxer_->num_browser_seek_requests();
551
552    EXPECT_FALSE(GetMediaDecoderJob(false));
553    CreateNextTextureAndSetVideoSurface();
554    StartVideoDecoderJob();
555    if (trigger_with_release_start) {
556      ReleasePlayer();
557      // Simulate demuxer's response to the video data request. The data will be
558      // discarded.
559      player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo());
560      EXPECT_FALSE(GetMediaDecoderJob(false));
561      EXPECT_FALSE(player_.IsPlaying());
562      EXPECT_EQ(expected_num_seek_requests, demuxer_->num_seek_requests());
563
564      CreateNextTextureAndSetVideoSurface();
565      Resume(false, false);
566      EXPECT_FALSE(GetMediaDecoderJob(false));
567    } else {
568      // Simulate demuxer's response to the video data request.
569      player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo());
570
571      // While the decoder is decoding, trigger a browser seek by changing
572      // surface. Demuxer does not know of browser seek in advance, so no
573      // |kAborted| data is required (though |kAborted| can certainly occur for
574      // any pending read in reality due to renderer preparing for a regular
575      // seek).
576      CreateNextTextureAndSetVideoSurface();
577
578      // Browser seek should not begin until decoding has completed.
579      EXPECT_TRUE(GetMediaDecoderJob(false));
580      EXPECT_EQ(expected_num_seek_requests, demuxer_->num_seek_requests());
581
582      // Wait for the decoder job to finish decoding and be reset pending the
583      // browser seek.
584      WaitForVideoDecodeDone();
585      player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo());
586    }
587
588    // Only one browser seek should have been initiated, and no further data
589    // should have been requested.
590    expected_num_seek_requests++;
591    expected_num_browser_seek_requests++;
592    EXPECT_EQ(expected_num_seek_requests, demuxer_->num_seek_requests());
593    EXPECT_EQ(expected_num_browser_seek_requests,
594              demuxer_->num_browser_seek_requests());
595    EXPECT_EQ(expected_num_data_requests, demuxer_->num_data_requests());
596  }
597
598  // Creates a new decoder job and feeds it data ending with a |kConfigChanged|
599  // access unit. If |config_unit_in_prefetch| is true, sends feeds the config
600  // change AU in response to the job's first read request (prefetch). If
601  // false, regular data is fed and decoded prior to feeding the config change
602  // AU in response to the second data request (after prefetch completed).
603  // |config_unit_index| controls which access unit is |kConfigChanged|.
604  void StartConfigChange(bool is_audio,
605                         bool config_unit_in_prefetch,
606                         int config_unit_index) {
607    EXPECT_FALSE(GetMediaDecoderJob(is_audio));
608    if (is_audio) {
609      StartAudioDecoderJob();
610    } else {
611      CreateNextTextureAndSetVideoSurface();
612      StartVideoDecoderJob();
613    }
614
615    int expected_num_data_requests = demuxer_->num_data_requests();
616
617    // Feed and decode a standalone access unit so the player exits prefetch.
618    if (!config_unit_in_prefetch) {
619      if (is_audio)
620        player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForAudio(0));
621      else
622        player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo());
623
624      WaitForDecodeDone(is_audio, !is_audio);
625
626      // We should have completed the prefetch phase at this point.
627      expected_num_data_requests++;
628      EXPECT_EQ(expected_num_data_requests, demuxer_->num_data_requests());
629    }
630
631    // Feed and decode access units with data for any units prior to
632    // |config_unit_index|, and a |kConfigChanged| unit at that index.
633    // Player should prepare to reconfigure the decoder job, and should request
634    // new demuxer configs.
635    player_.OnDemuxerDataAvailable(
636        CreateReadFromDemuxerAckWithConfigChanged(is_audio, config_unit_index));
637    // Run until decoder starts to request new data.
638    while (demuxer_->num_data_requests() == expected_num_data_requests)
639      message_loop_.RunUntilIdle();
640  }
641
642  void CreateNextTextureAndSetVideoSurface() {
643    gfx::SurfaceTexture* surface_texture;
644    if (surface_texture_a_is_next_) {
645      surface_texture_a_ = gfx::SurfaceTexture::Create(next_texture_id_++);
646      surface_texture = surface_texture_a_.get();
647    } else {
648      surface_texture_b_ = gfx::SurfaceTexture::Create(next_texture_id_++);
649      surface_texture = surface_texture_b_.get();
650    }
651
652    surface_texture_a_is_next_ = !surface_texture_a_is_next_;
653    gfx::ScopedJavaSurface surface = gfx::ScopedJavaSurface(surface_texture);
654    player_.SetVideoSurface(surface.Pass());
655  }
656
657  // Wait for one or both of the jobs to complete decoding. Decoder jobs are
658  // assumed to exist for any stream whose decode completion is awaited.
659  void WaitForDecodeDone(bool wait_for_audio, bool wait_for_video) {
660    DCHECK(wait_for_audio || wait_for_video);
661    while ((wait_for_audio && GetMediaDecoderJob(true) &&
662               GetMediaDecoderJob(true)->HasData() &&
663               GetMediaDecoderJob(true)->is_decoding()) ||
664           (wait_for_video && GetMediaDecoderJob(false) &&
665               GetMediaDecoderJob(false)->HasData() &&
666               GetMediaDecoderJob(false)->is_decoding())) {
667      message_loop_.RunUntilIdle();
668    }
669  }
670
671  void WaitForAudioDecodeDone() {
672    WaitForDecodeDone(true, false);
673  }
674
675  void WaitForVideoDecodeDone() {
676    WaitForDecodeDone(false, true);
677  }
678
679  void WaitForAudioVideoDecodeDone() {
680    WaitForDecodeDone(true, true);
681  }
682
683  // If |send_eos| is true, generates EOS for the stream corresponding to
684  // |eos_for_audio|. Verifies that playback completes and no further data
685  // is requested.
686  // If |send_eos| is false, then it is assumed that caller previously arranged
687  // for player to receive EOS for each stream, but the player has not yet
688  // decoded all of them. In this case, |eos_for_audio| is ignored.
689  void VerifyPlaybackCompletesOnEOSDecode(bool send_eos, bool eos_for_audio) {
690    int original_num_data_requests = demuxer_->num_data_requests();
691    if (send_eos)
692      player_.OnDemuxerDataAvailable(CreateEOSAck(eos_for_audio));
693    EXPECT_FALSE(manager_.playback_completed());
694    message_loop_.Run();
695    EXPECT_TRUE(manager_.playback_completed());
696    EXPECT_EQ(original_num_data_requests, demuxer_->num_data_requests());
697  }
698
699  void VerifyCompletedPlaybackResumesOnSeekPlusStart(bool have_audio,
700                                                     bool have_video) {
701    DCHECK(have_audio || have_video);
702
703    EXPECT_TRUE(manager_.playback_completed());
704
705    player_.SeekTo(base::TimeDelta());
706    player_.OnDemuxerSeekDone(kNoTimestamp());
707    Resume(have_audio, have_video);
708  }
709
710  // Starts the appropriate decoder jobs according to |have_audio| and
711  // |have_video|. Then starts seek during decode of EOS or non-EOS according to
712  // |eos_audio| and |eos_video|. Simulates seek completion and verifies that
713  // playback never completed. |eos_{audio,video}| is ignored if the
714  // corresponding |have_{audio,video}| is false.
715  void VerifySeekDuringEOSDecodePreventsPlaybackCompletion(bool have_audio,
716                                                           bool have_video,
717                                                           bool eos_audio,
718                                                           bool eos_video) {
719    DCHECK(have_audio || have_video);
720
721    if (have_video)
722      CreateNextTextureAndSetVideoSurface();
723
724    Start(CreateDemuxerConfigs(have_audio, have_video));
725
726    if (have_audio)
727      player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForAudio(0));
728
729    if (have_video)
730      player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo());
731
732    // Run until more data is requested a number of times equal to the number of
733    // media types configured. Since prefetching may be in progress, we cannot
734    // reliably expect Run() to complete until we have sent demuxer data for all
735    // configured media types, above.
736    WaitForDecodeDone(have_audio, have_video);
737
738    // Simulate seek while decoding EOS or non-EOS for the appropriate
739    // stream(s).
740    if (have_audio) {
741      if (eos_audio)
742        player_.OnDemuxerDataAvailable(CreateEOSAck(true));
743      else
744        player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForAudio(1));
745    }
746
747    if (have_video) {
748      if (eos_video)
749        player_.OnDemuxerDataAvailable(CreateEOSAck(false));
750      else
751        player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo());
752    }
753
754    player_.SeekTo(base::TimeDelta());
755    EXPECT_EQ(0, demuxer_->num_seek_requests());
756    WaitForDecodeDone(have_audio, have_video);
757    EXPECT_EQ(1, demuxer_->num_seek_requests());
758
759    player_.OnDemuxerSeekDone(kNoTimestamp());
760    EXPECT_FALSE(manager_.playback_completed());
761  }
762
763  base::TimeTicks StartTimeTicks() {
764    return player_.start_time_ticks_;
765  }
766
767  base::MessageLoop message_loop_;
768  MockMediaPlayerManager manager_;
769  MockDemuxerAndroid* demuxer_;  // Owned by |player_|.
770  MediaSourcePlayer player_;
771
772  // Track whether a possibly async decoder callback test hook has run.
773  bool decoder_callback_hook_executed_;
774
775  // We need to keep the surface texture while the decoder is actively decoding.
776  // Otherwise, it may trigger unexpected crashes on some devices. To switch
777  // surfaces, tests need to create a new surface texture without releasing
778  // their previous one. In CreateNextTextureAndSetVideoSurface(), we toggle
779  // between two surface textures, only replacing the N-2 texture. Assumption is
780  // that no more than N-1 texture is in use by decoder when
781  // CreateNextTextureAndSetVideoSurface() is called.
782  scoped_refptr<gfx::SurfaceTexture> surface_texture_a_;
783  scoped_refptr<gfx::SurfaceTexture> surface_texture_b_;
784  bool surface_texture_a_is_next_;
785  int next_texture_id_;
786
787  DISALLOW_COPY_AND_ASSIGN(MediaSourcePlayerTest);
788};
789
790TEST_F(MediaSourcePlayerTest, StartAudioDecoderWithValidConfig) {
791  SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
792
793  // Test audio decoder job will be created when codec is successfully started.
794  StartAudioDecoderJob();
795  EXPECT_EQ(0, demuxer_->num_seek_requests());
796}
797
798TEST_F(MediaSourcePlayerTest, StartAudioDecoderWithInvalidConfig) {
799  SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
800
801  // Test audio decoder job will not be created when failed to start the codec.
802  DemuxerConfigs configs = CreateAudioDemuxerConfigs(kCodecVorbis);
803  // Replace with invalid |audio_extra_data|
804  configs.audio_extra_data.clear();
805  uint8 invalid_codec_data[] = { 0x00, 0xff, 0xff, 0xff, 0xff };
806  configs.audio_extra_data.insert(configs.audio_extra_data.begin(),
807                                 invalid_codec_data, invalid_codec_data + 4);
808  Start(configs);
809  EXPECT_EQ(0, demuxer_->num_seek_requests());
810}
811
812TEST_F(MediaSourcePlayerTest, StartVideoCodecWithValidSurface) {
813  SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
814
815  // Test video decoder job will be created when surface is valid.
816  // Video decoder job will not be created until surface is available.
817  StartVideoDecoderJob();
818
819  // Set both an initial and a later video surface without receiving any
820  // demuxed data yet.
821  CreateNextTextureAndSetVideoSurface();
822  MediaDecoderJob* first_job = GetMediaDecoderJob(false);
823  EXPECT_TRUE(first_job);
824  CreateNextTextureAndSetVideoSurface();
825
826  // Setting another surface will not create a new job until any pending
827  // read is satisfied (and job is no longer decoding).
828  EXPECT_EQ(first_job, GetMediaDecoderJob(false));
829
830  // No seeks, even on setting surface, should have occurred. (Browser seeks can
831  // occur on setting surface, but only after previously receiving video data.)
832  EXPECT_EQ(0, demuxer_->num_seek_requests());
833
834  // Note, the decoder job for the second surface set, above, will be created
835  // only after the pending read is satisfied and decoded, and the resulting
836  // browser seek is done. See BrowserSeek_* tests for this coverage.
837}
838
839TEST_F(MediaSourcePlayerTest, StartVideoCodecWithInvalidSurface) {
840  SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
841
842  // Test video decoder job will not be created when surface is invalid.
843  scoped_refptr<gfx::SurfaceTexture> surface_texture(
844      gfx::SurfaceTexture::Create(0));
845  gfx::ScopedJavaSurface surface(surface_texture.get());
846  StartVideoDecoderJob();
847
848  // Release the surface texture.
849  surface_texture = NULL;
850  player_.SetVideoSurface(surface.Pass());
851
852  // Player should not seek the demuxer on setting initial surface.
853  EXPECT_EQ(0, demuxer_->num_seek_requests());
854
855  EXPECT_FALSE(GetMediaDecoderJob(false));
856  EXPECT_EQ(0, demuxer_->num_data_requests());
857}
858
859TEST_F(MediaSourcePlayerTest, ReadFromDemuxerAfterSeek) {
860  SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
861
862  // Test decoder job will resend a ReadFromDemuxer request after seek.
863  StartAudioDecoderJob();
864  SeekPlayerWithAbort(true, base::TimeDelta());
865}
866
867TEST_F(MediaSourcePlayerTest, SetSurfaceWhileSeeking) {
868  SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
869
870  // Test SetVideoSurface() will not cause an extra seek while the player is
871  // waiting for demuxer to indicate seek is done.
872  // Player is still waiting for SetVideoSurface(), so no request is sent.
873  StartVideoDecoderJob();  // Verifies no data requested.
874
875  // Initiate a seek. Skip requesting element seek of renderer.
876  // Instead behave as if the renderer has asked us to seek.
877  EXPECT_EQ(0, demuxer_->num_seek_requests());
878  player_.SeekTo(base::TimeDelta());
879  EXPECT_EQ(1, demuxer_->num_seek_requests());
880
881  CreateNextTextureAndSetVideoSurface();
882  EXPECT_FALSE(GetMediaDecoderJob(false));
883  EXPECT_EQ(1, demuxer_->num_seek_requests());
884
885  // Reconfirm player has not yet requested data.
886  EXPECT_EQ(0, demuxer_->num_data_requests());
887
888  // Send the seek done notification. The player should start requesting data.
889  player_.OnDemuxerSeekDone(kNoTimestamp());
890  EXPECT_TRUE(GetMediaDecoderJob(false));
891  EXPECT_EQ(1, demuxer_->num_data_requests());
892
893  // Reconfirm exactly 1 seek request has been made of demuxer, and that it
894  // was not a browser seek request.
895  EXPECT_EQ(1, demuxer_->num_seek_requests());
896  EXPECT_EQ(0, demuxer_->num_browser_seek_requests());
897}
898
899TEST_F(MediaSourcePlayerTest, ChangeMultipleSurfaceWhileDecoding) {
900  SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
901
902  // Test MediaSourcePlayer can switch multiple surfaces during decoding.
903  CreateNextTextureAndSetVideoSurface();
904  StartVideoDecoderJob();
905  EXPECT_EQ(0, demuxer_->num_seek_requests());
906
907  // Send the first input chunk.
908  player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo());
909
910  // While the decoder is decoding, change multiple surfaces. Pass an empty
911  // surface first.
912  gfx::ScopedJavaSurface empty_surface;
913  player_.SetVideoSurface(empty_surface.Pass());
914  // Next, pass a new non-empty surface.
915  CreateNextTextureAndSetVideoSurface();
916
917  // Wait for the decoder job to finish decoding and be reset pending a browser
918  // seek.
919  WaitForVideoDecodeDone();
920  player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo());
921
922  // Only one browser seek should have been initiated. No further data request
923  // should have been processed on |message_loop_| before surface change event
924  // became pending, above.
925  EXPECT_EQ(1, demuxer_->num_browser_seek_requests());
926  EXPECT_EQ(2, demuxer_->num_data_requests());
927
928  // Simulate browser seek is done and confirm player requests more data for new
929  // video decoder job.
930  player_.OnDemuxerSeekDone(player_.GetCurrentTime());
931  EXPECT_TRUE(GetMediaDecoderJob(false));
932  EXPECT_EQ(3, demuxer_->num_data_requests());
933  EXPECT_EQ(1, demuxer_->num_seek_requests());
934}
935
936TEST_F(MediaSourcePlayerTest, SetEmptySurfaceAndStarveWhileDecoding) {
937  SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
938
939  // Test player pauses if an empty surface is passed.
940  CreateNextTextureAndSetVideoSurface();
941  StartVideoDecoderJob();
942  EXPECT_EQ(1, demuxer_->num_data_requests());
943
944  // Send the first input chunk.
945  player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo());
946
947  // While the decoder is decoding, pass an empty surface.
948  gfx::ScopedJavaSurface empty_surface;
949  player_.SetVideoSurface(empty_surface.Pass());
950  // Let the player starve. However, it should not issue any new data request in
951  // this case.
952  TriggerPlayerStarvation();
953  // Wait for the decoder job to finish decoding and be reset.
954  while (GetMediaDecoderJob(false))
955    message_loop_.RunUntilIdle();
956
957  // No further seek or data requests should have been received since the
958  // surface is empty.
959  EXPECT_EQ(0, demuxer_->num_browser_seek_requests());
960  EXPECT_EQ(2, demuxer_->num_data_requests());
961  player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo());
962
963  // Playback resumes once a non-empty surface is passed.
964  CreateNextTextureAndSetVideoSurface();
965  EXPECT_EQ(1, demuxer_->num_browser_seek_requests());
966}
967
968TEST_F(MediaSourcePlayerTest, ReleaseVideoDecoderResourcesWhileDecoding) {
969  SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
970
971  // Test that if video decoder is released while decoding, the resources will
972  // not be immediately released.
973  CreateNextTextureAndSetVideoSurface();
974  StartVideoDecoderJob();
975  EXPECT_EQ(1, manager_.num_resources_requested());
976  ReleasePlayer();
977  // The resources will be immediately released since the decoder is idle.
978  EXPECT_EQ(1, manager_.num_resources_released());
979  player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo());
980
981  // Recreate the video decoder.
982  CreateNextTextureAndSetVideoSurface();
983  player_.Start();
984  EXPECT_EQ(1, demuxer_->num_browser_seek_requests());
985  player_.OnDemuxerSeekDone(base::TimeDelta());
986  EXPECT_EQ(2, manager_.num_resources_requested());
987  player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo());
988  ReleasePlayer();
989  // The resource is still held by the video decoder until it finishes decoding.
990  EXPECT_EQ(1, manager_.num_resources_released());
991  // Wait for the decoder job to finish decoding and be reset.
992  while (manager_.num_resources_released() != 2)
993    message_loop_.RunUntilIdle();
994}
995
996TEST_F(MediaSourcePlayerTest, AudioOnlyStartAfterSeekFinish) {
997  SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
998
999  // Test audio decoder job will not start until pending seek event is handled.
1000  DemuxerConfigs configs = CreateAudioDemuxerConfigs(kCodecVorbis);
1001  player_.OnDemuxerConfigsAvailable(configs);
1002  EXPECT_FALSE(GetMediaDecoderJob(true));
1003
1004  // Initiate a seek. Skip requesting element seek of renderer.
1005  // Instead behave as if the renderer has asked us to seek.
1006  player_.SeekTo(base::TimeDelta());
1007  EXPECT_EQ(1, demuxer_->num_seek_requests());
1008
1009  player_.Start();
1010  EXPECT_FALSE(GetMediaDecoderJob(true));
1011  EXPECT_EQ(0, demuxer_->num_data_requests());
1012
1013  // Sending back the seek done notification.
1014  player_.OnDemuxerSeekDone(kNoTimestamp());
1015  EXPECT_TRUE(GetMediaDecoderJob(true));
1016  EXPECT_EQ(1, demuxer_->num_data_requests());
1017
1018  // Reconfirm exactly 1 seek request has been made of demuxer.
1019  EXPECT_EQ(1, demuxer_->num_seek_requests());
1020}
1021
1022TEST_F(MediaSourcePlayerTest, VideoOnlyStartAfterSeekFinish) {
1023  SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1024
1025  // Test video decoder job will not start until pending seek event is handled.
1026  CreateNextTextureAndSetVideoSurface();
1027  DemuxerConfigs configs = CreateVideoDemuxerConfigs();
1028  player_.OnDemuxerConfigsAvailable(configs);
1029  EXPECT_FALSE(GetMediaDecoderJob(false));
1030
1031  // Initiate a seek. Skip requesting element seek of renderer.
1032  // Instead behave as if the renderer has asked us to seek.
1033  player_.SeekTo(base::TimeDelta());
1034  EXPECT_EQ(1, demuxer_->num_seek_requests());
1035
1036  player_.Start();
1037  EXPECT_FALSE(GetMediaDecoderJob(false));
1038  EXPECT_EQ(0, demuxer_->num_data_requests());
1039
1040  // Sending back the seek done notification.
1041  player_.OnDemuxerSeekDone(kNoTimestamp());
1042  EXPECT_TRUE(GetMediaDecoderJob(false));
1043  EXPECT_EQ(1, demuxer_->num_data_requests());
1044
1045  // Reconfirm exactly 1 seek request has been made of demuxer.
1046  EXPECT_EQ(1, demuxer_->num_seek_requests());
1047}
1048
1049TEST_F(MediaSourcePlayerTest, StartImmediatelyAfterPause) {
1050  SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1051
1052  // Test that if the decoding job is not fully stopped after Pause(),
1053  // calling Start() will be a noop.
1054  StartAudioDecoderJob();
1055
1056  MediaDecoderJob* decoder_job = GetMediaDecoderJob(true);
1057  EXPECT_FALSE(GetMediaDecoderJob(true)->is_decoding());
1058
1059  // Sending data to player.
1060  player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForAudio(0));
1061  EXPECT_TRUE(GetMediaDecoderJob(true)->is_decoding());
1062  EXPECT_EQ(2, demuxer_->num_data_requests());
1063
1064  // Decoder job will not immediately stop after Pause() since it is
1065  // running on another thread.
1066  player_.Pause(true);
1067  EXPECT_TRUE(GetMediaDecoderJob(true)->is_decoding());
1068
1069  // Nothing happens when calling Start() again.
1070  player_.Start();
1071  // Verify that Start() will not destroy and recreate the decoder job.
1072  EXPECT_EQ(decoder_job, GetMediaDecoderJob(true));
1073
1074  while (GetMediaDecoderJob(true)->is_decoding())
1075    message_loop_.RunUntilIdle();
1076  // The decoder job should finish and wait for data.
1077  EXPECT_EQ(2, demuxer_->num_data_requests());
1078  EXPECT_TRUE(GetMediaDecoderJob(true)->is_requesting_demuxer_data());
1079}
1080
1081TEST_F(MediaSourcePlayerTest, DecoderJobsCannotStartWithoutAudio) {
1082  SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1083
1084  // Test that when Start() is called, video decoder jobs will wait for audio
1085  // decoder job before start decoding the data.
1086  CreateNextTextureAndSetVideoSurface();
1087  Start(CreateAudioVideoDemuxerConfigs());
1088  MediaDecoderJob* audio_decoder_job = GetMediaDecoderJob(true);
1089  MediaDecoderJob* video_decoder_job = GetMediaDecoderJob(false);
1090
1091  EXPECT_FALSE(audio_decoder_job->is_decoding());
1092  EXPECT_FALSE(video_decoder_job->is_decoding());
1093
1094  // Sending video data to player, video decoder should not start.
1095  player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo());
1096  EXPECT_FALSE(video_decoder_job->is_decoding());
1097
1098  // Sending audio data to player, both decoders should start now.
1099  player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForAudio(0));
1100  EXPECT_TRUE(audio_decoder_job->is_decoding());
1101  EXPECT_TRUE(video_decoder_job->is_decoding());
1102
1103  // No seeks should have occurred.
1104  EXPECT_EQ(0, demuxer_->num_seek_requests());
1105}
1106
1107TEST_F(MediaSourcePlayerTest, StartTimeTicksResetAfterDecoderUnderruns) {
1108  SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1109
1110  // Test start time ticks will reset after decoder job underruns.
1111  StartAudioDecoderJob();
1112
1113  DecodeAudioDataUntilOutputBecomesAvailable();
1114
1115  // The decoder job should finish and a new request will be sent.
1116  base::TimeTicks previous = StartTimeTicks();
1117  player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForAudio(3));
1118
1119  // Let the decoder starve.
1120  TriggerPlayerStarvation();
1121  WaitForAudioDecodeDone();
1122
1123  // Verify the start time ticks is cleared at this point because the
1124  // player is prefetching.
1125  EXPECT_TRUE(StartTimeTicks() == base::TimeTicks());
1126
1127  // Send new data to the decoder so it can finish prefetching. This should
1128  // reset the start time ticks.
1129  player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForAudio(3));
1130  EXPECT_TRUE(StartTimeTicks() != base::TimeTicks());
1131
1132  base::TimeTicks current = StartTimeTicks();
1133  EXPECT_LE(0, (current - previous).InMillisecondsF());
1134}
1135
1136TEST_F(MediaSourcePlayerTest, V_SecondAccessUnitIsEOSAndResumePlayAfterSeek) {
1137  SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1138
1139  // Test MediaSourcePlayer can replay video after input EOS is reached.
1140  CreateNextTextureAndSetVideoSurface();
1141  StartVideoDecoderJob();
1142
1143  // Send the first input chunk.
1144  player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo());
1145  WaitForVideoDecodeDone();
1146
1147  VerifyPlaybackCompletesOnEOSDecode(true, false);
1148  VerifyCompletedPlaybackResumesOnSeekPlusStart(false, true);
1149}
1150
1151TEST_F(MediaSourcePlayerTest, A_FirstAccessUnitIsEOSAndResumePlayAfterSeek) {
1152  SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1153
1154  // Test decode of audio EOS buffer without any prior decode. See also
1155  // http://b/11696552.
1156  // Also tests that seeking+Start() after completing audio playback resumes
1157  // playback.
1158  Start(CreateAudioDemuxerConfigs(kCodecAAC));
1159  VerifyPlaybackCompletesOnEOSDecode(true, true);
1160  VerifyCompletedPlaybackResumesOnSeekPlusStart(true, false);
1161}
1162
1163TEST_F(MediaSourcePlayerTest, V_FirstAccessUnitAfterSeekIsEOS) {
1164  SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1165
1166  // Test decode of video EOS buffer, just after seeking, without any prior
1167  // decode (other than the simulated |kAborted| resulting from the seek
1168  // process.)
1169  CreateNextTextureAndSetVideoSurface();
1170  StartVideoDecoderJob();
1171  SeekPlayerWithAbort(false, base::TimeDelta());
1172  VerifyPlaybackCompletesOnEOSDecode(true, false);
1173}
1174
1175TEST_F(MediaSourcePlayerTest, A_FirstAccessUnitAfterSeekIsEOS) {
1176  SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1177
1178  // Test decode of audio EOS buffer, just after seeking, without any prior
1179  // decode (other than the simulated |kAborted| resulting from the seek
1180  // process.) See also http://b/11696552.
1181  Start(CreateAudioDemuxerConfigs(kCodecAAC));
1182  SeekPlayerWithAbort(true, base::TimeDelta());
1183  VerifyPlaybackCompletesOnEOSDecode(true, true);
1184}
1185
1186TEST_F(MediaSourcePlayerTest, AV_PlaybackCompletionAcrossConfigChange) {
1187  SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1188
1189  // Test that if one stream (audio) has completed decode of EOS and the other
1190  // stream (video) processes config change, that subsequent video EOS completes
1191  // A/V playback.
1192  // Also tests that seeking+Start() after completing playback resumes playback.
1193  CreateNextTextureAndSetVideoSurface();
1194  Start(CreateAudioVideoDemuxerConfigs());
1195
1196  player_.OnDemuxerDataAvailable(CreateEOSAck(true));  // Audio EOS
1197  player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckWithConfigChanged(
1198      false, 0));  // Video |kConfigChanged| as first unit.
1199
1200  WaitForAudioVideoDecodeDone();
1201
1202  EXPECT_EQ(3, demuxer_->num_data_requests());
1203
1204  // At no time after completing audio EOS decode, above, should the
1205  // audio decoder job resume decoding. Send and decode video EOS.
1206  VerifyPlaybackCompletesOnEOSDecode(true, false);
1207  VerifyCompletedPlaybackResumesOnSeekPlusStart(true, true);
1208}
1209
1210TEST_F(MediaSourcePlayerTest, VA_PlaybackCompletionAcrossConfigChange) {
1211  SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1212
1213  // Test that if one stream (video) has completed decode of EOS and the other
1214  // stream (audio) processes config change, that subsequent audio EOS completes
1215  // A/V playback.
1216  // Also tests that seeking+Start() after completing playback resumes playback.
1217  CreateNextTextureAndSetVideoSurface();
1218  Start(CreateAudioVideoDemuxerConfigs());
1219
1220  player_.OnDemuxerDataAvailable(CreateEOSAck(false));  // Video EOS
1221  player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckWithConfigChanged(
1222      true, 0));  // Audio |kConfigChanged| as first unit.
1223
1224  WaitForAudioVideoDecodeDone();
1225
1226  EXPECT_EQ(3, demuxer_->num_data_requests());
1227
1228  // At no time after completing video EOS decode, above, should the
1229  // video decoder job resume decoding. Send and decode audio EOS.
1230  VerifyPlaybackCompletesOnEOSDecode(true, true);
1231  VerifyCompletedPlaybackResumesOnSeekPlusStart(true, true);
1232}
1233
1234TEST_F(MediaSourcePlayerTest, AV_NoPrefetchForFinishedVideoOnAudioStarvation) {
1235  SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1236
1237  // Test that if one stream (video) has completed decode of EOS, prefetch
1238  // resulting from player starvation occurs only for the other stream (audio),
1239  // and responding to that prefetch with EOS completes A/V playback, even if
1240  // another starvation occurs during the latter EOS's decode.
1241  CreateNextTextureAndSetVideoSurface();
1242  Start(CreateAudioVideoDemuxerConfigs());
1243
1244  player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForAudio(0));
1245  player_.OnDemuxerDataAvailable(CreateEOSAck(false));  // Video EOS
1246
1247  // Wait until video EOS is processed and more data (assumed to be audio) is
1248  // requested.
1249  WaitForAudioVideoDecodeDone();
1250  EXPECT_EQ(3, demuxer_->num_data_requests());
1251
1252  // Simulate decoder underrun to trigger prefetch while still decoding audio.
1253  player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForAudio(1));
1254  EXPECT_TRUE(GetMediaDecoderJob(true)->is_decoding() &&
1255              !GetMediaDecoderJob(false)->is_decoding());
1256  TriggerPlayerStarvation();
1257
1258  // Complete the audio decode that was in progress when simulated player
1259  // starvation was triggered.
1260  WaitForAudioDecodeDone();
1261  EXPECT_EQ(4, demuxer_->num_data_requests());
1262  player_.OnDemuxerDataAvailable(CreateEOSAck(true));  // Audio EOS
1263  EXPECT_FALSE(GetMediaDecoderJob(false)->is_decoding());
1264  EXPECT_TRUE(GetMediaDecoderJob(true)->is_decoding());
1265
1266  // Simulate another decoder underrun to trigger prefetch while decoding EOS.
1267  TriggerPlayerStarvation();
1268  VerifyPlaybackCompletesOnEOSDecode(false, true /* ignored */);
1269}
1270
1271TEST_F(MediaSourcePlayerTest, V_StarvationDuringEOSDecode) {
1272  SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1273
1274  // Test that video-only playback completes without further data requested when
1275  // starvation occurs during EOS decode.
1276  CreateNextTextureAndSetVideoSurface();
1277  StartVideoDecoderJob();
1278  player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo());
1279  WaitForVideoDecodeDone();
1280
1281  // Simulate decoder underrun to trigger prefetch while decoding EOS.
1282  player_.OnDemuxerDataAvailable(CreateEOSAck(false));  // Video EOS
1283  EXPECT_TRUE(GetMediaDecoderJob(false)->is_decoding());
1284  TriggerPlayerStarvation();
1285  VerifyPlaybackCompletesOnEOSDecode(false, false /* ignored */);
1286}
1287
1288TEST_F(MediaSourcePlayerTest, A_StarvationDuringEOSDecode) {
1289  SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1290
1291  // Test that audio-only playback completes without further data requested when
1292  // starvation occurs during EOS decode.
1293  StartAudioDecoderJob();
1294  player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForAudio(0));
1295  WaitForAudioDecodeDone();
1296
1297  // Simulate decoder underrun to trigger prefetch while decoding EOS.
1298  player_.OnDemuxerDataAvailable(CreateEOSAck(true));  // Audio EOS
1299  EXPECT_TRUE(GetMediaDecoderJob(true)->is_decoding());
1300  TriggerPlayerStarvation();
1301  VerifyPlaybackCompletesOnEOSDecode(false, true /* ignored */);
1302}
1303
1304TEST_F(MediaSourcePlayerTest, AV_SeekDuringEOSDecodePreventsCompletion) {
1305  SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1306
1307  // Test that seek supercedes audio+video playback completion on simultaneous
1308  // audio and video EOS decode, if SeekTo() occurs during these EOS decodes.
1309  VerifySeekDuringEOSDecodePreventsPlaybackCompletion(true, true, true, true);
1310}
1311
1312TEST_F(MediaSourcePlayerTest, AV_SeekDuringAudioEOSDecodePreventsCompletion) {
1313  SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1314
1315  // Test that seek supercedes audio+video playback completion on simultaneous
1316  // audio EOS and video non-EOS decode, if SeekTo() occurs during these
1317  // decodes.
1318  VerifySeekDuringEOSDecodePreventsPlaybackCompletion(true, true, true, false);
1319}
1320
1321TEST_F(MediaSourcePlayerTest, AV_SeekDuringVideoEOSDecodePreventsCompletion) {
1322  SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1323
1324  // Test that seek supercedes audio+video playback completion on simultaneous
1325  // audio non-EOS and video EOS decode, if SeekTo() occurs during these
1326  // decodes.
1327  VerifySeekDuringEOSDecodePreventsPlaybackCompletion(true, true, false, true);
1328}
1329
1330TEST_F(MediaSourcePlayerTest, V_SeekDuringEOSDecodePreventsCompletion) {
1331  SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1332
1333  // Test that seek supercedes video-only playback completion on EOS decode, if
1334  // SeekTo() occurs during EOS decode.
1335  VerifySeekDuringEOSDecodePreventsPlaybackCompletion(false, true, false, true);
1336}
1337
1338TEST_F(MediaSourcePlayerTest, A_SeekDuringEOSDecodePreventsCompletion) {
1339  SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1340
1341  // Test that seek supercedes audio-only playback completion on EOS decode, if
1342  // SeekTo() occurs during EOS decode.
1343  VerifySeekDuringEOSDecodePreventsPlaybackCompletion(true, false, true, false);
1344}
1345
1346TEST_F(MediaSourcePlayerTest, NoRequestForDataAfterAbort) {
1347  SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1348
1349  // Test that the decoder will not request new data after receiving an aborted
1350  // access unit.
1351  StartAudioDecoderJob();
1352
1353  // Send an aborted access unit.
1354  player_.OnDemuxerDataAvailable(CreateAbortedAck(true));
1355  EXPECT_TRUE(GetMediaDecoderJob(true)->is_decoding());
1356  WaitForAudioDecodeDone();
1357
1358  // No request will be sent for new data.
1359  EXPECT_EQ(1, demuxer_->num_data_requests());
1360
1361  // No seek requests should have occurred.
1362  EXPECT_EQ(0, demuxer_->num_seek_requests());
1363}
1364
1365TEST_F(MediaSourcePlayerTest, DemuxerDataArrivesAfterRelease) {
1366  SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1367
1368  // Test that the decoder should not crash if demuxer data arrives after
1369  // Release().
1370  StartAudioDecoderJob();
1371
1372  ReleasePlayer();
1373  player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForAudio(0));
1374
1375  // The decoder job should have been released.
1376  EXPECT_FALSE(player_.IsPlaying());
1377
1378  // No further data should have been requested.
1379  EXPECT_EQ(1, demuxer_->num_data_requests());
1380
1381  // No seek requests should have occurred.
1382  EXPECT_EQ(0, demuxer_->num_seek_requests());
1383}
1384
1385TEST_F(MediaSourcePlayerTest, BrowserSeek_RegularSeekPendsBrowserSeekDone) {
1386  SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1387
1388  // Test that a browser seek, once started, delays a newly arrived regular
1389  // SeekTo() request's demuxer seek until the browser seek is done.
1390  BrowserSeekPlayer(false);
1391
1392  // Simulate renderer requesting a regular seek while browser seek in progress.
1393  player_.SeekTo(base::TimeDelta());
1394  EXPECT_FALSE(GetMediaDecoderJob(false));
1395
1396  // Simulate browser seek is done. Confirm player requests the regular seek,
1397  // still has no video decoder job configured, and has not requested any
1398  // further data since the surface change event became pending in
1399  // BrowserSeekPlayer().
1400  EXPECT_EQ(1, demuxer_->num_seek_requests());
1401  player_.OnDemuxerSeekDone(base::TimeDelta());
1402  EXPECT_FALSE(GetMediaDecoderJob(false));
1403  EXPECT_EQ(2, demuxer_->num_seek_requests());
1404  EXPECT_EQ(1, demuxer_->num_browser_seek_requests());
1405
1406  // Simulate regular seek is done and confirm player requests more data for
1407  // new video decoder job.
1408  player_.OnDemuxerSeekDone(kNoTimestamp());
1409  EXPECT_TRUE(GetMediaDecoderJob(false));
1410  EXPECT_EQ(3, demuxer_->num_data_requests());
1411  EXPECT_EQ(2, demuxer_->num_seek_requests());
1412}
1413
1414TEST_F(MediaSourcePlayerTest, BrowserSeek_InitialReleaseAndStart) {
1415  SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1416
1417  // Test that browser seek is requested if player Release() + Start() occurs
1418  // prior to receiving any data.
1419  CreateNextTextureAndSetVideoSurface();
1420  StartVideoDecoderJob();
1421  ReleasePlayer();
1422
1423  // Pass a new non-empty surface.
1424  CreateNextTextureAndSetVideoSurface();
1425
1426  player_.Start();
1427
1428  // The new player won't be created until the pending data request is
1429  // processed.
1430  EXPECT_EQ(1, demuxer_->num_data_requests());
1431  EXPECT_FALSE(GetMediaDecoderJob(false));
1432
1433  // A browser seek should be requested.
1434  player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForVideo());
1435  EXPECT_EQ(1, demuxer_->num_browser_seek_requests());
1436  EXPECT_EQ(1, demuxer_->num_data_requests());
1437}
1438
1439TEST_F(MediaSourcePlayerTest, BrowserSeek_MidStreamReleaseAndStart) {
1440  SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1441
1442  // Test that one browser seek is requested if player Release() + Start(), with
1443  // video data received between Release() and Start().
1444  BrowserSeekPlayer(true);
1445
1446  // Simulate browser seek is done and confirm player requests more data.
1447  player_.OnDemuxerSeekDone(base::TimeDelta());
1448  EXPECT_TRUE(GetMediaDecoderJob(false));
1449  EXPECT_EQ(2, demuxer_->num_data_requests());
1450  EXPECT_EQ(1, demuxer_->num_seek_requests());
1451}
1452
1453TEST_F(MediaSourcePlayerTest, PrerollAudioAfterSeek) {
1454  SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1455
1456  // Test decoder job will preroll the media to the seek position.
1457  StartAudioDecoderJob();
1458
1459  SeekPlayerWithAbort(true, base::TimeDelta::FromMilliseconds(100));
1460  EXPECT_TRUE(IsPrerolling(true));
1461  PrerollDecoderToTime(
1462      true, base::TimeDelta(), base::TimeDelta::FromMilliseconds(100));
1463}
1464
1465TEST_F(MediaSourcePlayerTest, PrerollVideoAfterSeek) {
1466  SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1467
1468  // Test decoder job will preroll the media to the seek position.
1469  CreateNextTextureAndSetVideoSurface();
1470  StartVideoDecoderJob();
1471
1472  SeekPlayerWithAbort(false, base::TimeDelta::FromMilliseconds(100));
1473  EXPECT_TRUE(IsPrerolling(false));
1474  PrerollDecoderToTime(
1475      false, base::TimeDelta(), base::TimeDelta::FromMilliseconds(100));
1476}
1477
1478TEST_F(MediaSourcePlayerTest, SeekingAfterCompletingPrerollRestartsPreroll) {
1479  SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1480
1481  // Test decoder job will begin prerolling upon seek, when it was not
1482  // prerolling prior to the seek.
1483  StartAudioDecoderJob();
1484  MediaDecoderJob* decoder_job = GetMediaDecoderJob(true);
1485  EXPECT_TRUE(IsPrerolling(true));
1486
1487  // Complete the initial preroll by feeding data to the decoder.
1488  DecodeAudioDataUntilOutputBecomesAvailable();
1489  EXPECT_FALSE(IsPrerolling(true));
1490
1491  SeekPlayerWithAbort(true, base::TimeDelta::FromMilliseconds(500));
1492
1493  // Prerolling should have begun again.
1494  EXPECT_TRUE(IsPrerolling(true));
1495  EXPECT_EQ(500.0, GetPrerollTimestamp().InMillisecondsF());
1496
1497  // Send data at and after the seek position. Prerolling should complete.
1498  for (int i = 0; i < 4; ++i) {
1499    DemuxerData data = CreateReadFromDemuxerAckForAudio(i);
1500    data.access_units[0].timestamp = base::TimeDelta::FromMilliseconds(
1501        500 + 30 * (i - 1));
1502    player_.OnDemuxerDataAvailable(data);
1503    EXPECT_TRUE(GetMediaDecoderJob(true)->is_decoding());
1504    WaitForAudioDecodeDone();
1505  }
1506  EXPECT_LT(500.0, player_.GetCurrentTime().InMillisecondsF());
1507  EXPECT_FALSE(IsPrerolling(true));
1508
1509  // Throughout this test, we should have not re-created the decoder job, so
1510  // IsPrerolling() transition from false to true was not due to constructor
1511  // initialization. It was due to BeginPrerolling().
1512  EXPECT_EQ(decoder_job, GetMediaDecoderJob(true));
1513}
1514
1515TEST_F(MediaSourcePlayerTest, PrerollContinuesAcrossReleaseAndStart) {
1516  SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1517
1518  // Test decoder job will resume media prerolling if interrupted by Release()
1519  // and Start().
1520  StartAudioDecoderJob();
1521
1522  base::TimeDelta target_timestamp = base::TimeDelta::FromMilliseconds(100);
1523  SeekPlayerWithAbort(true, target_timestamp);
1524  EXPECT_TRUE(IsPrerolling(true));
1525  EXPECT_EQ(100.0, GetPrerollTimestamp().InMillisecondsF());
1526
1527  // Send some data before the seek position.
1528  // Test uses 'large' number of iterations because decoder job may not get
1529  // MEDIA_CODEC_OK output status until after a few dequeue output attempts.
1530  // This allows decoder status to stabilize prior to AU timestamp reaching
1531  // the preroll target.
1532  DemuxerData data;
1533  for (int i = 0; i < 10; ++i) {
1534    data = CreateReadFromDemuxerAckForAudio(3);
1535    data.access_units[0].timestamp = base::TimeDelta::FromMilliseconds(i * 10);
1536    if (i == 1) {
1537      // While still prerolling, Release() and Start() the player.
1538      // TODO(qinmin): Simulation of multiple in-flight data requests (one from
1539      // before Release(), one from after Start()) is not included here, and
1540      // neither is any data enqueued for later decode if it arrives after
1541      // Release() and before Start(). See http://crbug.com/306314. Assumption
1542      // for this test, to prevent flakiness until the bug is fixed, is the
1543      // first request's data arrives before Start(). Though that data is not
1544      // seen by decoder, this assumption allows preroll continuation
1545      // verification and prevents multiple in-flight data requests.
1546      ReleasePlayer();
1547      player_.OnDemuxerDataAvailable(data);
1548      WaitForAudioDecodeDone();
1549      EXPECT_FALSE(GetMediaDecoderJob(true));
1550      Resume(true, false);
1551    } else {
1552      player_.OnDemuxerDataAvailable(data);
1553      EXPECT_TRUE(GetMediaDecoderJob(true)->is_decoding());
1554      WaitForAudioDecodeDone();
1555    }
1556    EXPECT_TRUE(IsPrerolling(true));
1557  }
1558  EXPECT_EQ(100.0, player_.GetCurrentTime().InMillisecondsF());
1559  EXPECT_TRUE(IsPrerolling(true));
1560
1561  // Send data after the seek position.
1562  PrerollDecoderToTime(true, target_timestamp, target_timestamp);
1563}
1564
1565TEST_F(MediaSourcePlayerTest, PrerollContinuesAcrossConfigChange) {
1566  SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1567
1568  // Test decoder job will resume media prerolling if interrupted by
1569  // |kConfigChanged| and OnDemuxerConfigsAvailable().
1570  StartAudioDecoderJob();
1571
1572  SeekPlayerWithAbort(true, base::TimeDelta::FromMilliseconds(100));
1573  EXPECT_TRUE(IsPrerolling(true));
1574  EXPECT_EQ(100.0, GetPrerollTimestamp().InMillisecondsF());
1575
1576
1577  // In response to data request, simulate that demuxer signals config change by
1578  // sending an AU with |kConfigChanged|. Player should reconfigure the
1579  // audio decoder job with the supplied configs.
1580  DemuxerData data = CreateReadFromDemuxerAckWithConfigChanged(true, 0);
1581  player_.OnDemuxerDataAvailable(data);
1582  PrerollDecoderToTime(
1583      true, base::TimeDelta(), base::TimeDelta::FromMilliseconds(100));
1584}
1585
1586TEST_F(MediaSourcePlayerTest, SimultaneousAudioVideoConfigChange) {
1587  SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1588
1589  // Test that the player allows simultaneous audio and video config change,
1590  // such as might occur during OnPrefetchDone() if next access unit for both
1591  // audio and video jobs is |kConfigChanged|.
1592  CreateNextTextureAndSetVideoSurface();
1593  Start(CreateAudioVideoDemuxerConfigs());
1594  MediaDecoderJob* first_audio_job = GetMediaDecoderJob(true);
1595  MediaDecoderJob* first_video_job = GetMediaDecoderJob(false);
1596
1597  // Simulate audio |kConfigChanged| prefetched as standalone access unit.
1598  player_.OnDemuxerDataAvailable(
1599      CreateReadFromDemuxerAckWithConfigChanged(true, 0));
1600
1601  // Simulate video |kConfigChanged| prefetched as standalone access unit.
1602  player_.OnDemuxerDataAvailable(
1603      CreateReadFromDemuxerAckWithConfigChanged(false, 0));
1604  EXPECT_EQ(4, demuxer_->num_data_requests());
1605
1606  // Both jobs should have been reconfigured by now.
1607  // TODO(qinmin): Fix flaky pointer-based MDJ inequality testing.
1608  // See http://crbug.com/327839.
1609  EXPECT_NE(first_audio_job, GetMediaDecoderJob(true));
1610  EXPECT_NE(first_video_job, GetMediaDecoderJob(false));
1611  EXPECT_TRUE(GetMediaDecoderJob(true) && GetMediaDecoderJob(false));
1612}
1613
1614TEST_F(MediaSourcePlayerTest, DemuxerConfigRequestedIfInPrefetchUnit0) {
1615  SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1616
1617  // Test that the player detects need for and requests demuxer configs if
1618  // the |kConfigChanged| unit is the very first unit in the set of units
1619  // received in OnDemuxerDataAvailable() ostensibly while
1620  // |PREFETCH_DONE_EVENT_PENDING|.
1621  StartConfigChange(true, true, 0);
1622}
1623
1624TEST_F(MediaSourcePlayerTest, DemuxerConfigRequestedIfInPrefetchUnit1) {
1625  SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1626
1627  // Test that the player detects need for and requests demuxer configs if
1628  // the |kConfigChanged| unit is not the first unit in the set of units
1629  // received in OnDemuxerDataAvailable() ostensibly while
1630  // |PREFETCH_DONE_EVENT_PENDING|.
1631  StartConfigChange(true, true, 1);
1632}
1633
1634TEST_F(MediaSourcePlayerTest, DemuxerConfigRequestedIfInUnit0AfterPrefetch) {
1635  SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1636
1637  // Test that the player detects need for and requests demuxer configs if
1638  // the |kConfigChanged| unit is the very first unit in the set of units
1639  // received in OnDemuxerDataAvailable() from data requested ostensibly while
1640  // not prefetching.
1641  StartConfigChange(true, false, 0);
1642}
1643
1644TEST_F(MediaSourcePlayerTest, DemuxerConfigRequestedIfInUnit1AfterPrefetch) {
1645  SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1646
1647  // Test that the player detects need for and requests demuxer configs if
1648  // the |kConfigChanged| unit is not the first unit in the set of units
1649  // received in OnDemuxerDataAvailable() from data requested ostensibly while
1650  // not prefetching.
1651  StartConfigChange(true, false, 1);
1652}
1653
1654TEST_F(MediaSourcePlayerTest, BrowserSeek_PrerollAfterBrowserSeek) {
1655  SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1656
1657  // Test decoder job will preroll the media to the actual seek position
1658  // resulting from a browser seek.
1659  BrowserSeekPlayer(false);
1660
1661  // Simulate browser seek is done, but to a later time than was requested.
1662  EXPECT_LT(player_.GetCurrentTime().InMillisecondsF(), 100);
1663  player_.OnDemuxerSeekDone(base::TimeDelta::FromMilliseconds(100));
1664  EXPECT_TRUE(GetMediaDecoderJob(false));
1665  EXPECT_EQ(100.0, player_.GetCurrentTime().InMillisecondsF());
1666  EXPECT_EQ(100.0, GetPrerollTimestamp().InMillisecondsF());
1667  EXPECT_EQ(3, demuxer_->num_data_requests());
1668
1669  PrerollDecoderToTime(
1670      false, base::TimeDelta(), base::TimeDelta::FromMilliseconds(100));
1671}
1672
1673TEST_F(MediaSourcePlayerTest, VideoDemuxerConfigChange) {
1674  SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1675
1676  // Test that video config change notification results in creating a new
1677  // video decoder job results without any browser seek.
1678  StartConfigChange(false, true, 1);
1679  EXPECT_TRUE(GetMediaDecoderJob(false));
1680  EXPECT_EQ(2, demuxer_->num_data_requests());
1681  EXPECT_EQ(0, demuxer_->num_seek_requests());
1682}
1683
1684TEST_F(MediaSourcePlayerTest, NewSurfaceAfterChangingConfigs) {
1685  SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1686
1687  // Test that no seek results from a SetVideoSurface() that occurs after
1688  // the player processes new demuxer configs. This test may be good to keep
1689  // beyond browser seek hack.
1690  StartConfigChange(false, false, 1);
1691  EXPECT_TRUE(GetMediaDecoderJob(false));
1692  EXPECT_EQ(3, demuxer_->num_data_requests());
1693
1694  CreateNextTextureAndSetVideoSurface();
1695  EXPECT_TRUE(GetMediaDecoderJob(false));
1696  EXPECT_EQ(3, demuxer_->num_data_requests());
1697  EXPECT_EQ(0, demuxer_->num_seek_requests());
1698}
1699
1700TEST_F(MediaSourcePlayerTest,
1701       BrowserSeek_DecoderStarvationWhilePendingSurfaceChange) {
1702  SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1703
1704  // Test video decoder starvation while handling a pending surface change
1705  // should not cause any crashes.
1706  CreateNextTextureAndSetVideoSurface();
1707  StartVideoDecoderJob();
1708  DemuxerData data = CreateReadFromDemuxerAckForVideo();
1709  player_.OnDemuxerDataAvailable(data);
1710
1711  // Trigger a surface change and decoder starvation.
1712  CreateNextTextureAndSetVideoSurface();
1713  TriggerPlayerStarvation();
1714  WaitForVideoDecodeDone();
1715  EXPECT_EQ(0, demuxer_->num_browser_seek_requests());
1716
1717  // Surface change should trigger a seek.
1718  player_.OnDemuxerDataAvailable(data);
1719  EXPECT_EQ(1, demuxer_->num_browser_seek_requests());
1720  player_.OnDemuxerSeekDone(base::TimeDelta());
1721  EXPECT_TRUE(GetMediaDecoderJob(false));
1722
1723  // A new data request should be sent.
1724  EXPECT_EQ(3, demuxer_->num_data_requests());
1725}
1726
1727TEST_F(MediaSourcePlayerTest, ReleaseWithOnPrefetchDoneAlreadyPosted) {
1728  SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1729
1730  // Test if OnPrefetchDone() had already been posted before and is executed
1731  // after Release(), then player does not DCHECK. This test is fragile to
1732  // change to MediaDecoderJob::Prefetch() implementation; it assumes task
1733  // is posted to run |prefetch_cb| if the job already HasData().
1734  // TODO(wolenetz): Remove MSP::set_decode_callback_for_testing() if this test
1735  // becomes obsolete. See http://crbug.com/304234.
1736  StartAudioDecoderJob();
1737
1738  // Escape the original prefetch by decoding a single access unit.
1739  player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForAudio(0));
1740  WaitForAudioDecodeDone();
1741
1742  // Prime the job with a few more access units, so that a later prefetch,
1743  // triggered by starvation to simulate decoder underrun, can trivially
1744  // post task to run OnPrefetchDone().
1745  player_.OnDemuxerDataAvailable(
1746      CreateReadFromDemuxerAckWithConfigChanged(true, 4));
1747  EXPECT_TRUE(GetMediaDecoderJob(true)->is_decoding());
1748
1749  // Simulate decoder underrun, so trivial prefetch starts while still decoding.
1750  // The prefetch and posting of OnPrefetchDone() will not occur until next
1751  // MediaDecoderCallBack() occurs.
1752  TriggerPlayerStarvation();
1753
1754  // Upon the next successful decode callback, post a task to call Release() on
1755  // the |player_|, such that the trivial OnPrefetchDone() task posting also
1756  // occurs and should execute after the Release().
1757  OnNextTestDecodeCallbackPostTaskToReleasePlayer();
1758
1759  WaitForAudioDecodeDone();
1760  EXPECT_TRUE(decoder_callback_hook_executed_);
1761  EXPECT_EQ(2, demuxer_->num_data_requests());
1762
1763  // Player should have no decoder job until after Start().
1764  Resume(true, false);
1765}
1766
1767TEST_F(MediaSourcePlayerTest, SeekToThenReleaseThenDemuxerSeekAndDone) {
1768  SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1769
1770  // Test if Release() occurs after SeekTo(), but the DemuxerSeek IPC request
1771  // has not yet been sent, then the seek request is sent after Release(). Also,
1772  // test if OnDemuxerSeekDone() occurs prior to next Start(), then the player
1773  // will resume correct post-seek preroll upon Start().
1774  StartAudioDecoderJobAndSeekToWhileDecoding(
1775      base::TimeDelta::FromMilliseconds(100));
1776  ReleasePlayer();
1777  EXPECT_EQ(1, demuxer_->num_seek_requests());
1778
1779  player_.OnDemuxerSeekDone(kNoTimestamp());
1780  EXPECT_EQ(100.0, GetPrerollTimestamp().InMillisecondsF());
1781  EXPECT_FALSE(GetMediaDecoderJob(true));
1782  EXPECT_FALSE(player_.IsPlaying());
1783
1784  // Player should begin prefetch and resume preroll upon Start().
1785  EXPECT_EQ(2, demuxer_->num_data_requests());
1786  Resume(true, false);
1787  EXPECT_TRUE(IsPrerolling(true));
1788  EXPECT_EQ(100.0, GetPrerollTimestamp().InMillisecondsF());
1789
1790  // No further seek should have been requested since Release(), above.
1791  EXPECT_EQ(1, demuxer_->num_seek_requests());
1792}
1793
1794TEST_F(MediaSourcePlayerTest, SeekToThenReleaseThenDemuxerSeekThenStart) {
1795  SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1796
1797  // Test if Release() occurs after SeekTo(), but the DemuxerSeek IPC request
1798  // has not yet been sent, then the seek request is sent after Release(). Also,
1799  // test if OnDemuxerSeekDone() does not occur until after the next Start(),
1800  // then the player remains pending seek done until (and resumes correct
1801  // post-seek preroll after) OnDemuxerSeekDone().
1802  StartAudioDecoderJobAndSeekToWhileDecoding(
1803      base::TimeDelta::FromMilliseconds(100));
1804  ReleasePlayer();
1805  EXPECT_EQ(1, demuxer_->num_seek_requests());
1806
1807  // Player should not prefetch upon Start() nor create the decoder job, due to
1808  // awaiting DemuxerSeekDone.
1809  EXPECT_EQ(2, demuxer_->num_data_requests());
1810  Resume(false, false);
1811
1812  player_.OnDemuxerSeekDone(kNoTimestamp());
1813  EXPECT_TRUE(GetMediaDecoderJob(true));
1814  EXPECT_TRUE(IsPrerolling(true));
1815  EXPECT_EQ(100.0, GetPrerollTimestamp().InMillisecondsF());
1816  EXPECT_EQ(3, demuxer_->num_data_requests());
1817
1818  // No further seek should have been requested since Release(), above.
1819  EXPECT_EQ(1, demuxer_->num_seek_requests());
1820}
1821
1822TEST_F(MediaSourcePlayerTest, SeekToThenDemuxerSeekThenReleaseThenSeekDone) {
1823  SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1824
1825  // Test if Release() occurs after a SeekTo()'s subsequent DemuxerSeek IPC
1826  // request and OnDemuxerSeekDone() arrives prior to the next Start(), then the
1827  // player will resume correct post-seek preroll upon Start().
1828  StartAudioDecoderJobAndSeekToWhileDecoding(
1829      base::TimeDelta::FromMilliseconds(100));
1830  WaitForAudioDecodeDone();
1831  EXPECT_EQ(1, demuxer_->num_seek_requests());
1832
1833  ReleasePlayer();
1834  player_.OnDemuxerSeekDone(kNoTimestamp());
1835  EXPECT_FALSE(player_.IsPlaying());
1836  EXPECT_FALSE(GetMediaDecoderJob(true));
1837  EXPECT_EQ(100.0, GetPrerollTimestamp().InMillisecondsF());
1838
1839  // Player should begin prefetch and resume preroll upon Start().
1840  EXPECT_EQ(2, demuxer_->num_data_requests());
1841  Resume(true, false);
1842  EXPECT_TRUE(IsPrerolling(true));
1843  EXPECT_EQ(100.0, GetPrerollTimestamp().InMillisecondsF());
1844
1845  // No further seek should have been requested since before Release(), above.
1846  EXPECT_EQ(1, demuxer_->num_seek_requests());
1847}
1848
1849TEST_F(MediaSourcePlayerTest, SeekToThenReleaseThenStart) {
1850  SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1851
1852  // Test if Release() occurs after a SeekTo()'s subsequent DemuxerSeeK IPC
1853  // request and OnDemuxerSeekDone() does not occur until after the next
1854  // Start(), then the player remains pending seek done until (and resumes
1855  // correct post-seek preroll after) OnDemuxerSeekDone().
1856  StartAudioDecoderJobAndSeekToWhileDecoding(
1857      base::TimeDelta::FromMilliseconds(100));
1858  WaitForAudioDecodeDone();
1859  EXPECT_EQ(1, demuxer_->num_seek_requests());
1860
1861  ReleasePlayer();
1862  EXPECT_EQ(2, demuxer_->num_data_requests());
1863  Resume(false, false);
1864
1865  player_.OnDemuxerSeekDone(kNoTimestamp());
1866  EXPECT_TRUE(GetMediaDecoderJob(true));
1867  EXPECT_TRUE(IsPrerolling(true));
1868  EXPECT_EQ(100.0, GetPrerollTimestamp().InMillisecondsF());
1869  EXPECT_EQ(3, demuxer_->num_data_requests());
1870
1871  // No further seek should have been requested since before Release(), above.
1872  EXPECT_EQ(1, demuxer_->num_seek_requests());
1873}
1874
1875TEST_F(MediaSourcePlayerTest, ConfigChangedThenReleaseThenStart) {
1876  SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1877
1878  // Test if Release() occurs after |kConfigChanged| detected, and then Start()
1879  // is called, then the player does not issue any new data request since it is
1880  // still waiting for the previous one.
1881  StartConfigChange(true, true, 0);
1882  ReleasePlayer();
1883
1884  EXPECT_FALSE(GetMediaDecoderJob(true));
1885  EXPECT_FALSE(player_.IsPlaying());
1886  EXPECT_EQ(2, demuxer_->num_data_requests());
1887
1888  // Player should resume upon Start().
1889  Resume(false, false);
1890  EXPECT_TRUE(GetMediaDecoderJob(true));
1891  EXPECT_TRUE(player_.IsPlaying());
1892  EXPECT_EQ(2, demuxer_->num_data_requests());
1893}
1894
1895TEST_F(MediaSourcePlayerTest, BrowserSeek_ThenReleaseThenDemuxerSeekDone) {
1896  SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1897
1898  // Test that Release() after a browser seek's DemuxerSeek IPC request has been
1899  // sent behaves similar to a regular seek: if OnDemuxerSeekDone() occurs
1900  // before the next Start()+SetVideoSurface(), then the player will resume
1901  // correct post-seek preroll upon Start()+SetVideoSurface().
1902  BrowserSeekPlayer(false);
1903  base::TimeDelta expected_preroll_timestamp = player_.GetCurrentTime();
1904  ReleasePlayer();
1905
1906  player_.OnDemuxerSeekDone(expected_preroll_timestamp);
1907  EXPECT_FALSE(player_.IsPlaying());
1908  EXPECT_FALSE(GetMediaDecoderJob(false));
1909  EXPECT_EQ(expected_preroll_timestamp, GetPrerollTimestamp());
1910
1911  // Player should begin prefetch and resume preroll upon Start().
1912  EXPECT_EQ(2, demuxer_->num_data_requests());
1913  CreateNextTextureAndSetVideoSurface();
1914  Resume(false, true);
1915  EXPECT_TRUE(IsPrerolling(false));
1916  EXPECT_EQ(expected_preroll_timestamp, GetPrerollTimestamp());
1917  EXPECT_EQ(expected_preroll_timestamp, player_.GetCurrentTime());
1918
1919  // No further seek should have been requested since BrowserSeekPlayer().
1920  EXPECT_EQ(1, demuxer_->num_seek_requests());
1921}
1922
1923TEST_F(MediaSourcePlayerTest, BrowserSeek_ThenReleaseThenStart) {
1924  SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1925
1926  // Test that Release() after a browser seek's DemuxerSeek IPC request has been
1927  // sent behaves similar to a regular seek: if OnDemuxerSeekDone() does not
1928  // occur until after the next Start()+SetVideoSurface(), then the player
1929  // remains pending seek done until (and resumes correct post-seek preroll
1930  // after) OnDemuxerSeekDone().
1931  BrowserSeekPlayer(false);
1932  base::TimeDelta expected_preroll_timestamp = player_.GetCurrentTime();
1933  ReleasePlayer();
1934
1935  EXPECT_EQ(2, demuxer_->num_data_requests());
1936  CreateNextTextureAndSetVideoSurface();
1937  Resume(false, false);
1938
1939  player_.OnDemuxerSeekDone(expected_preroll_timestamp);
1940  EXPECT_TRUE(GetMediaDecoderJob(false));
1941  EXPECT_TRUE(IsPrerolling(false));
1942  EXPECT_EQ(expected_preroll_timestamp, GetPrerollTimestamp());
1943  EXPECT_EQ(expected_preroll_timestamp, player_.GetCurrentTime());
1944  EXPECT_EQ(3, demuxer_->num_data_requests());
1945
1946  // No further seek should have been requested since BrowserSeekPlayer().
1947  EXPECT_EQ(1, demuxer_->num_seek_requests());
1948}
1949
1950// TODO(xhwang): Once we add tests to cover DrmBridge, update this test to
1951// also verify that the job is successfully created if SetDrmBridge(), Start()
1952// and eventually OnMediaCrypto() occur. This would increase test coverage of
1953// http://crbug.com/313470 and allow us to remove inspection of internal player
1954// pending event state. See http://crbug.com/313860.
1955TEST_F(MediaSourcePlayerTest, SurfaceChangeClearedEvenIfMediaCryptoAbsent) {
1956  SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1957
1958  // Test that |SURFACE_CHANGE_EVENT_PENDING| is not pending after
1959  // SetVideoSurface() for a player configured for encrypted video, when the
1960  // player has not yet received media crypto.
1961  DemuxerConfigs configs = CreateVideoDemuxerConfigs();
1962  configs.is_video_encrypted = true;
1963
1964  player_.OnDemuxerConfigsAvailable(configs);
1965  CreateNextTextureAndSetVideoSurface();
1966  EXPECT_FALSE(IsPendingSurfaceChange());
1967  EXPECT_FALSE(GetMediaDecoderJob(false));
1968}
1969
1970TEST_F(MediaSourcePlayerTest, CurrentTimeUpdatedWhileDecoderStarved) {
1971  SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1972
1973  // Test that current time is updated while decoder is starved.
1974  StartAudioDecoderJob();
1975  DecodeAudioDataUntilOutputBecomesAvailable();
1976
1977  // Trigger starvation while the decoder is decoding.
1978  player_.OnDemuxerDataAvailable(CreateReadFromDemuxerAckForAudio(3));
1979  manager_.ResetTimestampUpdated();
1980  TriggerPlayerStarvation();
1981  WaitForAudioDecodeDone();
1982
1983  // Current time should be updated.
1984  EXPECT_TRUE(manager_.timestamp_updated());
1985}
1986
1987TEST_F(MediaSourcePlayerTest, CurrentTimeKeepsIncreasingAfterConfigChange) {
1988  SKIP_TEST_IF_MEDIA_CODEC_BRIDGE_IS_NOT_AVAILABLE();
1989
1990  // Test current time keep on increasing after audio config change.
1991  // Test that current time is updated while decoder is starved.
1992  StartAudioDecoderJob();
1993
1994  DecodeAudioDataUntilOutputBecomesAvailable();
1995
1996  DemuxerData data = CreateReadFromDemuxerAckWithConfigChanged(true, 0);
1997  player_.OnDemuxerDataAvailable(data);
1998  WaitForAudioDecodeDone();
1999  DecodeAudioDataUntilOutputBecomesAvailable();
2000}
2001
2002}  // namespace media
2003