cast_sender.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // Copyright 2013 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. // This is the main interface for the cast sender.
  6. //
  7. // The AudioFrameInput, VideoFrameInput and PacketReciever interfaces should
  8. // be accessed from the main thread.
  9. #ifndef MEDIA_CAST_CAST_SENDER_H_
  10. #define MEDIA_CAST_CAST_SENDER_H_
  11. #include <memory>
  12. #include "base/callback.h"
  13. #include "base/memory/ref_counted.h"
  14. #include "base/time/tick_clock.h"
  15. #include "base/time/time.h"
  16. #include "media/base/audio_bus.h"
  17. #include "media/base/video_frame.h"
  18. #include "media/cast/cast_callbacks.h"
  19. #include "media/cast/cast_config.h"
  20. #include "media/cast/cast_environment.h"
  21. #include "media/cast/constants.h"
  22. #include "media/cast/net/cast_transport.h"
  23. namespace gfx {
  24. class Size;
  25. }
  26. namespace media {
  27. namespace cast {
  28. class VideoFrameInput : public base::RefCountedThreadSafe<VideoFrameInput> {
  29. public:
  30. // Insert video frames into Cast sender. Frames will be encoded, packetized
  31. // and sent to the network.
  32. virtual void InsertRawVideoFrame(scoped_refptr<media::VideoFrame> video_frame,
  33. base::TimeTicks capture_time) = 0;
  34. // Creates a |VideoFrame| optimized for the encoder. When available, these
  35. // frames offer performance benefits, such as memory copy elimination. The
  36. // format is guaranteed to be I420 or NV12.
  37. //
  38. // Not every encoder supports this method. Use |CanCreateOptimizedFrames| to
  39. // determine if you can and should use this method.
  40. //
  41. // Even if |CanCreateOptimizedFrames| indicates support, there are transient
  42. // conditions during a session where optimized frames cannot be provided. In
  43. // this case, the caller must be able to account for a nullptr return value
  44. // and instantiate its own media::VideoFrames.
  45. virtual scoped_refptr<VideoFrame> MaybeCreateOptimizedFrame(
  46. const gfx::Size& frame_size, base::TimeDelta timestamp) = 0;
  47. // Returns true if the encoder supports creating optimized frames.
  48. virtual bool CanCreateOptimizedFrames() const = 0;
  49. protected:
  50. virtual ~VideoFrameInput() {}
  51. private:
  52. friend class base::RefCountedThreadSafe<VideoFrameInput>;
  53. };
  54. class AudioFrameInput : public base::RefCountedThreadSafe<AudioFrameInput> {
  55. public:
  56. // Insert audio frames into Cast sender. Frames will be encoded, packetized
  57. // and sent to the network.
  58. virtual void InsertAudio(std::unique_ptr<AudioBus> audio_bus,
  59. const base::TimeTicks& recorded_time) = 0;
  60. protected:
  61. virtual ~AudioFrameInput() {}
  62. private:
  63. friend class base::RefCountedThreadSafe<AudioFrameInput>;
  64. };
  65. // All methods of CastSender must be called on the main thread.
  66. // Provided CastTransport will also be called on the main thread.
  67. class CastSender {
  68. public:
  69. static std::unique_ptr<CastSender> Create(
  70. scoped_refptr<CastEnvironment> cast_environment,
  71. CastTransport* const transport_sender);
  72. virtual ~CastSender() {}
  73. // All video frames for the session should be inserted to this object.
  74. virtual scoped_refptr<VideoFrameInput> video_frame_input() = 0;
  75. // All audio frames for the session should be inserted to this object.
  76. virtual scoped_refptr<AudioFrameInput> audio_frame_input() = 0;
  77. // Initialize the audio stack. Must be called in order to send audio frames.
  78. // |status_change_cb| will be run as operational status changes.
  79. virtual void InitializeAudio(const FrameSenderConfig& audio_config,
  80. StatusChangeOnceCallback status_change_cb) = 0;
  81. // Initialize the video stack. Must be called in order to send video frames.
  82. // |status_change_cb| will be run as operational status changes.
  83. virtual void InitializeVideo(
  84. const FrameSenderConfig& video_config,
  85. const StatusChangeCallback& status_change_cb,
  86. const CreateVideoEncodeAcceleratorCallback& create_vea_cb) = 0;
  87. // Change the target delay. This is only valid if the receiver
  88. // supports the "adaptive_target_delay" rtp extension.
  89. virtual void SetTargetPlayoutDelay(
  90. base::TimeDelta new_target_playout_delay) = 0;
  91. };
  92. } // namespace cast
  93. } // namespace media
  94. #endif // MEDIA_CAST_CAST_SENDER_H_