tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

TestOggWriter.cpp (2145B)


      1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* vim: set ts=2 et sw=2 tw=80: */
      3 /* This Source Code Form is subject to the terms of the Mozilla Public
      4 * License, v. 2.0. If a copy of the MPL was not distributed with this
      5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      6 
      7 #include "OggWriter.h"
      8 #include "OpusTrackEncoder.h"
      9 #include "gtest/gtest.h"
     10 
     11 using namespace mozilla;
     12 
     13 // Writing multiple 4kB-pages should return all of them on getting.
     14 TEST(TestOggWriter, MultiPageInput)
     15 {
     16  auto opusMeta = MakeRefPtr<OpusMetadata>();
     17  opusMeta->mChannels = 1;
     18  opusMeta->mSamplingFrequency = 48000;
     19  opusMeta->mIdHeader.AppendElement(1);
     20  opusMeta->mCommentHeader.AppendElement(1);
     21  AutoTArray<RefPtr<TrackMetadataBase>, 1> metadata;
     22  metadata.AppendElement(std::move(opusMeta));
     23 
     24  OggWriter ogg;
     25  MOZ_ALWAYS_SUCCEEDS(ogg.SetMetadata(metadata));
     26  {
     27    nsTArray<nsTArray<uint8_t>> buffer;
     28    MOZ_ALWAYS_SUCCEEDS(
     29        ogg.GetContainerData(&buffer, ContainerWriter::GET_HEADER));
     30  }
     31 
     32  size_t inputBytes = 0;
     33  const size_t USECS_PER_MS = 1000;
     34  auto frameData = MakeRefPtr<EncodedFrame::FrameData>();
     35  frameData->SetLength(320);  // 320B per 20ms == 128kbps
     36  PodZero(frameData->Elements(), frameData->Length());
     37  // 50 frames at 320B = 16kB = 4 4kB-pages
     38  for (int i = 0; i < 50; ++i) {
     39    auto frame = MakeRefPtr<EncodedFrame>(
     40        media::TimeUnit::FromMicroseconds(20 * USECS_PER_MS * i),
     41        48000 / 1000 * 20 /* 20ms */, 48000, EncodedFrame::OPUS_AUDIO_FRAME,
     42        frameData);
     43    AutoTArray<RefPtr<EncodedFrame>, 1> frames;
     44    frames.AppendElement(std::move(frame));
     45    uint32_t flags = 0;
     46    if (i == 49) {
     47      flags |= ContainerWriter::END_OF_STREAM;
     48    }
     49    MOZ_ALWAYS_SUCCEEDS(ogg.WriteEncodedTrack(frames, flags));
     50    inputBytes += frameData->Length();
     51  }
     52 
     53  nsTArray<nsTArray<uint8_t>> buffer;
     54  MOZ_ALWAYS_SUCCEEDS(
     55      ogg.GetContainerData(&buffer, ContainerWriter::FLUSH_NEEDED));
     56  size_t outputBytes = 0;
     57  for (const auto& b : buffer) {
     58    outputBytes += b.Length();
     59  }
     60 
     61  EXPECT_EQ(inputBytes, 16000U);
     62  EXPECT_EQ(outputBytes, 16208U);
     63 }