HTMLMediaElement.h 3.2 KB

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