HTMLMediaElement.h 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. void pause() const;
  39. JS::NonnullGCPtr<VideoTrackList> video_tracks() const { return *m_video_tracks; }
  40. protected:
  41. HTMLMediaElement(DOM::Document&, DOM::QualifiedName);
  42. virtual JS::ThrowCompletionOr<void> initialize(JS::Realm&) override;
  43. virtual void visit_edges(Cell::Visitor&) override;
  44. private:
  45. struct EntireResource { };
  46. using ByteRange = Variant<EntireResource>; // FIXME: This will need to include "until end" and an actual byte range.
  47. virtual void parse_attribute(DeprecatedFlyString const& name, DeprecatedString const& value) override;
  48. Task::Source media_element_event_task_source() const { return m_media_element_event_task_source.source; }
  49. WebIDL::ExceptionOr<void> load_element();
  50. WebIDL::ExceptionOr<void> select_resource();
  51. WebIDL::ExceptionOr<void> fetch_resource(AK::URL const&, Function<void()> failure_callback);
  52. static bool verify_response(JS::NonnullGCPtr<Fetch::Infrastructure::Response>, ByteRange const&);
  53. WebIDL::ExceptionOr<void> process_media_data(Function<void()> failure_callback);
  54. WebIDL::ExceptionOr<void> handle_media_source_failure();
  55. void forget_media_resource_specific_tracks();
  56. void set_ready_state(ReadyState);
  57. void set_duration(double);
  58. // https://html.spec.whatwg.org/multipage/media.html#media-element-event-task-source
  59. UniqueTaskSource m_media_element_event_task_source {};
  60. // https://html.spec.whatwg.org/multipage/media.html#dom-media-networkstate
  61. NetworkState m_network_state { NetworkState::Empty };
  62. // https://html.spec.whatwg.org/multipage/media.html#dom-media-readystate
  63. ReadyState m_ready_state { ReadyState::HaveNothing };
  64. bool m_first_data_load_event_since_load_start { false };
  65. // https://html.spec.whatwg.org/multipage/media.html#dom-media-duration
  66. double m_duration { NAN };
  67. // https://html.spec.whatwg.org/multipage/media.html#dom-media-videotracks
  68. JS::GCPtr<VideoTrackList> m_video_tracks;
  69. // https://html.spec.whatwg.org/multipage/media.html#media-data
  70. ByteBuffer m_media_data;
  71. JS::GCPtr<Fetch::Infrastructure::FetchController> m_fetch_controller;
  72. };
  73. }