1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124
  125
  126
  127
  128
  129
  130
  131
  132
  133
  134
  135
  136
  137
  138
  139
  140
  141
  142
  143
  144
  145
  146
  147
  148
  149
  150
  151
  152
  153
  154
  155
  156
  157
  158
  159
  160
  161
  162
  163
  164
  165
  166
  167
  168
  169
  170
  171
  172
  173
  174
  175
  176
  177
  178
  179
  180
  181
  182
  183
  184
  185
  186
  187
  188
  189
  190
  191
  192
  193
  194
  195
  196
  197
  198
  199
  200
  201
  202
  203
  204
  205
  206
  207
  208
  209
  210
  211
  212
  213
  214
  215
  216
  217
  218
  219
  220
  221
  222
  223
  224
  225
  226
  227
  228
  229
  230
  231
  232
  233
  234
  235
  236
  237
  238
  239
  240
  241
  242
  243
  244
  245
  246
  247
  248
  249
  250
  251
  252
  253
  254
  255
  256
  257
  258
  259
  260
  261
  262
  263
  264
  265
  266
  267
  268
  269
  270
  271
  272
  273
  274
  275
  276
  277
  278
  279
  280
  281
  282
  283
  284
  285
  286
  287
  288
  289
  290
  291
  292
  293
  294
  295
  296
  297
  298
  299
  300
  301
  302
  303
  304
  305
  306
  307
  308
  309
  310
  311
  312
  313
  314
  315
  316
  317
  318
  319
  320
  321
  322
  323
  324
  325
  326
  327
  328
  329
  330
  331
  332
  333
  334
  335
  336
  337
  338
  339
  340
  341
  342
  343
  344
  345

media / filters / hls_data_source_provider_impl_unittest.cc [blame]

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

#include "media/filters/hls_data_source_provider_impl.h"

#include <memory>
#include <string>
#include <vector>

#include "base/memory/scoped_refptr.h"
#include "base/run_loop.h"
#include "base/test/gmock_callback_support.h"
#include "base/test/simple_test_tick_clock.h"
#include "base/test/task_environment.h"
#include "media/base/mock_media_log.h"
#include "media/filters/hls_test_helpers.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace media {

using base::test::RunOnceCallback;
using testing::_;
using testing::AtLeast;
using testing::ByMove;
using testing::DoAll;
using testing::Eq;
using testing::Invoke;
using testing::NiceMock;
using testing::NotNull;
using testing::Ref;
using testing::Return;
using testing::SaveArg;
using testing::SetArgPointee;
using testing::StrictMock;

class HlsDataSourceProviderImplUnittest : public testing::Test {
 public:
  ~HlsDataSourceProviderImplUnittest() override = default;
  HlsDataSourceProviderImplUnittest() { RecreateImpl(); }

  void RecreateImpl() {
    auto factory = std::make_unique<MockDataSourceFactory>();
    factory_ = factory.get();
    impl_ = std::make_unique<HlsDataSourceProviderImpl>(std::move(factory));
  }

 protected:
  base::test::TaskEnvironment task_environment_;
  std::unique_ptr<HlsDataSourceProviderImpl> impl_;

  raw_ptr<MockDataSourceFactory> factory_;
};

TEST_F(HlsDataSourceProviderImplUnittest, TestReadFromUrlOnce) {
  // The entire read is satisfied, so there is more to read.
  factory_->AddReadExpectation(0, 16384, 16384);
  impl_->ReadFromUrl(
      {GURL("example.com"), std::nullopt},
      base::BindOnce([](HlsDataSourceProvider::ReadResult result) {
        ASSERT_TRUE(result.has_value());
        auto stream = std::move(result).value();
        ASSERT_EQ(stream->read_position(), 16384lu);
        ASSERT_EQ(stream->buffer_size(), 16384lu);
        ASSERT_EQ(stream->max_read_position(), std::nullopt);
        ASSERT_TRUE(stream->CanReadMore());
      }));
  task_environment_.RunUntilIdle();

  // Only got 400 bytes of requested, so the stream is _probably_ ended, but
  // we'd have to read again (and get a 0) to be sure.
  factory_->AddReadExpectation(0, 16384, 400);
  impl_->ReadFromUrl(
      {GURL("example.com"), std::nullopt},
      base::BindOnce([](HlsDataSourceProvider::ReadResult result) {
        ASSERT_TRUE(result.has_value());
        auto stream = std::move(result).value();
        ASSERT_TRUE(stream->CanReadMore());
        ASSERT_EQ(stream->read_position(), 400lu);
        ASSERT_EQ(stream->buffer_size(), 16384lu);
        ASSERT_EQ(stream->max_read_position(), std::nullopt);
      }));
  task_environment_.RunUntilIdle();

  // The data source should only be limited to 4242 total bytes and should start
  // at an offset of 99. The read should be from 99, size of 4242.
  factory_->AddReadExpectation(99, 4242, 4242);
  impl_->ReadFromUrl(
      {GURL("example.com"), hls::types::ByteRange::Validate(4242, 99)},
      base::BindOnce([](HlsDataSourceProvider::ReadResult result) {
        ASSERT_TRUE(result.has_value());
        auto stream = std::move(result).value();
        ASSERT_EQ(stream->read_position(), 4341lu);
        ASSERT_EQ(stream->buffer_size(), 4242lu);
        ASSERT_EQ(stream->max_read_position().value_or(0), 4341lu);
        ASSERT_FALSE(stream->CanReadMore());
      }));
  task_environment_.RunUntilIdle();
}

