70
Cloud Spin Bret McGowen Google @bretmcg Learning about the cloud by dodging bullets, Matrix-style

Cloud Spin - building a photo booth with the Google Cloud Platform

Embed Size (px)

Citation preview

Page 1: Cloud Spin - building a photo booth with the Google Cloud Platform

Cloud Spin

Bret McGowenGoogle@bretmcg

Learning about the cloud by dodging bullets, Matrix-style

Page 2: Cloud Spin - building a photo booth with the Google Cloud Platform

Bret McGowen

Developer Advocate, GoogleGoogle Cloud PlatformNew York, NY

@bretmcg

Howdy DevFest MN!

Page 3: Cloud Spin - building a photo booth with the Google Cloud Platform

I'm a fake startup CEO

Page 4: Cloud Spin - building a photo booth with the Google Cloud Platform

Fake startup, real problems

Page 5: Cloud Spin - building a photo booth with the Google Cloud Platform

3 weeks to minimum viable product

Page 6: Cloud Spin - building a photo booth with the Google Cloud Platform
Page 7: Cloud Spin - building a photo booth with the Google Cloud Platform
Page 8: Cloud Spin - building a photo booth with the Google Cloud Platform
Page 9: Cloud Spin - building a photo booth with the Google Cloud Platform

@bretmcg @googlecloud @googlecloudspin

Page 10: Cloud Spin - building a photo booth with the Google Cloud Platform

The user steps into the center of the Cloud Spin

area

Page 11: Cloud Spin - building a photo booth with the Google Cloud Platform

The user jumps, and we take a photo from all the

phones

Page 12: Cloud Spin - building a photo booth with the Google Cloud Platform

It's pretty tricky

Page 13: Cloud Spin - building a photo booth with the Google Cloud Platform

● Taking a photo at the same EXACT SAME INSTANT

● Coordinating the phone cameras

● File storage

● Scaling

● Image processing

● Controlling cost

● Create multiple versions

● Debugging & Logging

Problems to solve

Page 14: Cloud Spin - building a photo booth with the Google Cloud Platform

Taking the photos: how would you do it?

Page 15: Cloud Spin - building a photo booth with the Google Cloud Platform

@bretmcg @googlecloud @googlecloudspin

It isn’t that straightforward

● Need to coordinate 19 phones to take photosof the same moment!

● Android - random delay when taking a photo!

Hence impossible to take photos of the same moment

Page 16: Cloud Spin - building a photo booth with the Google Cloud Platform

@bretmcg @googlecloud @googlecloudspin

What we ended up with?

● Record a video

● 30 fps is like burst mode

Page 17: Cloud Spin - building a photo booth with the Google Cloud Platform

@bretmcg @googlecloud @googlecloudspin

More problems

● How do we identify the frames?

● Mark the the frame some how...

● Many Experiments: flash, timestamp

Page 18: Cloud Spin - building a photo booth with the Google Cloud Platform

Use an audio marker!

Page 19: Cloud Spin - building a photo booth with the Google Cloud Platform

@bretmcg @googlecloud @googlecloudspin

Mic-In Into the Phone

MIC

OMTP

MIC

GND

AHJ / CTIA

Page 20: Cloud Spin - building a photo booth with the Google Cloud Platform

@bretmcg @googlecloud @googlecloudspin

Audio can be tricky

Page 21: Cloud Spin - building a photo booth with the Google Cloud Platform

@bretmcg @googlecloud @googlecloudspin

Page 22: Cloud Spin - building a photo booth with the Google Cloud Platform

@bretmcg @googlecloud @googlecloudspin

Finally - Off the shelf components

Page 23: Cloud Spin - building a photo booth with the Google Cloud Platform

@bretmcg @googlecloud @googlecloudspin

Record the audio cue in the video

Page 24: Cloud Spin - building a photo booth with the Google Cloud Platform

How do we coordinate 19 phone cameras?

Page 25: Cloud Spin - building a photo booth with the Google Cloud Platform

No, seriously, how do we coordinate 19 phone cameras?

Page 26: Cloud Spin - building a photo booth with the Google Cloud Platform

@bretmcg @googlecloud @googlecloudspin

What do we need to coordinate?

● Camera app status & error message

● Exposure ISO & shutter speed

● Internal versus external microphone

● Timestamp to start the recording

● And more...

Page 27: Cloud Spin - building a photo booth with the Google Cloud Platform

@bretmcg @googlecloud @googlecloudspin

Let's use Firebase!

● Can power our app's backend

● Stores our data and syncs it in real-time

● JSON NoSQL database with real-time sync

● Updates across devices within milliseconds

Page 28: Cloud Spin - building a photo booth with the Google Cloud Platform

// Set this phone's status on Firebase at /cameras/{cameranumber}Firebase firebaseRef = new Firebase(FIREBASE_BASE_URL);Firebase cameraStatusRef = firebaseRef.child("cameras").child(cameraNumber);

CameraStatus cameraStatus = new CameraStatus();cameraStatus.appVersion = BuildConfig.VERSION_NAME;cameraStatus.externalMic = mExternalMic;...cameraStatus.status = status;

