fake_media_source.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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.
  4. // A fake media source that generates video and audio frames to a cast
  5. // sender.
  6. // This class can transcode a WebM file using FFmpeg. It can also
  7. // generate an animation and audio of fixed frequency.
  8. #ifndef MEDIA_CAST_TEST_FAKE_MEDIA_SOURCE_H_
  9. #define MEDIA_CAST_TEST_FAKE_MEDIA_SOURCE_H_
  10. #include <stdint.h>
  11. #include "base/containers/queue.h"
  12. #include "base/files/file_path.h"
  13. #include "base/files/memory_mapped_file.h"
  14. #include "base/memory/raw_ptr.h"
  15. #include "base/memory/ref_counted.h"
  16. #include "base/memory/weak_ptr.h"
  17. #include "base/task/single_thread_task_runner.h"
  18. #include "base/time/tick_clock.h"
  19. #include "base/time/time.h"
  20. #include "media/base/audio_converter.h"
  21. #include "media/base/audio_parameters.h"
  22. #include "media/base/media_util.h"
  23. #include "media/cast/cast_config.h"
  24. #include "media/ffmpeg/scoped_av_packet.h"
  25. #include "media/filters/audio_renderer_algorithm.h"
  26. struct AVCodecContext;
  27. struct AVFormatContext;
  28. struct AVFrame;
  29. struct AVStream;
  30. namespace media {
  31. class AudioBus;
  32. class AudioConverter;
  33. class AudioFifo;
  34. class AudioTimestampHelper;
  35. class FFmpegGlue;
  36. class FFmpegDecodingLoop;
  37. class InMemoryUrlProtocol;
  38. class VideoFrame;
  39. struct ScopedPtrAVFreeContext;
  40. namespace cast {
  41. class AudioFrameInput;
  42. class VideoFrameInput;
  43. class TestAudioBusFactory;
  44. class FakeMediaSource final : public media::AudioConverter::InputCallback {
  45. public:
  46. // |task_runner| is to schedule decoding tasks.
  47. // |clock| is used by this source but is not owned.
  48. // |audio_config| is the desired audio config.
  49. // |video_config| is the desired video config.
  50. // |keep_frames| is true if all VideoFrames are saved in a queue.
  51. FakeMediaSource(scoped_refptr<base::SingleThreadTaskRunner> task_runner,
  52. const base::TickClock* clock,
  53. const FrameSenderConfig& audio_config,
  54. const FrameSenderConfig& video_config,
  55. bool keep_frames);
  56. FakeMediaSource(const FakeMediaSource&) = delete;
  57. FakeMediaSource& operator=(const FakeMediaSource&) = delete;
  58. ~FakeMediaSource() final;
  59. // Transcode this file as the source of video and audio frames.
  60. // If |final_fps| is non zero then the file is played at the desired rate.
  61. void SetSourceFile(const base::FilePath& video_file, int final_fps);
  62. // Set to true to randomly change the frame size at random points in time.
  63. // Only applies when SetSourceFile() is not used.
  64. void SetVariableFrameSizeMode(bool enabled);
  65. void Start(scoped_refptr<AudioFrameInput> audio_frame_input,
  66. scoped_refptr<VideoFrameInput> video_frame_input);
  67. const FrameSenderConfig& get_video_config() const { return video_config_; }
  68. scoped_refptr<media::VideoFrame> PopOldestInsertedVideoFrame();
  69. private:
  70. bool is_transcoding_audio() const { return audio_stream_index_ >= 0; }
  71. bool is_transcoding_video() const { return video_stream_index_ >= 0; }
  72. void SendNextFrame();
  73. void SendNextFakeFrame();
  74. void UpdateNextFrameSize();
  75. // Return true if a frame was sent.
  76. bool SendNextTranscodedVideo(base::TimeDelta elapsed_time);
  77. // Return true if a frame was sent.
  78. bool SendNextTranscodedAudio(base::TimeDelta elapsed_time);
  79. // Helper methods to compute timestamps for the frame number specified.
  80. base::TimeDelta VideoFrameTime(int frame_number);
  81. base::TimeDelta ScaleTimestamp(base::TimeDelta timestamp);
  82. base::TimeDelta AudioFrameTime(int frame_number);
  83. // Go to the beginning of the stream.
  84. void Rewind();
  85. // Call FFmpeg to fetch one packet.
  86. ScopedAVPacket DemuxOnePacket(bool* audio);
  87. void DecodeAudio(ScopedAVPacket packet);
  88. bool OnNewAudioFrame(AVFrame* frame);
  89. void DecodeVideo(ScopedAVPacket packet);
  90. bool OnNewVideoFrame(AVFrame* frame);
  91. void Decode(bool decode_audio);
  92. // media::AudioConverter::InputCallback implementation.
  93. double ProvideInput(media::AudioBus* output_bus,
  94. uint32_t frames_delayed) final;
  95. AVStream* av_audio_stream();
  96. AVStream* av_video_stream();
  97. const scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
  98. const media::AudioParameters output_audio_params_;
  99. const FrameSenderConfig video_config_;
  100. const bool keep_frames_;
  101. bool variable_frame_size_mode_;
  102. gfx::Size current_frame_size_;
  103. base::TimeTicks next_frame_size_change_time_;
  104. scoped_refptr<AudioFrameInput> audio_frame_input_;
  105. scoped_refptr<VideoFrameInput> video_frame_input_;
  106. uint8_t synthetic_count_;
  107. const raw_ptr<const base::TickClock> clock_; // Not owned by this class.
  108. // Time when the stream starts.
  109. base::TimeTicks start_time_;
  110. // The following three members are used only for fake frames.
  111. int audio_frame_count_; // Each audio frame is exactly 10ms.
  112. int video_frame_count_;
  113. std::unique_ptr<TestAudioBusFactory> audio_bus_factory_;
  114. base::MemoryMappedFile file_data_;
  115. std::unique_ptr<InMemoryUrlProtocol> protocol_;
  116. std::unique_ptr<FFmpegGlue> glue_;
  117. raw_ptr<AVFormatContext> av_format_context_;
  118. int audio_stream_index_;
  119. std::unique_ptr<AVCodecContext, ScopedPtrAVFreeContext> av_audio_context_;
  120. std::unique_ptr<FFmpegDecodingLoop> audio_decoding_loop_;
  121. AudioParameters source_audio_params_;
  122. double playback_rate_;
  123. int video_stream_index_;
  124. std::unique_ptr<AVCodecContext, ScopedPtrAVFreeContext> av_video_context_;
  125. std::unique_ptr<FFmpegDecodingLoop> video_decoding_loop_;
  126. int video_frame_rate_numerator_;
  127. int video_frame_rate_denominator_;
  128. // These are used for audio resampling.
  129. std::unique_ptr<media::AudioConverter> audio_converter_;
  130. std::unique_ptr<media::AudioFifo> audio_fifo_;
  131. std::unique_ptr<media::AudioBus> audio_fifo_input_bus_;
  132. media::AudioRendererAlgorithm audio_algo_;
  133. media::NullMediaLog media_log_;
  134. // Track the timestamp of audio sent to the receiver.
  135. std::unique_ptr<media::AudioTimestampHelper> audio_sent_ts_;
  136. base::queue<scoped_refptr<VideoFrame>> video_frame_queue_;
  137. base::queue<scoped_refptr<VideoFrame>> inserted_video_frame_queue_;
  138. int64_t video_first_pts_;
  139. bool video_first_pts_set_;
  140. base::TimeDelta last_video_frame_timestamp_;
  141. base::queue<AudioBus*> audio_bus_queue_;
  142. // NOTE: Weak pointers must be invalidated before all other member variables.
  143. base::WeakPtrFactory<FakeMediaSource> weak_factory_{this};
  144. };
  145. } // namespace cast
  146. } // namespace media
  147. #endif // MEDIA_CAST_TEST_FAKE_MEDIA_SOURCE_H_