TEST_F(HlsDataSourceProviderImplUnittest, TestReadFromUrlThenReadAgain) {
  factory_->AddReadExpectation(0, 16384, 16384);
  factory_->AddReadExpectation(16384, 16384, 16384);
  factory_->AddReadExpectation(32768, 16384, 3);
  factory_->AddReadExpectation(32771, 16384, 0);
  impl_->ReadFromUrl(
      {GURL("example.com"), std::nullopt},
      base::BindOnce(
          [](HlsDataSourceProviderImpl* impl_ptr,
             HlsDataSourceProvider::ReadResult result) {
            ASSERT_TRUE(result.has_value());
            auto stream = std::move(result).value();
            ASSERT_EQ(stream->read_position(), 16384lu);
            ASSERT_EQ(stream->buffer_size(), 16384lu);
            ASSERT_TRUE(stream->CanReadMore());

            impl_ptr->ReadFromExistingStream(
                std::move(stream),
                base::BindOnce(
                    [](HlsDataSourceProviderImpl* impl_ptr,
                       HlsDataSourceProvider::ReadResult result) {
                      ASSERT_TRUE(result.has_value());
                      auto stream = std::move(result).value();
                      ASSERT_EQ(stream->read_position(), 32768lu);
                      ASSERT_EQ(stream->buffer_size(), 32768lu);
                      ASSERT_TRUE(stream->CanReadMore());

                      impl_ptr->ReadFromExistingStream(
                          std::move(stream),
                          base::BindOnce(
                              [](HlsDataSourceProviderImpl* impl_ptr,
                                 HlsDataSourceProvider::ReadResult result) {
                                ASSERT_TRUE(result.has_value());
                                auto stream = std::move(result).value();
                                ASSERT_EQ(stream->read_position(), 32771lu);
                                ASSERT_EQ(stream->buffer_size(), 49152lu);
                                ASSERT_TRUE(stream->CanReadMore());

                                impl_ptr->ReadFromExistingStream(
                                    std::move(stream),
                                    base::BindOnce(
                                        [](HlsDataSourceProvider::ReadResult
                                               result) {
                                          ASSERT_TRUE(result.has_value());
                                          auto stream =
                                              std::move(result).value();
                                          ASSERT_EQ(stream->read_position(),
                                                    32771lu);
                                          ASSERT_EQ(stream->buffer_size(),
                                                    32771lu);
                                          ASSERT_FALSE(stream->CanReadMore());
                                        }));
                              },
                              impl_ptr));
                    },
                    impl_ptr));
          },
          impl_.get()));

  task_environment_.RunUntilIdle();
}

TEST_F(HlsDataSourceProviderImplUnittest, TestAbortMidDownload) {
  // Pregenerating the mock requires setting all our own expectations.
  auto* mock_data_source = factory_->PregenerateNextMock();
  EXPECT_CALL(*mock_data_source, Initialize).WillOnce(RunOnceCallback<0>(true));
  EXPECT_CALL(*mock_data_source, Abort()).Times(0);
  EXPECT_CALL(*mock_data_source, Stop()).Times(0);

  DataSource::ReadCB read_cb;
  EXPECT_CALL(*mock_data_source, Read(0, _, _, _))
      .WillOnce(
          [&read_cb](int64_t, int, uint8_t*, DataSource::ReadCB cb) {
            read_cb = std::move(cb);
          });

  // The Read CB is captured, and so will not execute right away.
  bool has_been_read = false;
  impl_->ReadFromUrl(
      {GURL("example.com"), std::nullopt},
      base::BindOnce(
          [](bool* read_canary, HlsDataSourceProvider::ReadResult result) {
            *read_canary = true;
          },
          &has_been_read));

  // cycle everything and check that we are blocking the read.
  task_environment_.RunUntilIdle();
  ASSERT_FALSE(has_been_read);
  ASSERT_TRUE(!!read_cb);

  // Deleting the HlsDataSourceproviderImpl will stop all existing reads.
  EXPECT_CALL(*mock_data_source, Stop());
  RecreateImpl();
  task_environment_.RunUntilIdle();

  // Run with aborted signal.
  std::move(read_cb).Run(-2);

  task_environment_.RunUntilIdle();
  ASSERT_TRUE(has_been_read);
}

TEST_F(HlsDataSourceProviderImplUnittest, AbortMidInit) {
  auto* mock_data_source = factory_->PregenerateNextMock();

  // Don't run init cb!
  EXPECT_CALL(*mock_data_source, Initialize);
  EXPECT_CALL(*mock_data_source, Stop());

  bool has_been_read = false;
  impl_->ReadFromUrl(
      {GURL("example.com"), std::nullopt},
      base::BindOnce(
          [](bool* read_canary, HlsDataSourceProvider::ReadResult result) {
            *read_canary = true;
          },
          &has_been_read));

  // Despite the init never returning, it is stored in the `data_source_map_`
  // and all entries there get stopped on teardown.
  task_environment_.RunUntilIdle();

  // Should be false, because the stream init function won't post it's callback.
  ASSERT_FALSE(has_been_read);
}