cameraStatusRef.setValue(mCameraStatus);

Android: updating camera status with Firebase

Page 29: Cloud Spin - building a photo booth with the Google Cloud Platform

@bretmcg @googlecloud @googlecloudspin

Page 30: Cloud Spin - building a photo booth with the Google Cloud Platform

@bretmcg @googlecloud @googlecloudspin

Recording video with camera2 API

● camera2 API

● I learned a lot of this API from the android-

Camera2Video GitHub project

Page 31: Cloud Spin - building a photo booth with the Google Cloud Platform

Scaling with Google Cloud Platform

Page 32: Cloud Spin - building a photo booth with the Google Cloud Platform
Page 33: Cloud Spin - building a photo booth with the Google Cloud Platform

Stitching Q

Extraction Q

InputBucket

Stitching Q

Devices

Extraction Q

Notification Processor

Extractor

Stitcher

Video Uploads

Object Change Notifications

Output Bucket

Coordinator

InputBucket

Notification Processor

Extractor

Stitcher

Output Bucket

Page 34: Cloud Spin - building a photo booth with the Google Cloud Platform

@bretmcg @googlecloud @googlecloudspin

Video Uploads and notification processing

InputBucket

Stitching Q

Devices

Extraction Q

Notification Processor

Extractor

Stitcher

Video Uploads

Notification

Output Bucket

Coordinator

InputBucket

Notification Processor

Page 35: Cloud Spin - building a photo booth with the Google Cloud Platform

Cloud Datastore Cloud SQL Cloud Storage

Page 36: Cloud Spin - building a photo booth with the Google Cloud Platform

4.5 trillion! Managed MySQL Cat Videos?Cat Videos?

Jumping

People

Page 37: Cloud Spin - building a photo booth with the Google Cloud Platform

// Credentials for Google Cloud Platform.

Credential credential = ...

Storage storage = new Storage.Builder(httpTransport, jacksonFactory, credential)

.setApplicationName("[email protected]")

.build();

StorageObject storageObject = new StorageObject();

storageObject.setBucket(bucket);

InputStream fileUploadStream = new FileInputStream(videoFile);

try {

InputStreamContent content = new InputStreamContent("video/mp4", fileUploadStream);

Storage.Objects.Insert insert = storage.objects().insert(bucket, null, content);

insert.setName(cloudStoragePath);

insert.execute();

} finally {

fileUploadStream.close();

}

Page 38: Cloud Spin - building a photo booth with the Google Cloud Platform

@bretmcg @googlecloud @googlecloudspin

Object Change Notifications

Cloud Storage Bucket

[myapp-input-bucket]

App Engine

Object Change Notification

ClientAdd/update/remove object

Page 39: Cloud Spin - building a photo booth with the Google Cloud Platform

@bretmcg @googlecloud @googlecloudspin

Edge Caching

Page 40: Cloud Spin - building a photo booth with the Google Cloud Platform

@bretmcg @googlecloud @googlecloudspin

Google App Engine

Choice of Runtimes- Java, Python, Go- PHP- Custom, NodeJS!

Easy to develop- Build and test locally- Focus on App Code- Versioning- Traffic Splitting

Trivial to manage- Fully managed- No patches/updates- 24x7 operation by Google SREs- Autoscale

Page 41: Cloud Spin - building a photo booth with the Google Cloud Platform

@bretmcg @googlecloud @googlecloudspin

Page 42: Cloud Spin - building a photo booth with the Google Cloud Platform

@bretmcg @googlecloud @googlecloudspin

Building Pipelines

InputBucket

Stitching Q

Devices

Extraction Q

Notification Processor

Extractor

Stitcher

Video Uploads

Object Change Notifications

Output Bucket

Coordinator

Page 43: Cloud Spin - building a photo booth with the Google Cloud Platform

Subscriber Subscriber

Pub/Sub Pub/Sub

Push Subscription Pull Subscription

msg ack msg ack

RPC SendRPC Return

Page 44: Cloud Spin - building a photo booth with the Google Cloud Platform

@bretmcg @googlecloud @googlecloudspin

Cloud Pub/Sub (Beta)

Reliable andreal-timemessaging

Designed for Fast Data

Provides reliable, real-time, many-to-many, asynchronous messaging between applications

Send data into processing pipelines and out to other apps, devices and Google Cloud Services

Global by design and highly available. ‘fire-and-forget’ with minimal latency. Redundant forwarding paths

Designed for Google scale

Page 45: Cloud Spin - building a photo booth with the Google Cloud Platform

@bretmcg @googlecloud @googlecloudspin

Pub/Sub

Stitcher

Frames Topic

Extractor

Frame Ready Message

Publishers and Subscribers

Original Stitcher Subscription

Page 46: Cloud Spin - building a photo booth with the Google Cloud Platform

@bretmcg @googlecloud @googlecloudspin

Pub/Sub

Original Stitcher

Frames Topic

Extractor

Frame Ready Message

Publishers and Subscribers

Original Stitcher Subscription

Variation A Stitcher

Variation A Subscription

Variation B Stitcher

Variation B Subscription

