Shaka Packager SDK
hls_audio_util.cc
1 // Copyright 2018 Google LLC. All rights reserved.
2 //
3 // Use of this source code is governed by a BSD-style
4 // license that can be found in the LICENSE file or at
5 // https://developers.google.com/open-source/licenses/bsd
6 
7 #include <packager/media/codecs/hls_audio_util.h>
8 
9 #include <absl/log/check.h>
10 
11 #include <packager/media/base/buffer_writer.h>
12 #include <packager/media/base/fourccs.h>
13 #include <packager/media/codecs/aac_audio_specific_config.h>
14 
15 namespace shaka {
16 namespace media {
17 
18 bool WriteAudioSetupInformation(Codec codec,
19  const uint8_t* audio_specific_config,
20  size_t audio_specific_config_size,
21  BufferWriter* audio_setup_information) {
22  uint32_t audio_type = FOURCC_NULL;
23  switch (codec) {
24  case kCodecAAC: {
25  AACAudioSpecificConfig config;
26  const bool result = config.Parse(std::vector<uint8_t>(
27  audio_specific_config,
28  audio_specific_config + audio_specific_config_size));
29 
30  AACAudioSpecificConfig::AudioObjectType audio_object_type;
31  if (!result) {
32  LOG(WARNING) << "Failed to parse config. Assuming AAC-LC.";
33  audio_object_type = AACAudioSpecificConfig::AOT_AAC_LC;
34  } else {
35  audio_object_type = config.GetAudioObjectType();
36  }
37 
38  switch (audio_object_type) {
39  case AACAudioSpecificConfig::AOT_AAC_LC:
40  audio_type = FOURCC_zaac;
41  break;
42  case AACAudioSpecificConfig::AOT_SBR:
43  audio_type = FOURCC_zach;
44  break;
45  case AACAudioSpecificConfig::AOT_PS:
46  audio_type = FOURCC_zacp;
47  break;
48  default:
49  LOG(ERROR) << "Unknown object type for aac " << audio_object_type;
50  return false;
51  }
52  } break;
53  case kCodecAC3:
54  audio_type = FOURCC_zac3;
55  break;
56  case kCodecEAC3:
57  audio_type = FOURCC_zec3;
58  break;
59  default:
60  LOG(ERROR) << "Codec " << codec << " is not supported in encrypted TS.";
61  return false;
62  }
63 
64  DCHECK_NE(audio_type, FOURCC_NULL);
65  audio_setup_information->AppendInt(audio_type);
66  // Priming. Since no info from encoder, set it to 0x0000.
67  audio_setup_information->AppendInt(static_cast<uint16_t>(0x0000));
68  // Version is always 0x01.
69  audio_setup_information->AppendInt(static_cast<uint8_t>(0x01));
70 
71  // Size is one byte.
72  if (audio_specific_config_size > 0xFF) {
73  LOG(ERROR) << "Audio specific config should not be larger than one byte "
74  << audio_specific_config_size;
75  return false;
76  }
77  audio_setup_information->AppendInt(
78  static_cast<uint8_t>(audio_specific_config_size));
79 
80  audio_setup_information->AppendArray(audio_specific_config,
81  audio_specific_config_size);
82  return true;
83 }
84 
85 } // namespace media
86 } // namespace shaka
All the methods that are virtual are virtual for mocking.
Definition: crypto_flags.cc:66