HTMLMediaElement.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * Copyright (c) 2020, the SerenityOS developers.
  3. * Copyright (c) 2023, Tim Flynn <trflynn89@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <AK/ByteBuffer.h>
  9. #include <AK/Variant.h>
  10. #include <LibJS/SafeFunction.h>
  11. #include <LibWeb/HTML/EventLoop/Task.h>
  12. #include <LibWeb/HTML/HTMLElement.h>
  13. #include <math.h>
  14. namespace Web::HTML {
  15. class HTMLMediaElement : public HTMLElement {
  16. WEB_PLATFORM_OBJECT(HTMLMediaElement, HTMLElement);
  17. public:
  18. virtual ~HTMLMediaElement() override;
  19. void queue_a_media_element_task(JS::SafeFunction<void()> steps);
  20. enum class NetworkState : u16 {
  21. Empty,
  22. Idle,
  23. Loading,
  24. NoSource,
  25. };
  26. NetworkState network_state() const { return m_network_state; }
  27. WebIDL::ExceptionOr<Bindings::CanPlayTypeResult> can_play_type(DeprecatedString const& type) const;
  28. enum class ReadyState : u16 {
  29. HaveNothing,
  30. HaveMetadata,
  31. HaveCurrentData,
  32. HaveFutureData,
  33. HaveEnoughData,
  34. };
  35. ReadyState ready_state() const { return m_ready_state; }
  36. WebIDL::ExceptionOr<void> load();
  37. double duration() const;
  38. bool paused() const { return m_paused; }
  39. void pause() const;
  40. JS::NonnullGCPtr<VideoTrackList> video_tracks() const { return *m_video_tracks; }
  41. protected:
  42. HTMLMediaElement(DOM::Document&, DOM::QualifiedName);
  43. virtual JS::ThrowCompletionOr<void> initialize(JS::Realm&) override;
  44. virtual void visit_edges(Cell::Visitor&) override;
  45. // Override in subclasses to handle implementation-specific behavior when the element state changes
  46. // to playing or paused, e.g. to start/stop play timers.
  47. virtual void on_playing() { }
  48. virtual void on_paused() { }
  49. private:
  50. struct EntireResource { };
  51. using ByteRange = Variant<EntireResource>; // FIXME: This will need to include "until end" and an actual byte range.
  52. virtual void parse_attribute(DeprecatedFlyString const& name, DeprecatedString const& value) override;
  53. Task::Source media_element_event_task_source() const { return m_media_element_event_task_source.source; }
  54. WebIDL::ExceptionOr<void> load_element();
  55. WebIDL::ExceptionOr<void> select_resource();
  56. WebIDL::ExceptionOr<void> fetch_resource(AK::URL const&, Function<void()> failure_callback);
  57. static bool verify_response(JS::NonnullGCPtr<Fetch::Infrastructure::Response>, ByteRange const&);
  58. WebIDL::ExceptionOr<void> process_media_data(Function<void()> failure_callback);
  59. WebIDL::ExceptionOr<void> handle_media_source_failure();
  60. void forget_media_resource_specific_tracks();
  61. void set_ready_state(ReadyState);
  62. void notify_about_playing();
  63. void set_paused(bool);
  64. void set_duration(double);
  65. // https://html.spec.whatwg.org/multipage/media.html#media-element-event-task-source
  66. UniqueTaskSource m_media_element_event_task_source {};
  67. // https://html.spec.whatwg.org/multipage/media.html#dom-media-networkstate
  68. NetworkState m_network_state { NetworkState::Empty };
  69. // https://html.spec.whatwg.org/multipage/media.html#dom-media-readystate
  70. ReadyState m_ready_state { ReadyState::HaveNothing };
  71. bool m_first_data_load_event_since_load_start { false };
  72. // https://html.spec.whatwg.org/multipage/media.html#dom-media-duration
  73. double m_duration { NAN };
  74. // https://html.spec.whatwg.org/multipage/media.html#dom-media-paused
  75. bool m_paused { true };
  76. // https://html.spec.whatwg.org/multipage/media.html#dom-media-videotracks
  77. JS::GCPtr<VideoTrackList> m_video_tracks;
  78. // https://html.spec.whatwg.org/multipage/media.html#media-data
  79. ByteBuffer m_media_data;
  80. JS::GCPtr<Fetch::Infrastructure::FetchController> m_fetch_controller;
  81. };
  82. }