Page 47: Cloud Spin - building a photo booth with the Google Cloud Platform

@bretmcg @googlecloud @googlecloudspin

Variations - How Pub/Sub Can Help

Page 48: Cloud Spin - building a photo booth with the Google Cloud Platform

Meanwhile, in our secret startup lab

Page 49: Cloud Spin - building a photo booth with the Google Cloud Platform

Our first prototype was cheap and scrappy

Page 50: Cloud Spin - building a photo booth with the Google Cloud Platform

Like, REALLY scrappy.

Page 51: Cloud Spin - building a photo booth with the Google Cloud Platform

We used anything we could find.

Page 52: Cloud Spin - building a photo booth with the Google Cloud Platform

Like, REALscrappy(selfie sticks!)

Page 53: Cloud Spin - building a photo booth with the Google Cloud Platform

Like, REALscrappy(selfie sticks!)

Page 54: Cloud Spin - building a photo booth with the Google Cloud Platform

Like, REALscrappy(selfie sticks!)

Page 55: Cloud Spin - building a photo booth with the Google Cloud Platform

Extracting and stitching

Page 56: Cloud Spin - building a photo booth with the Google Cloud Platform

@bretmcg @googlecloud @googlecloudspin

Pipeline Processing

InputBucket

Stitching Q

Devices

Extraction Q

Notification Processor

Extractor

Stitcher

Video Uploads

Object Change Notifications

Output Bucket

Coordinator

Extractor

Stitcher

Page 57: Cloud Spin - building a photo booth with the Google Cloud Platform
Page 58: Cloud Spin - building a photo booth with the Google Cloud Platform
Page 59: Cloud Spin - building a photo booth with the Google Cloud Platform

from moviepy.video.io.VideoFileClip import VideoFileClip

import numpy

STD_DEV_SCALAR = 4

def calc_peak_time(clip):

audio_arr = clip.audio.to_soundarray(buffersize=1000)

frames = clip.fps * len(audio_arr) / clip.audio.fps

buckets = numpy.array_split(audio_arr, frames)

mean_arr = [numpy.sqrt(numpy.mean(bucket**2)) for bucket in buckets]

peaks = numpy.where(mean_arr > MEAN_SCALAR * numpy.mean(mean_arr))

if len(peaks) > 0 and len(peaks[0]) > 0:

return peaks[0][0] / float(clip.fps)

else:

return 0.0

Page 60: Cloud Spin - building a photo booth with the Google Cloud Platform

@bretmcg @googlecloud @googlecloudspin

Frames extracted

Page 61: Cloud Spin - building a photo booth with the Google Cloud Platform

@bretmcg @googlecloud @googlecloudspin

Google Compute Engine

Compute Resources- US, Europe and Asia zones- Fast SDN-Based virtual networking

Consistently Fast- Fast VM Provisioning - Consistent Performance

Cost Effective- Sub-Hour Billing- No IOPS charges for Block Storage- VMs not required for Load balancing

Page 62: Cloud Spin - building a photo booth with the Google Cloud Platform

@bretmcg @googlecloud @googlecloudspin

Compute Engine Autoscaler

ManagedInstanceGroup

create/destroy VMs

Instance Group Manager

actuator

monitor

Autoscaler

Cloud Monitoring

utilisation

VMVMVM

Pub/Sub Topic

Page 63: Cloud Spin - building a photo booth with the Google Cloud Platform

@bretmcg @googlecloud @googlecloudspin

Next step: Containerization

Extractor Workers

create/destroy VMs

Instance Group Manager

actuator

monitor

Autoscaler

Cloud Monitoring

utilisation

VMVMContainer

VM

Extract Topic

Page 64: Cloud Spin - building a photo booth with the Google Cloud Platform

@bretmcg @googlecloud @googlecloudspin

Stitching the frames together

Extract Q

Temp Store

ExtractorStitch QExtractorExtractors

READY

Stitcher

Firebase

All READY?

Retrieve Images

Visualizer

Page 65: Cloud Spin - building a photo booth with the Google Cloud Platform
Page 66: Cloud Spin - building a photo booth with the Google Cloud Platform

● Taking a photo at the same EXACT SAME INSTANT - video with audio beep

● Coordinating the phone cameras - Firebase

● File storage - Google Cloud Storage

● Scaling - Cloud Pub/Sub, Compute Engine Instance Groups

● Image processing - Compute Engine & Containers

● Controlling cost - Scaling instance group sizes

● Create multiple versions - Cloud Pub/Sub topics

● Debugging & Logging - Google Cloud Logging

Problems to solve

Page 68: Cloud Spin - building a photo booth with the Google Cloud Platform

@bretmcg @googlecloud @googlecloudspin

Productivity!

We built a lot in just 3 weeks

Focus 100% of time coding

Page 69: Cloud Spin - building a photo booth with the Google Cloud Platform

@bretmcg @googlecloud @googlecloudspin

• Talk with us!• Firebase• Containers• Kubernetes• Big data• BigQuery

Want to know more?

Page 70: Cloud Spin - building a photo booth with the Google Cloud Platform

Bret McGowen@bretmcg@googlecloudspinTHANK YOU