TEST_F(HlsDataSourceProviderImplUnittest, ReadMultipleSegments) {
  HlsDataSourceProvider::SegmentQueue segments;
  segments.emplace(GURL("example.com"), std::nullopt);
  segments.emplace(GURL("foo.com"), std::nullopt);

  // Request 16k, but only 4k is read. Another read then happens and the 0 byte
  // EOS read happens.
  factory_->AddReadExpectation(0, 16384, 4096);
  factory_->AddReadExpectation(4096, 16384, 0);

  std::unique_ptr<HlsDataSourceStream> read_result;
  impl_->ReadFromCombinedUrlQueue(
      std::move(segments), base::BindOnce(
                               [](std::unique_ptr<HlsDataSourceStream>* extract,
                                  HlsDataSourceProvider::ReadResult result) {
                                 *extract = std::move(result).value();
                               },
                               &read_result));
  task_environment_.RunUntilIdle();
  ASSERT_NE(read_result, nullptr);
  ASSERT_TRUE(read_result->CanReadMore());
  ASSERT_FALSE(read_result->RequiresNextDataSource());

  impl_->ReadFromExistingStream(
      std::move(read_result),
      base::BindOnce(
          [](std::unique_ptr<HlsDataSourceStream>* extract,
             HlsDataSourceProvider::ReadResult result) {
            *extract = std::move(result).value();
          },
          &read_result));
  task_environment_.RunUntilIdle();
  ASSERT_NE(read_result, nullptr);
  ASSERT_TRUE(read_result->CanReadMore());
  ASSERT_TRUE(read_result->RequiresNextDataSource());

  factory_->AddReadExpectation(0, 16384, 4096);
  factory_->AddReadExpectation(4096, 16384, 0);
  impl_->ReadFromExistingStream(
      std::move(read_result),
      base::BindOnce(
          [](std::unique_ptr<HlsDataSourceStream>* extract,
             HlsDataSourceProvider::ReadResult result) {
            *extract = std::move(result).value();
          },
          &read_result));

  task_environment_.RunUntilIdle();
  ASSERT_NE(read_result, nullptr);
  ASSERT_TRUE(read_result->CanReadMore());
  ASSERT_FALSE(read_result->RequiresNextDataSource());

  impl_->ReadFromExistingStream(
      std::move(read_result),
      base::BindOnce(
          [](std::unique_ptr<HlsDataSourceStream>* extract,
             HlsDataSourceProvider::ReadResult result) {
            *extract = std::move(result).value();
          },
          &read_result));

  task_environment_.RunUntilIdle();
  ASSERT_NE(read_result, nullptr);
  ASSERT_FALSE(read_result->CanReadMore());
  ASSERT_FALSE(read_result->RequiresNextDataSource());

  read_result = nullptr;
  task_environment_.RunUntilIdle();
}

TEST_F(HlsDataSourceProviderImplUnittest, ReadMultipleRangedSegments) {
  HlsDataSourceProvider::SegmentQueue segments;
  // Read 10 bytes from offset 0.
  segments.emplace(GURL("example.com"), hls::types::ByteRange::Validate(10, 0));

  // Read 100 bytes from offset 100
  segments.emplace(GURL("foo.com"), hls::types::ByteRange::Validate(100, 100));

  // Request 10 bytes from the 0 offset. this is fairly common for EXT-X-MAP.
  factory_->AddReadExpectation(0, 10, 10);

  std::unique_ptr<HlsDataSourceStream> read_result;
  impl_->ReadFromCombinedUrlQueue(
      std::move(segments), base::BindOnce(
                               [](std::unique_ptr<HlsDataSourceStream>* extract,
                                  HlsDataSourceProvider::ReadResult result) {
                                 *extract = std::move(result).value();
                               },
                               &read_result));

  task_environment_.RunUntilIdle();
  ASSERT_NE(read_result, nullptr);
  ASSERT_TRUE(read_result->CanReadMore());
  ASSERT_TRUE(read_result->RequiresNextDataSource());

  // The second segment will request another 100 bytes, and again, because it is
  // a range request, more should be returned.
  factory_->AddReadExpectation(100, 100, 100);
  impl_->ReadFromExistingStream(
      std::move(read_result),
      base::BindOnce(
          [](std::unique_ptr<HlsDataSourceStream>* extract,
             HlsDataSourceProvider::ReadResult result) {
            *extract = std::move(result).value();
          },
          &read_result));

  task_environment_.RunUntilIdle();
  ASSERT_NE(read_result, nullptr);
  ASSERT_FALSE(read_result->CanReadMore());
  ASSERT_FALSE(read_result->RequiresNextDataSource());

  read_result = nullptr;
  task_environment_.RunUntilIdle();
}

}  // namespace blink