Home | History | Annotate | Download | only in test
      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 
      5 // A fake media source that generates video and audio frames to a cast
      6 // sender.
      7 // This class can transcode a WebM file using FFmpeg. It can also
      8 // generate an animation and audio of fixed frequency.
      9 
     10 #ifndef MEDIA_CAST_TEST_FAKE_MEDIA_SOURCE_H_
     11 #define MEDIA_CAST_TEST_FAKE_MEDIA_SOURCE_H_
     12 
     13 #include <queue>
     14 
     15 #include "base/files/file_path.h"
     16 #include "base/files/memory_mapped_file.h"
     17 #include "base/memory/ref_counted.h"
     18 #include "base/memory/weak_ptr.h"
     19 #include "base/single_thread_task_runner.h"
     20 #include "base/time/tick_clock.h"
     21 #include "media/audio/audio_parameters.h"
     22 #include "media/cast/cast_config.h"
     23 #include "media/filters/audio_renderer_algorithm.h"
     24 #include "media/filters/ffmpeg_demuxer.h"
     25 
     26 struct AVCodecContext;
     27 struct AVFormatContext;
     28 
     29 namespace media {
     30 
     31 class AudioBus;
     32 class AudioFifo;
     33 class AudioTimestampHelper;
     34 class FFmpegGlue;
     35 class InMemoryUrlProtocol;
     36 class MultiChannelResampler;
     37 
     38 namespace cast {
     39 
     40 class AudioFrameInput;
     41 class VideoFrameInput;
     42 class TestAudioBusFactory;
     43 
     44 class FakeMediaSource {
     45  public:
     46   // |task_runner| is to schedule decoding tasks.
     47   // |clock| is used by this source but is not owned.
     48   // |video_config| is the desired video config.
     49   FakeMediaSource(scoped_refptr<base::SingleThreadTaskRunner> task_runner,
     50                   base::TickClock* clock,
     51                   const VideoSenderConfig& video_config);
     52   ~FakeMediaSource();
     53 
     54   // Transcode this file as the source of video and audio frames.
     55   // If |override_fps| is non zero then the file is played at the desired rate.
     56   void SetSourceFile(const base::FilePath& video_file, int override_fps);
     57 
     58   void Start(scoped_refptr<AudioFrameInput> audio_frame_input,
     59              scoped_refptr<VideoFrameInput> video_frame_input);
     60 
     61   const VideoSenderConfig& get_video_config() const { return video_config_; }
     62 
     63  private:
     64   bool is_transcoding_audio() const { return audio_stream_index_ >= 0; }
     65   bool is_transcoding_video() const { return video_stream_index_ >= 0; }
     66 
     67   void SendNextFrame();
     68   void SendNextFakeFrame();
     69 
     70   // Return true if a frame was sent.
     71   bool SendNextTranscodedVideo(base::TimeDelta elapsed_time);
     72 
     73   // Return true if a frame was sent.
     74   bool SendNextTranscodedAudio(base::TimeDelta elapsed_time);
     75 
     76   // Helper methods to compute timestamps for the frame number specified.
     77   base::TimeDelta VideoFrameTime(int frame_number);
     78 
     79   base::TimeDelta ScaleTimestamp(base::TimeDelta timestamp);
     80 
     81   base::TimeDelta AudioFrameTime(int frame_number);
     82 
     83   // Go to the beginning of the stream.
     84   void Rewind();
     85 
     86   // Call FFmpeg to fetch one packet.
     87   ScopedAVPacket DemuxOnePacket(bool* audio);
     88 
     89   void DecodeAudio(ScopedAVPacket packet);
     90   void DecodeVideo(ScopedAVPacket packet);
     91   void Decode(bool decode_audio);
     92 
     93   void ProvideData(int frame_delay, media::AudioBus* output_bus);
     94 
     95   AVStream* av_audio_stream();
     96   AVStream* av_video_stream();
     97   AVCodecContext* av_audio_context();
     98   AVCodecContext* av_video_context();
     99 
    100   scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
    101   VideoSenderConfig video_config_;
    102   scoped_refptr<AudioFrameInput> audio_frame_input_;
    103   scoped_refptr<VideoFrameInput> video_frame_input_;
    104   uint8 synthetic_count_;
    105   base::TickClock* const clock_;  // Not owned by this class.
    106 
    107   // Time when the stream starts.
    108   base::TimeTicks start_time_;
    109 
    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   scoped_ptr<TestAudioBusFactory> audio_bus_factory_;
    114 
    115   // NOTE: Weak pointers must be invalidated before all other member variables.
    116   base::WeakPtrFactory<FakeMediaSource> weak_factory_;
    117 
    118   base::MemoryMappedFile file_data_;
    119   scoped_ptr<InMemoryUrlProtocol> protocol_;
    120   scoped_ptr<FFmpegGlue> glue_;
    121   AVFormatContext* av_format_context_;
    122 
    123   int audio_stream_index_;
    124   AudioParameters audio_params_;
    125   double playback_rate_;
    126 
    127   int video_stream_index_;
    128   int video_frame_rate_numerator_;
    129   int video_frame_rate_denominator_;
    130 
    131   // These are used for audio resampling.
    132   scoped_ptr<media::MultiChannelResampler> audio_resampler_;
    133   scoped_ptr<media::AudioFifo> audio_fifo_;
    134   scoped_ptr<media::AudioBus> audio_fifo_input_bus_;
    135   media::AudioRendererAlgorithm audio_algo_;
    136 
    137   // Track the timestamp of audio sent to the receiver.
    138   scoped_ptr<media::AudioTimestampHelper> audio_sent_ts_;
    139 
    140   std::queue<scoped_refptr<VideoFrame> > video_frame_queue_;
    141   int64 video_first_pts_;
    142   bool video_first_pts_set_;
    143 
    144   std::queue<AudioBus*> audio_bus_queue_;
    145 
    146   DISALLOW_COPY_AND_ASSIGN(FakeMediaSource);
    147 };
    148 
    149 }  // namespace cast
    150 }  // namespace media
    151 
    152 #endif // MEDIA_CAST_TEST_FAKE_MEDIA_SOURCE_H_
    153