diff --git a/custom/live-streaming/.babelrc b/custom/live-streaming/.babelrc new file mode 100644 index 0000000..a6f4434 --- /dev/null +++ b/custom/live-streaming/.babelrc @@ -0,0 +1,4 @@ +{ + "presets": ["next/babel"], + "plugins": ["inline-react-svg"] +} diff --git a/custom/live-streaming/README.md b/custom/live-streaming/README.md new file mode 100644 index 0000000..e8da111 --- /dev/null +++ b/custom/live-streaming/README.md @@ -0,0 +1,41 @@ +# Live Streaming + +![Live Streaming](./image.png) + +### Live example + +**[See it in action here ➡️](https://custom-live-streaming.vercel.app)** + +--- + +## What does this demo do? + +- Use [startLiveStreaming](https://docs.daily.co/reference#%EF%B8%8F-startlivestreaming) to send video and audio to specified RTMP endpoint +- Listen for stream started / stopped / error events +- Allows call owner to specify stream layout (grid, single participant or active speaker) and maximum cams +- Extends the basic call demo with a live streaming provider, tray button and modal +- Show a notification bubble at the top of the screen when live streaming is in progress + +Please note: this demo is not currently mobile optimised + +### Getting started + +``` +# set both DAILY_API_KEY and DAILY_DOMAIN +mv env.example .env.local + +yarn +yarn workspace @custom/live-streaming dev +``` + +## How does this example work? + +In this example we extend the [basic call demo](../basic-call) with live streaming functionality. + +We pass a custom tray object, a custom app object (wrapping the original in a new `LiveStreamingProvider`) and a custom modal. We also symlink both the `public` and `pages/api` folders from the basic call. + +Single live streaming is only available to call owners, you must create a token when joining the call (for simplicity, we have disabled the abiltiy to join the call as a guest.) + +## Deploy your own on Vercel + +[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/daily-co/clone-flow?repository-url=https%3A%2F%2Fgithub.com%2Fdaily-demos%2Fexamples.git&env=DAILY_DOMAIN%2CDAILY_API_KEY&envDescription=Your%20Daily%20domain%20and%20API%20key%20can%20be%20found%20on%20your%20account%20dashboard&envLink=https%3A%2F%2Fdashboard.daily.co&project-name=daily-examples&repo-name=daily-examples) diff --git a/custom/live-streaming/components/App.js b/custom/live-streaming/components/App.js new file mode 100644 index 0000000..cd47217 --- /dev/null +++ b/custom/live-streaming/components/App.js @@ -0,0 +1,13 @@ +import React from 'react'; + +import App from '@custom/basic-call/components/App'; +import { LiveStreamingProvider } from '../contexts/LiveStreamingProvider'; + +// Extend our basic call app component with the live streaming context +export const AppWithLiveStreaming = () => ( + + + +); + +export default AppWithLiveStreaming; diff --git a/custom/live-streaming/components/LiveStreamingModal.js b/custom/live-streaming/components/LiveStreamingModal.js new file mode 100644 index 0000000..698f0d4 --- /dev/null +++ b/custom/live-streaming/components/LiveStreamingModal.js @@ -0,0 +1,148 @@ +import React, { useEffect, useState } from 'react'; +import { Button } from '@custom/shared/components/Button'; +import { CardBody } from '@custom/shared/components/Card'; +import Field from '@custom/shared/components/Field'; +import { TextInput, SelectInput } from '@custom/shared/components/Input'; +import Modal from '@custom/shared/components/Modal'; +import { Well } from '@custom/shared/components/Well'; +import { useCallState } from '@custom/shared/contexts/CallProvider'; +import { useParticipants } from '@custom/shared/contexts/ParticipantsProvider'; +import { useUIState } from '@custom/shared/contexts/UIStateProvider'; +import { useLiveStreaming } from '../contexts/LiveStreamingProvider'; + +export const LIVE_STREAMING_MODAL = 'live-streaming'; + +const LAYOUTS = [ + { label: 'Grid (default)', value: 'default' }, + { label: 'Single participant', value: 'single-participant' }, + { label: 'Active participant', value: 'active-participant' }, +]; + +export const LiveStreamingModal = () => { + const { callObject } = useCallState(); + const { allParticipants } = useParticipants(); + const { currentModals, closeModal } = useUIState(); + const { isStreaming, streamError } = useLiveStreaming(); + const [pending, setPending] = useState(false); + const [rtmpUrl, setRtmpUrl] = useState(''); + const [layout, setLayout] = useState(0); + const [maxCams, setMaxCams] = useState(9); + const [participant, setParticipant] = useState(0); + + useEffect(() => { + // Reset pending state whenever stream state changes + setPending(false); + }, [isStreaming]); + + function startLiveStream() { + setPending(true); + + const opts = + layout === 'single-participant' + ? { session_id: participant.id } + : { max_cam_streams: maxCams }; + callObject.startLiveStreaming({ rtmpUrl, preset: layout, ...opts }); + } + + function stopLiveStreaming() { + setPending(true); + callObject.stopLiveStreaming(); + } + + return ( + closeModal(LIVE_STREAMING_MODAL)} + actions={[ + , + !isStreaming ? ( + + ) : ( + + ), + ]} + > + {streamError && ( + + Unable to start stream. Error message: {streamError} + + )} + + + setLayout(Number(e.target.value))} + value={layout} + > + {LAYOUTS.map((l, i) => ( + + ))} + + + + {layout !== + LAYOUTS.findIndex((l) => l.value === 'single-participant') && ( + + setMaxCams(Number(e.target.value))} + value={maxCams} + > + + + + + + + + + + + + )} + + {layout === + LAYOUTS.findIndex((l) => l.value === 'single-participant') && ( + + setParticipant(e.target.value)} + value={participant} + > + {allParticipants.map((p) => ( + + ))} + + + )} + + + setRtmpUrl(e.target.value)} + /> + + + + ); +}; + +export default LiveStreamingModal; diff --git a/custom/live-streaming/components/Tray.js b/custom/live-streaming/components/Tray.js new file mode 100644 index 0000000..baee790 --- /dev/null +++ b/custom/live-streaming/components/Tray.js @@ -0,0 +1,25 @@ +import React from 'react'; + +import { TrayButton } from '@custom/shared/components/Tray'; +import { useUIState } from '@custom/shared/contexts/UIStateProvider'; +import { ReactComponent as IconStream } from '@custom/shared/icons/streaming-md.svg'; + +import { useLiveStreaming } from '../contexts/LiveStreamingProvider'; +import { LIVE_STREAMING_MODAL } from './LiveStreamingModal'; + +export const Tray = () => { + const { openModal } = useUIState(); + const { isStreaming } = useLiveStreaming(); + + return ( + openModal(LIVE_STREAMING_MODAL)} + > + + + ); +}; + +export default Tray; diff --git a/custom/live-streaming/contexts/LiveStreamingProvider.js b/custom/live-streaming/contexts/LiveStreamingProvider.js new file mode 100644 index 0000000..6e940c8 --- /dev/null +++ b/custom/live-streaming/contexts/LiveStreamingProvider.js @@ -0,0 +1,71 @@ +import React, { + useState, + createContext, + useContext, + useEffect, + useCallback, +} from 'react'; +import { useCallState } from '@custom/shared/contexts/CallProvider'; +import { useUIState } from '@custom/shared/contexts/UIStateProvider'; +import PropTypes from 'prop-types'; + +export const LiveStreamingContext = createContext(); + +export const LiveStreamingProvider = ({ children }) => { + const [isStreaming, setIsStreaming] = useState(false); + const [streamError, setStreamError] = useState(); + const { setCustomCapsule } = useUIState(); + const { callObject } = useCallState(); + + const handleStreamStarted = useCallback(() => { + console.log('📺 Live stream started'); + setIsStreaming(true); + setStreamError(null); + setCustomCapsule({ variant: 'recording', label: 'Live streaming' }); + }, [setCustomCapsule]); + + const handleStreamStopped = useCallback(() => { + console.log('📺 Live stream stopped'); + setIsStreaming(false); + setCustomCapsule(null); + }, [setCustomCapsule]); + + const handleStreamError = useCallback( + (e) => { + setIsStreaming(false); + setCustomCapsule(null); + setStreamError(e.errorMsg); + }, + [setCustomCapsule] + ); + + useEffect(() => { + if (!callObject) { + return false; + } + + console.log('📺 Live streaming provider listening for stream events'); + + callObject.on('live-streaming-started', handleStreamStarted); + callObject.on('live-streaming-stopped', handleStreamStopped); + callObject.on('live-streaming-error', handleStreamError); + + return () => { + callObject.off('live-streaming-started', handleStreamStarted); + callObject.off('live-streaming-stopped', handleStreamStopped); + callObject.on('live-streaming-error', handleStreamError); + }; + }, [callObject, handleStreamStarted, handleStreamStopped, handleStreamError]); + + return ( + + {children} + + ); +}; + +LiveStreamingProvider.propTypes = { + children: PropTypes.node, +}; + +export const useLiveStreaming = () => useContext(LiveStreamingContext); diff --git a/custom/live-streaming/env.example b/custom/live-streaming/env.example new file mode 100644 index 0000000..5ab7e03 --- /dev/null +++ b/custom/live-streaming/env.example @@ -0,0 +1,8 @@ +# Domain excluding 'https://' and 'daily.co' e.g. 'somedomain' +DAILY_DOMAIN= + +# Obtained from https://dashboard.daily.co/developers +DAILY_API_KEY= + +# Daily REST API endpoint +DAILY_REST_DOMAIN=https://api.daily.co/v1 diff --git a/custom/live-streaming/image.png b/custom/live-streaming/image.png new file mode 100644 index 0000000..9781261 Binary files /dev/null and b/custom/live-streaming/image.png differ diff --git a/custom/live-streaming/index.js b/custom/live-streaming/index.js new file mode 100644 index 0000000..9044efc --- /dev/null +++ b/custom/live-streaming/index.js @@ -0,0 +1 @@ +// Note: I am here because next-transpile-modules requires a mainfile diff --git a/custom/live-streaming/next.config.js b/custom/live-streaming/next.config.js new file mode 100644 index 0000000..9140e28 --- /dev/null +++ b/custom/live-streaming/next.config.js @@ -0,0 +1,13 @@ +const withPlugins = require('next-compose-plugins'); +const withTM = require('next-transpile-modules')([ + '@custom/shared', + '@custom/basic-call', +]); + +const packageJson = require('./package.json'); + +module.exports = withPlugins([withTM], { + env: { + PROJECT_TITLE: packageJson.description, + }, +}); diff --git a/custom/live-streaming/package.json b/custom/live-streaming/package.json new file mode 100644 index 0000000..e89298e --- /dev/null +++ b/custom/live-streaming/package.json @@ -0,0 +1,25 @@ +{ + "name": "@custom/live-streaming", + "description": "Basic Call + Live Streaming", + "version": "0.1.0", + "private": true, + "scripts": { + "dev": "next dev", + "build": "next build", + "start": "next start", + "lint": "next lint" + }, + "dependencies": { + "@custom/shared": "*", + "@custom/basic-call": "*", + "next": "^11.0.0", + "pluralize": "^8.0.0", + "react": "^17.0.2", + "react-dom": "^17.0.2" + }, + "devDependencies": { + "babel-plugin-module-resolver": "^4.1.0", + "next-compose-plugins": "^2.2.1", + "next-transpile-modules": "^8.0.0" + } +} diff --git a/custom/live-streaming/pages/_app.js b/custom/live-streaming/pages/_app.js new file mode 100644 index 0000000..4cc8b4b --- /dev/null +++ b/custom/live-streaming/pages/_app.js @@ -0,0 +1,12 @@ +import React from 'react'; +import App from '@custom/basic-call/pages/_app'; +import AppWithLiveStreaming from '../components/App'; + +import { LiveStreamingModal } from '../components/LiveStreamingModal'; +import Tray from '../components/Tray'; + +App.modals = [LiveStreamingModal]; +App.customAppComponent = ; +App.customTrayComponent = ; + +export default App; diff --git a/custom/live-streaming/pages/api b/custom/live-streaming/pages/api new file mode 120000 index 0000000..999f604 --- /dev/null +++ b/custom/live-streaming/pages/api @@ -0,0 +1 @@ +../../basic-call/pages/api \ No newline at end of file diff --git a/custom/live-streaming/pages/index.js b/custom/live-streaming/pages/index.js new file mode 100644 index 0000000..f61b429 --- /dev/null +++ b/custom/live-streaming/pages/index.js @@ -0,0 +1,16 @@ +import Index from '@custom/basic-call/pages'; +import getDemoProps from '@custom/shared/lib/demoProps'; + +export async function getStaticProps() { + const defaultProps = getDemoProps(); + + return { + props: { + ...defaultProps, + forceFetchToken: true, + forceOwner: true, + }, + }; +} + +export default Index; diff --git a/custom/live-streaming/public/assets/daily-logo-dark.svg b/custom/live-streaming/public/assets/daily-logo-dark.svg new file mode 100644 index 0000000..ef3a565 --- /dev/null +++ b/custom/live-streaming/public/assets/daily-logo-dark.svg @@ -0,0 +1,14 @@ + + + + + + + + + + + + + + diff --git a/custom/live-streaming/public/assets/daily-logo.svg b/custom/live-streaming/public/assets/daily-logo.svg new file mode 100644 index 0000000..534a18a --- /dev/null +++ b/custom/live-streaming/public/assets/daily-logo.svg @@ -0,0 +1,14 @@ + + + + + + + + + + + + + + diff --git a/custom/live-streaming/public/assets/join.mp3 b/custom/live-streaming/public/assets/join.mp3 new file mode 100644 index 0000000..7657915 Binary files /dev/null and b/custom/live-streaming/public/assets/join.mp3 differ diff --git a/custom/live-streaming/public/assets/message.mp3 b/custom/live-streaming/public/assets/message.mp3 new file mode 100644 index 0000000..a067315 Binary files /dev/null and b/custom/live-streaming/public/assets/message.mp3 differ diff --git a/custom/live-streaming/public/assets/pattern-bg.png b/custom/live-streaming/public/assets/pattern-bg.png new file mode 100644 index 0000000..01e0d0d Binary files /dev/null and b/custom/live-streaming/public/assets/pattern-bg.png differ