OggDecoder.cpp (2121B)
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim:set ts=2 sw=2 sts=2 et cindent: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #include "OggDecoder.h" 8 9 #include "MediaContainerType.h" 10 #include "MediaDecoder.h" 11 #include "mozilla/StaticPrefs_media.h" 12 #include "nsMimeTypes.h" 13 14 namespace mozilla { 15 16 /* static */ 17 bool OggDecoder::IsSupportedType(const MediaContainerType& aContainerType) { 18 if (!StaticPrefs::media_ogg_enabled()) { 19 return false; 20 } 21 22 if (aContainerType.Type() != MEDIAMIMETYPE(AUDIO_OGG) && 23 aContainerType.Type() != MEDIAMIMETYPE("application/ogg")) { 24 return false; 25 } 26 27 const MediaCodecs& codecs = aContainerType.ExtendedType().Codecs(); 28 if (codecs.IsEmpty()) { 29 // Ogg guarantees that the only codecs it contained are supported. 30 return true; 31 } 32 // Verify that all the codecs specified are ones that we expect that 33 // we can play. 34 for (const auto& codec : codecs.Range()) { 35 if ((MediaDecoder::IsOpusEnabled() && codec.EqualsLiteral("opus")) || 36 codec.EqualsLiteral("vorbis") || codec.EqualsLiteral("flac")) { 37 continue; 38 } 39 // Some unsupported codec. 40 return false; 41 } 42 return true; 43 } 44 45 /* static */ 46 nsTArray<UniquePtr<TrackInfo>> OggDecoder::GetTracksInfo( 47 const MediaContainerType& aType) { 48 nsTArray<UniquePtr<TrackInfo>> tracks; 49 if (!IsSupportedType(aType)) { 50 return tracks; 51 } 52 53 const MediaCodecs& codecs = aType.ExtendedType().Codecs(); 54 if (codecs.IsEmpty()) { 55 // Codecs must be specified for ogg as it can't be implied. 56 return tracks; 57 } 58 59 for (const auto& codec : codecs.Range()) { 60 if (codec.EqualsLiteral("opus") || codec.EqualsLiteral("vorbis") || 61 codec.EqualsLiteral("flac")) { 62 tracks.AppendElement( 63 CreateTrackInfoWithMIMETypeAndContainerTypeExtraParameters( 64 "audio/"_ns + NS_ConvertUTF16toUTF8(codec), aType)); 65 } 66 } 67 return tracks; 68 } 69 70 } // namespace mozilla