File: encoded_audio_chunk_test.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (122 lines) | stat: -rw-r--r-- 4,824 bytes parent folder | download | duplicates (6)
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
// 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 "third_party/blink/renderer/modules/webcodecs/encoded_audio_chunk.h"

#include "base/containers/span.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_binding_for_testing.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_decrypt_config.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_encoded_audio_chunk_init.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_encoded_audio_chunk_type.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_subsample_entry.h"
#include "third_party/blink/renderer/modules/webcodecs/test_helpers.h"
#include "third_party/blink/renderer/platform/testing/task_environment.h"
#include "third_party/blink/renderer/platform/wtf/text/wtf_string.h"

namespace blink {

namespace {

TEST(EncodedAudioChunkTest, ConstructorAndAttributes) {
  test::TaskEnvironment task_environment;
  V8TestingScope v8_scope;
  V8EncodedAudioChunkType::Enum type = V8EncodedAudioChunkType::Enum::kKey;
  int64_t timestamp = 1000000;
  std::string data = "test";
  auto* init = EncodedAudioChunkInit::Create();
  init->setTimestamp(timestamp);
  init->setType(type);
  init->setData(StringToBuffer(data));
  auto* encoded = EncodedAudioChunk::Create(v8_scope.GetScriptState(), init,
                                            v8_scope.GetExceptionState());

  EXPECT_EQ(type, encoded->type());
  EXPECT_EQ(timestamp, encoded->timestamp());
  EXPECT_EQ(data, BufferToString(*encoded->buffer()));
  EXPECT_FALSE(encoded->duration().has_value());
}

TEST(EncodedAudioChunkTest, ConstructorWithDuration) {
  test::TaskEnvironment task_environment;
  V8TestingScope v8_scope;
  V8EncodedAudioChunkType::Enum type = V8EncodedAudioChunkType::Enum::kKey;
  int64_t timestamp = 1000000;
  uint64_t duration = 16667;
  std::string data = "test";
  auto* init = EncodedAudioChunkInit::Create();
  init->setTimestamp(timestamp);
  init->setDuration(duration);
  init->setType(type);
  init->setData(StringToBuffer(data));
  auto* encoded = EncodedAudioChunk::Create(v8_scope.GetScriptState(), init,
                                            v8_scope.GetExceptionState());

  EXPECT_EQ(type, encoded->type());
  EXPECT_EQ(timestamp, encoded->timestamp());
  EXPECT_EQ(data, BufferToString(*encoded->buffer()));
  ASSERT_TRUE(encoded->duration().has_value());
  EXPECT_EQ(duration, encoded->duration().value());
}

TEST(EncodedAudioChunkTest, TransferBuffer) {
  test::TaskEnvironment task_environment;
  V8TestingScope v8_scope;
  String type = "key";
  int64_t timestamp = 1000000;
  std::string data = "test";
  auto* init = EncodedAudioChunkInit::Create();
  init->setTimestamp(timestamp);
  init->setType(type);
  auto* buffer = DOMArrayBuffer::Create(base::as_byte_span(data));
  init->setData(MakeGarbageCollected<AllowSharedBufferSource>(buffer));
  HeapVector<Member<DOMArrayBuffer>> transfer;
  transfer.push_back(Member<DOMArrayBuffer>(buffer));
  init->setTransfer(std::move(transfer));
  auto* encoded = EncodedAudioChunk::Create(v8_scope.GetScriptState(), init,
                                            v8_scope.GetExceptionState());

  EXPECT_TRUE(buffer->IsDetached());
  EXPECT_EQ(encoded->byteLength(), data.size());
}

TEST(EncodedAudioChunkTest, DecryptConfig) {
  test::TaskEnvironment task_environment;
  V8TestingScope v8_scope;
  auto* init = EncodedAudioChunkInit::Create();
  init->setTimestamp(1);
  init->setType("key");
  init->setData(StringToBuffer("test"));

  auto expected_media_decrypt_config =
      CreateTestDecryptConfig(media::EncryptionScheme::kCenc);

  auto* decrypt_config = MakeGarbageCollected<DecryptConfig>();
  decrypt_config->setEncryptionScheme("cenc");
  decrypt_config->setKeyId(
      StringToBuffer(expected_media_decrypt_config->key_id()));
  decrypt_config->setInitializationVector(
      StringToBuffer(expected_media_decrypt_config->iv()));

  HeapVector<Member<SubsampleEntry>> subsamples;
  for (const auto& entry : expected_media_decrypt_config->subsamples()) {
    auto* js_entry = MakeGarbageCollected<SubsampleEntry>();
    js_entry->setClearBytes(entry.clear_bytes);
    js_entry->setCypherBytes(entry.cypher_bytes);
    subsamples.push_back(js_entry);
  }
  decrypt_config->setSubsampleLayout(subsamples);
  init->setDecryptConfig(decrypt_config);

  auto* encoded = EncodedAudioChunk::Create(v8_scope.GetScriptState(), init,
                                            v8_scope.GetExceptionState());

  ASSERT_NE(nullptr, encoded->buffer()->decrypt_config());
  EXPECT_TRUE(expected_media_decrypt_config->Matches(
      *encoded->buffer()->decrypt_config()));
}

}  // namespace

}  // namespace blink