From 2568383352b7d4213ebe258fec13df70981292b8 Mon Sep 17 00:00:00 2001 From: Bilal Date: Mon, 20 Jul 2020 14:36:04 +0200 Subject: [PATCH 1/6] use full live stream url --- app/assets/javascripts/channels/broadcasts.coffee | 2 +- app/channels/broadcasts_channel.rb | 1 + app/controllers/stream_notifications_controller.rb | 8 +++++++- app/models/broadcast.rb | 4 ++++ ...309_add_full_live_stream_playback_url_to_broadcasts.rb | 5 +++++ 5 files changed, 18 insertions(+), 2 deletions(-) create mode 100644 db/migrate/20200720131309_add_full_live_stream_playback_url_to_broadcasts.rb diff --git a/app/assets/javascripts/channels/broadcasts.coffee b/app/assets/javascripts/channels/broadcasts.coffee index f163bee..823da04 100644 --- a/app/assets/javascripts/channels/broadcasts.coffee +++ b/app/assets/javascripts/channels/broadcasts.coffee @@ -26,7 +26,7 @@ $(document).on "turbolinks:load", -> $("#broadcast_video").html data.video_content new (Clappr.Player)( parentId: '#broadcast_video' - source: data.playback_url + source: data.full_live_stream_playback_url width: '100%', height: '100%', mute: true, diff --git a/app/channels/broadcasts_channel.rb b/app/channels/broadcasts_channel.rb index dd3d941..ad96756 100644 --- a/app/channels/broadcasts_channel.rb +++ b/app/channels/broadcasts_channel.rb @@ -16,6 +16,7 @@ class BroadcastsChannel < ApplicationCable::Channel event: :broadcast_stream_update, status: broadcast.status, playback_url: broadcast.stream_playback_url, + full_live_stream_playback_url: broadcast.full_live_stream_playback_url, video_content: video_content, status_content: status_content, streamer_status: broadcast.streamer_status diff --git a/app/controllers/stream_notifications_controller.rb b/app/controllers/stream_notifications_controller.rb index ce76ff6..e7fdb27 100644 --- a/app/controllers/stream_notifications_controller.rb +++ b/app/controllers/stream_notifications_controller.rb @@ -14,6 +14,11 @@ class StreamNotificationsController < ApplicationController when "video.live_stream.recording" @broadcast.streamer_recording! notify_users + when "video.asset.ready" + full_live_stream_playback_uid = notification.dig(:data, :playback_ids, 0, :id) + @broadcast.full_live_stream_playback_uid = full_live_stream_playback_uid + @broadcast.save + notify_users when "video.live_stream.active" @broadcast.active! notify_users @@ -59,7 +64,8 @@ class StreamNotificationsController < ApplicationController end def set_broadcast - if notification_type == "video.asset.static_renditions.ready" + case notification_type + when "video.asset.static_renditions.ready", "video.asset.ready" live_stream_id = notification.dig(:stream_notification, :data, :live_stream_id) @broadcast = Broadcast.find_by(stream_uid: live_stream_id) else diff --git a/app/models/broadcast.rb b/app/models/broadcast.rb index 564fd41..a9c9041 100644 --- a/app/models/broadcast.rb +++ b/app/models/broadcast.rb @@ -30,6 +30,10 @@ class Broadcast < ApplicationRecord "https://stream.mux.com/#{stream_playback_uid}.m3u8" end + def full_live_stream_playback_url + "https://stream.mux.com/#{full_live_stream_playback_uid}.m3u8" + end + def stream_server_url ENV['MUX_BROADCAST_SERVER_URL'] end diff --git a/db/migrate/20200720131309_add_full_live_stream_playback_url_to_broadcasts.rb b/db/migrate/20200720131309_add_full_live_stream_playback_url_to_broadcasts.rb new file mode 100644 index 0000000..84f78b5 --- /dev/null +++ b/db/migrate/20200720131309_add_full_live_stream_playback_url_to_broadcasts.rb @@ -0,0 +1,5 @@ +class AddFullLiveStreamPlaybackUrlToBroadcasts < ActiveRecord::Migration[6.0] + def change + add_column :broadcasts, :full_live_stream_playback_uid, :string + end +end -- 2.47.3 From 48a25e266decf20db25db9c2db9c649a03076e8a Mon Sep 17 00:00:00 2001 From: Bilal Date: Mon, 20 Jul 2020 14:45:11 +0200 Subject: [PATCH 2/6] add spec --- .../stream_notifications_controller_spec.rb | 65 +++++++++++++------ 1 file changed, 45 insertions(+), 20 deletions(-) diff --git a/spec/controllers/stream_notifications_controller_spec.rb b/spec/controllers/stream_notifications_controller_spec.rb index 8a9211f..9c57e02 100644 --- a/spec/controllers/stream_notifications_controller_spec.rb +++ b/spec/controllers/stream_notifications_controller_spec.rb @@ -4,27 +4,45 @@ RSpec.describe StreamNotificationsController, type: :controller do render_views let!(:broadcast) { create(:broadcast, :with_stream, skip_create_callback: true, name: "Live Stream") } - let(:active_status) { {type: "video.live_stream.active", object: { id: "mux_stream" }} } - let(:disconnected_status) { {type: "video.live_stream.disconnected", object: { id: "mux_stream" }} } - let(:idle_status) { {type: "video.live_stream.idle", object: { id: "mux_stream" }} } - let(:idle_status_for_unknown_broadcast) { {type: "video.live_stream.idle", object: { id: "unknown-id" }} } - let(:asset_ready) { { - type: "video.asset.static_renditions.ready", - object: { id: "asset_uid" }, - data: { - playback_ids: [ - {id: "playback_uid"} - ], - static_renditions: { - files: [{name: "high.mp4"}] - } - }, - stream_notification: { + let(:active_status) { { type: "video.live_stream.active", object: { id: "mux_stream" } } } + let(:disconnected_status) { { type: "video.live_stream.disconnected", object: { id: "mux_stream" } } } + let(:idle_status) { { type: "video.live_stream.idle", object: { id: "mux_stream" } } } + let(:idle_status_for_unknown_broadcast) { { type: "video.live_stream.idle", object: { id: "unknown-id" } } } + let(:asset_ready) do + { + type: "video.asset.static_renditions.ready", + object: { id: "asset_uid" }, data: { - live_stream_id: "mux_stream" + playback_ids: [ + { id: "playback_uid" } + ], + static_renditions: { + files: [{ name: "high.mp4" }] + } + }, + stream_notification: { + data: { + live_stream_id: "mux_stream" + } } } - } } + end + let(:full_live_stream_ready) do + { + type: "video.asset.ready", + object: { id: "active_asset_uid" }, + data: { + playback_ids: [ + { id: "full_live_stream_playback_uid" } + ] + }, + stream_notification: { + data: { + live_stream_id: "mux_stream" + } + } + } + end describe "#create" do before do @@ -54,13 +72,20 @@ RSpec.describe StreamNotificationsController, type: :controller do end it "creates a broadcast recording when static_renditions.ready is received in notification" do - expect { + expect do post :create, params: asset_ready - }.to change(BroadcastRecording, :count).by(1) + end.to change(BroadcastRecording, :count).by(1) expect(BroadcastsChannel).to have_received(:stream_recording_ready) end + it "stores full livestream playback uid and updates the broadcast" do + post :create, params: full_live_stream_ready + + expect(Broadcast.last.full_live_stream_playback_uid).to eq "full_live_stream_playback_uid" + expect(BroadcastsChannel).to have_received(:broadcast_stream_updates).with(be_kind_of(Broadcast)) + end + it "returns OK response even for non-existing broadcast" do post :create, params: idle_status_for_unknown_broadcast -- 2.47.3 From 1a2b6c8be3134d9c1f6a3167276596433bb44f3d Mon Sep 17 00:00:00 2001 From: Bilal Date: Mon, 20 Jul 2020 15:06:20 +0200 Subject: [PATCH 3/6] update specs --- spec/channels/broadcasts_channel_spec.rb | 1 + spec/factories/broadcasts.rb | 1 + 2 files changed, 2 insertions(+) diff --git a/spec/channels/broadcasts_channel_spec.rb b/spec/channels/broadcasts_channel_spec.rb index 2149998..a540e3f 100644 --- a/spec/channels/broadcasts_channel_spec.rb +++ b/spec/channels/broadcasts_channel_spec.rb @@ -24,6 +24,7 @@ RSpec.describe BroadcastsChannel, type: :channel do event: "broadcast_stream_update", status: broadcast.status, playback_url: broadcast.stream_playback_url, + full_live_stream_playback_url: broadcast.full_live_stream_playback_url, status_content: status_content, video_content: video_content, streamer_status: broadcast.streamer_status diff --git a/spec/factories/broadcasts.rb b/spec/factories/broadcasts.rb index 0784d1d..3b022c2 100644 --- a/spec/factories/broadcasts.rb +++ b/spec/factories/broadcasts.rb @@ -11,6 +11,7 @@ FactoryBot.define do stream_uid "mux_stream" stream_key "mux_key" stream_playback_uid "mux_playback_id" + full_live_stream_playback_uid "full_live_stream_playback_uid" status "created" streamer_status "idle" end -- 2.47.3 From 209da288dc3843d32daabe075f5f37ed567bcca6 Mon Sep 17 00:00:00 2001 From: Bilal Date: Tue, 21 Jul 2020 15:54:06 +0200 Subject: [PATCH 4/6] fix MR comments --- app/controllers/stream_notifications_controller.rb | 3 +-- app/models/broadcast.rb | 2 ++ 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/app/controllers/stream_notifications_controller.rb b/app/controllers/stream_notifications_controller.rb index e7fdb27..76a77c8 100644 --- a/app/controllers/stream_notifications_controller.rb +++ b/app/controllers/stream_notifications_controller.rb @@ -16,8 +16,7 @@ class StreamNotificationsController < ApplicationController notify_users when "video.asset.ready" full_live_stream_playback_uid = notification.dig(:data, :playback_ids, 0, :id) - @broadcast.full_live_stream_playback_uid = full_live_stream_playback_uid - @broadcast.save + @broadcast.update(full_live_stream_playback_uid: full_live_stream_playback_uid) notify_users when "video.live_stream.active" @broadcast.active! diff --git a/app/models/broadcast.rb b/app/models/broadcast.rb index a9c9041..2759933 100644 --- a/app/models/broadcast.rb +++ b/app/models/broadcast.rb @@ -31,6 +31,8 @@ class Broadcast < ApplicationRecord end def full_live_stream_playback_url + return stream_playback_url if full_live_stream_playback_uid.blank? + "https://stream.mux.com/#{full_live_stream_playback_uid}.m3u8" end -- 2.47.3 From 7089bef0d3a0bf66bf4696ccd2ad82c4f858d830 Mon Sep 17 00:00:00 2001 From: Bilal Date: Tue, 21 Jul 2020 16:26:02 +0200 Subject: [PATCH 5/6] fix MR comments --- app/models/broadcast.rb | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/app/models/broadcast.rb b/app/models/broadcast.rb index 2759933..d1f6943 100644 --- a/app/models/broadcast.rb +++ b/app/models/broadcast.rb @@ -31,9 +31,7 @@ class Broadcast < ApplicationRecord end def full_live_stream_playback_url - return stream_playback_url if full_live_stream_playback_uid.blank? - - "https://stream.mux.com/#{full_live_stream_playback_uid}.m3u8" + full_live_stream_playback_uid.blank? ? stream_playback_url : "https://stream.mux.com/#{full_live_stream_playback_uid}.m3u8" end def stream_server_url -- 2.47.3 From b3747f51fc8ecb0cdc72c346d6d9f30e60a5f68d Mon Sep 17 00:00:00 2001 From: Bilal Date: Wed, 22 Jul 2020 13:48:47 +0200 Subject: [PATCH 6/6] rebase --- db/structure.sql | 20 ++++---------------- 1 file changed, 4 insertions(+), 16 deletions(-) diff --git a/db/structure.sql b/db/structure.sql index a9d869f..a10feff 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -9,20 +9,6 @@ SET xmloption = content; SET client_min_messages = warning; SET row_security = off; --- --- Name: plpgsql; Type: EXTENSION; Schema: -; Owner: - --- - -CREATE EXTENSION IF NOT EXISTS plpgsql WITH SCHEMA pg_catalog; - - --- --- Name: EXTENSION plpgsql; Type: COMMENT; Schema: -; Owner: - --- - -COMMENT ON EXTENSION plpgsql IS 'PL/pgSQL procedural language'; - - -- -- Name: fuzzystrmatch; Type: EXTENSION; Schema: -; Owner: - -- @@ -554,7 +540,8 @@ CREATE TABLE public.broadcasts ( stream_playback_uid character varying, token character varying, streamer_status integer DEFAULT 0, - shoot_location_time_zone character varying DEFAULT 'UTC'::character varying + shoot_location_time_zone character varying DEFAULT 'UTC'::character varying, + full_live_stream_playback_uid character varying ); @@ -3954,6 +3941,7 @@ INSERT INTO "schema_migrations" (version) VALUES ('20200716094927'), ('20200716103525'), ('20200716105723'), -('20200720051634'); +('20200720051634'), +('20200720131309'); -- 2.47.3