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

media / filters / memory_data_source.cc [blame]

// Copyright 2016 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/memory_data_source.h"

#include <algorithm>

#include "base/check.h"
#include "base/functional/callback.h"

namespace media {

MemoryDataSource::MemoryDataSource(std::string data)
    : data_string_(std::move(data)),
      data_(reinterpret_cast<const uint8_t*>(data_string_.data())),
      size_(data_string_.size()) {}

MemoryDataSource::MemoryDataSource(const uint8_t* data, size_t size)
    : data_(data), size_(size) {}

MemoryDataSource::~MemoryDataSource() = default;

void MemoryDataSource::Read(int64_t position,
                            int size,
                            uint8_t* data,
                            DataSource::ReadCB read_cb) {
  DCHECK(read_cb);

  if (is_stopped_ || size < 0 || position < 0 ||
      static_cast<size_t>(position) > size_) {
    std::move(read_cb).Run(kReadError);
    return;
  }

  // Cap size within bounds.
  size_t clamped_size = std::min(static_cast<size_t>(size),
                                 size_ - static_cast<size_t>(position));

  if (clamped_size > 0) {
    DCHECK(data);
    memcpy(data, data_ + base::checked_cast<size_t>(position), clamped_size);
  }

  std::move(read_cb).Run(clamped_size);
}

void MemoryDataSource::Stop() {
  is_stopped_ = true;
}

void MemoryDataSource::Abort() {}

bool MemoryDataSource::GetSize(int64_t* size_out) {
  *size_out = size_;
  return true;
}

bool MemoryDataSource::IsStreaming() {
  return false;
}

void MemoryDataSource::SetBitrate(int bitrate) {}

bool MemoryDataSource::PassedTimingAllowOriginCheck() {
  // There are no HTTP responses, so this can safely return true.
  return true;
}

bool MemoryDataSource::WouldTaintOrigin() {
  // There are no HTTP responses, so this can safely return false.
  return false;
}

}  // namespace media