Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / media / formats / mp2t / mp2t_stream_parser.h
blob2bd7fa5941f0774920804a9e2000d426de92bceb
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_
8 #include <list>
9 #include <map>
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"
18 #include "media/formats/mp2t/timestamp_unroller.h"
20 namespace media {
22 class StreamParserBuffer;
24 namespace mp2t {
26 class PidState;
28 class MEDIA_EXPORT Mp2tStreamParser : public StreamParser {
29 public:
30 explicit Mp2tStreamParser(bool sbr_in_mimetype);
31 ~Mp2tStreamParser() override;
33 // StreamParser implementation.
34 void Init(const InitCB& init_cb,
35 const NewConfigCB& config_cb,
36 const NewBuffersCB& new_buffers_cb,
37 bool ignore_text_tracks,
38 const EncryptedMediaInitDataCB& encrypted_media_init_data_cb,
39 const NewMediaSegmentCB& new_segment_cb,
40 const base::Closure& end_of_segment_cb,
41 const scoped_refptr<MediaLog>& media_log) override;
42 void Flush() override;
43 bool Parse(const uint8* buf, int size) override;
45 private:
46 typedef std::map<int, PidState*> PidMap;
48 struct BufferQueueWithConfig {
49 BufferQueueWithConfig(bool is_cfg_sent,
50 const AudioDecoderConfig& audio_cfg,
51 const VideoDecoderConfig& video_cfg);
52 ~BufferQueueWithConfig();
54 bool is_config_sent;
55 AudioDecoderConfig audio_config;
56 StreamParser::BufferQueue audio_queue;
57 VideoDecoderConfig video_config;
58 StreamParser::BufferQueue video_queue;
61 // Callback invoked to register a Program Map Table.
62 // Note: Does nothing if the PID is already registered.
63 void RegisterPmt(int program_number, int pmt_pid);
65 // Callback invoked to register a PES pid.
66 // Possible values for |stream_type| are defined in:
67 // ISO-13818.1 / ITU H.222 Table 2.34 "Stream type assignments".
68 // |pes_pid| is part of the Program Map Table refered by |pmt_pid|.
69 void RegisterPes(int pmt_pid, int pes_pid, int stream_type);
71 // Since the StreamParser interface allows only one audio & video streams,
72 // an automatic PID filtering should be applied to select the audio & video
73 // streams.
74 void UpdatePidFilter();
76 // Callback invoked each time the audio/video decoder configuration is
77 // changed.
78 void OnVideoConfigChanged(int pes_pid,
79 const VideoDecoderConfig& video_decoder_config);
80 void OnAudioConfigChanged(int pes_pid,
81 const AudioDecoderConfig& audio_decoder_config);
83 // Invoke the initialization callback if needed.
84 bool FinishInitializationIfNeeded();
86 // Callback invoked by the ES stream parser
87 // to emit a new audio/video access unit.
88 void OnEmitAudioBuffer(
89 int pes_pid,
90 scoped_refptr<StreamParserBuffer> stream_parser_buffer);
91 void OnEmitVideoBuffer(
92 int pes_pid,
93 scoped_refptr<StreamParserBuffer> stream_parser_buffer);
94 bool EmitRemainingBuffers();
96 // List of callbacks.
97 InitCB init_cb_;
98 NewConfigCB config_cb_;
99 NewBuffersCB new_buffers_cb_;
100 EncryptedMediaInitDataCB encrypted_media_init_data_cb_;
101 NewMediaSegmentCB new_segment_cb_;
102 base::Closure end_of_segment_cb_;
103 scoped_refptr<MediaLog> media_log_;
105 // True when AAC SBR extension is signalled in the mimetype
106 // (mp4a.40.5 in the codecs parameter).
107 bool sbr_in_mimetype_;
109 // Bytes of the TS stream.
110 ByteQueue ts_byte_queue_;
112 // List of PIDs and their state.
113 PidMap pids_;
115 // Selected audio and video PIDs.
116 int selected_audio_pid_;
117 int selected_video_pid_;
119 // Pending audio & video buffers.
120 std::list<BufferQueueWithConfig> buffer_queue_chain_;
122 // Whether |init_cb_| has been invoked.
123 bool is_initialized_;
125 // Indicate whether a segment was started.
126 bool segment_started_;
128 // Timestamp unroller.
129 // Timestamps in PES packets must be unrolled using the same offset.
130 // So the unroller is global between PES pids.
131 TimestampUnroller timestamp_unroller_;
133 DISALLOW_COPY_AND_ASSIGN(Mp2tStreamParser);
136 } // namespace mp2t
137 } // namespace media
139 #endif