webcodecs_encoded_chunk_stream_parser.h 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // Copyright 2020 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.
  4. #ifndef MEDIA_FORMATS_WEBCODECS_WEBCODECS_ENCODED_CHUNK_STREAM_PARSER_H_
  5. #define MEDIA_FORMATS_WEBCODECS_WEBCODECS_ENCODED_CHUNK_STREAM_PARSER_H_
  6. #include <stdint.h>
  7. #include <memory>
  8. #include "base/memory/raw_ptr.h"
  9. #include "base/memory/ref_counted.h"
  10. #include "media/base/audio_decoder_config.h"
  11. #include "media/base/media_export.h"
  12. #include "media/base/stream_parser.h"
  13. #include "media/base/video_decoder_config.h"
  14. namespace media {
  15. class MEDIA_EXPORT WebCodecsEncodedChunkStreamParser : public StreamParser {
  16. public:
  17. explicit WebCodecsEncodedChunkStreamParser(
  18. std::unique_ptr<AudioDecoderConfig> audio_config);
  19. explicit WebCodecsEncodedChunkStreamParser(
  20. std::unique_ptr<VideoDecoderConfig> video_config);
  21. WebCodecsEncodedChunkStreamParser(const WebCodecsEncodedChunkStreamParser&) =
  22. delete;
  23. WebCodecsEncodedChunkStreamParser& operator=(
  24. const WebCodecsEncodedChunkStreamParser&) = delete;
  25. ~WebCodecsEncodedChunkStreamParser() override;
  26. // StreamParser implementation.
  27. void Init(InitCB init_cb,
  28. const NewConfigCB& config_cb,
  29. const NewBuffersCB& new_buffers_cb,
  30. bool ignore_text_tracks /* must be true */,
  31. const EncryptedMediaInitDataCB& encrypted_media_init_data_cb,
  32. const NewMediaSegmentCB& new_segment_cb,
  33. const EndMediaSegmentCB& end_of_segment_cb,
  34. MediaLog* media_log) override;
  35. void Flush() override;
  36. bool GetGenerateTimestampsFlag() const override;
  37. bool Parse(const uint8_t* buf, int size) override;
  38. // Processes and emits buffers from |buffer_queue|. If state is
  39. // kWaitingForConfigEmission, first emit the config.
  40. bool ProcessChunks(std::unique_ptr<BufferQueue> buffer_queue) override;
  41. private:
  42. enum State {
  43. kWaitingForInit,
  44. kWaitingForConfigEmission,
  45. kWaitingForEncodedChunks,
  46. kError
  47. };
  48. void ChangeState(State new_state);
  49. State state_;
  50. // These configs are populated during ctor. A copy of the appropriate config
  51. // is emitted on demand when "parsing" newly appended encoded chunks if that
  52. // append occurs when state is kWaitingForConfigEmission. Note, only one type
  53. // of config can be emitted (not both), for an instance of this parser.
  54. std::unique_ptr<AudioDecoderConfig> audio_config_;
  55. std::unique_ptr<VideoDecoderConfig> video_config_;
  56. InitCB init_cb_;
  57. NewConfigCB config_cb_;
  58. NewBuffersCB new_buffers_cb_;
  59. NewMediaSegmentCB new_segment_cb_;
  60. EndMediaSegmentCB end_of_segment_cb_;
  61. raw_ptr<MediaLog> media_log_;
  62. };
  63. } // namespace media
  64. #endif // MEDIA_FORMATS_WEBCODECS_WEBCODECS_ENCODED_CHUNK_STREAM_PARSER_H_