summaryrefslogtreecommitdiffstats
path: root/media/base/test_data_util.cc
blob: bb08175ff86d739c261258ef9077de720c6e2c5b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "media/base/test_data_util.h"

#include "base/file_util.h"
#include "base/logging.h"
#include "base/path_service.h"
#include "media/base/buffers.h"
#include "media/base/data_buffer.h"
#include "media/ffmpeg/ffmpeg_common.h"

namespace media {

std::string GetTestDataURL(const std::string& name) {
  FilePath file_path;
  CHECK(PathService::Get(base::DIR_SOURCE_ROOT, &file_path));

  file_path = file_path.Append(FILE_PATH_LITERAL("media"))
      .Append(FILE_PATH_LITERAL("test"))
      .Append(FILE_PATH_LITERAL("data"))
      .AppendASCII(name);
  return file_path.MaybeAsASCII();
}

void ReadTestDataFile(const std::string& name, scoped_array<uint8>* buffer,
                      int* size) {
  FilePath file_path;
  CHECK(PathService::Get(base::DIR_SOURCE_ROOT, &file_path));

  file_path = file_path.Append(FILE_PATH_LITERAL("media"))
      .Append(FILE_PATH_LITERAL("test"))
      .Append(FILE_PATH_LITERAL("data"))
      .AppendASCII(name);

  int64 tmp = 0;
  CHECK(file_util::GetFileSize(file_path, &tmp))
      << "Failed to get file size for '" << name << "'";

  // Why FF_INPUT_BUFFER_PADDING_SIZE? FFmpeg assumes all input buffers are
  // padded. Since most of our test data is passed to FFmpeg, it makes sense
  // to do the padding here instead of scattering it around test code.
  int file_size = static_cast<int>(tmp);
  int padded_size = file_size + FF_INPUT_BUFFER_PADDING_SIZE;
  buffer->reset(reinterpret_cast<uint8_t*>(new uint8[padded_size]));
  memset(buffer->get(), 0, padded_size);

  CHECK(file_size == file_util::ReadFile(file_path,
                                         reinterpret_cast<char*>(buffer->get()),
                                         file_size))
    << "Failed to read '" << name << "'";
  *size = file_size;
}

void ReadTestDataFile(const std::string& name,
                      scoped_refptr<DataBuffer>* buffer) {
  scoped_array<uint8> buf;
  int buf_size;
  ReadTestDataFile(name, &buf, &buf_size);
  *buffer = new DataBuffer(buf.Pass(), buf_size);
}

void ReadTestDataFile(const std::string& name,
                      scoped_refptr<Buffer>* buffer) {
  scoped_refptr<DataBuffer> data_buffer;
  ReadTestDataFile(name, &data_buffer);
  *buffer = data_buffer;
}

}  // namespace media