Skip to content

Commit

Permalink
Fix PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
yostyle committed Oct 11, 2022
1 parent 41c8c9a commit dd9cc41
Show file tree
Hide file tree
Showing 11 changed files with 68 additions and 63 deletions.
1 change: 1 addition & 0 deletions Config/BuildSettings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,7 @@ final class BuildSettings: NSObject {
// MARK: - Voice Broadcast
static let voiceBroadcastEnabled = false
static let voiceBroadcastChunkLength: Int = 600
static let voiceBroadcastMaxLength: Int = 144000

// MARK: - MXKAppSettings
static let enableBotCreation: Bool = false
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,15 @@
{
"images" : [
{
"filename" : "action_live.png",
"idiom" : "universal",
"scale" : "1x"
},
{
"filename" : "[email protected]",
"idiom" : "universal",
"scale" : "2x"
},
{
"filename" : "[email protected]",
"idiom" : "universal",
"scale" : "3x"
"filename" : "action_live.svg",
"idiom" : "universal"
}
],
"info" : {
"author" : "xcode",
"version" : 1
},
"properties" : {
"preserves-vector-representation" : true
}
}
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.
1 change: 1 addition & 0 deletions Riot/Modules/Room/RoomViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -2316,6 +2316,7 @@ - (void)setupActions {
((RoomInputToolbarView *) self.inputToolbarView).actionMenuOpened = NO;
}

// TODO: Init and start voice broadcast
MXSession* session = self.roomDataSource.mxSession;
[session getOrCreateVoiceBroadcastServiceFor:self.roomDataSource.room completion:^(VoiceBroadcastService *voiceBroadcastService) {
if (voiceBroadcastService) {
Expand Down
2 changes: 1 addition & 1 deletion Riot/Modules/VoiceBroadcast/MXSession+VoiceBroadcast.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ extension MXSession {

/// Convenient getter to retrieve VoiceBroadcastService associated to the session
@objc var voiceBroadcastService: VoiceBroadcastService? {
return VoiceBroadcastServiceProvider.shared.voiceBroadcastService
return VoiceBroadcastServiceProvider.shared.currentVoiceBroadcastService
}

/// Initialize VoiceBroadcastService
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,13 @@ NS_ASSUME_NONNULL_BEGIN

@interface VoiceBroadcastEventContent : MXJSONModel

/// The voice broadcast state (started - paused - resumed - stopped).
@property (nonatomic) NSString *state;

/// The length of the voice chunks in seconds. Only required on the started state event.
@property (nonatomic) NSInteger chunkLength;

/// The event id of the started voice broadcast info state event.
@property (nonatomic, strong, nullable) NSString* eventId;

- (instancetype)initWithState:(NSString *)state
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ public class VoiceBroadcastService: NSObject {
/// - completion: A closure called when the operation completes. Provides the event id of the event generated on the home server on success.
/// - Returns: a `MXHTTPOperation` instance.
func startVoiceBroadcast(completion: @escaping (MXResponse<String?>) -> Void) -> MXHTTPOperation? {
return sendVoiceBroadcastInfo(state: State.started) { (response) in
return sendVoiceBroadcastInfo(state: State.started) { [weak self] response in
guard let self = self else { return }

switch response {
case .success((let eventIdResponse)):
self.voiceBroadcastInfoEventId = eventIdResponse
Expand Down Expand Up @@ -146,14 +148,14 @@ public class VoiceBroadcastService: NSObject {
voiceBroadcastContent.chunkLength = BuildSettings.voiceBroadcastChunkLength
}


guard let stateEventContent = voiceBroadcastContent.jsonDictionary() as? [String: Any] else {
completion(.failure(VoiceBroadcastServiceError.unknown))
return nil
}


return self.room.sendStateEvent(.custom(VoiceBroadcastSettings.eventType), content: stateEventContent, stateKey: stateKey) { (response) in
return self.room.sendStateEvent(.custom(VoiceBroadcastSettings.eventType), content: stateEventContent, stateKey: stateKey) { [weak self] response in
guard let self = self else { return }

switch response {
case .success(let object):
self.state = state
Expand Down Expand Up @@ -239,32 +241,18 @@ extension VoiceBroadcastService {

// MARK: - Internal room additions
extension MXRoom {
/**
Send a voice broadcast to the room.

- parameters:
- localURL: the local filesystem path of the file to send.
- voiceBroadcastInfoEventId: The id of the voice broadcast info event.
- mimeType: (optional) the mime type of the file. Defaults to `audio/ogg`.
- duration: the length of the voice message in milliseconds
- samples: an array of floating point values normalized to [0, 1]
- threadId: the id of the thread to send the message. nil by default.

This pointer is set to an actual MXEvent object
containing the local created event which should be used to echo the message in
the messages list until the resulting event come through the server sync.
For information, the identifier of the created local event has the prefix
`kMXEventLocalEventIdPrefix`.

You may specify nil for this parameter if you do not want this information.

You may provide your own MXEvent object, in this case only its send state is updated.

- completion: A block object called when the operation completes.
- response: Provides the event id of the event generated on the home server on success.

- returns: a `MXHTTPOperation` instance.
*/

/// Send a voice broadcast to the room.
/// - Parameters:
/// - localURL: the local filesystem path of the file to send.
/// - voiceBroadcastInfoEventId: The id of the voice broadcast info event.
/// - mimeType: (optional) the mime type of the file. Defaults to `audio/ogg`.
/// - duration: the length of the voice message in milliseconds
/// - samples: an array of floating point values normalized to [0, 1]
/// - threadId: the id of the thread to send the message. nil by default.
/// - success: A closure called when the operation is complete.
/// - failure: A closure called when the operation fails.
/// - Returns: a `MXHTTPOperation` instance.
@nonobjc @discardableResult func sendChunkOfVoiceBroadcast(localURL: URL,
voiceBroadcastInfoEventId: String,
mimeType: String?,
Expand Down
51 changes: 32 additions & 19 deletions Riot/Modules/VoiceBroadcast/VoiceBroadcastServiceProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ class VoiceBroadcastServiceProvider {

// MARK: - Properties

// VoiceBroadcastService in the current session
public var voiceBroadcastService: VoiceBroadcastService? = nil
/// VoiceBroadcastService in the current session
public var currentVoiceBroadcastService: VoiceBroadcastService?

// MARK: - Setup

Expand All @@ -35,7 +35,7 @@ class VoiceBroadcastServiceProvider {
// MARK: - Public

public func getOrCreateVoiceBroadcastService(for room: MXRoom, completion: @escaping (VoiceBroadcastService?) -> Void) {
guard let voiceBroadcastService = self.voiceBroadcastService else {
guard let voiceBroadcastService = self.currentVoiceBroadcastService else {
self.setupVoiceBroadcastService(for: room) { voiceBroadcastService in
completion(voiceBroadcastService)
}
Expand All @@ -51,7 +51,7 @@ class VoiceBroadcastServiceProvider {

public func tearDownVoiceBroadcastService() {

self.voiceBroadcastService = nil
self.currentVoiceBroadcastService = nil

MXLog.debug("Stop monitoring voice broadcast recording")
}
Expand All @@ -60,48 +60,61 @@ class VoiceBroadcastServiceProvider {

// MARK: VoiceBroadcastService setup

/// Get latest voice broadcast info in a room
/// - Parameters:
/// - room: The room.
/// - completion: Completion block that will return the lastest voice broadcast info state event of the room.
private func getLastVoiceBroadcastInfo(for room: MXRoom, completion: @escaping (MXEvent?) -> Void) {
room.state { roomState in
completion(roomState?.stateEvents(with: .custom(VoiceBroadcastSettings.eventType))?.last ?? nil)
}
}

private func createVoiceBroadcastService(for room: MXRoom, state: VoiceBroadcastService.State) {

let voiceBroadcastService = VoiceBroadcastService(room: room, state: VoiceBroadcastService.State.stopped)

self.voiceBroadcastService = voiceBroadcastService
self.currentVoiceBroadcastService = voiceBroadcastService

MXLog.debug("Start monitoring voice broadcast recording")
}


/// Setup the voice broadcast service if no service is running locally.
///
/// A voice broadcast service is created in the following cases :
/// - A voice broadcast info state event doesn't exist in the room.
/// - The last voice broadcast info state event doesn't contain a valid content.
/// - The state of the last voice broadcast info state event is stopped.
/// - The state of the last voice broadcast info state event started by the end user is not stopped.
/// This may be due the following situations the application crashed or the voice broadcast has been started from another session.
///
/// - Parameters:
/// - room: The room.
/// - completion: Completion block that will return the voice broadcast service.
private func setupVoiceBroadcastService(for room: MXRoom, completion: @escaping (VoiceBroadcastService?) -> Void) {
self.getLastVoiceBroadcastInfo(for: room) { event in
guard let voiceBroadcastInfoEvent = event else {
self.createVoiceBroadcastService(for: room, state: VoiceBroadcastService.State.stopped)
completion(self.voiceBroadcastService)
completion(self.currentVoiceBroadcastService)
return
}

guard let voiceBroadcastInfoEventContent = VoiceBroadcastEventContent(fromJSON: voiceBroadcastInfoEvent.content) else {
self.createVoiceBroadcastService(for: room, state: VoiceBroadcastService.State.stopped)
completion(self.voiceBroadcastService)
completion(self.currentVoiceBroadcastService)
return
}

if voiceBroadcastInfoEventContent.state == VoiceBroadcastService.State.stopped.rawValue {
self.createVoiceBroadcastService(for: room, state: VoiceBroadcastService.State.stopped)
completion(self.voiceBroadcastService)
completion(self.currentVoiceBroadcastService)
} else if voiceBroadcastInfoEvent.stateKey == room.mxSession.myUserId {
self.createVoiceBroadcastService(for: room, state: VoiceBroadcastService.State(rawValue: voiceBroadcastInfoEventContent.state) ?? VoiceBroadcastService.State.stopped)
completion(self.voiceBroadcastService)
completion(self.currentVoiceBroadcastService)
} else {
completion(nil)
}
}
}

/// Get latest voice broadcast info in a room
/// - Parameters:
/// - roomId: The room id of the room
/// - completion: Give the lastest voice broadcast info of the room.
private func getLastVoiceBroadcastInfo(for room: MXRoom, completion: @escaping (MXEvent?) -> Void) {
room.state { roomState in
completion(roomState?.stateEvents(with: .custom(VoiceBroadcastSettings.eventType))?.last ?? nil)
}
}
}

0 comments on commit dd9cc41

Please sign in to comment.