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

media / mojo / services / mojo_demuxer_stream_adapter_unittest.cc [blame]

// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <memory>

#include "base/run_loop.h"
#include "base/task/sequenced_task_runner.h"
#include "base/test/bind.h"
#include "base/test/gmock_callback_support.h"
#include "base/test/task_environment.h"
#include "media/base/decoder_buffer.h"
#include "media/base/demuxer_stream.h"
#include "media/base/mock_filters.h"
#include "media/base/test_helpers.h"
#include "media/mojo/clients/mojo_demuxer_stream_impl.h"
#include "media/mojo/services/mojo_demuxer_stream_adapter.h"
#include "testing/gtest/include/gtest/gtest.h"

using ::base::test::RunOnceCallback;
using ::testing::_;
using ::testing::Invoke;

namespace media {

// This test both MojoDemuxerStreamImpl and MojoDemuxerStreamAdapter.
class MojoDemuxerStreamAdapterTest : public testing::Test {
 public:
  MojoDemuxerStreamAdapterTest() {
    task_runner_ = task_environment_.GetMainThreadTaskRunner();
  }

  void Initialize(DemuxerStream::Type StreamType) {
    base::RunLoop init_loop;
    // wait initialize finish.
    auto init_done_cb = base::BindLambdaForTesting([&]() {
      is_stream_ready_ = true;
      init_loop.QuitWhenIdle();
    });

    stream_ = std::make_unique<MockDemuxerStream>(StreamType);
    if (StreamType == DemuxerStream::Type::AUDIO)
      stream_->set_audio_decoder_config(TestAudioConfig::Normal());
    else if (StreamType == DemuxerStream::Type::VIDEO)
      stream_->set_video_decoder_config(TestVideoConfig::Normal());

    mojo::PendingRemote<mojom::DemuxerStream> stream_remote;
    mojo_client_stream_ = std::make_unique<MojoDemuxerStreamImpl>(
        stream_.get(), stream_remote.InitWithNewPipeAndPassReceiver());

    mojo_stream_adapter_ = std::make_unique<MojoDemuxerStreamAdapter>(
        std::move(stream_remote), std::move(init_done_cb));
    init_loop.Run();
  }

  void ReadBuffer(int count, DemuxerStream::ReadCB done_cb) {
    EXPECT_TRUE(is_stream_ready_);
    EXPECT_GT(count, 0);
    mojo_stream_adapter_->Read(count, std::move(done_cb));
  }

  base::test::TaskEnvironment task_environment_;
  scoped_refptr<base::SequencedTaskRunner> task_runner_;
  std::unique_ptr<MockDemuxerStream> stream_;

