1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef MEDIA_FORMATS_MP2T_MP2T_STREAM_PARSER_H_
6 #define MEDIA_FORMATS_MP2T_MP2T_STREAM_PARSER_H_
11 #include "base/memory/ref_counted.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "media/base/audio_decoder_config.h"
14 #include "media/base/byte_queue.h"
15 #include "media/base/media_export.h"
16 #include "media/base/stream_parser.h"
17 #include "media/base/video_decoder_config.h"
21 class StreamParserBuffer;
27 class MEDIA_EXPORT Mp2tStreamParser : public StreamParser {
29 explicit Mp2tStreamParser(bool sbr_in_mimetype);
30 virtual ~Mp2tStreamParser();
32 // StreamParser implementation.
33 virtual void Init(const InitCB& init_cb,
34 const NewConfigCB& config_cb,
35 const NewBuffersCB& new_buffers_cb,
36 bool ignore_text_tracks,
37 const NeedKeyCB& need_key_cb,
38 const NewMediaSegmentCB& new_segment_cb,
39 const base::Closure& end_of_segment_cb,
40 const LogCB& log_cb) OVERRIDE;
41 virtual void Flush() OVERRIDE;
42 virtual bool Parse(const uint8* buf, int size) OVERRIDE;
45 typedef std::map<int, PidState*> PidMap;
47 struct BufferQueueWithConfig {
48 BufferQueueWithConfig(bool is_cfg_sent,
49 const AudioDecoderConfig& audio_cfg,
50 const VideoDecoderConfig& video_cfg);
51 ~BufferQueueWithConfig();
54 AudioDecoderConfig audio_config;
55 StreamParser::BufferQueue audio_queue;
56 VideoDecoderConfig video_config;
57 StreamParser::BufferQueue video_queue;
60 // Callback invoked to register a Program Map Table.
61 // Note: Does nothing if the PID is already registered.
62 void RegisterPmt(int program_number, int pmt_pid);
64 // Callback invoked to register a PES pid.
65 // Possible values for |stream_type| are defined in:
66 // ISO-13818.1 / ITU H.222 Table 2.34 "Stream type assignments".
67 // |pes_pid| is part of the Program Map Table refered by |pmt_pid|.
68 void RegisterPes(int pmt_pid, int pes_pid, int stream_type);
70 // Since the StreamParser interface allows only one audio & video streams,
71 // an automatic PID filtering should be applied to select the audio & video
73 void UpdatePidFilter();
75 // Callback invoked each time the audio/video decoder configuration is
77 void OnVideoConfigChanged(int pes_pid,
78 const VideoDecoderConfig& video_decoder_config);
79 void OnAudioConfigChanged(int pes_pid,
80 const AudioDecoderConfig& audio_decoder_config);
82 // Invoke the initialization callback if needed.
83 bool FinishInitializationIfNeeded();
85 // Callback invoked by the ES stream parser
86 // to emit a new audio/video access unit.
87 void OnEmitAudioBuffer(
89 scoped_refptr<StreamParserBuffer> stream_parser_buffer);
90 void OnEmitVideoBuffer(
92 scoped_refptr<StreamParserBuffer> stream_parser_buffer);
93 bool EmitRemainingBuffers();
95 // At the beginning of a new segment, some video frames might be discarded.
96 // This function fills the hole by duplicating the first valid key frame
97 // given by |stream_parser_buffer|.
99 const scoped_refptr<StreamParserBuffer>& stream_parser_buffer);
101 // List of callbacks.
103 NewConfigCB config_cb_;
104 NewBuffersCB new_buffers_cb_;
105 NeedKeyCB need_key_cb_;
106 NewMediaSegmentCB new_segment_cb_;
107 base::Closure end_of_segment_cb_;
110 // True when AAC SBR extension is signalled in the mimetype
111 // (mp4a.40.5 in the codecs parameter).
112 bool sbr_in_mimetype_;
114 // Bytes of the TS stream.
115 ByteQueue ts_byte_queue_;
117 // List of PIDs and their state.
120 // Selected audio and video PIDs.
121 int selected_audio_pid_;
122 int selected_video_pid_;
124 // DTS of discarded buffers.
125 // Min PTS of discarded buffers.
126 std::list<base::TimeDelta> discarded_frames_dts_;
127 base::TimeDelta discarded_frames_min_pts_;
129 // Pending audio & video buffers.
130 std::list<BufferQueueWithConfig> buffer_queue_chain_;
132 // Whether |init_cb_| has been invoked.
133 bool is_initialized_;
135 // Indicate whether a segment was started.
136 bool segment_started_;
137 bool first_video_frame_in_segment_;
138 base::TimeDelta time_offset_;
140 DISALLOW_COPY_AND_ASSIGN(Mp2tStreamParser);