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

media / cast / openscreen / remoting_proto_utils_unittest.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/cast/openscreen/remoting_proto_utils.h"

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

#include "base/memory/ref_counted.h"
#include "media/base/audio_decoder_config.h"
#include "media/base/decoder_buffer.h"
#include "media/base/demuxer_stream.h"
#include "media/base/encryption_scheme.h"
#include "media/base/test_helpers.h"
#include "media/base/video_decoder_config.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/openscreen/src/cast/streaming/remoting.pb.h"

using testing::_;
using testing::Invoke;
using testing::Return;

namespace media::cast {

class ProtoUtilsTest : public testing::Test {
 protected:
  void SetUp() override {}
};

TEST_F(ProtoUtilsTest, PassEOSDecoderBuffer) {
  // 1. To DecoderBuffer
  const scoped_refptr<media::DecoderBuffer> input_buffer =
      media::DecoderBuffer::CreateEOSBuffer();

  // 2. To Byte Array
  const base::HeapArray<uint8_t> data = DecoderBufferToByteArray(*input_buffer);

  // 3. To DecoderBuffer
  const scoped_refptr<media::DecoderBuffer> output_buffer =
      ByteArrayToDecoderBuffer(data);
  DCHECK(output_buffer);

  ASSERT_TRUE(output_buffer->end_of_stream());
}

TEST_F(ProtoUtilsTest, PassValidDecoderBuffer) {
  const uint8_t buffer[] = {
      0,   0,   0,   1,   9,   224, 0,   0,   0,   1,   103, 77,  64,  21,  217,
      1,   177, 254, 78,  16,  0,   0,   62,  144, 0,   11,  184, 0,   241, 98,
      228, 128, 0,   0,   0,   1,   104, 235, 143, 32,  0,   0,   0,   1,   103,
      77,  64,  21,  217, 1,   177, 254, 78,  16,  0,   0,   62,  144, 0,   11,
      184, 0,   241, 98,  228, 128, 0,   0,   0,   1,   104, 235, 143, 32,  0,
      0,   0,   1,   101, 136, 132, 25,  255, 0,   191, 98,  0,   6,   29,  63,
      252, 65,  246, 207, 255, 235, 63,  172, 35,  112, 198, 115, 222, 243, 159,
      232, 208, 32,  0,   0,   3,   0,   0,   203, 255, 149, 20,  71,  203, 213,
      40,  0,   0,   139, 0,   24,  117, 166, 249, 227, 68,  230, 177, 134, 161,
      162, 1,   22,  105, 78,  66,  183, 130, 158, 108, 252, 112, 113, 58,  159,
      72,  116, 78,  141, 133, 76,  225, 209, 13,  221, 49,  187, 83,  123, 193,
      112, 123, 112, 74,  121, 133};
  const uint8_t side_buffer[] = {'X', 'X'};
  const base::TimeDelta pts = base::Milliseconds(5);

  // 1. To DecoderBuffer
  const scoped_refptr<media::DecoderBuffer> input_buffer =
      media::DecoderBuffer::CopyFrom(buffer);
  input_buffer->set_timestamp(pts);
  input_buffer->set_is_key_frame(true);
  input_buffer->WritableSideData().alpha_data =
      base::HeapArray<uint8_t>::CopiedFrom(side_buffer);

  // 2. To Byte Array
  const base::HeapArray<uint8_t> data = DecoderBufferToByteArray(*input_buffer);

  // 3. To DecoderBuffer
  const scoped_refptr<media::DecoderBuffer> output_buffer =
      ByteArrayToDecoderBuffer(data);
  DCHECK(output_buffer);

  ASSERT_FALSE(output_buffer->end_of_stream());
  ASSERT_TRUE(output_buffer->is_key_frame());
  ASSERT_EQ(output_buffer->timestamp(), pts);
  EXPECT_EQ(base::span(*output_buffer), base::span(buffer));
  ASSERT_TRUE(output_buffer->side_data());
  EXPECT_EQ(output_buffer->side_data()->alpha_data.as_span(),
            base::span(side_buffer));
}

TEST_F(ProtoUtilsTest, AudioDecoderConfigConversionTest) {
  const char extra_data[4] = {'A', 'C', 'E', 'G'};
  media::AudioDecoderConfig audio_config(
      media::AudioCodec::kOpus, media::kSampleFormatF32,
      media::CHANNEL_LAYOUT_MONO, 48000,
      std::vector<uint8_t>(std::begin(extra_data), std::end(extra_data)),
      media::EncryptionScheme::kUnencrypted);
  ASSERT_TRUE(audio_config.IsValidConfig());

  openscreen::cast::AudioDecoderConfig audio_message;
  ConvertAudioDecoderConfigToProto(audio_config, &audio_message);

  media::AudioDecoderConfig audio_output_config;
  ASSERT_TRUE(
      ConvertProtoToAudioDecoderConfig(audio_message, &audio_output_config));

  ASSERT_TRUE(audio_config.Matches(audio_output_config));
}

TEST_F(ProtoUtilsTest, AudioDecoderConfigHandlesAacExtraDataCorrectly) {
  constexpr char aac_extra_data[4] = {'A', 'C', 'E', 'G'};
  media::AudioDecoderConfig audio_config(
      media::AudioCodec::kAAC, media::kSampleFormatF32,
      media::CHANNEL_LAYOUT_MONO, 48000, std::vector<uint8_t>{},
      media::EncryptionScheme::kUnencrypted);
  audio_config.set_aac_extra_data(std::vector<uint8_t>(
      std::begin(aac_extra_data), std::end(aac_extra_data)));
  ASSERT_TRUE(audio_config.IsValidConfig());

  openscreen::cast::AudioDecoderConfig audio_message;
  ConvertAudioDecoderConfigToProto(audio_config, &audio_message);

  // We should have filled the "extra_data" protobuf field with
  // "aac_extra_data."
  const std::vector<uint8_t> proto_extra_data(
      audio_message.extra_data().begin(), audio_message.extra_data().end());
  EXPECT_THAT(proto_extra_data, testing::ElementsAreArray(aac_extra_data));

  media::AudioDecoderConfig audio_output_config;
  ASSERT_TRUE(
      ConvertProtoToAudioDecoderConfig(audio_message, &audio_output_config));
  ASSERT_TRUE(audio_config.Matches(audio_output_config))
      << "expected=" << audio_config.AsHumanReadableString()
      << ", actual=" << audio_output_config.AsHumanReadableString();
}

TEST_F(ProtoUtilsTest, PipelineStatisticsConversion) {
  media::PipelineStatistics original;
  // NOTE: all fields should be initialised here.
  original.audio_bytes_decoded = 123;
  original.video_bytes_decoded = 456;
  original.video_frames_decoded = 789;
  original.video_frames_decoded_power_efficient = 0;
  original.video_frames_dropped = 21;
  original.audio_memory_usage = 32;
  original.video_memory_usage = 43;
  original.video_keyframe_distance_average = base::TimeDelta::Max();
  original.video_frame_duration_average = base::TimeDelta::Max();
  original.audio_pipeline_info = {false, false,
                                  media::AudioDecoderType::kUnknown,
                                  media::EncryptionType::kClear};
  original.video_pipeline_info = {false, false,
                                  media::VideoDecoderType::kUnknown,
                                  media::EncryptionType::kClear};

  // There is no convert-to-proto function, so just do that here.
  openscreen::cast::PipelineStatistics pb_stats;
  openscreen::cast::VideoDecoderInfo* pb_video_info =
      pb_stats.mutable_video_decoder_info();
  openscreen::cast::AudioDecoderInfo* pb_audio_info =
      pb_stats.mutable_audio_decoder_info();
  pb_stats.set_audio_bytes_decoded(original.audio_bytes_decoded);
  pb_stats.set_video_bytes_decoded(original.video_bytes_decoded);
  pb_stats.set_video_frames_decoded(original.video_frames_decoded);
  pb_stats.set_video_frames_dropped(original.video_frames_dropped);
  pb_stats.set_audio_memory_usage(original.audio_memory_usage);
  pb_stats.set_video_memory_usage(original.video_memory_usage);
  pb_stats.set_video_frame_duration_average_usec(
      original.video_frame_duration_average.InMicroseconds());

  pb_video_info->set_decoder_type(
      static_cast<int64_t>(original.video_pipeline_info.decoder_type));
  pb_video_info->set_is_platform_decoder(
      original.video_pipeline_info.is_platform_decoder);

  pb_audio_info->set_decoder_type(
      static_cast<int64_t>(original.audio_pipeline_info.decoder_type));
  pb_audio_info->set_is_platform_decoder(
      original.audio_pipeline_info.is_platform_decoder);

  media::PipelineStatistics converted;

  // NOTE: fields will all be initialized with 0xcd. Forcing the conversion to
  // properly assigned them. Since nested structs have strings, memsetting must
  // be done infividually for them.
  memset(&converted, 0xcd,
         sizeof(converted) - sizeof(media::AudioPipelineInfo) -
             sizeof(media::VideoPipelineInfo));
  memset(&converted.audio_pipeline_info, 0xcd,
         sizeof(media::AudioPipelineInfo));
  memset(&converted.video_pipeline_info, 0xcd,
         sizeof(media::VideoPipelineInfo));

  ConvertProtoToPipelineStatistics(pb_stats, &converted);

  // If this fails, did media::PipelineStatistics add/change fields that are not
  // being set by media::remoting::ConvertProtoToPipelineStatistics()?
  EXPECT_EQ(original, converted);
}

TEST_F(ProtoUtilsTest, VideoDecoderConfigConversionTest) {
  const media::VideoDecoderConfig video_config =
      media::TestVideoConfig::Normal();
  ASSERT_TRUE(video_config.IsValidConfig());
  openscreen::cast::VideoDecoderConfig message;
  ConvertVideoDecoderConfigToProto(video_config, &message);
  media::VideoDecoderConfig converted;
  ASSERT_TRUE(ConvertProtoToVideoDecoderConfig(message, &converted));
  ASSERT_TRUE(converted.Matches(video_config));
}

}  // namespace media::cast