Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get audio from stream bucket #642

Merged
merged 5 commits into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion noncore/_models/db.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const { sequelize, Sequelize, options } = require('../../common/db')('noncore')
const { coreSequelize, coreOptions } = require('../../common/db')('noncore')

module.exports = { sequelize, Sequelize, options }
module.exports = { sequelize, Sequelize, options, coreSequelize, coreOptions }
8 changes: 6 additions & 2 deletions noncore/_models/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { sequelize, Sequelize, options } = require('./db')
const { sequelize, Sequelize, options, coreSequelize, coreOptions } = require('./db')

const models = {
GuardianSite: require('./guardian-sites/guardian-site')(sequelize, Sequelize),
Expand Down Expand Up @@ -55,11 +55,15 @@ const models = {
ShortLink: require('./misc/shortlink')(sequelize, Sequelize)
}

const coreModels = {
StreamSegment: require('../../core/_models/streams/stream-segment')(coreSequelize, Sequelize)
}

// Create associations
Object.keys(models).forEach(function (modelName) {
if ('associate' in models[modelName]) {
models[modelName].associate(models)
}
})

module.exports = { ...models, sequelize, Sequelize, options }
module.exports = { ...models, sequelize, Sequelize, options, ...coreModels, coreOptions }
7 changes: 3 additions & 4 deletions noncore/_utils/rfcx-mqtt/mqtt-checkin-router.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ function onMessageCheckin (data, messageId) {
.then((checkInObj) => {
return checkInDatabase.syncGuardianPrefs(checkInObj)
})
.then((checkInObj) => {
return checkInDatabase.createDbAudio(checkInObj)
})
.then((checkInObj) => {
return checkInDatabase.createDbScreenShot(checkInObj)
})
Expand All @@ -62,10 +65,6 @@ function onMessageCheckin (data, messageId) {
}
return Promise.resolve(checkInObj)
})
// Create audio db after getting path from ingest
.then((checkInObj) => {
return checkInDatabase.createDbAudio(checkInObj)
})
.then((checkInObj) => {
return checkInDatabase.finalizeCheckIn(checkInObj)
})
Expand Down
2 changes: 1 addition & 1 deletion noncore/_utils/rfcx-mqtt/mqtt-database.js
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ exports.checkInDatabase = {
site_id: checkInObj.db.dbGuardian.site_id,
check_in_id: checkInObj.db.dbCheckIn.id,
sha1_checksum: checkInObj.audio.meta.sha1CheckSum,
url: `s3://${checkInObj.audio.meta.ingestBucket}/${checkInObj.audio.meta.ingestPath}`,
url: null,
encode_duration: checkInObj.audio.meta.encodeDuration,
measured_at: checkInObj.audio.meta.measuredAt,
measured_at_local: moment.tz(checkInObj.audio.meta.measuredAt, (checkInObj.db.dbSite.timezone || 'UTC')).format('YYYY-MM-DDTHH:mm:ss.SSS'),
Expand Down
3 changes: 1 addition & 2 deletions noncore/_utils/rfcx-mqtt/mqtt-streams.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ async function ingestGuardianAudio (checkInObj) {
targetBitrate: checkInObj.audio.meta.bitRate,
sampleRate: checkInObj.audio.meta.sampleRate
})
checkInObj.audio.meta.ingestPath = uploadData.path
checkInObj.audio.meta.ingestBucket = uploadData.bucket

await S3Service.putObject(checkInObj.audio.filePath, uploadData.path, uploadData.bucket)
assetUtils.deleteLocalFileFromFileSystem(checkInObj.audio.filePath)
return checkInObj
Expand Down
19 changes: 12 additions & 7 deletions noncore/views/v1/models/guardian-audio.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ const moment = require('moment-timezone')
const fs = require('fs')
const random = require('../../../../common/crypto/random')
const audioUtils = require('../../../_utils/rfcx-audio').audioUtils
const assetUtils = require('../../../_utils/internal-rfcx/asset-utils').assetUtils
const validation = require('../../../_utils/misc/validation')
const { GuardianSite, Guardian, GuardianAudioFormat } = require('../../../_models')
const { GuardianSite, Guardian, GuardianAudioFormat, StreamSegment } = require('../../../_models')

exports.models = {

Expand All @@ -19,7 +18,7 @@ exports.models = {
{
model: Guardian,
as: 'Guardian',
attributes: ['guid', 'shortname']
attributes: ['guid', 'shortname', 'stream_id']
},
{
model: GuardianAudioFormat,
Expand All @@ -28,7 +27,7 @@ exports.models = {
}
],

guardianAudioFile: function (req, res, dbRow) {
guardianAudioFile: async function (req, res, dbRow) {
const outputFileExtension = req.rfcx.content_type
const outputFileName = dbRow.guid + '.' + outputFileExtension
const isOutputEnhanced = (outputFileExtension === 'mp3')
Expand All @@ -37,8 +36,11 @@ exports.models = {
const queryParams = parsePermittedQueryParams(req.query, clipDurationFull)

// auto-generate the asset filepath if it's not stored in the url column
const segment = await StreamSegment.findOne({ where: { stream_id: dbRow.stream_id, start: dbRow.measured_at } })
const dateTimeString = dbRow.measured_at.toISOString().substr(0, 19).replace(/:/g, '-')
const audioPath = `/${dateTimeString.substr(0, 4)}/${dateTimeString.substr(5, 2)}/${dateTimeString.substr(8, 2)}/${dbRow.stream_id}/${segment.id}.${dbRow.Format.file_extension}`
const audioStorageUrl = (dbRow.url == null)
? 's3://' + process.env.ASSET_BUCKET_AUDIO + assetUtils.getGuardianAssetStoragePath('audio', dbRow.measured_at, dbRow.Guardian.guid, dbRow.Format.file_extension)
? 's3://' + process.env.INGEST_BUCKET + audioPath
: dbRow.url

audioUtils.cacheSourceAudio(audioStorageUrl)
Expand Down Expand Up @@ -80,14 +82,17 @@ exports.models = {
})
},

guardianAudioSpectrogram: function (req, res, dbRow) {
guardianAudioSpectrogram: async function (req, res, dbRow) {
const tmpFilePath = process.env.CACHE_DIRECTORY + 'ffmpeg/' + random.randomString(32)

const queryParams = parsePermittedQueryParams(req.query, (dbRow.capture_sample_count / dbRow.Format.sample_rate))

// auto-generate the asset filepath if it's not stored in the url column
const segment = await StreamSegment.findOne({ where: { stream_id: dbRow.stream_id, start: dbRow.measured_at } })
const dateTimeString = dbRow.measured_at.toISOString().substr(0, 19).replace(/:/g, '-')
const audioPath = `/${dateTimeString.substr(0, 4)}/${dateTimeString.substr(5, 2)}/${dateTimeString.substr(8, 2)}/${dbRow.stream_id}/${segment.id}.${dbRow.Format.file_extension}`
const audioStorageUrl = (dbRow.url == null)
? 's3://' + process.env.ASSET_BUCKET_AUDIO + assetUtils.getGuardianAssetStoragePath('audio', dbRow.measured_at, dbRow.Guardian.guid, dbRow.Format.file_extension)
? 's3://' + process.env.INGEST_BUCKET + audioPath
: dbRow.url

audioUtils.cacheSourceAudio(audioStorageUrl)
Expand Down
Loading