[M120 Migration][hbbtv] Audio tracks count notification
[platform/framework/web/chromium-efl.git] / media / filters / hls_rendition.cc
1 // Copyright 2023 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "media/filters/hls_rendition.h"
6
7 #include "media/filters/hls_live_rendition.h"
8 #include "media/filters/hls_vod_rendition.h"
9 #include "media/filters/manifest_demuxer.h"
10
11 namespace media {
12
13 namespace {
14
15 absl::optional<base::TimeDelta> GetPlaylistDuration(
16     hls::MediaPlaylist* playlist) {
17   if (!playlist->HasMediaSequenceTag()) {
18     // Live playbacks have a media sequence tag, so if that's missing, then this
19     // playback is VOD, and we can use it's computed duration.
20     return playlist->GetComputedDuration();
21   }
22
23   auto segments = playlist->GetSegments();
24   // Usually manifests use an Endlist tag to end a live playback, but its
25   // also fairly common to see these on VOD content where the first media
26   // sequence is id 0 or 1.
27   if (playlist->IsEndList()) {
28     if (!segments.empty() && segments[0]->GetMediaSequenceNumber() < 2) {
29       return playlist->GetComputedDuration();
30     }
31   }
32
33   // Live content doesn't have a defined duration.
34   return absl::nullopt;
35 }
36
37 }  // namespace
38
39 HlsRenditionHost::~HlsRenditionHost() = default;
40
41 // Static
42 HlsDemuxerStatus::Or<std::unique_ptr<HlsRendition>>
43 HlsRendition::CreateRendition(ManifestDemuxerEngineHost* engine_host,
44                               HlsRenditionHost* rendition_host,
45                               std::string role,
46                               scoped_refptr<hls::MediaPlaylist> playlist,
47                               GURL uri) {
48   std::unique_ptr<HlsRendition> rendition;
49   auto duration = GetPlaylistDuration(playlist.get());
50   if (duration.has_value()) {
51     rendition = std::make_unique<HlsVodRendition>(
52         engine_host, rendition_host, std::move(role), std::move(playlist),
53         duration.value());
54   } else {
55     rendition = std::make_unique<HlsLiveRendition>(
56         engine_host, rendition_host, role, std::move(playlist), uri);
57   }
58   return rendition;
59 }
60
61 }  // namespace media