├── LICENSE ├── README.MD ├── include ├── DashToHlsApi.h └── DashToHlsApiAVFramework.h └── library ├── DashToHls.gyp ├── DashToHlsTools.gyp ├── DashToHls_ios.pch ├── DashToHls_osx.pch ├── adts ├── adts_out.cc └── adts_out.h ├── bit_reader.cc ├── bit_reader.h ├── bit_reader_test.cc ├── box_contents_test.cc ├── box_type_test.cc ├── compatibility.h ├── dash ├── audio_sample_entry_contents.cc ├── audio_sample_entry_contents.h ├── avc1_contents.cc ├── avc1_contents.h ├── avc_decoder_configuration_record.cc ├── avc_decoder_configuration_record.h ├── avcc_contents.cc ├── avcc_contents.h ├── base_descriptor.cc ├── base_descriptor.h ├── box.cc ├── box.h ├── box_contents.cc ├── box_contents.h ├── box_contents_test.cc ├── box_test.cc ├── box_type.cc ├── box_type.h ├── box_type_test.cc ├── dash_parser.cc ├── dash_parser.h ├── dash_parser_test.cc ├── decoder_descriptor.cc ├── decoder_descriptor.h ├── elementary_stream_descriptor.cc ├── elementary_stream_descriptor.h ├── elst_contents.cc ├── elst_contents.h ├── esds_contents.cc ├── esds_contents.h ├── full_box_contents.cc ├── full_box_contents.h ├── mdat_contents.cc ├── mdat_contents.h ├── mdhd_contents.cc ├── mdhd_contents.h ├── mp4a_contents.cc ├── mp4a_contents.h ├── mvhd_contents.cc ├── mvhd_contents.h ├── pssh_contents.cc ├── pssh_contents.h ├── saio_contents.cc ├── saio_contents.h ├── saiz_contents.cc ├── saiz_contents.h ├── sample_entry_contents.cc ├── sample_entry_contents.h ├── sidx_contents.cc ├── sidx_contents.h ├── stsd_contents.cc ├── stsd_contents.h ├── stsz_contents.cc ├── stsz_contents.h ├── tenc_contents.cc ├── tenc_contents.h ├── tfdt_contents.cc ├── tfdt_contents.h ├── tfhd_contents.cc ├── tfhd_contents.h ├── trex_contents.cc ├── trex_contents.h ├── trun_contents.cc ├── trun_contents.h ├── unknown_contents.cc ├── unknown_contents.h ├── video_sample_entry_contents.cc └── video_sample_entry_contents.h ├── dash_to_hls_api.cc ├── dash_to_hls_api_avframework.h ├── dash_to_hls_api_avframework.mm ├── dash_to_hls_api_test.cc ├── dash_to_hls_default_diagnostic_callback.mm ├── dash_to_hls_session.h ├── mac_test_files.h ├── mac_test_files.mm ├── player ├── DashHTTPConnection.h ├── DashHTTPConnection.mm ├── OSX │ ├── AppDelegate.h │ ├── AppDelegate.mm │ ├── Base.lproj │ │ └── MainMenu.xib │ ├── en.lproj │ │ └── InfoPlist.strings │ ├── main.m │ └── player-Info.plist └── iOS │ ├── AppDelegate.h │ ├── AppDelegate.mm │ ├── Base.lproj │ ├── Main_iPad.storyboard │ └── Main_iPhone.storyboard │ ├── Images.xcassets │ ├── AppIcon.appiconset │ │ └── Contents.json │ └── LaunchImage.launchimage │ │ └── Contents.json │ ├── ViewController.h │ ├── ViewController.mm │ ├── en.lproj │ └── InfoPlist.strings │ ├── main.m │ ├── player-Info.plist │ └── player-Prefix.pch ├── ps ├── nalu.cc ├── nalu.h ├── nalu_test.cc ├── pes.cc ├── pes.h ├── pes_test.cc ├── program_stream_out.cc ├── program_stream_out.h ├── program_stream_out_test.cc ├── psm.cc ├── psm.h ├── psm_test.cc ├── system_header.cc ├── system_header.h └── system_header_test.cc ├── tools └── OSX │ ├── Base.lproj │ └── MainMenu.xib │ ├── ToolsAppDelegate.h │ ├── ToolsAppDelegate.mm │ ├── ToolsMpdParser.h │ ├── ToolsMpdParser.mm │ ├── en.lproj │ └── InfoPlist.strings │ ├── main.m │ └── tools-Info.plist ├── ts ├── transport_stream_out.cc ├── transport_stream_out.h └── transport_stream_out_test.cc ├── utilities.cc ├── utilities.h ├── utilities_gmock.h └── utilities_test.cc /README.MD: -------------------------------------------------------------------------------- 1 | # NO LONGER MAINTAINED BY GOOGLE 2 | 3 | See #19. We apologize for any inconvenience. 4 | 5 | ----- 6 | 7 | If you do not know H.264 then this code can be quite confusing. 8 | 9 | ### CONTRIBUTIONS 10 | 11 | All non-googlers must sign a Contributing License Agreement (CLA). 12 | 13 | ### CUSTOMIZING 14 | 15 | Common.gypi must be customized. 16 | library/mac_test_files.mm needs to be populated with your content. 17 | DashToHlsTools.gyp needs to add paths, read the comments in that file. 18 | 19 | 20 | ### H.264 QUICK TUTORIAL. 21 | 22 | H.264, or mpeg-4 is actually 15+ specs ranging from how to compress bits to 23 | how to package a video for transport. H.264 can also be placed in Mpeg-2 24 | Program Streams (PS) and Transport Streams (TS). Depending on the layer you 25 | care about you should look at the different ISO 14496 specs. Most of this 26 | code is either 14496-10 or 14496-12. 27 | 28 | Starting at the top Dash is in a 14496-12 container, which consists of boxes. 29 | Each box starts with a length followed by a 4 byte code. In this code all 30 | objects are named after their code. Any box may contain box specific data, 31 | subboxes, or a combination of the two. See 14496-12 for the exact definition 32 | of any box. 33 | 34 | All mpeg-4 files start with a ftyp box followed by a moov. The ftyp is used 35 | to verify the content can be played. The moov contains ALL the information 36 | needed to set up the codecs. 37 | 38 | For any file with mpeg-4 there are several ways to find the data. The DASH 39 | content this code is targetted to uses a sidx box to specify segments. Each 40 | segment is a moof and an mdat. The moof will have several important boxes in 41 | it to specify the Samples. 42 | 43 | Each Sample is a low level Mpeg-4 chunk of data made up of nalu, slices, and 44 | other nitty gritty details. See 14496-10 for a description of these. 45 | 46 | The basic process is to find the DASH samples, massage the nalus, set up 47 | Mpeg2 nalus for the codecs, then write out PS segments. Take the PS segments 48 | and pack them into TS segments. 49 | 50 | ### CLOCKS IN H.264 51 | 52 | There are four clock pointers in H.264 delivered over Transport Stream. 53 | System Clock Reference (SCR) 54 | Program Clock Reference (PCR) 55 | Decode Time Stamp (DTS) 56 | Program Time Stamp (PTS) 57 | 58 | The DTS and PTS are all in 90KHz clock units. So a value of 900,000 would be 59 | 10 seconds. The SCR and PCR are in 27MHz clock units, so 10 seconds would be 60 | 900,000 * 300 = 270,000,000. 61 | 62 | Each Sample must be delivered by either its SCR (PS) or PCR (TS). Once the 63 | packet is received it will be decoded by its DTS and put on the screen at 64 | its PTS. In H.264 packets are decoded out of order so while the DTS will go 65 | up a constant amount the PTS will jump all over the place. 66 | 67 | Some devices need an SCR/PCR before the DTS to give time for decoding after 68 | the packets are guaranteed to arrive. This is called buffer time. 69 | While this library allows a buffer time it is expected to have a 0 buffer 70 | time (SCR == DTS). 71 | -------------------------------------------------------------------------------- /include/DashToHlsApiAVFramework.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2014 Google Inc. All rights reserved. 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | /* 17 | iOS and OSX require extra care to make sure that only the AVPlayer in the 18 | current application has access to the content in the clear. To solve that 19 | we use the resource loader to serve a random key. The key use of a key 20 | requires a random number function passed in DashToHls_InitializeEncryption. 21 | 22 | As reencryption is assuming there is a CDM the format of the function matches 23 | the built in OEMCrypto_GetRandom. 24 | 25 | When building any m3u8 manifests GetKeyUrl returns the m3u8 text that must be 26 | placed in the m3u8 as is. It has the full line of text describing the key 27 | and url with IV encoded. 28 | 29 | Finally DashToHls_SetAVURLAsset needs to be called on all AVURLAssets need to 30 | set up the AVResourceLoaderDelegate. An optional 31 | AVAssetResourceLoaderDelegate can be passed in as |assetDelegate|. This will 32 | be called on all AVAssetResourceLoaderDelegate not handled by the UDT. 33 | */ 34 | 35 | #ifndef _UDT_DASH_TRANSMUXER_DASHTOHLSAPIAVFRAMEWORK_H_ 36 | #define _UDT_DASH_TRANSMUXER_DASHTOHLSAPIAVFRAMEWORK_H_ 37 | 38 | #import 39 | 40 | #if OEMCRYPTO_DYLIB 41 | #include "DashToHlsApi.h" 42 | #else 43 | #include "include/DashToHlsApi.h" 44 | #endif // OEMCRYPTO_DYLIB 45 | 46 | #ifdef __cplusplus 47 | extern "C" { 48 | #endif // __cplusplus 49 | 50 | typedef void (*CENC_Random)(uint8_t* random_data, size_t data_length); 51 | 52 | void DashToHls_InitializeEncryption(CENC_Random random_function); 53 | 54 | DashToHlsStatus DashToHls_SetAVURLAsset(AVURLAsset* asset, 55 | id assetDelegate, 56 | dispatch_queue_t queue); 57 | NSString* GetKeyUrl(struct DashToHlsSession* session); 58 | 59 | #ifdef __cplusplus 60 | }; 61 | #endif // __cplusplus 62 | #endif // _UDT_DASH_TRANSMUXER_DASHTOHLSAPIAVFRAMEWORK_H_ 63 | -------------------------------------------------------------------------------- /library/DashToHls.gyp: -------------------------------------------------------------------------------- 1 | # Library targets for DashToHls. These minimize external dependencies (such 2 | # as OpenSSL) so other projects can depend on them without bringing in 3 | # unnecessary dependencies. 4 | { 5 | 'target_defaults': { 6 | 'xcode_settings': { 7 | 'CLANG_ENABLE_OBJC_ARC': [ 'YES' ], 8 | }, 9 | }, 10 | 'targets': [ 11 | { 12 | 'target_name': 'DashToHlsLibrary', 13 | 'type': 'static_library', 14 | 'xcode_settings': { 15 | 'GCC_PREFIX_HEADER': 'DashToHls_osx.pch', 16 | 'CLANG_CXX_LIBRARY': 'libc++', 17 | }, 18 | 'direct_dependent_settings': { 19 | 'include_dirs': [ 20 | '../include', 21 | '..', 22 | ] 23 | }, 24 | 'include_dirs': [ 25 | '..', 26 | ], 27 | 'conditions': [ 28 | ['OS=="mac"', { 29 | 'defines': [ 30 | 'USE_AVFRAMEWORK', 31 | ], 32 | 'sources': [ 33 | 'dash_to_hls_api_avframework.h', 34 | 'dash_to_hls_api_avframework.mm', 35 | ], 36 | }], 37 | ['OS=="ios"', { 38 | 'defines': [ 39 | 'USE_AVFRAMEWORK', 40 | ], 41 | 'sources': [ 42 | 'dash_to_hls_api_avframework.h', 43 | 'dash_to_hls_api_avframework.mm', 44 | ], 45 | }], 46 | ], 47 | 'sources': [ 48 | '../include/DashToHlsApi.h', 49 | '../include/DashToHlsApiAVFramework.h', 50 | 'dash_to_hls_api.cc', 51 | 'dash_to_hls_session.h', 52 | 'utilities.cc', 53 | 'utilities.h', 54 | ], 55 | 'dependencies': [ 56 | 'DashToHlsDash', 57 | 'DashToHlsDefaultDiagnosticCallback', 58 | 'DashToHlsPs', 59 | 'DashToHlsTs', 60 | ], 61 | }, 62 | { 63 | 'target_name': 'DashToHlsDash', 64 | 'type': 'static_library', 65 | 'xcode_settings': { 66 | 'GCC_PREFIX_HEADER': 'DashToHls_osx.pch', 67 | 'CLANG_CXX_LIBRARY': 'libc++', 68 | }, 69 | 'include_dirs': [ 70 | '..', 71 | ], 72 | 'direct_dependent_settings': { 73 | 'include_dirs': [ 74 | '..', 75 | ], 76 | }, 77 | 'sources': [ 78 | 'bit_reader.cc', 79 | 'bit_reader.h', 80 | 'utilities.h', 81 | 'utilities.cc', 82 | ' 19 | #import 20 | #endif 21 | -------------------------------------------------------------------------------- /library/DashToHls_osx.pch: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2014 Google Inc. All rights reserved. 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | #ifdef __OBJC__ 18 | #import 19 | #endif 20 | -------------------------------------------------------------------------------- /library/adts/adts_out.cc: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2014 Google Inc. All rights reserved. 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | #include "library/adts/adts_out.h" 18 | 19 | #include "library/utilities.h" 20 | 21 | namespace { 22 | const size_t kAudioFrameHeaderSize = 7; 23 | const size_t kMaxAudioFrameLength = 8191; 24 | } // namespace 25 | 26 | namespace dash2hls { 27 | // The Pat and Pmt for TS are always the same. There is no need to calculate 28 | // them. 29 | 30 | const uint8_t AdtsOut::kID3AudioTimeTag[73] = { 31 | 0x49, 0x44, 0x33, 0x03, 0x00, 0x00, 0x00, 0x00, 32 | 0x00, 0x3f, 0x50, 0x52, 0x49, 0x56, 0x00, 0x00, 33 | 0x00, 0x35, 0x00, 0x00, 0x63, 0x6f, 0x6d, 0x2e, 34 | 0x61, 0x70, 0x70, 0x6c, 0x65, 0x2e, 0x73, 0x74, 35 | 0x72, 0x65, 0x61, 0x6d, 0x69, 0x6e, 0x67, 0x2e, 36 | 0x74, 0x72, 0x61, 0x6e, 0x73, 0x70, 0x6f, 0x72, 37 | 0x74, 0x53, 0x74, 0x72, 0x65, 0x61, 0x6d, 0x54, 38 | 0x69, 0x6d, 0x65, 0x73, 0x74, 0x61, 0x6d, 0x70, 39 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 40 | 0x00 41 | }; 42 | const size_t kID3AudioTimeTagTimeOffsetFromEnd = 8; 43 | 44 | void AdtsOut::AddTimestamp(uint64_t pts, std::vector* out) { 45 | size_t out_size = out->size(); 46 | out->resize(out_size + sizeof(kID3AudioTimeTag)); 47 | memcpy(&(*out)[out_size], kID3AudioTimeTag, sizeof(kID3AudioTimeTag)); 48 | htonllToBuffer(pts, 49 | &(*out)[out->size() - kID3AudioTimeTagTimeOffsetFromEnd]); 50 | } 51 | 52 | // TODO(justsomeguy) Code copied from the Widevine tree has quite a few 53 | // magic numbers. This is because it builds up the header by adding bits 54 | // and shifting, adding bits and shifting. I want to rewrite this to build 55 | // in place and use constants. 56 | void AdtsOut::ProcessSample(const uint8_t* input, size_t input_length, 57 | std::vector* out) { 58 | size_t frame_size = input_length + kAudioFrameHeaderSize; 59 | if (frame_size > kMaxAudioFrameLength) { 60 | DASH_LOG("Bad audio sample", 61 | "Audio frames larger than 8191 bytes not supported.", 62 | DumpMemory(input, input_length).c_str()); 63 | return; 64 | } 65 | out->resize(frame_size); 66 | uint64_t adts_header = 0xfff; 67 | adts_header <<= 4; 68 | adts_header |= 0x01; 69 | adts_header <<= 2; 70 | adts_header |= (audio_object_type_ - 1); 71 | adts_header <<= 4; 72 | adts_header |= sampling_frequency_index_; 73 | adts_header <<= 4; 74 | adts_header |= channel_config_; 75 | adts_header <<= 4; 76 | adts_header |= 0x0c; 77 | adts_header <<= 13; 78 | adts_header |= frame_size; 79 | adts_header <<= 11; 80 | adts_header |= 0x7ff; 81 | adts_header <<=2; 82 | adts_header <<= 8; 83 | htonllToBuffer(adts_header, &(*out)[0]); 84 | memcpy(&(*out)[kAudioFrameHeaderSize], input, input_length); 85 | } 86 | } // namespace dash2hls 87 | -------------------------------------------------------------------------------- /library/adts/adts_out.h: -------------------------------------------------------------------------------- 1 | #ifndef _DASH2HLS_ADTS_OUT_H_ 2 | #define _DASH2HLS_ADTS_OUT_H_ 3 | 4 | /* 5 | Copyright 2014 Google Inc. All rights reserved. 6 | 7 | Licensed under the Apache License, Version 2.0 (the "License"); 8 | you may not use this file except in compliance with the License. 9 | You may obtain a copy of the License at 10 | 11 | http://www.apache.org/licenses/LICENSE-2.0 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" BASIS, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | See the License for the specific language governing permissions and 17 | limitations under the License. 18 | */ 19 | 20 | // AdtsOut takes mp4 samples and turns them into ADTS samples. 21 | // 22 | // Expected usage is to call AdtsOut::ProcessSample. 23 | // All other routines are exposed for unit testing. 24 | 25 | #include 26 | 27 | #include "include/DashToHlsApi.h" 28 | #include "library/dash/box_type.h" 29 | #include "library/dash/full_box_contents.h" 30 | #include "library/ps/nalu.h" 31 | #include "library/ps/pes.h" 32 | 33 | namespace dash2hls { 34 | 35 | class AdtsOut { 36 | public: 37 | void AddTimestamp(uint64_t pts, std::vector* out); 38 | // TODO(justsomeguy) this interface requires an extra copy of input because 39 | // it's const. See about doing it in place. 40 | void ProcessSample(const uint8_t* input, size_t input_length, 41 | std::vector* out); 42 | 43 | void set_audio_object_type(uint8_t type) {audio_object_type_ = type;} 44 | void set_channel_config(uint8_t config) {channel_config_ = config;} 45 | void set_sampling_frequency_index(uint8_t index) { 46 | sampling_frequency_index_ = index; 47 | } 48 | 49 | protected: 50 | static const uint8_t kID3AudioTimeTag[73]; 51 | 52 | private: 53 | uint8_t audio_object_type_; 54 | uint8_t sampling_frequency_index_; 55 | uint8_t channel_config_; 56 | }; 57 | } // namespace dash2hls 58 | 59 | #endif // _DASH2HLS_ADTS_OUT_H_ 60 | -------------------------------------------------------------------------------- /library/bit_reader.cc: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2014 Google Inc. All rights reserved. 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | #include "bit_reader.h" 18 | 19 | #include "utilities.h" 20 | 21 | namespace { 22 | uint8_t GetMask(uint8_t size) { 23 | return (1 << size) - 1; 24 | } 25 | } // namespace 26 | 27 | namespace dash2hls { 28 | 29 | BitReader::BitReader(const uint8_t* bits, size_t length): bits_(bits), 30 | length_(length), bit_position_(0), byte_position_(0) { 31 | } 32 | 33 | bool BitReader::Read(size_t bits_to_read, uint8_t* value) { 34 | if (bits_to_read > sizeof(*value) * 8) { 35 | return false; 36 | } 37 | if (bits_to_read + bit_position_ + (byte_position_ * 8) > length_ * 8) { 38 | return false; 39 | } 40 | 41 | if (bits_to_read + bit_position_ < 8) { 42 | *value = (bits_[byte_position_] >> (8 - bit_position_ - bits_to_read)) 43 | & GetMask(bits_to_read); 44 | bit_position_ += bits_to_read; 45 | return true; 46 | } 47 | size_t msb_to_read = 8 - bit_position_; 48 | size_t lsb_to_read = bits_to_read - msb_to_read; 49 | *value = ((bits_[byte_position_] & GetMask(msb_to_read)) << 50 | (bits_to_read - msb_to_read)) | 51 | ((bits_[byte_position_ + 1] >> (8 - lsb_to_read)) 52 | & GetMask(lsb_to_read)); 53 | bit_position_ = lsb_to_read; 54 | ++byte_position_; 55 | return true; 56 | } 57 | 58 | bool BitReader::Read(size_t bits_to_read, uint16_t* value) { 59 | return ReadInternal(bits_to_read, value); 60 | } 61 | 62 | bool BitReader::Read(size_t bits_to_read, uint32_t* value) { 63 | return ReadInternal(bits_to_read, value); 64 | } 65 | 66 | bool BitReader::Read(size_t bits_to_read, uint64_t* value) { 67 | return ReadInternal(bits_to_read, value); 68 | } 69 | 70 | template bool BitReader::ReadInternal(size_t bits_to_read, 71 | T* value) { 72 | if (bits_to_read > sizeof(*value) * 8) { 73 | return false; 74 | } 75 | if (bits_to_read + bit_position_ + (byte_position_ * 8) > length_ * 8) { 76 | return false; 77 | } 78 | 79 | T result = 0; 80 | while (bits_to_read > 8) { 81 | result <<= 8; 82 | uint8_t next_byte = 0; 83 | if (!Read(8, &next_byte)) { 84 | return false; 85 | } 86 | result += next_byte; 87 | bits_to_read -= 8; 88 | } 89 | result <<= bits_to_read; 90 | uint8_t next_byte = 0; 91 | if (!Read(bits_to_read, &next_byte)) { 92 | return false; 93 | } 94 | result += next_byte; 95 | *value = result; 96 | return true; 97 | } 98 | } // namespace dash2hls 99 | -------------------------------------------------------------------------------- /library/bit_reader.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2014 Google Inc. All rights reserved. 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | // Light weight bit reader. Does not keep a copy or memory nor is it thread 18 | // safe. Meant strictly for the simple cases of a routine needing to parse 19 | // a single dash box. Trying to use it generically or in complicated 20 | // situations is unwise. 21 | // 22 | // Read methods check for overflows and return false if there are not enough 23 | // bits in the buffer or bits_to_read is greater than the size of value. 24 | // 25 | // EXAMPLE: 26 | // This will read 1 bit into the byte_value then the next 12 bits into 27 | // the short_value. If aVector does not have enough bits then it will call 28 | // Error. 29 | // 30 | // vector aVector; 31 | // uint8_t byte_value = 0; 32 | // uint16_t short_value = 0; 33 | // BitReader bit_reader(&aVector[0], aVector.size()); 34 | // if (!bit_reader.read(1, &byte_value)) { 35 | // Error("Could not read byte_value"); 36 | // } 37 | // if (!bit_reader.read(12, &short_value)) { 38 | // Error("Could not read short_value"); 39 | // } 40 | 41 | #ifndef DASHTOHLS_BIT_READER_H_ 42 | #define DASHTOHLS_BIT_READER_H_ 43 | 44 | #include 45 | #include 46 | 47 | namespace dash2hls { 48 | class BitReader { 49 | public: 50 | BitReader(const uint8_t* bits, size_t length); 51 | bool Read(size_t bits_to_read, uint8_t* value); 52 | bool Read(size_t bits_to_read, uint16_t* value); 53 | bool Read(size_t bits_to_read, uint32_t* value); 54 | bool Read(size_t bits_to_read, uint64_t* value); 55 | private: 56 | template bool ReadInternal(size_t bits_to_read, T* value); 57 | 58 | const uint8_t* bits_; 59 | size_t length_; 60 | size_t bit_position_; 61 | size_t byte_position_; 62 | }; // class BitReader 63 | } // namespace dash2hls 64 | #endif // DASHTOHLS_BIT_READER_H_ 65 | -------------------------------------------------------------------------------- /library/bit_reader_test.cc: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2014 Google Inc. All rights reserved. 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | #include "bit_reader.h" 18 | 19 | #include 20 | 21 | namespace { 22 | // binary value of s_bytes: 0b1010101011111111110011001101110111101110 23 | const uint8_t s_bytes[] = {0xaa, 0xff, 0xcc, 0xdd, 0xee}; 24 | const uint8_t s_bytes64[] = {0xaa, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}; 25 | } // namespace 26 | 27 | namespace dash2hls { 28 | 29 | 30 | // Each test below uses a pattern of bits to verify overflow and crossing byte 31 | // boundaries works. The choices are arbitrary as long as the following cases 32 | // are checked: 33 | // Overflow size of value. 34 | // Read off the end of the buffer. 35 | // Read sizeof(value) when aligned. 36 | // Read sizeof(value) when misaligned. 37 | // Read less than the sizeof(value). 38 | 39 | TEST(Dash2HLS, BitRead8) { 40 | BitReader reader(s_bytes, sizeof(s_bytes)); 41 | uint8_t value; 42 | EXPECT_FALSE(reader.Read(9, &value)); 43 | EXPECT_TRUE(reader.Read(1, &value)); 44 | EXPECT_EQ(1, value); 45 | EXPECT_TRUE(reader.Read(1, &value)); 46 | EXPECT_EQ(0, value); 47 | EXPECT_TRUE(reader.Read(8, &value)); 48 | EXPECT_EQ(0b10101011, value); 49 | EXPECT_TRUE(reader.Read(7, &value)); 50 | EXPECT_EQ(0b1111111, value); 51 | EXPECT_TRUE(reader.Read(7, &value)); 52 | EXPECT_EQ(0b1001100, value); 53 | EXPECT_TRUE(reader.Read(7, &value)); 54 | EXPECT_EQ(0b1101110, value); 55 | EXPECT_TRUE(reader.Read(7, &value)); 56 | EXPECT_EQ(0b1111011, value); 57 | EXPECT_FALSE(reader.Read(7, &value)); 58 | } 59 | 60 | TEST(Dash2HLS, BitRead8_Exact) { 61 | BitReader reader(s_bytes, sizeof(s_bytes)); 62 | uint8_t value; 63 | EXPECT_TRUE(reader.Read(8, &value)); 64 | EXPECT_TRUE(reader.Read(8, &value)); 65 | EXPECT_TRUE(reader.Read(8, &value)); 66 | EXPECT_TRUE(reader.Read(8, &value)); 67 | EXPECT_TRUE(reader.Read(8, &value)); 68 | EXPECT_FALSE(reader.Read(8, &value)); 69 | } 70 | 71 | TEST(Dash2HLS, BitRead16) { 72 | BitReader reader(s_bytes, sizeof(s_bytes)); 73 | uint16_t value; 74 | EXPECT_FALSE(reader.Read(17, &value)); 75 | EXPECT_TRUE(reader.Read(1, &value)); 76 | EXPECT_EQ(1, value); 77 | EXPECT_TRUE(reader.Read(1, &value)); 78 | EXPECT_EQ(0, value); 79 | EXPECT_TRUE(reader.Read(8, &value)); 80 | EXPECT_EQ(0b10101011, value); 81 | EXPECT_TRUE(reader.Read(13, &value)); 82 | EXPECT_EQ(0b1111111100110, value); 83 | EXPECT_FALSE(reader.Read(32, &value)); 84 | } 85 | 86 | TEST(Dash2HLS, BitRead32) { 87 | BitReader reader(s_bytes, sizeof(s_bytes)); 88 | uint32_t value; 89 | EXPECT_FALSE(reader.Read(33, &value)); 90 | EXPECT_TRUE(reader.Read(1, &value)); 91 | EXPECT_EQ(0b1, value); 92 | EXPECT_TRUE(reader.Read(1, &value)); 93 | EXPECT_EQ(0, value); 94 | EXPECT_TRUE(reader.Read(8, &value)); 95 | EXPECT_EQ(0b10101011, value); 96 | EXPECT_TRUE(reader.Read(17, &value)); 97 | EXPECT_EQ(0b11111111001100110, value); 98 | EXPECT_FALSE(reader.Read(32, &value)); 99 | } 100 | 101 | TEST(Dash2HLS, BitRead64) { 102 | BitReader reader(s_bytes64, sizeof(s_bytes64)); 103 | uint64_t value; 104 | EXPECT_FALSE(reader.Read(65, &value)); 105 | EXPECT_TRUE(reader.Read(1, &value)); 106 | EXPECT_EQ(1, value); 107 | EXPECT_TRUE(reader.Read(1, &value)); 108 | EXPECT_EQ(0, value); 109 | EXPECT_TRUE(reader.Read(62, &value)); 110 | // EXPECT_EQ throws pukes on 64 bit constants. 111 | EXPECT_TRUE(0x2affffffffffffff == value); 112 | EXPECT_FALSE(reader.Read(1, &value)); 113 | } 114 | } // namespace dash2hls 115 | -------------------------------------------------------------------------------- /library/box_contents_test.cc: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2014 Google Inc. All rights reserved. 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | #include 18 | 19 | #include "box_contents.h" 20 | 21 | TEST(Dash2Hls, BoxContents) { 22 | // TODO(justsomeguy) placeholder for now. 23 | } 24 | -------------------------------------------------------------------------------- /library/box_type_test.cc: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2014 Google Inc. All rights reserved. 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | #include 18 | 19 | #include "box_type.h" 20 | #include "utilities.h" 21 | 22 | namespace dash2hls { 23 | 24 | TEST(DashToHls, BoxType) { 25 | uint8_t vmhd[4]; 26 | memcpy(vmhd, "vmhd", sizeof(vmhd)); 27 | BoxType box_type_1; 28 | box_type_1.set_type(BoxType::kBox_vmhd); 29 | EXPECT_EQ(std::string("vmhd"), box_type_1.PrettyPrint("")); 30 | EXPECT_EQ(BoxType::kBox_vmhd, box_type_1.asUint32()); 31 | EXPECT_EQ(ntohl(BoxType::kBox_vmhd), box_type_1.asBoxType()); 32 | 33 | BoxType box_type_2; 34 | box_type_2.set_type(vmhd); 35 | EXPECT_EQ(std::string("vmhd"), box_type_1.PrettyPrint("")); 36 | EXPECT_EQ(BoxType::kBox_vmhd, box_type_1.asUint32()); 37 | EXPECT_EQ(ntohl(BoxType::kBox_vmhd), box_type_1.asBoxType()); 38 | } 39 | } // namespace dash2hls 40 | -------------------------------------------------------------------------------- /library/compatibility.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2014 Google Inc. All rights reserved. 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | #ifndef DASHTOHLS_COMPATIBILITY_H_ 18 | #define DASHTOHLS_COMPATIBILITY_H_ 19 | 20 | #if C98 21 | #define nullptr 0 22 | #endif // C98 23 | 24 | #ifdef SHARED_PTR_INCLUDE 25 | #include SHARED_PTR_INCLUDE 26 | #endif // SHARED_PTR_INCLUDE 27 | 28 | namespace dash2hls { 29 | #if C98 30 | #ifdef SHARED_PTR_DEFINE 31 | SHARED_PTR_DEFINE 32 | #else // SHARED_PTR_DEFINE 33 | #error Define SHARED_PTR_DEFINE 34 | #error Examples: 35 | #error -DSHARED_PTR_INCLUDE='\#include ;' 36 | #error -DSHARED_PTR_DEFINE='using boost::shared_ptr;' 37 | #endif // SHARED_PTR_DEFINE 38 | #else // C98 39 | using std::shared_ptr; 40 | #endif // C98 41 | } // namespace dash2hls 42 | 43 | #endif // DASHTOHLS_COMPATIBILITY_H_ 44 | -------------------------------------------------------------------------------- /library/dash/audio_sample_entry_contents.cc: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2014 Google Inc. All rights reserved. 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | #include "library/dash/audio_sample_entry_contents.h" 18 | 19 | #include "library/dash/box.h" 20 | #include "library/dash/dash_parser.h" 21 | #include "library/utilities.h" 22 | 23 | namespace { 24 | const size_t kMinBoxSize = 7 * sizeof(uint32_t); 25 | } // namespace 26 | 27 | namespace dash2hls { 28 | 29 | // See ISO 14496-15 and 14495-12 for details. 30 | // 31 | // class AudioSampleEntry(codingname) extends SampleEntry (codingname){ 32 | // const unsigned int(32)[2] reserved = 0; 33 | // template unsigned int(16) channelcount = 2; 34 | // template unsigned int(16) samplesize = 16; 35 | // unsigned int(16) pre_defined = 0; 36 | // const unsigned int(16) reserved = 0 ; 37 | // template unsigned int(32) samplerate = { default samplerate of media}<<16; 38 | // } 39 | size_t AudioSampleEntryContents::Parse(const uint8_t *buffer, size_t length) { 40 | const uint8_t* volatile ptr = 41 | buffer + SampleEntryContents::Parse(buffer, length); 42 | if ((ptr == buffer) || 43 | !EnoughBytesToParse(ptr - buffer, kMinBoxSize, length)) { 44 | DASH_LOG((BoxName() + " too short").c_str(), 45 | "At least 28 bytes are required", 46 | DumpMemory(buffer, length).c_str()); 47 | return DashParser::kParseFailure; 48 | } 49 | // Reserved bytes to skip 50 | ptr += sizeof(uint32_t) * 2; 51 | channel_count_ = ntohsFromBuffer(ptr); 52 | ptr += sizeof(channel_count_); 53 | sample_size_ = ntohsFromBuffer(ptr); 54 | ptr += sizeof(sample_size_); 55 | // Skip pre_defined and reserved. 56 | ptr += sizeof(uint16_t) * 2; 57 | sample_rate_ = ntohlFromBuffer(ptr); 58 | ptr += sizeof(sample_rate_); 59 | 60 | size_t bytes_left = length - (ptr - buffer); 61 | if (bytes_left > 0) { 62 | dash_parser_ = new DashParser; 63 | dash_parser_->set_current_position(stream_position_ + bytes_left); 64 | size_t bytes_parsed = dash_parser_->Parse(ptr, bytes_left); 65 | if (bytes_parsed != bytes_left) { 66 | return DashParser::kParseFailure; 67 | } 68 | ptr += bytes_parsed; 69 | } 70 | 71 | return ptr - buffer; 72 | } 73 | 74 | std::string AudioSampleEntryContents::PrettyPrint(std::string indent) const { 75 | std::string result = " Channels: " + PrettyPrintValue(channel_count_); 76 | result += " Sample Size: " + PrettyPrintValue(sample_size_); 77 | result += " Sample Rate: " + PrettyPrintValue(sample_rate_); 78 | result += SampleEntryContents::PrettyPrint(indent); 79 | return result; 80 | } 81 | } // namespace dash2hls 82 | -------------------------------------------------------------------------------- /library/dash/audio_sample_entry_contents.h: -------------------------------------------------------------------------------- 1 | #ifndef _DASH2HLS_AUDIO_SAMPLE_ENTRY_CONTENTS_H_ 2 | #define _DASH2HLS_AUDIO_SAMPLE_ENTRY_CONTENTS_H_ 3 | 4 | /* 5 | Copyright 2014 Google Inc. All rights reserved. 6 | 7 | Licensed under the Apache License, Version 2.0 (the "License"); 8 | you may not use this file except in compliance with the License. 9 | You may obtain a copy of the License at 10 | 11 | http://www.apache.org/licenses/LICENSE-2.0 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" BASIS, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | See the License for the specific language governing permissions and 17 | limitations under the License. 18 | */ 19 | 20 | // Sample box containing the codec information. 21 | 22 | #include 23 | 24 | #include "library/dash/elementary_stream_descriptor.h" 25 | #include "library/dash/sample_entry_contents.h" 26 | 27 | namespace dash2hls { 28 | 29 | class AudioSampleEntryContents : public SampleEntryContents { 30 | public: 31 | AudioSampleEntryContents(uint32_t type, uint64_t stream_position) 32 | : SampleEntryContents(type, stream_position) {} 33 | virtual std::string PrettyPrint(std::string indent) const; 34 | virtual std::string BoxName() const {return "AudioSampleEntry";} 35 | 36 | uint16_t get_channel_count() const { 37 | return channel_count_; 38 | } 39 | 40 | uint16_t get_sample_size() const { 41 | return sample_size_; 42 | } 43 | 44 | uint32_t get_sample_rate() const { 45 | return sample_rate_ >> 16; 46 | } 47 | 48 | protected: 49 | virtual size_t Parse(const uint8_t* buffer, size_t length); 50 | 51 | private: 52 | uint16_t channel_count_; 53 | uint16_t sample_size_; 54 | uint32_t sample_rate_; 55 | }; 56 | } // namespace dash2hls 57 | 58 | #endif // _DASH2HLS_AUDIO_SAMPLE_CONTENTS_H_ 59 | -------------------------------------------------------------------------------- /library/dash/avc1_contents.cc: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2014 Google Inc. All rights reserved. 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | #include "library/dash/avc1_contents.h" 18 | 19 | #include "library/dash/box.h" 20 | #include "library/utilities.h" 21 | 22 | namespace dash2hls { 23 | 24 | // See ISO 14496-15 and 14495-12 for details. 25 | // 26 | // class AVCSampleEntry() extends VisualSampleEntry (‘avc1’){ 27 | // AVCConfigurationBox config; 28 | // MPEG4BitRateBox (); // optional 29 | // MPEG4ExtensionDescriptorsBox (); // optional 30 | // } 31 | size_t Avc1Contents::Parse(const uint8_t *buffer, size_t length) { 32 | const uint8_t* ptr = buffer + 33 | VideoSampleEntryContents::Parse(buffer, length); 34 | return ptr - buffer; 35 | } 36 | 37 | std::string Avc1Contents::PrettyPrint(std::string indent) const { 38 | std::string result = VideoSampleEntryContents::PrettyPrint(indent); 39 | return result; 40 | } 41 | } // namespace dash2hls 42 | -------------------------------------------------------------------------------- /library/dash/avc1_contents.h: -------------------------------------------------------------------------------- 1 | #ifndef _DASH2HLS_AVC1_CONTENTS_H_ 2 | #define _DASH2HLS_AVC1_CONTENTS_H_ 3 | 4 | /* 5 | Copyright 2014 Google Inc. All rights reserved. 6 | 7 | Licensed under the Apache License, Version 2.0 (the "License"); 8 | you may not use this file except in compliance with the License. 9 | You may obtain a copy of the License at 10 | 11 | http://www.apache.org/licenses/LICENSE-2.0 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" BASIS, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | See the License for the specific language governing permissions and 17 | limitations under the License. 18 | */ 19 | 20 | // Sample box containing the codec information. 21 | 22 | #include 23 | 24 | #include "library/dash/box_type.h" 25 | #include "library/dash/video_sample_entry_contents.h" 26 | 27 | namespace dash2hls { 28 | 29 | class Avc1Contents : public VideoSampleEntryContents { 30 | public: 31 | explicit Avc1Contents(uint64_t stream_position) 32 | : VideoSampleEntryContents(BoxType::kBox_avc1, stream_position) {} 33 | virtual std::string PrettyPrint(std::string indent) const; 34 | virtual std::string BoxName() const {return "AVC1 Decode Information";} 35 | 36 | protected: 37 | virtual size_t Parse(const uint8_t* buffer, size_t length); 38 | 39 | private: 40 | }; 41 | } // namespace dash2hls 42 | 43 | #endif // _DASH2HLS_AVC1_CONTENTS_H_ 44 | -------------------------------------------------------------------------------- /library/dash/avc_decoder_configuration_record.h: -------------------------------------------------------------------------------- 1 | // Video codec information. Comes from either an mp4v or avcC box. With Dash 2 | // only the avcC box is ever seen. 3 | // HLS only cares about the sps, pps, and nalu length. The rest of the 4 | // fields are parsed to help with diagnostics. 5 | #ifndef _DASH2HLS_AVC_DECODER_CONFIGURATION_RECORD_H_ 6 | #define _DASH2HLS_AVC_DECODER_CONFIGURATION_RECORD_H_ 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | namespace dash2hls { 13 | class AvcDecoderConfigurationRecord { 14 | public: 15 | const std::vector >& 16 | get_sequence_parameter_sets() const {return sequence_parameter_sets_;} 17 | const std::vector >& 18 | get_picture_parameter_sets() const {return picture_parameter_sets_;} 19 | size_t GetNaluLength() const {return length_size_minus_one_ + 1;} 20 | virtual size_t Parse(const uint8_t* buffer, size_t length); 21 | virtual std::string PrettyPrint(std::string indent) const; 22 | 23 | private: 24 | uint8_t version_; 25 | uint8_t avc_profile_indication_; 26 | uint8_t profile_compatibility_; 27 | uint8_t avc_level_indication_; 28 | uint8_t length_size_minus_one_; 29 | size_t sequence_parameter_sets_count_; 30 | std::vector > sequence_parameter_sets_; 31 | size_t picture_parameter_sets_count_; 32 | std::vector > picture_parameter_sets_; 33 | }; 34 | } // namespace dash2hls 35 | #endif // _DASH2HLS_AVC_DECODER_CONFIGURATION_RECORD_H_ 36 | -------------------------------------------------------------------------------- /library/dash/avcc_contents.cc: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2014 Google Inc. All rights reserved. 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | #include "library/dash/avcc_contents.h" 18 | #include "library/dash/avc_decoder_configuration_record.h" 19 | 20 | #include "library/utilities.h" 21 | 22 | namespace dash2hls { 23 | 24 | // See ISO 14496-12 for details. 25 | // 26 | // aligned(8) class AVCDecoderConfigurationRecord { 27 | // unsigned int(8) configurationVersion = 1; 28 | // unsigned int(8) AVCProfileIndication; 29 | // unsigned int(8) profile_compatibility; 30 | // unsigned int(8) AVCLevelIndication; 31 | // bit(6) reserved = ‘111111’b; 32 | // unsigned int(2) lengthSizeMinusOne; 33 | // bit(3) reserved = ‘111’b; 34 | // unsigned int(5) numOfSequenceParameterSets; 35 | // for (i=0; i< numOfSequenceParameterSets; i++) { 36 | // unsigned int(16) sequenceParameterSetLength ; 37 | // bit(8*sequenceParameterSetLength) sequenceParameterSetNALUnit; 38 | // } 39 | // unsigned int(8) numOfPictureParameterSets; 40 | // for (i=0; i< numOfPictureParameterSets; i++) { 41 | // unsigned int(16) pictureParameterSetLength; 42 | // bit(8*pictureParameterSetLength) pictureParameterSetNALUnit; 43 | // } 44 | // } 45 | size_t AvcCContents::Parse(const uint8_t *buffer, size_t length) { 46 | return avc_record_.Parse(buffer, length); 47 | } 48 | 49 | std::string AvcCContents::PrettyPrint(std::string indent) const { 50 | std::string result = BoxContents::PrettyPrint(indent); 51 | result += avc_record_.PrettyPrint(indent); 52 | return result; 53 | } 54 | } // namespace dash2hls 55 | -------------------------------------------------------------------------------- /library/dash/avcc_contents.h: -------------------------------------------------------------------------------- 1 | #ifndef _DASH2HLS_DASH_AVCC_CONTENTS_H_ 2 | #define _DASH2HLS_DASH_AVCC_CONTENTS_H_ 3 | 4 | /* 5 | Copyright 2014 Google Inc. All rights reserved. 6 | 7 | Licensed under the Apache License, Version 2.0 (the "License"); 8 | you may not use this file except in compliance with the License. 9 | You may obtain a copy of the License at 10 | 11 | http://www.apache.org/licenses/LICENSE-2.0 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" BASIS, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | See the License for the specific language governing permissions and 17 | limitations under the License. 18 | */ 19 | 20 | // Sample box containing the AVC decoder information. 21 | 22 | #include 23 | #include 24 | 25 | #include "library/dash/avc_decoder_configuration_record.h" 26 | #include "library/dash/box_contents.h" 27 | #include "library/dash/box_type.h" 28 | 29 | namespace dash2hls { 30 | 31 | class AvcCContents : public BoxContents { 32 | public: 33 | explicit AvcCContents(uint64_t stream_position) 34 | : BoxContents(BoxType::kBox_avcC, stream_position) {} 35 | const std::vector >& 36 | get_sequence_parameter_sets() const { 37 | return avc_record_.get_sequence_parameter_sets(); 38 | } 39 | const std::vector >& 40 | get_picture_parameter_sets() const { 41 | return avc_record_.get_picture_parameter_sets(); 42 | } 43 | size_t GetNaluLength() const { 44 | return avc_record_.GetNaluLength(); 45 | } 46 | virtual std::string PrettyPrint(std::string indent) const; 47 | virtual std::string BoxName() const {return "AVC1 Decoder Configuration";} 48 | 49 | protected: 50 | virtual size_t Parse(const uint8_t* buffer, size_t length); 51 | 52 | private: 53 | AvcDecoderConfigurationRecord avc_record_; 54 | }; 55 | } // namespace dash2hls 56 | 57 | #endif // _DASH2HLS_DASH_AVCC_CONTENTS_H_ 58 | -------------------------------------------------------------------------------- /library/dash/base_descriptor.h: -------------------------------------------------------------------------------- 1 | // Descriptors in H.264 are used to describe configurations inside of 2 | // various boxes. For DASH they tend to only be used in audio mp4a boxes. 3 | // 4 | // See ISO 14496-1 for more information (section 8.3.3). 5 | #ifndef _DASH2HLS_DASH_BASE_DESCRIPTOR_H_ 6 | #define _DASH2HLS_DASH_BASE_DESCRIPTOR_H_ 7 | 8 | #include 9 | #include 10 | 11 | namespace dash2hls { 12 | class BaseDescriptor { 13 | public: 14 | enum Tag { 15 | kForbiddenTag = 0x00, 16 | kObjectDescrTag = 0x01, 17 | kInitialObjectDescrTag = 0x02, 18 | kES_DescrTag = 0x03, 19 | kDecoderConfigDescrTag = 0x04, 20 | kDecSpecificInfoTag = 0x05, 21 | kSLConfigDescrTag= 0x06, 22 | kContentIdentDescrTag = 0x07, 23 | kSupplContentIdentDescrTag = 0x08, 24 | kIPI_DescrPointerTag = 0x09, 25 | kIPMP_DescrPointerTag = 0x0A, 26 | kIPMP_DescrTag = 0x0B, 27 | kQoS_DescrTag = 0x0C, 28 | kRegistrationDescrTag = 0x0D, 29 | kES_ID_IncTag = 0x0E, 30 | kES_ID_RefTag= 0x0F, 31 | kMP4_IOD_Tag = 0x10, 32 | kMP4_OD_Tag = 0x11, 33 | kIPL_DescrPointerRefTag = 0x12, 34 | kExtensionProfileLevelDescrTag = 0x13, 35 | kprofileLevelIndicationIndexDescrTag = 0x14, 36 | // 0x15-0x3F Reserved for ISO use. 37 | kContentClassificationDescrTag = 0x40, 38 | kKeyWordDescrTag = 0x41, 39 | kRatingDescrTag = 0x42, 40 | kLanguageDescrTag = 0x43, 41 | kShortTextualDescrTag = 0x44, 42 | kExpandedTextualDescrTag = 0x45, 43 | kContentCreatorNameDescrTag = 0x46, 44 | kContentCreationDateDescrTag = 0x47, 45 | kOCICreatorNameDescrTag = 0x48, 46 | kOCICreationDateDescrTag = 0x49, 47 | kSmpteCameraPositionDescrTag = 0x4A, 48 | kSegmentDescrTag = 0x4B, 49 | kMediaTimeDescrTag = 0x4C, 50 | // 0x4D-0x5F Reserved for ISO use (OCI extensions). 51 | kIPMP_ToolsListDescrTag = 0x60, 52 | kIPMP_ToolTag = 0x61, 53 | kM4MuxTimingDescrTag = 0x62, 54 | kM4MuxCodeTableDescrTag = 0x63, 55 | kExtSLConfigDescrTag = 0x64, 56 | kM4MuxBufferSizeDescrTag = 0x65, 57 | kM4MuxIdentDescrTag = 0x66, 58 | kDependencyPointerTag = 0x67, 59 | kDependencyMarkerTag = 0x68, 60 | kM4MuxChannelDescrTag = 0x69, 61 | // 0x6A-0xBF Reserved for ISO use. 62 | // 0xC0-0xFE User private. 63 | kForbiddenLastTag = 0xFF, 64 | }; 65 | 66 | BaseDescriptor() : tag_(kForbiddenTag), size_(0) {} 67 | virtual ~BaseDescriptor() { 68 | } 69 | 70 | Tag get_tag() {return tag_;} 71 | size_t get_size() {return size_;} 72 | 73 | // Returns the size of the header and populates the tag_ and size_. 74 | size_t ParseHeader(const uint8_t* buffer, size_t length); 75 | 76 | // Subclasses should not call Parse. Parse assumes there is no 77 | // subclass and just eats the entire content. 78 | virtual size_t Parse(const uint8_t* buffer, size_t length); 79 | virtual std::string PrettyPrint(std::string indent) const; 80 | 81 | private: 82 | Tag tag_; 83 | size_t size_; 84 | }; 85 | } // namespace dash2hls 86 | 87 | #endif // _DASH2HLS_DASH_BASE_DESCRIPTOR_H_ 88 | -------------------------------------------------------------------------------- /library/dash/box.h: -------------------------------------------------------------------------------- 1 | #ifndef _DASH2HLS_BOX_H_ 2 | #define _DASH2HLS_BOX_H_ 3 | 4 | /* 5 | Copyright 2014 Google Inc. All rights reserved. 6 | 7 | Licensed under the Apache License, Version 2.0 (the "License"); 8 | you may not use this file except in compliance with the License. 9 | You may obtain a copy of the License at 10 | 11 | http://www.apache.org/licenses/LICENSE-2.0 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" BASIS, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | See the License for the specific language governing permissions and 17 | limitations under the License. 18 | */ 19 | 20 | // An mp4 box (atom) containing box specific data or more boxes. Box specific 21 | // data is contained in a subclass of BoxContents. 22 | // 23 | // To optimize parsing the Box is parsed in steps. The first step reads the 24 | // type and size of the box. After that it Parses the entire BoxContents at 25 | // once. BytesNeededToContinue returns how many bytes the Box Parse requires. 26 | // 27 | // Example: 28 | // uint8_t* dash_content = GetDashContent(); 29 | // size_t dash_size = GetDashSize(); 30 | // size_t bytes_parsed = 0; 31 | // Box box; 32 | // while (!box.DoneParsing) { 33 | // if (box.BytesNeededToContinue() > dash_size - bytes_parsed) { 34 | // return ERROR; 35 | // } 36 | // bytes_parsed += box.Parse(dash_content + bytes_parsed, 37 | // dash_size - bytes_parsed); 38 | // } 39 | 40 | #include 41 | 42 | #include "library/compatibility.h" 43 | #include "library/dash/box_contents.h" 44 | #include "library/dash/box_type.h" 45 | 46 | namespace dash2hls { 47 | 48 | class Box { 49 | public: 50 | enum { 51 | kBoxHeaderSize = sizeof(uint32_t) * 2 52 | }; 53 | // The |start_position| is where in the stream this Box starts. 54 | explicit Box(uint64_t start_position); 55 | 56 | bool DoneParsing() const; 57 | 58 | // Minimum number of bytes needed to call Parse. 59 | size_t BytesNeededToContinue() const; 60 | // Handle the next phase of parsing returning the number of bytes parsed. 61 | // length must be at least as long as BytesNeededToContinue. 62 | size_t Parse(const uint8_t* buffer, size_t length); 63 | 64 | const BoxContents* get_contents() const {return contents_.get();} 65 | const BoxType& get_type() const {return type_;} 66 | // Debugging routine for diagnostics. 67 | std::string PrettyPrint(std::string indent) const; 68 | 69 | protected: 70 | enum State { 71 | kInitialState = 0, 72 | kReadingType, 73 | kReadingBytes, 74 | kParsed, 75 | kLast 76 | }; 77 | void CreateContentsObject(); 78 | 79 | private: 80 | State state_; 81 | size_t size_; 82 | BoxType type_; 83 | size_t bytes_read_; 84 | dash2hls::shared_ptr contents_; 85 | uint64_t stream_position_; 86 | }; 87 | } // namespace dash2hls 88 | 89 | #endif // DASH2HLS_BOX_H_ 90 | -------------------------------------------------------------------------------- /library/dash/box_contents.cc: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2014 Google Inc. All rights reserved. 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | #include "library/dash/box_contents.h" 18 | #include "library/dash/dash_parser.h" 19 | 20 | namespace dash2hls { 21 | BoxContents::BoxContents(uint32_t box_type, uint64_t stream_position) 22 | : stream_position_(stream_position), 23 | dash_parser_(nullptr), 24 | box_type_(box_type) { 25 | } 26 | 27 | BoxContents::~BoxContents() { 28 | delete dash_parser_; 29 | } 30 | 31 | size_t BoxContents::Parse(const uint8_t* buffer, size_t length) { 32 | if (!dash_parser_) { 33 | dash_parser_ = new DashParser; 34 | dash_parser_->set_current_position(stream_position_); 35 | } 36 | return dash_parser_->Parse(buffer, length); 37 | } 38 | 39 | std::string BoxContents::PrettyPrint(std::string indent) const { 40 | if (dash_parser_) { 41 | return "\n" + dash_parser_->PrettyPrint(indent); 42 | } else { 43 | return ""; 44 | } 45 | } 46 | 47 | // BoxContents can have their own BoxName. These are the common boxes that 48 | // are not subclassed. 49 | std::string BoxContents::BoxName() const { 50 | switch (box_type_) { 51 | case BoxType::kBox_dinf: return "DataInformation"; 52 | case BoxType::kBox_edts: return "Edit (time mapping)"; 53 | case BoxType::kBox_mdat: return "MediaData"; 54 | case BoxType::kBox_mdia: return "Media"; 55 | case BoxType::kBox_minf: return "MediaInformation"; 56 | case BoxType::kBox_moof: return "MovieFragment"; 57 | case BoxType::kBox_moov: return "Movie"; 58 | case BoxType::kBox_mvex: return "MovieExtend"; 59 | case BoxType::kBox_stbl: return "SampleTable"; 60 | case BoxType::kBox_traf: return "TrackFragment"; 61 | case BoxType::kBox_trak: return "Track"; 62 | default: return "Unknown"; 63 | } 64 | } 65 | } // namespace dash2hls 66 | -------------------------------------------------------------------------------- /library/dash/box_contents.h: -------------------------------------------------------------------------------- 1 | #ifndef _DASH2HLS_BOX_CONTENTS_H_ 2 | #define _DASH2HLS_BOX_CONTENTS_H_ 3 | 4 | /* 5 | Copyright 2014 Google Inc. All rights reserved. 6 | 7 | Licensed under the Apache License, Version 2.0 (the "License"); 8 | you may not use this file except in compliance with the License. 9 | You may obtain a copy of the License at 10 | 11 | http://www.apache.org/licenses/LICENSE-2.0 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" BASIS, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | See the License for the specific language governing permissions and 17 | limitations under the License. 18 | */ 19 | 20 | // Superclass of each Box type. 21 | 22 | #include 23 | #include 24 | 25 | namespace dash2hls { 26 | 27 | class DashParser; 28 | 29 | class BoxContents { 30 | friend class Box; 31 | public: 32 | BoxContents(uint32_t box_type, uint64_t stream_position); 33 | virtual ~BoxContents(); 34 | const DashParser* get_dash_parser() const { 35 | return dash_parser_; 36 | } 37 | uint64_t get_stream_position() const {return stream_position_;} 38 | 39 | virtual std::string PrettyPrint(std::string indent) const; 40 | virtual std::string BoxName() const; 41 | 42 | protected: 43 | // Returns the bytes parsed. Most boxes parse length bytes always. 44 | virtual size_t Parse(const uint8_t* buffer, size_t length); 45 | 46 | protected: 47 | uint64_t stream_position_; 48 | DashParser* dash_parser_; 49 | 50 | private: 51 | uint32_t box_type_; 52 | }; 53 | } // namespace dash2hls 54 | 55 | #endif // _DASH2HLS_BOX_CONTENTS_H_ 56 | -------------------------------------------------------------------------------- /library/dash/box_contents_test.cc: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2014 Google Inc. All rights reserved. 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | #include 18 | 19 | #include "library/dash/box_contents.h" 20 | 21 | TEST(Dash2Hls, BoxContents) { 22 | // TODO(justsomeguy) placeholder for now. 23 | } 24 | -------------------------------------------------------------------------------- /library/dash/box_test.cc: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2014 Google Inc. All rights reserved. 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | #include 18 | 19 | #include "library/dash/box.h" 20 | 21 | namespace { 22 | // Sample box with a length of 10 and a trailing character. 23 | const char kSimpleBox[] = "\0\0\0\012abcdxyz"; 24 | const size_t kExpectedBytesParsed = 10; 25 | const size_t kSimpleBoxDataSize = 2; 26 | } // namespace 27 | 28 | namespace dash2hls { 29 | 30 | TEST(DashToHls, Box) { 31 | Box box(0); 32 | size_t bytes_parsed = box.Parse(reinterpret_cast(kSimpleBox), 33 | sizeof(kSimpleBox)); 34 | EXPECT_EQ(kExpectedBytesParsed, bytes_parsed); 35 | EXPECT_EQ(size_t(0), box.BytesNeededToContinue()); 36 | EXPECT_EQ(true, box.DoneParsing()); 37 | } 38 | 39 | TEST(DashToHls, BoxInChunks) { 40 | Box box(0); 41 | EXPECT_EQ(false, box.DoneParsing()); 42 | EXPECT_EQ(sizeof(uint32_t), box.BytesNeededToContinue()); 43 | size_t bytes_parsed = box.Parse(reinterpret_cast(kSimpleBox), 44 | box.BytesNeededToContinue()); 45 | EXPECT_EQ(sizeof(uint32_t), bytes_parsed); 46 | EXPECT_EQ(false, box.DoneParsing()); 47 | 48 | EXPECT_EQ(sizeof(uint32_t), box.BytesNeededToContinue()); 49 | bytes_parsed += box.Parse(reinterpret_cast(kSimpleBox + 50 | bytes_parsed), 51 | box.BytesNeededToContinue()); 52 | EXPECT_EQ(sizeof(uint32_t) * 2, bytes_parsed); 53 | EXPECT_EQ(false, box.DoneParsing()); 54 | 55 | EXPECT_EQ(kSimpleBoxDataSize, box.BytesNeededToContinue()); 56 | bytes_parsed += box.Parse(reinterpret_cast(kSimpleBox + 57 | bytes_parsed), 58 | box.BytesNeededToContinue()); 59 | EXPECT_EQ(kExpectedBytesParsed, bytes_parsed); 60 | EXPECT_EQ(size_t(0), box.BytesNeededToContinue()); 61 | EXPECT_EQ(true, box.DoneParsing()); 62 | } 63 | } // namespace dash2hls 64 | -------------------------------------------------------------------------------- /library/dash/box_type.cc: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2014 Google Inc. All rights reserved. 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | #include "library/dash/box_type.h" 18 | 19 | #include "library/utilities.h" 20 | 21 | namespace dash2hls { 22 | 23 | void BoxType::set_type(Type type) { 24 | box_type_[0] = (type >> 24) & 0xff; 25 | box_type_[1] = (type >> 16) & 0xff; 26 | box_type_[2] = (type >> 8) & 0xff; 27 | box_type_[3] = (type) & 0xff; 28 | } 29 | 30 | void BoxType::set_type(const uint8_t* type) { 31 | box_type_[0] = type[0]; 32 | box_type_[1] = type[1]; 33 | box_type_[2] = type[2]; 34 | box_type_[3] = type[3]; 35 | } 36 | 37 | uint32_t BoxType::asUint32() const { 38 | return ntohl(*(reinterpret_cast(box_type_))); 39 | } 40 | 41 | uint32_t BoxType::asBoxType() const { 42 | return *(reinterpret_cast(box_type_)); 43 | } 44 | 45 | std::string BoxType::PrettyPrint(std::string indent) const { 46 | return std::string(box_type_, 4); 47 | } 48 | 49 | } // namespace dash2hls 50 | -------------------------------------------------------------------------------- /library/dash/box_type.h: -------------------------------------------------------------------------------- 1 | #ifndef _DASH2HLS_BOX_TYPE_H_ 2 | #define _DASH2HLS_BOX_TYPE_H_ 3 | 4 | /* 5 | Copyright 2014 Google Inc. All rights reserved. 6 | 7 | Licensed under the Apache License, Version 2.0 (the "License"); 8 | you may not use this file except in compliance with the License. 9 | You may obtain a copy of the License at 10 | 11 | http://www.apache.org/licenses/LICENSE-2.0 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" BASIS, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | See the License for the specific language governing permissions and 17 | limitations under the License. 18 | */ 19 | 20 | // All mp4/dash files are made up of boxes. Each box starts with a 4 byte 21 | // code describing the box. The 4 bytes in network byte order are a human 22 | // readable abbreviation of the type of box, for example, moov is a Movie box. 23 | // 24 | // BoxType is a simple class to detect the box type and Pretty print the box 25 | // type during debugging. 26 | 27 | #include 28 | #include 29 | #include 30 | 31 | #include "library/utilities.h" 32 | 33 | namespace dash2hls { 34 | 35 | class BoxType { 36 | public: 37 | // Mpeg box types. The hex value is ntohl of the 4 char name of the box. 38 | // This list is not comprehensive but contains the most likely found boxes. 39 | enum Type { 40 | kBox_avc1 = 'avc1', 41 | kBox_avcC = 'avcC', 42 | kBox_dinf = 'dinf', 43 | kBox_edts = 'edts', 44 | kBox_elst = 'elst', 45 | kBox_enca = 'enca', 46 | kBox_encv = 'encv', 47 | kBox_esds = 'esds', 48 | kBox_ftyp = 'ftyp', 49 | kBox_hdlr = 'hdlr', 50 | kBox_mdat = 'mdat', 51 | kBox_mdhd = 'mdhd', 52 | kBox_mdia = 'mdia', 53 | kBox_mfhd = 'mfhd', 54 | kBox_minf = 'minf', 55 | kBox_moof = 'moof', 56 | kBox_moov = 'moov', 57 | kBox_mp4a = 'mp4a', 58 | kBox_mvex = 'mvex', 59 | kBox_mvhd = 'mvhd', 60 | kBox_pssh = 'pssh', 61 | kBox_saio = 'saio', 62 | kBox_saiz = 'saiz', 63 | kBox_schi = 'schi', 64 | kBox_sidx = 'sidx', 65 | kBox_sinf = 'sinf', 66 | kBox_smhd = 'smhd', 67 | kBox_stbl = 'stbl', 68 | kBox_stco = 'stco', 69 | kBox_stsc = 'stsc', 70 | kBox_stsd = 'stsd', 71 | kBox_stss = 'stss', 72 | kBox_stsz = 'stsz', 73 | kBox_stts = 'stts', 74 | kBox_tenc = 'tenc', 75 | kBox_tfdt = 'tfdt', 76 | kBox_tfhd = 'tfhd', 77 | kBox_tkhd = 'tkhd', 78 | kBox_traf = 'traf', 79 | kBox_trak = 'trak', 80 | kBox_trex = 'trex', 81 | kBox_trun = 'trun', 82 | kBox_vmhd = 'vmhd', 83 | kBox_NoNe = '....', // Not a real box, useful for testing. 84 | }; 85 | 86 | public: 87 | BoxType() { 88 | memset(box_type_, 0, sizeof(box_type_)); 89 | } 90 | 91 | explicit BoxType(Type type) { 92 | set_type(type); 93 | } 94 | 95 | // Sets the box_type_ according to host byte order. 96 | void set_type(Type type); 97 | // Sets the box_type_ from an array of network ordered bytes. 98 | void set_type(const uint8_t* type); 99 | 100 | // The host byte order value of box_type_. 101 | uint32_t asUint32() const; 102 | 103 | // The network byte order value of the box_type_. 104 | uint32_t asBoxType() const; 105 | 106 | // Debugging routine to print diagnostic information. 107 | std::string PrettyPrint(std::string indent) const; 108 | 109 | private: 110 | char box_type_[4]; 111 | }; 112 | 113 | } // namespace dash2hls 114 | 115 | #endif // DASH2HLS_BOX_TYPE_H_ 116 | -------------------------------------------------------------------------------- /library/dash/box_type_test.cc: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2014 Google Inc. All rights reserved. 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | #include 18 | 19 | #include "library/dash/box_type.h" 20 | #include "library/utilities.h" 21 | 22 | namespace dash2hls { 23 | 24 | TEST(DashToHls, BoxType) { 25 | uint8_t vmhd[4]; 26 | memcpy(vmhd, "vmhd", sizeof(vmhd)); 27 | BoxType box_type_1; 28 | box_type_1.set_type(BoxType::kBox_vmhd); 29 | EXPECT_EQ(std::string("vmhd"), box_type_1.PrettyPrint("")); 30 | EXPECT_EQ(BoxType::kBox_vmhd, box_type_1.asUint32()); 31 | EXPECT_EQ(ntohl(BoxType::kBox_vmhd), box_type_1.asBoxType()); 32 | 33 | BoxType box_type_2; 34 | box_type_2.set_type(vmhd); 35 | EXPECT_EQ(std::string("vmhd"), box_type_1.PrettyPrint("")); 36 | EXPECT_EQ(BoxType::kBox_vmhd, box_type_1.asUint32()); 37 | EXPECT_EQ(ntohl(BoxType::kBox_vmhd), box_type_1.asBoxType()); 38 | } 39 | } // namespace dash2hls 40 | -------------------------------------------------------------------------------- /library/dash/decoder_descriptor.h: -------------------------------------------------------------------------------- 1 | // The DecodeDescriptor contains quite a bit of interesting information that 2 | // is not needed for HLS. It does contain the DecoderSpecificInformation, 3 | // which has the necessary audio configuration. Pretty much everything else is 4 | // only for diagnostics. 5 | // 6 | // See ISO 14496-1 for more information (section 7.2.6.6.1). 7 | #ifndef _DASH2HLS_DASH_DECODER_DESCRIPTOR_H_ 8 | #define _DASH2HLS_DASH_DECODER_DESCRIPTOR_H_ 9 | 10 | #include 11 | #include 12 | 13 | #include "library/dash/base_descriptor.h" 14 | 15 | namespace dash2hls { 16 | 17 | class DecoderDescriptor : public BaseDescriptor { 18 | public: 19 | virtual size_t Parse(const uint8_t* buffer, size_t length); 20 | virtual std::string PrettyPrint(std::string indent) const; 21 | 22 | // Framing needs these values, which are parsed out of the audio_config_. 23 | uint8_t get_audio_object_type() const { 24 | return decoder_specific_info_.get_audio_object_type(); 25 | } 26 | 27 | uint8_t get_sampling_frequency_index() const { 28 | return decoder_specific_info_.get_sampling_frequency_index(); 29 | } 30 | 31 | uint32_t get_extension_sampling_frequency() const { 32 | return decoder_specific_info_.get_extension_sampling_frequency(); 33 | } 34 | 35 | uint8_t get_channel_config() const { 36 | return decoder_specific_info_.get_channel_config(); 37 | } 38 | 39 | // The PMT needs the raw audio_config_. 40 | const std::vector& get_audio_config() const { 41 | return decoder_specific_info_.get_audio_config(); 42 | } 43 | 44 | bool sbr_present() const { 45 | return decoder_specific_info_.sbr_present(); 46 | } 47 | 48 | private: 49 | class DecoderSpecificInfo : public BaseDescriptor { 50 | public: 51 | virtual size_t Parse(const uint8_t* buffer, size_t length); 52 | virtual std::string PrettyPrint(std::string indent) const; 53 | uint8_t get_audio_object_type() const {return audio_object_type_;} 54 | uint8_t get_sampling_frequency_index() const { 55 | return sampling_frequency_index_; 56 | } 57 | uint32_t get_extension_sampling_frequency() const; 58 | uint8_t get_channel_config() const {return channel_config_;} 59 | const std::vector& get_audio_config() const {return audio_config_;} 60 | bool sbr_present() const {return sbr_present_;} 61 | 62 | private: 63 | uint8_t audio_object_type_; 64 | uint8_t sampling_frequency_index_; 65 | uint8_t channel_config_; 66 | uint8_t extension_audio_object_type_ = 0; 67 | uint8_t extension_sampling_frequency_index_ = 0; 68 | std::vector audio_config_; 69 | bool sbr_present_ = false; 70 | }; 71 | 72 | uint8_t objectTypeIndication_; 73 | uint8_t stream_type_; 74 | bool upstream_; 75 | uint32_t buffer_size_db_; 76 | uint32_t max_bitrate_; 77 | uint32_t average_bitrate_; 78 | DecoderSpecificInfo decoder_specific_info_; 79 | BaseDescriptor profile_descriptor_; 80 | }; 81 | } // namespace dash2hls 82 | 83 | #endif // _DASH2HLS_DASH_DECODER_DESCRIPTOR_H_ 84 | -------------------------------------------------------------------------------- /library/dash/elementary_stream_descriptor.h: -------------------------------------------------------------------------------- 1 | // ElementaryStreamDescriptor contain all information needed by the codec 2 | // to decode the samples that follow. The only really relevant information 3 | // needed for is the DecodeSpecificInformation, namely the 4 | // audio_config_ found in mp4a boxes. In theory DASH could use 5 | // mp4v boxes also but they tend to use avc1 instead, sp these boxes should 6 | // be seen in only audio tracks. 7 | // 8 | // See ISO 14496-1 for more information (section 7.2.6.5). 9 | #ifndef _DASH2HLS_DASH_ELEMENTARY_STREAM_DESCRIPTOR_H_ 10 | #define _DASH2HLS_DASH_ELEMENTARY_STREAM_DESCRIPTOR_H_ 11 | 12 | #include 13 | #include 14 | 15 | #include "library/dash/base_descriptor.h" 16 | #include "library/dash/decoder_descriptor.h" 17 | 18 | namespace dash2hls { 19 | 20 | class ElementaryStreamDescriptor : public BaseDescriptor { 21 | public: 22 | virtual std::string PrettyPrint(std::string indent) const; 23 | virtual size_t Parse(const uint8_t* buffer, size_t length); 24 | 25 | bool HasStreamDependsFlag() const { 26 | return flags_ & kStreamDependenceFlagMask; 27 | } 28 | 29 | bool HasUrl() const {return flags_ & kUrlFlagMask;} 30 | bool HasOcrStream() const {return flags_ & kOcrStreamFlagMask;} 31 | uint8_t StreamPriority() const {return flags_ & kStreamPriorityMask;} 32 | 33 | uint8_t get_audio_object_type() const { 34 | return decoder_descriptor_.get_audio_object_type(); 35 | } 36 | 37 | uint8_t get_sampling_frequency_index() const { 38 | return decoder_descriptor_.get_sampling_frequency_index(); 39 | } 40 | 41 | uint32_t get_extension_sampling_frequency() const { 42 | return decoder_descriptor_.get_extension_sampling_frequency(); 43 | } 44 | 45 | uint8_t get_channel_config() const { 46 | return decoder_descriptor_.get_channel_config(); 47 | } 48 | 49 | const std::vector& get_audio_config() const { 50 | return decoder_descriptor_.get_audio_config(); 51 | } 52 | 53 | bool sbr_present() const {return decoder_descriptor_.sbr_present();} 54 | 55 | private: 56 | enum { 57 | kStreamDependenceFlagMask = 0x80, 58 | kUrlFlagMask = 0x40, 59 | kOcrStreamFlagMask = 0x20, 60 | kStreamPriorityMask = 0x1f, 61 | }; 62 | uint16_t id_; 63 | uint8_t flags_; 64 | uint8_t depends_on_id_; // Misleading name but that is what it's called. 65 | std::vector url_; 66 | uint16_t ocr_id_; 67 | DecoderDescriptor decoder_descriptor_; 68 | 69 | // The following descriptors are not needed for Dash. 70 | BaseDescriptor sl_descriptor_; 71 | BaseDescriptor ipi_ptr_; 72 | BaseDescriptor language_; 73 | BaseDescriptor qos_; 74 | BaseDescriptor registration_; 75 | BaseDescriptor extension_; 76 | }; 77 | } // namespace dash2hls 78 | 79 | #endif // _DASH2HLS_DASH_ELEMENTARY_STREAM_DESCRIPTOR_H_ 80 | -------------------------------------------------------------------------------- /library/dash/elst_contents.h: -------------------------------------------------------------------------------- 1 | #ifndef _DASH2HLS_ELST_CONTENTS_H_ 2 | #define _DASH2HLS_ELST_CONTENTS_H_ 3 | 4 | /* 5 | Copyright 2015 Google Inc. All rights reserved. 6 | 7 | Licensed under the Apache License, Version 2.0 (the "License"); 8 | you may not use this file except in compliance with the License. 9 | You may obtain a copy of the License at 10 | 11 | http://www.apache.org/licenses/LICENSE-2.0 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" BASIS, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | See the License for the specific language governing permissions and 17 | limitations under the License. 18 | */ 19 | 20 | #include 21 | #include 22 | 23 | #include "library/dash/box_type.h" 24 | #include "library/dash/full_box_contents.h" 25 | 26 | namespace dash2hls { 27 | 28 | class ElstContents : public FullBoxContents { 29 | public: 30 | class Entry { 31 | public: 32 | uint64_t segment_duration_; 33 | int64_t media_time_; 34 | int16_t media_rate_integer_; 35 | int16_t media_rate_fraction_; 36 | }; 37 | 38 | explicit ElstContents(uint64_t stream_position) 39 | : FullBoxContents(BoxType::kBox_elst, stream_position) {} 40 | 41 | const std::vector& get_entries() const {return entries_;} 42 | 43 | virtual std::string PrettyPrintEntry(const Entry& entry) const; 44 | virtual std::string PrettyPrint(std::string indent) const; 45 | virtual std::string BoxName() const {return "EditList";} 46 | 47 | protected: 48 | virtual size_t Parse(const uint8_t* buffer, size_t length); 49 | 50 | private: 51 | uint32_t entry_count_; 52 | std::vector entries_; 53 | }; 54 | } // namespace dash2hls 55 | 56 | #endif // _DASH2HLS_ELST_CONTENTS_H_ 57 | -------------------------------------------------------------------------------- /library/dash/esds_contents.cc: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2014 Google Inc. All rights reserved. 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | #include "library/dash/esds_contents.h" 18 | 19 | #include "library/dash/box.h" 20 | #include "library/dash/dash_parser.h" 21 | #include "library/utilities.h" 22 | 23 | namespace dash2hls { 24 | 25 | // See ISO 14496-15 and 14495-12 for details. 26 | // 27 | // aligned(8) class ESDBox 28 | // extends FullBox(‘esds’, version = 0, 0) { 29 | // ES_Descriptor ES; 30 | // } 31 | size_t EsdsContents::Parse(const uint8_t *buffer, size_t length) { 32 | const uint8_t* ptr = buffer + FullBoxContents::Parse(buffer, length); 33 | size_t bytes_parsed = descriptor_.Parse(ptr, length - (ptr - buffer)); 34 | if (!bytes_parsed) { 35 | return DashParser::kParseFailure; 36 | } 37 | ptr += bytes_parsed; 38 | return ptr - buffer; 39 | } 40 | 41 | std::string EsdsContents::PrettyPrint(std::string indent) const { 42 | std::string result = FullBoxContents::PrettyPrint(indent); 43 | result += std::string("\n") + descriptor_.PrettyPrint(indent + " "); 44 | return result; 45 | } 46 | } // namespace dash2hls 47 | -------------------------------------------------------------------------------- /library/dash/esds_contents.h: -------------------------------------------------------------------------------- 1 | #ifndef _DASH2HLS_ELEMENTARY_STREAM_DESCRIPTOR_CONTENTS_H_ 2 | #define _DASH2HLS_ELEMENTARY_STREAM_DESCRIPTOR_CONTENTS_H_ 3 | 4 | /* 5 | Copyright 2014 Google Inc. All rights reserved. 6 | 7 | Licensed under the Apache License, Version 2.0 (the "License"); 8 | you may not use this file except in compliance with the License. 9 | You may obtain a copy of the License at 10 | 11 | http://www.apache.org/licenses/LICENSE-2.0 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" BASIS, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | See the License for the specific language governing permissions and 17 | limitations under the License. 18 | */ 19 | 20 | // Box containing a codec information. Usually a subbox of the mp4a. 21 | 22 | #include 23 | 24 | #include "library/dash/box_type.h" 25 | #include "library/dash/elementary_stream_descriptor.h" 26 | #include "library/dash/full_box_contents.h" 27 | 28 | namespace dash2hls { 29 | 30 | class EsdsContents : public FullBoxContents { 31 | public: 32 | explicit EsdsContents(uint64_t stream_position) 33 | : FullBoxContents(BoxType::kBox_esds, stream_position) {} 34 | virtual std::string PrettyPrint(std::string indent) const; 35 | virtual std::string BoxName() const {return "Elementary Stream Description";} 36 | 37 | uint8_t get_audio_object_type() const { 38 | return descriptor_.get_audio_object_type(); 39 | } 40 | 41 | uint8_t get_sampling_frequency_index() const { 42 | return descriptor_.get_sampling_frequency_index(); 43 | } 44 | 45 | uint32_t get_extension_sampling_frequency() const { 46 | return descriptor_.get_extension_sampling_frequency(); 47 | } 48 | 49 | uint8_t get_channel_config() const {return descriptor_.get_channel_config();} 50 | 51 | const std::vector& get_audio_config() const { 52 | return descriptor_.get_audio_config(); 53 | } 54 | 55 | bool sbr_present() const {return descriptor_.sbr_present();} 56 | 57 | protected: 58 | virtual size_t Parse(const uint8_t* buffer, size_t length); 59 | 60 | private: 61 | ElementaryStreamDescriptor descriptor_; 62 | }; 63 | } // namespace dash2hls 64 | 65 | #endif // _DASH2HLS_ELEMENTARY_STREAM_DESCRIPTOR_CONTENTS_H_ 66 | -------------------------------------------------------------------------------- /library/dash/full_box_contents.cc: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2014 Google Inc. All rights reserved. 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | #include "library/dash/full_box_contents.h" 18 | 19 | #include 20 | 21 | #include "library/dash/dash_parser.h" 22 | #include "library/utilities.h" 23 | 24 | namespace { 25 | const uint32_t kBufferSize = 1024; 26 | } // namespace 27 | 28 | namespace dash2hls { 29 | 30 | // A FullBoxContents is the super class of most boxes with box specific 31 | // data. Each FullBoxContents has a version and flags. 32 | size_t FullBoxContents::Parse(const uint8_t* buffer, size_t length) { 33 | if (!EnoughBytesToParse(0, sizeof(uint32_t), length)) { 34 | DASH_LOG((BoxName() + " too short").c_str(), 35 | "At least 4 bytes are required", 36 | DumpMemory(buffer, length).c_str()); 37 | return DashParser::kParseFailure; 38 | } 39 | version_ = static_cast(buffer[0]); 40 | if (version_ >= kVersionLast) { 41 | DASH_LOG((BoxName() + " bad version").c_str(), 42 | "Only support version 0 and 1.", 43 | DumpMemory(buffer, length).c_str()); 44 | return DashParser::kParseFailure; 45 | } 46 | flags_ = (buffer[1] << 16) | (buffer[2] << 8) | buffer[3]; 47 | return sizeof(flags_); 48 | } 49 | 50 | std::string FullBoxContents::PrettyPrint(std::string indent) const { 51 | char buffer[kBufferSize]; 52 | snprintf(buffer, sizeof(buffer), "Version %d flags %x", version_, flags_); 53 | return buffer; 54 | } 55 | } // namespace dash2hls 56 | -------------------------------------------------------------------------------- /library/dash/full_box_contents.h: -------------------------------------------------------------------------------- 1 | #ifndef _DASH2HLS_FULL_BOX_CONTENTS_H_ 2 | #define _DASH2HLS_FULL_BOX_CONTENTS_H_ 3 | 4 | /* 5 | Copyright 2014 Google Inc. All rights reserved. 6 | 7 | Licensed under the Apache License, Version 2.0 (the "License"); 8 | you may not use this file except in compliance with the License. 9 | You may obtain a copy of the License at 10 | 11 | http://www.apache.org/licenses/LICENSE-2.0 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" BASIS, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | See the License for the specific language governing permissions and 17 | limitations under the License. 18 | */ 19 | 20 | // A FullBox is a box with box specific contents. All full boxes start with 21 | // a version and flags. The Parse is always 4 bytes. 22 | 23 | #include 24 | 25 | #include "library/dash/box_contents.h" 26 | 27 | namespace dash2hls { 28 | 29 | class FullBoxContents : public BoxContents { 30 | public: 31 | enum Version { 32 | kVersion0 = 0, 33 | kVersion1 = 1, 34 | kVersionLast 35 | }; 36 | 37 | static const uint32_t kFullBoxHeaderSize = sizeof(uint32_t); 38 | FullBoxContents(uint32_t type, uint64_t stream_position) : 39 | BoxContents(type, stream_position) {} 40 | 41 | virtual size_t Parse(const uint8_t* buffer, size_t length); 42 | virtual std::string PrettyPrint(std::string indent) const; 43 | 44 | protected: 45 | Version version_; 46 | uint32_t flags_; 47 | }; 48 | } // namespace dash2hls 49 | 50 | #endif // _DASH2HLS_FULL_BOX_CONTENTS_H_ 51 | -------------------------------------------------------------------------------- /library/dash/mdat_contents.cc: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2014 Google Inc. All rights reserved. 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | #include "library/dash/mdat_contents.h" 18 | 19 | #include 20 | 21 | #include "library/utilities.h" 22 | 23 | namespace dash2hls { 24 | // See ISO 14495-12 for details. 25 | // 26 | // Mdat is the raw samples concatenated without any markers or framing. 27 | // Other boxes are required to find the starts and stops of samples. 28 | size_t MdatContents::Parse(const uint8_t* buffer, size_t length) { 29 | raw_data_ = buffer; 30 | raw_data_length_ = length; 31 | return length; 32 | } 33 | 34 | std::string MdatContents::PrettyPrint(std::string indent) const { 35 | std::string result = BoxContents::PrettyPrint(indent); 36 | result += " " + PrettyPrintValue(raw_data_length_) + " bytes"; 37 | return result; 38 | } 39 | } // namespace dash2hls 40 | -------------------------------------------------------------------------------- /library/dash/mdat_contents.h: -------------------------------------------------------------------------------- 1 | #ifndef _DASH2HLS_MDAT_CONTENTS_H_ 2 | #define _DASH2HLS_MDAT_CONTENTS_H_ 3 | 4 | /* 5 | Copyright 2014 Google Inc. All rights reserved. 6 | 7 | Licensed under the Apache License, Version 2.0 (the "License"); 8 | you may not use this file except in compliance with the License. 9 | You may obtain a copy of the License at 10 | 11 | http://www.apache.org/licenses/LICENSE-2.0 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" BASIS, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | See the License for the specific language governing permissions and 17 | limitations under the License. 18 | */ 19 | 20 | // Raw data samples. The data are packed based on the contents of the other 21 | // boxes in the traf. 22 | 23 | #include 24 | 25 | #include "library/compatibility.h" 26 | #include "library/dash/box_contents.h" 27 | #include "library/dash/box_type.h" 28 | 29 | namespace dash2hls { 30 | 31 | class MdatContents : public BoxContents { 32 | public: 33 | explicit MdatContents(uint64_t position) 34 | : BoxContents(BoxType::kBox_mdat, position), 35 | raw_data_(nullptr), 36 | raw_data_length_(0) { 37 | } 38 | const uint8_t* get_raw_data() const {return raw_data_;} 39 | size_t get_raw_data_length() const {return raw_data_length_;} 40 | 41 | protected: 42 | virtual size_t Parse(const uint8_t* buffer, size_t length); 43 | virtual std::string PrettyPrint(std::string indent) const; 44 | virtual std::string BoxName() const {return "Media Data";} 45 | 46 | private: 47 | const uint8_t* raw_data_; 48 | size_t raw_data_length_; 49 | }; 50 | } // namespace dash2hls 51 | 52 | #endif // _DASH2HLS_MDAT_CONTENTS_H_ 53 | -------------------------------------------------------------------------------- /library/dash/mdhd_contents.cc: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2014 Google Inc. All rights reserved. 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | #include "library/dash/mdhd_contents.h" 18 | 19 | #include "library/dash/box.h" 20 | #include "library/dash/dash_parser.h" 21 | #include "library/utilities.h" 22 | 23 | namespace dash2hls { 24 | 25 | // See ISO 14496-12 for details. 26 | // aligned(8) class MovieHeaderBox extends FullBox(‘mvhd’, version, 0) { 27 | // if (version==1) { 28 | // unsigned int(64) creation_time; 29 | // unsigned int(64) modification_time; 30 | // unsigned int(32) timescale; 31 | // unsigned int(64) duration; 32 | // } else { // version==0 33 | // unsigned int(32) creation_time; 34 | // unsigned int(32) modification_time; 35 | // unsigned int(32) timescale; 36 | // unsigned int(32) duration; 37 | // } 38 | // bit(1) pad = 0; 39 | // unsigned int(5)[3] language; // ISO-639-2/T language code 40 | // unsigned int(16) pre_defined = 0; 41 | // } 42 | 43 | size_t MdhdContents::Parse(const uint8_t* buffer, size_t length) { 44 | const uint8_t* ptr = buffer + FullBoxContents::Parse(buffer, length); 45 | if (ptr == buffer) { 46 | DASH_LOG((BoxName() + " too short").c_str(), 47 | "Header not found.", 48 | DumpMemory(buffer, length).c_str()); 49 | return DashParser::kParseFailure; 50 | } 51 | if (version_ == kVersion0) { 52 | if (!EnoughBytesToParse(ptr - buffer, sizeof(uint32_t) * 4, length)) { 53 | DASH_LOG((BoxName() + " too short").c_str(), 54 | "Mandatory fields not found.", 55 | DumpMemory(buffer, length).c_str()); 56 | } 57 | creation_time_ = ntohlFromBuffer(ptr); 58 | ptr += sizeof(uint32_t); 59 | modification_time_ = ntohlFromBuffer(ptr); 60 | ptr += sizeof(uint32_t); 61 | timescale_ = ntohlFromBuffer(ptr); 62 | ptr += sizeof(uint32_t); 63 | duration_ = ntohlFromBuffer(ptr); 64 | ptr += sizeof(uint32_t); 65 | } else { 66 | if (!EnoughBytesToParse(ptr - buffer, sizeof(uint64_t) * 4, length)) { 67 | DASH_LOG((BoxName() + " too short").c_str(), 68 | "Mandatory fields not found.", 69 | DumpMemory(buffer, length).c_str()); 70 | } 71 | creation_time_ = ntohllFromBuffer(ptr); 72 | ptr += sizeof(uint64_t); 73 | modification_time_ = ntohllFromBuffer(ptr); 74 | ptr += sizeof(uint64_t); 75 | timescale_ = ntohlFromBuffer(ptr); 76 | ptr += sizeof(uint32_t); 77 | duration_ = ntohllFromBuffer(ptr); 78 | ptr += sizeof(uint64_t); 79 | } 80 | if (!EnoughBytesToParse(ptr - buffer, sizeof(uint8_t) + sizeof(uint16_t), 81 | length)) { 82 | DASH_LOG((BoxName() + " too short").c_str(), 83 | "Mandatory fields not found.", 84 | DumpMemory(buffer, length).c_str()); 85 | return DashParser::kParseFailure; 86 | } 87 | language_ = ptr[0]; 88 | ptr += sizeof(uint8_t) + sizeof(uint16_t); 89 | return ptr - buffer; 90 | } 91 | 92 | std::string MdhdContents::PrettyPrint(std::string indent) const { 93 | std::string result = FullBoxContents::PrettyPrint(indent); 94 | result += " Creation Time: " + PrettyPrintValue(creation_time_); 95 | result += " Modification Time: " + PrettyPrintValue(modification_time_); 96 | result += " Timescale: " + PrettyPrintValue(timescale_); 97 | result += " Duration: " + PrettyPrintValue(duration_); 98 | result += " Language code: " + PrettyPrintValue(language_); 99 | return result; 100 | } 101 | } // namespace dash2hls 102 | -------------------------------------------------------------------------------- /library/dash/mdhd_contents.h: -------------------------------------------------------------------------------- 1 | #ifndef _DASH2HLS_MDHD_CONTENTS_H_ 2 | #define _DASH2HLS_MDHD_CONTENTS_H_ 3 | 4 | /* 5 | Copyright 2014 Google Inc. All rights reserved. 6 | 7 | Licensed under the Apache License, Version 2.0 (the "License"); 8 | you may not use this file except in compliance with the License. 9 | You may obtain a copy of the License at 10 | 11 | http://www.apache.org/licenses/LICENSE-2.0 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" BASIS, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | See the License for the specific language governing permissions and 17 | limitations under the License. 18 | */ 19 | 20 | // The mvhd box contains information about the asset, the important one is 21 | // the timescale. 22 | 23 | #include 24 | 25 | #include "library/dash/box_type.h" 26 | #include "library/dash/full_box_contents.h" 27 | 28 | namespace dash2hls { 29 | 30 | class MdhdContents : public FullBoxContents { 31 | public: 32 | explicit MdhdContents(uint64_t stream_position) 33 | : FullBoxContents(BoxType::kBox_mvhd, stream_position) {} 34 | 35 | uint64_t get_creation_time() const {return creation_time_;} 36 | uint64_t get_modification_time() const {return modification_time_;} 37 | uint64_t get_timescale() const {return timescale_;} 38 | uint64_t get_duration() const {return duration_;} 39 | uint8_t get_language() const {return language_;} 40 | 41 | virtual std::string PrettyPrint(std::string indent) const; 42 | virtual std::string BoxName() const {return "Movie Header";} 43 | 44 | protected: 45 | virtual size_t Parse(const uint8_t* buffer, size_t length); 46 | 47 | private: 48 | uint64_t creation_time_; 49 | uint64_t modification_time_; 50 | uint64_t timescale_; 51 | uint64_t duration_; 52 | uint8_t language_; 53 | }; 54 | } // namespace dash2hls 55 | 56 | #endif // _DASH2HLS_MDHD_CONTENTS_H_ 57 | -------------------------------------------------------------------------------- /library/dash/mp4a_contents.cc: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2014 Google Inc. All rights reserved. 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | #include "library/dash/mp4a_contents.h" 18 | 19 | #include "library/dash/box.h" 20 | #include "library/dash/box_type.h" 21 | #include "library/dash/dash_parser.h" 22 | #include "library/utilities.h" 23 | 24 | namespace dash2hls { 25 | 26 | // See ISO 14496-15 and 14495-12 for details. 27 | // 28 | // class MP4AudioSampleEntry() extends AudioSampleEntry ('mp4a'){ 29 | // ESDBox ES; 30 | // } 31 | size_t Mp4aContents::Parse(const uint8_t *buffer, size_t length) { 32 | const uint8_t* ptr = buffer + 33 | AudioSampleEntryContents::Parse(buffer, length); 34 | if (!dash_parser_) { 35 | DASH_LOG("Bad mp4a", 36 | "mp4a does not contain boxes inside it.", 37 | DumpMemory(buffer, length).c_str()); 38 | return DashParser::kParseFailure; 39 | } 40 | const Box* box = dash_parser_->Find(BoxType::kBox_esds); 41 | if (!box) { 42 | DASH_LOG("Bad mp4a", 43 | "mp4a does not contain an esds box.", 44 | DumpMemory(buffer, length).c_str()); 45 | return DashParser::kParseFailure; 46 | } 47 | 48 | esds_ = reinterpret_cast(box->get_contents()); 49 | return ptr - buffer; 50 | } 51 | 52 | std::string Mp4aContents::PrettyPrint(std::string indent) const { 53 | std::string result = AudioSampleEntryContents::PrettyPrint(indent); 54 | return result; 55 | } 56 | } // namespace dash2hls 57 | -------------------------------------------------------------------------------- /library/dash/mp4a_contents.h: -------------------------------------------------------------------------------- 1 | #ifndef _DASH2HLS_MP4A_CONTENTS_H_ 2 | #define _DASH2HLS_MP4A_CONTENTS_H_ 3 | 4 | /* 5 | Copyright 2014 Google Inc. All rights reserved. 6 | 7 | Licensed under the Apache License, Version 2.0 (the "License"); 8 | you may not use this file except in compliance with the License. 9 | You may obtain a copy of the License at 10 | 11 | http://www.apache.org/licenses/LICENSE-2.0 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" BASIS, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | See the License for the specific language governing permissions and 17 | limitations under the License. 18 | */ 19 | 20 | // SampleEntryBox containing the audio codec information. 21 | 22 | #include 23 | 24 | #include "library/dash/box_type.h" 25 | #include "library/dash/audio_sample_entry_contents.h" 26 | #include "library/dash/esds_contents.h" 27 | 28 | namespace dash2hls { 29 | 30 | class EsdsContents; 31 | 32 | class Mp4aContents : public AudioSampleEntryContents { 33 | public: 34 | explicit Mp4aContents(uint64_t stream_position) 35 | : AudioSampleEntryContents(BoxType::kBox_mp4a, stream_position) {} 36 | virtual std::string PrettyPrint(std::string indent) const; 37 | virtual std::string BoxName() const {return "Audio Decode Information";} 38 | 39 | uint8_t get_audio_object_type() const { 40 | return esds_->get_audio_object_type(); 41 | } 42 | 43 | uint8_t get_sampling_frequency_index() const { 44 | return esds_->get_sampling_frequency_index(); 45 | } 46 | 47 | uint32_t get_extension_sampling_frequency() const { 48 | return esds_->get_extension_sampling_frequency(); 49 | } 50 | 51 | uint8_t get_channel_config() const {return esds_->get_channel_config();} 52 | 53 | const std::vector& get_audio_config() const { 54 | return esds_->get_audio_config(); 55 | } 56 | 57 | bool sbr_present() const {return esds_->sbr_present();} 58 | 59 | protected: 60 | virtual size_t Parse(const uint8_t* buffer, size_t length); 61 | 62 | private: 63 | const EsdsContents* esds_; 64 | }; 65 | } // namespace dash2hls 66 | 67 | #endif // _DASH2HLS_MP4A_CONTENTS_H_ 68 | -------------------------------------------------------------------------------- /library/dash/mvhd_contents.cc: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2014 Google Inc. All rights reserved. 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | #include "library/dash/mvhd_contents.h" 18 | 19 | #include "library/dash/box.h" 20 | #include "library/dash/dash_parser.h" 21 | #include "library/utilities.h" 22 | 23 | namespace dash2hls { 24 | 25 | // See ISO 14496-12 for details. 26 | // aligned(8) class MovieHeaderBox extends FullBox(‘mvhd’, version, 0) { 27 | // if (version==1) { 28 | // unsigned int(64) creation_time; 29 | // unsigned int(64) modification_time; 30 | // unsigned int(32) timescale; 31 | // unsigned int(64) duration; 32 | // } else { // version==0 33 | // unsigned int(32) creation_time; 34 | // unsigned int(32) modification_time; 35 | // unsigned int(32) timescale; 36 | // unsigned int(32) duration; 37 | // } 38 | // template int(32) rate = 0x00010000; // typically 1.0 39 | // template int(16) volume = 0x0100; // typically, full volume 40 | // const bit(16) reserved = 0; 41 | // const unsigned int(32)[2] reserved = 0; 42 | // template int(32)[9] matrix = 43 | // { 0x00010000,0,0,0,0x00010000,0,0,0,0x40000000 }; 44 | // // Unity matrix 45 | // bit(32)[6] pre_defined = 0; 46 | // unsigned int(32) next_track_ID; 47 | // } 48 | 49 | size_t MvhdContents::Parse(const uint8_t* buffer, size_t length) { 50 | const uint8_t* ptr = buffer + FullBoxContents::Parse(buffer, length); 51 | if (ptr == buffer) { 52 | DASH_LOG((BoxName() + " too short").c_str(), 53 | "Header not found.", 54 | DumpMemory(buffer, length).c_str()); 55 | return DashParser::kParseFailure; 56 | } 57 | if (version_ == kVersion0) { 58 | if (!EnoughBytesToParse(ptr - buffer, sizeof(uint32_t) * 4, length)) { 59 | DASH_LOG((BoxName() + " too short").c_str(), 60 | "Mandatory fields not found.", 61 | DumpMemory(buffer, length).c_str()); 62 | } 63 | creation_time_ = ntohlFromBuffer(ptr); 64 | ptr += sizeof(uint32_t); 65 | modification_time_ = ntohlFromBuffer(ptr); 66 | ptr += sizeof(uint32_t); 67 | timescale_ = ntohlFromBuffer(ptr); 68 | ptr += sizeof(uint32_t); 69 | duration_ = ntohlFromBuffer(ptr); 70 | ptr += sizeof(uint32_t); 71 | } else { 72 | if (!EnoughBytesToParse(ptr - buffer, sizeof(uint64_t) * 4, length)) { 73 | DASH_LOG((BoxName() + " too short").c_str(), 74 | "Mandatory fields not found.", 75 | DumpMemory(buffer, length).c_str()); 76 | } 77 | creation_time_ = ntohllFromBuffer(ptr); 78 | ptr += sizeof(uint64_t); 79 | modification_time_ = ntohllFromBuffer(ptr); 80 | ptr += sizeof(uint64_t); 81 | timescale_ = ntohllFromBuffer(ptr); 82 | ptr += sizeof(uint64_t); 83 | duration_ = ntohllFromBuffer(ptr); 84 | ptr += sizeof(uint64_t); 85 | } 86 | return ptr - buffer; 87 | } 88 | 89 | std::string MvhdContents::PrettyPrint(std::string indent) const { 90 | std::string result = FullBoxContents::PrettyPrint(indent); 91 | result += " Creation Time: " + PrettyPrintValue(creation_time_); 92 | result += " Modification Time: " + PrettyPrintValue(modification_time_); 93 | result += " Timescale: " + PrettyPrintValue(timescale_); 94 | result += " Duration: " + PrettyPrintValue(duration_); 95 | return result; 96 | } 97 | } // namespace dash2hls 98 | -------------------------------------------------------------------------------- /library/dash/mvhd_contents.h: -------------------------------------------------------------------------------- 1 | #ifndef _DASH2HLS_MVHD_CONTENTS_H_ 2 | #define _DASH2HLS_MVHD_CONTENTS_H_ 3 | 4 | /* 5 | Copyright 2014 Google Inc. All rights reserved. 6 | 7 | Licensed under the Apache License, Version 2.0 (the "License"); 8 | you may not use this file except in compliance with the License. 9 | You may obtain a copy of the License at 10 | 11 | http://www.apache.org/licenses/LICENSE-2.0 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" BASIS, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | See the License for the specific language governing permissions and 17 | limitations under the License. 18 | */ 19 | 20 | // The mvhd box contains information about the asset, the important one is 21 | // the timescale. 22 | 23 | #include 24 | 25 | #include "library/dash/box_type.h" 26 | #include "library/dash/full_box_contents.h" 27 | 28 | namespace dash2hls { 29 | 30 | class MvhdContents : public FullBoxContents { 31 | public: 32 | explicit MvhdContents(uint64_t stream_position) 33 | : FullBoxContents(BoxType::kBox_mvhd, stream_position) {} 34 | 35 | uint64_t get_creation_time() const {return creation_time_;} 36 | uint64_t get_modification_time() const {return modification_time_;} 37 | uint64_t get_timescale() const {return timescale_;} 38 | uint64_t get_duration() const {return duration_;} 39 | 40 | virtual std::string PrettyPrint(std::string indent) const; 41 | virtual std::string BoxName() const {return "Movie Header";} 42 | 43 | protected: 44 | virtual size_t Parse(const uint8_t* buffer, size_t length); 45 | 46 | private: 47 | uint64_t creation_time_; 48 | uint64_t modification_time_; 49 | uint64_t timescale_; 50 | uint64_t duration_; 51 | }; 52 | } // namespace dash2hls 53 | 54 | #endif // _DASH2HLS_MVHD_CONTENTS_H_ 55 | -------------------------------------------------------------------------------- /library/dash/pssh_contents.cc: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2014 Google Inc. All rights reserved. 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | #include "library/dash/pssh_contents.h" 18 | 19 | #include "library/dash/box.h" 20 | #include "library/dash/dash_parser.h" 21 | #include "library/utilities.h" 22 | 23 | namespace dash2hls { 24 | 25 | // See ISO 23001-7 for details. 26 | // aligned(8) class ProtectionSystemSpecificHeaderBox extends FullBox(‘pssh’, 27 | // version=0, flags=0) 28 | // { 29 | // unsigned int(8)[16] SystemID; 30 | // unsigned int(32) DataSize; 31 | // unsigned int(8)[DataSize] Data; 32 | // } 33 | size_t PsshContents::Parse(const uint8_t *buffer, size_t length) { 34 | full_box_.resize(length + sizeof(uint32_t) * 2); 35 | htonlToBuffer(static_cast(length + sizeof(uint32_t) * 2), 36 | &full_box_[0]); 37 | full_box_[4] = 'p'; 38 | full_box_[5] = 's'; 39 | full_box_[6] = 's'; 40 | full_box_[7] = 'h'; 41 | memcpy(&full_box_[sizeof(uint32_t) * 2], buffer, length); 42 | const uint8_t* ptr = buffer + FullBoxContents::Parse(buffer, length); 43 | if ((ptr == buffer) || 44 | !EnoughBytesToParse(ptr - buffer, sizeof(uint32_t), length)) { 45 | DASH_LOG((BoxName() + " too short").c_str(), 46 | "At least 4 bytes are required", 47 | DumpMemory(buffer, length).c_str()); 48 | return DashParser::kParseFailure; 49 | } 50 | contents_.resize(length - (ptr - buffer)); 51 | memcpy(&contents_[0], ptr, contents_.size()); 52 | if (!EnoughBytesToParse(ptr - buffer, sizeof(system_id_) + sizeof(uint32_t), 53 | length)) { 54 | DASH_LOG((BoxName() + " too short").c_str(), 55 | "At least 20 more bytes is required", 56 | DumpMemory(buffer, length).c_str()); 57 | return DashParser::kParseFailure; 58 | } 59 | memcpy(system_id_, ptr, sizeof(system_id_)); 60 | ptr += sizeof(system_id_); 61 | data_.resize(ntohlFromBuffer(ptr)); 62 | ptr += sizeof(uint32_t); 63 | if (!EnoughBytesToParse(ptr - buffer, data_.size(), length)) { 64 | DASH_LOG((BoxName() + " too short").c_str(), 65 | "Data missing.", 66 | DumpMemory(buffer, length).c_str()); 67 | return DashParser::kParseFailure; 68 | } 69 | memcpy(&data_[0], ptr, data_.size()); 70 | ptr += data_.size(); 71 | return ptr - buffer; 72 | } 73 | 74 | std::string PsshContents::PrettyPrint(std::string indent) const { 75 | std::string result = FullBoxContents::PrettyPrint(indent); 76 | result += "SystemId: " + PrettyPrintBuffer(system_id_, sizeof(system_id_)); 77 | if (g_verbose_pretty_print) { 78 | result += "Data: " + PrettyPrintBuffer(data_.data(), data_.size()); 79 | } 80 | return result; 81 | } 82 | } // namespace dash2hls 83 | -------------------------------------------------------------------------------- /library/dash/pssh_contents.h: -------------------------------------------------------------------------------- 1 | #ifndef _DASH2HLS_PSSH_CONTENTS_H_ 2 | #define _DASH2HLS_PSSH_CONTENTS_H_ 3 | 4 | /* 5 | Copyright 2014 Google Inc. All rights reserved. 6 | 7 | Licensed under the Apache License, Version 2.0 (the "License"); 8 | you may not use this file except in compliance with the License. 9 | You may obtain a copy of the License at 10 | 11 | http://www.apache.org/licenses/LICENSE-2.0 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" BASIS, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | See the License for the specific language governing permissions and 17 | limitations under the License. 18 | */ 19 | 20 | // pssh is the Protection System Specification Header. While the pssh has a 21 | // structure the CDM code takes the entire contents (without the mp4 header) 22 | // and parses it. Parsing in this box is just to provide a human readable 23 | // output for diagnostics. 24 | 25 | #include 26 | #include 27 | 28 | #include "library/dash/box_type.h" 29 | #include "library/dash/full_box_contents.h" 30 | 31 | namespace dash2hls { 32 | 33 | class PsshContents : public FullBoxContents { 34 | public: 35 | explicit PsshContents(uint64_t stream_position) 36 | : FullBoxContents(BoxType::kBox_pssh, stream_position) {} 37 | 38 | virtual std::string PrettyPrint(std::string indent) const; 39 | virtual std::string BoxName() const { 40 | return "ProtectionSystemSpecificHeader"; 41 | } 42 | 43 | const std::vector get_full_box() const {return full_box_;} 44 | const std::vector get_contents() const {return contents_;} 45 | 46 | protected: 47 | virtual size_t Parse(const uint8_t* buffer, size_t length); 48 | 49 | private: 50 | uint8_t system_id_[16]; 51 | std::vector full_box_; 52 | std::vector data_; 53 | std::vector contents_; 54 | }; 55 | } // namespace dash2hls 56 | 57 | #endif // _DASH2HLS_PSSH_CONTENTS_H_ 58 | -------------------------------------------------------------------------------- /library/dash/saio_contents.cc: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2014 Google Inc. All rights reserved. 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | #include "library/dash/saio_contents.h" 18 | 19 | #include "library/dash/box.h" 20 | #include "library/dash/dash_parser.h" 21 | #include "library/utilities.h" 22 | 23 | namespace dash2hls { 24 | 25 | bool SaioContents::IsAuxInfoPresent() const { 26 | return flags_ & kAuxInfoPresentMask; 27 | } 28 | 29 | // Currently only parses version 1 of a sidx box. 30 | // 31 | // See ISO 14496-12 for details. 32 | // aligned(8) class SampleAuxiliaryInformationOffsetsBox 33 | // extends FullBox(‘saio’, version, flags) 34 | // { 35 | // if (flags & 1) { 36 | // unsigned int(32) aux_info_type; 37 | // unsigned int(32) aux_info_type_parameter; 38 | // } 39 | // unsigned int(32) entry_count; 40 | // if ( version == 0 ) { 41 | // unsigned int(32) offset[ entry_count ]; 42 | // } 43 | // else { 44 | // unsigned int(64) offset[ entry_count ]; 45 | // } 46 | size_t SaioContents::Parse(const uint8_t *buffer, size_t length) { 47 | const uint8_t* ptr = buffer + FullBoxContents::Parse(buffer, length); 48 | if ((ptr == buffer) || 49 | !EnoughBytesToParse(ptr - buffer, sizeof(uint32_t), length)) { 50 | DASH_LOG((BoxName() + " too short").c_str(), 51 | "At least 4 bytes are required", 52 | DumpMemory(buffer, length).c_str()); 53 | return DashParser::kParseFailure; 54 | } 55 | if (IsAuxInfoPresent()) { 56 | if (!EnoughBytesToParse(ptr - buffer, 2 * sizeof(uint32_t), length)) { 57 | DASH_LOG((BoxName() + " too short").c_str(), 58 | "At least 8 more bytes are required for aux info", 59 | DumpMemory(buffer, length).c_str()); 60 | return DashParser::kParseFailure; 61 | } 62 | aux_info_type_ = ntohlFromBuffer(ptr); 63 | ptr += sizeof(aux_info_type_); 64 | aux_info_type_parameter_ = ntohlFromBuffer(ptr); 65 | ptr += sizeof(aux_info_type_parameter_); 66 | } 67 | if (!EnoughBytesToParse(ptr - buffer, sizeof(uint32_t), length)) { 68 | DASH_LOG((BoxName() + " too short").c_str(), 69 | "At least 1 more bytes is required for sample count", 70 | DumpMemory(buffer, length).c_str()); 71 | return DashParser::kParseFailure; 72 | } 73 | offsets_.resize(ntohlFromBuffer(ptr)); 74 | ptr += sizeof(uint32_t); 75 | if (!EnoughBytesToParse(ptr - buffer, sizeof(uint32_t) * offsets_.size(), 76 | length)) { 77 | DASH_LOG((BoxName() + " too short").c_str(), 78 | "Not enough data for samples", 79 | DumpMemory(buffer, length).c_str()); 80 | return DashParser::kParseFailure; 81 | } 82 | for (size_t count = 0; count < offsets_.size(); ++count) { 83 | offsets_[count] = ntohlFromBuffer(ptr); 84 | ptr += sizeof(uint32_t); 85 | } 86 | return ptr - buffer; 87 | } 88 | 89 | std::string SaioContents::PrettyPrint(std::string indent) const { 90 | std::string result = FullBoxContents::PrettyPrint(indent); 91 | if (IsAuxInfoPresent()) { 92 | result += " Type:" + PrettyPrintValue(aux_info_type_); 93 | result += " Parameter:" + PrettyPrintValue(aux_info_type_parameter_); 94 | } 95 | result += " Offsets:" + PrettyPrintValue(offsets_.size()); 96 | if (g_verbose_pretty_print) { 97 | for (uint32_t count = 0; count < offsets_.size(); ++count) { 98 | result += "\n" + indent + " " + PrettyPrintValue(offsets_[count]); 99 | } 100 | } 101 | return result; 102 | } 103 | } // namespace dash2hls 104 | -------------------------------------------------------------------------------- /library/dash/saio_contents.h: -------------------------------------------------------------------------------- 1 | #ifndef _DASH2HLS_SAIO_CONTENTS_H_ 2 | #define _DASH2HLS_SAIO_CONTENTS_H_ 3 | 4 | /* 5 | Copyright 2014 Google Inc. All rights reserved. 6 | 7 | Licensed under the Apache License, Version 2.0 (the "License"); 8 | you may not use this file except in compliance with the License. 9 | You may obtain a copy of the License at 10 | 11 | http://www.apache.org/licenses/LICENSE-2.0 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" BASIS, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | See the License for the specific language governing permissions and 17 | limitations under the License. 18 | */ 19 | 20 | // Samples can have auxiliary information stored in the content. The 21 | // information per sample is stored in either a continuous block or spread 22 | // throughout the data. The entry_count of 1 means continous, the only other 23 | // legal value is the entry_count in the trun. 24 | 25 | #include 26 | #include 27 | 28 | #include "library/dash/box_type.h" 29 | #include "library/dash/full_box_contents.h" 30 | 31 | namespace dash2hls { 32 | 33 | class SaioContents : public FullBoxContents { 34 | public: 35 | explicit SaioContents(uint64_t stream_position) 36 | : FullBoxContents(BoxType::kBox_saio, stream_position) {} 37 | 38 | // Fields are optional. 39 | bool IsAuxInfoPresent() const; 40 | 41 | uint32_t get_aux_info_type() {return aux_info_type_;} 42 | uint32_t get_aux_info_type_parameter() {return aux_info_type_parameter_;} 43 | 44 | const std::vector& get_offsets() const {return offsets_;} 45 | 46 | virtual std::string PrettyPrint(std::string indent) const; 47 | virtual std::string BoxName() const { 48 | return "SampleAuxiliaryInformationOffsetsBox"; 49 | } 50 | 51 | protected: 52 | static const uint32_t kAuxInfoPresentMask = 0x000001; 53 | 54 | protected: 55 | virtual size_t Parse(const uint8_t* buffer, size_t length); 56 | 57 | private: 58 | uint32_t aux_info_type_; 59 | uint32_t aux_info_type_parameter_; 60 | std::vector offsets_; 61 | }; 62 | } // namespace dash2hls 63 | 64 | #endif // _DASH2HLS_SAIO_CONTENTS_H_ 65 | -------------------------------------------------------------------------------- /library/dash/saiz_contents.cc: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2014 Google Inc. All rights reserved. 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | #include "library/dash/saiz_contents.h" 18 | 19 | #include "library/dash/box.h" 20 | #include "library/dash/dash_parser.h" 21 | #include "library/utilities.h" 22 | 23 | namespace dash2hls { 24 | 25 | bool SaizContents::IsAuxInfoPresent() const { 26 | return flags_ & kAuxInfoPresentMask; 27 | } 28 | 29 | // See ISO 14496-12 for details. 30 | // aligned(8) class SampleAuxiliaryInformationSizesBox 31 | // extends FullBox(‘saiz’, version = 0, flags) 32 | // { 33 | // if (flags & 1) { 34 | // unsigned int(32) aux_info_type; 35 | // unsigned int(32) aux_info_type_parameter; 36 | // } 37 | // unsigned int(8) default_sample_info_size; 38 | // unsigned int(32) sample_count; 39 | // if (default_sample_info_size == 0) { 40 | // unsigned int(8) sample_info_size[ sample_count ]; 41 | // } 42 | size_t SaizContents::Parse(const uint8_t *buffer, size_t length) { 43 | const uint8_t* ptr = buffer + FullBoxContents::Parse(buffer, length); 44 | if ((ptr == buffer) || 45 | !EnoughBytesToParse(ptr - buffer, sizeof(uint32_t), length)) { 46 | DASH_LOG((BoxName() + " too short").c_str(), 47 | "At least 4 bytes are required", 48 | DumpMemory(buffer, length).c_str()); 49 | return DashParser::kParseFailure; 50 | } 51 | if (IsAuxInfoPresent()) { 52 | if (!EnoughBytesToParse(ptr - buffer, 2 * sizeof(uint32_t), length)) { 53 | DASH_LOG((BoxName() + " too short").c_str(), 54 | "At least 8 more bytes are required for aux info", 55 | DumpMemory(buffer, length).c_str()); 56 | return DashParser::kParseFailure; 57 | } 58 | aux_info_type_ = ntohlFromBuffer(ptr); 59 | ptr += sizeof(aux_info_type_); 60 | aux_info_type_parameter_ = ntohlFromBuffer(ptr); 61 | ptr += sizeof(aux_info_type_parameter_); 62 | } 63 | if (!EnoughBytesToParse(ptr - buffer, sizeof(uint8_t) + sizeof(uint32_t), 64 | length)) { 65 | DASH_LOG((BoxName() + " too short").c_str(), 66 | "At least 5 more bytes is required for default size and " 67 | "sample count", 68 | DumpMemory(buffer, length).c_str()); 69 | return DashParser::kParseFailure; 70 | } 71 | default_sample_info_size_ = *ptr; 72 | ++ptr; 73 | sizes_.resize(ntohlFromBuffer(ptr)); 74 | ptr += sizeof(uint32_t); 75 | // saiz boxes can either contain records or everything uses the default size. 76 | if (default_sample_info_size_ == 0) { 77 | if (!EnoughBytesToParse(ptr - buffer, sizes_.size(), length)) { 78 | DASH_LOG((BoxName() + " too short").c_str(), 79 | "Not enough data for samples", 80 | DumpMemory(buffer, length).c_str()); 81 | return DashParser::kParseFailure; 82 | } 83 | memcpy(&sizes_[0], ptr, sizes_.size()); 84 | ptr += sizes_.size(); 85 | } else { 86 | for (size_t count = 0; count < sizes_.size(); ++count) { 87 | sizes_[count] = default_sample_info_size_; 88 | } 89 | } 90 | return ptr - buffer; 91 | } 92 | 93 | std::string SaizContents::PrettyPrint(std::string indent) const { 94 | std::string result = FullBoxContents::PrettyPrint(indent); 95 | if (IsAuxInfoPresent()) { 96 | result += " Type:" + PrettyPrintValue(aux_info_type_); 97 | result += " Parameter:" + PrettyPrintValue(aux_info_type_parameter_); 98 | } 99 | result += " Default Size:" + PrettyPrintValue(default_sample_info_size_); 100 | result += " Sizes:" + PrettyPrintValue(sizes_.size()); 101 | if (g_verbose_pretty_print) { 102 | for (uint32_t count = 0; count < sizes_.size(); ++count) { 103 | result += "\n" + indent + " " + PrettyPrintValue(sizes_[count]); 104 | } 105 | } 106 | return result; 107 | } 108 | } // namespace dash2hls 109 | -------------------------------------------------------------------------------- /library/dash/saiz_contents.h: -------------------------------------------------------------------------------- 1 | #ifndef _DASH2HLS_SAIZ_CONTENTS_H_ 2 | #define _DASH2HLS_SAIZ_CONTENTS_H_ 3 | 4 | /* 5 | Copyright 2014 Google Inc. All rights reserved. 6 | 7 | Licensed under the Apache License, Version 2.0 (the "License"); 8 | you may not use this file except in compliance with the License. 9 | You may obtain a copy of the License at 10 | 11 | http://www.apache.org/licenses/LICENSE-2.0 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" BASIS, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | See the License for the specific language governing permissions and 17 | limitations under the License. 18 | */ 19 | 20 | // Samples can have auxiliary information stored in the content. The 21 | // information per sample is stored at the offset described by the saio with 22 | // sample sizes as determined by this box. 23 | // 24 | // Not all samples need auxiliary information but there is no skipping. If 25 | // there are 100 samples and only 10 sizes then the first 10 samples have info. 26 | 27 | #include 28 | #include 29 | 30 | #include "library/dash/box_type.h" 31 | #include "library/dash/full_box_contents.h" 32 | #include "library/utilities.h" 33 | 34 | namespace dash2hls { 35 | 36 | class SaizContents : public FullBoxContents { 37 | public: 38 | enum { 39 | SaizRecordSize = sizeof(uint16_t) + sizeof(uint32_t), 40 | }; 41 | class SaizRecord { 42 | public: 43 | size_t clear_bytes() const { 44 | return ntohsFromBuffer(reinterpret_cast(storage)); 45 | } 46 | size_t encrypted_bytes() const { 47 | return ntohlFromBuffer(reinterpret_cast 48 | (&storage[sizeof(uint16_t)])); 49 | } 50 | uint8_t storage[SaizRecordSize]; 51 | }; 52 | explicit SaizContents(uint64_t stream_position) 53 | : FullBoxContents(BoxType::kBox_saiz, stream_position) {} 54 | 55 | // Fields are optional. 56 | bool IsAuxInfoPresent() const; 57 | 58 | uint32_t get_aux_info_type() {return aux_info_type_;} 59 | uint32_t get_aux_info_type_parameter() {return aux_info_type_parameter_;} 60 | uint32_t get_default_sample_info_size() {return default_sample_info_size_;} 61 | 62 | const std::vector& get_sizes() const {return sizes_;} 63 | 64 | virtual std::string PrettyPrint(std::string indent) const; 65 | virtual std::string BoxName() const { 66 | return "SampleAuxiliaryInformationSizeBox"; 67 | } 68 | 69 | protected: 70 | static const uint32_t kAuxInfoPresentMask = 0x000001; 71 | 72 | protected: 73 | virtual size_t Parse(const uint8_t* buffer, size_t length); 74 | 75 | private: 76 | uint32_t aux_info_type_; 77 | uint32_t aux_info_type_parameter_; 78 | uint8_t default_sample_info_size_; 79 | std::vector sizes_; 80 | }; 81 | } // namespace dash2hls 82 | 83 | #endif // _DASH2HLS_SAIZ_CONTENTS_H_ 84 | -------------------------------------------------------------------------------- /library/dash/sample_entry_contents.cc: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2014 Google Inc. All rights reserved. 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | #include "library/dash/sample_entry_contents.h" 18 | 19 | #include "library/dash/box.h" 20 | #include "library/dash/dash_parser.h" 21 | #include "library/utilities.h" 22 | 23 | namespace dash2hls { 24 | 25 | // See ISO 14495-12 for details. 26 | // 27 | // aligned(8) abstract class SampleEntry (unsigned int(32) format) 28 | // extends Box(format){ 29 | // const unsigned int(8)[6] reserved = 0; 30 | // unsigned int(16) data_reference_index; 31 | // 32 | size_t SampleEntryContents::Parse(const uint8_t *buffer, size_t length) { 33 | const uint8_t* ptr = buffer; 34 | if (length < 6 + sizeof(uint16_t)) { 35 | DASH_LOG((BoxName() + " too short").c_str(), 36 | "At least 6 bytes are required", 37 | DumpMemory(buffer, length).c_str()); 38 | return DashParser::kParseFailure; 39 | } 40 | ptr += sizeof(uint8_t) * 6; // Reserved bytes to skip 41 | data_reference_index_ = ntohsFromBuffer(ptr); 42 | if (ptr + data_reference_index_ > buffer + length) { 43 | DASH_LOG((BoxName() + " too short").c_str(), 44 | "Could not read data_reference_index_", 45 | DumpMemory(buffer, length).c_str()); 46 | return DashParser::kParseFailure; 47 | } 48 | ptr += sizeof(data_reference_index_); 49 | return ptr - buffer; 50 | } 51 | 52 | std::string SampleEntryContents::PrettyPrint(std::string indent) const { 53 | std::string result = " data_reference_index: "; 54 | result += PrettyPrintValue(data_reference_index_); 55 | result += BoxContents::PrettyPrint(indent); 56 | return result; 57 | } 58 | } // namespace dash2hls 59 | -------------------------------------------------------------------------------- /library/dash/sample_entry_contents.h: -------------------------------------------------------------------------------- 1 | #ifndef _DASH2HLS_SAMPLE_ENTRY_CONTENTS_H_ 2 | #define _DASH2HLS_SAMPLE_ENTRY_CONTENTS_H_ 3 | 4 | /* 5 | Copyright 2014 Google Inc. All rights reserved. 6 | 7 | Licensed under the Apache License, Version 2.0 (the "License"); 8 | you may not use this file except in compliance with the License. 9 | You may obtain a copy of the License at 10 | 11 | http://www.apache.org/licenses/LICENSE-2.0 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" BASIS, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | See the License for the specific language governing permissions and 17 | limitations under the License. 18 | */ 19 | 20 | // Sample box containing the codec information. 21 | 22 | #include 23 | 24 | #include "library/dash/box_type.h" 25 | #include "library/dash/box_contents.h" 26 | 27 | namespace dash2hls { 28 | 29 | class SampleEntryContents : public BoxContents { 30 | public: 31 | SampleEntryContents(uint32_t type, uint64_t stream_position) 32 | : BoxContents(type, stream_position) {} 33 | 34 | virtual std::string PrettyPrint(std::string indent) const; 35 | virtual std::string BoxName() const {return "SampleEntry";} 36 | 37 | protected: 38 | virtual size_t Parse(const uint8_t* buffer, size_t length); 39 | 40 | private: 41 | uint16_t data_reference_index_; 42 | }; 43 | } // namespace dash2hls 44 | 45 | #endif // _DASH2HLS_SAMPLE_ENTRY_CONTENTS_H_ 46 | -------------------------------------------------------------------------------- /library/dash/sidx_contents.h: -------------------------------------------------------------------------------- 1 | #ifndef _DASH2HLS_SIDX_CONTENTS_H_ 2 | #define _DASH2HLS_SIDX_CONTENTS_H_ 3 | 4 | /* 5 | Copyright 2014 Google Inc. All rights reserved. 6 | 7 | Licensed under the Apache License, Version 2.0 (the "License"); 8 | you may not use this file except in compliance with the License. 9 | You may obtain a copy of the License at 10 | 11 | http://www.apache.org/licenses/LICENSE-2.0 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" BASIS, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | See the License for the specific language governing permissions and 17 | limitations under the License. 18 | */ 19 | 20 | // A sidx contains the offset and size to find each moof/mdat set of boxes. 21 | 22 | #include 23 | #include 24 | 25 | #include "include/DashToHlsApi.h" 26 | #include "library/dash/box_type.h" 27 | #include "library/dash/full_box_contents.h" 28 | 29 | namespace dash2hls { 30 | 31 | class SidxContents : public FullBoxContents { 32 | public: 33 | explicit SidxContents(uint64_t stream_position) : 34 | FullBoxContents(BoxType::kBox_sidx, stream_position) {} 35 | // Each Reference is one segment of data. 36 | class Reference { 37 | public: 38 | bool segment_index_type_; 39 | uint32_t size_; 40 | uint32_t subsegment_duration_; 41 | bool starts_with_sap_; 42 | uint8_t sap_type_; 43 | uint32_t sap_delta_time; 44 | }; 45 | const std::vector& get_locations() const { 46 | return locations_; 47 | } 48 | virtual std::string PrettyPrint(std::string indent) const; 49 | virtual std::string BoxName() const {return "SegmentIndex";} 50 | uint32_t get_timescale() const {return timescale_;} 51 | 52 | protected: 53 | virtual size_t Parse(const uint8_t* buffer, size_t length); 54 | 55 | private: 56 | uint32_t reference_id_; 57 | uint32_t timescale_; 58 | uint64_t earliest_presentation_time_; 59 | uint64_t first_offset_; 60 | uint16_t reference_count_; 61 | std::vector references_; 62 | std::vector locations_; 63 | }; 64 | } // namespace dash2hls 65 | 66 | #endif // _DASH2HLS_SIDX_CONTENTS_H_ 67 | -------------------------------------------------------------------------------- /library/dash/stsd_contents.cc: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2014 Google Inc. All rights reserved. 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | #include "library/dash/stsd_contents.h" 18 | 19 | #include "library/dash/box.h" 20 | #include "library/dash/dash_parser.h" 21 | #include "library/utilities.h" 22 | 23 | namespace dash2hls { 24 | 25 | // See ISO 14496-12 for details. 26 | // aligned(8) class SampleDescriptionBox (unsigned int(32) handler_type) 27 | // extends FullBox('stsd', 0, 0){ 28 | // int i ; 29 | // unsigned int(32) entry_count; 30 | // for (i = 1 ; i <= entry_count ; i++){ 31 | // switch (handler_type){ 32 | // case ‘soun’: // for audio tracks 33 | // AudioSampleEntry(); 34 | // break; 35 | // case ‘vide’: // for video tracks 36 | // VisualSampleEntry(); 37 | // break; 38 | // case ‘hint’: // Hint track 39 | // HintSampleEntry(); 40 | // break; 41 | // case ‘meta’: // Metadata track 42 | // MetadataSampleEntry(); 43 | // break; 44 | // } 45 | // } 46 | // } 47 | size_t StsdContents::Parse(const uint8_t *buffer, size_t length) { 48 | const uint8_t* ptr = buffer + FullBoxContents::Parse(buffer, length); 49 | if ((ptr == buffer) || 50 | !EnoughBytesToParse(ptr - buffer, sizeof(uint32_t), length)) { 51 | DASH_LOG((BoxName() + " too short").c_str(), 52 | "At least 4 bytes are required", 53 | DumpMemory(buffer, length).c_str()); 54 | return DashParser::kParseFailure; 55 | } 56 | entry_count_ = ntohlFromBuffer(ptr); 57 | ptr += sizeof(entry_count_); 58 | size_t bytes_parsed = BoxContents::Parse(ptr, length - (ptr - buffer)); 59 | if (bytes_parsed == 0) { 60 | return DashParser::kParseFailure; 61 | } 62 | ptr += bytes_parsed; 63 | return ptr - buffer; 64 | } 65 | 66 | std::string StsdContents::PrettyPrint(std::string indent) const { 67 | std::string result = FullBoxContents::PrettyPrint(indent); 68 | result += " descriptions: " + PrettyPrintValue(entry_count_); 69 | result += BoxContents::PrettyPrint(indent); 70 | return result; 71 | } 72 | } // namespace dash2hls 73 | -------------------------------------------------------------------------------- /library/dash/stsd_contents.h: -------------------------------------------------------------------------------- 1 | #ifndef _DASH2HLS_STSD_CONTENTS_H_ 2 | #define _DASH2HLS_STSD_CONTENTS_H_ 3 | 4 | /* 5 | Copyright 2014 Google Inc. All rights reserved. 6 | 7 | Licensed under the Apache License, Version 2.0 (the "License"); 8 | you may not use this file except in compliance with the License. 9 | You may obtain a copy of the License at 10 | 11 | http://www.apache.org/licenses/LICENSE-2.0 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" BASIS, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | See the License for the specific language governing permissions and 17 | limitations under the License. 18 | */ 19 | 20 | // Sample box containing the codec information. 21 | 22 | #include 23 | 24 | #include "library/dash/box_type.h" 25 | #include "library/dash/full_box_contents.h" 26 | 27 | namespace dash2hls { 28 | 29 | class StsdContents : public FullBoxContents { 30 | public: 31 | explicit StsdContents(uint64_t stream_position) 32 | : FullBoxContents(BoxType::kBox_stsd, stream_position) {} 33 | virtual std::string PrettyPrint(std::string indent) const; 34 | virtual std::string BoxName() const {return "SampleDescription";} 35 | 36 | protected: 37 | virtual size_t Parse(const uint8_t* buffer, size_t length); 38 | 39 | private: 40 | uint32_t entry_count_; 41 | }; 42 | } // namespace dash2hls 43 | 44 | #endif // _DASH2HLS_SAMPLE_ENTRY_CONTENTS_H_ 45 | -------------------------------------------------------------------------------- /library/dash/stsz_contents.cc: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2014 Google Inc. All rights reserved. 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | #include "library/dash/stsz_contents.h" 18 | 19 | #include 20 | 21 | #include "library/dash/box.h" 22 | #include "library/dash/dash_parser.h" 23 | #include "library/utilities.h" 24 | 25 | namespace dash2hls { 26 | 27 | // See ISO 14496-12 for details. 28 | // aligned(8) class SampleSizeBox extends FullBox(‘stsz’, version = 0, 0) { 29 | // unsigned int(32) sample_size; 30 | // unsigned int(32) sample_count; 31 | // if (sample_size==0) { 32 | // for (i=1; i <= sample_count; i++) { 33 | // unsigned int(32) entry_size; 34 | // } 35 | // } 36 | // } 37 | size_t StszContents::Parse(const uint8_t* buffer, size_t length) { 38 | const uint8_t* ptr = buffer + FullBoxContents::Parse(buffer, length); 39 | if ((ptr == buffer) || 40 | !EnoughBytesToParse(ptr - buffer, 2 * sizeof(uint32_t), length)) { 41 | DASH_LOG((BoxName() + " too short").c_str(), 42 | "At least 8 bytes are required", 43 | DumpMemory(buffer, length).c_str()); 44 | return DashParser::kParseFailure; 45 | } 46 | sample_size_ = ntohlFromBuffer(ptr); 47 | ptr += sizeof(sample_size_); 48 | sample_count_ = ntohlFromBuffer(ptr); 49 | ptr += sizeof(sample_count_); 50 | for (uint32_t count = 0; count < sample_count_; ++count) { 51 | if (!EnoughBytesToParse(ptr - buffer, sizeof(uint32_t), length)) { 52 | char buffer[1024]; 53 | snprintf(buffer, sizeof(buffer), 54 | "Not enough bytes to parse sample %d of %d\n", count, 55 | sample_count_); 56 | DASH_LOG((BoxName() + " too short").c_str(), 57 | "Could not parse sample_count_", 58 | (std::string(buffer) + 59 | DumpMemory(reinterpret_cast(buffer), 60 | length)).c_str()); 61 | return DashParser::kParseFailure; 62 | } 63 | uint32_t sample_size = ntohlFromBuffer(ptr); 64 | samples_sizes_.push_back(sample_size); 65 | ptr += sizeof(sample_size); 66 | } 67 | return ptr - buffer; 68 | } 69 | 70 | std::string StszContents::PrettyPrint(std::string indent) const { 71 | std::string result = FullBoxContents::PrettyPrint(indent); 72 | result += " samples: " + PrettyPrintValue(sample_count_); 73 | for (std::vector::const_iterator iter = samples_sizes_.begin(); 74 | iter != samples_sizes_.end(); ++iter) { 75 | result += "\n" + indent + " " + PrettyPrintValue(*iter); 76 | } 77 | return result; 78 | } 79 | } // namespace dash2hls 80 | -------------------------------------------------------------------------------- /library/dash/stsz_contents.h: -------------------------------------------------------------------------------- 1 | #ifndef _DASH2HLS_STSZ_CONTENTS_H_ 2 | #define _DASH2HLS_STSZ_CONTENTS_H_ 3 | 4 | /* 5 | Copyright 2014 Google Inc. All rights reserved. 6 | 7 | Licensed under the Apache License, Version 2.0 (the "License"); 8 | you may not use this file except in compliance with the License. 9 | You may obtain a copy of the License at 10 | 11 | http://www.apache.org/licenses/LICENSE-2.0 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" BASIS, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | See the License for the specific language governing permissions and 17 | limitations under the License. 18 | */ 19 | 20 | // Sample Table Box contains a list of all samples. 21 | 22 | #include 23 | #include 24 | 25 | #include "library/dash/box_type.h" 26 | #include "library/dash/full_box_contents.h" 27 | 28 | namespace dash2hls { 29 | 30 | class StszContents : public FullBoxContents { 31 | public: 32 | explicit StszContents(uint64_t stream_position) 33 | : FullBoxContents(BoxType::kBox_stsz, stream_position) {} 34 | virtual std::string PrettyPrint(std::string indent) const; 35 | virtual std::string BoxName() const {return "SampleTable";} 36 | 37 | protected: 38 | virtual size_t Parse(const uint8_t* buffer, size_t length); 39 | 40 | private: 41 | uint32_t sample_size_; // Default sample size 42 | uint32_t sample_count_; 43 | std::vector samples_sizes_; 44 | }; 45 | } // namespace dash2hls 46 | 47 | #endif // _DASH2HLS_STSZ_CONTENTS_H_ 48 | -------------------------------------------------------------------------------- /library/dash/tenc_contents.cc: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2014 Google Inc. All rights reserved. 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | #include "library/dash/tenc_contents.h" 18 | 19 | #include "library/dash/box.h" 20 | #include "library/dash/dash_parser.h" 21 | #include "library/utilities.h" 22 | 23 | namespace dash2hls { 24 | 25 | // See ISO 23001-7 for details. 26 | // aligned(8) class TrackEncryptionBox extends FullBox(‘tenc’, version=0, 27 | // flags=0) 28 | // { 29 | // unsigned int(24) default_IsEncrypted; 30 | // unsigned int(8) default_IV_size; 31 | // unsigned int(8)[16] default_KID; 32 | // } 33 | size_t TencContents::Parse(const uint8_t* buffer, size_t length) { 34 | const uint8_t* ptr = buffer + FullBoxContents::Parse(buffer, length); 35 | if ((ptr == buffer) || 36 | !EnoughBytesToParse(ptr - buffer, 37 | kDefaultIsEncryptedSize + sizeof(uint8_t) + 38 | kKidSize, length)) { 39 | DASH_LOG((BoxName() + " too short").c_str(), 40 | "At least 20 bytes are required", 41 | DumpMemory(buffer, length).c_str()); 42 | return DashParser::kParseFailure; 43 | } 44 | default_is_encrypted_ = ntohlFromBuffer(ptr) >> kDefaultShift; 45 | ptr += kDefaultIsEncryptedSize; 46 | default_iv_size_ = *ptr; 47 | ++ptr; 48 | memcpy(default_kid_, ptr, kKidSize); 49 | ptr += kKidSize; 50 | return ptr - buffer; 51 | } 52 | 53 | std::string TencContents::PrettyPrint(std::string indent) const { 54 | std::string result = FullBoxContents::PrettyPrint(indent); 55 | result += " IsEncrypted: " + PrettyPrintValue(default_is_encrypted_); 56 | result += " IV size:" + PrettyPrintValue(default_iv_size_); 57 | result += " Default KID:" + PrettyPrintBuffer(default_kid_, kKidSize); 58 | return result; 59 | } 60 | } // namespace dash2hls 61 | -------------------------------------------------------------------------------- /library/dash/tenc_contents.h: -------------------------------------------------------------------------------- 1 | #ifndef _DASH2HLS_TENC_CONTENTS_H_ 2 | #define _DASH2HLS_TENC_CONTENTS_H_ 3 | 4 | /* 5 | Copyright 2014 Google Inc. All rights reserved. 6 | 7 | Licensed under the Apache License, Version 2.0 (the "License"); 8 | you may not use this file except in compliance with the License. 9 | You may obtain a copy of the License at 10 | 11 | http://www.apache.org/licenses/LICENSE-2.0 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" BASIS, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | See the License for the specific language governing permissions and 17 | limitations under the License. 18 | */ 19 | 20 | // Sample Table Box contains a list of all samples. 21 | 22 | #include 23 | #include 24 | 25 | #include "library/dash/box_type.h" 26 | #include "library/dash/full_box_contents.h" 27 | 28 | namespace dash2hls { 29 | 30 | class TencContents : public FullBoxContents { 31 | public: 32 | enum { 33 | kKidSize = 16, 34 | kDefaultShift = 8, 35 | kDefaultIsEncryptedSize = 3, 36 | }; 37 | explicit TencContents(uint64_t stream_position) 38 | : FullBoxContents(BoxType::kBox_stsz, stream_position) {} 39 | virtual std::string PrettyPrint(std::string indent) const; 40 | virtual std::string BoxName() const {return "Track Encryption Box";} 41 | size_t get_default_iv_size() const {return default_iv_size_;} 42 | const uint8_t* get_default_kid() const {return default_kid_;} 43 | 44 | protected: 45 | virtual size_t Parse(const uint8_t* buffer, size_t length); 46 | 47 | private: 48 | uint32_t default_is_encrypted_; 49 | size_t default_iv_size_; 50 | uint8_t default_kid_[kKidSize]; 51 | }; 52 | } // namespace dash2hls 53 | 54 | #endif // _DASH2HLS_TENC_CONTENTS_H_ 55 | -------------------------------------------------------------------------------- /library/dash/tfdt_contents.cc: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2014 Google Inc. All rights reserved. 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | #include "library/dash/tfdt_contents.h" 18 | 19 | #include "library/dash/box.h" 20 | #include "library/dash/dash_parser.h" 21 | #include "library/utilities.h" 22 | 23 | namespace dash2hls { 24 | 25 | // Currently only parses version 1 of a sidx box. 26 | // 27 | // See ISO 14496-12 for details. 28 | // aligned(8) class TrackFragmentBaseMediaDecodeTimeBox 29 | // extends FullBox(‘tfdt’, version, 0) { 30 | // if (version==1) { 31 | // unsigned int(64) baseMediaDecodeTime; 32 | // } else { // version==0 33 | // unsigned int(32) baseMediaDecodeTime; 34 | // } 35 | // } 36 | size_t TfdtContents::Parse(const uint8_t* buffer, size_t length) { 37 | const uint8_t* ptr = buffer + FullBoxContents::Parse(buffer, length); 38 | if ((ptr == buffer) || 39 | !EnoughBytesToParse(ptr - buffer, sizeof(uint32_t), length)) { 40 | DASH_LOG((BoxName() + " too short").c_str(), 41 | "At least 4 bytes are required", 42 | DumpMemory(buffer, length).c_str()); 43 | return DashParser::kParseFailure; 44 | } 45 | if (version_ == kVersion0) { 46 | base_media_decode_time_ = ntohlFromBuffer(buffer + 4); 47 | } else { 48 | base_media_decode_time_ = ntohllFromBuffer(buffer + 4); 49 | } 50 | return ptr - buffer; 51 | } 52 | 53 | std::string TfdtContents::PrettyPrint(std::string indent) const { 54 | std::string result = FullBoxContents::PrettyPrint(indent); 55 | result += " Base Media Decode Time: " + 56 | PrettyPrintValue(base_media_decode_time_); 57 | return result; 58 | } 59 | } // namespace dash2hls 60 | -------------------------------------------------------------------------------- /library/dash/tfdt_contents.h: -------------------------------------------------------------------------------- 1 | #ifndef _DASH2HLS_TFDT_CONTENTS_H_ 2 | #define _DASH2HLS_TFDT_CONTENTS_H_ 3 | 4 | /* 5 | Copyright 2014 Google Inc. All rights reserved. 6 | 7 | Licensed under the Apache License, Version 2.0 (the "License"); 8 | you may not use this file except in compliance with the License. 9 | You may obtain a copy of the License at 10 | 11 | http://www.apache.org/licenses/LICENSE-2.0 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" BASIS, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | See the License for the specific language governing permissions and 17 | limitations under the License. 18 | */ 19 | 20 | // The tfdt box contains the absolute media decode time. 21 | 22 | #include 23 | 24 | #include "library/dash/box_type.h" 25 | #include "library/dash/full_box_contents.h" 26 | 27 | namespace dash2hls { 28 | 29 | class TfdtContents : public FullBoxContents { 30 | public: 31 | explicit TfdtContents(uint64_t stream_position) 32 | : FullBoxContents(BoxType::kBox_tfdt, stream_position) {} 33 | 34 | uint64_t get_base_media_decode_time() const {return base_media_decode_time_;} 35 | virtual std::string PrettyPrint(std::string indent) const; 36 | virtual std::string BoxName() const {return "TrackFragment";} 37 | 38 | protected: 39 | virtual size_t Parse(const uint8_t* buffer, size_t length); 40 | 41 | private: 42 | uint64_t base_media_decode_time_; 43 | }; 44 | } // namespace dash2hls 45 | 46 | #endif // _DASH2HLS_TFDT_CONTENTS_H_ 47 | -------------------------------------------------------------------------------- /library/dash/tfhd_contents.h: -------------------------------------------------------------------------------- 1 | #ifndef _DASH2HLS_TFHD_CONTENTS_H_ 2 | #define _DASH2HLS_TFHD_CONTENTS_H_ 3 | 4 | /* 5 | Copyright 2014 Google Inc. All rights reserved. 6 | 7 | Licensed under the Apache License, Version 2.0 (the "License"); 8 | you may not use this file except in compliance with the License. 9 | You may obtain a copy of the License at 10 | 11 | http://www.apache.org/licenses/LICENSE-2.0 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" BASIS, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | See the License for the specific language governing permissions and 17 | limitations under the License. 18 | */ 19 | 20 | // A TrackFragmentHeader sets default values for a run. The trun box can 21 | // specify these values on a sample by sample basis. To save space a value 22 | // set in this box can apply to every run. 23 | 24 | #include 25 | 26 | #include "library/dash/box_type.h" 27 | #include "library/dash/full_box_contents.h" 28 | 29 | namespace dash2hls { 30 | 31 | class TfhdContents : public FullBoxContents { 32 | public: 33 | explicit TfhdContents(uint64_t stream_position) 34 | : FullBoxContents(BoxType::kBox_tfhd, stream_position) {} 35 | 36 | bool IsBaseDataOffsetPresent() const { 37 | return flags_ & kBaseDataOffsetPresentMask; 38 | } 39 | bool IsSampleDescriptionIndexPresent() const { 40 | return flags_ & kSampleDescriptionIndexPresentMask; 41 | } 42 | bool IsDefaultSampleDurationPresent() const { 43 | return flags_ & kDefaultSampleDurationPresentMask; 44 | } 45 | bool IsDefaultSampleSizePresent() const { 46 | return flags_ & kDefaultSampleSizePresentMask; 47 | } 48 | bool IsDefaultSampleFlagsPresent() const { 49 | return flags_ & kDefaultSampleFlagsPresentMask; 50 | } 51 | 52 | uint64_t get_base_data_offset() const { 53 | return base_data_offset_; 54 | } 55 | uint32_t get_sample_description_index() const { 56 | return sample_description_index_; 57 | } 58 | uint32_t get_default_sample_duration() const { 59 | return default_sample_duration_; 60 | } 61 | uint32_t get_default_sample_size() const { 62 | return default_sample_size_; 63 | } 64 | uint32_t get_default_sample_flags() const { 65 | return default_sample_flags_; 66 | } 67 | 68 | virtual std::string PrettyPrint(std::string indent) const; 69 | virtual std::string BoxName() const {return "TrackFragmentHeader";} 70 | 71 | protected: 72 | static const uint32_t kBaseDataOffsetPresentMask = 0x000001; 73 | static const uint32_t kSampleDescriptionIndexPresentMask = 0x000002; 74 | static const uint32_t kDefaultSampleDurationPresentMask = 0x000008; 75 | static const uint32_t kDefaultSampleSizePresentMask = 0x000010; 76 | static const uint32_t kDefaultSampleFlagsPresentMask = 0x000020; 77 | static const uint32_t kDurationIsEmptyMask = 0x010000; 78 | static const uint32_t kDefaultBaseIsMoofMask = 0x020000; 79 | 80 | virtual size_t Parse(const uint8_t* buffer, size_t length); 81 | 82 | private: 83 | uint32_t track_id_; 84 | uint64_t base_data_offset_; 85 | uint32_t sample_description_index_; 86 | uint32_t default_sample_duration_; 87 | uint32_t default_sample_size_; 88 | uint32_t default_sample_flags_; // TODO(justsomeguy) parse sample flags 89 | }; 90 | } // namespace dash2hls 91 | 92 | #endif // _DASH2HLS_TFHD_CONTENTS_H_ 93 | -------------------------------------------------------------------------------- /library/dash/trex_contents.cc: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2014 Google Inc. All rights reserved. 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | #include "library/dash/trex_contents.h" 18 | 19 | #include 20 | 21 | #include "library/dash/box.h" 22 | #include "library/dash/dash_parser.h" 23 | #include "library/utilities.h" 24 | 25 | namespace dash2hls { 26 | 27 | // See ISO 14496-12 for details. 28 | // aligned(8) class TrackExtendsBox extends FullBox(‘trex’, 0, 0){ 29 | // unsigned int(32) track_ID; 30 | // unsigned int(32) default_sample_description_index; 31 | // unsigned int(32) default_sample_duration; 32 | // unsigned int(32) default_sample_size; 33 | // unsigned int(32) default_sample_flags 34 | // } 35 | size_t TrexContents::Parse(const uint8_t* buffer, size_t length) { 36 | const uint8_t* ptr = buffer + FullBoxContents::Parse(buffer, length); 37 | if ((ptr == buffer) || 38 | !EnoughBytesToParse(ptr - buffer, 5 * sizeof(uint32_t), length)) { 39 | DASH_LOG((BoxName() + " too short").c_str(), 40 | "At least 20 bytes are required", 41 | DumpMemory(buffer, length).c_str()); 42 | return DashParser::kParseFailure; 43 | } 44 | track_id_ = ntohlFromBuffer(ptr); 45 | ptr += sizeof(track_id_); 46 | default_sample_description_index_ = ntohlFromBuffer(ptr); 47 | ptr += sizeof(default_sample_description_index_); 48 | default_sample_duration_ = ntohlFromBuffer(ptr); 49 | ptr += sizeof(default_sample_duration_); 50 | default_sample_size_ = ntohlFromBuffer(ptr); 51 | ptr += sizeof(default_sample_size_); 52 | default_sample_flags_ = ntohlFromBuffer(ptr); 53 | ptr += sizeof(default_sample_flags_); 54 | return ptr - buffer; 55 | } 56 | 57 | std::string TrexContents::PrettyPrint(std::string indent) const { 58 | std::string result = FullBoxContents::PrettyPrint(indent); 59 | result += " Track ID:" + PrettyPrintValue(track_id_); 60 | result += " Default Sample Description Index:" + 61 | PrettyPrintValue(default_sample_description_index_); 62 | result += " Default Duration:" + 63 | PrettyPrintValue(default_sample_duration_); 64 | result += " Default Size:" + PrettyPrintValue(default_sample_size_); 65 | result += " Default Flags:" + PrettyPrintValue(default_sample_flags_); 66 | return result; 67 | } 68 | } // namespace dash2hls 69 | -------------------------------------------------------------------------------- /library/dash/trex_contents.h: -------------------------------------------------------------------------------- 1 | #ifndef _DASH2HLS_TREX_CONTENTS_H_ 2 | #define _DASH2HLS_TREX_CONTENTS_H_ 3 | 4 | /* 5 | Copyright 2014 Google Inc. All rights reserved. 6 | 7 | Licensed under the Apache License, Version 2.0 (the "License"); 8 | you may not use this file except in compliance with the License. 9 | You may obtain a copy of the License at 10 | 11 | http://www.apache.org/licenses/LICENSE-2.0 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" BASIS, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | See the License for the specific language governing permissions and 17 | limitations under the License. 18 | */ 19 | 20 | // A TrackExtends sets default values for all movie fragments. 21 | 22 | #include 23 | 24 | #include "library/dash/box_type.h" 25 | #include "library/dash/full_box_contents.h" 26 | 27 | namespace dash2hls { 28 | 29 | class TrexContents : public FullBoxContents { 30 | public: 31 | explicit TrexContents(uint64_t stream_position) 32 | : FullBoxContents(BoxType::kBox_trex, stream_position) {} 33 | 34 | uint32_t get_track_id() const { 35 | return track_id_; 36 | } 37 | uint32_t get_default_sample_description_index() const { 38 | return default_sample_description_index_; 39 | } 40 | uint32_t get_default_sample_duration() const { 41 | return default_sample_duration_; 42 | } 43 | uint32_t get_default_sample_size() const { 44 | return default_sample_size_; 45 | } 46 | uint32_t get_default_sample_flags() const { 47 | return default_sample_flags_; 48 | } 49 | 50 | virtual std::string PrettyPrint(std::string indent) const; 51 | virtual std::string BoxName() const { 52 | return "TrackExtends"; 53 | } 54 | 55 | protected: 56 | virtual size_t Parse(const uint8_t* buffer, size_t length); 57 | 58 | private: 59 | uint32_t track_id_; 60 | uint32_t default_sample_description_index_; 61 | uint32_t default_sample_duration_; 62 | uint32_t default_sample_size_; 63 | uint32_t default_sample_flags_; 64 | }; 65 | } // namespace dash2hls 66 | 67 | #endif // _DASH2HLS_TREX_CONTENTS_H_ 68 | -------------------------------------------------------------------------------- /library/dash/trun_contents.h: -------------------------------------------------------------------------------- 1 | #ifndef _DASH2HLS_TRUN_CONTENTS_H_ 2 | #define _DASH2HLS_TRUN_CONTENTS_H_ 3 | 4 | /* 5 | Copyright 2014 Google Inc. All rights reserved. 6 | 7 | Licensed under the Apache License, Version 2.0 (the "License"); 8 | you may not use this file except in compliance with the License. 9 | You may obtain a copy of the License at 10 | 11 | http://www.apache.org/licenses/LICENSE-2.0 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" BASIS, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | See the License for the specific language governing permissions and 17 | limitations under the License. 18 | */ 19 | 20 | // TrackRun boxes contain the offsets to each sample in an mdat. 21 | // 22 | // Each field is optional but all runs either have the field or don't. 23 | 24 | #include 25 | #include 26 | 27 | #include "library/dash/box_type.h" 28 | #include "library/dash/full_box_contents.h" 29 | 30 | namespace dash2hls { 31 | 32 | class TrunContents : public FullBoxContents { 33 | public: 34 | // Each value is optional and unitialized if not present. 35 | class TrackRun { 36 | public: 37 | uint32_t sample_duration_; 38 | uint32_t sample_size_; 39 | uint32_t sample_flags_; 40 | uint32_t sample_composition_time_offset_; 41 | }; 42 | 43 | explicit TrunContents(uint64_t stream_position) 44 | : FullBoxContents(BoxType::kBox_trex, stream_position) {} 45 | 46 | // Fields are optional. 47 | bool IsBaseDataOffsetPresent() const; 48 | bool IsFirstSampleFlagsPresent() const; 49 | bool IsSampleDurationPresent() const; 50 | bool IsSampleSizePresent() const; 51 | bool IsSampleFlagsPresent() const; 52 | bool IsSampleCompositionPresent() const; 53 | 54 | const std::vector& get_track_runs() const {return track_runs_;} 55 | int32_t get_data_offset() const {return data_offset_;} 56 | 57 | virtual std::string PrettyPrint(std::string indent) const; 58 | std::string PrettyPrintTrackRun(const TrackRun& run) const; 59 | virtual std::string BoxName() const {return "TrackFragmentRun";} 60 | 61 | protected: 62 | static const uint32_t kBaseDataOffsetPresentMask = 0x000001; 63 | static const uint32_t kFirstSampleFlagsPresentMask = 0x000004; 64 | static const uint32_t kSampleDurationPresentMask = 0x0000100; 65 | static const uint32_t kSampleSizePresentMask = 0x000200; 66 | static const uint32_t kSampleFlagsPresentMask = 0x000400; 67 | static const uint32_t kSampleCompositionPresentMask = 0x00800; 68 | 69 | protected: 70 | virtual size_t Parse(const uint8_t* buffer, size_t length); 71 | 72 | private: 73 | uint32_t sample_count_; 74 | int32_t data_offset_; 75 | uint32_t first_sample_flags_; 76 | std::vector track_runs_; 77 | }; 78 | } // namespace dash2hls 79 | 80 | #endif // _DASH2HLS_TRUN_CONTENTS_H_ 81 | -------------------------------------------------------------------------------- /library/dash/unknown_contents.cc: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2014 Google Inc. All rights reserved. 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | #include "library/dash/unknown_contents.h" 18 | 19 | #include 20 | 21 | namespace dash2hls { 22 | 23 | size_t UnknownContents::Parse(const uint8_t* buffer, size_t length) { 24 | bytes_ignored_ += length; 25 | return length; 26 | } 27 | } // namespace dash2hls 28 | -------------------------------------------------------------------------------- /library/dash/unknown_contents.h: -------------------------------------------------------------------------------- 1 | #ifndef _DASH2HLS_UNKNOWN_CONTENTS_H_ 2 | #define _DASH2HLS_UNKNOWN_CONTENTS_H_ 3 | 4 | /* 5 | Copyright 2014 Google Inc. All rights reserved. 6 | 7 | Licensed under the Apache License, Version 2.0 (the "License"); 8 | you may not use this file except in compliance with the License. 9 | You may obtain a copy of the License at 10 | 11 | http://www.apache.org/licenses/LICENSE-2.0 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" BASIS, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | See the License for the specific language governing permissions and 17 | limitations under the License. 18 | */ 19 | 20 | // Not really unknown, more an irrelevant Box not needed for turning DASH 21 | // into HLS. See box_contents.h for comments. 22 | 23 | #include 24 | 25 | #include "library/dash/box_contents.h" 26 | 27 | namespace dash2hls { 28 | 29 | class UnknownContents : public BoxContents { 30 | public: 31 | explicit UnknownContents(uint64_t position) : BoxContents(0, position), 32 | bytes_ignored_(0) { 33 | } 34 | virtual std::string BoxName() const {return "Unknown";} 35 | 36 | protected: 37 | virtual size_t Parse(const uint8_t* buffer, size_t length); 38 | 39 | private: 40 | size_t bytes_ignored_; 41 | }; 42 | } // namespace dash2hls 43 | 44 | #endif // _DASH2HLS_UNKNOWN_CONTENTS_H_ 45 | -------------------------------------------------------------------------------- /library/dash/video_sample_entry_contents.h: -------------------------------------------------------------------------------- 1 | #ifndef _DASH2HLS_VIDEO_SAMPLE_ENTRY_CONTENTS_H_ 2 | #define _DASH2HLS_VIDEO_SAMPLE_ENTRY_CONTENTS_H_ 3 | 4 | /* 5 | Copyright 2014 Google Inc. All rights reserved. 6 | 7 | Licensed under the Apache License, Version 2.0 (the "License"); 8 | you may not use this file except in compliance with the License. 9 | You may obtain a copy of the License at 10 | 11 | http://www.apache.org/licenses/LICENSE-2.0 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" BASIS, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | See the License for the specific language governing permissions and 17 | limitations under the License. 18 | */ 19 | 20 | // Sample box containing the codec information. 21 | 22 | #include 23 | 24 | #include "library/dash/sample_entry_contents.h" 25 | 26 | namespace dash2hls { 27 | 28 | class VideoSampleEntryContents : public SampleEntryContents { 29 | public: 30 | VideoSampleEntryContents(uint32_t type, uint64_t stream_position) 31 | : SampleEntryContents(type, stream_position) {} 32 | virtual std::string PrettyPrint(std::string indent) const; 33 | virtual std::string BoxName() const {return "SampleEntry";} 34 | 35 | protected: 36 | virtual size_t Parse(const uint8_t* buffer, size_t length); 37 | 38 | private: 39 | enum { 40 | kCompressorLength = 32, 41 | }; 42 | uint16_t width_; 43 | uint16_t height_; 44 | uint16_t horiz_resolution_[2]; 45 | uint16_t vert_resolution_[2]; 46 | uint16_t frame_count_; 47 | char compressor_name_[kCompressorLength]; 48 | uint16_t depth_; 49 | }; 50 | } // namespace dash2hls 51 | 52 | #endif // _DASH2HLS_VIDEO_SAMPLE_CONTENTS_H_ 53 | -------------------------------------------------------------------------------- /library/dash_to_hls_api_avframework.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2014 Google Inc. All rights reserved. 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | #ifndef DASHTOHLS_DASHTOHLS_API_AVFRAMEWORK_H_ 18 | #define DASHTOHLS_DASHTOHLS_API_AVFRAMEWORK_H_ 19 | 20 | #include 21 | 22 | struct DashToHlsSession; 23 | 24 | namespace dash2hls { 25 | // Have we set up a key to cause reencryption? 26 | bool is_encrypting(); 27 | // Encrypt one segment with the global key or return false if encryption 28 | // failed. 29 | bool Encrypt(const DashToHlsSession* session, std::vector* segment); 30 | } // namespace dash2hls 31 | 32 | #endif // DASHTOHLS_DASHTOHLS_API_AVFRAMEWORK_H_ 33 | 34 | -------------------------------------------------------------------------------- /library/dash_to_hls_default_diagnostic_callback.mm: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2014 Google Inc. All rights reserved. 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | namespace dash2hls { 18 | void DashToHlsDefaultDiagnosticCallback(const char* message) { 19 | NSLog(@"%s", message); 20 | } 21 | } // namespace dash2hls 22 | -------------------------------------------------------------------------------- /library/dash_to_hls_session.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2014 Google Inc. All rights reserved. 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | #ifndef DASHTOHLS_DASHTOHLS_SESSION_H_ 18 | #define DASHTOHLS_DASHTOHLS_SESSION_H_ 19 | 20 | #include 21 | 22 | #include "include/DashToHlsApi.h" 23 | #include "library/dash/dash_parser.h" 24 | #include "library/dash/tenc_contents.h" 25 | 26 | namespace dash2hls { 27 | // Internal Session object. Tracks all information used by the calls. 28 | class Session { 29 | public: 30 | Session() : 31 | is_video_(false), is_encrypted_(false), pssh_handler_(nullptr), 32 | decryption_handler_(nullptr), default_iv_size_(0), nalu_length_(0), 33 | audio_object_type_(0), sampling_frequency_index_(0), channel_config_(0), 34 | pssh_context_(nullptr), decryption_context_(nullptr), timescale_(0), 35 | trex_default_sample_duration_(0) { 36 | } 37 | bool is_video_; 38 | DashParser parser_; 39 | DashToHlsIndex index_; 40 | bool is_encrypted_; 41 | CENC_PsshHandler pssh_handler_; 42 | CENC_DecryptionHandler decryption_handler_; 43 | std::vector reencryption_key; 44 | size_t default_iv_size_; 45 | 46 | std::map > output_; 47 | 48 | // Video specific settings. 49 | std::vector sps_pps_; 50 | size_t nalu_length_; 51 | 52 | // Audio specific settings. 53 | uint8_t audio_object_type_; 54 | uint8_t sampling_frequency_index_; 55 | uint8_t channel_config_; 56 | uint8_t audio_config_[2]; 57 | DashToHlsContext pssh_context_; 58 | DashToHlsContext decryption_context_; 59 | uint64_t timescale_; 60 | uint8_t key_id_[TencContents::kKidSize]; 61 | uint64_t trex_default_sample_duration_; 62 | }; 63 | } // namespace dash2hls 64 | 65 | #endif // DASHTOHLS_DASHTOHLS_SESSION_H_ 66 | -------------------------------------------------------------------------------- /library/mac_test_files.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2014 Google Inc. All rights reserved. 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | // Test FILE objects. On the Mac there is special code to find the resource. 18 | // Not in a namespace because it might be included by ObjC code. 19 | 20 | #ifndef _DASH2HLS_MAC_TEST_FILES_H_ 21 | #define _DASH2HLS_MAC_TEST_FILES_H_ 22 | 23 | #include 24 | 25 | FILE* Dash2HLS_GetTestVideoFile(); 26 | FILE* Dash2HLS_GetTestAudioFile(); 27 | 28 | FILE* Dash2HLS_GetTestCencVideoFile(); 29 | FILE* Dash2HLS_GetTestCencAudioFile(); 30 | 31 | FILE* Dash2HLS_GetTestCencVideoHeader(); 32 | FILE* Dash2HLS_GetTestCencAudioHeader(); 33 | 34 | FILE* Dash2HLS_GetTestCencVideoSegment(); 35 | FILE* Dash2HLS_GetTestCencAudioSegment(); 36 | 37 | FILE* Dash2HLS_GetMp4BoxInitSegment(); 38 | 39 | #endif // _DASH2HLS_MAC_TEST_FILES_H_ 40 | -------------------------------------------------------------------------------- /library/mac_test_files.mm: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2014 Google Inc. All rights reserved. 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | #include "mac_test_files.h" 18 | 19 | #import 20 | 21 | namespace { 22 | NSString* kMp4BoxInitSegment = @"mp4box_init"; 23 | NSString* kDashSampleCencVideoFile = @""; 24 | NSString* kDashSampleCencAudioFile = @""; 25 | NSString* kDashSampleVideoFile = @""; 26 | NSString* kDashSampleAudioFile = @""; 27 | NSString* kDashSampleVideoHeader = @""; 28 | NSString* kDashSampleAudioHeader = @""; 29 | NSString* kDashSampleVideoSegment = @""; 30 | NSString* kDashSampleAudioSegment = @""; 31 | } // namespace 32 | 33 | FILE* Dash2HLS_GetTestVideoFile() { 34 | NSString* mp4_path = 35 | [[NSBundle mainBundle] pathForResource:kDashSampleVideoFile 36 | ofType:@"fmp4"]; 37 | return fopen([mp4_path UTF8String], "r"); 38 | } 39 | 40 | FILE* Dash2HLS_GetTestAudioFile() { 41 | NSString* mp4_path = 42 | [[NSBundle mainBundle] pathForResource:kDashSampleAudioFile 43 | ofType:@"fmp4"]; 44 | return fopen([mp4_path UTF8String], "r"); 45 | } 46 | 47 | FILE* Dash2HLS_GetTestCencVideoFile() { 48 | NSString* mp4_path = 49 | [[NSBundle mainBundle] pathForResource:kDashSampleCencVideoFile 50 | ofType:@"mp4"]; 51 | return fopen([mp4_path UTF8String], "r"); 52 | } 53 | 54 | FILE* Dash2HLS_GetTestCencAudioFile() { 55 | NSString* mp4_path = 56 | [[NSBundle mainBundle] pathForResource:kDashSampleCencAudioFile 57 | ofType:@"mp4"]; 58 | return fopen([mp4_path UTF8String], "r"); 59 | } 60 | 61 | FILE* Dash2HLS_GetTestCencVideoHeader() { 62 | NSString* mp4_path = 63 | [[NSBundle mainBundle] pathForResource:kDashSampleVideoHeader 64 | ofType:@"mp4"]; 65 | return fopen([mp4_path UTF8String], "r"); 66 | } 67 | 68 | FILE* Dash2HLS_GetTestCencAudioHeader() { 69 | NSString* mp4_path = 70 | [[NSBundle mainBundle] pathForResource:kDashSampleAudioHeader 71 | ofType:@"mp4"]; 72 | return fopen([mp4_path UTF8String], "r"); 73 | } 74 | 75 | FILE* Dash2HLS_GetTestCencVideoSegment() { 76 | NSString* mp4_path = 77 | [[NSBundle mainBundle] pathForResource:kDashSampleVideoSegment 78 | ofType:@"mp4"]; 79 | return fopen([mp4_path UTF8String], "r"); 80 | } 81 | 82 | FILE* Dash2HLS_GetTestCencAudioSegment() { 83 | NSString* mp4_path = 84 | [[NSBundle mainBundle] pathForResource:kDashSampleAudioSegment 85 | ofType:@"mp4"]; 86 | return fopen([mp4_path UTF8String], "r"); 87 | } 88 | 89 | FILE* Dash2HLS_GetMp4BoxInitSegment() { 90 | NSString* mp4_path = 91 | [[NSBundle mainBundle] pathForResource:kMp4BoxInitSegment 92 | ofType:@"mp4"]; 93 | return fopen([mp4_path UTF8String], "r"); 94 | } 95 | -------------------------------------------------------------------------------- /library/player/DashHTTPConnection.h: -------------------------------------------------------------------------------- 1 | #ifndef DASH2HLS_PLAYER_DASHHTTPCONNECTION_H_ 2 | #define DASH2HLS_PLAYER_DASHHTTPCONNECTION_H_ 3 | 4 | /* 5 | Copyright 2014 Google Inc. All rights reserved. 6 | 7 | Licensed under the Apache License, Version 2.0 (the "License"); 8 | you may not use this file except in compliance with the License. 9 | You may obtain a copy of the License at 10 | 11 | http://www.apache.org/licenses/LICENSE-2.0 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" BASIS, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | See the License for the specific language governing permissions and 17 | limitations under the License. 18 | */ 19 | 20 | // Proxy connection for handling one movie. 21 | // 22 | // Uses static variables so multiple DashHTTPConnections will use the same 23 | // DashParsers. To simplify readability of this example very little error 24 | // detection is done. In production code the error handling must be 25 | // included. 26 | // 27 | // The included sample asset is truncated. iOS does not like that so this 28 | // example returns only the first 15 seconds. Productions code should 29 | // return the entire contents. 30 | // 31 | // Absolutely requires ParseAudio/ParseVideo to be called before Convert. 32 | // 33 | // This example uses amazingly trivial manifests. No adaption, no audio 34 | // only cellular acceptable sessions, no alternate languages. Just pure 35 | // simple trivial manifest. 36 | 37 | #import 38 | #import "HTTPConnection.h" 39 | 40 | @interface DashHTTPConnection : HTTPConnection 41 | @end 42 | 43 | #endif // DASH2HLS_PLAYER_DASHHTTPCONNECTION_H_ 44 | -------------------------------------------------------------------------------- /library/player/OSX/AppDelegate.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2014 Google Inc. All rights reserved. 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | // Starts a proxy server, sets the server to have DashHTTPConnection handle 18 | // all incoming connections. Creates an AVPlayerView and starts the sample 19 | // movie. 20 | 21 | #import 22 | #import 23 | 24 | @class HTTPServer; 25 | 26 | @interface AppDelegate : NSObject { 27 | HTTPServer* _http_server; 28 | } 29 | 30 | @property(assign) IBOutlet AVPlayerView *movie_player; 31 | @property(assign) IBOutlet NSWindow *window; 32 | 33 | @end 34 | -------------------------------------------------------------------------------- /library/player/OSX/AppDelegate.mm: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2014 Google Inc. All rights reserved. 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | #import "AppDelegate.h" 18 | 19 | #import 20 | #import 21 | #include 22 | #include "library/player/DashHTTPConnection.h" 23 | 24 | @implementation AppDelegate 25 | 26 | - (void)applicationDidFinishLaunching:(NSNotification *)aNotification 27 | { 28 | _http_server = [[HTTPServer alloc] init]; 29 | [_http_server setConnectionClass:[DashHTTPConnection class]]; 30 | [_http_server setType:@"_http._tcp."]; 31 | [_http_server setPort:12345]; 32 | NSError* error; 33 | BOOL success = [_http_server start:&error]; 34 | if (!success) { 35 | NSLog(@"Could not start http server %@", error); 36 | return; 37 | } 38 | _movie_player.player = [AVPlayer playerWithURL: 39 | [NSURL URLWithString:@"http://localhost:12345/movie.m3u8"]]; 40 | [[_movie_player player] play]; 41 | } 42 | 43 | @end 44 | -------------------------------------------------------------------------------- /library/player/OSX/en.lproj/InfoPlist.strings: -------------------------------------------------------------------------------- 1 | /* Localized versions of Info.plist keys */ 2 | 3 | -------------------------------------------------------------------------------- /library/player/OSX/main.m: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2014 Google Inc. All rights reserved. 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | #import 18 | 19 | int main(int argc, const char * argv[]) 20 | { 21 | return NSApplicationMain(argc, argv); 22 | } 23 | -------------------------------------------------------------------------------- /library/player/OSX/player-Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | ${EXECUTABLE_NAME} 9 | CFBundleIconFile 10 | 11 | CFBundleIdentifier 12 | com.google.${PRODUCT_NAME:rfc1034identifier} 13 | CFBundleInfoDictionaryVersion 14 | 6.0 15 | CFBundleName 16 | ${PRODUCT_NAME} 17 | CFBundlePackageType 18 | APPL 19 | CFBundleShortVersionString 20 | 1.0 21 | CFBundleSignature 22 | ???? 23 | CFBundleVersion 24 | 1 25 | LSMinimumSystemVersion 26 | ${MACOSX_DEPLOYMENT_TARGET} 27 | NSHumanReadableCopyright 28 | Copyright © 2014 Google. All rights reserved. 29 | NSMainNibFile 30 | MainMenu 31 | NSPrincipalClass 32 | NSApplication 33 | 34 | 35 | -------------------------------------------------------------------------------- /library/player/iOS/AppDelegate.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2014 Google Inc. All rights reserved. 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | // Starts a proxy server, sets the server to have DashHTTPConnection handle 18 | // all incoming connections. 19 | #import 20 | 21 | @class HTTPServer; 22 | 23 | @interface AppDelegate : UIResponder { 24 | HTTPServer* _http_server; 25 | } 26 | 27 | @property (strong, nonatomic) UIWindow *window; 28 | 29 | @end 30 | -------------------------------------------------------------------------------- /library/player/iOS/AppDelegate.mm: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2014 Google Inc. All rights reserved. 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | #import "AppDelegate.h" 18 | 19 | #include 20 | #include "library/player/DashHTTPConnection.h" 21 | 22 | @implementation AppDelegate 23 | 24 | - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 25 | { 26 | _http_server = [[HTTPServer alloc] init]; 27 | [_http_server setConnectionClass:[DashHTTPConnection class]]; 28 | [_http_server setType:@"_http._tcp."]; 29 | [_http_server setPort:12345]; 30 | NSError* error; 31 | BOOL success = [_http_server start:&error]; 32 | if (!success) { 33 | NSLog(@"Could not start http server %@", error); 34 | return NO; 35 | } 36 | 37 | return YES; 38 | } 39 | 40 | @end 41 | -------------------------------------------------------------------------------- /library/player/iOS/Base.lproj/Main_iPad.storyboard: -------------------------------------------------------------------------------- 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 | -------------------------------------------------------------------------------- /library/player/iOS/Base.lproj/Main_iPhone.storyboard: -------------------------------------------------------------------------------- 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 | -------------------------------------------------------------------------------- /library/player/iOS/Images.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "size" : "29x29", 6 | "scale" : "2x" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "size" : "40x40", 11 | "scale" : "2x" 12 | }, 13 | { 14 | "idiom" : "iphone", 15 | "size" : "60x60", 16 | "scale" : "2x" 17 | }, 18 | { 19 | "idiom" : "ipad", 20 | "size" : "29x29", 21 | "scale" : "1x" 22 | }, 23 | { 24 | "idiom" : "ipad", 25 | "size" : "29x29", 26 | "scale" : "2x" 27 | }, 28 | { 29 | "idiom" : "ipad", 30 | "size" : "40x40", 31 | "scale" : "1x" 32 | }, 33 | { 34 | "idiom" : "ipad", 35 | "size" : "40x40", 36 | "scale" : "2x" 37 | }, 38 | { 39 | "idiom" : "ipad", 40 | "size" : "76x76", 41 | "scale" : "1x" 42 | }, 43 | { 44 | "idiom" : "ipad", 45 | "size" : "76x76", 46 | "scale" : "2x" 47 | } 48 | ], 49 | "info" : { 50 | "version" : 1, 51 | "author" : "xcode" 52 | } 53 | } -------------------------------------------------------------------------------- /library/player/iOS/Images.xcassets/LaunchImage.launchimage/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "orientation" : "portrait", 5 | "idiom" : "iphone", 6 | "extent" : "full-screen", 7 | "minimum-system-version" : "7.0", 8 | "scale" : "2x" 9 | }, 10 | { 11 | "orientation" : "portrait", 12 | "idiom" : "iphone", 13 | "subtype" : "retina4", 14 | "extent" : "full-screen", 15 | "minimum-system-version" : "7.0", 16 | "scale" : "2x" 17 | }, 18 | { 19 | "orientation" : "portrait", 20 | "idiom" : "ipad", 21 | "extent" : "full-screen", 22 | "minimum-system-version" : "7.0", 23 | "scale" : "1x" 24 | }, 25 | { 26 | "orientation" : "landscape", 27 | "idiom" : "ipad", 28 | "extent" : "full-screen", 29 | "minimum-system-version" : "7.0", 30 | "scale" : "1x" 31 | }, 32 | { 33 | "orientation" : "portrait", 34 | "idiom" : "ipad", 35 | "extent" : "full-screen", 36 | "minimum-system-version" : "7.0", 37 | "scale" : "2x" 38 | }, 39 | { 40 | "orientation" : "landscape", 41 | "idiom" : "ipad", 42 | "extent" : "full-screen", 43 | "minimum-system-version" : "7.0", 44 | "scale" : "2x" 45 | } 46 | ], 47 | "info" : { 48 | "version" : 1, 49 | "author" : "xcode" 50 | } 51 | } -------------------------------------------------------------------------------- /library/player/iOS/ViewController.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2014 Google Inc. All rights reserved. 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | // Creates an MPMoviePlayer and starts playing the sample movie. 18 | // 19 | // Uses MPMoviePlayer because iOS does not have an AVPlayerView, so using 20 | // the AV framework on iOS adds unrelated code to get an AVPlayer to show 21 | // up on the screen. 22 | #import 23 | 24 | @interface ViewController : UIViewController 25 | @end 26 | -------------------------------------------------------------------------------- /library/player/iOS/ViewController.mm: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2014 Google Inc. All rights reserved. 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | #import "ViewController.h" 18 | 19 | #import 20 | #import 21 | 22 | NSString* kMovieUrl = @"http://localhost:12345/movie.m3u8"; 23 | 24 | @interface ViewController () 25 | @property(strong, nonatomic) IBOutlet UIView* movie_holder; 26 | @property(strong, nonatomic) MPMoviePlayerController* movie_player; 27 | @end 28 | 29 | @implementation ViewController 30 | 31 | - (void)viewDidLoad 32 | { 33 | [super viewDidLoad]; 34 | self.movie_player = 35 | [[MPMoviePlayerController alloc] initWithContentURL: 36 | [NSURL URLWithString:kMovieUrl]]; 37 | [self.movie_holder addSubview:self.movie_player.view]; 38 | [self.movie_player play]; 39 | } 40 | 41 | - (void)viewWillLayoutSubviews { 42 | [super viewWillLayoutSubviews]; 43 | [self.movie_player.view setFrame:[self.movie_holder bounds]]; 44 | } 45 | 46 | - (void)dealloc { 47 | } 48 | 49 | @end 50 | -------------------------------------------------------------------------------- /library/player/iOS/en.lproj/InfoPlist.strings: -------------------------------------------------------------------------------- 1 | /* Localized versions of Info.plist keys */ 2 | 3 | -------------------------------------------------------------------------------- /library/player/iOS/main.m: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2014 Google Inc. All rights reserved. 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | #import 18 | 19 | #import "AppDelegate.h" 20 | 21 | int main(int argc, char * argv[]) 22 | { 23 | @autoreleasepool { 24 | return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /library/player/iOS/player-Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleDisplayName 8 | ${PRODUCT_NAME} 9 | CFBundleExecutable 10 | ${EXECUTABLE_NAME} 11 | CFBundleIdentifier 12 | com.google.${PRODUCT_NAME:rfc1034identifier} 13 | CFBundleInfoDictionaryVersion 14 | 6.0 15 | CFBundleName 16 | ${PRODUCT_NAME} 17 | CFBundlePackageType 18 | APPL 19 | CFBundleShortVersionString 20 | 1.0 21 | CFBundleSignature 22 | ???? 23 | CFBundleVersion 24 | 1.0 25 | LSRequiresIPhoneOS 26 | 27 | UIMainStoryboardFile 28 | Main_iPhone 29 | UIMainStoryboardFile~ipad 30 | Main_iPad 31 | UIRequiredDeviceCapabilities 32 | 33 | armv7 34 | 35 | UISupportedInterfaceOrientations 36 | 37 | UIInterfaceOrientationPortrait 38 | UIInterfaceOrientationLandscapeLeft 39 | UIInterfaceOrientationLandscapeRight 40 | 41 | UISupportedInterfaceOrientations~ipad 42 | 43 | UIInterfaceOrientationPortrait 44 | UIInterfaceOrientationPortraitUpsideDown 45 | UIInterfaceOrientationLandscapeLeft 46 | UIInterfaceOrientationLandscapeRight 47 | 48 | 49 | 50 | -------------------------------------------------------------------------------- /library/player/iOS/player-Prefix.pch: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2014 Google Inc. All rights reserved. 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | #import 18 | 19 | #ifndef __IPHONE_5_0 20 | #warning "This project uses features only available in iOS SDK 5.0 and later." 21 | #endif 22 | 23 | #ifdef __OBJC__ 24 | #import 25 | #import 26 | #endif 27 | -------------------------------------------------------------------------------- /library/ps/pes.h: -------------------------------------------------------------------------------- 1 | #ifndef _DASH2HLS_PES_H_ 2 | #define _DASH2HLS_PES_H_ 3 | 4 | /* 5 | Copyright 2014 Google Inc. All rights reserved. 6 | 7 | Licensed under the Apache License, Version 2.0 (the "License"); 8 | you may not use this file except in compliance with the License. 9 | You may obtain a copy of the License at 10 | 11 | http://www.apache.org/licenses/LICENSE-2.0 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" BASIS, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | See the License for the specific language governing permissions and 17 | limitations under the License. 18 | */ 19 | 20 | // Every sample is encoded in a PES packet. 21 | // See ISO-14966-10 for details on its internals. 22 | 23 | #include 24 | #include 25 | #include 26 | 27 | namespace dash2hls { 28 | 29 | class PES { 30 | public: 31 | static const uint8_t kPesStartCode[3]; 32 | enum { 33 | kPsmStreamId = 0xbc, 34 | kAudioStreamId = 0xc0, 35 | kVideoStreamId = 0xe0, 36 | kVideoStreamType = 0x1b, 37 | kAudioStreamType = 0x0f, 38 | }; 39 | 40 | public: 41 | PES(); 42 | 43 | size_t GetSize() const; 44 | size_t GetHeaderSize() const; 45 | void set_max_size(size_t size) {max_size_ = size;} 46 | 47 | size_t Write(uint8_t* buffer, size_t max_length) const; 48 | size_t WritePartial(uint8_t* buffer, size_t start, size_t length) const; 49 | size_t WriteHeader(uint8_t* buffer, size_t max_length) const; 50 | 51 | void AddPayload(const uint8_t* payload, size_t length) { 52 | payload_ = payload; 53 | payload_size_ = length; 54 | } 55 | size_t GetFreePayloadBytes() const; 56 | 57 | void SetCopyright(bool copyright); 58 | void SetOriginal(bool original); 59 | void SetScramblingBits(uint8_t scrambling_bits); 60 | void SetDataAlignmentIndicator(bool aligned); 61 | void SetExtensionFlags(uint8_t flags1, uint8_t flags2) { 62 | flags1_ = flags1; 63 | flags2_ = flags2; 64 | } 65 | bool HasOptHeader() const; 66 | 67 | void SetPts(uint64_t pts); 68 | void SetDts(uint64_t dts); 69 | void set_stream_id(uint32_t stream_id) {stream_id_ = stream_id;} 70 | 71 | private: 72 | size_t max_size_; 73 | uint8_t stream_id_; 74 | uint8_t flags1_; 75 | uint8_t flags2_; 76 | uint64_t pts_; 77 | uint64_t dts_; 78 | const uint8_t* payload_; 79 | size_t payload_size_; 80 | }; 81 | } // namespace dash2hls 82 | 83 | #endif // _DASH2HLS_PES_H_ 84 | -------------------------------------------------------------------------------- /library/ps/program_stream_out.h: -------------------------------------------------------------------------------- 1 | // ProgramStreamOut takes mp4 samples and turns them into PS samples. 2 | // These samples can then be wrapped in a TS layer. 3 | // 4 | // Expected usage is to call ProgramStreamOut::ProcessSample(in, out); 5 | // All other routines are exposed for unit testing. 6 | #ifndef _DASH2HLS_PROGRAM_STREAM_OUT_H_ 7 | #define _DASH2HLS_PROGRAM_STREAM_OUT_H_ 8 | 9 | #include 10 | 11 | #include "include/DashToHlsApi.h" 12 | #include "library/dash/box_type.h" 13 | #include "library/dash/full_box_contents.h" 14 | #include "library/ps/nalu.h" 15 | 16 | namespace dash2hls { 17 | class SystemHeader; 18 | class PES; 19 | class PSM; 20 | 21 | class ProgramStreamOut { 22 | public: 23 | ProgramStreamOut() : nalu_length_(0), 24 | has_video_(false), 25 | has_audio_(false) { 26 | } 27 | 28 | // TODO(justsomeguy) this interface requires an extra copy of input because 29 | // it's const. See about doing it in place. 30 | void ProcessSample(const uint8_t* input, size_t input_length, 31 | bool is_video, 32 | bool is_sync_sample, 33 | uint64_t pts, uint64_t dts, uint64_t scr, 34 | uint64_t duration, 35 | std::vector* out); 36 | 37 | void set_nalu_length(size_t nalu_length) {nalu_length_ = nalu_length;} 38 | void set_sps_pps(const std::vector& sps_pps) {sps_pps_ = sps_pps;} 39 | void set_audio_oid(const std::vector& audio_oid) { 40 | audio_oid_ = audio_oid; 41 | } 42 | void set_has_video(bool flag) {has_video_ = flag;} 43 | void set_has_audio(bool flag) {has_audio_ = flag;} 44 | 45 | protected: 46 | void PreprocessNalus(std::vector* buffer, bool* has_aud, 47 | nalu::PicType* pic_type); 48 | void AddNeededNalu(std::vector* buffer, nalu::PicType pic_type, 49 | bool is_sync_sample); 50 | void ConvertLengthToStartCode(std::vector* buffer); 51 | size_t WriteHeader(uint8_t* buffer, uint64_t scr, uint32_t mux_rate); 52 | void AddHeaders(const PES& pes, bool is_sync_sample, uint64_t duration, 53 | uint64_t scr, std::vector* out); 54 | size_t GetHeaderSize() const; 55 | size_t GetSize(const PES& pes) const; 56 | size_t GetSizeOfSyncPacket(bool is_sync_sample, 57 | const SystemHeader& system_header, 58 | const PSM& psm, const PES& pes) const; 59 | 60 | private: 61 | static const uint8_t kPackStartCode[4]; 62 | size_t nalu_length_; 63 | std::vector sps_pps_; 64 | std::vector audio_oid_; 65 | bool has_video_; 66 | bool has_audio_; 67 | }; 68 | } // namespace dash2hls 69 | 70 | #endif // _DASH2HLS_PROGRAM_STREAM_OUT_H_ 71 | -------------------------------------------------------------------------------- /library/ps/psm.cc: -------------------------------------------------------------------------------- 1 | #include "library/ps/psm.h" 2 | 3 | #include 4 | 5 | #include "library/ps/pes.h" 6 | #include "library/utilities.h" 7 | 8 | namespace { 9 | const size_t kBasePsmSize = 16; 10 | const size_t kDescriptorOverheadSize = sizeof(uint32_t); 11 | } // namespace 12 | 13 | namespace dash2hls { 14 | 15 | const uint8_t PSM::kVideoAlignmentDescriptor[3] = {0x06, 0x01, 0x03}; 16 | const uint8_t PSM::kAudioAlignmentDescriptor[3] = {0x06, 0x01, 0x01}; 17 | 18 | PSM::PSM() : current_next_indicator_(true), 19 | psm_version_(0) { 20 | } 21 | 22 | size_t PSM::GetSize() const { 23 | size_t size = kBasePsmSize + descriptors_.size(); 24 | for (std::vector::const_iterator 25 | iter = elementary_streams_.begin(); 26 | iter != elementary_streams_.end(); ++iter) { 27 | size += kDescriptorOverheadSize + iter->descriptors.size(); 28 | } 29 | return size; 30 | } 31 | 32 | // This routine has magic numbers still. They were pulled from the Widevine 33 | // code and should be replaced with meaningful constants. 34 | // TODO(justsomeguy) look up magic numbers. 35 | size_t PSM::Write(uint8_t* buffer, uint32_t max_length) const { 36 | uint8_t* original_buffer = buffer; 37 | size_t size = GetSize(); 38 | if (size > max_length) { 39 | return 0; 40 | } 41 | memcpy(buffer, PES::kPesStartCode, sizeof(PES::kPesStartCode)); 42 | buffer += sizeof(PES::kPesStartCode); 43 | *buffer = 0xbc; 44 | ++buffer; 45 | htonsToBuffer(size - 6, buffer); 46 | buffer += sizeof(uint16_t); 47 | *buffer = 0x60 | (psm_version_ & 0x1f); 48 | if (current_next_indicator_) { 49 | *buffer |= 0x80; 50 | } 51 | ++buffer; 52 | *buffer = 0xff; 53 | ++buffer; 54 | htonsToBuffer(descriptors_.size(), buffer); 55 | buffer += sizeof(uint16_t); 56 | if (!descriptors_.empty()) { 57 | memcpy(buffer, descriptors_.data(), descriptors_.size()); 58 | buffer += descriptors_.size(); 59 | } 60 | // The rest of the size is the elemetary_streams_. 61 | htonsToBuffer(size - kBasePsmSize - descriptors_.size(), buffer); 62 | buffer += sizeof(uint16_t); 63 | for (std::vector::const_iterator 64 | iter = elementary_streams_.begin(); 65 | iter != elementary_streams_.end(); ++iter) { 66 | *buffer = iter->stream_type; 67 | ++buffer; 68 | *buffer = iter->stream_id; 69 | ++buffer; 70 | htonsToBuffer(iter->descriptors.size(), buffer); 71 | buffer += sizeof(uint16_t); 72 | if (!iter->descriptors.empty()) { 73 | memcpy(buffer, iter->descriptors.data(), iter->descriptors.size()); 74 | buffer += iter->descriptors.size(); 75 | } 76 | } 77 | htonlToBuffer(static_cast(wvcrc32(original_buffer, 78 | static_cast(size - 4))), 79 | buffer); 80 | return size; 81 | } 82 | 83 | void PSM::AddDescriptor(const uint8_t* descriptor, uint16_t size) { 84 | size_t old_size = descriptors_.size(); 85 | descriptors_.resize(old_size + size); 86 | memcpy(&descriptors_[old_size], descriptor, size); 87 | } 88 | 89 | void PSM::AddElementaryStream(uint8_t stream_id, const uint8_t stream_type) { 90 | ElementaryStreamInfo new_info; 91 | new_info.stream_id = stream_id; 92 | new_info.stream_type = stream_type; 93 | elementary_streams_.push_back(new_info); 94 | } 95 | 96 | void PSM::AddElementaryStreamDescriptor(uint8_t stream_id, 97 | const uint8_t* descriptor, 98 | uint16_t size) { 99 | for (std::vector::iterator 100 | iter = elementary_streams_.begin(); 101 | iter != elementary_streams_.end(); ++iter) { 102 | if (iter->stream_id == stream_id) { 103 | size_t old_size = iter->descriptors.size(); 104 | iter->descriptors.resize(old_size + size); 105 | memcpy(&iter->descriptors[old_size], descriptor, size); 106 | } 107 | } 108 | } 109 | } // namespace dash2hls 110 | -------------------------------------------------------------------------------- /library/ps/psm.h: -------------------------------------------------------------------------------- 1 | #ifndef _DASH2HLS_PSM_H_ 2 | #define _DASH2HLS_PSM_H_ 3 | 4 | /* 5 | Copyright 2014 Google Inc. All rights reserved. 6 | 7 | Licensed under the Apache License, Version 2.0 (the "License"); 8 | you may not use this file except in compliance with the License. 9 | You may obtain a copy of the License at 10 | 11 | http://www.apache.org/licenses/LICENSE-2.0 12 | 13 | Unless required by applicable law or agreed to in writing, software 14 | distributed under the License is distributed on an "AS IS" BASIS, 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | See the License for the specific language governing permissions and 17 | limitations under the License. 18 | */ 19 | 20 | // Every segment has a PSM following the system header. 21 | // See ISO-14966-10 for details on its internals. 22 | 23 | #include 24 | #include 25 | #include 26 | 27 | namespace dash2hls { 28 | 29 | class PSM { 30 | public: 31 | static const uint8_t kVideoAlignmentDescriptor[3]; 32 | static const uint8_t kAudioAlignmentDescriptor[3]; 33 | 34 | public: 35 | PSM(); 36 | 37 | size_t GetSize() const; 38 | 39 | size_t Write(uint8_t* buffer, uint32_t max_length) const; 40 | void set_current_next_indicator(bool current_next) { 41 | current_next_indicator_ = current_next; 42 | } 43 | void set_psm_version(uint8_t psm_version) {psm_version_ = psm_version;} 44 | void AddDescriptor(const uint8_t* descriptor, uint16_t size); 45 | void AddElementaryStream(uint8_t stream_id, const uint8_t stream_type); 46 | void AddElementaryStreamDescriptor(uint8_t stream_id, 47 | const uint8_t* descriptor, uint16_t size); 48 | 49 | private: 50 | struct ElementaryStreamInfo { 51 | uint8_t stream_id; 52 | uint8_t stream_type; 53 | std::vector descriptors; 54 | }; 55 | bool current_next_indicator_; 56 | uint8_t psm_version_; 57 | std::vector descriptors_; 58 | std::vector elementary_streams_; 59 | }; 60 | } // namespace dash2hls 61 | 62 | #endif // _DASH2HLS_PSM_H_ 63 | -------------------------------------------------------------------------------- /library/ps/psm_test.cc: -------------------------------------------------------------------------------- 1 | #include "library/ps/psm.h" 2 | 3 | #include 4 | #include 5 | 6 | #include "library/ps/pes.h" 7 | #include "library/utilities_gmock.h" 8 | 9 | namespace { 10 | // These test values are taken from the Widevine packager and live data. 11 | const uint8_t kVideoStreamType = 0x1b; 12 | const uint8_t kAudioStreamType = 0x0f; 13 | const uint8_t kAudioOid[] = { 14 | 0x1d, 0x34, 0x10, 0x01, 0x02, 0x80, 0x80, 0x2e, 15 | 0x00, 0x4f, 0xff, 0xff, 0xfe, 0xfe, 0xff, 0x03, 16 | 0x80, 0x80, 0x80, 0x22, 0x00, 0xc0, 0x00, 0x04, 17 | 0x80, 0x80, 0x80, 0x14, 0x40, 0x15, 0x00, 0x18, 18 | 0x00, 0x00, 0x01, 0xf4, 0x00, 0x00, 0x01, 0xf4, 19 | 0x00, 0x05, 0x80, 0x80, 0x80, 0x02, 0x12, 0x10, 20 | 0x06, 0x80, 0x80, 0x80, 0x01, 0x02}; 21 | const uint8_t kExpectedPsm[] = { 22 | 0x00, 0x00, 0x01, 0xbc, 0x00, 0x4e, 0xe0, 0xff, 23 | 0x00, 0x36, 0x1d, 0x34, 0x10, 0x01, 0x02, 0x80, 24 | 0x80, 0x2e, 0x00, 0x4f, 0xff, 0xff, 0xfe, 0xfe, 25 | 0xff, 0x03, 0x80, 0x80, 0x80, 0x22, 0x00, 0xc0, 26 | 0x00, 0x04, 0x80, 0x80, 0x80, 0x14, 0x40, 0x15, 27 | 0x00, 0x18, 0x00, 0x00, 0x01, 0xf4, 0x00, 0x00, 28 | 0x01, 0xf4, 0x00, 0x05, 0x80, 0x80, 0x80, 0x02, 29 | 0x12, 0x10, 0x06, 0x80, 0x80, 0x80, 0x01, 0x02, 30 | 0x00, 0x0e, 0x1b, 0xe0, 0x00, 0x03, 0x06, 0x01, 31 | 0x03, 0x0f, 0xc0, 0x00, 0x03, 0x06, 0x01, 0x01, 32 | 0xad, 0xd0, 0x20, 0x1a}; 33 | } // namespace 34 | 35 | namespace dash2hls { 36 | 37 | TEST(PSM, BuildExpected) { 38 | PSM psm; 39 | psm.set_current_next_indicator(true); 40 | psm.set_psm_version(0); 41 | psm.AddElementaryStream(PES::kVideoStreamId, kVideoStreamType); 42 | psm.AddElementaryStreamDescriptor(PES::kVideoStreamId, 43 | PSM::kVideoAlignmentDescriptor, 44 | sizeof(PSM::kVideoAlignmentDescriptor)); 45 | psm.AddDescriptor(kAudioOid, sizeof(kAudioOid)); 46 | 47 | psm.AddElementaryStream(PES::kAudioStreamId, kAudioStreamType); 48 | psm.AddElementaryStreamDescriptor(PES::kAudioStreamId, 49 | PSM::kAudioAlignmentDescriptor, 50 | sizeof(PSM::kAudioAlignmentDescriptor)); 51 | 52 | ASSERT_EQ(sizeof(kExpectedPsm), psm.GetSize()); 53 | uint8_t buffer[sizeof(kExpectedPsm)]; 54 | EXPECT_EQ(sizeof(kExpectedPsm), 55 | psm.Write(buffer, sizeof(buffer))); 56 | EXPECT_THAT(std::make_pair(buffer, sizeof(buffer)), 57 | testing::MemEq(kExpectedPsm, 58 | sizeof(kExpectedPsm))); 59 | } 60 | } // namespace dash2hls 61 | -------------------------------------------------------------------------------- /library/ps/system_header.h: -------------------------------------------------------------------------------- 1 | // Every segment starts with a system header. See ISO-14966-10 for details 2 | // on its internals. 3 | #ifndef _DASH2HLS_SYSTEM_HEADER_H_ 4 | #define _DASH2HLS_SYSTEM_HEADER_H_ 5 | 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | namespace dash2hls { 12 | 13 | class SystemHeader { 14 | public: 15 | SystemHeader(); 16 | 17 | uint32_t GetSize() const; 18 | uint32_t Write(uint8_t* buffer, uint32_t max_length) const; 19 | 20 | void SetRateBound(uint32_t rate_bound) { 21 | rate_bound_ = rate_bound; 22 | } 23 | void SetAudioBound(uint8_t audio_bound) { 24 | audio_bound_ = audio_bound; 25 | } 26 | void SetFixedFlag(bool fixedFlag); 27 | void SetCspsFlag(bool cspsFlag); 28 | void SetAudioLockFlag(bool audioLockFlag); 29 | void SetVideoLockFlag(bool videoLockFlag); 30 | void SetVideoBound(uint8_t video_bound) { 31 | video_bound_ = video_bound; 32 | } 33 | void SetPacketRestrictionFlag(bool flag); 34 | void AddStream(uint8_t stream_id); 35 | void SetBufferSizeBound(uint8_t stream_id, uint8_t buffer_size_scale, 36 | uint16_t buffer_size_bound); 37 | 38 | private: 39 | uint32_t rate_bound_; 40 | uint8_t audio_bound_; 41 | uint8_t video_bound_; 42 | uint8_t flags1_; 43 | uint8_t flags2_; 44 | uint8_t flags3_; 45 | std::vector > streams_; 46 | }; 47 | } // namespace dash2hls 48 | 49 | #endif // _DASH2HLS_SYSTEM_HEADER_H_ 50 | -------------------------------------------------------------------------------- /library/ps/system_header_test.cc: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2014 Google Inc. All rights reserved. 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | #include "library/ps/system_header.h" 18 | 19 | #include 20 | #include 21 | 22 | #include "library/ps/pes.h" 23 | #include "library/utilities_gmock.h" 24 | 25 | namespace { 26 | // These test values are taken from the Widevine packager and live data. 27 | const uint8_t kExpectedSystemHeader[] = { 28 | 0x00, 0x00, 0x01, 0xbb, 0x00, 0x0f, 0x80, 0x00, 29 | 0x01, 0x04, 0xe1, 0x7f, 0xbc, 0xe0, 0x00, 0xe0, 30 | 0xe0, 0x00, 0xc0, 0xe0, 0x00}; 31 | } // namespace 32 | 33 | namespace dash2hls { 34 | 35 | TEST(SystemHeader, BuildExpected) { 36 | SystemHeader header; 37 | header.AddStream(PES::kPsmStreamId); 38 | header.AddStream(PES::kVideoStreamId); 39 | header.AddStream(PES::kAudioStreamId); 40 | header.SetAudioBound(1); 41 | header.SetVideoBound(1); 42 | header.SetFixedFlag(false); 43 | header.SetCspsFlag(false); 44 | header.SetAudioLockFlag(true); 45 | header.SetVideoLockFlag(true); 46 | header.SetPacketRestrictionFlag(false); 47 | ASSERT_EQ(sizeof(kExpectedSystemHeader), header.GetSize()); 48 | uint8_t buffer[sizeof(kExpectedSystemHeader)]; 49 | EXPECT_EQ(sizeof(kExpectedSystemHeader), 50 | header.Write(buffer, sizeof(buffer))); 51 | EXPECT_THAT(std::make_pair(buffer, sizeof(buffer)), 52 | testing::MemEq(kExpectedSystemHeader, 53 | sizeof(kExpectedSystemHeader))); 54 | } 55 | } // namespace dash2hls 56 | -------------------------------------------------------------------------------- /library/tools/OSX/ToolsAppDelegate.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2014 Google Inc. All rights reserved. 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | #import 18 | 19 | @interface ToolsAppDelegate : NSObject { 20 | NSURL* _outputDirectory; 21 | NSURL* _mp4Url; 22 | } 23 | 24 | - (void)AddToTextView:(NSString*)text; 25 | 26 | - (IBAction)SetOutputDirectory:(id)sender; 27 | - (IBAction)Live:(id)sender; 28 | - (IBAction)GenerateTS:(id)sender; 29 | - (IBAction)SelectDashFile:(id)sender; 30 | - (IBAction)GenerateM3u8:(id)sender; 31 | 32 | @property(assign) IBOutlet NSWindow *window; 33 | @property(weak) IBOutlet NSButton *outputDirectoryButton; 34 | @property(weak) IBOutlet NSButton *selectDashButton; 35 | @property(weak) IBOutlet NSButton *generateTsButton; 36 | @property(weak) IBOutlet NSButton *liveButton; 37 | @property(weak) IBOutlet NSButton *generateM3u8Button; 38 | @property(unsafe_unretained) IBOutlet NSTextView *logTextView; 39 | 40 | @end 41 | -------------------------------------------------------------------------------- /library/tools/OSX/ToolsMpdParser.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2014 Google Inc. All rights reserved. 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | // ToolsMpdParser is a standalone class to take a standard 18 | // YouTube or gFiber MPD file and turn it into a set of 19 | // m3u8 files. It is expected to be used when converting 20 | // DASH to HLS. 21 | 22 | #import 23 | 24 | @interface ToolsMpdParser : NSObject 25 | 26 | // _audioTrack contains the audio m3u8. 27 | @property(nonatomic, readonly) NSData *audioTrack; 28 | 29 | // URL and range for getting the DASH header. Pass this data 30 | // to the DashToHlsApi. 31 | @property(nonatomic, readonly) NSString *audioInitialization; 32 | @property(nonatomic, readonly) NSString *audioInitializationRange; 33 | 34 | // _videoTrack contains the video m3u8. 35 | @property(nonatomic, readonly) NSData *videoTrack; 36 | 37 | // URL and range for getting the DASH header. Pass this data 38 | // to the DashToHlsApi. 39 | @property(nonatomic, readonly) NSString *videoInitialization; 40 | @property(nonatomic, readonly) NSString *videoInitializationRange; 41 | 42 | - (id)initWithURL:(NSURL *)url; 43 | 44 | @end 45 | -------------------------------------------------------------------------------- /library/tools/OSX/en.lproj/InfoPlist.strings: -------------------------------------------------------------------------------- 1 | /* Localized versions of Info.plist keys */ 2 | 3 | -------------------------------------------------------------------------------- /library/tools/OSX/main.m: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2014 Google Inc. All rights reserved. 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | #import 18 | 19 | int main(int argc, const char * argv[]) { 20 | return NSApplicationMain(argc, argv); 21 | } 22 | -------------------------------------------------------------------------------- /library/tools/OSX/tools-Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | ${EXECUTABLE_NAME} 9 | CFBundleIconFile 10 | 11 | CFBundleIdentifier 12 | com.google.${PRODUCT_NAME:rfc1034identifier} 13 | CFBundleInfoDictionaryVersion 14 | 6.0 15 | CFBundleName 16 | ${PRODUCT_NAME} 17 | CFBundlePackageType 18 | APPL 19 | CFBundleShortVersionString 20 | 1.0 21 | CFBundleSignature 22 | ???? 23 | CFBundleVersion 24 | 1 25 | LSMinimumSystemVersion 26 | ${MACOSX_DEPLOYMENT_TARGET} 27 | NSHumanReadableCopyright 28 | Copyright © 2014 Google. All rights reserved. 29 | NSMainNibFile 30 | MainMenu 31 | NSPrincipalClass 32 | NSApplication 33 | 34 | 35 | -------------------------------------------------------------------------------- /library/utilities.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2014 Google Inc. All rights reserved. 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | #ifndef DASHTOHLS_UTILITIES_H_ 18 | #define DASHTOHLS_UTILITIES_H_ 19 | 20 | #include 21 | #include 22 | #include 23 | 24 | namespace dash2hls { 25 | // Network conversions that handle the casting correctly. 26 | // There are many places in H.264 where various bytes need to be read or 27 | // written to a buffer correctly. In practice this conversion is very error 28 | // prone and hard to read. These helper routines are to guarantee correctness 29 | // and improve readability in the code. 30 | // 31 | // The uint8_t* buffer is assumed to always be big enough to hold the 32 | // appropriate value. 33 | inline uint16_t ntohsFromBuffer(const uint8_t* buffer) { 34 | uint16_t networkBytes; 35 | memcpy(&networkBytes, buffer, sizeof(networkBytes)); 36 | return ntohs(networkBytes); 37 | } 38 | inline void htonsToBuffer(uint16_t value, uint8_t* buffer) { 39 | uint16_t network_value = htons(value); 40 | memcpy(buffer, &network_value, sizeof(network_value)); 41 | } 42 | 43 | inline uint32_t ntohlFromBuffer(const uint8_t* buffer) { 44 | uint32_t networkBytes; 45 | memcpy(&networkBytes, buffer, sizeof(networkBytes)); 46 | return ntohl(networkBytes); 47 | } 48 | inline void htonlToBuffer(uint32_t value, uint8_t* buffer) { 49 | uint32_t network_value = htonl(value); 50 | memcpy(buffer, &network_value, sizeof(network_value)); 51 | } 52 | 53 | uint64_t ntohllFromBuffer(const uint8_t* buffer); 54 | void htonllToBuffer(uint64_t value, uint8_t* buffer); 55 | 56 | // Determines if there are enough bytes left to continue parsing. 57 | // Even though it's a trivial routine it documents the code by giving it an 58 | // obvious name. Also avoids size_t vs long comparison warnings. 59 | inline bool EnoughBytesToParse(size_t parsed, size_t needed, 60 | size_t total_length) { 61 | return parsed + needed <= total_length; 62 | } 63 | 64 | // Easy routines to print numbers. Used by debugging routines. 65 | std::string PrettyPrintValue(int8_t); 66 | std::string PrettyPrintValue(int16_t); 67 | std::string PrettyPrintValue(int32_t); 68 | std::string PrettyPrintValue(int64_t); 69 | std::string PrettyPrintValue(uint8_t); 70 | std::string PrettyPrintValue(uint16_t); 71 | std::string PrettyPrintValue(uint32_t); 72 | std::string PrettyPrintValue(size_t); 73 | std::string PrettyPrintValue(uint64_t); 74 | std::string PrettyPrintBuffer(const uint8_t* buffer, size_t length); 75 | 76 | // verbosity when debugging. 77 | extern bool g_verbose_pretty_print; 78 | 79 | std::string DumpMemory(const uint8_t* buffer, size_t length); 80 | 81 | void DashToHlsDefaultDiagnosticCallback(const char* message); 82 | extern "C" void (*g_diagnostic_callback)(const char* diagnostic_message); 83 | void SendDashLog(const char* file, uint32_t line, const char* message, 84 | const char* reason, const char* extra_fields); 85 | #define DASH_LOG(message, reason, extra_fields) \ 86 | SendDashLog(__FILE__, __LINE__, message, reason, extra_fields); 87 | 88 | size_t wvcrc32(uint8_t* begin, int count); 89 | size_t wvrunningcrc32(uint8_t* begin, int count, uint32_t crc); 90 | 91 | } // namespace dash2hls 92 | #endif // DASHTOHLS_UTILITIES_H_ 93 | -------------------------------------------------------------------------------- /library/utilities_gmock.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2014 Google Inc. All rights reserved. 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | #ifndef DASHTOHLS_UTILITIES_GMOCK_H_ 18 | #define DASHTOHLS_UTILITIES_GMOCK_H_ 19 | 20 | #include 21 | #include 22 | #include 23 | 24 | namespace testing { 25 | namespace internal { 26 | template 27 | class MemEqMatcher { 28 | public: 29 | enum { 30 | kDefaultBytesToPrint = 8, 31 | }; 32 | MemEqMatcher(T* expected, size_t expected_length) 33 | : expected_(expected, expected + expected_length), 34 | max_bytes_to_print_(kDefaultBytesToPrint) { 35 | } 36 | 37 | MemEqMatcher(T* expected, size_t expected_length, size_t max_bytes_to_print) 38 | : expected_(expected, expected + expected_length), 39 | max_bytes_to_print_(max_bytes_to_print) { 40 | } 41 | 42 | void DescribeTo(::std::ostream* os) const { 43 | *os << "a " << expected_.size() << " byte memory block "; 44 | DumpStartOfRange(os, &expected_[0], expected_.size()); 45 | } 46 | void DescribeNegationTo(::std::ostream* os) const { 47 | *os << "not a " << expected_.size() 48 | << " byte memory block "; 49 | DumpStartOfRange(os, &expected_[0], expected_.size()); 50 | } 51 | template 52 | void DumpStartOfRange(::std::ostream* os, Iterator bytes, 53 | size_t length) const { 54 | size_t bytes_to_print = length; 55 | if (bytes_to_print > max_bytes_to_print_) { 56 | *os << "starting with "; 57 | bytes_to_print = max_bytes_to_print_; 58 | } else { 59 | *os << "containing "; 60 | } 61 | for (size_t count = 0; count < bytes_to_print; ++count) { 62 | *os << std::hex << uint32_t(*bytes); 63 | if (count + 1 < bytes_to_print) { 64 | *os << ", "; 65 | } 66 | ++bytes; 67 | } 68 | *os << std::dec; 69 | } 70 | template 71 | bool MatchAndExplain(std::pairactual, 72 | MatchResultListener* listener) const { 73 | if (expected_.size() != actual.second) { 74 | if (listener->stream()) { 75 | *listener << "a " << actual.second << 76 | " byte memory block "; 77 | DumpStartOfRange(listener->stream(), actual.first, actual.second); 78 | } 79 | return false; 80 | } 81 | for (size_t count = 0; count < expected_.size(); ++count) { 82 | if (actual.first[count] != expected_[count]) { 83 | if (listener->stream()) { 84 | *listener << "a " << actual.second << " byte memory block "; 85 | DumpStartOfRange(listener->stream(), actual.first, actual.second); 86 | *listener << " and at byte " << count << " has a value of " 87 | << std::hex << uint32_t(actual.first[count]) 88 | << " instead of " << uint32_t(expected_[count]) 89 | << std::dec; 90 | } 91 | return false; 92 | } 93 | } 94 | return true; 95 | } 96 | 97 | private: 98 | std::vector expected_; 99 | uint32_t max_bytes_to_print_; 100 | }; 101 | } // namespace internal 102 | 103 | template 104 | PolymorphicMatcher > MemEq( 105 | T* expected, size_t expected_length) { 106 | return MakePolymorphicMatcher(internal::MemEqMatcher(expected, 107 | expected_length)); 108 | } 109 | 110 | template 111 | PolymorphicMatcher > MemEq( 112 | T* expected, size_t expected_length, size_t max_bytes_to_print) { 113 | return MakePolymorphicMatcher(internal::MemEqMatcher(expected, 114 | expected_length, 115 | max_bytes_to_print)); 116 | } 117 | } // namespace testing 118 | #endif // DASHTOHLS_UTILITIES_GMOCK_H_ 119 | -------------------------------------------------------------------------------- /library/utilities_test.cc: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2014 Google Inc. All rights reserved. 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | #include "utilities.h" 18 | 19 | #include 20 | 21 | #include "include/DashToHlsApi.h" 22 | 23 | namespace { 24 | const uint8_t kUint8 = 0x01; 25 | const uint16_t kUint16 = 0x0102; 26 | const uint32_t kUint32 = 0x01020304; 27 | const uint64_t kUint64 = 0x0102030405060708; 28 | const int8_t kInt8 = -0x01; 29 | const int16_t kInt16 = -0x0102; 30 | const int32_t kInt32 = -0x01020304; 31 | const int64_t kInt64 = -0x0102030405060708; 32 | }; 33 | 34 | namespace dash2hls { 35 | 36 | TEST(Dash2HLS, ntoh) { 37 | uint8_t buffer[sizeof(uint64_t)]; 38 | for (size_t count = 0; count < sizeof(uint64_t); ++count) { 39 | buffer[count] = count + 1; 40 | } 41 | 42 | EXPECT_EQ(0x0102, ntohsFromBuffer(buffer)); 43 | EXPECT_EQ(0x01020304U, ntohlFromBuffer(buffer)); 44 | EXPECT_EQ(0x0102030405060708U, ntohllFromBuffer(buffer)); 45 | } 46 | 47 | TEST(Dash2HLS, hton) { 48 | uint8_t buffer[sizeof(uint64_t)]; 49 | htonsToBuffer(0x0102, buffer); 50 | htonlToBuffer(0x03040506, buffer + sizeof(uint16_t)); 51 | for (size_t count = 0; count < sizeof(uint16_t) + sizeof(uint32_t); 52 | ++count) { 53 | EXPECT_EQ(count + 1, buffer[count]); 54 | } 55 | htonllToBuffer(kUint64, buffer); 56 | for (size_t count = 0; count < sizeof(uint64_t); 57 | ++count) { 58 | EXPECT_EQ(count + 1, buffer[count]); 59 | } 60 | } 61 | 62 | TEST(Dash2HLS, PrettyPrintValue) { 63 | EXPECT_EQ("1", PrettyPrintValue(kUint8)); 64 | EXPECT_EQ("-1", PrettyPrintValue(kInt8)); 65 | EXPECT_EQ("258", PrettyPrintValue(kUint16)); 66 | EXPECT_EQ("-258", PrettyPrintValue(kInt16)); 67 | EXPECT_EQ("16909060", PrettyPrintValue(kUint32)); 68 | EXPECT_EQ("-16909060", PrettyPrintValue(kInt32)); 69 | EXPECT_EQ("72623859790382856", PrettyPrintValue(kUint64)); 70 | EXPECT_EQ("-72623859790382856", PrettyPrintValue(kInt64)); 71 | size_t a_size = kUint16; 72 | EXPECT_EQ("258", PrettyPrintValue(a_size)); 73 | } 74 | 75 | TEST(Dash2HLS, EnoughBytesToParse) { 76 | EXPECT_TRUE(EnoughBytesToParse(0, 10, 10)); 77 | EXPECT_TRUE(EnoughBytesToParse(5, 5, 15)); 78 | EXPECT_TRUE(EnoughBytesToParse(10, 0, 10)); 79 | EXPECT_FALSE(EnoughBytesToParse(0, 10, 5)); 80 | EXPECT_FALSE(EnoughBytesToParse(10, 10, 15)); 81 | EXPECT_FALSE(EnoughBytesToParse(10, 10, 19)); 82 | } 83 | 84 | namespace { 85 | std::string s_callback_result; 86 | void s_callback(const char* message) { 87 | s_callback_result = message; 88 | } 89 | } // namespace 90 | 91 | TEST(Dash2Hls, DiagnosticCallback) { 92 | SetDiagnosticCallback(s_callback); 93 | char line_buffer[1024]; 94 | 95 | // The sprintf must be immediatedly after the DASH_LOG. 96 | DASH_LOG("message", "reason", nullptr); 97 | snprintf(line_buffer, sizeof(line_buffer), "%u", __LINE__ -1); 98 | std::string expected = "{\n\t'Message':'message',\n\t'File':'"; 99 | expected += std::string(__FILE__) + "',\n\t'Line':" + line_buffer; 100 | expected += std::string(",\n\t'Reason':'reason'\n}"); 101 | EXPECT_EQ(expected, s_callback_result); 102 | 103 | // The sprintf must be immediatedly after the DASH_LOG. 104 | DASH_LOG("message", "reason", "extra_fields"); 105 | snprintf(line_buffer, sizeof(line_buffer), "%u", __LINE__ - 1); 106 | expected = "{\n\t'Message':'message',\n\t'File':'"; 107 | expected += std::string(__FILE__) + "',\n\t'Line':" + line_buffer + 108 | std::string(",\n\t'Reason':'reason',\n\t'Extra':'extra_fields'\n}"); 109 | EXPECT_EQ(expected, s_callback_result); 110 | SetDiagnosticCallback(nullptr); 111 | } 112 | } // namespace dash2hls 113 | --------------------------------------------------------------------------------