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 "packager/media/base/buffer_writer.h"
10 #include "packager/media/base/fourccs.h"
11 #include "packager/media/codecs/aac_audio_specific_config.h"
12 
13 namespace shaka {
14 namespace media {
15 
16 bool WriteAudioSetupInformation(Codec codec,
17  const uint8_t* audio_specific_config,
18  size_t audio_specific_config_size,
19  BufferWriter* audio_setup_information) {
20  uint32_t audio_type = FOURCC_NULL;
21  switch (codec) {
22  case kCodecAAC: {
23  AACAudioSpecificConfig config;
24  const bool result = config.Parse(std::vector<uint8_t>(
25  audio_specific_config,
26  audio_specific_config + audio_specific_config_size));
27 
28  AACAudioSpecificConfig::AudioObjectType audio_object_type;
29  if (!result) {
30  LOG(WARNING) << "Failed to parse config. Assuming AAC-LC.";
31  audio_object_type = AACAudioSpecificConfig::AOT_AAC_LC;
32  } else {
33  audio_object_type = config.GetAudioObjectType();
34  }
35 
36  switch (audio_object_type) {
37  case AACAudioSpecificConfig::AOT_AAC_LC:
38  audio_type = FOURCC_zaac;
39  break;
40  case AACAudioSpecificConfig::AOT_SBR:
41  audio_type = FOURCC_zach;
42  break;
43  case AACAudioSpecificConfig::AOT_PS:
44  audio_type = FOURCC_zacp;
45  break;
46  default:
47  LOG(ERROR) << "Unknown object type for aac " << audio_object_type;
48  return false;
49  }
50  } break;
51  case kCodecAC3:
52  audio_type = FOURCC_zac3;
53  break;
54  case kCodecEAC3:
55  audio_type = FOURCC_zec3;
56  break;
57  default:
58  LOG(ERROR) << "Codec " << codec << " is not supported in encrypted TS.";
59  return false;
60  }
61 
62  DCHECK_NE(audio_type, FOURCC_NULL);
63  audio_setup_information->AppendInt(audio_type);
64  // Priming. Since no info from encoder, set it to 0x0000.
65  audio_setup_information->AppendInt(static_cast<uint16_t>(0x0000));
66  // Version is always 0x01.
67  audio_setup_information->AppendInt(static_cast<uint8_t>(0x01));
68 
69  // Size is one byte.
70  if (audio_specific_config_size > 0xFF) {
71  LOG(ERROR) << "Audio specific config should not be larger than one byte "
72  << audio_specific_config_size;
73  return false;
74  }
75  audio_setup_information->AppendInt(
76  static_cast<uint8_t>(audio_specific_config_size));
77 
78  audio_setup_information->AppendArray(audio_specific_config,
79  audio_specific_config_size);
80  return true;
81 }
82 
83 } // namespace media
84 } // namespace shaka
All the methods that are virtual are virtual for mocking.