  bool is_stream_ready_ = false;
  std::unique_ptr<MojoDemuxerStreamImpl> mojo_client_stream_;
  std::unique_ptr<MojoDemuxerStreamAdapter> mojo_stream_adapter_;
};

TEST_F(MojoDemuxerStreamAdapterTest, InitializeAudioStream) {
  Initialize(DemuxerStream::Type::AUDIO);

  mojo_stream_adapter_->audio_decoder_config();
  EXPECT_EQ(mojo_stream_adapter_->type(), DemuxerStream::Type::AUDIO);
  EXPECT_TRUE(mojo_stream_adapter_->SupportsConfigChanges());
}

TEST_F(MojoDemuxerStreamAdapterTest, InitializeAudioStreamAndReadMultiBuffer) {
  Initialize(DemuxerStream::Type::AUDIO);

  {
    base::RunLoop success_read_loop;
    DemuxerStream::DecoderBufferVector buffers;
    // Requested 200 but just return 100 buffer.
    for (int i = 0; i < 100; ++i) {
      buffers.emplace_back(base::MakeRefCounted<DecoderBuffer>(12));
    }
    EXPECT_CALL(*stream_, OnRead(_))
        .WillOnce(RunOnceCallback<0>(DemuxerStream::Status::kOk, buffers));

    auto done_cb = base::BindLambdaForTesting(
        [&](DemuxerStream::Status status,
            DemuxerStream::DecoderBufferVector buffers) {
          EXPECT_EQ(status, DemuxerStream::Status::kOk);
          EXPECT_EQ(buffers.size(), 100u);
          success_read_loop.QuitWhenIdle();
        });
    ReadBuffer(200, done_cb);
    success_read_loop.Run();
  }

  {
    base::RunLoop config_changed_read_loop;
    EXPECT_CALL(*stream_, OnRead(_))
        .WillOnce(RunOnceCallback<0>(DemuxerStream::Status::kConfigChanged,
                                     DemuxerStream::DecoderBufferVector()));

    auto done_cb = base::BindLambdaForTesting(
        [&](DemuxerStream::Status status,
            DemuxerStream::DecoderBufferVector buffers) {
          EXPECT_EQ(status, DemuxerStream::Status::kConfigChanged);
          EXPECT_TRUE(buffers.empty());
          config_changed_read_loop.QuitWhenIdle();
        });
    ReadBuffer(200, done_cb);
    config_changed_read_loop.Run();
  }

  {
    base::RunLoop abort_read_loop;
    EXPECT_CALL(*stream_, OnRead(_))
        .WillOnce(RunOnceCallback<0>(DemuxerStream::Status::kAborted,
                                     DemuxerStream::DecoderBufferVector()));

    auto done_cb = base::BindLambdaForTesting(
        [&](DemuxerStream::Status status,
            DemuxerStream::DecoderBufferVector buffers) {
          EXPECT_EQ(status, DemuxerStream::Status::kAborted);
          EXPECT_TRUE(buffers.empty());
          abort_read_loop.QuitWhenIdle();
        });
    ReadBuffer(200, done_cb);
    abort_read_loop.Run();
  }
}

TEST_F(MojoDemuxerStreamAdapterTest, InitializeAudioStreamAndReadOneBuffer) {
  Initialize(DemuxerStream::Type::AUDIO);

  {
    base::RunLoop success_read_loop;
    DemuxerStream::DecoderBufferVector buffers;
    buffers.emplace_back(base::MakeRefCounted<DecoderBuffer>(12));
    EXPECT_CALL(*stream_, OnRead(_))
        .WillOnce(RunOnceCallback<0>(DemuxerStream::Status::kOk, buffers));

    auto done_cb = base::BindLambdaForTesting(
        [&](DemuxerStream::Status status,
            DemuxerStream::DecoderBufferVector buffers) {
          EXPECT_EQ(status, DemuxerStream::Status::kOk);
          success_read_loop.QuitWhenIdle();
        });
    ReadBuffer(1, done_cb);
    success_read_loop.Run();
  }

  {
    base::RunLoop config_changed_read_loop;
    EXPECT_CALL(*stream_, OnRead(_))
        .WillOnce(RunOnceCallback<0>(DemuxerStream::Status::kConfigChanged,
                                     DemuxerStream::DecoderBufferVector()));

    auto done_cb = base::BindLambdaForTesting(
        [&](DemuxerStream::Status status,
            DemuxerStream::DecoderBufferVector buffers) {
          EXPECT_EQ(status, DemuxerStream::Status::kConfigChanged);
          EXPECT_TRUE(buffers.empty());
          config_changed_read_loop.QuitWhenIdle();
        });
    ReadBuffer(1, done_cb);
    config_changed_read_loop.Run();
  }

  {
    base::RunLoop abort_read_loop;
    EXPECT_CALL(*stream_, OnRead(_))
        .WillOnce(RunOnceCallback<0>(DemuxerStream::Status::kAborted,
                                     DemuxerStream::DecoderBufferVector()));

    auto done_cb = base::BindLambdaForTesting(
        [&](DemuxerStream::Status status,
            DemuxerStream::DecoderBufferVector buffers) {
          EXPECT_EQ(status, DemuxerStream::Status::kAborted);
          EXPECT_TRUE(buffers.empty());
          abort_read_loop.QuitWhenIdle();
        });
    ReadBuffer(1, done_cb);
    abort_read_loop.Run();
  }
}

TEST_F(MojoDemuxerStreamAdapterTest, InitializeVideoStream) {
  Initialize(DemuxerStream::Type::VIDEO);

  mojo_stream_adapter_->video_decoder_config();
  EXPECT_EQ(mojo_stream_adapter_->type(), DemuxerStream::Type::VIDEO);
  EXPECT_TRUE(mojo_stream_adapter_->SupportsConfigChanges());
}

TEST_F(MojoDemuxerStreamAdapterTest, EnableBitstreamConverter) {
  Initialize(DemuxerStream::Type::VIDEO);

  base::RunLoop loop;
  EXPECT_CALL(*stream_, EnableBitstreamConverter());
  mojo_stream_adapter_->EnableBitstreamConverter();
  loop.RunUntilIdle();
}

}  // namespace media