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 // A fake media source that generates video and audio frames to a cast
7 // This class can transcode a WebM file using FFmpeg. It can also
8 // generate an animation and audio of fixed frequency.
10 #ifndef MEDIA_CAST_TEST_FAKE_MEDIA_SOURCE_H_
11 #define MEDIA_CAST_TEST_FAKE_MEDIA_SOURCE_H_
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"
26 struct AVCodecContext
;
27 struct AVFormatContext
;
33 class AudioTimestampHelper
;
35 class InMemoryUrlProtocol
;
36 class MultiChannelResampler
;
40 class AudioFrameInput
;
41 class VideoFrameInput
;
42 class TestAudioBusFactory
;
44 class FakeMediaSource
{
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
);
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
);
58 void Start(scoped_refptr
<AudioFrameInput
> audio_frame_input
,
59 scoped_refptr
<VideoFrameInput
> video_frame_input
);
61 const VideoSenderConfig
& get_video_config() const { return video_config_
; }
64 bool is_transcoding_audio() const { return audio_stream_index_
>= 0; }
65 bool is_transcoding_video() const { return video_stream_index_
>= 0; }
68 void SendNextFakeFrame();
70 // Return true if a frame was sent.
71 bool SendNextTranscodedVideo(base::TimeDelta elapsed_time
);
73 // Return true if a frame was sent.
74 bool SendNextTranscodedAudio(base::TimeDelta elapsed_time
);
76 // Helper methods to compute timestamps for the frame number specified.
77 base::TimeDelta
VideoFrameTime(int frame_number
);
79 base::TimeDelta
ScaleTimestamp(base::TimeDelta timestamp
);
81 base::TimeDelta
AudioFrameTime(int frame_number
);
83 // Go to the beginning of the stream.
86 // Call FFmpeg to fetch one packet.
87 ScopedAVPacket
DemuxOnePacket(bool* audio
);
89 void DecodeAudio(ScopedAVPacket packet
);
90 void DecodeVideo(ScopedAVPacket packet
);
91 void Decode(bool decode_audio
);
93 void ProvideData(int frame_delay
, media::AudioBus
* output_bus
);
95 AVStream
* av_audio_stream();
96 AVStream
* av_video_stream();
97 AVCodecContext
* av_audio_context();
98 AVCodecContext
* av_video_context();
100 const scoped_refptr
<base::SingleThreadTaskRunner
> task_runner_
;
101 const 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.
107 // Time when the stream starts.
108 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 scoped_ptr
<TestAudioBusFactory
> audio_bus_factory_
;
115 base::MemoryMappedFile file_data_
;
116 scoped_ptr
<InMemoryUrlProtocol
> protocol_
;
117 scoped_ptr
<FFmpegGlue
> glue_
;
118 AVFormatContext
* av_format_context_
;
120 int audio_stream_index_
;
121 AudioParameters audio_params_
;
122 double playback_rate_
;
124 int video_stream_index_
;
125 int video_frame_rate_numerator_
;
126 int video_frame_rate_denominator_
;
128 // These are used for audio resampling.
129 scoped_ptr
<media::MultiChannelResampler
> audio_resampler_
;
130 scoped_ptr
<media::AudioFifo
> audio_fifo_
;
131 scoped_ptr
<media::AudioBus
> audio_fifo_input_bus_
;
132 media::AudioRendererAlgorithm audio_algo_
;
134 // Track the timestamp of audio sent to the receiver.
135 scoped_ptr
<media::AudioTimestampHelper
> audio_sent_ts_
;
137 std::queue
<scoped_refptr
<VideoFrame
> > video_frame_queue_
;
138 int64 video_first_pts_
;
139 bool video_first_pts_set_
;
140 base::TimeDelta last_video_frame_timestamp_
;
142 std::queue
<AudioBus
*> audio_bus_queue_
;
144 // NOTE: Weak pointers must be invalidated before all other member variables.
145 base::WeakPtrFactory
<FakeMediaSource
> weak_factory_
;
147 DISALLOW_COPY_AND_ASSIGN(FakeMediaSource
);
153 #endif // MEDIA_CAST_TEST_FAKE_MEDIA_SOURCE_H_